OSDN Git Service

2010-05-25 Shujing Zhao <pearly.zhao@oracle.com>
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic.h"
34 #include "toplev.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-common.h"
39 #include "plugin.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 GTY(()) tree_check {
51   /* The value associated with the token.  */
52   tree value;
53   /* The checks that have been associated with value.  */
54   VEC (deferred_access_check, gc)* checks;
55   /* The token's qualifying scope (used when it is a
56      CPP_NESTED_NAME_SPECIFIER).  */
57   tree qualifying_scope;
58 };
59
60 /* A C++ token.  */
61
62 typedef struct GTY (()) cp_token {
63   /* The kind of token.  */
64   ENUM_BITFIELD (cpp_ttype) type : 8;
65   /* If this token is a keyword, this value indicates which keyword.
66      Otherwise, this value is RID_MAX.  */
67   ENUM_BITFIELD (rid) keyword : 8;
68   /* Token flags.  */
69   unsigned char flags;
70   /* Identifier for the pragma.  */
71   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
72   /* True if this token is from a context where it is implicitly extern "C" */
73   BOOL_BITFIELD implicit_extern_c : 1;
74   /* True for a CPP_NAME token that is not a keyword (i.e., for which
75      KEYWORD is RID_MAX) iff this name was looked up and found to be
76      ambiguous.  An error has already been reported.  */
77   BOOL_BITFIELD ambiguous_p : 1;
78   /* The location at which this token was found.  */
79   location_t location;
80   /* The value associated with this token, if any.  */
81   union cp_token_value {
82     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
83     struct tree_check* GTY((tag ("1"))) tree_check_value;
84     /* Use for all other tokens.  */
85     tree GTY((tag ("0"))) value;
86   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 } cp_token;
88
89 /* We use a stack of token pointer for saving token sets.  */
90 typedef struct cp_token *cp_token_position;
91 DEF_VEC_P (cp_token_position);
92 DEF_VEC_ALLOC_P (cp_token_position,heap);
93
94 static cp_token eof_token =
95 {
96   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
97 };
98
99 /* The cp_lexer structure represents the C++ lexer.  It is responsible
100    for managing the token stream from the preprocessor and supplying
101    it to the parser.  Tokens are never added to the cp_lexer after
102    it is created.  */
103
104 typedef struct GTY (()) cp_lexer {
105   /* The memory allocated for the buffer.  NULL if this lexer does not
106      own the token buffer.  */
107   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
108   /* If the lexer owns the buffer, this is the number of tokens in the
109      buffer.  */
110   size_t buffer_length;
111
112   /* A pointer just past the last available token.  The tokens
113      in this lexer are [buffer, last_token).  */
114   cp_token_position GTY ((skip)) last_token;
115
116   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
117      no more available tokens.  */
118   cp_token_position GTY ((skip)) next_token;
119
120   /* A stack indicating positions at which cp_lexer_save_tokens was
121      called.  The top entry is the most recent position at which we
122      began saving tokens.  If the stack is non-empty, we are saving
123      tokens.  */
124   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
125
126   /* The next lexer in a linked list of lexers.  */
127   struct cp_lexer *next;
128
129   /* True if we should output debugging information.  */
130   bool debugging_p;
131
132   /* True if we're in the context of parsing a pragma, and should not
133      increment past the end-of-line marker.  */
134   bool in_pragma;
135 } cp_lexer;
136
137 /* cp_token_cache is a range of tokens.  There is no need to represent
138    allocate heap memory for it, since tokens are never removed from the
139    lexer's array.  There is also no need for the GC to walk through
140    a cp_token_cache, since everything in here is referenced through
141    a lexer.  */
142
143 typedef struct GTY(()) cp_token_cache {
144   /* The beginning of the token range.  */
145   cp_token * GTY((skip)) first;
146
147   /* Points immediately after the last token in the range.  */
148   cp_token * GTY ((skip)) last;
149 } cp_token_cache;
150
151 /* The various kinds of non integral constant we encounter. */
152 typedef enum non_integral_constant {
153   /* floating-point literal */
154   NIC_FLOAT,
155   /* %<this%> */
156   NIC_THIS,
157   /* %<__FUNCTION__%> */
158   NIC_FUNC_NAME,
159   /* %<__PRETTY_FUNCTION__%> */
160   NIC_PRETTY_FUNC,
161   /* %<__func__%> */
162   NIC_C99_FUNC,
163   /* "%<va_arg%> */
164   NIC_VA_ARG,
165   /* a cast */
166   NIC_CAST,
167   /* %<typeid%> operator */
168   NIC_TYPEID,
169   /* non-constant compound literals */
170   NIC_NCC,
171   /* a function call */
172   NIC_FUNC_CALL,
173   /* an increment */
174   NIC_INC,
175   /* an decrement */
176   NIC_DEC,
177   /* an array reference */
178   NIC_ARRAY_REF,
179   /* %<->%> */
180   NIC_ARROW,
181   /* %<.%> */
182   NIC_POINT,
183   /* the address of a label */
184   NIC_ADDR_LABEL,
185   /* %<*%> */
186   NIC_STAR,
187   /* %<&%> */
188   NIC_ADDR,
189   /* %<++%> */
190   NIC_PREINCREMENT,
191   /* %<--%> */
192   NIC_PREDECREMENT,
193   /* %<new%> */
194   NIC_NEW,
195   /* %<delete%> */
196   NIC_DEL,
197   /* calls to overloaded operators */
198   NIC_OVERLOADED,
199   /* an assignment */
200   NIC_ASSIGNMENT,
201   /* a comma operator */
202   NIC_COMMA,
203   /* a call to a constructor */
204   NIC_CONSTRUCTOR
205 } non_integral_constant;
206
207 /* The various kinds of errors about name-lookup failing. */
208 typedef enum name_lookup_error {
209   /* NULL */
210   NLE_NULL,
211   /* is not a type */
212   NLE_TYPE,
213   /* is not a class or namespace */
214   NLE_CXX98,
215   /* is not a class, namespace, or enumeration */
216   NLE_NOT_CXX98
217 } name_lookup_error;
218
219 /* The various kinds of required token */
220 typedef enum required_token {
221   RT_SEMICOLON = 1,  /* ';' */
222   RT_OPEN_PAREN, /* '(' */
223   RT_CLOSE_BRACE, /* '}' */
224   RT_OPEN_BRACE,  /* '{' */
225   RT_CLOSE_SQUARE, /* ']' */
226   RT_OPEN_SQUARE,  /* '[' */
227   RT_COMMA, /* ',' */
228   RT_SCOPE, /* '::' */
229   RT_LESS, /* '<' */
230   RT_GREATER, /* '>' */
231   RT_EQ, /* '=' */
232   RT_ELLIPSIS, /* '...' */
233   RT_MULT, /* '*' */
234   RT_COMPL, /* '~' */
235   RT_COLON, /* ':' */
236   RT_COLON_SCOPE, /* ':' or '::' */
237   RT_CLOSE_PAREN, /* ')' */
238   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
239   RT_PRAGMA_EOL, /* end of line */
240   RT_NAME, /* identifier */
241
242   /* The type is CPP_KEYWORD */
243   RT_NEW, /* new */
244   RT_DELETE, /* delete */
245   RT_RETURN, /* return */
246   RT_WHILE, /* while */
247   RT_EXTERN, /* extern */
248   RT_STATIC_ASSERT, /* static_assert */
249   RT_DECLTYPE, /* decltype */
250   RT_OPERATOR, /* operator */
251   RT_CLASS, /* class */
252   RT_TEMPLATE, /* template */
253   RT_NAMESPACE, /* namespace */
254   RT_USING, /* using */
255   RT_ASM, /* asm */
256   RT_TRY, /* try */
257   RT_CATCH, /* catch */
258   RT_THROW, /* throw */
259   RT_LABEL, /* __label__ */
260   RT_AT_TRY, /* @try */
261   RT_AT_SYNCHRONIZED, /* @synchronized */
262   RT_AT_THROW, /* @throw */
263
264   RT_SELECT,  /* selection-statement */
265   RT_INTERATION, /* iteration-statement */
266   RT_JUMP, /* jump-statement */
267   RT_CLASS_KEY, /* class-key */
268   RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
269 } required_token;
270
271 /* Prototypes.  */
272
273 static cp_lexer *cp_lexer_new_main
274   (void);
275 static cp_lexer *cp_lexer_new_from_tokens
276   (cp_token_cache *tokens);
277 static void cp_lexer_destroy
278   (cp_lexer *);
279 static int cp_lexer_saving_tokens
280   (const cp_lexer *);
281 static cp_token_position cp_lexer_token_position
282   (cp_lexer *, bool);
283 static cp_token *cp_lexer_token_at
284   (cp_lexer *, cp_token_position);
285 static void cp_lexer_get_preprocessor_token
286   (cp_lexer *, cp_token *);
287 static inline cp_token *cp_lexer_peek_token
288   (cp_lexer *);
289 static cp_token *cp_lexer_peek_nth_token
290   (cp_lexer *, size_t);
291 static inline bool cp_lexer_next_token_is
292   (cp_lexer *, enum cpp_ttype);
293 static bool cp_lexer_next_token_is_not
294   (cp_lexer *, enum cpp_ttype);
295 static bool cp_lexer_next_token_is_keyword
296   (cp_lexer *, enum rid);
297 static cp_token *cp_lexer_consume_token
298   (cp_lexer *);
299 static void cp_lexer_purge_token
300   (cp_lexer *);
301 static void cp_lexer_purge_tokens_after
302   (cp_lexer *, cp_token_position);
303 static void cp_lexer_save_tokens
304   (cp_lexer *);
305 static void cp_lexer_commit_tokens
306   (cp_lexer *);
307 static void cp_lexer_rollback_tokens
308   (cp_lexer *);
309 #ifdef ENABLE_CHECKING
310 static void cp_lexer_print_token
311   (FILE *, cp_token *);
312 static inline bool cp_lexer_debugging_p
313   (cp_lexer *);
314 static void cp_lexer_start_debugging
315   (cp_lexer *) ATTRIBUTE_UNUSED;
316 static void cp_lexer_stop_debugging
317   (cp_lexer *) ATTRIBUTE_UNUSED;
318 #else
319 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
320    about passing NULL to functions that require non-NULL arguments
321    (fputs, fprintf).  It will never be used, so all we need is a value
322    of the right type that's guaranteed not to be NULL.  */
323 #define cp_lexer_debug_stream stdout
324 #define cp_lexer_print_token(str, tok) (void) 0
325 #define cp_lexer_debugging_p(lexer) 0
326 #endif /* ENABLE_CHECKING */
327
328 static cp_token_cache *cp_token_cache_new
329   (cp_token *, cp_token *);
330
331 static void cp_parser_initial_pragma
332   (cp_token *);
333
334 /* Manifest constants.  */
335 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
336 #define CP_SAVED_TOKEN_STACK 5
337
338 /* A token type for keywords, as opposed to ordinary identifiers.  */
339 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
340
341 /* A token type for template-ids.  If a template-id is processed while
342    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
343    the value of the CPP_TEMPLATE_ID is whatever was returned by
344    cp_parser_template_id.  */
345 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
346
347 /* A token type for nested-name-specifiers.  If a
348    nested-name-specifier is processed while parsing tentatively, it is
349    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
350    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
351    cp_parser_nested_name_specifier_opt.  */
352 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
353
354 /* A token type for tokens that are not tokens at all; these are used
355    to represent slots in the array where there used to be a token
356    that has now been deleted.  */
357 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
358
359 /* The number of token types, including C++-specific ones.  */
360 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
361
362 /* Variables.  */
363
364 #ifdef ENABLE_CHECKING
365 /* The stream to which debugging output should be written.  */
366 static FILE *cp_lexer_debug_stream;
367 #endif /* ENABLE_CHECKING */
368
369 /* Nonzero if we are parsing an unevaluated operand: an operand to
370    sizeof, typeof, or alignof.  */
371 int cp_unevaluated_operand;
372
373 /* Create a new main C++ lexer, the lexer that gets tokens from the
374    preprocessor.  */
375
376 static cp_lexer *
377 cp_lexer_new_main (void)
378 {
379   cp_token first_token;
380   cp_lexer *lexer;
381   cp_token *pos;
382   size_t alloc;
383   size_t space;
384   cp_token *buffer;
385
386   /* It's possible that parsing the first pragma will load a PCH file,
387      which is a GC collection point.  So we have to do that before
388      allocating any memory.  */
389   cp_parser_initial_pragma (&first_token);
390
391   c_common_no_more_pch ();
392
393   /* Allocate the memory.  */
394   lexer = GGC_CNEW (cp_lexer);
395
396 #ifdef ENABLE_CHECKING
397   /* Initially we are not debugging.  */
398   lexer->debugging_p = false;
399 #endif /* ENABLE_CHECKING */
400   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
401                                    CP_SAVED_TOKEN_STACK);
402
403   /* Create the buffer.  */
404   alloc = CP_LEXER_BUFFER_SIZE;
405   buffer = GGC_NEWVEC (cp_token, alloc);
406
407   /* Put the first token in the buffer.  */
408   space = alloc;
409   pos = buffer;
410   *pos = first_token;
411
412   /* Get the remaining tokens from the preprocessor.  */
413   while (pos->type != CPP_EOF)
414     {
415       pos++;
416       if (!--space)
417         {
418           space = alloc;
419           alloc *= 2;
420           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
421           pos = buffer + space;
422         }
423       cp_lexer_get_preprocessor_token (lexer, pos);
424     }
425   lexer->buffer = buffer;
426   lexer->buffer_length = alloc - space;
427   lexer->last_token = pos;
428   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
429
430   /* Subsequent preprocessor diagnostics should use compiler
431      diagnostic functions to get the compiler source location.  */
432   done_lexing = true;
433
434   gcc_assert (lexer->next_token->type != CPP_PURGED);
435   return lexer;
436 }
437
438 /* Create a new lexer whose token stream is primed with the tokens in
439    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
440
441 static cp_lexer *
442 cp_lexer_new_from_tokens (cp_token_cache *cache)
443 {
444   cp_token *first = cache->first;
445   cp_token *last = cache->last;
446   cp_lexer *lexer = GGC_CNEW (cp_lexer);
447
448   /* We do not own the buffer.  */
449   lexer->buffer = NULL;
450   lexer->buffer_length = 0;
451   lexer->next_token = first == last ? &eof_token : first;
452   lexer->last_token = last;
453
454   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
455                                    CP_SAVED_TOKEN_STACK);
456
457 #ifdef ENABLE_CHECKING
458   /* Initially we are not debugging.  */
459   lexer->debugging_p = false;
460 #endif
461
462   gcc_assert (lexer->next_token->type != CPP_PURGED);
463   return lexer;
464 }
465
466 /* Frees all resources associated with LEXER.  */
467
468 static void
469 cp_lexer_destroy (cp_lexer *lexer)
470 {
471   if (lexer->buffer)
472     ggc_free (lexer->buffer);
473   VEC_free (cp_token_position, heap, lexer->saved_tokens);
474   ggc_free (lexer);
475 }
476
477 /* Returns nonzero if debugging information should be output.  */
478
479 #ifdef ENABLE_CHECKING
480
481 static inline bool
482 cp_lexer_debugging_p (cp_lexer *lexer)
483 {
484   return lexer->debugging_p;
485 }
486
487 #endif /* ENABLE_CHECKING */
488
489 static inline cp_token_position
490 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
491 {
492   gcc_assert (!previous_p || lexer->next_token != &eof_token);
493
494   return lexer->next_token - previous_p;
495 }
496
497 static inline cp_token *
498 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
499 {
500   return pos;
501 }
502
503 /* nonzero if we are presently saving tokens.  */
504
505 static inline int
506 cp_lexer_saving_tokens (const cp_lexer* lexer)
507 {
508   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
509 }
510
511 /* Store the next token from the preprocessor in *TOKEN.  Return true
512    if we reach EOF.  If LEXER is NULL, assume we are handling an
513    initial #pragma pch_preprocess, and thus want the lexer to return
514    processed strings.  */
515
516 static void
517 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
518 {
519   static int is_extern_c = 0;
520
521    /* Get a new token from the preprocessor.  */
522   token->type
523     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
524                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
525   token->keyword = RID_MAX;
526   token->pragma_kind = PRAGMA_NONE;
527
528   /* On some systems, some header files are surrounded by an
529      implicit extern "C" block.  Set a flag in the token if it
530      comes from such a header.  */
531   is_extern_c += pending_lang_change;
532   pending_lang_change = 0;
533   token->implicit_extern_c = is_extern_c > 0;
534
535   /* Check to see if this token is a keyword.  */
536   if (token->type == CPP_NAME)
537     {
538       if (C_IS_RESERVED_WORD (token->u.value))
539         {
540           /* Mark this token as a keyword.  */
541           token->type = CPP_KEYWORD;
542           /* Record which keyword.  */
543           token->keyword = C_RID_CODE (token->u.value);
544         }
545       else
546         {
547           if (warn_cxx0x_compat
548               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
549               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
550             {
551               /* Warn about the C++0x keyword (but still treat it as
552                  an identifier).  */
553               warning (OPT_Wc__0x_compat, 
554                        "identifier %qE will become a keyword in C++0x",
555                        token->u.value);
556
557               /* Clear out the C_RID_CODE so we don't warn about this
558                  particular identifier-turned-keyword again.  */
559               C_SET_RID_CODE (token->u.value, RID_MAX);
560             }
561
562           token->ambiguous_p = false;
563           token->keyword = RID_MAX;
564         }
565     }
566   /* Handle Objective-C++ keywords.  */
567   else if (token->type == CPP_AT_NAME)
568     {
569       token->type = CPP_KEYWORD;
570       switch (C_RID_CODE (token->u.value))
571         {
572         /* Map 'class' to '@class', 'private' to '@private', etc.  */
573         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
574         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
575         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
576         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
577         case RID_THROW: token->keyword = RID_AT_THROW; break;
578         case RID_TRY: token->keyword = RID_AT_TRY; break;
579         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
580         default: token->keyword = C_RID_CODE (token->u.value);
581         }
582     }
583   else if (token->type == CPP_PRAGMA)
584     {
585       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
586       token->pragma_kind = ((enum pragma_kind)
587                             TREE_INT_CST_LOW (token->u.value));
588       token->u.value = NULL_TREE;
589     }
590 }
591
592 /* Update the globals input_location and the input file stack from TOKEN.  */
593 static inline void
594 cp_lexer_set_source_position_from_token (cp_token *token)
595 {
596   if (token->type != CPP_EOF)
597     {
598       input_location = token->location;
599     }
600 }
601
602 /* Return a pointer to the next token in the token stream, but do not
603    consume it.  */
604
605 static inline cp_token *
606 cp_lexer_peek_token (cp_lexer *lexer)
607 {
608   if (cp_lexer_debugging_p (lexer))
609     {
610       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
611       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
612       putc ('\n', cp_lexer_debug_stream);
613     }
614   return lexer->next_token;
615 }
616
617 /* Return true if the next token has the indicated TYPE.  */
618
619 static inline bool
620 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
621 {
622   return cp_lexer_peek_token (lexer)->type == type;
623 }
624
625 /* Return true if the next token does not have the indicated TYPE.  */
626
627 static inline bool
628 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
629 {
630   return !cp_lexer_next_token_is (lexer, type);
631 }
632
633 /* Return true if the next token is the indicated KEYWORD.  */
634
635 static inline bool
636 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
637 {
638   return cp_lexer_peek_token (lexer)->keyword == keyword;
639 }
640
641 /* Return true if the next token is not the indicated KEYWORD.  */
642
643 static inline bool
644 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
645 {
646   return cp_lexer_peek_token (lexer)->keyword != keyword;
647 }
648
649 /* Return true if the next token is a keyword for a decl-specifier.  */
650
651 static bool
652 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
653 {
654   cp_token *token;
655
656   token = cp_lexer_peek_token (lexer);
657   switch (token->keyword) 
658     {
659       /* auto specifier: storage-class-specifier in C++,
660          simple-type-specifier in C++0x.  */
661     case RID_AUTO:
662       /* Storage classes.  */
663     case RID_REGISTER:
664     case RID_STATIC:
665     case RID_EXTERN:
666     case RID_MUTABLE:
667     case RID_THREAD:
668       /* Elaborated type specifiers.  */
669     case RID_ENUM:
670     case RID_CLASS:
671     case RID_STRUCT:
672     case RID_UNION:
673     case RID_TYPENAME:
674       /* Simple type specifiers.  */
675     case RID_CHAR:
676     case RID_CHAR16:
677     case RID_CHAR32:
678     case RID_WCHAR:
679     case RID_BOOL:
680     case RID_SHORT:
681     case RID_INT:
682     case RID_LONG:
683     case RID_SIGNED:
684     case RID_UNSIGNED:
685     case RID_FLOAT:
686     case RID_DOUBLE:
687     case RID_VOID:
688       /* GNU extensions.  */ 
689     case RID_ATTRIBUTE:
690     case RID_TYPEOF:
691       /* C++0x extensions.  */
692     case RID_DECLTYPE:
693       return true;
694
695     default:
696       return false;
697     }
698 }
699
700 /* Return a pointer to the Nth token in the token stream.  If N is 1,
701    then this is precisely equivalent to cp_lexer_peek_token (except
702    that it is not inline).  One would like to disallow that case, but
703    there is one case (cp_parser_nth_token_starts_template_id) where
704    the caller passes a variable for N and it might be 1.  */
705
706 static cp_token *
707 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
708 {
709   cp_token *token;
710
711   /* N is 1-based, not zero-based.  */
712   gcc_assert (n > 0);
713
714   if (cp_lexer_debugging_p (lexer))
715     fprintf (cp_lexer_debug_stream,
716              "cp_lexer: peeking ahead %ld at token: ", (long)n);
717
718   --n;
719   token = lexer->next_token;
720   gcc_assert (!n || token != &eof_token);
721   while (n != 0)
722     {
723       ++token;
724       if (token == lexer->last_token)
725         {
726           token = &eof_token;
727           break;
728         }
729
730       if (token->type != CPP_PURGED)
731         --n;
732     }
733
734   if (cp_lexer_debugging_p (lexer))
735     {
736       cp_lexer_print_token (cp_lexer_debug_stream, token);
737       putc ('\n', cp_lexer_debug_stream);
738     }
739
740   return token;
741 }
742
743 /* Return the next token, and advance the lexer's next_token pointer
744    to point to the next non-purged token.  */
745
746 static cp_token *
747 cp_lexer_consume_token (cp_lexer* lexer)
748 {
749   cp_token *token = lexer->next_token;
750
751   gcc_assert (token != &eof_token);
752   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
753
754   do
755     {
756       lexer->next_token++;
757       if (lexer->next_token == lexer->last_token)
758         {
759           lexer->next_token = &eof_token;
760           break;
761         }
762
763     }
764   while (lexer->next_token->type == CPP_PURGED);
765
766   cp_lexer_set_source_position_from_token (token);
767
768   /* Provide debugging output.  */
769   if (cp_lexer_debugging_p (lexer))
770     {
771       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
772       cp_lexer_print_token (cp_lexer_debug_stream, token);
773       putc ('\n', cp_lexer_debug_stream);
774     }
775
776   return token;
777 }
778
779 /* Permanently remove the next token from the token stream, and
780    advance the next_token pointer to refer to the next non-purged
781    token.  */
782
783 static void
784 cp_lexer_purge_token (cp_lexer *lexer)
785 {
786   cp_token *tok = lexer->next_token;
787
788   gcc_assert (tok != &eof_token);
789   tok->type = CPP_PURGED;
790   tok->location = UNKNOWN_LOCATION;
791   tok->u.value = NULL_TREE;
792   tok->keyword = RID_MAX;
793
794   do
795     {
796       tok++;
797       if (tok == lexer->last_token)
798         {
799           tok = &eof_token;
800           break;
801         }
802     }
803   while (tok->type == CPP_PURGED);
804   lexer->next_token = tok;
805 }
806
807 /* Permanently remove all tokens after TOK, up to, but not
808    including, the token that will be returned next by
809    cp_lexer_peek_token.  */
810
811 static void
812 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
813 {
814   cp_token *peek = lexer->next_token;
815
816   if (peek == &eof_token)
817     peek = lexer->last_token;
818
819   gcc_assert (tok < peek);
820
821   for ( tok += 1; tok != peek; tok += 1)
822     {
823       tok->type = CPP_PURGED;
824       tok->location = UNKNOWN_LOCATION;
825       tok->u.value = NULL_TREE;
826       tok->keyword = RID_MAX;
827     }
828 }
829
830 /* Begin saving tokens.  All tokens consumed after this point will be
831    preserved.  */
832
833 static void
834 cp_lexer_save_tokens (cp_lexer* lexer)
835 {
836   /* Provide debugging output.  */
837   if (cp_lexer_debugging_p (lexer))
838     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
839
840   VEC_safe_push (cp_token_position, heap,
841                  lexer->saved_tokens, lexer->next_token);
842 }
843
844 /* Commit to the portion of the token stream most recently saved.  */
845
846 static void
847 cp_lexer_commit_tokens (cp_lexer* lexer)
848 {
849   /* Provide debugging output.  */
850   if (cp_lexer_debugging_p (lexer))
851     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
852
853   VEC_pop (cp_token_position, lexer->saved_tokens);
854 }
855
856 /* Return all tokens saved since the last call to cp_lexer_save_tokens
857    to the token stream.  Stop saving tokens.  */
858
859 static void
860 cp_lexer_rollback_tokens (cp_lexer* lexer)
861 {
862   /* Provide debugging output.  */
863   if (cp_lexer_debugging_p (lexer))
864     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
865
866   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
867 }
868
869 /* Print a representation of the TOKEN on the STREAM.  */
870
871 #ifdef ENABLE_CHECKING
872
873 static void
874 cp_lexer_print_token (FILE * stream, cp_token *token)
875 {
876   /* We don't use cpp_type2name here because the parser defines
877      a few tokens of its own.  */
878   static const char *const token_names[] = {
879     /* cpplib-defined token types */
880 #define OP(e, s) #e,
881 #define TK(e, s) #e,
882     TTYPE_TABLE
883 #undef OP
884 #undef TK
885     /* C++ parser token types - see "Manifest constants", above.  */
886     "KEYWORD",
887     "TEMPLATE_ID",
888     "NESTED_NAME_SPECIFIER",
889     "PURGED"
890   };
891
892   /* If we have a name for the token, print it out.  Otherwise, we
893      simply give the numeric code.  */
894   gcc_assert (token->type < ARRAY_SIZE(token_names));
895   fputs (token_names[token->type], stream);
896
897   /* For some tokens, print the associated data.  */
898   switch (token->type)
899     {
900     case CPP_KEYWORD:
901       /* Some keywords have a value that is not an IDENTIFIER_NODE.
902          For example, `struct' is mapped to an INTEGER_CST.  */
903       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
904         break;
905       /* else fall through */
906     case CPP_NAME:
907       fputs (IDENTIFIER_POINTER (token->u.value), stream);
908       break;
909
910     case CPP_STRING:
911     case CPP_STRING16:
912     case CPP_STRING32:
913     case CPP_WSTRING:
914     case CPP_UTF8STRING:
915       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
916       break;
917
918     default:
919       break;
920     }
921 }
922
923 /* Start emitting debugging information.  */
924
925 static void
926 cp_lexer_start_debugging (cp_lexer* lexer)
927 {
928   lexer->debugging_p = true;
929 }
930
931 /* Stop emitting debugging information.  */
932
933 static void
934 cp_lexer_stop_debugging (cp_lexer* lexer)
935 {
936   lexer->debugging_p = false;
937 }
938
939 #endif /* ENABLE_CHECKING */
940
941 /* Create a new cp_token_cache, representing a range of tokens.  */
942
943 static cp_token_cache *
944 cp_token_cache_new (cp_token *first, cp_token *last)
945 {
946   cp_token_cache *cache = GGC_NEW (cp_token_cache);
947   cache->first = first;
948   cache->last = last;
949   return cache;
950 }
951
952 \f
953 /* Decl-specifiers.  */
954
955 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
956
957 static void
958 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
959 {
960   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
961 }
962
963 /* Declarators.  */
964
965 /* Nothing other than the parser should be creating declarators;
966    declarators are a semi-syntactic representation of C++ entities.
967    Other parts of the front end that need to create entities (like
968    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
969
970 static cp_declarator *make_call_declarator
971   (cp_declarator *, tree, cp_cv_quals, tree, tree);
972 static cp_declarator *make_array_declarator
973   (cp_declarator *, tree);
974 static cp_declarator *make_pointer_declarator
975   (cp_cv_quals, cp_declarator *);
976 static cp_declarator *make_reference_declarator
977   (cp_cv_quals, cp_declarator *, bool);
978 static cp_parameter_declarator *make_parameter_declarator
979   (cp_decl_specifier_seq *, cp_declarator *, tree);
980 static cp_declarator *make_ptrmem_declarator
981   (cp_cv_quals, tree, cp_declarator *);
982
983 /* An erroneous declarator.  */
984 static cp_declarator *cp_error_declarator;
985
986 /* The obstack on which declarators and related data structures are
987    allocated.  */
988 static struct obstack declarator_obstack;
989
990 /* Alloc BYTES from the declarator memory pool.  */
991
992 static inline void *
993 alloc_declarator (size_t bytes)
994 {
995   return obstack_alloc (&declarator_obstack, bytes);
996 }
997
998 /* Allocate a declarator of the indicated KIND.  Clear fields that are
999    common to all declarators.  */
1000
1001 static cp_declarator *
1002 make_declarator (cp_declarator_kind kind)
1003 {
1004   cp_declarator *declarator;
1005
1006   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1007   declarator->kind = kind;
1008   declarator->attributes = NULL_TREE;
1009   declarator->declarator = NULL;
1010   declarator->parameter_pack_p = false;
1011   declarator->id_loc = UNKNOWN_LOCATION;
1012
1013   return declarator;
1014 }
1015
1016 /* Make a declarator for a generalized identifier.  If
1017    QUALIFYING_SCOPE is non-NULL, the identifier is
1018    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1019    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1020    is, if any.   */
1021
1022 static cp_declarator *
1023 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1024                     special_function_kind sfk)
1025 {
1026   cp_declarator *declarator;
1027
1028   /* It is valid to write:
1029
1030        class C { void f(); };
1031        typedef C D;
1032        void D::f();
1033
1034      The standard is not clear about whether `typedef const C D' is
1035      legal; as of 2002-09-15 the committee is considering that
1036      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1037      well.  */
1038   if (qualifying_scope && TYPE_P (qualifying_scope))
1039     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1040
1041   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1042               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1043               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1044
1045   declarator = make_declarator (cdk_id);
1046   declarator->u.id.qualifying_scope = qualifying_scope;
1047   declarator->u.id.unqualified_name = unqualified_name;
1048   declarator->u.id.sfk = sfk;
1049   
1050   return declarator;
1051 }
1052
1053 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1054    of modifiers such as const or volatile to apply to the pointer
1055    type, represented as identifiers.  */
1056
1057 cp_declarator *
1058 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1059 {
1060   cp_declarator *declarator;
1061
1062   declarator = make_declarator (cdk_pointer);
1063   declarator->declarator = target;
1064   declarator->u.pointer.qualifiers = cv_qualifiers;
1065   declarator->u.pointer.class_type = NULL_TREE;
1066   if (target)
1067     {
1068       declarator->parameter_pack_p = target->parameter_pack_p;
1069       target->parameter_pack_p = false;
1070     }
1071   else
1072     declarator->parameter_pack_p = false;
1073
1074   return declarator;
1075 }
1076
1077 /* Like make_pointer_declarator -- but for references.  */
1078
1079 cp_declarator *
1080 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1081                            bool rvalue_ref)
1082 {
1083   cp_declarator *declarator;
1084
1085   declarator = make_declarator (cdk_reference);
1086   declarator->declarator = target;
1087   declarator->u.reference.qualifiers = cv_qualifiers;
1088   declarator->u.reference.rvalue_ref = rvalue_ref;
1089   if (target)
1090     {
1091       declarator->parameter_pack_p = target->parameter_pack_p;
1092       target->parameter_pack_p = false;
1093     }
1094   else
1095     declarator->parameter_pack_p = false;
1096
1097   return declarator;
1098 }
1099
1100 /* Like make_pointer_declarator -- but for a pointer to a non-static
1101    member of CLASS_TYPE.  */
1102
1103 cp_declarator *
1104 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1105                         cp_declarator *pointee)
1106 {
1107   cp_declarator *declarator;
1108
1109   declarator = make_declarator (cdk_ptrmem);
1110   declarator->declarator = pointee;
1111   declarator->u.pointer.qualifiers = cv_qualifiers;
1112   declarator->u.pointer.class_type = class_type;
1113
1114   if (pointee)
1115     {
1116       declarator->parameter_pack_p = pointee->parameter_pack_p;
1117       pointee->parameter_pack_p = false;
1118     }
1119   else
1120     declarator->parameter_pack_p = false;
1121
1122   return declarator;
1123 }
1124
1125 /* Make a declarator for the function given by TARGET, with the
1126    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1127    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1128    indicates what exceptions can be thrown.  */
1129
1130 cp_declarator *
1131 make_call_declarator (cp_declarator *target,
1132                       tree parms,
1133                       cp_cv_quals cv_qualifiers,
1134                       tree exception_specification,
1135                       tree late_return_type)
1136 {
1137   cp_declarator *declarator;
1138
1139   declarator = make_declarator (cdk_function);
1140   declarator->declarator = target;
1141   declarator->u.function.parameters = parms;
1142   declarator->u.function.qualifiers = cv_qualifiers;
1143   declarator->u.function.exception_specification = exception_specification;
1144   declarator->u.function.late_return_type = late_return_type;
1145   if (target)
1146     {
1147       declarator->parameter_pack_p = target->parameter_pack_p;
1148       target->parameter_pack_p = false;
1149     }
1150   else
1151     declarator->parameter_pack_p = false;
1152
1153   return declarator;
1154 }
1155
1156 /* Make a declarator for an array of BOUNDS elements, each of which is
1157    defined by ELEMENT.  */
1158
1159 cp_declarator *
1160 make_array_declarator (cp_declarator *element, tree bounds)
1161 {
1162   cp_declarator *declarator;
1163
1164   declarator = make_declarator (cdk_array);
1165   declarator->declarator = element;
1166   declarator->u.array.bounds = bounds;
1167   if (element)
1168     {
1169       declarator->parameter_pack_p = element->parameter_pack_p;
1170       element->parameter_pack_p = false;
1171     }
1172   else
1173     declarator->parameter_pack_p = false;
1174
1175   return declarator;
1176 }
1177
1178 /* Determine whether the declarator we've seen so far can be a
1179    parameter pack, when followed by an ellipsis.  */
1180 static bool 
1181 declarator_can_be_parameter_pack (cp_declarator *declarator)
1182 {
1183   /* Search for a declarator name, or any other declarator that goes
1184      after the point where the ellipsis could appear in a parameter
1185      pack. If we find any of these, then this declarator can not be
1186      made into a parameter pack.  */
1187   bool found = false;
1188   while (declarator && !found)
1189     {
1190       switch ((int)declarator->kind)
1191         {
1192         case cdk_id:
1193         case cdk_array:
1194           found = true;
1195           break;
1196
1197         case cdk_error:
1198           return true;
1199
1200         default:
1201           declarator = declarator->declarator;
1202           break;
1203         }
1204     }
1205
1206   return !found;
1207 }
1208
1209 cp_parameter_declarator *no_parameters;
1210
1211 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1212    DECLARATOR and DEFAULT_ARGUMENT.  */
1213
1214 cp_parameter_declarator *
1215 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1216                            cp_declarator *declarator,
1217                            tree default_argument)
1218 {
1219   cp_parameter_declarator *parameter;
1220
1221   parameter = ((cp_parameter_declarator *)
1222                alloc_declarator (sizeof (cp_parameter_declarator)));
1223   parameter->next = NULL;
1224   if (decl_specifiers)
1225     parameter->decl_specifiers = *decl_specifiers;
1226   else
1227     clear_decl_specs (&parameter->decl_specifiers);
1228   parameter->declarator = declarator;
1229   parameter->default_argument = default_argument;
1230   parameter->ellipsis_p = false;
1231
1232   return parameter;
1233 }
1234
1235 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1236
1237 static bool
1238 function_declarator_p (const cp_declarator *declarator)
1239 {
1240   while (declarator)
1241     {
1242       if (declarator->kind == cdk_function
1243           && declarator->declarator->kind == cdk_id)
1244         return true;
1245       if (declarator->kind == cdk_id
1246           || declarator->kind == cdk_error)
1247         return false;
1248       declarator = declarator->declarator;
1249     }
1250   return false;
1251 }
1252  
1253 /* The parser.  */
1254
1255 /* Overview
1256    --------
1257
1258    A cp_parser parses the token stream as specified by the C++
1259    grammar.  Its job is purely parsing, not semantic analysis.  For
1260    example, the parser breaks the token stream into declarators,
1261    expressions, statements, and other similar syntactic constructs.
1262    It does not check that the types of the expressions on either side
1263    of an assignment-statement are compatible, or that a function is
1264    not declared with a parameter of type `void'.
1265
1266    The parser invokes routines elsewhere in the compiler to perform
1267    semantic analysis and to build up the abstract syntax tree for the
1268    code processed.
1269
1270    The parser (and the template instantiation code, which is, in a
1271    way, a close relative of parsing) are the only parts of the
1272    compiler that should be calling push_scope and pop_scope, or
1273    related functions.  The parser (and template instantiation code)
1274    keeps track of what scope is presently active; everything else
1275    should simply honor that.  (The code that generates static
1276    initializers may also need to set the scope, in order to check
1277    access control correctly when emitting the initializers.)
1278
1279    Methodology
1280    -----------
1281
1282    The parser is of the standard recursive-descent variety.  Upcoming
1283    tokens in the token stream are examined in order to determine which
1284    production to use when parsing a non-terminal.  Some C++ constructs
1285    require arbitrary look ahead to disambiguate.  For example, it is
1286    impossible, in the general case, to tell whether a statement is an
1287    expression or declaration without scanning the entire statement.
1288    Therefore, the parser is capable of "parsing tentatively."  When the
1289    parser is not sure what construct comes next, it enters this mode.
1290    Then, while we attempt to parse the construct, the parser queues up
1291    error messages, rather than issuing them immediately, and saves the
1292    tokens it consumes.  If the construct is parsed successfully, the
1293    parser "commits", i.e., it issues any queued error messages and
1294    the tokens that were being preserved are permanently discarded.
1295    If, however, the construct is not parsed successfully, the parser
1296    rolls back its state completely so that it can resume parsing using
1297    a different alternative.
1298
1299    Future Improvements
1300    -------------------
1301
1302    The performance of the parser could probably be improved substantially.
1303    We could often eliminate the need to parse tentatively by looking ahead
1304    a little bit.  In some places, this approach might not entirely eliminate
1305    the need to parse tentatively, but it might still speed up the average
1306    case.  */
1307
1308 /* Flags that are passed to some parsing functions.  These values can
1309    be bitwise-ored together.  */
1310
1311 enum
1312 {
1313   /* No flags.  */
1314   CP_PARSER_FLAGS_NONE = 0x0,
1315   /* The construct is optional.  If it is not present, then no error
1316      should be issued.  */
1317   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1318   /* When parsing a type-specifier, treat user-defined type-names
1319      as non-type identifiers.  */
1320   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1321   /* When parsing a type-specifier, do not try to parse a class-specifier
1322      or enum-specifier.  */
1323   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1324 };
1325
1326 /* This type is used for parameters and variables which hold
1327    combinations of the above flags.  */
1328 typedef int cp_parser_flags;
1329
1330 /* The different kinds of declarators we want to parse.  */
1331
1332 typedef enum cp_parser_declarator_kind
1333 {
1334   /* We want an abstract declarator.  */
1335   CP_PARSER_DECLARATOR_ABSTRACT,
1336   /* We want a named declarator.  */
1337   CP_PARSER_DECLARATOR_NAMED,
1338   /* We don't mind, but the name must be an unqualified-id.  */
1339   CP_PARSER_DECLARATOR_EITHER
1340 } cp_parser_declarator_kind;
1341
1342 /* The precedence values used to parse binary expressions.  The minimum value
1343    of PREC must be 1, because zero is reserved to quickly discriminate
1344    binary operators from other tokens.  */
1345
1346 enum cp_parser_prec
1347 {
1348   PREC_NOT_OPERATOR,
1349   PREC_LOGICAL_OR_EXPRESSION,
1350   PREC_LOGICAL_AND_EXPRESSION,
1351   PREC_INCLUSIVE_OR_EXPRESSION,
1352   PREC_EXCLUSIVE_OR_EXPRESSION,
1353   PREC_AND_EXPRESSION,
1354   PREC_EQUALITY_EXPRESSION,
1355   PREC_RELATIONAL_EXPRESSION,
1356   PREC_SHIFT_EXPRESSION,
1357   PREC_ADDITIVE_EXPRESSION,
1358   PREC_MULTIPLICATIVE_EXPRESSION,
1359   PREC_PM_EXPRESSION,
1360   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1361 };
1362
1363 /* A mapping from a token type to a corresponding tree node type, with a
1364    precedence value.  */
1365
1366 typedef struct cp_parser_binary_operations_map_node
1367 {
1368   /* The token type.  */
1369   enum cpp_ttype token_type;
1370   /* The corresponding tree code.  */
1371   enum tree_code tree_type;
1372   /* The precedence of this operator.  */
1373   enum cp_parser_prec prec;
1374 } cp_parser_binary_operations_map_node;
1375
1376 /* The status of a tentative parse.  */
1377
1378 typedef enum cp_parser_status_kind
1379 {
1380   /* No errors have occurred.  */
1381   CP_PARSER_STATUS_KIND_NO_ERROR,
1382   /* An error has occurred.  */
1383   CP_PARSER_STATUS_KIND_ERROR,
1384   /* We are committed to this tentative parse, whether or not an error
1385      has occurred.  */
1386   CP_PARSER_STATUS_KIND_COMMITTED
1387 } cp_parser_status_kind;
1388
1389 typedef struct cp_parser_expression_stack_entry
1390 {
1391   /* Left hand side of the binary operation we are currently
1392      parsing.  */
1393   tree lhs;
1394   /* Original tree code for left hand side, if it was a binary
1395      expression itself (used for -Wparentheses).  */
1396   enum tree_code lhs_type;
1397   /* Tree code for the binary operation we are parsing.  */
1398   enum tree_code tree_type;
1399   /* Precedence of the binary operation we are parsing.  */
1400   enum cp_parser_prec prec;
1401 } cp_parser_expression_stack_entry;
1402
1403 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1404    entries because precedence levels on the stack are monotonically
1405    increasing.  */
1406 typedef struct cp_parser_expression_stack_entry
1407   cp_parser_expression_stack[NUM_PREC_VALUES];
1408
1409 /* Context that is saved and restored when parsing tentatively.  */
1410 typedef struct GTY (()) cp_parser_context {
1411   /* If this is a tentative parsing context, the status of the
1412      tentative parse.  */
1413   enum cp_parser_status_kind status;
1414   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1415      that are looked up in this context must be looked up both in the
1416      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1417      the context of the containing expression.  */
1418   tree object_type;
1419
1420   /* The next parsing context in the stack.  */
1421   struct cp_parser_context *next;
1422 } cp_parser_context;
1423
1424 /* Prototypes.  */
1425
1426 /* Constructors and destructors.  */
1427
1428 static cp_parser_context *cp_parser_context_new
1429   (cp_parser_context *);
1430
1431 /* Class variables.  */
1432
1433 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1434
1435 /* The operator-precedence table used by cp_parser_binary_expression.
1436    Transformed into an associative array (binops_by_token) by
1437    cp_parser_new.  */
1438
1439 static const cp_parser_binary_operations_map_node binops[] = {
1440   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1441   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1442
1443   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1444   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1445   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1446
1447   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1448   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1449
1450   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1451   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1452
1453   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1454   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1455   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1456   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1457
1458   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1459   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1460
1461   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1462
1463   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1464
1465   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1466
1467   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1468
1469   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1470 };
1471
1472 /* The same as binops, but initialized by cp_parser_new so that
1473    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1474    for speed.  */
1475 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1476
1477 /* Constructors and destructors.  */
1478
1479 /* Construct a new context.  The context below this one on the stack
1480    is given by NEXT.  */
1481
1482 static cp_parser_context *
1483 cp_parser_context_new (cp_parser_context* next)
1484 {
1485   cp_parser_context *context;
1486
1487   /* Allocate the storage.  */
1488   if (cp_parser_context_free_list != NULL)
1489     {
1490       /* Pull the first entry from the free list.  */
1491       context = cp_parser_context_free_list;
1492       cp_parser_context_free_list = context->next;
1493       memset (context, 0, sizeof (*context));
1494     }
1495   else
1496     context = GGC_CNEW (cp_parser_context);
1497
1498   /* No errors have occurred yet in this context.  */
1499   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1500   /* If this is not the bottommost context, copy information that we
1501      need from the previous context.  */
1502   if (next)
1503     {
1504       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1505          expression, then we are parsing one in this context, too.  */
1506       context->object_type = next->object_type;
1507       /* Thread the stack.  */
1508       context->next = next;
1509     }
1510
1511   return context;
1512 }
1513
1514 /* The cp_parser structure represents the C++ parser.  */
1515
1516 typedef struct GTY(()) cp_parser {
1517   /* The lexer from which we are obtaining tokens.  */
1518   cp_lexer *lexer;
1519
1520   /* The scope in which names should be looked up.  If NULL_TREE, then
1521      we look up names in the scope that is currently open in the
1522      source program.  If non-NULL, this is either a TYPE or
1523      NAMESPACE_DECL for the scope in which we should look.  It can
1524      also be ERROR_MARK, when we've parsed a bogus scope.
1525
1526      This value is not cleared automatically after a name is looked
1527      up, so we must be careful to clear it before starting a new look
1528      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1529      will look up `Z' in the scope of `X', rather than the current
1530      scope.)  Unfortunately, it is difficult to tell when name lookup
1531      is complete, because we sometimes peek at a token, look it up,
1532      and then decide not to consume it.   */
1533   tree scope;
1534
1535   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1536      last lookup took place.  OBJECT_SCOPE is used if an expression
1537      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1538      respectively.  QUALIFYING_SCOPE is used for an expression of the
1539      form "X::Y"; it refers to X.  */
1540   tree object_scope;
1541   tree qualifying_scope;
1542
1543   /* A stack of parsing contexts.  All but the bottom entry on the
1544      stack will be tentative contexts.
1545
1546      We parse tentatively in order to determine which construct is in
1547      use in some situations.  For example, in order to determine
1548      whether a statement is an expression-statement or a
1549      declaration-statement we parse it tentatively as a
1550      declaration-statement.  If that fails, we then reparse the same
1551      token stream as an expression-statement.  */
1552   cp_parser_context *context;
1553
1554   /* True if we are parsing GNU C++.  If this flag is not set, then
1555      GNU extensions are not recognized.  */
1556   bool allow_gnu_extensions_p;
1557
1558   /* TRUE if the `>' token should be interpreted as the greater-than
1559      operator.  FALSE if it is the end of a template-id or
1560      template-parameter-list. In C++0x mode, this flag also applies to
1561      `>>' tokens, which are viewed as two consecutive `>' tokens when
1562      this flag is FALSE.  */
1563   bool greater_than_is_operator_p;
1564
1565   /* TRUE if default arguments are allowed within a parameter list
1566      that starts at this point. FALSE if only a gnu extension makes
1567      them permissible.  */
1568   bool default_arg_ok_p;
1569
1570   /* TRUE if we are parsing an integral constant-expression.  See
1571      [expr.const] for a precise definition.  */
1572   bool integral_constant_expression_p;
1573
1574   /* TRUE if we are parsing an integral constant-expression -- but a
1575      non-constant expression should be permitted as well.  This flag
1576      is used when parsing an array bound so that GNU variable-length
1577      arrays are tolerated.  */
1578   bool allow_non_integral_constant_expression_p;
1579
1580   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1581      been seen that makes the expression non-constant.  */
1582   bool non_integral_constant_expression_p;
1583
1584   /* TRUE if local variable names and `this' are forbidden in the
1585      current context.  */
1586   bool local_variables_forbidden_p;
1587
1588   /* TRUE if the declaration we are parsing is part of a
1589      linkage-specification of the form `extern string-literal
1590      declaration'.  */
1591   bool in_unbraced_linkage_specification_p;
1592
1593   /* TRUE if we are presently parsing a declarator, after the
1594      direct-declarator.  */
1595   bool in_declarator_p;
1596
1597   /* TRUE if we are presently parsing a template-argument-list.  */
1598   bool in_template_argument_list_p;
1599
1600   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1601      to IN_OMP_BLOCK if parsing OpenMP structured block and
1602      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1603      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1604      iteration-statement, OpenMP block or loop within that switch.  */
1605 #define IN_SWITCH_STMT          1
1606 #define IN_ITERATION_STMT       2
1607 #define IN_OMP_BLOCK            4
1608 #define IN_OMP_FOR              8
1609 #define IN_IF_STMT             16
1610   unsigned char in_statement;
1611
1612   /* TRUE if we are presently parsing the body of a switch statement.
1613      Note that this doesn't quite overlap with in_statement above.
1614      The difference relates to giving the right sets of error messages:
1615      "case not in switch" vs "break statement used with OpenMP...".  */
1616   bool in_switch_statement_p;
1617
1618   /* TRUE if we are parsing a type-id in an expression context.  In
1619      such a situation, both "type (expr)" and "type (type)" are valid
1620      alternatives.  */
1621   bool in_type_id_in_expr_p;
1622
1623   /* TRUE if we are currently in a header file where declarations are
1624      implicitly extern "C".  */
1625   bool implicit_extern_c;
1626
1627   /* TRUE if strings in expressions should be translated to the execution
1628      character set.  */
1629   bool translate_strings_p;
1630
1631   /* TRUE if we are presently parsing the body of a function, but not
1632      a local class.  */
1633   bool in_function_body;
1634
1635   /* If non-NULL, then we are parsing a construct where new type
1636      definitions are not permitted.  The string stored here will be
1637      issued as an error message if a type is defined.  */
1638   const char *type_definition_forbidden_message;
1639
1640   /* A list of lists. The outer list is a stack, used for member
1641      functions of local classes. At each level there are two sub-list,
1642      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1643      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1644      TREE_VALUE's. The functions are chained in reverse declaration
1645      order.
1646
1647      The TREE_PURPOSE sublist contains those functions with default
1648      arguments that need post processing, and the TREE_VALUE sublist
1649      contains those functions with definitions that need post
1650      processing.
1651
1652      These lists can only be processed once the outermost class being
1653      defined is complete.  */
1654   tree unparsed_functions_queues;
1655
1656   /* The number of classes whose definitions are currently in
1657      progress.  */
1658   unsigned num_classes_being_defined;
1659
1660   /* The number of template parameter lists that apply directly to the
1661      current declaration.  */
1662   unsigned num_template_parameter_lists;
1663 } cp_parser;
1664
1665 /* Prototypes.  */
1666
1667 /* Constructors and destructors.  */
1668
1669 static cp_parser *cp_parser_new
1670   (void);
1671
1672 /* Routines to parse various constructs.
1673
1674    Those that return `tree' will return the error_mark_node (rather
1675    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1676    Sometimes, they will return an ordinary node if error-recovery was
1677    attempted, even though a parse error occurred.  So, to check
1678    whether or not a parse error occurred, you should always use
1679    cp_parser_error_occurred.  If the construct is optional (indicated
1680    either by an `_opt' in the name of the function that does the
1681    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1682    the construct is not present.  */
1683
1684 /* Lexical conventions [gram.lex]  */
1685
1686 static tree cp_parser_identifier
1687   (cp_parser *);
1688 static tree cp_parser_string_literal
1689   (cp_parser *, bool, bool);
1690
1691 /* Basic concepts [gram.basic]  */
1692
1693 static bool cp_parser_translation_unit
1694   (cp_parser *);
1695
1696 /* Expressions [gram.expr]  */
1697
1698 static tree cp_parser_primary_expression
1699   (cp_parser *, bool, bool, bool, cp_id_kind *);
1700 static tree cp_parser_id_expression
1701   (cp_parser *, bool, bool, bool *, bool, bool);
1702 static tree cp_parser_unqualified_id
1703   (cp_parser *, bool, bool, bool, bool);
1704 static tree cp_parser_nested_name_specifier_opt
1705   (cp_parser *, bool, bool, bool, bool);
1706 static tree cp_parser_nested_name_specifier
1707   (cp_parser *, bool, bool, bool, bool);
1708 static tree cp_parser_qualifying_entity
1709   (cp_parser *, bool, bool, bool, bool, bool);
1710 static tree cp_parser_postfix_expression
1711   (cp_parser *, bool, bool, bool, cp_id_kind *);
1712 static tree cp_parser_postfix_open_square_expression
1713   (cp_parser *, tree, bool);
1714 static tree cp_parser_postfix_dot_deref_expression
1715   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1716 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1717   (cp_parser *, int, bool, bool, bool *);
1718 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1719 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1720 static void cp_parser_pseudo_destructor_name
1721   (cp_parser *, tree *, tree *);
1722 static tree cp_parser_unary_expression
1723   (cp_parser *, bool, bool, cp_id_kind *);
1724 static enum tree_code cp_parser_unary_operator
1725   (cp_token *);
1726 static tree cp_parser_new_expression
1727   (cp_parser *);
1728 static VEC(tree,gc) *cp_parser_new_placement
1729   (cp_parser *);
1730 static tree cp_parser_new_type_id
1731   (cp_parser *, tree *);
1732 static cp_declarator *cp_parser_new_declarator_opt
1733   (cp_parser *);
1734 static cp_declarator *cp_parser_direct_new_declarator
1735   (cp_parser *);
1736 static VEC(tree,gc) *cp_parser_new_initializer
1737   (cp_parser *);
1738 static tree cp_parser_delete_expression
1739   (cp_parser *);
1740 static tree cp_parser_cast_expression
1741   (cp_parser *, bool, bool, cp_id_kind *);
1742 static tree cp_parser_binary_expression
1743   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1744 static tree cp_parser_question_colon_clause
1745   (cp_parser *, tree);
1746 static tree cp_parser_assignment_expression
1747   (cp_parser *, bool, cp_id_kind *);
1748 static enum tree_code cp_parser_assignment_operator_opt
1749   (cp_parser *);
1750 static tree cp_parser_expression
1751   (cp_parser *, bool, cp_id_kind *);
1752 static tree cp_parser_constant_expression
1753   (cp_parser *, bool, bool *);
1754 static tree cp_parser_builtin_offsetof
1755   (cp_parser *);
1756 static tree cp_parser_lambda_expression
1757   (cp_parser *);
1758 static void cp_parser_lambda_introducer
1759   (cp_parser *, tree);
1760 static void cp_parser_lambda_declarator_opt
1761   (cp_parser *, tree);
1762 static void cp_parser_lambda_body
1763   (cp_parser *, tree);
1764
1765 /* Statements [gram.stmt.stmt]  */
1766
1767 static void cp_parser_statement
1768   (cp_parser *, tree, bool, bool *);
1769 static void cp_parser_label_for_labeled_statement
1770   (cp_parser *);
1771 static tree cp_parser_expression_statement
1772   (cp_parser *, tree);
1773 static tree cp_parser_compound_statement
1774   (cp_parser *, tree, bool);
1775 static void cp_parser_statement_seq_opt
1776   (cp_parser *, tree);
1777 static tree cp_parser_selection_statement
1778   (cp_parser *, bool *);
1779 static tree cp_parser_condition
1780   (cp_parser *);
1781 static tree cp_parser_iteration_statement
1782   (cp_parser *);
1783 static void cp_parser_for_init_statement
1784   (cp_parser *);
1785 static tree cp_parser_jump_statement
1786   (cp_parser *);
1787 static void cp_parser_declaration_statement
1788   (cp_parser *);
1789
1790 static tree cp_parser_implicitly_scoped_statement
1791   (cp_parser *, bool *);
1792 static void cp_parser_already_scoped_statement
1793   (cp_parser *);
1794
1795 /* Declarations [gram.dcl.dcl] */
1796
1797 static void cp_parser_declaration_seq_opt
1798   (cp_parser *);
1799 static void cp_parser_declaration
1800   (cp_parser *);
1801 static void cp_parser_block_declaration
1802   (cp_parser *, bool);
1803 static void cp_parser_simple_declaration
1804   (cp_parser *, bool);
1805 static void cp_parser_decl_specifier_seq
1806   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1807 static tree cp_parser_storage_class_specifier_opt
1808   (cp_parser *);
1809 static tree cp_parser_function_specifier_opt
1810   (cp_parser *, cp_decl_specifier_seq *);
1811 static tree cp_parser_type_specifier
1812   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1813    int *, bool *);
1814 static tree cp_parser_simple_type_specifier
1815   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1816 static tree cp_parser_type_name
1817   (cp_parser *);
1818 static tree cp_parser_nonclass_name 
1819   (cp_parser* parser);
1820 static tree cp_parser_elaborated_type_specifier
1821   (cp_parser *, bool, bool);
1822 static tree cp_parser_enum_specifier
1823   (cp_parser *);
1824 static void cp_parser_enumerator_list
1825   (cp_parser *, tree);
1826 static void cp_parser_enumerator_definition
1827   (cp_parser *, tree);
1828 static tree cp_parser_namespace_name
1829   (cp_parser *);
1830 static void cp_parser_namespace_definition
1831   (cp_parser *);
1832 static void cp_parser_namespace_body
1833   (cp_parser *);
1834 static tree cp_parser_qualified_namespace_specifier
1835   (cp_parser *);
1836 static void cp_parser_namespace_alias_definition
1837   (cp_parser *);
1838 static bool cp_parser_using_declaration
1839   (cp_parser *, bool);
1840 static void cp_parser_using_directive
1841   (cp_parser *);
1842 static void cp_parser_asm_definition
1843   (cp_parser *);
1844 static void cp_parser_linkage_specification
1845   (cp_parser *);
1846 static void cp_parser_static_assert
1847   (cp_parser *, bool);
1848 static tree cp_parser_decltype
1849   (cp_parser *);
1850
1851 /* Declarators [gram.dcl.decl] */
1852
1853 static tree cp_parser_init_declarator
1854   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1855 static cp_declarator *cp_parser_declarator
1856   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1857 static cp_declarator *cp_parser_direct_declarator
1858   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1859 static enum tree_code cp_parser_ptr_operator
1860   (cp_parser *, tree *, cp_cv_quals *);
1861 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1862   (cp_parser *);
1863 static tree cp_parser_late_return_type_opt
1864   (cp_parser *);
1865 static tree cp_parser_declarator_id
1866   (cp_parser *, bool);
1867 static tree cp_parser_type_id
1868   (cp_parser *);
1869 static tree cp_parser_template_type_arg
1870   (cp_parser *);
1871 static tree cp_parser_trailing_type_id (cp_parser *);
1872 static tree cp_parser_type_id_1
1873   (cp_parser *, bool, bool);
1874 static void cp_parser_type_specifier_seq
1875   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1876 static tree cp_parser_parameter_declaration_clause
1877   (cp_parser *);
1878 static tree cp_parser_parameter_declaration_list
1879   (cp_parser *, bool *);
1880 static cp_parameter_declarator *cp_parser_parameter_declaration
1881   (cp_parser *, bool, bool *);
1882 static tree cp_parser_default_argument 
1883   (cp_parser *, bool);
1884 static void cp_parser_function_body
1885   (cp_parser *);
1886 static tree cp_parser_initializer
1887   (cp_parser *, bool *, bool *);
1888 static tree cp_parser_initializer_clause
1889   (cp_parser *, bool *);
1890 static tree cp_parser_braced_list
1891   (cp_parser*, bool*);
1892 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1893   (cp_parser *, bool *);
1894
1895 static bool cp_parser_ctor_initializer_opt_and_function_body
1896   (cp_parser *);
1897
1898 /* Classes [gram.class] */
1899
1900 static tree cp_parser_class_name
1901   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1902 static tree cp_parser_class_specifier
1903   (cp_parser *);
1904 static tree cp_parser_class_head
1905   (cp_parser *, bool *, tree *, tree *);
1906 static enum tag_types cp_parser_class_key
1907   (cp_parser *);
1908 static void cp_parser_member_specification_opt
1909   (cp_parser *);
1910 static void cp_parser_member_declaration
1911   (cp_parser *);
1912 static tree cp_parser_pure_specifier
1913   (cp_parser *);
1914 static tree cp_parser_constant_initializer
1915   (cp_parser *);
1916
1917 /* Derived classes [gram.class.derived] */
1918
1919 static tree cp_parser_base_clause
1920   (cp_parser *);
1921 static tree cp_parser_base_specifier
1922   (cp_parser *);
1923
1924 /* Special member functions [gram.special] */
1925
1926 static tree cp_parser_conversion_function_id
1927   (cp_parser *);
1928 static tree cp_parser_conversion_type_id
1929   (cp_parser *);
1930 static cp_declarator *cp_parser_conversion_declarator_opt
1931   (cp_parser *);
1932 static bool cp_parser_ctor_initializer_opt
1933   (cp_parser *);
1934 static void cp_parser_mem_initializer_list
1935   (cp_parser *);
1936 static tree cp_parser_mem_initializer
1937   (cp_parser *);
1938 static tree cp_parser_mem_initializer_id
1939   (cp_parser *);
1940
1941 /* Overloading [gram.over] */
1942
1943 static tree cp_parser_operator_function_id
1944   (cp_parser *);
1945 static tree cp_parser_operator
1946   (cp_parser *);
1947
1948 /* Templates [gram.temp] */
1949
1950 static void cp_parser_template_declaration
1951   (cp_parser *, bool);
1952 static tree cp_parser_template_parameter_list
1953   (cp_parser *);
1954 static tree cp_parser_template_parameter
1955   (cp_parser *, bool *, bool *);
1956 static tree cp_parser_type_parameter
1957   (cp_parser *, bool *);
1958 static tree cp_parser_template_id
1959   (cp_parser *, bool, bool, bool);
1960 static tree cp_parser_template_name
1961   (cp_parser *, bool, bool, bool, bool *);
1962 static tree cp_parser_template_argument_list
1963   (cp_parser *);
1964 static tree cp_parser_template_argument
1965   (cp_parser *);
1966 static void cp_parser_explicit_instantiation
1967   (cp_parser *);
1968 static void cp_parser_explicit_specialization
1969   (cp_parser *);
1970
1971 /* Exception handling [gram.exception] */
1972
1973 static tree cp_parser_try_block
1974   (cp_parser *);
1975 static bool cp_parser_function_try_block
1976   (cp_parser *);
1977 static void cp_parser_handler_seq
1978   (cp_parser *);
1979 static void cp_parser_handler
1980   (cp_parser *);
1981 static tree cp_parser_exception_declaration
1982   (cp_parser *);
1983 static tree cp_parser_throw_expression
1984   (cp_parser *);
1985 static tree cp_parser_exception_specification_opt
1986   (cp_parser *);
1987 static tree cp_parser_type_id_list
1988   (cp_parser *);
1989
1990 /* GNU Extensions */
1991
1992 static tree cp_parser_asm_specification_opt
1993   (cp_parser *);
1994 static tree cp_parser_asm_operand_list
1995   (cp_parser *);
1996 static tree cp_parser_asm_clobber_list
1997   (cp_parser *);
1998 static tree cp_parser_asm_label_list
1999   (cp_parser *);
2000 static tree cp_parser_attributes_opt
2001   (cp_parser *);
2002 static tree cp_parser_attribute_list
2003   (cp_parser *);
2004 static bool cp_parser_extension_opt
2005   (cp_parser *, int *);
2006 static void cp_parser_label_declaration
2007   (cp_parser *);
2008
2009 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2010 static bool cp_parser_pragma
2011   (cp_parser *, enum pragma_context);
2012
2013 /* Objective-C++ Productions */
2014
2015 static tree cp_parser_objc_message_receiver
2016   (cp_parser *);
2017 static tree cp_parser_objc_message_args
2018   (cp_parser *);
2019 static tree cp_parser_objc_message_expression
2020   (cp_parser *);
2021 static tree cp_parser_objc_encode_expression
2022   (cp_parser *);
2023 static tree cp_parser_objc_defs_expression
2024   (cp_parser *);
2025 static tree cp_parser_objc_protocol_expression
2026   (cp_parser *);
2027 static tree cp_parser_objc_selector_expression
2028   (cp_parser *);
2029 static tree cp_parser_objc_expression
2030   (cp_parser *);
2031 static bool cp_parser_objc_selector_p
2032   (enum cpp_ttype);
2033 static tree cp_parser_objc_selector
2034   (cp_parser *);
2035 static tree cp_parser_objc_protocol_refs_opt
2036   (cp_parser *);
2037 static void cp_parser_objc_declaration
2038   (cp_parser *);
2039 static tree cp_parser_objc_statement
2040   (cp_parser *);
2041
2042 /* Utility Routines */
2043
2044 static tree cp_parser_lookup_name
2045   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2046 static tree cp_parser_lookup_name_simple
2047   (cp_parser *, tree, location_t);
2048 static tree cp_parser_maybe_treat_template_as_class
2049   (tree, bool);
2050 static bool cp_parser_check_declarator_template_parameters
2051   (cp_parser *, cp_declarator *, location_t);
2052 static bool cp_parser_check_template_parameters
2053   (cp_parser *, unsigned, location_t, cp_declarator *);
2054 static tree cp_parser_simple_cast_expression
2055   (cp_parser *);
2056 static tree cp_parser_global_scope_opt
2057   (cp_parser *, bool);
2058 static bool cp_parser_constructor_declarator_p
2059   (cp_parser *, bool);
2060 static tree cp_parser_function_definition_from_specifiers_and_declarator
2061   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2062 static tree cp_parser_function_definition_after_declarator
2063   (cp_parser *, bool);
2064 static void cp_parser_template_declaration_after_export
2065   (cp_parser *, bool);
2066 static void cp_parser_perform_template_parameter_access_checks
2067   (VEC (deferred_access_check,gc)*);
2068 static tree cp_parser_single_declaration
2069   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2070 static tree cp_parser_functional_cast
2071   (cp_parser *, tree);
2072 static tree cp_parser_save_member_function_body
2073   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2074 static tree cp_parser_enclosed_template_argument_list
2075   (cp_parser *);
2076 static void cp_parser_save_default_args
2077   (cp_parser *, tree);
2078 static void cp_parser_late_parsing_for_member
2079   (cp_parser *, tree);
2080 static void cp_parser_late_parsing_default_args
2081   (cp_parser *, tree);
2082 static tree cp_parser_sizeof_operand
2083   (cp_parser *, enum rid);
2084 static tree cp_parser_trait_expr
2085   (cp_parser *, enum rid);
2086 static bool cp_parser_declares_only_class_p
2087   (cp_parser *);
2088 static void cp_parser_set_storage_class
2089   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2090 static void cp_parser_set_decl_spec_type
2091   (cp_decl_specifier_seq *, tree, location_t, bool);
2092 static bool cp_parser_friend_p
2093   (const cp_decl_specifier_seq *);
2094 static void cp_parser_required_error
2095   (cp_parser *, required_token, bool);
2096 static cp_token *cp_parser_require
2097   (cp_parser *, enum cpp_ttype, required_token);
2098 static cp_token *cp_parser_require_keyword
2099   (cp_parser *, enum rid, required_token);
2100 static bool cp_parser_token_starts_function_definition_p
2101   (cp_token *);
2102 static bool cp_parser_next_token_starts_class_definition_p
2103   (cp_parser *);
2104 static bool cp_parser_next_token_ends_template_argument_p
2105   (cp_parser *);
2106 static bool cp_parser_nth_token_starts_template_argument_list_p
2107   (cp_parser *, size_t);
2108 static enum tag_types cp_parser_token_is_class_key
2109   (cp_token *);
2110 static void cp_parser_check_class_key
2111   (enum tag_types, tree type);
2112 static void cp_parser_check_access_in_redeclaration
2113   (tree type, location_t location);
2114 static bool cp_parser_optional_template_keyword
2115   (cp_parser *);
2116 static void cp_parser_pre_parsed_nested_name_specifier
2117   (cp_parser *);
2118 static bool cp_parser_cache_group
2119   (cp_parser *, enum cpp_ttype, unsigned);
2120 static void cp_parser_parse_tentatively
2121   (cp_parser *);
2122 static void cp_parser_commit_to_tentative_parse
2123   (cp_parser *);
2124 static void cp_parser_abort_tentative_parse
2125   (cp_parser *);
2126 static bool cp_parser_parse_definitely
2127   (cp_parser *);
2128 static inline bool cp_parser_parsing_tentatively
2129   (cp_parser *);
2130 static bool cp_parser_uncommitted_to_tentative_parse_p
2131   (cp_parser *);
2132 static void cp_parser_error
2133   (cp_parser *, const char *);
2134 static void cp_parser_name_lookup_error
2135   (cp_parser *, tree, tree, name_lookup_error, location_t);
2136 static bool cp_parser_simulate_error
2137   (cp_parser *);
2138 static bool cp_parser_check_type_definition
2139   (cp_parser *);
2140 static void cp_parser_check_for_definition_in_return_type
2141   (cp_declarator *, tree, location_t type_location);
2142 static void cp_parser_check_for_invalid_template_id
2143   (cp_parser *, tree, location_t location);
2144 static bool cp_parser_non_integral_constant_expression
2145   (cp_parser *, non_integral_constant);
2146 static void cp_parser_diagnose_invalid_type_name
2147   (cp_parser *, tree, tree, location_t);
2148 static bool cp_parser_parse_and_diagnose_invalid_type_name
2149   (cp_parser *);
2150 static int cp_parser_skip_to_closing_parenthesis
2151   (cp_parser *, bool, bool, bool);
2152 static void cp_parser_skip_to_end_of_statement
2153   (cp_parser *);
2154 static void cp_parser_consume_semicolon_at_end_of_statement
2155   (cp_parser *);
2156 static void cp_parser_skip_to_end_of_block_or_statement
2157   (cp_parser *);
2158 static bool cp_parser_skip_to_closing_brace
2159   (cp_parser *);
2160 static void cp_parser_skip_to_end_of_template_parameter_list
2161   (cp_parser *);
2162 static void cp_parser_skip_to_pragma_eol
2163   (cp_parser*, cp_token *);
2164 static bool cp_parser_error_occurred
2165   (cp_parser *);
2166 static bool cp_parser_allow_gnu_extensions_p
2167   (cp_parser *);
2168 static bool cp_parser_is_string_literal
2169   (cp_token *);
2170 static bool cp_parser_is_keyword
2171   (cp_token *, enum rid);
2172 static tree cp_parser_make_typename_type
2173   (cp_parser *, tree, tree, location_t location);
2174 static cp_declarator * cp_parser_make_indirect_declarator
2175   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2176
2177 /* Returns nonzero if we are parsing tentatively.  */
2178
2179 static inline bool
2180 cp_parser_parsing_tentatively (cp_parser* parser)
2181 {
2182   return parser->context->next != NULL;
2183 }
2184
2185 /* Returns nonzero if TOKEN is a string literal.  */
2186
2187 static bool
2188 cp_parser_is_string_literal (cp_token* token)
2189 {
2190   return (token->type == CPP_STRING ||
2191           token->type == CPP_STRING16 ||
2192           token->type == CPP_STRING32 ||
2193           token->type == CPP_WSTRING ||
2194           token->type == CPP_UTF8STRING);
2195 }
2196
2197 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2198
2199 static bool
2200 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2201 {
2202   return token->keyword == keyword;
2203 }
2204
2205 /* If not parsing tentatively, issue a diagnostic of the form
2206       FILE:LINE: MESSAGE before TOKEN
2207    where TOKEN is the next token in the input stream.  MESSAGE
2208    (specified by the caller) is usually of the form "expected
2209    OTHER-TOKEN".  */
2210
2211 static void
2212 cp_parser_error (cp_parser* parser, const char* gmsgid)
2213 {
2214   if (!cp_parser_simulate_error (parser))
2215     {
2216       cp_token *token = cp_lexer_peek_token (parser->lexer);
2217       /* This diagnostic makes more sense if it is tagged to the line
2218          of the token we just peeked at.  */
2219       cp_lexer_set_source_position_from_token (token);
2220
2221       if (token->type == CPP_PRAGMA)
2222         {
2223           error_at (token->location,
2224                     "%<#pragma%> is not allowed here");
2225           cp_parser_skip_to_pragma_eol (parser, token);
2226           return;
2227         }
2228
2229       c_parse_error (gmsgid,
2230                      /* Because c_parser_error does not understand
2231                         CPP_KEYWORD, keywords are treated like
2232                         identifiers.  */
2233                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2234                      token->u.value, token->flags);
2235     }
2236 }
2237
2238 /* Issue an error about name-lookup failing.  NAME is the
2239    IDENTIFIER_NODE DECL is the result of
2240    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2241    the thing that we hoped to find.  */
2242
2243 static void
2244 cp_parser_name_lookup_error (cp_parser* parser,
2245                              tree name,
2246                              tree decl,
2247                              name_lookup_error desired,
2248                              location_t location)
2249 {
2250   /* If name lookup completely failed, tell the user that NAME was not
2251      declared.  */
2252   if (decl == error_mark_node)
2253     {
2254       if (parser->scope && parser->scope != global_namespace)
2255         error_at (location, "%<%E::%E%> has not been declared",
2256                   parser->scope, name);
2257       else if (parser->scope == global_namespace)
2258         error_at (location, "%<::%E%> has not been declared", name);
2259       else if (parser->object_scope
2260                && !CLASS_TYPE_P (parser->object_scope))
2261         error_at (location, "request for member %qE in non-class type %qT",
2262                   name, parser->object_scope);
2263       else if (parser->object_scope)
2264         error_at (location, "%<%T::%E%> has not been declared",
2265                   parser->object_scope, name);
2266       else
2267         error_at (location, "%qE has not been declared", name);
2268     }
2269   else if (parser->scope && parser->scope != global_namespace)
2270     {
2271       switch (desired)
2272         {
2273           case NLE_TYPE:
2274             error_at (location, "%<%E::%E%> is not a type",
2275                                 parser->scope, name);
2276             break;
2277           case NLE_CXX98:
2278             error_at (location, "%<%E::%E%> is not a class or namespace",
2279                                 parser->scope, name);
2280             break;
2281           case NLE_NOT_CXX98:
2282             error_at (location,
2283                       "%<%E::%E%> is not a class, namespace, or enumeration",
2284                       parser->scope, name);
2285             break;
2286           default:
2287             gcc_unreachable ();
2288             
2289         }
2290     }
2291   else if (parser->scope == global_namespace)
2292     {
2293       switch (desired)
2294         {
2295           case NLE_TYPE:
2296             error_at (location, "%<::%E%> is not a type", name);
2297             break;
2298           case NLE_CXX98:
2299             error_at (location, "%<::%E%> is not a class or namespace", name);
2300             break;
2301           case NLE_NOT_CXX98:
2302             error_at (location,
2303                       "%<::%E%> is not a class, namespace, or enumeration",
2304                       name);
2305             break;
2306           default:
2307             gcc_unreachable ();
2308         }
2309     }
2310   else
2311     {
2312       switch (desired)
2313         {
2314           case NLE_TYPE:
2315             error_at (location, "%qE is not a type", name);
2316             break;
2317           case NLE_CXX98:
2318             error_at (location, "%qE is not a class or namespace", name);
2319             break;
2320           case NLE_NOT_CXX98:
2321             error_at (location,
2322                       "%qE is not a class, namespace, or enumeration", name);
2323             break;
2324           default:
2325             gcc_unreachable ();
2326         }
2327     }
2328 }
2329
2330 /* If we are parsing tentatively, remember that an error has occurred
2331    during this tentative parse.  Returns true if the error was
2332    simulated; false if a message should be issued by the caller.  */
2333
2334 static bool
2335 cp_parser_simulate_error (cp_parser* parser)
2336 {
2337   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2338     {
2339       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2340       return true;
2341     }
2342   return false;
2343 }
2344
2345 /* Check for repeated decl-specifiers.  */
2346
2347 static void
2348 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2349                            location_t location)
2350 {
2351   int ds;
2352
2353   for (ds = ds_first; ds != ds_last; ++ds)
2354     {
2355       unsigned count = decl_specs->specs[ds];
2356       if (count < 2)
2357         continue;
2358       /* The "long" specifier is a special case because of "long long".  */
2359       if (ds == ds_long)
2360         {
2361           if (count > 2)
2362             error_at (location, "%<long long long%> is too long for GCC");
2363           else 
2364             pedwarn_cxx98 (location, OPT_Wlong_long, 
2365                            "ISO C++ 1998 does not support %<long long%>");
2366         }
2367       else if (count > 1)
2368         {
2369           static const char *const decl_spec_names[] = {
2370             "signed",
2371             "unsigned",
2372             "short",
2373             "long",
2374             "const",
2375             "volatile",
2376             "restrict",
2377             "inline",
2378             "virtual",
2379             "explicit",
2380             "friend",
2381             "typedef",
2382             "constexpr",
2383             "__complex",
2384             "__thread"
2385           };
2386           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2387         }
2388     }
2389 }
2390
2391 /* This function is called when a type is defined.  If type
2392    definitions are forbidden at this point, an error message is
2393    issued.  */
2394
2395 static bool
2396 cp_parser_check_type_definition (cp_parser* parser)
2397 {
2398   /* If types are forbidden here, issue a message.  */
2399   if (parser->type_definition_forbidden_message)
2400     {
2401       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2402          in the message need to be interpreted.  */
2403       error (parser->type_definition_forbidden_message);
2404       return false;
2405     }
2406   return true;
2407 }
2408
2409 /* This function is called when the DECLARATOR is processed.  The TYPE
2410    was a type defined in the decl-specifiers.  If it is invalid to
2411    define a type in the decl-specifiers for DECLARATOR, an error is
2412    issued. TYPE_LOCATION is the location of TYPE and is used
2413    for error reporting.  */
2414
2415 static void
2416 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2417                                                tree type, location_t type_location)
2418 {
2419   /* [dcl.fct] forbids type definitions in return types.
2420      Unfortunately, it's not easy to know whether or not we are
2421      processing a return type until after the fact.  */
2422   while (declarator
2423          && (declarator->kind == cdk_pointer
2424              || declarator->kind == cdk_reference
2425              || declarator->kind == cdk_ptrmem))
2426     declarator = declarator->declarator;
2427   if (declarator
2428       && declarator->kind == cdk_function)
2429     {
2430       error_at (type_location,
2431                 "new types may not be defined in a return type");
2432       inform (type_location, 
2433               "(perhaps a semicolon is missing after the definition of %qT)",
2434               type);
2435     }
2436 }
2437
2438 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2439    "<" in any valid C++ program.  If the next token is indeed "<",
2440    issue a message warning the user about what appears to be an
2441    invalid attempt to form a template-id. LOCATION is the location
2442    of the type-specifier (TYPE) */
2443
2444 static void
2445 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2446                                          tree type, location_t location)
2447 {
2448   cp_token_position start = 0;
2449
2450   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2451     {
2452       if (TYPE_P (type))
2453         error_at (location, "%qT is not a template", type);
2454       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2455         error_at (location, "%qE is not a template", type);
2456       else
2457         error_at (location, "invalid template-id");
2458       /* Remember the location of the invalid "<".  */
2459       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2460         start = cp_lexer_token_position (parser->lexer, true);
2461       /* Consume the "<".  */
2462       cp_lexer_consume_token (parser->lexer);
2463       /* Parse the template arguments.  */
2464       cp_parser_enclosed_template_argument_list (parser);
2465       /* Permanently remove the invalid template arguments so that
2466          this error message is not issued again.  */
2467       if (start)
2468         cp_lexer_purge_tokens_after (parser->lexer, start);
2469     }
2470 }
2471
2472 /* If parsing an integral constant-expression, issue an error message
2473    about the fact that THING appeared and return true.  Otherwise,
2474    return false.  In either case, set
2475    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2476
2477 static bool
2478 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2479                                             non_integral_constant thing)
2480 {
2481   parser->non_integral_constant_expression_p = true;
2482   if (parser->integral_constant_expression_p)
2483     {
2484       if (!parser->allow_non_integral_constant_expression_p)
2485         {
2486           const char *msg = NULL;
2487           switch (thing)
2488             {
2489               case NIC_FLOAT:
2490                 error ("floating-point literal "
2491                        "cannot appear in a constant-expression");
2492                 return true;
2493               case NIC_CAST:
2494                 error ("a cast to a type other than an integral or "
2495                        "enumeration type cannot appear in a "
2496                        "constant-expression");
2497                 return true;
2498               case NIC_TYPEID:
2499                 error ("%<typeid%> operator "
2500                        "cannot appear in a constant-expression");
2501                 return true;
2502               case NIC_NCC:
2503                 error ("non-constant compound literals "
2504                        "cannot appear in a constant-expression");
2505                 return true;
2506               case NIC_FUNC_CALL:
2507                 error ("a function call "
2508                        "cannot appear in a constant-expression");
2509                 return true;
2510               case NIC_INC:
2511                 error ("an increment "
2512                        "cannot appear in a constant-expression");
2513                 return true;
2514               case NIC_DEC:
2515                 error ("an decrement "
2516                        "cannot appear in a constant-expression");
2517                 return true;
2518               case NIC_ARRAY_REF:
2519                 error ("an array reference "
2520                        "cannot appear in a constant-expression");
2521                 return true;
2522               case NIC_ADDR_LABEL:
2523                 error ("the address of a label "
2524                        "cannot appear in a constant-expression");
2525                 return true;
2526               case NIC_OVERLOADED:
2527                 error ("calls to overloaded operators "
2528                        "cannot appear in a constant-expression");
2529                 return true;
2530               case NIC_ASSIGNMENT:
2531                 error ("an assignment cannot appear in a constant-expression");
2532                 return true;
2533               case NIC_COMMA:
2534                 error ("a comma operator "
2535                        "cannot appear in a constant-expression");
2536                 return true;
2537               case NIC_CONSTRUCTOR:
2538                 error ("a call to a constructor "
2539                        "cannot appear in a constant-expression");
2540                 return true;
2541               case NIC_THIS:
2542                 msg = "this";
2543                 break;
2544               case NIC_FUNC_NAME:
2545                 msg = "__FUNCTION__";
2546                 break;
2547               case NIC_PRETTY_FUNC:
2548                 msg = "__PRETTY_FUNCTION__";
2549                 break;
2550               case NIC_C99_FUNC:
2551                 msg = "__func__";
2552                 break;
2553               case NIC_VA_ARG:
2554                 msg = "va_arg";
2555                 break;
2556               case NIC_ARROW:
2557                 msg = "->";
2558                 break;
2559               case NIC_POINT:
2560                 msg = ".";
2561                 break;
2562               case NIC_STAR:
2563                 msg = "*";
2564                 break;
2565               case NIC_ADDR:
2566                 msg = "&";
2567                 break;
2568               case NIC_PREINCREMENT:
2569                 msg = "++";
2570                 break;
2571               case NIC_PREDECREMENT:
2572                 msg = "--";
2573                 break;
2574               case NIC_NEW:
2575                 msg = "new";
2576                 break;
2577               case NIC_DEL:
2578                 msg = "delete";
2579                 break;
2580               default:
2581                 gcc_unreachable ();
2582             }
2583           if (msg)
2584             error ("%qs cannot appear in a constant-expression", msg);
2585           return true;
2586         }
2587     }
2588   return false;
2589 }
2590
2591 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2592    qualifying scope (or NULL, if none) for ID.  This function commits
2593    to the current active tentative parse, if any.  (Otherwise, the
2594    problematic construct might be encountered again later, resulting
2595    in duplicate error messages.) LOCATION is the location of ID.  */
2596
2597 static void
2598 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2599                                       tree scope, tree id,
2600                                       location_t location)
2601 {
2602   tree decl, old_scope;
2603   /* Try to lookup the identifier.  */
2604   old_scope = parser->scope;
2605   parser->scope = scope;
2606   decl = cp_parser_lookup_name_simple (parser, id, location);
2607   parser->scope = old_scope;
2608   /* If the lookup found a template-name, it means that the user forgot
2609   to specify an argument list. Emit a useful error message.  */
2610   if (TREE_CODE (decl) == TEMPLATE_DECL)
2611     error_at (location,
2612               "invalid use of template-name %qE without an argument list",
2613               decl);
2614   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2615     error_at (location, "invalid use of destructor %qD as a type", id);
2616   else if (TREE_CODE (decl) == TYPE_DECL)
2617     /* Something like 'unsigned A a;'  */
2618     error_at (location, "invalid combination of multiple type-specifiers");
2619   else if (!parser->scope)
2620     {
2621       /* Issue an error message.  */
2622       error_at (location, "%qE does not name a type", id);
2623       /* If we're in a template class, it's possible that the user was
2624          referring to a type from a base class.  For example:
2625
2626            template <typename T> struct A { typedef T X; };
2627            template <typename T> struct B : public A<T> { X x; };
2628
2629          The user should have said "typename A<T>::X".  */
2630       if (processing_template_decl && current_class_type
2631           && TYPE_BINFO (current_class_type))
2632         {
2633           tree b;
2634
2635           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2636                b;
2637                b = TREE_CHAIN (b))
2638             {
2639               tree base_type = BINFO_TYPE (b);
2640               if (CLASS_TYPE_P (base_type)
2641                   && dependent_type_p (base_type))
2642                 {
2643                   tree field;
2644                   /* Go from a particular instantiation of the
2645                      template (which will have an empty TYPE_FIELDs),
2646                      to the main version.  */
2647                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2648                   for (field = TYPE_FIELDS (base_type);
2649                        field;
2650                        field = TREE_CHAIN (field))
2651                     if (TREE_CODE (field) == TYPE_DECL
2652                         && DECL_NAME (field) == id)
2653                       {
2654                         inform (location, 
2655                                 "(perhaps %<typename %T::%E%> was intended)",
2656                                 BINFO_TYPE (b), id);
2657                         break;
2658                       }
2659                   if (field)
2660                     break;
2661                 }
2662             }
2663         }
2664     }
2665   /* Here we diagnose qualified-ids where the scope is actually correct,
2666      but the identifier does not resolve to a valid type name.  */
2667   else if (parser->scope != error_mark_node)
2668     {
2669       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2670         error_at (location, "%qE in namespace %qE does not name a type",
2671                   id, parser->scope);
2672       else if (CLASS_TYPE_P (parser->scope)
2673                && constructor_name_p (id, parser->scope))
2674         {
2675           /* A<T>::A<T>() */
2676           error_at (location, "%<%T::%E%> names the constructor, not"
2677                     " the type", parser->scope, id);
2678           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2679             error_at (location, "and %qT has no template constructors",
2680                       parser->scope);
2681         }
2682       else if (TYPE_P (parser->scope)
2683                && dependent_scope_p (parser->scope))
2684         error_at (location, "need %<typename%> before %<%T::%E%> because "
2685                   "%qT is a dependent scope",
2686                   parser->scope, id, parser->scope);
2687       else if (TYPE_P (parser->scope))
2688         error_at (location, "%qE in class %qT does not name a type",
2689                   id, parser->scope);
2690       else
2691         gcc_unreachable ();
2692     }
2693   cp_parser_commit_to_tentative_parse (parser);
2694 }
2695
2696 /* Check for a common situation where a type-name should be present,
2697    but is not, and issue a sensible error message.  Returns true if an
2698    invalid type-name was detected.
2699
2700    The situation handled by this function are variable declarations of the
2701    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2702    Usually, `ID' should name a type, but if we got here it means that it
2703    does not. We try to emit the best possible error message depending on
2704    how exactly the id-expression looks like.  */
2705
2706 static bool
2707 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2708 {
2709   tree id;
2710   cp_token *token = cp_lexer_peek_token (parser->lexer);
2711
2712   /* Avoid duplicate error about ambiguous lookup.  */
2713   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2714     {
2715       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2716       if (next->type == CPP_NAME && next->ambiguous_p)
2717         goto out;
2718     }
2719
2720   cp_parser_parse_tentatively (parser);
2721   id = cp_parser_id_expression (parser,
2722                                 /*template_keyword_p=*/false,
2723                                 /*check_dependency_p=*/true,
2724                                 /*template_p=*/NULL,
2725                                 /*declarator_p=*/true,
2726                                 /*optional_p=*/false);
2727   /* If the next token is a (, this is a function with no explicit return
2728      type, i.e. constructor, destructor or conversion op.  */
2729   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2730       || TREE_CODE (id) == TYPE_DECL)
2731     {
2732       cp_parser_abort_tentative_parse (parser);
2733       return false;
2734     }
2735   if (!cp_parser_parse_definitely (parser))
2736     return false;
2737
2738   /* Emit a diagnostic for the invalid type.  */
2739   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2740                                         id, token->location);
2741  out:
2742   /* If we aren't in the middle of a declarator (i.e. in a
2743      parameter-declaration-clause), skip to the end of the declaration;
2744      there's no point in trying to process it.  */
2745   if (!parser->in_declarator_p)
2746     cp_parser_skip_to_end_of_block_or_statement (parser);
2747   return true;
2748 }
2749
2750 /* Consume tokens up to, and including, the next non-nested closing `)'.
2751    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2752    are doing error recovery. Returns -1 if OR_COMMA is true and we
2753    found an unnested comma.  */
2754
2755 static int
2756 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2757                                        bool recovering,
2758                                        bool or_comma,
2759                                        bool consume_paren)
2760 {
2761   unsigned paren_depth = 0;
2762   unsigned brace_depth = 0;
2763   unsigned square_depth = 0;
2764
2765   if (recovering && !or_comma
2766       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2767     return 0;
2768
2769   while (true)
2770     {
2771       cp_token * token = cp_lexer_peek_token (parser->lexer);
2772
2773       switch (token->type)
2774         {
2775         case CPP_EOF:
2776         case CPP_PRAGMA_EOL:
2777           /* If we've run out of tokens, then there is no closing `)'.  */
2778           return 0;
2779
2780         /* This is good for lambda expression capture-lists.  */
2781         case CPP_OPEN_SQUARE:
2782           ++square_depth;
2783           break;
2784         case CPP_CLOSE_SQUARE:
2785           if (!square_depth--)
2786             return 0;
2787           break;
2788
2789         case CPP_SEMICOLON:
2790           /* This matches the processing in skip_to_end_of_statement.  */
2791           if (!brace_depth)
2792             return 0;
2793           break;
2794
2795         case CPP_OPEN_BRACE:
2796           ++brace_depth;
2797           break;
2798         case CPP_CLOSE_BRACE:
2799           if (!brace_depth--)
2800             return 0;
2801           break;
2802
2803         case CPP_COMMA:
2804           if (recovering && or_comma && !brace_depth && !paren_depth
2805               && !square_depth)
2806             return -1;
2807           break;
2808
2809         case CPP_OPEN_PAREN:
2810           if (!brace_depth)
2811             ++paren_depth;
2812           break;
2813
2814         case CPP_CLOSE_PAREN:
2815           if (!brace_depth && !paren_depth--)
2816             {
2817               if (consume_paren)
2818                 cp_lexer_consume_token (parser->lexer);
2819               return 1;
2820             }
2821           break;
2822
2823         default:
2824           break;
2825         }
2826
2827       /* Consume the token.  */
2828       cp_lexer_consume_token (parser->lexer);
2829     }
2830 }
2831
2832 /* Consume tokens until we reach the end of the current statement.
2833    Normally, that will be just before consuming a `;'.  However, if a
2834    non-nested `}' comes first, then we stop before consuming that.  */
2835
2836 static void
2837 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2838 {
2839   unsigned nesting_depth = 0;
2840
2841   while (true)
2842     {
2843       cp_token *token = cp_lexer_peek_token (parser->lexer);
2844
2845       switch (token->type)
2846         {
2847         case CPP_EOF:
2848         case CPP_PRAGMA_EOL:
2849           /* If we've run out of tokens, stop.  */
2850           return;
2851
2852         case CPP_SEMICOLON:
2853           /* If the next token is a `;', we have reached the end of the
2854              statement.  */
2855           if (!nesting_depth)
2856             return;
2857           break;
2858
2859         case CPP_CLOSE_BRACE:
2860           /* If this is a non-nested '}', stop before consuming it.
2861              That way, when confronted with something like:
2862
2863                { 3 + }
2864
2865              we stop before consuming the closing '}', even though we
2866              have not yet reached a `;'.  */
2867           if (nesting_depth == 0)
2868             return;
2869
2870           /* If it is the closing '}' for a block that we have
2871              scanned, stop -- but only after consuming the token.
2872              That way given:
2873
2874                 void f g () { ... }
2875                 typedef int I;
2876
2877              we will stop after the body of the erroneously declared
2878              function, but before consuming the following `typedef'
2879              declaration.  */
2880           if (--nesting_depth == 0)
2881             {
2882               cp_lexer_consume_token (parser->lexer);
2883               return;
2884             }
2885
2886         case CPP_OPEN_BRACE:
2887           ++nesting_depth;
2888           break;
2889
2890         default:
2891           break;
2892         }
2893
2894       /* Consume the token.  */
2895       cp_lexer_consume_token (parser->lexer);
2896     }
2897 }
2898
2899 /* This function is called at the end of a statement or declaration.
2900    If the next token is a semicolon, it is consumed; otherwise, error
2901    recovery is attempted.  */
2902
2903 static void
2904 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2905 {
2906   /* Look for the trailing `;'.  */
2907   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2908     {
2909       /* If there is additional (erroneous) input, skip to the end of
2910          the statement.  */
2911       cp_parser_skip_to_end_of_statement (parser);
2912       /* If the next token is now a `;', consume it.  */
2913       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2914         cp_lexer_consume_token (parser->lexer);
2915     }
2916 }
2917
2918 /* Skip tokens until we have consumed an entire block, or until we
2919    have consumed a non-nested `;'.  */
2920
2921 static void
2922 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2923 {
2924   int nesting_depth = 0;
2925
2926   while (nesting_depth >= 0)
2927     {
2928       cp_token *token = cp_lexer_peek_token (parser->lexer);
2929
2930       switch (token->type)
2931         {
2932         case CPP_EOF:
2933         case CPP_PRAGMA_EOL:
2934           /* If we've run out of tokens, stop.  */
2935           return;
2936
2937         case CPP_SEMICOLON:
2938           /* Stop if this is an unnested ';'. */
2939           if (!nesting_depth)
2940             nesting_depth = -1;
2941           break;
2942
2943         case CPP_CLOSE_BRACE:
2944           /* Stop if this is an unnested '}', or closes the outermost
2945              nesting level.  */
2946           nesting_depth--;
2947           if (nesting_depth < 0)
2948             return;
2949           if (!nesting_depth)
2950             nesting_depth = -1;
2951           break;
2952
2953         case CPP_OPEN_BRACE:
2954           /* Nest. */
2955           nesting_depth++;
2956           break;
2957
2958         default:
2959           break;
2960         }
2961
2962       /* Consume the token.  */
2963       cp_lexer_consume_token (parser->lexer);
2964     }
2965 }
2966
2967 /* Skip tokens until a non-nested closing curly brace is the next
2968    token, or there are no more tokens. Return true in the first case,
2969    false otherwise.  */
2970
2971 static bool
2972 cp_parser_skip_to_closing_brace (cp_parser *parser)
2973 {
2974   unsigned nesting_depth = 0;
2975
2976   while (true)
2977     {
2978       cp_token *token = cp_lexer_peek_token (parser->lexer);
2979
2980       switch (token->type)
2981         {
2982         case CPP_EOF:
2983         case CPP_PRAGMA_EOL:
2984           /* If we've run out of tokens, stop.  */
2985           return false;
2986
2987         case CPP_CLOSE_BRACE:
2988           /* If the next token is a non-nested `}', then we have reached
2989              the end of the current block.  */
2990           if (nesting_depth-- == 0)
2991             return true;
2992           break;
2993
2994         case CPP_OPEN_BRACE:
2995           /* If it the next token is a `{', then we are entering a new
2996              block.  Consume the entire block.  */
2997           ++nesting_depth;
2998           break;
2999
3000         default:
3001           break;
3002         }
3003
3004       /* Consume the token.  */
3005       cp_lexer_consume_token (parser->lexer);
3006     }
3007 }
3008
3009 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3010    parameter is the PRAGMA token, allowing us to purge the entire pragma
3011    sequence.  */
3012
3013 static void
3014 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3015 {
3016   cp_token *token;
3017
3018   parser->lexer->in_pragma = false;
3019
3020   do
3021     token = cp_lexer_consume_token (parser->lexer);
3022   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3023
3024   /* Ensure that the pragma is not parsed again.  */
3025   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3026 }
3027
3028 /* Require pragma end of line, resyncing with it as necessary.  The
3029    arguments are as for cp_parser_skip_to_pragma_eol.  */
3030
3031 static void
3032 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3033 {
3034   parser->lexer->in_pragma = false;
3035   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3036     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3037 }
3038
3039 /* This is a simple wrapper around make_typename_type. When the id is
3040    an unresolved identifier node, we can provide a superior diagnostic
3041    using cp_parser_diagnose_invalid_type_name.  */
3042
3043 static tree
3044 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3045                               tree id, location_t id_location)
3046 {
3047   tree result;
3048   if (TREE_CODE (id) == IDENTIFIER_NODE)
3049     {
3050       result = make_typename_type (scope, id, typename_type,
3051                                    /*complain=*/tf_none);
3052       if (result == error_mark_node)
3053         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3054       return result;
3055     }
3056   return make_typename_type (scope, id, typename_type, tf_error);
3057 }
3058
3059 /* This is a wrapper around the
3060    make_{pointer,ptrmem,reference}_declarator functions that decides
3061    which one to call based on the CODE and CLASS_TYPE arguments. The
3062    CODE argument should be one of the values returned by
3063    cp_parser_ptr_operator. */
3064 static cp_declarator *
3065 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3066                                     cp_cv_quals cv_qualifiers,
3067                                     cp_declarator *target)
3068 {
3069   if (code == ERROR_MARK)
3070     return cp_error_declarator;
3071
3072   if (code == INDIRECT_REF)
3073     if (class_type == NULL_TREE)
3074       return make_pointer_declarator (cv_qualifiers, target);
3075     else
3076       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3077   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3078     return make_reference_declarator (cv_qualifiers, target, false);
3079   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3080     return make_reference_declarator (cv_qualifiers, target, true);
3081   gcc_unreachable ();
3082 }
3083
3084 /* Create a new C++ parser.  */
3085
3086 static cp_parser *
3087 cp_parser_new (void)
3088 {
3089   cp_parser *parser;
3090   cp_lexer *lexer;
3091   unsigned i;
3092
3093   /* cp_lexer_new_main is called before calling ggc_alloc because
3094      cp_lexer_new_main might load a PCH file.  */
3095   lexer = cp_lexer_new_main ();
3096
3097   /* Initialize the binops_by_token so that we can get the tree
3098      directly from the token.  */
3099   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3100     binops_by_token[binops[i].token_type] = binops[i];
3101
3102   parser = GGC_CNEW (cp_parser);
3103   parser->lexer = lexer;
3104   parser->context = cp_parser_context_new (NULL);
3105
3106   /* For now, we always accept GNU extensions.  */
3107   parser->allow_gnu_extensions_p = 1;
3108
3109   /* The `>' token is a greater-than operator, not the end of a
3110      template-id.  */
3111   parser->greater_than_is_operator_p = true;
3112
3113   parser->default_arg_ok_p = true;
3114
3115   /* We are not parsing a constant-expression.  */
3116   parser->integral_constant_expression_p = false;
3117   parser->allow_non_integral_constant_expression_p = false;
3118   parser->non_integral_constant_expression_p = false;
3119
3120   /* Local variable names are not forbidden.  */
3121   parser->local_variables_forbidden_p = false;
3122
3123   /* We are not processing an `extern "C"' declaration.  */
3124   parser->in_unbraced_linkage_specification_p = false;
3125
3126   /* We are not processing a declarator.  */
3127   parser->in_declarator_p = false;
3128
3129   /* We are not processing a template-argument-list.  */
3130   parser->in_template_argument_list_p = false;
3131
3132   /* We are not in an iteration statement.  */
3133   parser->in_statement = 0;
3134
3135   /* We are not in a switch statement.  */
3136   parser->in_switch_statement_p = false;
3137
3138   /* We are not parsing a type-id inside an expression.  */
3139   parser->in_type_id_in_expr_p = false;
3140
3141   /* Declarations aren't implicitly extern "C".  */
3142   parser->implicit_extern_c = false;
3143
3144   /* String literals should be translated to the execution character set.  */
3145   parser->translate_strings_p = true;
3146
3147   /* We are not parsing a function body.  */
3148   parser->in_function_body = false;
3149
3150   /* The unparsed function queue is empty.  */
3151   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
3152
3153   /* There are no classes being defined.  */
3154   parser->num_classes_being_defined = 0;
3155
3156   /* No template parameters apply.  */
3157   parser->num_template_parameter_lists = 0;
3158
3159   return parser;
3160 }
3161
3162 /* Create a cp_lexer structure which will emit the tokens in CACHE
3163    and push it onto the parser's lexer stack.  This is used for delayed
3164    parsing of in-class method bodies and default arguments, and should
3165    not be confused with tentative parsing.  */
3166 static void
3167 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3168 {
3169   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3170   lexer->next = parser->lexer;
3171   parser->lexer = lexer;
3172
3173   /* Move the current source position to that of the first token in the
3174      new lexer.  */
3175   cp_lexer_set_source_position_from_token (lexer->next_token);
3176 }
3177
3178 /* Pop the top lexer off the parser stack.  This is never used for the
3179    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3180 static void
3181 cp_parser_pop_lexer (cp_parser *parser)
3182 {
3183   cp_lexer *lexer = parser->lexer;
3184   parser->lexer = lexer->next;
3185   cp_lexer_destroy (lexer);
3186
3187   /* Put the current source position back where it was before this
3188      lexer was pushed.  */
3189   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3190 }
3191
3192 /* Lexical conventions [gram.lex]  */
3193
3194 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3195    identifier.  */
3196
3197 static tree
3198 cp_parser_identifier (cp_parser* parser)
3199 {
3200   cp_token *token;
3201
3202   /* Look for the identifier.  */
3203   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3204   /* Return the value.  */
3205   return token ? token->u.value : error_mark_node;
3206 }
3207
3208 /* Parse a sequence of adjacent string constants.  Returns a
3209    TREE_STRING representing the combined, nul-terminated string
3210    constant.  If TRANSLATE is true, translate the string to the
3211    execution character set.  If WIDE_OK is true, a wide string is
3212    invalid here.
3213
3214    C++98 [lex.string] says that if a narrow string literal token is
3215    adjacent to a wide string literal token, the behavior is undefined.
3216    However, C99 6.4.5p4 says that this results in a wide string literal.
3217    We follow C99 here, for consistency with the C front end.
3218
3219    This code is largely lifted from lex_string() in c-lex.c.
3220
3221    FUTURE: ObjC++ will need to handle @-strings here.  */
3222 static tree
3223 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3224 {
3225   tree value;
3226   size_t count;
3227   struct obstack str_ob;
3228   cpp_string str, istr, *strs;
3229   cp_token *tok;
3230   enum cpp_ttype type;
3231
3232   tok = cp_lexer_peek_token (parser->lexer);
3233   if (!cp_parser_is_string_literal (tok))
3234     {
3235       cp_parser_error (parser, "expected string-literal");
3236       return error_mark_node;
3237     }
3238
3239   type = tok->type;
3240
3241   /* Try to avoid the overhead of creating and destroying an obstack
3242      for the common case of just one string.  */
3243   if (!cp_parser_is_string_literal
3244       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3245     {
3246       cp_lexer_consume_token (parser->lexer);
3247
3248       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3249       str.len = TREE_STRING_LENGTH (tok->u.value);
3250       count = 1;
3251
3252       strs = &str;
3253     }
3254   else
3255     {
3256       gcc_obstack_init (&str_ob);
3257       count = 0;
3258
3259       do
3260         {
3261           cp_lexer_consume_token (parser->lexer);
3262           count++;
3263           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3264           str.len = TREE_STRING_LENGTH (tok->u.value);
3265
3266           if (type != tok->type)
3267             {
3268               if (type == CPP_STRING)
3269                 type = tok->type;
3270               else if (tok->type != CPP_STRING)
3271                 error_at (tok->location,
3272                           "unsupported non-standard concatenation "
3273                           "of string literals");
3274             }
3275
3276           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3277
3278           tok = cp_lexer_peek_token (parser->lexer);
3279         }
3280       while (cp_parser_is_string_literal (tok));
3281
3282       strs = (cpp_string *) obstack_finish (&str_ob);
3283     }
3284
3285   if (type != CPP_STRING && !wide_ok)
3286     {
3287       cp_parser_error (parser, "a wide string is invalid in this context");
3288       type = CPP_STRING;
3289     }
3290
3291   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3292       (parse_in, strs, count, &istr, type))
3293     {
3294       value = build_string (istr.len, (const char *)istr.text);
3295       free (CONST_CAST (unsigned char *, istr.text));
3296
3297       switch (type)
3298         {
3299         default:
3300         case CPP_STRING:
3301         case CPP_UTF8STRING:
3302           TREE_TYPE (value) = char_array_type_node;
3303           break;
3304         case CPP_STRING16:
3305           TREE_TYPE (value) = char16_array_type_node;
3306           break;
3307         case CPP_STRING32:
3308           TREE_TYPE (value) = char32_array_type_node;
3309           break;
3310         case CPP_WSTRING:
3311           TREE_TYPE (value) = wchar_array_type_node;
3312           break;
3313         }
3314
3315       value = fix_string_type (value);
3316     }
3317   else
3318     /* cpp_interpret_string has issued an error.  */
3319     value = error_mark_node;
3320
3321   if (count > 1)
3322     obstack_free (&str_ob, 0);
3323
3324   return value;
3325 }
3326
3327
3328 /* Basic concepts [gram.basic]  */
3329
3330 /* Parse a translation-unit.
3331
3332    translation-unit:
3333      declaration-seq [opt]
3334
3335    Returns TRUE if all went well.  */
3336
3337 static bool
3338 cp_parser_translation_unit (cp_parser* parser)
3339 {
3340   /* The address of the first non-permanent object on the declarator
3341      obstack.  */
3342   static void *declarator_obstack_base;
3343
3344   bool success;
3345
3346   /* Create the declarator obstack, if necessary.  */
3347   if (!cp_error_declarator)
3348     {
3349       gcc_obstack_init (&declarator_obstack);
3350       /* Create the error declarator.  */
3351       cp_error_declarator = make_declarator (cdk_error);
3352       /* Create the empty parameter list.  */
3353       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3354       /* Remember where the base of the declarator obstack lies.  */
3355       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3356     }
3357
3358   cp_parser_declaration_seq_opt (parser);
3359
3360   /* If there are no tokens left then all went well.  */
3361   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3362     {
3363       /* Get rid of the token array; we don't need it any more.  */
3364       cp_lexer_destroy (parser->lexer);
3365       parser->lexer = NULL;
3366
3367       /* This file might have been a context that's implicitly extern
3368          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3369       if (parser->implicit_extern_c)
3370         {
3371           pop_lang_context ();
3372           parser->implicit_extern_c = false;
3373         }
3374
3375       /* Finish up.  */
3376       finish_translation_unit ();
3377
3378       success = true;
3379     }
3380   else
3381     {
3382       cp_parser_error (parser, "expected declaration");
3383       success = false;
3384     }
3385
3386   /* Make sure the declarator obstack was fully cleaned up.  */
3387   gcc_assert (obstack_next_free (&declarator_obstack)
3388               == declarator_obstack_base);
3389
3390   /* All went well.  */
3391   return success;
3392 }
3393
3394 /* Expressions [gram.expr] */
3395
3396 /* Parse a primary-expression.
3397
3398    primary-expression:
3399      literal
3400      this
3401      ( expression )
3402      id-expression
3403
3404    GNU Extensions:
3405
3406    primary-expression:
3407      ( compound-statement )
3408      __builtin_va_arg ( assignment-expression , type-id )
3409      __builtin_offsetof ( type-id , offsetof-expression )
3410
3411    C++ Extensions:
3412      __has_nothrow_assign ( type-id )   
3413      __has_nothrow_constructor ( type-id )
3414      __has_nothrow_copy ( type-id )
3415      __has_trivial_assign ( type-id )   
3416      __has_trivial_constructor ( type-id )
3417      __has_trivial_copy ( type-id )
3418      __has_trivial_destructor ( type-id )
3419      __has_virtual_destructor ( type-id )     
3420      __is_abstract ( type-id )
3421      __is_base_of ( type-id , type-id )
3422      __is_class ( type-id )
3423      __is_convertible_to ( type-id , type-id )     
3424      __is_empty ( type-id )
3425      __is_enum ( type-id )
3426      __is_pod ( type-id )
3427      __is_polymorphic ( type-id )
3428      __is_union ( type-id )
3429
3430    Objective-C++ Extension:
3431
3432    primary-expression:
3433      objc-expression
3434
3435    literal:
3436      __null
3437
3438    ADDRESS_P is true iff this expression was immediately preceded by
3439    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3440    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3441    true iff this expression is a template argument.
3442
3443    Returns a representation of the expression.  Upon return, *IDK
3444    indicates what kind of id-expression (if any) was present.  */
3445
3446 static tree
3447 cp_parser_primary_expression (cp_parser *parser,
3448                               bool address_p,
3449                               bool cast_p,
3450                               bool template_arg_p,
3451                               cp_id_kind *idk)
3452 {
3453   cp_token *token = NULL;
3454
3455   /* Assume the primary expression is not an id-expression.  */
3456   *idk = CP_ID_KIND_NONE;
3457
3458   /* Peek at the next token.  */
3459   token = cp_lexer_peek_token (parser->lexer);
3460   switch (token->type)
3461     {
3462       /* literal:
3463            integer-literal
3464            character-literal
3465            floating-literal
3466            string-literal
3467            boolean-literal  */
3468     case CPP_CHAR:
3469     case CPP_CHAR16:
3470     case CPP_CHAR32:
3471     case CPP_WCHAR:
3472     case CPP_NUMBER:
3473       token = cp_lexer_consume_token (parser->lexer);
3474       if (TREE_CODE (token->u.value) == FIXED_CST)
3475         {
3476           error_at (token->location,
3477                     "fixed-point types not supported in C++");
3478           return error_mark_node;
3479         }
3480       /* Floating-point literals are only allowed in an integral
3481          constant expression if they are cast to an integral or
3482          enumeration type.  */
3483       if (TREE_CODE (token->u.value) == REAL_CST
3484           && parser->integral_constant_expression_p
3485           && pedantic)
3486         {
3487           /* CAST_P will be set even in invalid code like "int(2.7 +
3488              ...)".   Therefore, we have to check that the next token
3489              is sure to end the cast.  */
3490           if (cast_p)
3491             {
3492               cp_token *next_token;
3493
3494               next_token = cp_lexer_peek_token (parser->lexer);
3495               if (/* The comma at the end of an
3496                      enumerator-definition.  */
3497                   next_token->type != CPP_COMMA
3498                   /* The curly brace at the end of an enum-specifier.  */
3499                   && next_token->type != CPP_CLOSE_BRACE
3500                   /* The end of a statement.  */
3501                   && next_token->type != CPP_SEMICOLON
3502                   /* The end of the cast-expression.  */
3503                   && next_token->type != CPP_CLOSE_PAREN
3504                   /* The end of an array bound.  */
3505                   && next_token->type != CPP_CLOSE_SQUARE
3506                   /* The closing ">" in a template-argument-list.  */
3507                   && (next_token->type != CPP_GREATER
3508                       || parser->greater_than_is_operator_p)
3509                   /* C++0x only: A ">>" treated like two ">" tokens,
3510                      in a template-argument-list.  */
3511                   && (next_token->type != CPP_RSHIFT
3512                       || (cxx_dialect == cxx98)
3513                       || parser->greater_than_is_operator_p))
3514                 cast_p = false;
3515             }
3516
3517           /* If we are within a cast, then the constraint that the
3518              cast is to an integral or enumeration type will be
3519              checked at that point.  If we are not within a cast, then
3520              this code is invalid.  */
3521           if (!cast_p)
3522             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3523         }
3524       return token->u.value;
3525
3526     case CPP_STRING:
3527     case CPP_STRING16:
3528     case CPP_STRING32:
3529     case CPP_WSTRING:
3530     case CPP_UTF8STRING:
3531       /* ??? Should wide strings be allowed when parser->translate_strings_p
3532          is false (i.e. in attributes)?  If not, we can kill the third
3533          argument to cp_parser_string_literal.  */
3534       return cp_parser_string_literal (parser,
3535                                        parser->translate_strings_p,
3536                                        true);
3537
3538     case CPP_OPEN_PAREN:
3539       {
3540         tree expr;
3541         bool saved_greater_than_is_operator_p;
3542
3543         /* Consume the `('.  */
3544         cp_lexer_consume_token (parser->lexer);
3545         /* Within a parenthesized expression, a `>' token is always
3546            the greater-than operator.  */
3547         saved_greater_than_is_operator_p
3548           = parser->greater_than_is_operator_p;
3549         parser->greater_than_is_operator_p = true;
3550         /* If we see `( { ' then we are looking at the beginning of
3551            a GNU statement-expression.  */
3552         if (cp_parser_allow_gnu_extensions_p (parser)
3553             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3554           {
3555             /* Statement-expressions are not allowed by the standard.  */
3556             pedwarn (token->location, OPT_pedantic, 
3557                      "ISO C++ forbids braced-groups within expressions");
3558
3559             /* And they're not allowed outside of a function-body; you
3560                cannot, for example, write:
3561
3562                  int i = ({ int j = 3; j + 1; });
3563
3564                at class or namespace scope.  */
3565             if (!parser->in_function_body
3566                 || parser->in_template_argument_list_p)
3567               {
3568                 error_at (token->location,
3569                           "statement-expressions are not allowed outside "
3570                           "functions nor in template-argument lists");
3571                 cp_parser_skip_to_end_of_block_or_statement (parser);
3572                 expr = error_mark_node;
3573               }
3574             else
3575               {
3576                 /* Start the statement-expression.  */
3577                 expr = begin_stmt_expr ();
3578                 /* Parse the compound-statement.  */
3579                 cp_parser_compound_statement (parser, expr, false);
3580                 /* Finish up.  */
3581                 expr = finish_stmt_expr (expr, false);
3582               }
3583           }
3584         else
3585           {
3586             /* Parse the parenthesized expression.  */
3587             expr = cp_parser_expression (parser, cast_p, idk);
3588             /* Let the front end know that this expression was
3589                enclosed in parentheses. This matters in case, for
3590                example, the expression is of the form `A::B', since
3591                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3592                not.  */
3593             finish_parenthesized_expr (expr);
3594           }
3595         /* The `>' token might be the end of a template-id or
3596            template-parameter-list now.  */
3597         parser->greater_than_is_operator_p
3598           = saved_greater_than_is_operator_p;
3599         /* Consume the `)'.  */
3600         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3601           cp_parser_skip_to_end_of_statement (parser);
3602
3603         return expr;
3604       }
3605
3606     case CPP_OPEN_SQUARE:
3607       if (c_dialect_objc ())
3608         /* We have an Objective-C++ message. */
3609         return cp_parser_objc_expression (parser);
3610       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3611       return cp_parser_lambda_expression (parser);
3612
3613     case CPP_OBJC_STRING:
3614       if (c_dialect_objc ())
3615         /* We have an Objective-C++ string literal. */
3616         return cp_parser_objc_expression (parser);
3617       cp_parser_error (parser, "expected primary-expression");
3618       return error_mark_node;
3619
3620     case CPP_KEYWORD:
3621       switch (token->keyword)
3622         {
3623           /* These two are the boolean literals.  */
3624         case RID_TRUE:
3625           cp_lexer_consume_token (parser->lexer);
3626           return boolean_true_node;
3627         case RID_FALSE:
3628           cp_lexer_consume_token (parser->lexer);
3629           return boolean_false_node;
3630
3631           /* The `__null' literal.  */
3632         case RID_NULL:
3633           cp_lexer_consume_token (parser->lexer);
3634           return null_node;
3635
3636           /* The `nullptr' literal.  */
3637         case RID_NULLPTR:
3638           cp_lexer_consume_token (parser->lexer);
3639           return nullptr_node;
3640
3641           /* Recognize the `this' keyword.  */
3642         case RID_THIS:
3643           cp_lexer_consume_token (parser->lexer);
3644           if (parser->local_variables_forbidden_p)
3645             {
3646               error_at (token->location,
3647                         "%<this%> may not be used in this context");
3648               return error_mark_node;
3649             }
3650           /* Pointers cannot appear in constant-expressions.  */
3651           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3652             return error_mark_node;
3653           return finish_this_expr ();
3654
3655           /* The `operator' keyword can be the beginning of an
3656              id-expression.  */
3657         case RID_OPERATOR:
3658           goto id_expression;
3659
3660         case RID_FUNCTION_NAME:
3661         case RID_PRETTY_FUNCTION_NAME:
3662         case RID_C99_FUNCTION_NAME:
3663           {
3664             non_integral_constant name;
3665
3666             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3667                __func__ are the names of variables -- but they are
3668                treated specially.  Therefore, they are handled here,
3669                rather than relying on the generic id-expression logic
3670                below.  Grammatically, these names are id-expressions.
3671
3672                Consume the token.  */
3673             token = cp_lexer_consume_token (parser->lexer);
3674
3675             switch (token->keyword)
3676               {
3677               case RID_FUNCTION_NAME:
3678                 name = NIC_FUNC_NAME;
3679                 break;
3680               case RID_PRETTY_FUNCTION_NAME:
3681                 name = NIC_PRETTY_FUNC;
3682                 break;
3683               case RID_C99_FUNCTION_NAME:
3684                 name = NIC_C99_FUNC;
3685                 break;
3686               default:
3687                 gcc_unreachable ();
3688               }
3689
3690             if (cp_parser_non_integral_constant_expression (parser, name))
3691               return error_mark_node;
3692
3693             /* Look up the name.  */
3694             return finish_fname (token->u.value);
3695           }
3696
3697         case RID_VA_ARG:
3698           {
3699             tree expression;
3700             tree type;
3701
3702             /* The `__builtin_va_arg' construct is used to handle
3703                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3704             cp_lexer_consume_token (parser->lexer);
3705             /* Look for the opening `('.  */
3706             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3707             /* Now, parse the assignment-expression.  */
3708             expression = cp_parser_assignment_expression (parser,
3709                                                           /*cast_p=*/false, NULL);
3710             /* Look for the `,'.  */
3711             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3712             /* Parse the type-id.  */
3713             type = cp_parser_type_id (parser);
3714             /* Look for the closing `)'.  */
3715             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3716             /* Using `va_arg' in a constant-expression is not
3717                allowed.  */
3718             if (cp_parser_non_integral_constant_expression (parser,NIC_VA_ARG))
3719               return error_mark_node;
3720             return build_x_va_arg (expression, type);
3721           }
3722
3723         case RID_OFFSETOF:
3724           return cp_parser_builtin_offsetof (parser);
3725
3726         case RID_HAS_NOTHROW_ASSIGN:
3727         case RID_HAS_NOTHROW_CONSTRUCTOR:
3728         case RID_HAS_NOTHROW_COPY:        
3729         case RID_HAS_TRIVIAL_ASSIGN:
3730         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3731         case RID_HAS_TRIVIAL_COPY:        
3732         case RID_HAS_TRIVIAL_DESTRUCTOR:
3733         case RID_HAS_VIRTUAL_DESTRUCTOR:
3734         case RID_IS_ABSTRACT:
3735         case RID_IS_BASE_OF:
3736         case RID_IS_CLASS:
3737         case RID_IS_CONVERTIBLE_TO:
3738         case RID_IS_EMPTY:
3739         case RID_IS_ENUM:
3740         case RID_IS_POD:
3741         case RID_IS_POLYMORPHIC:
3742         case RID_IS_STD_LAYOUT:
3743         case RID_IS_TRIVIAL:
3744         case RID_IS_UNION:
3745           return cp_parser_trait_expr (parser, token->keyword);
3746
3747         /* Objective-C++ expressions.  */
3748         case RID_AT_ENCODE:
3749         case RID_AT_PROTOCOL:
3750         case RID_AT_SELECTOR:
3751           return cp_parser_objc_expression (parser);
3752
3753         default:
3754           cp_parser_error (parser, "expected primary-expression");
3755           return error_mark_node;
3756         }
3757
3758       /* An id-expression can start with either an identifier, a
3759          `::' as the beginning of a qualified-id, or the "operator"
3760          keyword.  */
3761     case CPP_NAME:
3762     case CPP_SCOPE:
3763     case CPP_TEMPLATE_ID:
3764     case CPP_NESTED_NAME_SPECIFIER:
3765       {
3766         tree id_expression;
3767         tree decl;
3768         const char *error_msg;
3769         bool template_p;
3770         bool done;
3771         cp_token *id_expr_token;
3772
3773       id_expression:
3774         /* Parse the id-expression.  */
3775         id_expression
3776           = cp_parser_id_expression (parser,
3777                                      /*template_keyword_p=*/false,
3778                                      /*check_dependency_p=*/true,
3779                                      &template_p,
3780                                      /*declarator_p=*/false,
3781                                      /*optional_p=*/false);
3782         if (id_expression == error_mark_node)
3783           return error_mark_node;
3784         id_expr_token = token;
3785         token = cp_lexer_peek_token (parser->lexer);
3786         done = (token->type != CPP_OPEN_SQUARE
3787                 && token->type != CPP_OPEN_PAREN
3788                 && token->type != CPP_DOT
3789                 && token->type != CPP_DEREF
3790                 && token->type != CPP_PLUS_PLUS
3791                 && token->type != CPP_MINUS_MINUS);
3792         /* If we have a template-id, then no further lookup is
3793            required.  If the template-id was for a template-class, we
3794            will sometimes have a TYPE_DECL at this point.  */
3795         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3796                  || TREE_CODE (id_expression) == TYPE_DECL)
3797           decl = id_expression;
3798         /* Look up the name.  */
3799         else
3800           {
3801             tree ambiguous_decls;
3802
3803             /* If we already know that this lookup is ambiguous, then
3804                we've already issued an error message; there's no reason
3805                to check again.  */
3806             if (id_expr_token->type == CPP_NAME
3807                 && id_expr_token->ambiguous_p)
3808               {
3809                 cp_parser_simulate_error (parser);
3810                 return error_mark_node;
3811               }
3812
3813             decl = cp_parser_lookup_name (parser, id_expression,
3814                                           none_type,
3815                                           template_p,
3816                                           /*is_namespace=*/false,
3817                                           /*check_dependency=*/true,
3818                                           &ambiguous_decls,
3819                                           id_expr_token->location);
3820             /* If the lookup was ambiguous, an error will already have
3821                been issued.  */
3822             if (ambiguous_decls)
3823               return error_mark_node;
3824
3825             /* In Objective-C++, an instance variable (ivar) may be preferred
3826                to whatever cp_parser_lookup_name() found.  */
3827             decl = objc_lookup_ivar (decl, id_expression);
3828
3829             /* If name lookup gives us a SCOPE_REF, then the
3830                qualifying scope was dependent.  */
3831             if (TREE_CODE (decl) == SCOPE_REF)
3832               {
3833                 /* At this point, we do not know if DECL is a valid
3834                    integral constant expression.  We assume that it is
3835                    in fact such an expression, so that code like:
3836
3837                       template <int N> struct A {
3838                         int a[B<N>::i];
3839                       };
3840                      
3841                    is accepted.  At template-instantiation time, we
3842                    will check that B<N>::i is actually a constant.  */
3843                 return decl;
3844               }
3845             /* Check to see if DECL is a local variable in a context
3846                where that is forbidden.  */
3847             if (parser->local_variables_forbidden_p
3848                 && local_variable_p (decl))
3849               {
3850                 /* It might be that we only found DECL because we are
3851                    trying to be generous with pre-ISO scoping rules.
3852                    For example, consider:
3853
3854                      int i;
3855                      void g() {
3856                        for (int i = 0; i < 10; ++i) {}
3857                        extern void f(int j = i);
3858                      }
3859
3860                    Here, name look up will originally find the out
3861                    of scope `i'.  We need to issue a warning message,
3862                    but then use the global `i'.  */
3863                 decl = check_for_out_of_scope_variable (decl);
3864                 if (local_variable_p (decl))
3865                   {
3866                     error_at (id_expr_token->location,
3867                               "local variable %qD may not appear in this context",
3868                               decl);
3869                     return error_mark_node;
3870                   }
3871               }
3872           }
3873
3874         decl = (finish_id_expression
3875                 (id_expression, decl, parser->scope,
3876                  idk,
3877                  parser->integral_constant_expression_p,
3878                  parser->allow_non_integral_constant_expression_p,
3879                  &parser->non_integral_constant_expression_p,
3880                  template_p, done, address_p,
3881                  template_arg_p,
3882                  &error_msg,
3883                  id_expr_token->location));
3884         if (error_msg)
3885           cp_parser_error (parser, error_msg);
3886         return decl;
3887       }
3888
3889       /* Anything else is an error.  */
3890     default:
3891       cp_parser_error (parser, "expected primary-expression");
3892       return error_mark_node;
3893     }
3894 }
3895
3896 /* Parse an id-expression.
3897
3898    id-expression:
3899      unqualified-id
3900      qualified-id
3901
3902    qualified-id:
3903      :: [opt] nested-name-specifier template [opt] unqualified-id
3904      :: identifier
3905      :: operator-function-id
3906      :: template-id
3907
3908    Return a representation of the unqualified portion of the
3909    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3910    a `::' or nested-name-specifier.
3911
3912    Often, if the id-expression was a qualified-id, the caller will
3913    want to make a SCOPE_REF to represent the qualified-id.  This
3914    function does not do this in order to avoid wastefully creating
3915    SCOPE_REFs when they are not required.
3916
3917    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3918    `template' keyword.
3919
3920    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3921    uninstantiated templates.
3922
3923    If *TEMPLATE_P is non-NULL, it is set to true iff the
3924    `template' keyword is used to explicitly indicate that the entity
3925    named is a template.
3926
3927    If DECLARATOR_P is true, the id-expression is appearing as part of
3928    a declarator, rather than as part of an expression.  */
3929
3930 static tree
3931 cp_parser_id_expression (cp_parser *parser,
3932                          bool template_keyword_p,
3933                          bool check_dependency_p,
3934                          bool *template_p,
3935                          bool declarator_p,
3936                          bool optional_p)
3937 {
3938   bool global_scope_p;
3939   bool nested_name_specifier_p;
3940
3941   /* Assume the `template' keyword was not used.  */
3942   if (template_p)
3943     *template_p = template_keyword_p;
3944
3945   /* Look for the optional `::' operator.  */
3946   global_scope_p
3947     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3948        != NULL_TREE);
3949   /* Look for the optional nested-name-specifier.  */
3950   nested_name_specifier_p
3951     = (cp_parser_nested_name_specifier_opt (parser,
3952                                             /*typename_keyword_p=*/false,
3953                                             check_dependency_p,
3954                                             /*type_p=*/false,
3955                                             declarator_p)
3956        != NULL_TREE);
3957   /* If there is a nested-name-specifier, then we are looking at
3958      the first qualified-id production.  */
3959   if (nested_name_specifier_p)
3960     {
3961       tree saved_scope;
3962       tree saved_object_scope;
3963       tree saved_qualifying_scope;
3964       tree unqualified_id;
3965       bool is_template;
3966
3967       /* See if the next token is the `template' keyword.  */
3968       if (!template_p)
3969         template_p = &is_template;
3970       *template_p = cp_parser_optional_template_keyword (parser);
3971       /* Name lookup we do during the processing of the
3972          unqualified-id might obliterate SCOPE.  */
3973       saved_scope = parser->scope;
3974       saved_object_scope = parser->object_scope;
3975       saved_qualifying_scope = parser->qualifying_scope;
3976       /* Process the final unqualified-id.  */
3977       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3978                                                  check_dependency_p,
3979                                                  declarator_p,
3980                                                  /*optional_p=*/false);
3981       /* Restore the SAVED_SCOPE for our caller.  */
3982       parser->scope = saved_scope;
3983       parser->object_scope = saved_object_scope;
3984       parser->qualifying_scope = saved_qualifying_scope;
3985
3986       return unqualified_id;
3987     }
3988   /* Otherwise, if we are in global scope, then we are looking at one
3989      of the other qualified-id productions.  */
3990   else if (global_scope_p)
3991     {
3992       cp_token *token;
3993       tree id;
3994
3995       /* Peek at the next token.  */
3996       token = cp_lexer_peek_token (parser->lexer);
3997
3998       /* If it's an identifier, and the next token is not a "<", then
3999          we can avoid the template-id case.  This is an optimization
4000          for this common case.  */
4001       if (token->type == CPP_NAME
4002           && !cp_parser_nth_token_starts_template_argument_list_p
4003                (parser, 2))
4004         return cp_parser_identifier (parser);
4005
4006       cp_parser_parse_tentatively (parser);
4007       /* Try a template-id.  */
4008       id = cp_parser_template_id (parser,
4009                                   /*template_keyword_p=*/false,
4010                                   /*check_dependency_p=*/true,
4011                                   declarator_p);
4012       /* If that worked, we're done.  */
4013       if (cp_parser_parse_definitely (parser))
4014         return id;
4015
4016       /* Peek at the next token.  (Changes in the token buffer may
4017          have invalidated the pointer obtained above.)  */
4018       token = cp_lexer_peek_token (parser->lexer);
4019
4020       switch (token->type)
4021         {
4022         case CPP_NAME:
4023           return cp_parser_identifier (parser);
4024
4025         case CPP_KEYWORD:
4026           if (token->keyword == RID_OPERATOR)
4027             return cp_parser_operator_function_id (parser);
4028           /* Fall through.  */
4029
4030         default:
4031           cp_parser_error (parser, "expected id-expression");
4032           return error_mark_node;
4033         }
4034     }
4035   else
4036     return cp_parser_unqualified_id (parser, template_keyword_p,
4037                                      /*check_dependency_p=*/true,
4038                                      declarator_p,
4039                                      optional_p);
4040 }
4041
4042 /* Parse an unqualified-id.
4043
4044    unqualified-id:
4045      identifier
4046      operator-function-id
4047      conversion-function-id
4048      ~ class-name
4049      template-id
4050
4051    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4052    keyword, in a construct like `A::template ...'.
4053
4054    Returns a representation of unqualified-id.  For the `identifier'
4055    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4056    production a BIT_NOT_EXPR is returned; the operand of the
4057    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4058    other productions, see the documentation accompanying the
4059    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4060    names are looked up in uninstantiated templates.  If DECLARATOR_P
4061    is true, the unqualified-id is appearing as part of a declarator,
4062    rather than as part of an expression.  */
4063
4064 static tree
4065 cp_parser_unqualified_id (cp_parser* parser,
4066                           bool template_keyword_p,
4067                           bool check_dependency_p,
4068                           bool declarator_p,
4069                           bool optional_p)
4070 {
4071   cp_token *token;
4072
4073   /* Peek at the next token.  */
4074   token = cp_lexer_peek_token (parser->lexer);
4075
4076   switch (token->type)
4077     {
4078     case CPP_NAME:
4079       {
4080         tree id;
4081
4082         /* We don't know yet whether or not this will be a
4083            template-id.  */
4084         cp_parser_parse_tentatively (parser);
4085         /* Try a template-id.  */
4086         id = cp_parser_template_id (parser, template_keyword_p,
4087                                     check_dependency_p,
4088                                     declarator_p);
4089         /* If it worked, we're done.  */
4090         if (cp_parser_parse_definitely (parser))
4091           return id;
4092         /* Otherwise, it's an ordinary identifier.  */
4093         return cp_parser_identifier (parser);
4094       }
4095
4096     case CPP_TEMPLATE_ID:
4097       return cp_parser_template_id (parser, template_keyword_p,
4098                                     check_dependency_p,
4099                                     declarator_p);
4100
4101     case CPP_COMPL:
4102       {
4103         tree type_decl;
4104         tree qualifying_scope;
4105         tree object_scope;
4106         tree scope;
4107         bool done;
4108
4109         /* Consume the `~' token.  */
4110         cp_lexer_consume_token (parser->lexer);
4111         /* Parse the class-name.  The standard, as written, seems to
4112            say that:
4113
4114              template <typename T> struct S { ~S (); };
4115              template <typename T> S<T>::~S() {}
4116
4117            is invalid, since `~' must be followed by a class-name, but
4118            `S<T>' is dependent, and so not known to be a class.
4119            That's not right; we need to look in uninstantiated
4120            templates.  A further complication arises from:
4121
4122              template <typename T> void f(T t) {
4123                t.T::~T();
4124              }
4125
4126            Here, it is not possible to look up `T' in the scope of `T'
4127            itself.  We must look in both the current scope, and the
4128            scope of the containing complete expression.
4129
4130            Yet another issue is:
4131
4132              struct S {
4133                int S;
4134                ~S();
4135              };
4136
4137              S::~S() {}
4138
4139            The standard does not seem to say that the `S' in `~S'
4140            should refer to the type `S' and not the data member
4141            `S::S'.  */
4142
4143         /* DR 244 says that we look up the name after the "~" in the
4144            same scope as we looked up the qualifying name.  That idea
4145            isn't fully worked out; it's more complicated than that.  */
4146         scope = parser->scope;
4147         object_scope = parser->object_scope;
4148         qualifying_scope = parser->qualifying_scope;
4149
4150         /* Check for invalid scopes.  */
4151         if (scope == error_mark_node)
4152           {
4153             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4154               cp_lexer_consume_token (parser->lexer);
4155             return error_mark_node;
4156           }
4157         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4158           {
4159             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4160               error_at (token->location,
4161                         "scope %qT before %<~%> is not a class-name",
4162                         scope);
4163             cp_parser_simulate_error (parser);
4164             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4165               cp_lexer_consume_token (parser->lexer);
4166             return error_mark_node;
4167           }
4168         gcc_assert (!scope || TYPE_P (scope));
4169
4170         /* If the name is of the form "X::~X" it's OK even if X is a
4171            typedef.  */
4172         token = cp_lexer_peek_token (parser->lexer);
4173         if (scope
4174             && token->type == CPP_NAME
4175             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4176                 != CPP_LESS)
4177             && (token->u.value == TYPE_IDENTIFIER (scope)
4178                 || constructor_name_p (token->u.value, scope)))
4179           {
4180             cp_lexer_consume_token (parser->lexer);
4181             return build_nt (BIT_NOT_EXPR, scope);
4182           }
4183
4184         /* If there was an explicit qualification (S::~T), first look
4185            in the scope given by the qualification (i.e., S).
4186
4187            Note: in the calls to cp_parser_class_name below we pass
4188            typename_type so that lookup finds the injected-class-name
4189            rather than the constructor.  */
4190         done = false;
4191         type_decl = NULL_TREE;
4192         if (scope)
4193           {
4194             cp_parser_parse_tentatively (parser);
4195             type_decl = cp_parser_class_name (parser,
4196                                               /*typename_keyword_p=*/false,
4197                                               /*template_keyword_p=*/false,
4198                                               typename_type,
4199                                               /*check_dependency=*/false,
4200                                               /*class_head_p=*/false,
4201                                               declarator_p);
4202             if (cp_parser_parse_definitely (parser))
4203               done = true;
4204           }
4205         /* In "N::S::~S", look in "N" as well.  */
4206         if (!done && scope && qualifying_scope)
4207           {
4208             cp_parser_parse_tentatively (parser);
4209             parser->scope = qualifying_scope;
4210             parser->object_scope = NULL_TREE;
4211             parser->qualifying_scope = NULL_TREE;
4212             type_decl
4213               = cp_parser_class_name (parser,
4214                                       /*typename_keyword_p=*/false,
4215                                       /*template_keyword_p=*/false,
4216                                       typename_type,
4217                                       /*check_dependency=*/false,
4218                                       /*class_head_p=*/false,
4219                                       declarator_p);
4220             if (cp_parser_parse_definitely (parser))
4221               done = true;
4222           }
4223         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4224         else if (!done && object_scope)
4225           {
4226             cp_parser_parse_tentatively (parser);
4227             parser->scope = object_scope;
4228             parser->object_scope = NULL_TREE;
4229             parser->qualifying_scope = NULL_TREE;
4230             type_decl
4231               = cp_parser_class_name (parser,
4232                                       /*typename_keyword_p=*/false,
4233                                       /*template_keyword_p=*/false,
4234                                       typename_type,
4235                                       /*check_dependency=*/false,
4236                                       /*class_head_p=*/false,
4237                                       declarator_p);
4238             if (cp_parser_parse_definitely (parser))
4239               done = true;
4240           }
4241         /* Look in the surrounding context.  */
4242         if (!done)
4243           {
4244             parser->scope = NULL_TREE;
4245             parser->object_scope = NULL_TREE;
4246             parser->qualifying_scope = NULL_TREE;
4247             if (processing_template_decl)
4248               cp_parser_parse_tentatively (parser);
4249             type_decl
4250               = cp_parser_class_name (parser,
4251                                       /*typename_keyword_p=*/false,
4252                                       /*template_keyword_p=*/false,
4253                                       typename_type,
4254                                       /*check_dependency=*/false,
4255                                       /*class_head_p=*/false,
4256                                       declarator_p);
4257             if (processing_template_decl
4258                 && ! cp_parser_parse_definitely (parser))
4259               {
4260                 /* We couldn't find a type with this name, so just accept
4261                    it and check for a match at instantiation time.  */
4262                 type_decl = cp_parser_identifier (parser);
4263                 if (type_decl != error_mark_node)
4264                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4265                 return type_decl;
4266               }
4267           }
4268         /* If an error occurred, assume that the name of the
4269            destructor is the same as the name of the qualifying
4270            class.  That allows us to keep parsing after running
4271            into ill-formed destructor names.  */
4272         if (type_decl == error_mark_node && scope)
4273           return build_nt (BIT_NOT_EXPR, scope);
4274         else if (type_decl == error_mark_node)
4275           return error_mark_node;
4276
4277         /* Check that destructor name and scope match.  */
4278         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4279           {
4280             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4281               error_at (token->location,
4282                         "declaration of %<~%T%> as member of %qT",
4283                         type_decl, scope);
4284             cp_parser_simulate_error (parser);
4285             return error_mark_node;
4286           }
4287
4288         /* [class.dtor]
4289
4290            A typedef-name that names a class shall not be used as the
4291            identifier in the declarator for a destructor declaration.  */
4292         if (declarator_p
4293             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4294             && !DECL_SELF_REFERENCE_P (type_decl)
4295             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4296           error_at (token->location,
4297                     "typedef-name %qD used as destructor declarator",
4298                     type_decl);
4299
4300         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4301       }
4302
4303     case CPP_KEYWORD:
4304       if (token->keyword == RID_OPERATOR)
4305         {
4306           tree id;
4307
4308           /* This could be a template-id, so we try that first.  */
4309           cp_parser_parse_tentatively (parser);
4310           /* Try a template-id.  */
4311           id = cp_parser_template_id (parser, template_keyword_p,
4312                                       /*check_dependency_p=*/true,
4313                                       declarator_p);
4314           /* If that worked, we're done.  */
4315           if (cp_parser_parse_definitely (parser))
4316             return id;
4317           /* We still don't know whether we're looking at an
4318              operator-function-id or a conversion-function-id.  */
4319           cp_parser_parse_tentatively (parser);
4320           /* Try an operator-function-id.  */
4321           id = cp_parser_operator_function_id (parser);
4322           /* If that didn't work, try a conversion-function-id.  */
4323           if (!cp_parser_parse_definitely (parser))
4324             id = cp_parser_conversion_function_id (parser);
4325
4326           return id;
4327         }
4328       /* Fall through.  */
4329
4330     default:
4331       if (optional_p)
4332         return NULL_TREE;
4333       cp_parser_error (parser, "expected unqualified-id");
4334       return error_mark_node;
4335     }
4336 }
4337
4338 /* Parse an (optional) nested-name-specifier.
4339
4340    nested-name-specifier: [C++98]
4341      class-or-namespace-name :: nested-name-specifier [opt]
4342      class-or-namespace-name :: template nested-name-specifier [opt]
4343
4344    nested-name-specifier: [C++0x]
4345      type-name ::
4346      namespace-name ::
4347      nested-name-specifier identifier ::
4348      nested-name-specifier template [opt] simple-template-id ::
4349
4350    PARSER->SCOPE should be set appropriately before this function is
4351    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4352    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4353    in name lookups.
4354
4355    Sets PARSER->SCOPE to the class (TYPE) or namespace
4356    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4357    it unchanged if there is no nested-name-specifier.  Returns the new
4358    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4359
4360    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4361    part of a declaration and/or decl-specifier.  */
4362
4363 static tree
4364 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4365                                      bool typename_keyword_p,
4366                                      bool check_dependency_p,
4367                                      bool type_p,
4368                                      bool is_declaration)
4369 {
4370   bool success = false;
4371   cp_token_position start = 0;
4372   cp_token *token;
4373
4374   /* Remember where the nested-name-specifier starts.  */
4375   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4376     {
4377       start = cp_lexer_token_position (parser->lexer, false);
4378       push_deferring_access_checks (dk_deferred);
4379     }
4380
4381   while (true)
4382     {
4383       tree new_scope;
4384       tree old_scope;
4385       tree saved_qualifying_scope;
4386       bool template_keyword_p;
4387
4388       /* Spot cases that cannot be the beginning of a
4389          nested-name-specifier.  */
4390       token = cp_lexer_peek_token (parser->lexer);
4391
4392       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4393          the already parsed nested-name-specifier.  */
4394       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4395         {
4396           /* Grab the nested-name-specifier and continue the loop.  */
4397           cp_parser_pre_parsed_nested_name_specifier (parser);
4398           /* If we originally encountered this nested-name-specifier
4399              with IS_DECLARATION set to false, we will not have
4400              resolved TYPENAME_TYPEs, so we must do so here.  */
4401           if (is_declaration
4402               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4403             {
4404               new_scope = resolve_typename_type (parser->scope,
4405                                                  /*only_current_p=*/false);
4406               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4407                 parser->scope = new_scope;
4408             }
4409           success = true;
4410           continue;
4411         }
4412
4413       /* Spot cases that cannot be the beginning of a
4414          nested-name-specifier.  On the second and subsequent times
4415          through the loop, we look for the `template' keyword.  */
4416       if (success && token->keyword == RID_TEMPLATE)
4417         ;
4418       /* A template-id can start a nested-name-specifier.  */
4419       else if (token->type == CPP_TEMPLATE_ID)
4420         ;
4421       else
4422         {
4423           /* If the next token is not an identifier, then it is
4424              definitely not a type-name or namespace-name.  */
4425           if (token->type != CPP_NAME)
4426             break;
4427           /* If the following token is neither a `<' (to begin a
4428              template-id), nor a `::', then we are not looking at a
4429              nested-name-specifier.  */
4430           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4431           if (token->type != CPP_SCOPE
4432               && !cp_parser_nth_token_starts_template_argument_list_p
4433                   (parser, 2))
4434             break;
4435         }
4436
4437       /* The nested-name-specifier is optional, so we parse
4438          tentatively.  */
4439       cp_parser_parse_tentatively (parser);
4440
4441       /* Look for the optional `template' keyword, if this isn't the
4442          first time through the loop.  */
4443       if (success)
4444         template_keyword_p = cp_parser_optional_template_keyword (parser);
4445       else
4446         template_keyword_p = false;
4447
4448       /* Save the old scope since the name lookup we are about to do
4449          might destroy it.  */
4450       old_scope = parser->scope;
4451       saved_qualifying_scope = parser->qualifying_scope;
4452       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4453          look up names in "X<T>::I" in order to determine that "Y" is
4454          a template.  So, if we have a typename at this point, we make
4455          an effort to look through it.  */
4456       if (is_declaration
4457           && !typename_keyword_p
4458           && parser->scope
4459           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4460         parser->scope = resolve_typename_type (parser->scope,
4461                                                /*only_current_p=*/false);
4462       /* Parse the qualifying entity.  */
4463       new_scope
4464         = cp_parser_qualifying_entity (parser,
4465                                        typename_keyword_p,
4466                                        template_keyword_p,
4467                                        check_dependency_p,
4468                                        type_p,
4469                                        is_declaration);
4470       /* Look for the `::' token.  */
4471       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4472
4473       /* If we found what we wanted, we keep going; otherwise, we're
4474          done.  */
4475       if (!cp_parser_parse_definitely (parser))
4476         {
4477           bool error_p = false;
4478
4479           /* Restore the OLD_SCOPE since it was valid before the
4480              failed attempt at finding the last
4481              class-or-namespace-name.  */
4482           parser->scope = old_scope;
4483           parser->qualifying_scope = saved_qualifying_scope;
4484           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4485             break;
4486           /* If the next token is an identifier, and the one after
4487              that is a `::', then any valid interpretation would have
4488              found a class-or-namespace-name.  */
4489           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4490                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4491                      == CPP_SCOPE)
4492                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4493                      != CPP_COMPL))
4494             {
4495               token = cp_lexer_consume_token (parser->lexer);
4496               if (!error_p)
4497                 {
4498                   if (!token->ambiguous_p)
4499                     {
4500                       tree decl;
4501                       tree ambiguous_decls;
4502
4503                       decl = cp_parser_lookup_name (parser, token->u.value,
4504                                                     none_type,
4505                                                     /*is_template=*/false,
4506                                                     /*is_namespace=*/false,
4507                                                     /*check_dependency=*/true,
4508                                                     &ambiguous_decls,
4509                                                     token->location);
4510                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4511                         error_at (token->location,
4512                                   "%qD used without template parameters",
4513                                   decl);
4514                       else if (ambiguous_decls)
4515                         {
4516                           error_at (token->location,
4517                                     "reference to %qD is ambiguous",
4518                                     token->u.value);
4519                           print_candidates (ambiguous_decls);
4520                           decl = error_mark_node;
4521                         }
4522                       else
4523                         {
4524                           if (cxx_dialect != cxx98)
4525                             cp_parser_name_lookup_error
4526                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4527                              token->location);
4528                           else
4529                             cp_parser_name_lookup_error
4530                             (parser, token->u.value, decl, NLE_CXX98,
4531                              token->location);
4532                         }
4533                     }
4534                   parser->scope = error_mark_node;
4535                   error_p = true;
4536                   /* Treat this as a successful nested-name-specifier
4537                      due to:
4538
4539                      [basic.lookup.qual]
4540
4541                      If the name found is not a class-name (clause
4542                      _class_) or namespace-name (_namespace.def_), the
4543                      program is ill-formed.  */
4544                   success = true;
4545                 }
4546               cp_lexer_consume_token (parser->lexer);
4547             }
4548           break;
4549         }
4550       /* We've found one valid nested-name-specifier.  */
4551       success = true;
4552       /* Name lookup always gives us a DECL.  */
4553       if (TREE_CODE (new_scope) == TYPE_DECL)
4554         new_scope = TREE_TYPE (new_scope);
4555       /* Uses of "template" must be followed by actual templates.  */
4556       if (template_keyword_p
4557           && !(CLASS_TYPE_P (new_scope)
4558                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4559                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4560                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4561           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4562                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4563                    == TEMPLATE_ID_EXPR)))
4564         permerror (input_location, TYPE_P (new_scope)
4565                    ? "%qT is not a template"
4566                    : "%qD is not a template",
4567                    new_scope);
4568       /* If it is a class scope, try to complete it; we are about to
4569          be looking up names inside the class.  */
4570       if (TYPE_P (new_scope)
4571           /* Since checking types for dependency can be expensive,
4572              avoid doing it if the type is already complete.  */
4573           && !COMPLETE_TYPE_P (new_scope)
4574           /* Do not try to complete dependent types.  */
4575           && !dependent_type_p (new_scope))
4576         {
4577           new_scope = complete_type (new_scope);
4578           /* If it is a typedef to current class, use the current
4579              class instead, as the typedef won't have any names inside
4580              it yet.  */
4581           if (!COMPLETE_TYPE_P (new_scope)
4582               && currently_open_class (new_scope))
4583             new_scope = TYPE_MAIN_VARIANT (new_scope);
4584         }
4585       /* Make sure we look in the right scope the next time through
4586          the loop.  */
4587       parser->scope = new_scope;
4588     }
4589
4590   /* If parsing tentatively, replace the sequence of tokens that makes
4591      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4592      token.  That way, should we re-parse the token stream, we will
4593      not have to repeat the effort required to do the parse, nor will
4594      we issue duplicate error messages.  */
4595   if (success && start)
4596     {
4597       cp_token *token;
4598
4599       token = cp_lexer_token_at (parser->lexer, start);
4600       /* Reset the contents of the START token.  */
4601       token->type = CPP_NESTED_NAME_SPECIFIER;
4602       /* Retrieve any deferred checks.  Do not pop this access checks yet
4603          so the memory will not be reclaimed during token replacing below.  */
4604       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4605       token->u.tree_check_value->value = parser->scope;
4606       token->u.tree_check_value->checks = get_deferred_access_checks ();
4607       token->u.tree_check_value->qualifying_scope =
4608         parser->qualifying_scope;
4609       token->keyword = RID_MAX;
4610
4611       /* Purge all subsequent tokens.  */
4612       cp_lexer_purge_tokens_after (parser->lexer, start);
4613     }
4614
4615   if (start)
4616     pop_to_parent_deferring_access_checks ();
4617
4618   return success ? parser->scope : NULL_TREE;
4619 }
4620
4621 /* Parse a nested-name-specifier.  See
4622    cp_parser_nested_name_specifier_opt for details.  This function
4623    behaves identically, except that it will an issue an error if no
4624    nested-name-specifier is present.  */
4625
4626 static tree
4627 cp_parser_nested_name_specifier (cp_parser *parser,
4628                                  bool typename_keyword_p,
4629                                  bool check_dependency_p,
4630                                  bool type_p,
4631                                  bool is_declaration)
4632 {
4633   tree scope;
4634
4635   /* Look for the nested-name-specifier.  */
4636   scope = cp_parser_nested_name_specifier_opt (parser,
4637                                                typename_keyword_p,
4638                                                check_dependency_p,
4639                                                type_p,
4640                                                is_declaration);
4641   /* If it was not present, issue an error message.  */
4642   if (!scope)
4643     {
4644       cp_parser_error (parser, "expected nested-name-specifier");
4645       parser->scope = NULL_TREE;
4646     }
4647
4648   return scope;
4649 }
4650
4651 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4652    this is either a class-name or a namespace-name (which corresponds
4653    to the class-or-namespace-name production in the grammar). For
4654    C++0x, it can also be a type-name that refers to an enumeration
4655    type.
4656
4657    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4658    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4659    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4660    TYPE_P is TRUE iff the next name should be taken as a class-name,
4661    even the same name is declared to be another entity in the same
4662    scope.
4663
4664    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4665    specified by the class-or-namespace-name.  If neither is found the
4666    ERROR_MARK_NODE is returned.  */
4667
4668 static tree
4669 cp_parser_qualifying_entity (cp_parser *parser,
4670                              bool typename_keyword_p,
4671                              bool template_keyword_p,
4672                              bool check_dependency_p,
4673                              bool type_p,
4674                              bool is_declaration)
4675 {
4676   tree saved_scope;
4677   tree saved_qualifying_scope;
4678   tree saved_object_scope;
4679   tree scope;
4680   bool only_class_p;
4681   bool successful_parse_p;
4682
4683   /* Before we try to parse the class-name, we must save away the
4684      current PARSER->SCOPE since cp_parser_class_name will destroy
4685      it.  */
4686   saved_scope = parser->scope;
4687   saved_qualifying_scope = parser->qualifying_scope;
4688   saved_object_scope = parser->object_scope;
4689   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4690      there is no need to look for a namespace-name.  */
4691   only_class_p = template_keyword_p 
4692     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4693   if (!only_class_p)
4694     cp_parser_parse_tentatively (parser);
4695   scope = cp_parser_class_name (parser,
4696                                 typename_keyword_p,
4697                                 template_keyword_p,
4698                                 type_p ? class_type : none_type,
4699                                 check_dependency_p,
4700                                 /*class_head_p=*/false,
4701                                 is_declaration);
4702   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4703   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4704   if (!only_class_p 
4705       && cxx_dialect != cxx98
4706       && !successful_parse_p)
4707     {
4708       /* Restore the saved scope.  */
4709       parser->scope = saved_scope;
4710       parser->qualifying_scope = saved_qualifying_scope;
4711       parser->object_scope = saved_object_scope;
4712
4713       /* Parse tentatively.  */
4714       cp_parser_parse_tentatively (parser);
4715      
4716       /* Parse a typedef-name or enum-name.  */
4717       scope = cp_parser_nonclass_name (parser);
4718
4719       /* "If the name found does not designate a namespace or a class,
4720          enumeration, or dependent type, the program is ill-formed."
4721
4722          We cover classes and dependent types above and namespaces below,
4723          so this code is only looking for enums.  */
4724       if (!scope || TREE_CODE (scope) != TYPE_DECL
4725           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4726         cp_parser_simulate_error (parser);
4727
4728       successful_parse_p = cp_parser_parse_definitely (parser);
4729     }
4730   /* If that didn't work, try for a namespace-name.  */
4731   if (!only_class_p && !successful_parse_p)
4732     {
4733       /* Restore the saved scope.  */
4734       parser->scope = saved_scope;
4735       parser->qualifying_scope = saved_qualifying_scope;
4736       parser->object_scope = saved_object_scope;
4737       /* If we are not looking at an identifier followed by the scope
4738          resolution operator, then this is not part of a
4739          nested-name-specifier.  (Note that this function is only used
4740          to parse the components of a nested-name-specifier.)  */
4741       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4742           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4743         return error_mark_node;
4744       scope = cp_parser_namespace_name (parser);
4745     }
4746
4747   return scope;
4748 }
4749
4750 /* Parse a postfix-expression.
4751
4752    postfix-expression:
4753      primary-expression
4754      postfix-expression [ expression ]
4755      postfix-expression ( expression-list [opt] )
4756      simple-type-specifier ( expression-list [opt] )
4757      typename :: [opt] nested-name-specifier identifier
4758        ( expression-list [opt] )
4759      typename :: [opt] nested-name-specifier template [opt] template-id
4760        ( expression-list [opt] )
4761      postfix-expression . template [opt] id-expression
4762      postfix-expression -> template [opt] id-expression
4763      postfix-expression . pseudo-destructor-name
4764      postfix-expression -> pseudo-destructor-name
4765      postfix-expression ++
4766      postfix-expression --
4767      dynamic_cast < type-id > ( expression )
4768      static_cast < type-id > ( expression )
4769      reinterpret_cast < type-id > ( expression )
4770      const_cast < type-id > ( expression )
4771      typeid ( expression )
4772      typeid ( type-id )
4773
4774    GNU Extension:
4775
4776    postfix-expression:
4777      ( type-id ) { initializer-list , [opt] }
4778
4779    This extension is a GNU version of the C99 compound-literal
4780    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4781    but they are essentially the same concept.)
4782
4783    If ADDRESS_P is true, the postfix expression is the operand of the
4784    `&' operator.  CAST_P is true if this expression is the target of a
4785    cast.
4786
4787    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4788    class member access expressions [expr.ref].
4789
4790    Returns a representation of the expression.  */
4791
4792 static tree
4793 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4794                               bool member_access_only_p,
4795                               cp_id_kind * pidk_return)
4796 {
4797   cp_token *token;
4798   enum rid keyword;
4799   cp_id_kind idk = CP_ID_KIND_NONE;
4800   tree postfix_expression = NULL_TREE;
4801   bool is_member_access = false;
4802
4803   /* Peek at the next token.  */
4804   token = cp_lexer_peek_token (parser->lexer);
4805   /* Some of the productions are determined by keywords.  */
4806   keyword = token->keyword;
4807   switch (keyword)
4808     {
4809     case RID_DYNCAST:
4810     case RID_STATCAST:
4811     case RID_REINTCAST:
4812     case RID_CONSTCAST:
4813       {
4814         tree type;
4815         tree expression;
4816         const char *saved_message;
4817
4818         /* All of these can be handled in the same way from the point
4819            of view of parsing.  Begin by consuming the token
4820            identifying the cast.  */
4821         cp_lexer_consume_token (parser->lexer);
4822
4823         /* New types cannot be defined in the cast.  */
4824         saved_message = parser->type_definition_forbidden_message;
4825         parser->type_definition_forbidden_message
4826           = G_("types may not be defined in casts");
4827
4828         /* Look for the opening `<'.  */
4829         cp_parser_require (parser, CPP_LESS, RT_LESS);
4830         /* Parse the type to which we are casting.  */
4831         type = cp_parser_type_id (parser);
4832         /* Look for the closing `>'.  */
4833         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4834         /* Restore the old message.  */
4835         parser->type_definition_forbidden_message = saved_message;
4836
4837         /* And the expression which is being cast.  */
4838         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4839         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4840         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4841
4842         /* Only type conversions to integral or enumeration types
4843            can be used in constant-expressions.  */
4844         if (!cast_valid_in_integral_constant_expression_p (type)
4845             && (cp_parser_non_integral_constant_expression (parser, NIC_CAST)))
4846           return error_mark_node;
4847
4848         switch (keyword)
4849           {
4850           case RID_DYNCAST:
4851             postfix_expression
4852               = build_dynamic_cast (type, expression, tf_warning_or_error);
4853             break;
4854           case RID_STATCAST:
4855             postfix_expression
4856               = build_static_cast (type, expression, tf_warning_or_error);
4857             break;
4858           case RID_REINTCAST:
4859             postfix_expression
4860               = build_reinterpret_cast (type, expression, 
4861                                         tf_warning_or_error);
4862             break;
4863           case RID_CONSTCAST:
4864             postfix_expression
4865               = build_const_cast (type, expression, tf_warning_or_error);
4866             break;
4867           default:
4868             gcc_unreachable ();
4869           }
4870       }
4871       break;
4872
4873     case RID_TYPEID:
4874       {
4875         tree type;
4876         const char *saved_message;
4877         bool saved_in_type_id_in_expr_p;
4878
4879         /* Consume the `typeid' token.  */
4880         cp_lexer_consume_token (parser->lexer);
4881         /* Look for the `(' token.  */
4882         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4883         /* Types cannot be defined in a `typeid' expression.  */
4884         saved_message = parser->type_definition_forbidden_message;
4885         parser->type_definition_forbidden_message
4886           = G_("types may not be defined in a %<typeid%> expression");
4887         /* We can't be sure yet whether we're looking at a type-id or an
4888            expression.  */
4889         cp_parser_parse_tentatively (parser);
4890         /* Try a type-id first.  */
4891         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4892         parser->in_type_id_in_expr_p = true;
4893         type = cp_parser_type_id (parser);
4894         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4895         /* Look for the `)' token.  Otherwise, we can't be sure that
4896            we're not looking at an expression: consider `typeid (int
4897            (3))', for example.  */
4898         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4899         /* If all went well, simply lookup the type-id.  */
4900         if (cp_parser_parse_definitely (parser))
4901           postfix_expression = get_typeid (type);
4902         /* Otherwise, fall back to the expression variant.  */
4903         else
4904           {
4905             tree expression;
4906
4907             /* Look for an expression.  */
4908             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4909             /* Compute its typeid.  */
4910             postfix_expression = build_typeid (expression);
4911             /* Look for the `)' token.  */
4912             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4913           }
4914         /* Restore the saved message.  */
4915         parser->type_definition_forbidden_message = saved_message;
4916         /* `typeid' may not appear in an integral constant expression.  */
4917         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4918           return error_mark_node;
4919       }
4920       break;
4921
4922     case RID_TYPENAME:
4923       {
4924         tree type;
4925         /* The syntax permitted here is the same permitted for an
4926            elaborated-type-specifier.  */
4927         type = cp_parser_elaborated_type_specifier (parser,
4928                                                     /*is_friend=*/false,
4929                                                     /*is_declaration=*/false);
4930         postfix_expression = cp_parser_functional_cast (parser, type);
4931       }
4932       break;
4933
4934     default:
4935       {
4936         tree type;
4937
4938         /* If the next thing is a simple-type-specifier, we may be
4939            looking at a functional cast.  We could also be looking at
4940            an id-expression.  So, we try the functional cast, and if
4941            that doesn't work we fall back to the primary-expression.  */
4942         cp_parser_parse_tentatively (parser);
4943         /* Look for the simple-type-specifier.  */
4944         type = cp_parser_simple_type_specifier (parser,
4945                                                 /*decl_specs=*/NULL,
4946                                                 CP_PARSER_FLAGS_NONE);
4947         /* Parse the cast itself.  */
4948         if (!cp_parser_error_occurred (parser))
4949           postfix_expression
4950             = cp_parser_functional_cast (parser, type);
4951         /* If that worked, we're done.  */
4952         if (cp_parser_parse_definitely (parser))
4953           break;
4954
4955         /* If the functional-cast didn't work out, try a
4956            compound-literal.  */
4957         if (cp_parser_allow_gnu_extensions_p (parser)
4958             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4959           {
4960             VEC(constructor_elt,gc) *initializer_list = NULL;
4961             bool saved_in_type_id_in_expr_p;
4962
4963             cp_parser_parse_tentatively (parser);
4964             /* Consume the `('.  */
4965             cp_lexer_consume_token (parser->lexer);
4966             /* Parse the type.  */
4967             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4968             parser->in_type_id_in_expr_p = true;
4969             type = cp_parser_type_id (parser);
4970             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4971             /* Look for the `)'.  */
4972             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4973             /* Look for the `{'.  */
4974             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4975             /* If things aren't going well, there's no need to
4976                keep going.  */
4977             if (!cp_parser_error_occurred (parser))
4978               {
4979                 bool non_constant_p;
4980                 /* Parse the initializer-list.  */
4981                 initializer_list
4982                   = cp_parser_initializer_list (parser, &non_constant_p);
4983                 /* Allow a trailing `,'.  */
4984                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4985                   cp_lexer_consume_token (parser->lexer);
4986                 /* Look for the final `}'.  */
4987                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4988               }
4989             /* If that worked, we're definitely looking at a
4990                compound-literal expression.  */
4991             if (cp_parser_parse_definitely (parser))
4992               {
4993                 /* Warn the user that a compound literal is not
4994                    allowed in standard C++.  */
4995                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4996                 /* For simplicity, we disallow compound literals in
4997                    constant-expressions.  We could
4998                    allow compound literals of integer type, whose
4999                    initializer was a constant, in constant
5000                    expressions.  Permitting that usage, as a further
5001                    extension, would not change the meaning of any
5002                    currently accepted programs.  (Of course, as
5003                    compound literals are not part of ISO C++, the
5004                    standard has nothing to say.)  */
5005                 if (cp_parser_non_integral_constant_expression (parser,
5006                                                                 NIC_NCC))
5007                   {
5008                     postfix_expression = error_mark_node;
5009                     break;
5010                   }
5011                 /* Form the representation of the compound-literal.  */
5012                 postfix_expression
5013                   = (finish_compound_literal
5014                      (type, build_constructor (init_list_type_node,
5015                                                initializer_list)));
5016                 break;
5017               }
5018           }
5019
5020         /* It must be a primary-expression.  */
5021         postfix_expression
5022           = cp_parser_primary_expression (parser, address_p, cast_p,
5023                                           /*template_arg_p=*/false,
5024                                           &idk);
5025       }
5026       break;
5027     }
5028
5029   /* Keep looping until the postfix-expression is complete.  */
5030   while (true)
5031     {
5032       if (idk == CP_ID_KIND_UNQUALIFIED
5033           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5034           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5035         /* It is not a Koenig lookup function call.  */
5036         postfix_expression
5037           = unqualified_name_lookup_error (postfix_expression);
5038
5039       /* Peek at the next token.  */
5040       token = cp_lexer_peek_token (parser->lexer);
5041
5042       switch (token->type)
5043         {
5044         case CPP_OPEN_SQUARE:
5045           postfix_expression
5046             = cp_parser_postfix_open_square_expression (parser,
5047                                                         postfix_expression,
5048                                                         false);
5049           idk = CP_ID_KIND_NONE;
5050           is_member_access = false;
5051           break;
5052
5053         case CPP_OPEN_PAREN:
5054           /* postfix-expression ( expression-list [opt] ) */
5055           {
5056             bool koenig_p;
5057             bool is_builtin_constant_p;
5058             bool saved_integral_constant_expression_p = false;
5059             bool saved_non_integral_constant_expression_p = false;
5060             VEC(tree,gc) *args;
5061
5062             is_member_access = false;
5063
5064             is_builtin_constant_p
5065               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5066             if (is_builtin_constant_p)
5067               {
5068                 /* The whole point of __builtin_constant_p is to allow
5069                    non-constant expressions to appear as arguments.  */
5070                 saved_integral_constant_expression_p
5071                   = parser->integral_constant_expression_p;
5072                 saved_non_integral_constant_expression_p
5073                   = parser->non_integral_constant_expression_p;
5074                 parser->integral_constant_expression_p = false;
5075               }
5076             args = (cp_parser_parenthesized_expression_list
5077                     (parser, non_attr,
5078                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5079                      /*non_constant_p=*/NULL));
5080             if (is_builtin_constant_p)
5081               {
5082                 parser->integral_constant_expression_p
5083                   = saved_integral_constant_expression_p;
5084                 parser->non_integral_constant_expression_p
5085                   = saved_non_integral_constant_expression_p;
5086               }
5087
5088             if (args == NULL)
5089               {
5090                 postfix_expression = error_mark_node;
5091                 break;
5092               }
5093
5094             /* Function calls are not permitted in
5095                constant-expressions.  */
5096             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5097                 && cp_parser_non_integral_constant_expression (parser,
5098                                                                NIC_FUNC_CALL))
5099               {
5100                 postfix_expression = error_mark_node;
5101                 release_tree_vector (args);
5102                 break;
5103               }
5104
5105             koenig_p = false;
5106             if (idk == CP_ID_KIND_UNQUALIFIED
5107                 || idk == CP_ID_KIND_TEMPLATE_ID)
5108               {
5109                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5110                   {
5111                     if (!VEC_empty (tree, args))
5112                       {
5113                         koenig_p = true;
5114                         if (!any_type_dependent_arguments_p (args))
5115                           postfix_expression
5116                             = perform_koenig_lookup (postfix_expression, args);
5117                       }
5118                     else
5119                       postfix_expression
5120                         = unqualified_fn_lookup_error (postfix_expression);
5121                   }
5122                 /* We do not perform argument-dependent lookup if
5123                    normal lookup finds a non-function, in accordance
5124                    with the expected resolution of DR 218.  */
5125                 else if (!VEC_empty (tree, args)
5126                          && is_overloaded_fn (postfix_expression))
5127                   {
5128                     tree fn = get_first_fn (postfix_expression);
5129                     fn = STRIP_TEMPLATE (fn);
5130
5131                     /* Do not do argument dependent lookup if regular
5132                        lookup finds a member function or a block-scope
5133                        function declaration.  [basic.lookup.argdep]/3  */
5134                     if (!DECL_FUNCTION_MEMBER_P (fn)
5135                         && !DECL_LOCAL_FUNCTION_P (fn))
5136                       {
5137                         koenig_p = true;
5138                         if (!any_type_dependent_arguments_p (args))
5139                           postfix_expression
5140                             = perform_koenig_lookup (postfix_expression, args);
5141                       }
5142                   }
5143               }
5144
5145             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5146               {
5147                 tree instance = TREE_OPERAND (postfix_expression, 0);
5148                 tree fn = TREE_OPERAND (postfix_expression, 1);
5149
5150                 if (processing_template_decl
5151                     && (type_dependent_expression_p (instance)
5152                         || (!BASELINK_P (fn)
5153                             && TREE_CODE (fn) != FIELD_DECL)
5154                         || type_dependent_expression_p (fn)
5155                         || any_type_dependent_arguments_p (args)))
5156                   {
5157                     postfix_expression
5158                       = build_nt_call_vec (postfix_expression, args);
5159                     release_tree_vector (args);
5160                     break;
5161                   }
5162
5163                 if (BASELINK_P (fn))
5164                   {
5165                   postfix_expression
5166                     = (build_new_method_call
5167                        (instance, fn, &args, NULL_TREE,
5168                         (idk == CP_ID_KIND_QUALIFIED
5169                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5170                         /*fn_p=*/NULL,
5171                         tf_warning_or_error));
5172                   }
5173                 else
5174                   postfix_expression
5175                     = finish_call_expr (postfix_expression, &args,
5176                                         /*disallow_virtual=*/false,
5177                                         /*koenig_p=*/false,
5178                                         tf_warning_or_error);
5179               }
5180             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5181                      || TREE_CODE (postfix_expression) == MEMBER_REF
5182                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5183               postfix_expression = (build_offset_ref_call_from_tree
5184                                     (postfix_expression, &args));
5185             else if (idk == CP_ID_KIND_QUALIFIED)
5186               /* A call to a static class member, or a namespace-scope
5187                  function.  */
5188               postfix_expression
5189                 = finish_call_expr (postfix_expression, &args,
5190                                     /*disallow_virtual=*/true,
5191                                     koenig_p,
5192                                     tf_warning_or_error);
5193             else
5194               /* All other function calls.  */
5195               postfix_expression
5196                 = finish_call_expr (postfix_expression, &args,
5197                                     /*disallow_virtual=*/false,
5198                                     koenig_p,
5199                                     tf_warning_or_error);
5200
5201             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5202             idk = CP_ID_KIND_NONE;
5203
5204             release_tree_vector (args);
5205           }
5206           break;
5207
5208         case CPP_DOT:
5209         case CPP_DEREF:
5210           /* postfix-expression . template [opt] id-expression
5211              postfix-expression . pseudo-destructor-name
5212              postfix-expression -> template [opt] id-expression
5213              postfix-expression -> pseudo-destructor-name */
5214
5215           /* Consume the `.' or `->' operator.  */
5216           cp_lexer_consume_token (parser->lexer);
5217
5218           postfix_expression
5219             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5220                                                       postfix_expression,
5221                                                       false, &idk,
5222                                                       token->location);
5223
5224           is_member_access = true;
5225           break;
5226
5227         case CPP_PLUS_PLUS:
5228           /* postfix-expression ++  */
5229           /* Consume the `++' token.  */
5230           cp_lexer_consume_token (parser->lexer);
5231           /* Generate a representation for the complete expression.  */
5232           postfix_expression
5233             = finish_increment_expr (postfix_expression,
5234                                      POSTINCREMENT_EXPR);
5235           /* Increments may not appear in constant-expressions.  */
5236           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5237             postfix_expression = error_mark_node;
5238           idk = CP_ID_KIND_NONE;
5239           is_member_access = false;
5240           break;
5241
5242         case CPP_MINUS_MINUS:
5243           /* postfix-expression -- */
5244           /* Consume the `--' token.  */
5245           cp_lexer_consume_token (parser->lexer);
5246           /* Generate a representation for the complete expression.  */
5247           postfix_expression
5248             = finish_increment_expr (postfix_expression,
5249                                      POSTDECREMENT_EXPR);
5250           /* Decrements may not appear in constant-expressions.  */
5251           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5252             postfix_expression = error_mark_node;
5253           idk = CP_ID_KIND_NONE;
5254           is_member_access = false;
5255           break;
5256
5257         default:
5258           if (pidk_return != NULL)
5259             * pidk_return = idk;
5260           if (member_access_only_p)
5261             return is_member_access? postfix_expression : error_mark_node;
5262           else
5263             return postfix_expression;
5264         }
5265     }
5266
5267   /* We should never get here.  */
5268   gcc_unreachable ();
5269   return error_mark_node;
5270 }
5271
5272 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5273    by cp_parser_builtin_offsetof.  We're looking for
5274
5275      postfix-expression [ expression ]
5276
5277    FOR_OFFSETOF is set if we're being called in that context, which
5278    changes how we deal with integer constant expressions.  */
5279
5280 static tree
5281 cp_parser_postfix_open_square_expression (cp_parser *parser,
5282                                           tree postfix_expression,
5283                                           bool for_offsetof)
5284 {
5285   tree index;
5286
5287   /* Consume the `[' token.  */
5288   cp_lexer_consume_token (parser->lexer);
5289
5290   /* Parse the index expression.  */
5291   /* ??? For offsetof, there is a question of what to allow here.  If
5292      offsetof is not being used in an integral constant expression context,
5293      then we *could* get the right answer by computing the value at runtime.
5294      If we are in an integral constant expression context, then we might
5295      could accept any constant expression; hard to say without analysis.
5296      Rather than open the barn door too wide right away, allow only integer
5297      constant expressions here.  */
5298   if (for_offsetof)
5299     index = cp_parser_constant_expression (parser, false, NULL);
5300   else
5301     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5302
5303   /* Look for the closing `]'.  */
5304   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5305
5306   /* Build the ARRAY_REF.  */
5307   postfix_expression = grok_array_decl (postfix_expression, index);
5308
5309   /* When not doing offsetof, array references are not permitted in
5310      constant-expressions.  */
5311   if (!for_offsetof
5312       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5313     postfix_expression = error_mark_node;
5314
5315   return postfix_expression;
5316 }
5317
5318 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5319    by cp_parser_builtin_offsetof.  We're looking for
5320
5321      postfix-expression . template [opt] id-expression
5322      postfix-expression . pseudo-destructor-name
5323      postfix-expression -> template [opt] id-expression
5324      postfix-expression -> pseudo-destructor-name
5325
5326    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5327    limits what of the above we'll actually accept, but nevermind.
5328    TOKEN_TYPE is the "." or "->" token, which will already have been
5329    removed from the stream.  */
5330
5331 static tree
5332 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5333                                         enum cpp_ttype token_type,
5334                                         tree postfix_expression,
5335                                         bool for_offsetof, cp_id_kind *idk,
5336                                         location_t location)
5337 {
5338   tree name;
5339   bool dependent_p;
5340   bool pseudo_destructor_p;
5341   tree scope = NULL_TREE;
5342
5343   /* If this is a `->' operator, dereference the pointer.  */
5344   if (token_type == CPP_DEREF)
5345     postfix_expression = build_x_arrow (postfix_expression);
5346   /* Check to see whether or not the expression is type-dependent.  */
5347   dependent_p = type_dependent_expression_p (postfix_expression);
5348   /* The identifier following the `->' or `.' is not qualified.  */
5349   parser->scope = NULL_TREE;
5350   parser->qualifying_scope = NULL_TREE;
5351   parser->object_scope = NULL_TREE;
5352   *idk = CP_ID_KIND_NONE;
5353
5354   /* Enter the scope corresponding to the type of the object
5355      given by the POSTFIX_EXPRESSION.  */
5356   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5357     {
5358       scope = TREE_TYPE (postfix_expression);
5359       /* According to the standard, no expression should ever have
5360          reference type.  Unfortunately, we do not currently match
5361          the standard in this respect in that our internal representation
5362          of an expression may have reference type even when the standard
5363          says it does not.  Therefore, we have to manually obtain the
5364          underlying type here.  */
5365       scope = non_reference (scope);
5366       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5367       if (scope == unknown_type_node)
5368         {
5369           error_at (location, "%qE does not have class type",
5370                     postfix_expression);
5371           scope = NULL_TREE;
5372         }
5373       else
5374         scope = complete_type_or_else (scope, NULL_TREE);
5375       /* Let the name lookup machinery know that we are processing a
5376          class member access expression.  */
5377       parser->context->object_type = scope;
5378       /* If something went wrong, we want to be able to discern that case,
5379          as opposed to the case where there was no SCOPE due to the type
5380          of expression being dependent.  */
5381       if (!scope)
5382         scope = error_mark_node;
5383       /* If the SCOPE was erroneous, make the various semantic analysis
5384          functions exit quickly -- and without issuing additional error
5385          messages.  */
5386       if (scope == error_mark_node)
5387         postfix_expression = error_mark_node;
5388     }
5389
5390   /* Assume this expression is not a pseudo-destructor access.  */
5391   pseudo_destructor_p = false;
5392
5393   /* If the SCOPE is a scalar type, then, if this is a valid program,
5394      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5395      is type dependent, it can be pseudo-destructor-name or something else.
5396      Try to parse it as pseudo-destructor-name first.  */
5397   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5398     {
5399       tree s;
5400       tree type;
5401
5402       cp_parser_parse_tentatively (parser);
5403       /* Parse the pseudo-destructor-name.  */
5404       s = NULL_TREE;
5405       cp_parser_pseudo_destructor_name (parser, &s, &type);
5406       if (dependent_p
5407           && (cp_parser_error_occurred (parser)
5408               || TREE_CODE (type) != TYPE_DECL
5409               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5410         cp_parser_abort_tentative_parse (parser);
5411       else if (cp_parser_parse_definitely (parser))
5412         {
5413           pseudo_destructor_p = true;
5414           postfix_expression
5415             = finish_pseudo_destructor_expr (postfix_expression,
5416                                              s, TREE_TYPE (type));
5417         }
5418     }
5419
5420   if (!pseudo_destructor_p)
5421     {
5422       /* If the SCOPE is not a scalar type, we are looking at an
5423          ordinary class member access expression, rather than a
5424          pseudo-destructor-name.  */
5425       bool template_p;
5426       cp_token *token = cp_lexer_peek_token (parser->lexer);
5427       /* Parse the id-expression.  */
5428       name = (cp_parser_id_expression
5429               (parser,
5430                cp_parser_optional_template_keyword (parser),
5431                /*check_dependency_p=*/true,
5432                &template_p,
5433                /*declarator_p=*/false,
5434                /*optional_p=*/false));
5435       /* In general, build a SCOPE_REF if the member name is qualified.
5436          However, if the name was not dependent and has already been
5437          resolved; there is no need to build the SCOPE_REF.  For example;
5438
5439              struct X { void f(); };
5440              template <typename T> void f(T* t) { t->X::f(); }
5441
5442          Even though "t" is dependent, "X::f" is not and has been resolved
5443          to a BASELINK; there is no need to include scope information.  */
5444
5445       /* But we do need to remember that there was an explicit scope for
5446          virtual function calls.  */
5447       if (parser->scope)
5448         *idk = CP_ID_KIND_QUALIFIED;
5449
5450       /* If the name is a template-id that names a type, we will get a
5451          TYPE_DECL here.  That is invalid code.  */
5452       if (TREE_CODE (name) == TYPE_DECL)
5453         {
5454           error_at (token->location, "invalid use of %qD", name);
5455           postfix_expression = error_mark_node;
5456         }
5457       else
5458         {
5459           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5460             {
5461               name = build_qualified_name (/*type=*/NULL_TREE,
5462                                            parser->scope,
5463                                            name,
5464                                            template_p);
5465               parser->scope = NULL_TREE;
5466               parser->qualifying_scope = NULL_TREE;
5467               parser->object_scope = NULL_TREE;
5468             }
5469           if (scope && name && BASELINK_P (name))
5470             adjust_result_of_qualified_name_lookup
5471               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5472           postfix_expression
5473             = finish_class_member_access_expr (postfix_expression, name,
5474                                                template_p, 
5475                                                tf_warning_or_error);
5476         }
5477     }
5478
5479   /* We no longer need to look up names in the scope of the object on
5480      the left-hand side of the `.' or `->' operator.  */
5481   parser->context->object_type = NULL_TREE;
5482
5483   /* Outside of offsetof, these operators may not appear in
5484      constant-expressions.  */
5485   if (!for_offsetof
5486       && (cp_parser_non_integral_constant_expression
5487           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5488     postfix_expression = error_mark_node;
5489
5490   return postfix_expression;
5491 }
5492
5493 /* Parse a parenthesized expression-list.
5494
5495    expression-list:
5496      assignment-expression
5497      expression-list, assignment-expression
5498
5499    attribute-list:
5500      expression-list
5501      identifier
5502      identifier, expression-list
5503
5504    CAST_P is true if this expression is the target of a cast.
5505
5506    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5507    argument pack.
5508
5509    Returns a vector of trees.  Each element is a representation of an
5510    assignment-expression.  NULL is returned if the ( and or ) are
5511    missing.  An empty, but allocated, vector is returned on no
5512    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5513    if we are parsing an attribute list for an attribute that wants a
5514    plain identifier argument, normal_attr for an attribute that wants
5515    an expression, or non_attr if we aren't parsing an attribute list.  If
5516    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5517    not all of the expressions in the list were constant.  */
5518
5519 static VEC(tree,gc) *
5520 cp_parser_parenthesized_expression_list (cp_parser* parser,
5521                                          int is_attribute_list,
5522                                          bool cast_p,
5523                                          bool allow_expansion_p,
5524                                          bool *non_constant_p)
5525 {
5526   VEC(tree,gc) *expression_list;
5527   bool fold_expr_p = is_attribute_list != non_attr;
5528   tree identifier = NULL_TREE;
5529   bool saved_greater_than_is_operator_p;
5530
5531   /* Assume all the expressions will be constant.  */
5532   if (non_constant_p)
5533     *non_constant_p = false;
5534
5535   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5536     return NULL;
5537
5538   expression_list = make_tree_vector ();
5539
5540   /* Within a parenthesized expression, a `>' token is always
5541      the greater-than operator.  */
5542   saved_greater_than_is_operator_p
5543     = parser->greater_than_is_operator_p;
5544   parser->greater_than_is_operator_p = true;
5545
5546   /* Consume expressions until there are no more.  */
5547   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5548     while (true)
5549       {
5550         tree expr;
5551
5552         /* At the beginning of attribute lists, check to see if the
5553            next token is an identifier.  */
5554         if (is_attribute_list == id_attr
5555             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5556           {
5557             cp_token *token;
5558
5559             /* Consume the identifier.  */
5560             token = cp_lexer_consume_token (parser->lexer);
5561             /* Save the identifier.  */
5562             identifier = token->u.value;
5563           }
5564         else
5565           {
5566             bool expr_non_constant_p;
5567
5568             /* Parse the next assignment-expression.  */
5569             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5570               {
5571                 /* A braced-init-list.  */
5572                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5573                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5574                 if (non_constant_p && expr_non_constant_p)
5575                   *non_constant_p = true;
5576               }
5577             else if (non_constant_p)
5578               {
5579                 expr = (cp_parser_constant_expression
5580                         (parser, /*allow_non_constant_p=*/true,
5581                          &expr_non_constant_p));
5582                 if (expr_non_constant_p)
5583                   *non_constant_p = true;
5584               }
5585             else
5586               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5587
5588             if (fold_expr_p)
5589               expr = fold_non_dependent_expr (expr);
5590
5591             /* If we have an ellipsis, then this is an expression
5592                expansion.  */
5593             if (allow_expansion_p
5594                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5595               {
5596                 /* Consume the `...'.  */
5597                 cp_lexer_consume_token (parser->lexer);
5598
5599                 /* Build the argument pack.  */
5600                 expr = make_pack_expansion (expr);
5601               }
5602
5603              /* Add it to the list.  We add error_mark_node
5604                 expressions to the list, so that we can still tell if
5605                 the correct form for a parenthesized expression-list
5606                 is found. That gives better errors.  */
5607             VEC_safe_push (tree, gc, expression_list, expr);
5608
5609             if (expr == error_mark_node)
5610               goto skip_comma;
5611           }
5612
5613         /* After the first item, attribute lists look the same as
5614            expression lists.  */
5615         is_attribute_list = non_attr;
5616
5617       get_comma:;
5618         /* If the next token isn't a `,', then we are done.  */
5619         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5620           break;
5621
5622         /* Otherwise, consume the `,' and keep going.  */
5623         cp_lexer_consume_token (parser->lexer);
5624       }
5625
5626   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5627     {
5628       int ending;
5629
5630     skip_comma:;
5631       /* We try and resync to an unnested comma, as that will give the
5632          user better diagnostics.  */
5633       ending = cp_parser_skip_to_closing_parenthesis (parser,
5634                                                       /*recovering=*/true,
5635                                                       /*or_comma=*/true,
5636                                                       /*consume_paren=*/true);
5637       if (ending < 0)
5638         goto get_comma;
5639       if (!ending)
5640         {
5641           parser->greater_than_is_operator_p
5642             = saved_greater_than_is_operator_p;
5643           return NULL;
5644         }
5645     }
5646
5647   parser->greater_than_is_operator_p
5648     = saved_greater_than_is_operator_p;
5649
5650   if (identifier)
5651     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5652
5653   return expression_list;
5654 }
5655
5656 /* Parse a pseudo-destructor-name.
5657
5658    pseudo-destructor-name:
5659      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5660      :: [opt] nested-name-specifier template template-id :: ~ type-name
5661      :: [opt] nested-name-specifier [opt] ~ type-name
5662
5663    If either of the first two productions is used, sets *SCOPE to the
5664    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5665    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5666    or ERROR_MARK_NODE if the parse fails.  */
5667
5668 static void
5669 cp_parser_pseudo_destructor_name (cp_parser* parser,
5670                                   tree* scope,
5671                                   tree* type)
5672 {
5673   bool nested_name_specifier_p;
5674
5675   /* Assume that things will not work out.  */
5676   *type = error_mark_node;
5677
5678   /* Look for the optional `::' operator.  */
5679   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5680   /* Look for the optional nested-name-specifier.  */
5681   nested_name_specifier_p
5682     = (cp_parser_nested_name_specifier_opt (parser,
5683                                             /*typename_keyword_p=*/false,
5684                                             /*check_dependency_p=*/true,
5685                                             /*type_p=*/false,
5686                                             /*is_declaration=*/false)
5687        != NULL_TREE);
5688   /* Now, if we saw a nested-name-specifier, we might be doing the
5689      second production.  */
5690   if (nested_name_specifier_p
5691       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5692     {
5693       /* Consume the `template' keyword.  */
5694       cp_lexer_consume_token (parser->lexer);
5695       /* Parse the template-id.  */
5696       cp_parser_template_id (parser,
5697                              /*template_keyword_p=*/true,
5698                              /*check_dependency_p=*/false,
5699                              /*is_declaration=*/true);
5700       /* Look for the `::' token.  */
5701       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5702     }
5703   /* If the next token is not a `~', then there might be some
5704      additional qualification.  */
5705   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5706     {
5707       /* At this point, we're looking for "type-name :: ~".  The type-name
5708          must not be a class-name, since this is a pseudo-destructor.  So,
5709          it must be either an enum-name, or a typedef-name -- both of which
5710          are just identifiers.  So, we peek ahead to check that the "::"
5711          and "~" tokens are present; if they are not, then we can avoid
5712          calling type_name.  */
5713       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5714           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5715           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5716         {
5717           cp_parser_error (parser, "non-scalar type");
5718           return;
5719         }
5720
5721       /* Look for the type-name.  */
5722       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5723       if (*scope == error_mark_node)
5724         return;
5725
5726       /* Look for the `::' token.  */
5727       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5728     }
5729   else
5730     *scope = NULL_TREE;
5731
5732   /* Look for the `~'.  */
5733   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5734   /* Look for the type-name again.  We are not responsible for
5735      checking that it matches the first type-name.  */
5736   *type = cp_parser_nonclass_name (parser);
5737 }
5738
5739 /* Parse a unary-expression.
5740
5741    unary-expression:
5742      postfix-expression
5743      ++ cast-expression
5744      -- cast-expression
5745      unary-operator cast-expression
5746      sizeof unary-expression
5747      sizeof ( type-id )
5748      new-expression
5749      delete-expression
5750
5751    GNU Extensions:
5752
5753    unary-expression:
5754      __extension__ cast-expression
5755      __alignof__ unary-expression
5756      __alignof__ ( type-id )
5757      __real__ cast-expression
5758      __imag__ cast-expression
5759      && identifier
5760
5761    ADDRESS_P is true iff the unary-expression is appearing as the
5762    operand of the `&' operator.   CAST_P is true if this expression is
5763    the target of a cast.
5764
5765    Returns a representation of the expression.  */
5766
5767 static tree
5768 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5769                             cp_id_kind * pidk)
5770 {
5771   cp_token *token;
5772   enum tree_code unary_operator;
5773
5774   /* Peek at the next token.  */
5775   token = cp_lexer_peek_token (parser->lexer);
5776   /* Some keywords give away the kind of expression.  */
5777   if (token->type == CPP_KEYWORD)
5778     {
5779       enum rid keyword = token->keyword;
5780
5781       switch (keyword)
5782         {
5783         case RID_ALIGNOF:
5784         case RID_SIZEOF:
5785           {
5786             tree operand;
5787             enum tree_code op;
5788
5789             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5790             /* Consume the token.  */
5791             cp_lexer_consume_token (parser->lexer);
5792             /* Parse the operand.  */
5793             operand = cp_parser_sizeof_operand (parser, keyword);
5794
5795             if (TYPE_P (operand))
5796               return cxx_sizeof_or_alignof_type (operand, op, true);
5797             else
5798               return cxx_sizeof_or_alignof_expr (operand, op, true);
5799           }
5800
5801         case RID_NEW:
5802           return cp_parser_new_expression (parser);
5803
5804         case RID_DELETE:
5805           return cp_parser_delete_expression (parser);
5806
5807         case RID_EXTENSION:
5808           {
5809             /* The saved value of the PEDANTIC flag.  */
5810             int saved_pedantic;
5811             tree expr;
5812
5813             /* Save away the PEDANTIC flag.  */
5814             cp_parser_extension_opt (parser, &saved_pedantic);
5815             /* Parse the cast-expression.  */
5816             expr = cp_parser_simple_cast_expression (parser);
5817             /* Restore the PEDANTIC flag.  */
5818             pedantic = saved_pedantic;
5819
5820             return expr;
5821           }
5822
5823         case RID_REALPART:
5824         case RID_IMAGPART:
5825           {
5826             tree expression;
5827
5828             /* Consume the `__real__' or `__imag__' token.  */
5829             cp_lexer_consume_token (parser->lexer);
5830             /* Parse the cast-expression.  */
5831             expression = cp_parser_simple_cast_expression (parser);
5832             /* Create the complete representation.  */
5833             return build_x_unary_op ((keyword == RID_REALPART
5834                                       ? REALPART_EXPR : IMAGPART_EXPR),
5835                                      expression,
5836                                      tf_warning_or_error);
5837           }
5838           break;
5839
5840         default:
5841           break;
5842         }
5843     }
5844
5845   /* Look for the `:: new' and `:: delete', which also signal the
5846      beginning of a new-expression, or delete-expression,
5847      respectively.  If the next token is `::', then it might be one of
5848      these.  */
5849   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5850     {
5851       enum rid keyword;
5852
5853       /* See if the token after the `::' is one of the keywords in
5854          which we're interested.  */
5855       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5856       /* If it's `new', we have a new-expression.  */
5857       if (keyword == RID_NEW)
5858         return cp_parser_new_expression (parser);
5859       /* Similarly, for `delete'.  */
5860       else if (keyword == RID_DELETE)
5861         return cp_parser_delete_expression (parser);
5862     }
5863
5864   /* Look for a unary operator.  */
5865   unary_operator = cp_parser_unary_operator (token);
5866   /* The `++' and `--' operators can be handled similarly, even though
5867      they are not technically unary-operators in the grammar.  */
5868   if (unary_operator == ERROR_MARK)
5869     {
5870       if (token->type == CPP_PLUS_PLUS)
5871         unary_operator = PREINCREMENT_EXPR;
5872       else if (token->type == CPP_MINUS_MINUS)
5873         unary_operator = PREDECREMENT_EXPR;
5874       /* Handle the GNU address-of-label extension.  */
5875       else if (cp_parser_allow_gnu_extensions_p (parser)
5876                && token->type == CPP_AND_AND)
5877         {
5878           tree identifier;
5879           tree expression;
5880           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5881
5882           /* Consume the '&&' token.  */
5883           cp_lexer_consume_token (parser->lexer);
5884           /* Look for the identifier.  */
5885           identifier = cp_parser_identifier (parser);
5886           /* Create an expression representing the address.  */
5887           expression = finish_label_address_expr (identifier, loc);
5888           if (cp_parser_non_integral_constant_expression (parser,
5889                                                           NIC_ADDR_LABEL))
5890             expression = error_mark_node;
5891           return expression;
5892         }
5893     }
5894   if (unary_operator != ERROR_MARK)
5895     {
5896       tree cast_expression;
5897       tree expression = error_mark_node;
5898       non_integral_constant non_constant_p = 0;
5899
5900       /* Consume the operator token.  */
5901       token = cp_lexer_consume_token (parser->lexer);
5902       /* Parse the cast-expression.  */
5903       cast_expression
5904         = cp_parser_cast_expression (parser,
5905                                      unary_operator == ADDR_EXPR,
5906                                      /*cast_p=*/false, pidk);
5907       /* Now, build an appropriate representation.  */
5908       switch (unary_operator)
5909         {
5910         case INDIRECT_REF:
5911           non_constant_p = NIC_STAR;
5912           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5913                                              tf_warning_or_error);
5914           break;
5915
5916         case ADDR_EXPR:
5917            non_constant_p = NIC_ADDR;
5918           /* Fall through.  */
5919         case BIT_NOT_EXPR:
5920           expression = build_x_unary_op (unary_operator, cast_expression,
5921                                          tf_warning_or_error);
5922           break;
5923
5924         case PREINCREMENT_EXPR:
5925         case PREDECREMENT_EXPR:
5926           non_constant_p = unary_operator == PREINCREMENT_EXPR
5927                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5928           /* Fall through.  */
5929         case UNARY_PLUS_EXPR:
5930         case NEGATE_EXPR:
5931         case TRUTH_NOT_EXPR:
5932           expression = finish_unary_op_expr (unary_operator, cast_expression);
5933           break;
5934
5935         default:
5936           gcc_unreachable ();
5937         }
5938
5939       if (non_constant_p
5940           && cp_parser_non_integral_constant_expression (parser,
5941                                                          non_constant_p))
5942         expression = error_mark_node;
5943
5944       return expression;
5945     }
5946
5947   return cp_parser_postfix_expression (parser, address_p, cast_p,
5948                                        /*member_access_only_p=*/false,
5949                                        pidk);
5950 }
5951
5952 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5953    unary-operator, the corresponding tree code is returned.  */
5954
5955 static enum tree_code
5956 cp_parser_unary_operator (cp_token* token)
5957 {
5958   switch (token->type)
5959     {
5960     case CPP_MULT:
5961       return INDIRECT_REF;
5962
5963     case CPP_AND:
5964       return ADDR_EXPR;
5965
5966     case CPP_PLUS:
5967       return UNARY_PLUS_EXPR;
5968
5969     case CPP_MINUS:
5970       return NEGATE_EXPR;
5971
5972     case CPP_NOT:
5973       return TRUTH_NOT_EXPR;
5974
5975     case CPP_COMPL:
5976       return BIT_NOT_EXPR;
5977
5978     default:
5979       return ERROR_MARK;
5980     }
5981 }
5982
5983 /* Parse a new-expression.
5984
5985    new-expression:
5986      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5987      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5988
5989    Returns a representation of the expression.  */
5990
5991 static tree
5992 cp_parser_new_expression (cp_parser* parser)
5993 {
5994   bool global_scope_p;
5995   VEC(tree,gc) *placement;
5996   tree type;
5997   VEC(tree,gc) *initializer;
5998   tree nelts;
5999   tree ret;
6000
6001   /* Look for the optional `::' operator.  */
6002   global_scope_p
6003     = (cp_parser_global_scope_opt (parser,
6004                                    /*current_scope_valid_p=*/false)
6005        != NULL_TREE);
6006   /* Look for the `new' operator.  */
6007   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6008   /* There's no easy way to tell a new-placement from the
6009      `( type-id )' construct.  */
6010   cp_parser_parse_tentatively (parser);
6011   /* Look for a new-placement.  */
6012   placement = cp_parser_new_placement (parser);
6013   /* If that didn't work out, there's no new-placement.  */
6014   if (!cp_parser_parse_definitely (parser))
6015     {
6016       if (placement != NULL)
6017         release_tree_vector (placement);
6018       placement = NULL;
6019     }
6020
6021   /* If the next token is a `(', then we have a parenthesized
6022      type-id.  */
6023   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6024     {
6025       cp_token *token;
6026       /* Consume the `('.  */
6027       cp_lexer_consume_token (parser->lexer);
6028       /* Parse the type-id.  */
6029       type = cp_parser_type_id (parser);
6030       /* Look for the closing `)'.  */
6031       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6032       token = cp_lexer_peek_token (parser->lexer);
6033       /* There should not be a direct-new-declarator in this production,
6034          but GCC used to allowed this, so we check and emit a sensible error
6035          message for this case.  */
6036       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6037         {
6038           error_at (token->location,
6039                     "array bound forbidden after parenthesized type-id");
6040           inform (token->location, 
6041                   "try removing the parentheses around the type-id");
6042           cp_parser_direct_new_declarator (parser);
6043         }
6044       nelts = NULL_TREE;
6045     }
6046   /* Otherwise, there must be a new-type-id.  */
6047   else
6048     type = cp_parser_new_type_id (parser, &nelts);
6049
6050   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6051   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6052       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6053     initializer = cp_parser_new_initializer (parser);
6054   else
6055     initializer = NULL;
6056
6057   /* A new-expression may not appear in an integral constant
6058      expression.  */
6059   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6060     ret = error_mark_node;
6061   else
6062     {
6063       /* Create a representation of the new-expression.  */
6064       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6065                        tf_warning_or_error);
6066     }
6067
6068   if (placement != NULL)
6069     release_tree_vector (placement);
6070   if (initializer != NULL)
6071     release_tree_vector (initializer);
6072
6073   return ret;
6074 }
6075
6076 /* Parse a new-placement.
6077
6078    new-placement:
6079      ( expression-list )
6080
6081    Returns the same representation as for an expression-list.  */
6082
6083 static VEC(tree,gc) *
6084 cp_parser_new_placement (cp_parser* parser)
6085 {
6086   VEC(tree,gc) *expression_list;
6087
6088   /* Parse the expression-list.  */
6089   expression_list = (cp_parser_parenthesized_expression_list
6090                      (parser, non_attr, /*cast_p=*/false,
6091                       /*allow_expansion_p=*/true,
6092                       /*non_constant_p=*/NULL));
6093
6094   return expression_list;
6095 }
6096
6097 /* Parse a new-type-id.
6098
6099    new-type-id:
6100      type-specifier-seq new-declarator [opt]
6101
6102    Returns the TYPE allocated.  If the new-type-id indicates an array
6103    type, *NELTS is set to the number of elements in the last array
6104    bound; the TYPE will not include the last array bound.  */
6105
6106 static tree
6107 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6108 {
6109   cp_decl_specifier_seq type_specifier_seq;
6110   cp_declarator *new_declarator;
6111   cp_declarator *declarator;
6112   cp_declarator *outer_declarator;
6113   const char *saved_message;
6114   tree type;
6115
6116   /* The type-specifier sequence must not contain type definitions.
6117      (It cannot contain declarations of new types either, but if they
6118      are not definitions we will catch that because they are not
6119      complete.)  */
6120   saved_message = parser->type_definition_forbidden_message;
6121   parser->type_definition_forbidden_message
6122     = G_("types may not be defined in a new-type-id");
6123   /* Parse the type-specifier-seq.  */
6124   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6125                                 /*is_trailing_return=*/false,
6126                                 &type_specifier_seq);
6127   /* Restore the old message.  */
6128   parser->type_definition_forbidden_message = saved_message;
6129   /* Parse the new-declarator.  */
6130   new_declarator = cp_parser_new_declarator_opt (parser);
6131
6132   /* Determine the number of elements in the last array dimension, if
6133      any.  */
6134   *nelts = NULL_TREE;
6135   /* Skip down to the last array dimension.  */
6136   declarator = new_declarator;
6137   outer_declarator = NULL;
6138   while (declarator && (declarator->kind == cdk_pointer
6139                         || declarator->kind == cdk_ptrmem))
6140     {
6141       outer_declarator = declarator;
6142       declarator = declarator->declarator;
6143     }
6144   while (declarator
6145          && declarator->kind == cdk_array
6146          && declarator->declarator
6147          && declarator->declarator->kind == cdk_array)
6148     {
6149       outer_declarator = declarator;
6150       declarator = declarator->declarator;
6151     }
6152
6153   if (declarator && declarator->kind == cdk_array)
6154     {
6155       *nelts = declarator->u.array.bounds;
6156       if (*nelts == error_mark_node)
6157         *nelts = integer_one_node;
6158
6159       if (outer_declarator)
6160         outer_declarator->declarator = declarator->declarator;
6161       else
6162         new_declarator = NULL;
6163     }
6164
6165   type = groktypename (&type_specifier_seq, new_declarator, false);
6166   return type;
6167 }
6168
6169 /* Parse an (optional) new-declarator.
6170
6171    new-declarator:
6172      ptr-operator new-declarator [opt]
6173      direct-new-declarator
6174
6175    Returns the declarator.  */
6176
6177 static cp_declarator *
6178 cp_parser_new_declarator_opt (cp_parser* parser)
6179 {
6180   enum tree_code code;
6181   tree type;
6182   cp_cv_quals cv_quals;
6183
6184   /* We don't know if there's a ptr-operator next, or not.  */
6185   cp_parser_parse_tentatively (parser);
6186   /* Look for a ptr-operator.  */
6187   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6188   /* If that worked, look for more new-declarators.  */
6189   if (cp_parser_parse_definitely (parser))
6190     {
6191       cp_declarator *declarator;
6192
6193       /* Parse another optional declarator.  */
6194       declarator = cp_parser_new_declarator_opt (parser);
6195
6196       return cp_parser_make_indirect_declarator
6197         (code, type, cv_quals, declarator);
6198     }
6199
6200   /* If the next token is a `[', there is a direct-new-declarator.  */
6201   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6202     return cp_parser_direct_new_declarator (parser);
6203
6204   return NULL;
6205 }
6206
6207 /* Parse a direct-new-declarator.
6208
6209    direct-new-declarator:
6210      [ expression ]
6211      direct-new-declarator [constant-expression]
6212
6213    */
6214
6215 static cp_declarator *
6216 cp_parser_direct_new_declarator (cp_parser* parser)
6217 {
6218   cp_declarator *declarator = NULL;
6219
6220   while (true)
6221     {
6222       tree expression;
6223
6224       /* Look for the opening `['.  */
6225       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6226       /* The first expression is not required to be constant.  */
6227       if (!declarator)
6228         {
6229           cp_token *token = cp_lexer_peek_token (parser->lexer);
6230           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6231           /* The standard requires that the expression have integral
6232              type.  DR 74 adds enumeration types.  We believe that the
6233              real intent is that these expressions be handled like the
6234              expression in a `switch' condition, which also allows
6235              classes with a single conversion to integral or
6236              enumeration type.  */
6237           if (!processing_template_decl)
6238             {
6239               expression
6240                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6241                                               expression,
6242                                               /*complain=*/true);
6243               if (!expression)
6244                 {
6245                   error_at (token->location,
6246                             "expression in new-declarator must have integral "
6247                             "or enumeration type");
6248                   expression = error_mark_node;
6249                 }
6250             }
6251         }
6252       /* But all the other expressions must be.  */
6253       else
6254         expression
6255           = cp_parser_constant_expression (parser,
6256                                            /*allow_non_constant=*/false,
6257                                            NULL);
6258       /* Look for the closing `]'.  */
6259       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6260
6261       /* Add this bound to the declarator.  */
6262       declarator = make_array_declarator (declarator, expression);
6263
6264       /* If the next token is not a `[', then there are no more
6265          bounds.  */
6266       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6267         break;
6268     }
6269
6270   return declarator;
6271 }
6272
6273 /* Parse a new-initializer.
6274
6275    new-initializer:
6276      ( expression-list [opt] )
6277      braced-init-list
6278
6279    Returns a representation of the expression-list.  */
6280
6281 static VEC(tree,gc) *
6282 cp_parser_new_initializer (cp_parser* parser)
6283 {
6284   VEC(tree,gc) *expression_list;
6285
6286   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6287     {
6288       tree t;
6289       bool expr_non_constant_p;
6290       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6291       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6292       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6293       expression_list = make_tree_vector_single (t);
6294     }
6295   else
6296     expression_list = (cp_parser_parenthesized_expression_list
6297                        (parser, non_attr, /*cast_p=*/false,
6298                         /*allow_expansion_p=*/true,
6299                         /*non_constant_p=*/NULL));
6300
6301   return expression_list;
6302 }
6303
6304 /* Parse a delete-expression.
6305
6306    delete-expression:
6307      :: [opt] delete cast-expression
6308      :: [opt] delete [ ] cast-expression
6309
6310    Returns a representation of the expression.  */
6311
6312 static tree
6313 cp_parser_delete_expression (cp_parser* parser)
6314 {
6315   bool global_scope_p;
6316   bool array_p;
6317   tree expression;
6318
6319   /* Look for the optional `::' operator.  */
6320   global_scope_p
6321     = (cp_parser_global_scope_opt (parser,
6322                                    /*current_scope_valid_p=*/false)
6323        != NULL_TREE);
6324   /* Look for the `delete' keyword.  */
6325   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6326   /* See if the array syntax is in use.  */
6327   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6328     {
6329       /* Consume the `[' token.  */
6330       cp_lexer_consume_token (parser->lexer);
6331       /* Look for the `]' token.  */
6332       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6333       /* Remember that this is the `[]' construct.  */
6334       array_p = true;
6335     }
6336   else
6337     array_p = false;
6338
6339   /* Parse the cast-expression.  */
6340   expression = cp_parser_simple_cast_expression (parser);
6341
6342   /* A delete-expression may not appear in an integral constant
6343      expression.  */
6344   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6345     return error_mark_node;
6346
6347   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6348 }
6349
6350 /* Returns true if TOKEN may start a cast-expression and false
6351    otherwise.  */
6352
6353 static bool
6354 cp_parser_token_starts_cast_expression (cp_token *token)
6355 {
6356   switch (token->type)
6357     {
6358     case CPP_COMMA:
6359     case CPP_SEMICOLON:
6360     case CPP_QUERY:
6361     case CPP_COLON:
6362     case CPP_CLOSE_SQUARE:
6363     case CPP_CLOSE_PAREN:
6364     case CPP_CLOSE_BRACE:
6365     case CPP_DOT:
6366     case CPP_DOT_STAR:
6367     case CPP_DEREF:
6368     case CPP_DEREF_STAR:
6369     case CPP_DIV:
6370     case CPP_MOD:
6371     case CPP_LSHIFT:
6372     case CPP_RSHIFT:
6373     case CPP_LESS:
6374     case CPP_GREATER:
6375     case CPP_LESS_EQ:
6376     case CPP_GREATER_EQ:
6377     case CPP_EQ_EQ:
6378     case CPP_NOT_EQ:
6379     case CPP_EQ:
6380     case CPP_MULT_EQ:
6381     case CPP_DIV_EQ:
6382     case CPP_MOD_EQ:
6383     case CPP_PLUS_EQ:
6384     case CPP_MINUS_EQ:
6385     case CPP_RSHIFT_EQ:
6386     case CPP_LSHIFT_EQ:
6387     case CPP_AND_EQ:
6388     case CPP_XOR_EQ:
6389     case CPP_OR_EQ:
6390     case CPP_XOR:
6391     case CPP_OR:
6392     case CPP_OR_OR:
6393     case CPP_EOF:
6394       return false;
6395
6396       /* '[' may start a primary-expression in obj-c++.  */
6397     case CPP_OPEN_SQUARE:
6398       return c_dialect_objc ();
6399
6400     default:
6401       return true;
6402     }
6403 }
6404
6405 /* Parse a cast-expression.
6406
6407    cast-expression:
6408      unary-expression
6409      ( type-id ) cast-expression
6410
6411    ADDRESS_P is true iff the unary-expression is appearing as the
6412    operand of the `&' operator.   CAST_P is true if this expression is
6413    the target of a cast.
6414
6415    Returns a representation of the expression.  */
6416
6417 static tree
6418 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6419                            cp_id_kind * pidk)
6420 {
6421   /* If it's a `(', then we might be looking at a cast.  */
6422   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6423     {
6424       tree type = NULL_TREE;
6425       tree expr = NULL_TREE;
6426       bool compound_literal_p;
6427       const char *saved_message;
6428
6429       /* There's no way to know yet whether or not this is a cast.
6430          For example, `(int (3))' is a unary-expression, while `(int)
6431          3' is a cast.  So, we resort to parsing tentatively.  */
6432       cp_parser_parse_tentatively (parser);
6433       /* Types may not be defined in a cast.  */
6434       saved_message = parser->type_definition_forbidden_message;
6435       parser->type_definition_forbidden_message
6436         = G_("types may not be defined in casts");
6437       /* Consume the `('.  */
6438       cp_lexer_consume_token (parser->lexer);
6439       /* A very tricky bit is that `(struct S) { 3 }' is a
6440          compound-literal (which we permit in C++ as an extension).
6441          But, that construct is not a cast-expression -- it is a
6442          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6443          is legal; if the compound-literal were a cast-expression,
6444          you'd need an extra set of parentheses.)  But, if we parse
6445          the type-id, and it happens to be a class-specifier, then we
6446          will commit to the parse at that point, because we cannot
6447          undo the action that is done when creating a new class.  So,
6448          then we cannot back up and do a postfix-expression.
6449
6450          Therefore, we scan ahead to the closing `)', and check to see
6451          if the token after the `)' is a `{'.  If so, we are not
6452          looking at a cast-expression.
6453
6454          Save tokens so that we can put them back.  */
6455       cp_lexer_save_tokens (parser->lexer);
6456       /* Skip tokens until the next token is a closing parenthesis.
6457          If we find the closing `)', and the next token is a `{', then
6458          we are looking at a compound-literal.  */
6459       compound_literal_p
6460         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6461                                                   /*consume_paren=*/true)
6462            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6463       /* Roll back the tokens we skipped.  */
6464       cp_lexer_rollback_tokens (parser->lexer);
6465       /* If we were looking at a compound-literal, simulate an error
6466          so that the call to cp_parser_parse_definitely below will
6467          fail.  */
6468       if (compound_literal_p)
6469         cp_parser_simulate_error (parser);
6470       else
6471         {
6472           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6473           parser->in_type_id_in_expr_p = true;
6474           /* Look for the type-id.  */
6475           type = cp_parser_type_id (parser);
6476           /* Look for the closing `)'.  */
6477           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6478           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6479         }
6480
6481       /* Restore the saved message.  */
6482       parser->type_definition_forbidden_message = saved_message;
6483
6484       /* At this point this can only be either a cast or a
6485          parenthesized ctor such as `(T ())' that looks like a cast to
6486          function returning T.  */
6487       if (!cp_parser_error_occurred (parser)
6488           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6489                                                      (parser->lexer)))
6490         {
6491           cp_parser_parse_definitely (parser);
6492           expr = cp_parser_cast_expression (parser,
6493                                             /*address_p=*/false,
6494                                             /*cast_p=*/true, pidk);
6495
6496           /* Warn about old-style casts, if so requested.  */
6497           if (warn_old_style_cast
6498               && !in_system_header
6499               && !VOID_TYPE_P (type)
6500               && current_lang_name != lang_name_c)
6501             warning (OPT_Wold_style_cast, "use of old-style cast");
6502
6503           /* Only type conversions to integral or enumeration types
6504              can be used in constant-expressions.  */
6505           if (!cast_valid_in_integral_constant_expression_p (type)
6506               && (cp_parser_non_integral_constant_expression (parser,
6507                                                               NIC_CAST)))
6508             return error_mark_node;
6509
6510           /* Perform the cast.  */
6511           expr = build_c_cast (input_location, type, expr);
6512           return expr;
6513         }
6514       else 
6515         cp_parser_abort_tentative_parse (parser);
6516     }
6517
6518   /* If we get here, then it's not a cast, so it must be a
6519      unary-expression.  */
6520   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6521 }
6522
6523 /* Parse a binary expression of the general form:
6524
6525    pm-expression:
6526      cast-expression
6527      pm-expression .* cast-expression
6528      pm-expression ->* cast-expression
6529
6530    multiplicative-expression:
6531      pm-expression
6532      multiplicative-expression * pm-expression
6533      multiplicative-expression / pm-expression
6534      multiplicative-expression % pm-expression
6535
6536    additive-expression:
6537      multiplicative-expression
6538      additive-expression + multiplicative-expression
6539      additive-expression - multiplicative-expression
6540
6541    shift-expression:
6542      additive-expression
6543      shift-expression << additive-expression
6544      shift-expression >> additive-expression
6545
6546    relational-expression:
6547      shift-expression
6548      relational-expression < shift-expression
6549      relational-expression > shift-expression
6550      relational-expression <= shift-expression
6551      relational-expression >= shift-expression
6552
6553   GNU Extension:
6554
6555    relational-expression:
6556      relational-expression <? shift-expression
6557      relational-expression >? shift-expression
6558
6559    equality-expression:
6560      relational-expression
6561      equality-expression == relational-expression
6562      equality-expression != relational-expression
6563
6564    and-expression:
6565      equality-expression
6566      and-expression & equality-expression
6567
6568    exclusive-or-expression:
6569      and-expression
6570      exclusive-or-expression ^ and-expression
6571
6572    inclusive-or-expression:
6573      exclusive-or-expression
6574      inclusive-or-expression | exclusive-or-expression
6575
6576    logical-and-expression:
6577      inclusive-or-expression
6578      logical-and-expression && inclusive-or-expression
6579
6580    logical-or-expression:
6581      logical-and-expression
6582      logical-or-expression || logical-and-expression
6583
6584    All these are implemented with a single function like:
6585
6586    binary-expression:
6587      simple-cast-expression
6588      binary-expression <token> binary-expression
6589
6590    CAST_P is true if this expression is the target of a cast.
6591
6592    The binops_by_token map is used to get the tree codes for each <token> type.
6593    binary-expressions are associated according to a precedence table.  */
6594
6595 #define TOKEN_PRECEDENCE(token)                              \
6596 (((token->type == CPP_GREATER                                \
6597    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6598   && !parser->greater_than_is_operator_p)                    \
6599  ? PREC_NOT_OPERATOR                                         \
6600  : binops_by_token[token->type].prec)
6601
6602 static tree
6603 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6604                              bool no_toplevel_fold_p,
6605                              enum cp_parser_prec prec,
6606                              cp_id_kind * pidk)
6607 {
6608   cp_parser_expression_stack stack;
6609   cp_parser_expression_stack_entry *sp = &stack[0];
6610   tree lhs, rhs;
6611   cp_token *token;
6612   enum tree_code tree_type, lhs_type, rhs_type;
6613   enum cp_parser_prec new_prec, lookahead_prec;
6614   bool overloaded_p;
6615
6616   /* Parse the first expression.  */
6617   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6618   lhs_type = ERROR_MARK;
6619
6620   for (;;)
6621     {
6622       /* Get an operator token.  */
6623       token = cp_lexer_peek_token (parser->lexer);
6624
6625       if (warn_cxx0x_compat
6626           && token->type == CPP_RSHIFT
6627           && !parser->greater_than_is_operator_p)
6628         {
6629           if (warning_at (token->location, OPT_Wc__0x_compat, 
6630                           "%<>>%> operator will be treated as"
6631                           " two right angle brackets in C++0x"))
6632             inform (token->location,
6633                     "suggest parentheses around %<>>%> expression");
6634         }
6635
6636       new_prec = TOKEN_PRECEDENCE (token);
6637
6638       /* Popping an entry off the stack means we completed a subexpression:
6639          - either we found a token which is not an operator (`>' where it is not
6640            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6641            will happen repeatedly;
6642          - or, we found an operator which has lower priority.  This is the case
6643            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6644            parsing `3 * 4'.  */
6645       if (new_prec <= prec)
6646         {
6647           if (sp == stack)
6648             break;
6649           else
6650             goto pop;
6651         }
6652
6653      get_rhs:
6654       tree_type = binops_by_token[token->type].tree_type;
6655
6656       /* We used the operator token.  */
6657       cp_lexer_consume_token (parser->lexer);
6658
6659       /* For "false && x" or "true || x", x will never be executed;
6660          disable warnings while evaluating it.  */
6661       if (tree_type == TRUTH_ANDIF_EXPR)
6662         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6663       else if (tree_type == TRUTH_ORIF_EXPR)
6664         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6665
6666       /* Extract another operand.  It may be the RHS of this expression
6667          or the LHS of a new, higher priority expression.  */
6668       rhs = cp_parser_simple_cast_expression (parser);
6669       rhs_type = ERROR_MARK;
6670
6671       /* Get another operator token.  Look up its precedence to avoid
6672          building a useless (immediately popped) stack entry for common
6673          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6674       token = cp_lexer_peek_token (parser->lexer);
6675       lookahead_prec = TOKEN_PRECEDENCE (token);
6676       if (lookahead_prec > new_prec)
6677         {
6678           /* ... and prepare to parse the RHS of the new, higher priority
6679              expression.  Since precedence levels on the stack are
6680              monotonically increasing, we do not have to care about
6681              stack overflows.  */
6682           sp->prec = prec;
6683           sp->tree_type = tree_type;
6684           sp->lhs = lhs;
6685           sp->lhs_type = lhs_type;
6686           sp++;
6687           lhs = rhs;
6688           lhs_type = rhs_type;
6689           prec = new_prec;
6690           new_prec = lookahead_prec;
6691           goto get_rhs;
6692
6693          pop:
6694           lookahead_prec = new_prec;
6695           /* If the stack is not empty, we have parsed into LHS the right side
6696              (`4' in the example above) of an expression we had suspended.
6697              We can use the information on the stack to recover the LHS (`3')
6698              from the stack together with the tree code (`MULT_EXPR'), and
6699              the precedence of the higher level subexpression
6700              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6701              which will be used to actually build the additive expression.  */
6702           --sp;
6703           prec = sp->prec;
6704           tree_type = sp->tree_type;
6705           rhs = lhs;
6706           rhs_type = lhs_type;
6707           lhs = sp->lhs;
6708           lhs_type = sp->lhs_type;
6709         }
6710
6711       /* Undo the disabling of warnings done above.  */
6712       if (tree_type == TRUTH_ANDIF_EXPR)
6713         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6714       else if (tree_type == TRUTH_ORIF_EXPR)
6715         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6716
6717       overloaded_p = false;
6718       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6719          ERROR_MARK for everything that is not a binary expression.
6720          This makes warn_about_parentheses miss some warnings that
6721          involve unary operators.  For unary expressions we should
6722          pass the correct tree_code unless the unary expression was
6723          surrounded by parentheses.
6724       */
6725       if (no_toplevel_fold_p
6726           && lookahead_prec <= prec
6727           && sp == stack
6728           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6729         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6730       else
6731         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6732                                  &overloaded_p, tf_warning_or_error);
6733       lhs_type = tree_type;
6734
6735       /* If the binary operator required the use of an overloaded operator,
6736          then this expression cannot be an integral constant-expression.
6737          An overloaded operator can be used even if both operands are
6738          otherwise permissible in an integral constant-expression if at
6739          least one of the operands is of enumeration type.  */
6740
6741       if (overloaded_p
6742           && (cp_parser_non_integral_constant_expression (parser,
6743                                                           NIC_OVERLOADED)))
6744         return error_mark_node;
6745     }
6746
6747   return lhs;
6748 }
6749
6750
6751 /* Parse the `? expression : assignment-expression' part of a
6752    conditional-expression.  The LOGICAL_OR_EXPR is the
6753    logical-or-expression that started the conditional-expression.
6754    Returns a representation of the entire conditional-expression.
6755
6756    This routine is used by cp_parser_assignment_expression.
6757
6758      ? expression : assignment-expression
6759
6760    GNU Extensions:
6761
6762      ? : assignment-expression */
6763
6764 static tree
6765 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6766 {
6767   tree expr;
6768   tree assignment_expr;
6769
6770   /* Consume the `?' token.  */
6771   cp_lexer_consume_token (parser->lexer);
6772   if (cp_parser_allow_gnu_extensions_p (parser)
6773       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6774     {
6775       /* Implicit true clause.  */
6776       expr = NULL_TREE;
6777       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6778     }
6779   else
6780     {
6781       /* Parse the expression.  */
6782       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6783       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6784       c_inhibit_evaluation_warnings +=
6785         ((logical_or_expr == truthvalue_true_node)
6786          - (logical_or_expr == truthvalue_false_node));
6787     }
6788
6789   /* The next token should be a `:'.  */
6790   cp_parser_require (parser, CPP_COLON, RT_COLON);
6791   /* Parse the assignment-expression.  */
6792   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6793   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6794
6795   /* Build the conditional-expression.  */
6796   return build_x_conditional_expr (logical_or_expr,
6797                                    expr,
6798                                    assignment_expr,
6799                                    tf_warning_or_error);
6800 }
6801
6802 /* Parse an assignment-expression.
6803
6804    assignment-expression:
6805      conditional-expression
6806      logical-or-expression assignment-operator assignment_expression
6807      throw-expression
6808
6809    CAST_P is true if this expression is the target of a cast.
6810
6811    Returns a representation for the expression.  */
6812
6813 static tree
6814 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6815                                  cp_id_kind * pidk)
6816 {
6817   tree expr;
6818
6819   /* If the next token is the `throw' keyword, then we're looking at
6820      a throw-expression.  */
6821   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6822     expr = cp_parser_throw_expression (parser);
6823   /* Otherwise, it must be that we are looking at a
6824      logical-or-expression.  */
6825   else
6826     {
6827       /* Parse the binary expressions (logical-or-expression).  */
6828       expr = cp_parser_binary_expression (parser, cast_p, false,
6829                                           PREC_NOT_OPERATOR, pidk);
6830       /* If the next token is a `?' then we're actually looking at a
6831          conditional-expression.  */
6832       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6833         return cp_parser_question_colon_clause (parser, expr);
6834       else
6835         {
6836           enum tree_code assignment_operator;
6837
6838           /* If it's an assignment-operator, we're using the second
6839              production.  */
6840           assignment_operator
6841             = cp_parser_assignment_operator_opt (parser);
6842           if (assignment_operator != ERROR_MARK)
6843             {
6844               bool non_constant_p;
6845
6846               /* Parse the right-hand side of the assignment.  */
6847               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6848
6849               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6850                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6851
6852               /* An assignment may not appear in a
6853                  constant-expression.  */
6854               if (cp_parser_non_integral_constant_expression (parser,
6855                                                               NIC_ASSIGNMENT))
6856                 return error_mark_node;
6857               /* Build the assignment expression.  */
6858               expr = build_x_modify_expr (expr,
6859                                           assignment_operator,
6860                                           rhs,
6861                                           tf_warning_or_error);
6862             }
6863         }
6864     }
6865
6866   return expr;
6867 }
6868
6869 /* Parse an (optional) assignment-operator.
6870
6871    assignment-operator: one of
6872      = *= /= %= += -= >>= <<= &= ^= |=
6873
6874    GNU Extension:
6875
6876    assignment-operator: one of
6877      <?= >?=
6878
6879    If the next token is an assignment operator, the corresponding tree
6880    code is returned, and the token is consumed.  For example, for
6881    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6882    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6883    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6884    operator, ERROR_MARK is returned.  */
6885
6886 static enum tree_code
6887 cp_parser_assignment_operator_opt (cp_parser* parser)
6888 {
6889   enum tree_code op;
6890   cp_token *token;
6891
6892   /* Peek at the next token.  */
6893   token = cp_lexer_peek_token (parser->lexer);
6894
6895   switch (token->type)
6896     {
6897     case CPP_EQ:
6898       op = NOP_EXPR;
6899       break;
6900
6901     case CPP_MULT_EQ:
6902       op = MULT_EXPR;
6903       break;
6904
6905     case CPP_DIV_EQ:
6906       op = TRUNC_DIV_EXPR;
6907       break;
6908
6909     case CPP_MOD_EQ:
6910       op = TRUNC_MOD_EXPR;
6911       break;
6912
6913     case CPP_PLUS_EQ:
6914       op = PLUS_EXPR;
6915       break;
6916
6917     case CPP_MINUS_EQ:
6918       op = MINUS_EXPR;
6919       break;
6920
6921     case CPP_RSHIFT_EQ:
6922       op = RSHIFT_EXPR;
6923       break;
6924
6925     case CPP_LSHIFT_EQ:
6926       op = LSHIFT_EXPR;
6927       break;
6928
6929     case CPP_AND_EQ:
6930       op = BIT_AND_EXPR;
6931       break;
6932
6933     case CPP_XOR_EQ:
6934       op = BIT_XOR_EXPR;
6935       break;
6936
6937     case CPP_OR_EQ:
6938       op = BIT_IOR_EXPR;
6939       break;
6940
6941     default:
6942       /* Nothing else is an assignment operator.  */
6943       op = ERROR_MARK;
6944     }
6945
6946   /* If it was an assignment operator, consume it.  */
6947   if (op != ERROR_MARK)
6948     cp_lexer_consume_token (parser->lexer);
6949
6950   return op;
6951 }
6952
6953 /* Parse an expression.
6954
6955    expression:
6956      assignment-expression
6957      expression , assignment-expression
6958
6959    CAST_P is true if this expression is the target of a cast.
6960
6961    Returns a representation of the expression.  */
6962
6963 static tree
6964 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6965 {
6966   tree expression = NULL_TREE;
6967
6968   while (true)
6969     {
6970       tree assignment_expression;
6971
6972       /* Parse the next assignment-expression.  */
6973       assignment_expression
6974         = cp_parser_assignment_expression (parser, cast_p, pidk);
6975       /* If this is the first assignment-expression, we can just
6976          save it away.  */
6977       if (!expression)
6978         expression = assignment_expression;
6979       else
6980         expression = build_x_compound_expr (expression,
6981                                             assignment_expression,
6982                                             tf_warning_or_error);
6983       /* If the next token is not a comma, then we are done with the
6984          expression.  */
6985       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6986         break;
6987       /* Consume the `,'.  */
6988       cp_lexer_consume_token (parser->lexer);
6989       /* A comma operator cannot appear in a constant-expression.  */
6990       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6991         expression = error_mark_node;
6992     }
6993
6994   return expression;
6995 }
6996
6997 /* Parse a constant-expression.
6998
6999    constant-expression:
7000      conditional-expression
7001
7002   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7003   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7004   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7005   is false, NON_CONSTANT_P should be NULL.  */
7006
7007 static tree
7008 cp_parser_constant_expression (cp_parser* parser,
7009                                bool allow_non_constant_p,
7010                                bool *non_constant_p)
7011 {
7012   bool saved_integral_constant_expression_p;
7013   bool saved_allow_non_integral_constant_expression_p;
7014   bool saved_non_integral_constant_expression_p;
7015   tree expression;
7016
7017   /* It might seem that we could simply parse the
7018      conditional-expression, and then check to see if it were
7019      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7020      one that the compiler can figure out is constant, possibly after
7021      doing some simplifications or optimizations.  The standard has a
7022      precise definition of constant-expression, and we must honor
7023      that, even though it is somewhat more restrictive.
7024
7025      For example:
7026
7027        int i[(2, 3)];
7028
7029      is not a legal declaration, because `(2, 3)' is not a
7030      constant-expression.  The `,' operator is forbidden in a
7031      constant-expression.  However, GCC's constant-folding machinery
7032      will fold this operation to an INTEGER_CST for `3'.  */
7033
7034   /* Save the old settings.  */
7035   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7036   saved_allow_non_integral_constant_expression_p
7037     = parser->allow_non_integral_constant_expression_p;
7038   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7039   /* We are now parsing a constant-expression.  */
7040   parser->integral_constant_expression_p = true;
7041   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
7042   parser->non_integral_constant_expression_p = false;
7043   /* Although the grammar says "conditional-expression", we parse an
7044      "assignment-expression", which also permits "throw-expression"
7045      and the use of assignment operators.  In the case that
7046      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7047      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7048      actually essential that we look for an assignment-expression.
7049      For example, cp_parser_initializer_clauses uses this function to
7050      determine whether a particular assignment-expression is in fact
7051      constant.  */
7052   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7053   /* Restore the old settings.  */
7054   parser->integral_constant_expression_p
7055     = saved_integral_constant_expression_p;
7056   parser->allow_non_integral_constant_expression_p
7057     = saved_allow_non_integral_constant_expression_p;
7058   if (allow_non_constant_p)
7059     *non_constant_p = parser->non_integral_constant_expression_p;
7060   else if (parser->non_integral_constant_expression_p)
7061     expression = error_mark_node;
7062   parser->non_integral_constant_expression_p
7063     = saved_non_integral_constant_expression_p;
7064
7065   return expression;
7066 }
7067
7068 /* Parse __builtin_offsetof.
7069
7070    offsetof-expression:
7071      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7072
7073    offsetof-member-designator:
7074      id-expression
7075      | offsetof-member-designator "." id-expression
7076      | offsetof-member-designator "[" expression "]"
7077      | offsetof-member-designator "->" id-expression  */
7078
7079 static tree
7080 cp_parser_builtin_offsetof (cp_parser *parser)
7081 {
7082   int save_ice_p, save_non_ice_p;
7083   tree type, expr;
7084   cp_id_kind dummy;
7085   cp_token *token;
7086
7087   /* We're about to accept non-integral-constant things, but will
7088      definitely yield an integral constant expression.  Save and
7089      restore these values around our local parsing.  */
7090   save_ice_p = parser->integral_constant_expression_p;
7091   save_non_ice_p = parser->non_integral_constant_expression_p;
7092
7093   /* Consume the "__builtin_offsetof" token.  */
7094   cp_lexer_consume_token (parser->lexer);
7095   /* Consume the opening `('.  */
7096   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7097   /* Parse the type-id.  */
7098   type = cp_parser_type_id (parser);
7099   /* Look for the `,'.  */
7100   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7101   token = cp_lexer_peek_token (parser->lexer);
7102
7103   /* Build the (type *)null that begins the traditional offsetof macro.  */
7104   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7105                             tf_warning_or_error);
7106
7107   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7108   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7109                                                  true, &dummy, token->location);
7110   while (true)
7111     {
7112       token = cp_lexer_peek_token (parser->lexer);
7113       switch (token->type)
7114         {
7115         case CPP_OPEN_SQUARE:
7116           /* offsetof-member-designator "[" expression "]" */
7117           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7118           break;
7119
7120         case CPP_DEREF:
7121           /* offsetof-member-designator "->" identifier */
7122           expr = grok_array_decl (expr, integer_zero_node);
7123           /* FALLTHRU */
7124
7125         case CPP_DOT:
7126           /* offsetof-member-designator "." identifier */
7127           cp_lexer_consume_token (parser->lexer);
7128           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7129                                                          expr, true, &dummy,
7130                                                          token->location);
7131           break;
7132
7133         case CPP_CLOSE_PAREN:
7134           /* Consume the ")" token.  */
7135           cp_lexer_consume_token (parser->lexer);
7136           goto success;
7137
7138         default:
7139           /* Error.  We know the following require will fail, but
7140              that gives the proper error message.  */
7141           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7142           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7143           expr = error_mark_node;
7144           goto failure;
7145         }
7146     }
7147
7148  success:
7149   /* If we're processing a template, we can't finish the semantics yet.
7150      Otherwise we can fold the entire expression now.  */
7151   if (processing_template_decl)
7152     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7153   else
7154     expr = finish_offsetof (expr);
7155
7156  failure:
7157   parser->integral_constant_expression_p = save_ice_p;
7158   parser->non_integral_constant_expression_p = save_non_ice_p;
7159
7160   return expr;
7161 }
7162
7163 /* Parse a trait expression.  */
7164
7165 static tree
7166 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7167 {
7168   cp_trait_kind kind;
7169   tree type1, type2 = NULL_TREE;
7170   bool binary = false;
7171   cp_decl_specifier_seq decl_specs;
7172
7173   switch (keyword)
7174     {
7175     case RID_HAS_NOTHROW_ASSIGN:
7176       kind = CPTK_HAS_NOTHROW_ASSIGN;
7177       break;
7178     case RID_HAS_NOTHROW_CONSTRUCTOR:
7179       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7180       break;
7181     case RID_HAS_NOTHROW_COPY:
7182       kind = CPTK_HAS_NOTHROW_COPY;
7183       break;
7184     case RID_HAS_TRIVIAL_ASSIGN:
7185       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7186       break;
7187     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7188       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7189       break;
7190     case RID_HAS_TRIVIAL_COPY:
7191       kind = CPTK_HAS_TRIVIAL_COPY;
7192       break;
7193     case RID_HAS_TRIVIAL_DESTRUCTOR:
7194       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7195       break;
7196     case RID_HAS_VIRTUAL_DESTRUCTOR:
7197       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7198       break;
7199     case RID_IS_ABSTRACT:
7200       kind = CPTK_IS_ABSTRACT;
7201       break;
7202     case RID_IS_BASE_OF:
7203       kind = CPTK_IS_BASE_OF;
7204       binary = true;
7205       break;
7206     case RID_IS_CLASS:
7207       kind = CPTK_IS_CLASS;
7208       break;
7209     case RID_IS_CONVERTIBLE_TO:
7210       kind = CPTK_IS_CONVERTIBLE_TO;
7211       binary = true;
7212       break;
7213     case RID_IS_EMPTY:
7214       kind = CPTK_IS_EMPTY;
7215       break;
7216     case RID_IS_ENUM:
7217       kind = CPTK_IS_ENUM;
7218       break;
7219     case RID_IS_POD:
7220       kind = CPTK_IS_POD;
7221       break;
7222     case RID_IS_POLYMORPHIC:
7223       kind = CPTK_IS_POLYMORPHIC;
7224       break;
7225     case RID_IS_STD_LAYOUT:
7226       kind = CPTK_IS_STD_LAYOUT;
7227       break;
7228     case RID_IS_TRIVIAL:
7229       kind = CPTK_IS_TRIVIAL;
7230       break;
7231     case RID_IS_UNION:
7232       kind = CPTK_IS_UNION;
7233       break;
7234     default:
7235       gcc_unreachable ();
7236     }
7237
7238   /* Consume the token.  */
7239   cp_lexer_consume_token (parser->lexer);
7240
7241   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7242
7243   type1 = cp_parser_type_id (parser);
7244
7245   if (type1 == error_mark_node)
7246     return error_mark_node;
7247
7248   /* Build a trivial decl-specifier-seq.  */
7249   clear_decl_specs (&decl_specs);
7250   decl_specs.type = type1;
7251
7252   /* Call grokdeclarator to figure out what type this is.  */
7253   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7254                           /*initialized=*/0, /*attrlist=*/NULL);
7255
7256   if (binary)
7257     {
7258       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7259  
7260       type2 = cp_parser_type_id (parser);
7261
7262       if (type2 == error_mark_node)
7263         return error_mark_node;
7264
7265       /* Build a trivial decl-specifier-seq.  */
7266       clear_decl_specs (&decl_specs);
7267       decl_specs.type = type2;
7268
7269       /* Call grokdeclarator to figure out what type this is.  */
7270       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7271                               /*initialized=*/0, /*attrlist=*/NULL);
7272     }
7273
7274   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7275
7276   /* Complete the trait expression, which may mean either processing
7277      the trait expr now or saving it for template instantiation.  */
7278   return finish_trait_expr (kind, type1, type2);
7279 }
7280
7281 /* Lambdas that appear in variable initializer or default argument scope
7282    get that in their mangling, so we need to record it.  We might as well
7283    use the count for function and namespace scopes as well.  */
7284 static GTY(()) tree lambda_scope;
7285 static GTY(()) int lambda_count;
7286 typedef struct GTY(()) tree_int
7287 {
7288   tree t;
7289   int i;
7290 } tree_int;
7291 DEF_VEC_O(tree_int);
7292 DEF_VEC_ALLOC_O(tree_int,gc);
7293 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7294
7295 static void
7296 start_lambda_scope (tree decl)
7297 {
7298   tree_int ti;
7299   gcc_assert (decl);
7300   /* Once we're inside a function, we ignore other scopes and just push
7301      the function again so that popping works properly.  */
7302   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7303     decl = current_function_decl;
7304   ti.t = lambda_scope;
7305   ti.i = lambda_count;
7306   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7307   if (lambda_scope != decl)
7308     {
7309       /* Don't reset the count if we're still in the same function.  */
7310       lambda_scope = decl;
7311       lambda_count = 0;
7312     }
7313 }
7314
7315 static void
7316 record_lambda_scope (tree lambda)
7317 {
7318   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7319   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7320 }
7321
7322 static void
7323 finish_lambda_scope (void)
7324 {
7325   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7326   if (lambda_scope != p->t)
7327     {
7328       lambda_scope = p->t;
7329       lambda_count = p->i;
7330     }
7331   VEC_pop (tree_int, lambda_scope_stack);
7332 }
7333
7334 /* Parse a lambda expression.
7335
7336    lambda-expression:
7337      lambda-introducer lambda-declarator [opt] compound-statement
7338
7339    Returns a representation of the expression.  */
7340
7341 static tree
7342 cp_parser_lambda_expression (cp_parser* parser)
7343 {
7344   tree lambda_expr = build_lambda_expr ();
7345   tree type;
7346
7347   LAMBDA_EXPR_LOCATION (lambda_expr)
7348     = cp_lexer_peek_token (parser->lexer)->location;
7349
7350   if (cp_unevaluated_operand)
7351     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7352               "lambda-expression in unevaluated context");
7353
7354   /* We may be in the middle of deferred access check.  Disable
7355      it now.  */
7356   push_deferring_access_checks (dk_no_deferred);
7357
7358   cp_parser_lambda_introducer (parser, lambda_expr);
7359
7360   type = begin_lambda_type (lambda_expr);
7361
7362   record_lambda_scope (lambda_expr);
7363
7364   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7365   determine_visibility (TYPE_NAME (type));
7366
7367   /* Now that we've started the type, add the capture fields for any
7368      explicit captures.  */
7369   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7370
7371   {
7372     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7373     unsigned int saved_num_template_parameter_lists
7374         = parser->num_template_parameter_lists;
7375
7376     parser->num_template_parameter_lists = 0;
7377
7378     /* By virtue of defining a local class, a lambda expression has access to
7379        the private variables of enclosing classes.  */
7380
7381     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7382
7383     cp_parser_lambda_body (parser, lambda_expr);
7384
7385     /* The capture list was built up in reverse order; fix that now.  */
7386     {
7387       tree newlist = NULL_TREE;
7388       tree elt, next;
7389
7390       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7391            elt; elt = next)
7392         {
7393           tree field = TREE_PURPOSE (elt);
7394           char *buf;
7395
7396           next = TREE_CHAIN (elt);
7397           TREE_CHAIN (elt) = newlist;
7398           newlist = elt;
7399
7400           /* Also add __ to the beginning of the field name so that code
7401              outside the lambda body can't see the captured name.  We could
7402              just remove the name entirely, but this is more useful for
7403              debugging.  */
7404           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7405             /* The 'this' capture already starts with __.  */
7406             continue;
7407
7408           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7409           buf[1] = buf[0] = '_';
7410           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7411                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7412           DECL_NAME (field) = get_identifier (buf);
7413         }
7414       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7415     }
7416
7417     maybe_add_lambda_conv_op (type);
7418
7419     type = finish_struct (type, /*attributes=*/NULL_TREE);
7420
7421     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7422   }
7423
7424   pop_deferring_access_checks ();
7425
7426   return build_lambda_object (lambda_expr);
7427 }
7428
7429 /* Parse the beginning of a lambda expression.
7430
7431    lambda-introducer:
7432      [ lambda-capture [opt] ]
7433
7434    LAMBDA_EXPR is the current representation of the lambda expression.  */
7435
7436 static void
7437 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7438 {
7439   /* Need commas after the first capture.  */
7440   bool first = true;
7441
7442   /* Eat the leading `['.  */
7443   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7444
7445   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7446   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7447       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7448     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7449   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7450     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7451
7452   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7453     {
7454       cp_lexer_consume_token (parser->lexer);
7455       first = false;
7456     }
7457
7458   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7459     {
7460       cp_token* capture_token;
7461       tree capture_id;
7462       tree capture_init_expr;
7463       cp_id_kind idk = CP_ID_KIND_NONE;
7464       bool explicit_init_p = false;
7465
7466       enum capture_kind_type
7467       {
7468         BY_COPY,
7469         BY_REFERENCE
7470       };
7471       enum capture_kind_type capture_kind = BY_COPY;
7472
7473       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7474         {
7475           error ("expected end of capture-list");
7476           return;
7477         }
7478
7479       if (first)
7480         first = false;
7481       else
7482         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7483
7484       /* Possibly capture `this'.  */
7485       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7486         {
7487           cp_lexer_consume_token (parser->lexer);
7488           add_capture (lambda_expr,
7489                        /*id=*/get_identifier ("__this"),
7490                        /*initializer=*/finish_this_expr(),
7491                        /*by_reference_p=*/false,
7492                        explicit_init_p);
7493           continue;
7494         }
7495
7496       /* Remember whether we want to capture as a reference or not.  */
7497       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7498         {
7499           capture_kind = BY_REFERENCE;
7500           cp_lexer_consume_token (parser->lexer);
7501         }
7502
7503       /* Get the identifier.  */
7504       capture_token = cp_lexer_peek_token (parser->lexer);
7505       capture_id = cp_parser_identifier (parser);
7506
7507       if (capture_id == error_mark_node)
7508         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7509            delimiters, but I modified this to stop on unnested ']' as well.  It
7510            was already changed to stop on unnested '}', so the
7511            "closing_parenthesis" name is no more misleading with my change.  */
7512         {
7513           cp_parser_skip_to_closing_parenthesis (parser,
7514                                                  /*recovering=*/true,
7515                                                  /*or_comma=*/true,
7516                                                  /*consume_paren=*/true);
7517           break;
7518         }
7519
7520       /* Find the initializer for this capture.  */
7521       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7522         {
7523           /* An explicit expression exists.  */
7524           cp_lexer_consume_token (parser->lexer);
7525           pedwarn (input_location, OPT_pedantic,
7526                    "ISO C++ does not allow initializers "
7527                    "in lambda expression capture lists");
7528           capture_init_expr = cp_parser_assignment_expression (parser,
7529                                                                /*cast_p=*/true,
7530                                                                &idk);
7531           explicit_init_p = true;
7532         }
7533       else
7534         {
7535           const char* error_msg;
7536
7537           /* Turn the identifier into an id-expression.  */
7538           capture_init_expr
7539             = cp_parser_lookup_name
7540                 (parser,
7541                  capture_id,
7542                  none_type,
7543                  /*is_template=*/false,
7544                  /*is_namespace=*/false,
7545                  /*check_dependency=*/true,
7546                  /*ambiguous_decls=*/NULL,
7547                  capture_token->location);
7548
7549           capture_init_expr
7550             = finish_id_expression
7551                 (capture_id,
7552                  capture_init_expr,
7553                  parser->scope,
7554                  &idk,
7555                  /*integral_constant_expression_p=*/false,
7556                  /*allow_non_integral_constant_expression_p=*/false,
7557                  /*non_integral_constant_expression_p=*/NULL,
7558                  /*template_p=*/false,
7559                  /*done=*/true,
7560                  /*address_p=*/false,
7561                  /*template_arg_p=*/false,
7562                  &error_msg,
7563                  capture_token->location);
7564         }
7565
7566       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7567         capture_init_expr
7568           = unqualified_name_lookup_error (capture_init_expr);
7569
7570       add_capture (lambda_expr,
7571                    capture_id,
7572                    capture_init_expr,
7573                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7574                    explicit_init_p);
7575     }
7576
7577   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7578 }
7579
7580 /* Parse the (optional) middle of a lambda expression.
7581
7582    lambda-declarator:
7583      ( parameter-declaration-clause [opt] )
7584        attribute-specifier [opt]
7585        mutable [opt]
7586        exception-specification [opt]
7587        lambda-return-type-clause [opt]
7588
7589    LAMBDA_EXPR is the current representation of the lambda expression.  */
7590
7591 static void
7592 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7593 {
7594   /* 5.1.1.4 of the standard says:
7595        If a lambda-expression does not include a lambda-declarator, it is as if
7596        the lambda-declarator were ().
7597      This means an empty parameter list, no attributes, and no exception
7598      specification.  */
7599   tree param_list = void_list_node;
7600   tree attributes = NULL_TREE;
7601   tree exception_spec = NULL_TREE;
7602   tree t;
7603
7604   /* The lambda-declarator is optional, but must begin with an opening
7605      parenthesis if present.  */
7606   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7607     {
7608       cp_lexer_consume_token (parser->lexer);
7609
7610       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7611
7612       /* Parse parameters.  */
7613       param_list = cp_parser_parameter_declaration_clause (parser);
7614
7615       /* Default arguments shall not be specified in the
7616          parameter-declaration-clause of a lambda-declarator.  */
7617       for (t = param_list; t; t = TREE_CHAIN (t))
7618         if (TREE_PURPOSE (t))
7619           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7620                    "default argument specified for lambda parameter");
7621
7622       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7623
7624       attributes = cp_parser_attributes_opt (parser);
7625
7626       /* Parse optional `mutable' keyword.  */
7627       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7628         {
7629           cp_lexer_consume_token (parser->lexer);
7630           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7631         }
7632
7633       /* Parse optional exception specification.  */
7634       exception_spec = cp_parser_exception_specification_opt (parser);
7635
7636       /* Parse optional trailing return type.  */
7637       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7638         {
7639           cp_lexer_consume_token (parser->lexer);
7640           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7641         }
7642
7643       /* The function parameters must be in scope all the way until after the
7644          trailing-return-type in case of decltype.  */
7645       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7646         pop_binding (DECL_NAME (t), t);
7647
7648       leave_scope ();
7649     }
7650
7651   /* Create the function call operator.
7652
7653      Messing with declarators like this is no uglier than building up the
7654      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7655      other code.  */
7656   {
7657     cp_decl_specifier_seq return_type_specs;
7658     cp_declarator* declarator;
7659     tree fco;
7660     int quals;
7661     void *p;
7662
7663     clear_decl_specs (&return_type_specs);
7664     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7665       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7666     else
7667       /* Maybe we will deduce the return type later, but we can use void
7668          as a placeholder return type anyways.  */
7669       return_type_specs.type = void_type_node;
7670
7671     p = obstack_alloc (&declarator_obstack, 0);
7672
7673     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7674                                      sfk_none);
7675
7676     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7677              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7678     declarator = make_call_declarator (declarator, param_list, quals,
7679                                        exception_spec,
7680                                        /*late_return_type=*/NULL_TREE);
7681     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7682
7683     fco = grokmethod (&return_type_specs,
7684                       declarator,
7685                       attributes);
7686     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7687     DECL_ARTIFICIAL (fco) = 1;
7688
7689     finish_member_declaration (fco);
7690
7691     obstack_free (&declarator_obstack, p);
7692   }
7693 }
7694
7695 /* Parse the body of a lambda expression, which is simply
7696
7697    compound-statement
7698
7699    but which requires special handling.
7700    LAMBDA_EXPR is the current representation of the lambda expression.  */
7701
7702 static void
7703 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7704 {
7705   bool nested = (current_function_decl != NULL_TREE);
7706   if (nested)
7707     push_function_context ();
7708
7709   /* Finish the function call operator
7710      - class_specifier
7711      + late_parsing_for_member
7712      + function_definition_after_declarator
7713      + ctor_initializer_opt_and_function_body  */
7714   {
7715     tree fco = lambda_function (lambda_expr);
7716     tree body;
7717     bool done = false;
7718
7719     /* Let the front end know that we are going to be defining this
7720        function.  */
7721     start_preparsed_function (fco,
7722                               NULL_TREE,
7723                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7724
7725     start_lambda_scope (fco);
7726     body = begin_function_body ();
7727
7728     /* 5.1.1.4 of the standard says:
7729          If a lambda-expression does not include a trailing-return-type, it
7730          is as if the trailing-return-type denotes the following type:
7731           * if the compound-statement is of the form
7732                { return attribute-specifier [opt] expression ; }
7733              the type of the returned expression after lvalue-to-rvalue
7734              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7735              (_conv.array_ 4.2), and function-to-pointer conversion
7736              (_conv.func_ 4.3);
7737           * otherwise, void.  */
7738
7739     /* In a lambda that has neither a lambda-return-type-clause
7740        nor a deducible form, errors should be reported for return statements
7741        in the body.  Since we used void as the placeholder return type, parsing
7742        the body as usual will give such desired behavior.  */
7743     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7744         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7745         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7746         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7747       {
7748         tree compound_stmt;
7749         tree expr = NULL_TREE;
7750         cp_id_kind idk = CP_ID_KIND_NONE;
7751
7752         /* Parse tentatively in case there's more after the initial return
7753            statement.  */
7754         cp_parser_parse_tentatively (parser);
7755
7756         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7757         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7758
7759         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7760
7761         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7762         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7763
7764         if (cp_parser_parse_definitely (parser))
7765           {
7766             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7767
7768             compound_stmt = begin_compound_stmt (0);
7769             /* Will get error here if type not deduced yet.  */
7770             finish_return_stmt (expr);
7771             finish_compound_stmt (compound_stmt);
7772
7773             done = true;
7774           }
7775       }
7776
7777     if (!done)
7778       {
7779         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7780           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7781         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7782            cp_parser_compound_stmt does not pass it.  */
7783         cp_parser_function_body (parser);
7784         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7785       }
7786
7787     finish_function_body (body);
7788     finish_lambda_scope ();
7789
7790     /* Finish the function and generate code for it if necessary.  */
7791     expand_or_defer_fn (finish_function (/*inline*/2));
7792   }
7793
7794   if (nested)
7795     pop_function_context();
7796 }
7797
7798 /* Statements [gram.stmt.stmt]  */
7799
7800 /* Parse a statement.
7801
7802    statement:
7803      labeled-statement
7804      expression-statement
7805      compound-statement
7806      selection-statement
7807      iteration-statement
7808      jump-statement
7809      declaration-statement
7810      try-block
7811
7812   IN_COMPOUND is true when the statement is nested inside a
7813   cp_parser_compound_statement; this matters for certain pragmas.
7814
7815   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7816   is a (possibly labeled) if statement which is not enclosed in braces
7817   and has an else clause.  This is used to implement -Wparentheses.  */
7818
7819 static void
7820 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7821                      bool in_compound, bool *if_p)
7822 {
7823   tree statement;
7824   cp_token *token;
7825   location_t statement_location;
7826
7827  restart:
7828   if (if_p != NULL)
7829     *if_p = false;
7830   /* There is no statement yet.  */
7831   statement = NULL_TREE;
7832   /* Peek at the next token.  */
7833   token = cp_lexer_peek_token (parser->lexer);
7834   /* Remember the location of the first token in the statement.  */
7835   statement_location = token->location;
7836   /* If this is a keyword, then that will often determine what kind of
7837      statement we have.  */
7838   if (token->type == CPP_KEYWORD)
7839     {
7840       enum rid keyword = token->keyword;
7841
7842       switch (keyword)
7843         {
7844         case RID_CASE:
7845         case RID_DEFAULT:
7846           /* Looks like a labeled-statement with a case label.
7847              Parse the label, and then use tail recursion to parse
7848              the statement.  */
7849           cp_parser_label_for_labeled_statement (parser);
7850           goto restart;
7851
7852         case RID_IF:
7853         case RID_SWITCH:
7854           statement = cp_parser_selection_statement (parser, if_p);
7855           break;
7856
7857         case RID_WHILE:
7858         case RID_DO:
7859         case RID_FOR:
7860           statement = cp_parser_iteration_statement (parser);
7861           break;
7862
7863         case RID_BREAK:
7864         case RID_CONTINUE:
7865         case RID_RETURN:
7866         case RID_GOTO:
7867           statement = cp_parser_jump_statement (parser);
7868           break;
7869
7870           /* Objective-C++ exception-handling constructs.  */
7871         case RID_AT_TRY:
7872         case RID_AT_CATCH:
7873         case RID_AT_FINALLY:
7874         case RID_AT_SYNCHRONIZED:
7875         case RID_AT_THROW:
7876           statement = cp_parser_objc_statement (parser);
7877           break;
7878
7879         case RID_TRY:
7880           statement = cp_parser_try_block (parser);
7881           break;
7882
7883         case RID_NAMESPACE:
7884           /* This must be a namespace alias definition.  */
7885           cp_parser_declaration_statement (parser);
7886           return;
7887           
7888         default:
7889           /* It might be a keyword like `int' that can start a
7890              declaration-statement.  */
7891           break;
7892         }
7893     }
7894   else if (token->type == CPP_NAME)
7895     {
7896       /* If the next token is a `:', then we are looking at a
7897          labeled-statement.  */
7898       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7899       if (token->type == CPP_COLON)
7900         {
7901           /* Looks like a labeled-statement with an ordinary label.
7902              Parse the label, and then use tail recursion to parse
7903              the statement.  */
7904           cp_parser_label_for_labeled_statement (parser);
7905           goto restart;
7906         }
7907     }
7908   /* Anything that starts with a `{' must be a compound-statement.  */
7909   else if (token->type == CPP_OPEN_BRACE)
7910     statement = cp_parser_compound_statement (parser, NULL, false);
7911   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7912      a statement all its own.  */
7913   else if (token->type == CPP_PRAGMA)
7914     {
7915       /* Only certain OpenMP pragmas are attached to statements, and thus
7916          are considered statements themselves.  All others are not.  In
7917          the context of a compound, accept the pragma as a "statement" and
7918          return so that we can check for a close brace.  Otherwise we
7919          require a real statement and must go back and read one.  */
7920       if (in_compound)
7921         cp_parser_pragma (parser, pragma_compound);
7922       else if (!cp_parser_pragma (parser, pragma_stmt))
7923         goto restart;
7924       return;
7925     }
7926   else if (token->type == CPP_EOF)
7927     {
7928       cp_parser_error (parser, "expected statement");
7929       return;
7930     }
7931
7932   /* Everything else must be a declaration-statement or an
7933      expression-statement.  Try for the declaration-statement
7934      first, unless we are looking at a `;', in which case we know that
7935      we have an expression-statement.  */
7936   if (!statement)
7937     {
7938       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7939         {
7940           cp_parser_parse_tentatively (parser);
7941           /* Try to parse the declaration-statement.  */
7942           cp_parser_declaration_statement (parser);
7943           /* If that worked, we're done.  */
7944           if (cp_parser_parse_definitely (parser))
7945             return;
7946         }
7947       /* Look for an expression-statement instead.  */
7948       statement = cp_parser_expression_statement (parser, in_statement_expr);
7949     }
7950
7951   /* Set the line number for the statement.  */
7952   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7953     SET_EXPR_LOCATION (statement, statement_location);
7954 }
7955
7956 /* Parse the label for a labeled-statement, i.e.
7957
7958    identifier :
7959    case constant-expression :
7960    default :
7961
7962    GNU Extension:
7963    case constant-expression ... constant-expression : statement
7964
7965    When a label is parsed without errors, the label is added to the
7966    parse tree by the finish_* functions, so this function doesn't
7967    have to return the label.  */
7968
7969 static void
7970 cp_parser_label_for_labeled_statement (cp_parser* parser)
7971 {
7972   cp_token *token;
7973   tree label = NULL_TREE;
7974
7975   /* The next token should be an identifier.  */
7976   token = cp_lexer_peek_token (parser->lexer);
7977   if (token->type != CPP_NAME
7978       && token->type != CPP_KEYWORD)
7979     {
7980       cp_parser_error (parser, "expected labeled-statement");
7981       return;
7982     }
7983
7984   switch (token->keyword)
7985     {
7986     case RID_CASE:
7987       {
7988         tree expr, expr_hi;
7989         cp_token *ellipsis;
7990
7991         /* Consume the `case' token.  */
7992         cp_lexer_consume_token (parser->lexer);
7993         /* Parse the constant-expression.  */
7994         expr = cp_parser_constant_expression (parser,
7995                                               /*allow_non_constant_p=*/false,
7996                                               NULL);
7997
7998         ellipsis = cp_lexer_peek_token (parser->lexer);
7999         if (ellipsis->type == CPP_ELLIPSIS)
8000           {
8001             /* Consume the `...' token.  */
8002             cp_lexer_consume_token (parser->lexer);
8003             expr_hi =
8004               cp_parser_constant_expression (parser,
8005                                              /*allow_non_constant_p=*/false,
8006                                              NULL);
8007             /* We don't need to emit warnings here, as the common code
8008                will do this for us.  */
8009           }
8010         else
8011           expr_hi = NULL_TREE;
8012
8013         if (parser->in_switch_statement_p)
8014           finish_case_label (token->location, expr, expr_hi);
8015         else
8016           error_at (token->location,
8017                     "case label %qE not within a switch statement",
8018                     expr);
8019       }
8020       break;
8021
8022     case RID_DEFAULT:
8023       /* Consume the `default' token.  */
8024       cp_lexer_consume_token (parser->lexer);
8025
8026       if (parser->in_switch_statement_p)
8027         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8028       else
8029         error_at (token->location, "case label not within a switch statement");
8030       break;
8031
8032     default:
8033       /* Anything else must be an ordinary label.  */
8034       label = finish_label_stmt (cp_parser_identifier (parser));
8035       break;
8036     }
8037
8038   /* Require the `:' token.  */
8039   cp_parser_require (parser, CPP_COLON, RT_COLON);
8040
8041   /* An ordinary label may optionally be followed by attributes.
8042      However, this is only permitted if the attributes are then
8043      followed by a semicolon.  This is because, for backward
8044      compatibility, when parsing
8045        lab: __attribute__ ((unused)) int i;
8046      we want the attribute to attach to "i", not "lab".  */
8047   if (label != NULL_TREE
8048       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8049     {
8050       tree attrs;
8051
8052       cp_parser_parse_tentatively (parser);
8053       attrs = cp_parser_attributes_opt (parser);
8054       if (attrs == NULL_TREE
8055           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8056         cp_parser_abort_tentative_parse (parser);
8057       else if (!cp_parser_parse_definitely (parser))
8058         ;
8059       else
8060         cplus_decl_attributes (&label, attrs, 0);
8061     }
8062 }
8063
8064 /* Parse an expression-statement.
8065
8066    expression-statement:
8067      expression [opt] ;
8068
8069    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8070    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8071    indicates whether this expression-statement is part of an
8072    expression statement.  */
8073
8074 static tree
8075 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8076 {
8077   tree statement = NULL_TREE;
8078   cp_token *token = cp_lexer_peek_token (parser->lexer);
8079
8080   /* If the next token is a ';', then there is no expression
8081      statement.  */
8082   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8083     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8084
8085   /* Give a helpful message for "A<T>::type t;" and the like.  */
8086   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8087       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8088     {
8089       if (TREE_CODE (statement) == SCOPE_REF)
8090         error_at (token->location, "need %<typename%> before %qE because "
8091                   "%qT is a dependent scope",
8092                   statement, TREE_OPERAND (statement, 0));
8093       else if (is_overloaded_fn (statement)
8094                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8095         {
8096           /* A::A a; */
8097           tree fn = get_first_fn (statement);
8098           error_at (token->location,
8099                     "%<%T::%D%> names the constructor, not the type",
8100                     DECL_CONTEXT (fn), DECL_NAME (fn));
8101         }
8102     }
8103
8104   /* Consume the final `;'.  */
8105   cp_parser_consume_semicolon_at_end_of_statement (parser);
8106
8107   if (in_statement_expr
8108       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8109     /* This is the final expression statement of a statement
8110        expression.  */
8111     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8112   else if (statement)
8113     statement = finish_expr_stmt (statement);
8114   else
8115     finish_stmt ();
8116
8117   return statement;
8118 }
8119
8120 /* Parse a compound-statement.
8121
8122    compound-statement:
8123      { statement-seq [opt] }
8124
8125    GNU extension:
8126
8127    compound-statement:
8128      { label-declaration-seq [opt] statement-seq [opt] }
8129
8130    label-declaration-seq:
8131      label-declaration
8132      label-declaration-seq label-declaration
8133
8134    Returns a tree representing the statement.  */
8135
8136 static tree
8137 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8138                               bool in_try)
8139 {
8140   tree compound_stmt;
8141
8142   /* Consume the `{'.  */
8143   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8144     return error_mark_node;
8145   /* Begin the compound-statement.  */
8146   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8147   /* If the next keyword is `__label__' we have a label declaration.  */
8148   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8149     cp_parser_label_declaration (parser);
8150   /* Parse an (optional) statement-seq.  */
8151   cp_parser_statement_seq_opt (parser, in_statement_expr);
8152   /* Finish the compound-statement.  */
8153   finish_compound_stmt (compound_stmt);
8154   /* Consume the `}'.  */
8155   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8156
8157   return compound_stmt;
8158 }
8159
8160 /* Parse an (optional) statement-seq.
8161
8162    statement-seq:
8163      statement
8164      statement-seq [opt] statement  */
8165
8166 static void
8167 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8168 {
8169   /* Scan statements until there aren't any more.  */
8170   while (true)
8171     {
8172       cp_token *token = cp_lexer_peek_token (parser->lexer);
8173
8174       /* If we're looking at a `}', then we've run out of statements.  */
8175       if (token->type == CPP_CLOSE_BRACE
8176           || token->type == CPP_EOF
8177           || token->type == CPP_PRAGMA_EOL)
8178         break;
8179       
8180       /* If we are in a compound statement and find 'else' then
8181          something went wrong.  */
8182       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8183         {
8184           if (parser->in_statement & IN_IF_STMT) 
8185             break;
8186           else
8187             {
8188               token = cp_lexer_consume_token (parser->lexer);
8189               error_at (token->location, "%<else%> without a previous %<if%>");
8190             }
8191         }
8192
8193       /* Parse the statement.  */
8194       cp_parser_statement (parser, in_statement_expr, true, NULL);
8195     }
8196 }
8197
8198 /* Parse a selection-statement.
8199
8200    selection-statement:
8201      if ( condition ) statement
8202      if ( condition ) statement else statement
8203      switch ( condition ) statement
8204
8205    Returns the new IF_STMT or SWITCH_STMT.
8206
8207    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8208    is a (possibly labeled) if statement which is not enclosed in
8209    braces and has an else clause.  This is used to implement
8210    -Wparentheses.  */
8211
8212 static tree
8213 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8214 {
8215   cp_token *token;
8216   enum rid keyword;
8217
8218   if (if_p != NULL)
8219     *if_p = false;
8220
8221   /* Peek at the next token.  */
8222   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8223
8224   /* See what kind of keyword it is.  */
8225   keyword = token->keyword;
8226   switch (keyword)
8227     {
8228     case RID_IF:
8229     case RID_SWITCH:
8230       {
8231         tree statement;
8232         tree condition;
8233
8234         /* Look for the `('.  */
8235         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8236           {
8237             cp_parser_skip_to_end_of_statement (parser);
8238             return error_mark_node;
8239           }
8240
8241         /* Begin the selection-statement.  */
8242         if (keyword == RID_IF)
8243           statement = begin_if_stmt ();
8244         else
8245           statement = begin_switch_stmt ();
8246
8247         /* Parse the condition.  */
8248         condition = cp_parser_condition (parser);
8249         /* Look for the `)'.  */
8250         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8251           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8252                                                  /*consume_paren=*/true);
8253
8254         if (keyword == RID_IF)
8255           {
8256             bool nested_if;
8257             unsigned char in_statement;
8258
8259             /* Add the condition.  */
8260             finish_if_stmt_cond (condition, statement);
8261
8262             /* Parse the then-clause.  */
8263             in_statement = parser->in_statement;
8264             parser->in_statement |= IN_IF_STMT;
8265             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8266               {
8267                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8268                 add_stmt (build_empty_stmt (loc));
8269                 cp_lexer_consume_token (parser->lexer);
8270                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8271                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8272                               "empty body in an %<if%> statement");
8273                 nested_if = false;
8274               }
8275             else
8276               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8277             parser->in_statement = in_statement;
8278
8279             finish_then_clause (statement);
8280
8281             /* If the next token is `else', parse the else-clause.  */
8282             if (cp_lexer_next_token_is_keyword (parser->lexer,
8283                                                 RID_ELSE))
8284               {
8285                 /* Consume the `else' keyword.  */
8286                 cp_lexer_consume_token (parser->lexer);
8287                 begin_else_clause (statement);
8288                 /* Parse the else-clause.  */
8289                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8290                   {
8291                     location_t loc;
8292                     loc = cp_lexer_peek_token (parser->lexer)->location;
8293                     warning_at (loc,
8294                                 OPT_Wempty_body, "suggest braces around "
8295                                 "empty body in an %<else%> statement");
8296                     add_stmt (build_empty_stmt (loc));
8297                     cp_lexer_consume_token (parser->lexer);
8298                   }
8299                 else
8300                   cp_parser_implicitly_scoped_statement (parser, NULL);
8301
8302                 finish_else_clause (statement);
8303
8304                 /* If we are currently parsing a then-clause, then
8305                    IF_P will not be NULL.  We set it to true to
8306                    indicate that this if statement has an else clause.
8307                    This may trigger the Wparentheses warning below
8308                    when we get back up to the parent if statement.  */
8309                 if (if_p != NULL)
8310                   *if_p = true;
8311               }
8312             else
8313               {
8314                 /* This if statement does not have an else clause.  If
8315                    NESTED_IF is true, then the then-clause is an if
8316                    statement which does have an else clause.  We warn
8317                    about the potential ambiguity.  */
8318                 if (nested_if)
8319                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8320                               "suggest explicit braces to avoid ambiguous"
8321                               " %<else%>");
8322               }
8323
8324             /* Now we're all done with the if-statement.  */
8325             finish_if_stmt (statement);
8326           }
8327         else
8328           {
8329             bool in_switch_statement_p;
8330             unsigned char in_statement;
8331
8332             /* Add the condition.  */
8333             finish_switch_cond (condition, statement);
8334
8335             /* Parse the body of the switch-statement.  */
8336             in_switch_statement_p = parser->in_switch_statement_p;
8337             in_statement = parser->in_statement;
8338             parser->in_switch_statement_p = true;
8339             parser->in_statement |= IN_SWITCH_STMT;
8340             cp_parser_implicitly_scoped_statement (parser, NULL);
8341             parser->in_switch_statement_p = in_switch_statement_p;
8342             parser->in_statement = in_statement;
8343
8344             /* Now we're all done with the switch-statement.  */
8345             finish_switch_stmt (statement);
8346           }
8347
8348         return statement;
8349       }
8350       break;
8351
8352     default:
8353       cp_parser_error (parser, "expected selection-statement");
8354       return error_mark_node;
8355     }
8356 }
8357
8358 /* Parse a condition.
8359
8360    condition:
8361      expression
8362      type-specifier-seq declarator = initializer-clause
8363      type-specifier-seq declarator braced-init-list
8364
8365    GNU Extension:
8366
8367    condition:
8368      type-specifier-seq declarator asm-specification [opt]
8369        attributes [opt] = assignment-expression
8370
8371    Returns the expression that should be tested.  */
8372
8373 static tree
8374 cp_parser_condition (cp_parser* parser)
8375 {
8376   cp_decl_specifier_seq type_specifiers;
8377   const char *saved_message;
8378
8379   /* Try the declaration first.  */
8380   cp_parser_parse_tentatively (parser);
8381   /* New types are not allowed in the type-specifier-seq for a
8382      condition.  */
8383   saved_message = parser->type_definition_forbidden_message;
8384   parser->type_definition_forbidden_message
8385     = G_("types may not be defined in conditions");
8386   /* Parse the type-specifier-seq.  */
8387   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8388                                 /*is_trailing_return=*/false,
8389                                 &type_specifiers);
8390   /* Restore the saved message.  */
8391   parser->type_definition_forbidden_message = saved_message;
8392   /* If all is well, we might be looking at a declaration.  */
8393   if (!cp_parser_error_occurred (parser))
8394     {
8395       tree decl;
8396       tree asm_specification;
8397       tree attributes;
8398       cp_declarator *declarator;
8399       tree initializer = NULL_TREE;
8400
8401       /* Parse the declarator.  */
8402       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8403                                          /*ctor_dtor_or_conv_p=*/NULL,
8404                                          /*parenthesized_p=*/NULL,
8405                                          /*member_p=*/false);
8406       /* Parse the attributes.  */
8407       attributes = cp_parser_attributes_opt (parser);
8408       /* Parse the asm-specification.  */
8409       asm_specification = cp_parser_asm_specification_opt (parser);
8410       /* If the next token is not an `=' or '{', then we might still be
8411          looking at an expression.  For example:
8412
8413            if (A(a).x)
8414
8415          looks like a decl-specifier-seq and a declarator -- but then
8416          there is no `=', so this is an expression.  */
8417       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8418           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8419         cp_parser_simulate_error (parser);
8420         
8421       /* If we did see an `=' or '{', then we are looking at a declaration
8422          for sure.  */
8423       if (cp_parser_parse_definitely (parser))
8424         {
8425           tree pushed_scope;
8426           bool non_constant_p;
8427           bool flags = LOOKUP_ONLYCONVERTING;
8428
8429           /* Create the declaration.  */
8430           decl = start_decl (declarator, &type_specifiers,
8431                              /*initialized_p=*/true,
8432                              attributes, /*prefix_attributes=*/NULL_TREE,
8433                              &pushed_scope);
8434
8435           /* Parse the initializer.  */
8436           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8437             {
8438               initializer = cp_parser_braced_list (parser, &non_constant_p);
8439               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8440               flags = 0;
8441             }
8442           else
8443             {
8444               /* Consume the `='.  */
8445               cp_parser_require (parser, CPP_EQ, RT_EQ);
8446               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8447             }
8448           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8449             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8450
8451           if (!non_constant_p)
8452             initializer = fold_non_dependent_expr (initializer);
8453
8454           /* Process the initializer.  */
8455           cp_finish_decl (decl,
8456                           initializer, !non_constant_p,
8457                           asm_specification,
8458                           flags);
8459
8460           if (pushed_scope)
8461             pop_scope (pushed_scope);
8462
8463           return convert_from_reference (decl);
8464         }
8465     }
8466   /* If we didn't even get past the declarator successfully, we are
8467      definitely not looking at a declaration.  */
8468   else
8469     cp_parser_abort_tentative_parse (parser);
8470
8471   /* Otherwise, we are looking at an expression.  */
8472   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8473 }
8474
8475 /* Parse an iteration-statement.
8476
8477    iteration-statement:
8478      while ( condition ) statement
8479      do statement while ( expression ) ;
8480      for ( for-init-statement condition [opt] ; expression [opt] )
8481        statement
8482
8483    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8484
8485 static tree
8486 cp_parser_iteration_statement (cp_parser* parser)
8487 {
8488   cp_token *token;
8489   enum rid keyword;
8490   tree statement;
8491   unsigned char in_statement;
8492
8493   /* Peek at the next token.  */
8494   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8495   if (!token)
8496     return error_mark_node;
8497
8498   /* Remember whether or not we are already within an iteration
8499      statement.  */
8500   in_statement = parser->in_statement;
8501
8502   /* See what kind of keyword it is.  */
8503   keyword = token->keyword;
8504   switch (keyword)
8505     {
8506     case RID_WHILE:
8507       {
8508         tree condition;
8509
8510         /* Begin the while-statement.  */
8511         statement = begin_while_stmt ();
8512         /* Look for the `('.  */
8513         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8514         /* Parse the condition.  */
8515         condition = cp_parser_condition (parser);
8516         finish_while_stmt_cond (condition, statement);
8517         /* Look for the `)'.  */
8518         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8519         /* Parse the dependent statement.  */
8520         parser->in_statement = IN_ITERATION_STMT;
8521         cp_parser_already_scoped_statement (parser);
8522         parser->in_statement = in_statement;
8523         /* We're done with the while-statement.  */
8524         finish_while_stmt (statement);
8525       }
8526       break;
8527
8528     case RID_DO:
8529       {
8530         tree expression;
8531
8532         /* Begin the do-statement.  */
8533         statement = begin_do_stmt ();
8534         /* Parse the body of the do-statement.  */
8535         parser->in_statement = IN_ITERATION_STMT;
8536         cp_parser_implicitly_scoped_statement (parser, NULL);
8537         parser->in_statement = in_statement;
8538         finish_do_body (statement);
8539         /* Look for the `while' keyword.  */
8540         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8541         /* Look for the `('.  */
8542         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8543         /* Parse the expression.  */
8544         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8545         /* We're done with the do-statement.  */
8546         finish_do_stmt (expression, statement);
8547         /* Look for the `)'.  */
8548         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8549         /* Look for the `;'.  */
8550         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8551       }
8552       break;
8553
8554     case RID_FOR:
8555       {
8556         tree condition = NULL_TREE;
8557         tree expression = NULL_TREE;
8558
8559         /* Begin the for-statement.  */
8560         statement = begin_for_stmt ();
8561         /* Look for the `('.  */
8562         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8563         /* Parse the initialization.  */
8564         cp_parser_for_init_statement (parser);
8565         finish_for_init_stmt (statement);
8566
8567         /* If there's a condition, process it.  */
8568         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8569           condition = cp_parser_condition (parser);
8570         finish_for_cond (condition, statement);
8571         /* Look for the `;'.  */
8572         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8573
8574         /* If there's an expression, process it.  */
8575         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8576           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8577         finish_for_expr (expression, statement);
8578         /* Look for the `)'.  */
8579         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8580
8581         /* Parse the body of the for-statement.  */
8582         parser->in_statement = IN_ITERATION_STMT;
8583         cp_parser_already_scoped_statement (parser);
8584         parser->in_statement = in_statement;
8585
8586         /* We're done with the for-statement.  */
8587         finish_for_stmt (statement);
8588       }
8589       break;
8590
8591     default:
8592       cp_parser_error (parser, "expected iteration-statement");
8593       statement = error_mark_node;
8594       break;
8595     }
8596
8597   return statement;
8598 }
8599
8600 /* Parse a for-init-statement.
8601
8602    for-init-statement:
8603      expression-statement
8604      simple-declaration  */
8605
8606 static void
8607 cp_parser_for_init_statement (cp_parser* parser)
8608 {
8609   /* If the next token is a `;', then we have an empty
8610      expression-statement.  Grammatically, this is also a
8611      simple-declaration, but an invalid one, because it does not
8612      declare anything.  Therefore, if we did not handle this case
8613      specially, we would issue an error message about an invalid
8614      declaration.  */
8615   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8616     {
8617       /* We're going to speculatively look for a declaration, falling back
8618          to an expression, if necessary.  */
8619       cp_parser_parse_tentatively (parser);
8620       /* Parse the declaration.  */
8621       cp_parser_simple_declaration (parser,
8622                                     /*function_definition_allowed_p=*/false);
8623       /* If the tentative parse failed, then we shall need to look for an
8624          expression-statement.  */
8625       if (cp_parser_parse_definitely (parser))
8626         return;
8627     }
8628
8629   cp_parser_expression_statement (parser, NULL_TREE);
8630 }
8631
8632 /* Parse a jump-statement.
8633
8634    jump-statement:
8635      break ;
8636      continue ;
8637      return expression [opt] ;
8638      return braced-init-list ;
8639      goto identifier ;
8640
8641    GNU extension:
8642
8643    jump-statement:
8644      goto * expression ;
8645
8646    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8647
8648 static tree
8649 cp_parser_jump_statement (cp_parser* parser)
8650 {
8651   tree statement = error_mark_node;
8652   cp_token *token;
8653   enum rid keyword;
8654   unsigned char in_statement;
8655
8656   /* Peek at the next token.  */
8657   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
8658   if (!token)
8659     return error_mark_node;
8660
8661   /* See what kind of keyword it is.  */
8662   keyword = token->keyword;
8663   switch (keyword)
8664     {
8665     case RID_BREAK:
8666       in_statement = parser->in_statement & ~IN_IF_STMT;      
8667       switch (in_statement)
8668         {
8669         case 0:
8670           error_at (token->location, "break statement not within loop or switch");
8671           break;
8672         default:
8673           gcc_assert ((in_statement & IN_SWITCH_STMT)
8674                       || in_statement == IN_ITERATION_STMT);
8675           statement = finish_break_stmt ();
8676           break;
8677         case IN_OMP_BLOCK:
8678           error_at (token->location, "invalid exit from OpenMP structured block");
8679           break;
8680         case IN_OMP_FOR:
8681           error_at (token->location, "break statement used with OpenMP for loop");
8682           break;
8683         }
8684       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8685       break;
8686
8687     case RID_CONTINUE:
8688       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8689         {
8690         case 0:
8691           error_at (token->location, "continue statement not within a loop");
8692           break;
8693         case IN_ITERATION_STMT:
8694         case IN_OMP_FOR:
8695           statement = finish_continue_stmt ();
8696           break;
8697         case IN_OMP_BLOCK:
8698           error_at (token->location, "invalid exit from OpenMP structured block");
8699           break;
8700         default:
8701           gcc_unreachable ();
8702         }
8703       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8704       break;
8705
8706     case RID_RETURN:
8707       {
8708         tree expr;
8709         bool expr_non_constant_p;
8710
8711         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8712           {
8713             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8714             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8715           }
8716         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8717           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8718         else
8719           /* If the next token is a `;', then there is no
8720              expression.  */
8721           expr = NULL_TREE;
8722         /* Build the return-statement.  */
8723         statement = finish_return_stmt (expr);
8724         /* Look for the final `;'.  */
8725         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8726       }
8727       break;
8728
8729     case RID_GOTO:
8730       /* Create the goto-statement.  */
8731       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8732         {
8733           /* Issue a warning about this use of a GNU extension.  */
8734           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8735           /* Consume the '*' token.  */
8736           cp_lexer_consume_token (parser->lexer);
8737           /* Parse the dependent expression.  */
8738           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8739         }
8740       else
8741         finish_goto_stmt (cp_parser_identifier (parser));
8742       /* Look for the final `;'.  */
8743       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8744       break;
8745
8746     default:
8747       cp_parser_error (parser, "expected jump-statement");
8748       break;
8749     }
8750
8751   return statement;
8752 }
8753
8754 /* Parse a declaration-statement.
8755
8756    declaration-statement:
8757      block-declaration  */
8758
8759 static void
8760 cp_parser_declaration_statement (cp_parser* parser)
8761 {
8762   void *p;
8763
8764   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8765   p = obstack_alloc (&declarator_obstack, 0);
8766
8767  /* Parse the block-declaration.  */
8768   cp_parser_block_declaration (parser, /*statement_p=*/true);
8769
8770   /* Free any declarators allocated.  */
8771   obstack_free (&declarator_obstack, p);
8772
8773   /* Finish off the statement.  */
8774   finish_stmt ();
8775 }
8776
8777 /* Some dependent statements (like `if (cond) statement'), are
8778    implicitly in their own scope.  In other words, if the statement is
8779    a single statement (as opposed to a compound-statement), it is
8780    none-the-less treated as if it were enclosed in braces.  Any
8781    declarations appearing in the dependent statement are out of scope
8782    after control passes that point.  This function parses a statement,
8783    but ensures that is in its own scope, even if it is not a
8784    compound-statement.
8785
8786    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8787    is a (possibly labeled) if statement which is not enclosed in
8788    braces and has an else clause.  This is used to implement
8789    -Wparentheses.
8790
8791    Returns the new statement.  */
8792
8793 static tree
8794 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8795 {
8796   tree statement;
8797
8798   if (if_p != NULL)
8799     *if_p = false;
8800
8801   /* Mark if () ; with a special NOP_EXPR.  */
8802   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8803     {
8804       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8805       cp_lexer_consume_token (parser->lexer);
8806       statement = add_stmt (build_empty_stmt (loc));
8807     }
8808   /* if a compound is opened, we simply parse the statement directly.  */
8809   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8810     statement = cp_parser_compound_statement (parser, NULL, false);
8811   /* If the token is not a `{', then we must take special action.  */
8812   else
8813     {
8814       /* Create a compound-statement.  */
8815       statement = begin_compound_stmt (0);
8816       /* Parse the dependent-statement.  */
8817       cp_parser_statement (parser, NULL_TREE, false, if_p);
8818       /* Finish the dummy compound-statement.  */
8819       finish_compound_stmt (statement);
8820     }
8821
8822   /* Return the statement.  */
8823   return statement;
8824 }
8825
8826 /* For some dependent statements (like `while (cond) statement'), we
8827    have already created a scope.  Therefore, even if the dependent
8828    statement is a compound-statement, we do not want to create another
8829    scope.  */
8830
8831 static void
8832 cp_parser_already_scoped_statement (cp_parser* parser)
8833 {
8834   /* If the token is a `{', then we must take special action.  */
8835   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8836     cp_parser_statement (parser, NULL_TREE, false, NULL);
8837   else
8838     {
8839       /* Avoid calling cp_parser_compound_statement, so that we
8840          don't create a new scope.  Do everything else by hand.  */
8841       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
8842       /* If the next keyword is `__label__' we have a label declaration.  */
8843       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8844         cp_parser_label_declaration (parser);
8845       /* Parse an (optional) statement-seq.  */
8846       cp_parser_statement_seq_opt (parser, NULL_TREE);
8847       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8848     }
8849 }
8850
8851 /* Declarations [gram.dcl.dcl] */
8852
8853 /* Parse an optional declaration-sequence.
8854
8855    declaration-seq:
8856      declaration
8857      declaration-seq declaration  */
8858
8859 static void
8860 cp_parser_declaration_seq_opt (cp_parser* parser)
8861 {
8862   while (true)
8863     {
8864       cp_token *token;
8865
8866       token = cp_lexer_peek_token (parser->lexer);
8867
8868       if (token->type == CPP_CLOSE_BRACE
8869           || token->type == CPP_EOF
8870           || token->type == CPP_PRAGMA_EOL)
8871         break;
8872
8873       if (token->type == CPP_SEMICOLON)
8874         {
8875           /* A declaration consisting of a single semicolon is
8876              invalid.  Allow it unless we're being pedantic.  */
8877           cp_lexer_consume_token (parser->lexer);
8878           if (!in_system_header)
8879             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8880           continue;
8881         }
8882
8883       /* If we're entering or exiting a region that's implicitly
8884          extern "C", modify the lang context appropriately.  */
8885       if (!parser->implicit_extern_c && token->implicit_extern_c)
8886         {
8887           push_lang_context (lang_name_c);
8888           parser->implicit_extern_c = true;
8889         }
8890       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8891         {
8892           pop_lang_context ();
8893           parser->implicit_extern_c = false;
8894         }
8895
8896       if (token->type == CPP_PRAGMA)
8897         {
8898           /* A top-level declaration can consist solely of a #pragma.
8899              A nested declaration cannot, so this is done here and not
8900              in cp_parser_declaration.  (A #pragma at block scope is
8901              handled in cp_parser_statement.)  */
8902           cp_parser_pragma (parser, pragma_external);
8903           continue;
8904         }
8905
8906       /* Parse the declaration itself.  */
8907       cp_parser_declaration (parser);
8908     }
8909 }
8910
8911 /* Parse a declaration.
8912
8913    declaration:
8914      block-declaration
8915      function-definition
8916      template-declaration
8917      explicit-instantiation
8918      explicit-specialization
8919      linkage-specification
8920      namespace-definition
8921
8922    GNU extension:
8923
8924    declaration:
8925       __extension__ declaration */
8926
8927 static void
8928 cp_parser_declaration (cp_parser* parser)
8929 {
8930   cp_token token1;
8931   cp_token token2;
8932   int saved_pedantic;
8933   void *p;
8934
8935   /* Check for the `__extension__' keyword.  */
8936   if (cp_parser_extension_opt (parser, &saved_pedantic))
8937     {
8938       /* Parse the qualified declaration.  */
8939       cp_parser_declaration (parser);
8940       /* Restore the PEDANTIC flag.  */
8941       pedantic = saved_pedantic;
8942
8943       return;
8944     }
8945
8946   /* Try to figure out what kind of declaration is present.  */
8947   token1 = *cp_lexer_peek_token (parser->lexer);
8948
8949   if (token1.type != CPP_EOF)
8950     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8951   else
8952     {
8953       token2.type = CPP_EOF;
8954       token2.keyword = RID_MAX;
8955     }
8956
8957   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8958   p = obstack_alloc (&declarator_obstack, 0);
8959
8960   /* If the next token is `extern' and the following token is a string
8961      literal, then we have a linkage specification.  */
8962   if (token1.keyword == RID_EXTERN
8963       && cp_parser_is_string_literal (&token2))
8964     cp_parser_linkage_specification (parser);
8965   /* If the next token is `template', then we have either a template
8966      declaration, an explicit instantiation, or an explicit
8967      specialization.  */
8968   else if (token1.keyword == RID_TEMPLATE)
8969     {
8970       /* `template <>' indicates a template specialization.  */
8971       if (token2.type == CPP_LESS
8972           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8973         cp_parser_explicit_specialization (parser);
8974       /* `template <' indicates a template declaration.  */
8975       else if (token2.type == CPP_LESS)
8976         cp_parser_template_declaration (parser, /*member_p=*/false);
8977       /* Anything else must be an explicit instantiation.  */
8978       else
8979         cp_parser_explicit_instantiation (parser);
8980     }
8981   /* If the next token is `export', then we have a template
8982      declaration.  */
8983   else if (token1.keyword == RID_EXPORT)
8984     cp_parser_template_declaration (parser, /*member_p=*/false);
8985   /* If the next token is `extern', 'static' or 'inline' and the one
8986      after that is `template', we have a GNU extended explicit
8987      instantiation directive.  */
8988   else if (cp_parser_allow_gnu_extensions_p (parser)
8989            && (token1.keyword == RID_EXTERN
8990                || token1.keyword == RID_STATIC
8991                || token1.keyword == RID_INLINE)
8992            && token2.keyword == RID_TEMPLATE)
8993     cp_parser_explicit_instantiation (parser);
8994   /* If the next token is `namespace', check for a named or unnamed
8995      namespace definition.  */
8996   else if (token1.keyword == RID_NAMESPACE
8997            && (/* A named namespace definition.  */
8998                (token2.type == CPP_NAME
8999                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9000                     != CPP_EQ))
9001                /* An unnamed namespace definition.  */
9002                || token2.type == CPP_OPEN_BRACE
9003                || token2.keyword == RID_ATTRIBUTE))
9004     cp_parser_namespace_definition (parser);
9005   /* An inline (associated) namespace definition.  */
9006   else if (token1.keyword == RID_INLINE
9007            && token2.keyword == RID_NAMESPACE)
9008     cp_parser_namespace_definition (parser);
9009   /* Objective-C++ declaration/definition.  */
9010   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9011     cp_parser_objc_declaration (parser);
9012   /* We must have either a block declaration or a function
9013      definition.  */
9014   else
9015     /* Try to parse a block-declaration, or a function-definition.  */
9016     cp_parser_block_declaration (parser, /*statement_p=*/false);
9017
9018   /* Free any declarators allocated.  */
9019   obstack_free (&declarator_obstack, p);
9020 }
9021
9022 /* Parse a block-declaration.
9023
9024    block-declaration:
9025      simple-declaration
9026      asm-definition
9027      namespace-alias-definition
9028      using-declaration
9029      using-directive
9030
9031    GNU Extension:
9032
9033    block-declaration:
9034      __extension__ block-declaration
9035
9036    C++0x Extension:
9037
9038    block-declaration:
9039      static_assert-declaration
9040
9041    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9042    part of a declaration-statement.  */
9043
9044 static void
9045 cp_parser_block_declaration (cp_parser *parser,
9046                              bool      statement_p)
9047 {
9048   cp_token *token1;
9049   int saved_pedantic;
9050
9051   /* Check for the `__extension__' keyword.  */
9052   if (cp_parser_extension_opt (parser, &saved_pedantic))
9053     {
9054       /* Parse the qualified declaration.  */
9055       cp_parser_block_declaration (parser, statement_p);
9056       /* Restore the PEDANTIC flag.  */
9057       pedantic = saved_pedantic;
9058
9059       return;
9060     }
9061
9062   /* Peek at the next token to figure out which kind of declaration is
9063      present.  */
9064   token1 = cp_lexer_peek_token (parser->lexer);
9065
9066   /* If the next keyword is `asm', we have an asm-definition.  */
9067   if (token1->keyword == RID_ASM)
9068     {
9069       if (statement_p)
9070         cp_parser_commit_to_tentative_parse (parser);
9071       cp_parser_asm_definition (parser);
9072     }
9073   /* If the next keyword is `namespace', we have a
9074      namespace-alias-definition.  */
9075   else if (token1->keyword == RID_NAMESPACE)
9076     cp_parser_namespace_alias_definition (parser);
9077   /* If the next keyword is `using', we have either a
9078      using-declaration or a using-directive.  */
9079   else if (token1->keyword == RID_USING)
9080     {
9081       cp_token *token2;
9082
9083       if (statement_p)
9084         cp_parser_commit_to_tentative_parse (parser);
9085       /* If the token after `using' is `namespace', then we have a
9086          using-directive.  */
9087       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9088       if (token2->keyword == RID_NAMESPACE)
9089         cp_parser_using_directive (parser);
9090       /* Otherwise, it's a using-declaration.  */
9091       else
9092         cp_parser_using_declaration (parser,
9093                                      /*access_declaration_p=*/false);
9094     }
9095   /* If the next keyword is `__label__' we have a misplaced label
9096      declaration.  */
9097   else if (token1->keyword == RID_LABEL)
9098     {
9099       cp_lexer_consume_token (parser->lexer);
9100       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9101       cp_parser_skip_to_end_of_statement (parser);
9102       /* If the next token is now a `;', consume it.  */
9103       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9104         cp_lexer_consume_token (parser->lexer);
9105     }
9106   /* If the next token is `static_assert' we have a static assertion.  */
9107   else if (token1->keyword == RID_STATIC_ASSERT)
9108     cp_parser_static_assert (parser, /*member_p=*/false);
9109   /* Anything else must be a simple-declaration.  */
9110   else
9111     cp_parser_simple_declaration (parser, !statement_p);
9112 }
9113
9114 /* Parse a simple-declaration.
9115
9116    simple-declaration:
9117      decl-specifier-seq [opt] init-declarator-list [opt] ;
9118
9119    init-declarator-list:
9120      init-declarator
9121      init-declarator-list , init-declarator
9122
9123    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9124    function-definition as a simple-declaration.  */
9125
9126 static void
9127 cp_parser_simple_declaration (cp_parser* parser,
9128                               bool function_definition_allowed_p)
9129 {
9130   cp_decl_specifier_seq decl_specifiers;
9131   int declares_class_or_enum;
9132   bool saw_declarator;
9133
9134   /* Defer access checks until we know what is being declared; the
9135      checks for names appearing in the decl-specifier-seq should be
9136      done as if we were in the scope of the thing being declared.  */
9137   push_deferring_access_checks (dk_deferred);
9138
9139   /* Parse the decl-specifier-seq.  We have to keep track of whether
9140      or not the decl-specifier-seq declares a named class or
9141      enumeration type, since that is the only case in which the
9142      init-declarator-list is allowed to be empty.
9143
9144      [dcl.dcl]
9145
9146      In a simple-declaration, the optional init-declarator-list can be
9147      omitted only when declaring a class or enumeration, that is when
9148      the decl-specifier-seq contains either a class-specifier, an
9149      elaborated-type-specifier, or an enum-specifier.  */
9150   cp_parser_decl_specifier_seq (parser,
9151                                 CP_PARSER_FLAGS_OPTIONAL,
9152                                 &decl_specifiers,
9153                                 &declares_class_or_enum);
9154   /* We no longer need to defer access checks.  */
9155   stop_deferring_access_checks ();
9156
9157   /* In a block scope, a valid declaration must always have a
9158      decl-specifier-seq.  By not trying to parse declarators, we can
9159      resolve the declaration/expression ambiguity more quickly.  */
9160   if (!function_definition_allowed_p
9161       && !decl_specifiers.any_specifiers_p)
9162     {
9163       cp_parser_error (parser, "expected declaration");
9164       goto done;
9165     }
9166
9167   /* If the next two tokens are both identifiers, the code is
9168      erroneous. The usual cause of this situation is code like:
9169
9170        T t;
9171
9172      where "T" should name a type -- but does not.  */
9173   if (!decl_specifiers.any_type_specifiers_p
9174       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9175     {
9176       /* If parsing tentatively, we should commit; we really are
9177          looking at a declaration.  */
9178       cp_parser_commit_to_tentative_parse (parser);
9179       /* Give up.  */
9180       goto done;
9181     }
9182
9183   /* If we have seen at least one decl-specifier, and the next token
9184      is not a parenthesis, then we must be looking at a declaration.
9185      (After "int (" we might be looking at a functional cast.)  */
9186   if (decl_specifiers.any_specifiers_p
9187       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9188       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9189       && !cp_parser_error_occurred (parser))
9190     cp_parser_commit_to_tentative_parse (parser);
9191
9192   /* Keep going until we hit the `;' at the end of the simple
9193      declaration.  */
9194   saw_declarator = false;
9195   while (cp_lexer_next_token_is_not (parser->lexer,
9196                                      CPP_SEMICOLON))
9197     {
9198       cp_token *token;
9199       bool function_definition_p;
9200       tree decl;
9201
9202       if (saw_declarator)
9203         {
9204           /* If we are processing next declarator, coma is expected */
9205           token = cp_lexer_peek_token (parser->lexer);
9206           gcc_assert (token->type == CPP_COMMA);
9207           cp_lexer_consume_token (parser->lexer);
9208         }
9209       else
9210         saw_declarator = true;
9211
9212       /* Parse the init-declarator.  */
9213       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9214                                         /*checks=*/NULL,
9215                                         function_definition_allowed_p,
9216                                         /*member_p=*/false,
9217                                         declares_class_or_enum,
9218                                         &function_definition_p);
9219       /* If an error occurred while parsing tentatively, exit quickly.
9220          (That usually happens when in the body of a function; each
9221          statement is treated as a declaration-statement until proven
9222          otherwise.)  */
9223       if (cp_parser_error_occurred (parser))
9224         goto done;
9225       /* Handle function definitions specially.  */
9226       if (function_definition_p)
9227         {
9228           /* If the next token is a `,', then we are probably
9229              processing something like:
9230
9231                void f() {}, *p;
9232
9233              which is erroneous.  */
9234           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9235             {
9236               cp_token *token = cp_lexer_peek_token (parser->lexer);
9237               error_at (token->location,
9238                         "mixing"
9239                         " declarations and function-definitions is forbidden");
9240             }
9241           /* Otherwise, we're done with the list of declarators.  */
9242           else
9243             {
9244               pop_deferring_access_checks ();
9245               return;
9246             }
9247         }
9248       /* The next token should be either a `,' or a `;'.  */
9249       token = cp_lexer_peek_token (parser->lexer);
9250       /* If it's a `,', there are more declarators to come.  */
9251       if (token->type == CPP_COMMA)
9252         /* will be consumed next time around */;
9253       /* If it's a `;', we are done.  */
9254       else if (token->type == CPP_SEMICOLON)
9255         break;
9256       /* Anything else is an error.  */
9257       else
9258         {
9259           /* If we have already issued an error message we don't need
9260              to issue another one.  */
9261           if (decl != error_mark_node
9262               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9263             cp_parser_error (parser, "expected %<,%> or %<;%>");
9264           /* Skip tokens until we reach the end of the statement.  */
9265           cp_parser_skip_to_end_of_statement (parser);
9266           /* If the next token is now a `;', consume it.  */
9267           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9268             cp_lexer_consume_token (parser->lexer);
9269           goto done;
9270         }
9271       /* After the first time around, a function-definition is not
9272          allowed -- even if it was OK at first.  For example:
9273
9274            int i, f() {}
9275
9276          is not valid.  */
9277       function_definition_allowed_p = false;
9278     }
9279
9280   /* Issue an error message if no declarators are present, and the
9281      decl-specifier-seq does not itself declare a class or
9282      enumeration.  */
9283   if (!saw_declarator)
9284     {
9285       if (cp_parser_declares_only_class_p (parser))
9286         shadow_tag (&decl_specifiers);
9287       /* Perform any deferred access checks.  */
9288       perform_deferred_access_checks ();
9289     }
9290
9291   /* Consume the `;'.  */
9292   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9293
9294  done:
9295   pop_deferring_access_checks ();
9296 }
9297
9298 /* Parse a decl-specifier-seq.
9299
9300    decl-specifier-seq:
9301      decl-specifier-seq [opt] decl-specifier
9302
9303    decl-specifier:
9304      storage-class-specifier
9305      type-specifier
9306      function-specifier
9307      friend
9308      typedef
9309
9310    GNU Extension:
9311
9312    decl-specifier:
9313      attributes
9314
9315    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9316
9317    The parser flags FLAGS is used to control type-specifier parsing.
9318
9319    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9320    flags:
9321
9322      1: one of the decl-specifiers is an elaborated-type-specifier
9323         (i.e., a type declaration)
9324      2: one of the decl-specifiers is an enum-specifier or a
9325         class-specifier (i.e., a type definition)
9326
9327    */
9328
9329 static void
9330 cp_parser_decl_specifier_seq (cp_parser* parser,
9331                               cp_parser_flags flags,
9332                               cp_decl_specifier_seq *decl_specs,
9333                               int* declares_class_or_enum)
9334 {
9335   bool constructor_possible_p = !parser->in_declarator_p;
9336   cp_token *start_token = NULL;
9337
9338   /* Clear DECL_SPECS.  */
9339   clear_decl_specs (decl_specs);
9340
9341   /* Assume no class or enumeration type is declared.  */
9342   *declares_class_or_enum = 0;
9343
9344   /* Keep reading specifiers until there are no more to read.  */
9345   while (true)
9346     {
9347       bool constructor_p;
9348       bool found_decl_spec;
9349       cp_token *token;
9350
9351       /* Peek at the next token.  */
9352       token = cp_lexer_peek_token (parser->lexer);
9353
9354       /* Save the first token of the decl spec list for error
9355          reporting.  */
9356       if (!start_token)
9357         start_token = token;
9358       /* Handle attributes.  */
9359       if (token->keyword == RID_ATTRIBUTE)
9360         {
9361           /* Parse the attributes.  */
9362           decl_specs->attributes
9363             = chainon (decl_specs->attributes,
9364                        cp_parser_attributes_opt (parser));
9365           continue;
9366         }
9367       /* Assume we will find a decl-specifier keyword.  */
9368       found_decl_spec = true;
9369       /* If the next token is an appropriate keyword, we can simply
9370          add it to the list.  */
9371       switch (token->keyword)
9372         {
9373           /* decl-specifier:
9374                friend
9375                constexpr */
9376         case RID_FRIEND:
9377           if (!at_class_scope_p ())
9378             {
9379               error_at (token->location, "%<friend%> used outside of class");
9380               cp_lexer_purge_token (parser->lexer);
9381             }
9382           else
9383             {
9384               ++decl_specs->specs[(int) ds_friend];
9385               /* Consume the token.  */
9386               cp_lexer_consume_token (parser->lexer);
9387             }
9388           break;
9389
9390         case RID_CONSTEXPR:
9391           ++decl_specs->specs[(int) ds_constexpr];
9392           cp_lexer_consume_token (parser->lexer);
9393           break;
9394
9395           /* function-specifier:
9396                inline
9397                virtual
9398                explicit  */
9399         case RID_INLINE:
9400         case RID_VIRTUAL:
9401         case RID_EXPLICIT:
9402           cp_parser_function_specifier_opt (parser, decl_specs);
9403           break;
9404
9405           /* decl-specifier:
9406                typedef  */
9407         case RID_TYPEDEF:
9408           ++decl_specs->specs[(int) ds_typedef];
9409           /* Consume the token.  */
9410           cp_lexer_consume_token (parser->lexer);
9411           /* A constructor declarator cannot appear in a typedef.  */
9412           constructor_possible_p = false;
9413           /* The "typedef" keyword can only occur in a declaration; we
9414              may as well commit at this point.  */
9415           cp_parser_commit_to_tentative_parse (parser);
9416
9417           if (decl_specs->storage_class != sc_none)
9418             decl_specs->conflicting_specifiers_p = true;
9419           break;
9420
9421           /* storage-class-specifier:
9422                auto
9423                register
9424                static
9425                extern
9426                mutable
9427
9428              GNU Extension:
9429                thread  */
9430         case RID_AUTO:
9431           if (cxx_dialect == cxx98) 
9432             {
9433               /* Consume the token.  */
9434               cp_lexer_consume_token (parser->lexer);
9435
9436               /* Complain about `auto' as a storage specifier, if
9437                  we're complaining about C++0x compatibility.  */
9438               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9439                           " will change meaning in C++0x; please remove it");
9440
9441               /* Set the storage class anyway.  */
9442               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9443                                            token->location);
9444             }
9445           else
9446             /* C++0x auto type-specifier.  */
9447             found_decl_spec = false;
9448           break;
9449
9450         case RID_REGISTER:
9451         case RID_STATIC:
9452         case RID_EXTERN:
9453         case RID_MUTABLE:
9454           /* Consume the token.  */
9455           cp_lexer_consume_token (parser->lexer);
9456           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9457                                        token->location);
9458           break;
9459         case RID_THREAD:
9460           /* Consume the token.  */
9461           cp_lexer_consume_token (parser->lexer);
9462           ++decl_specs->specs[(int) ds_thread];
9463           break;
9464
9465         default:
9466           /* We did not yet find a decl-specifier yet.  */
9467           found_decl_spec = false;
9468           break;
9469         }
9470
9471       /* Constructors are a special case.  The `S' in `S()' is not a
9472          decl-specifier; it is the beginning of the declarator.  */
9473       constructor_p
9474         = (!found_decl_spec
9475            && constructor_possible_p
9476            && (cp_parser_constructor_declarator_p
9477                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9478
9479       /* If we don't have a DECL_SPEC yet, then we must be looking at
9480          a type-specifier.  */
9481       if (!found_decl_spec && !constructor_p)
9482         {
9483           int decl_spec_declares_class_or_enum;
9484           bool is_cv_qualifier;
9485           tree type_spec;
9486
9487           type_spec
9488             = cp_parser_type_specifier (parser, flags,
9489                                         decl_specs,
9490                                         /*is_declaration=*/true,
9491                                         &decl_spec_declares_class_or_enum,
9492                                         &is_cv_qualifier);
9493           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9494
9495           /* If this type-specifier referenced a user-defined type
9496              (a typedef, class-name, etc.), then we can't allow any
9497              more such type-specifiers henceforth.
9498
9499              [dcl.spec]
9500
9501              The longest sequence of decl-specifiers that could
9502              possibly be a type name is taken as the
9503              decl-specifier-seq of a declaration.  The sequence shall
9504              be self-consistent as described below.
9505
9506              [dcl.type]
9507
9508              As a general rule, at most one type-specifier is allowed
9509              in the complete decl-specifier-seq of a declaration.  The
9510              only exceptions are the following:
9511
9512              -- const or volatile can be combined with any other
9513                 type-specifier.
9514
9515              -- signed or unsigned can be combined with char, long,
9516                 short, or int.
9517
9518              -- ..
9519
9520              Example:
9521
9522                typedef char* Pc;
9523                void g (const int Pc);
9524
9525              Here, Pc is *not* part of the decl-specifier seq; it's
9526              the declarator.  Therefore, once we see a type-specifier
9527              (other than a cv-qualifier), we forbid any additional
9528              user-defined types.  We *do* still allow things like `int
9529              int' to be considered a decl-specifier-seq, and issue the
9530              error message later.  */
9531           if (type_spec && !is_cv_qualifier)
9532             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9533           /* A constructor declarator cannot follow a type-specifier.  */
9534           if (type_spec)
9535             {
9536               constructor_possible_p = false;
9537               found_decl_spec = true;
9538               if (!is_cv_qualifier)
9539                 decl_specs->any_type_specifiers_p = true;
9540             }
9541         }
9542
9543       /* If we still do not have a DECL_SPEC, then there are no more
9544          decl-specifiers.  */
9545       if (!found_decl_spec)
9546         break;
9547
9548       decl_specs->any_specifiers_p = true;
9549       /* After we see one decl-specifier, further decl-specifiers are
9550          always optional.  */
9551       flags |= CP_PARSER_FLAGS_OPTIONAL;
9552     }
9553
9554   cp_parser_check_decl_spec (decl_specs, start_token->location);
9555
9556   /* Don't allow a friend specifier with a class definition.  */
9557   if (decl_specs->specs[(int) ds_friend] != 0
9558       && (*declares_class_or_enum & 2))
9559     error_at (start_token->location,
9560               "class definition may not be declared a friend");
9561 }
9562
9563 /* Parse an (optional) storage-class-specifier.
9564
9565    storage-class-specifier:
9566      auto
9567      register
9568      static
9569      extern
9570      mutable
9571
9572    GNU Extension:
9573
9574    storage-class-specifier:
9575      thread
9576
9577    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9578
9579 static tree
9580 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9581 {
9582   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9583     {
9584     case RID_AUTO:
9585       if (cxx_dialect != cxx98)
9586         return NULL_TREE;
9587       /* Fall through for C++98.  */
9588
9589     case RID_REGISTER:
9590     case RID_STATIC:
9591     case RID_EXTERN:
9592     case RID_MUTABLE:
9593     case RID_THREAD:
9594       /* Consume the token.  */
9595       return cp_lexer_consume_token (parser->lexer)->u.value;
9596
9597     default:
9598       return NULL_TREE;
9599     }
9600 }
9601
9602 /* Parse an (optional) function-specifier.
9603
9604    function-specifier:
9605      inline
9606      virtual
9607      explicit
9608
9609    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9610    Updates DECL_SPECS, if it is non-NULL.  */
9611
9612 static tree
9613 cp_parser_function_specifier_opt (cp_parser* parser,
9614                                   cp_decl_specifier_seq *decl_specs)
9615 {
9616   cp_token *token = cp_lexer_peek_token (parser->lexer);
9617   switch (token->keyword)
9618     {
9619     case RID_INLINE:
9620       if (decl_specs)
9621         ++decl_specs->specs[(int) ds_inline];
9622       break;
9623
9624     case RID_VIRTUAL:
9625       /* 14.5.2.3 [temp.mem]
9626
9627          A member function template shall not be virtual.  */
9628       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9629         error_at (token->location, "templates may not be %<virtual%>");
9630       else if (decl_specs)
9631         ++decl_specs->specs[(int) ds_virtual];
9632       break;
9633
9634     case RID_EXPLICIT:
9635       if (decl_specs)
9636         ++decl_specs->specs[(int) ds_explicit];
9637       break;
9638
9639     default:
9640       return NULL_TREE;
9641     }
9642
9643   /* Consume the token.  */
9644   return cp_lexer_consume_token (parser->lexer)->u.value;
9645 }
9646
9647 /* Parse a linkage-specification.
9648
9649    linkage-specification:
9650      extern string-literal { declaration-seq [opt] }
9651      extern string-literal declaration  */
9652
9653 static void
9654 cp_parser_linkage_specification (cp_parser* parser)
9655 {
9656   tree linkage;
9657
9658   /* Look for the `extern' keyword.  */
9659   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
9660
9661   /* Look for the string-literal.  */
9662   linkage = cp_parser_string_literal (parser, false, false);
9663
9664   /* Transform the literal into an identifier.  If the literal is a
9665      wide-character string, or contains embedded NULs, then we can't
9666      handle it as the user wants.  */
9667   if (strlen (TREE_STRING_POINTER (linkage))
9668       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9669     {
9670       cp_parser_error (parser, "invalid linkage-specification");
9671       /* Assume C++ linkage.  */
9672       linkage = lang_name_cplusplus;
9673     }
9674   else
9675     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9676
9677   /* We're now using the new linkage.  */
9678   push_lang_context (linkage);
9679
9680   /* If the next token is a `{', then we're using the first
9681      production.  */
9682   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9683     {
9684       /* Consume the `{' token.  */
9685       cp_lexer_consume_token (parser->lexer);
9686       /* Parse the declarations.  */
9687       cp_parser_declaration_seq_opt (parser);
9688       /* Look for the closing `}'.  */
9689       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9690     }
9691   /* Otherwise, there's just one declaration.  */
9692   else
9693     {
9694       bool saved_in_unbraced_linkage_specification_p;
9695
9696       saved_in_unbraced_linkage_specification_p
9697         = parser->in_unbraced_linkage_specification_p;
9698       parser->in_unbraced_linkage_specification_p = true;
9699       cp_parser_declaration (parser);
9700       parser->in_unbraced_linkage_specification_p
9701         = saved_in_unbraced_linkage_specification_p;
9702     }
9703
9704   /* We're done with the linkage-specification.  */
9705   pop_lang_context ();
9706 }
9707
9708 /* Parse a static_assert-declaration.
9709
9710    static_assert-declaration:
9711      static_assert ( constant-expression , string-literal ) ; 
9712
9713    If MEMBER_P, this static_assert is a class member.  */
9714
9715 static void 
9716 cp_parser_static_assert(cp_parser *parser, bool member_p)
9717 {
9718   tree condition;
9719   tree message;
9720   cp_token *token;
9721   location_t saved_loc;
9722
9723   /* Peek at the `static_assert' token so we can keep track of exactly
9724      where the static assertion started.  */
9725   token = cp_lexer_peek_token (parser->lexer);
9726   saved_loc = token->location;
9727
9728   /* Look for the `static_assert' keyword.  */
9729   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9730                                   RT_STATIC_ASSERT))
9731     return;
9732
9733   /*  We know we are in a static assertion; commit to any tentative
9734       parse.  */
9735   if (cp_parser_parsing_tentatively (parser))
9736     cp_parser_commit_to_tentative_parse (parser);
9737
9738   /* Parse the `(' starting the static assertion condition.  */
9739   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9740
9741   /* Parse the constant-expression.  */
9742   condition = 
9743     cp_parser_constant_expression (parser,
9744                                    /*allow_non_constant_p=*/false,
9745                                    /*non_constant_p=*/NULL);
9746
9747   /* Parse the separating `,'.  */
9748   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9749
9750   /* Parse the string-literal message.  */
9751   message = cp_parser_string_literal (parser, 
9752                                       /*translate=*/false,
9753                                       /*wide_ok=*/true);
9754
9755   /* A `)' completes the static assertion.  */
9756   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9757     cp_parser_skip_to_closing_parenthesis (parser, 
9758                                            /*recovering=*/true, 
9759                                            /*or_comma=*/false,
9760                                            /*consume_paren=*/true);
9761
9762   /* A semicolon terminates the declaration.  */
9763   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9764
9765   /* Complete the static assertion, which may mean either processing 
9766      the static assert now or saving it for template instantiation.  */
9767   finish_static_assert (condition, message, saved_loc, member_p);
9768 }
9769
9770 /* Parse a `decltype' type. Returns the type. 
9771
9772    simple-type-specifier:
9773      decltype ( expression )  */
9774
9775 static tree
9776 cp_parser_decltype (cp_parser *parser)
9777 {
9778   tree expr;
9779   bool id_expression_or_member_access_p = false;
9780   const char *saved_message;
9781   bool saved_integral_constant_expression_p;
9782   bool saved_non_integral_constant_expression_p;
9783   cp_token *id_expr_start_token;
9784
9785   /* Look for the `decltype' token.  */
9786   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
9787     return error_mark_node;
9788
9789   /* Types cannot be defined in a `decltype' expression.  Save away the
9790      old message.  */
9791   saved_message = parser->type_definition_forbidden_message;
9792
9793   /* And create the new one.  */
9794   parser->type_definition_forbidden_message
9795     = G_("types may not be defined in %<decltype%> expressions");
9796
9797   /* The restrictions on constant-expressions do not apply inside
9798      decltype expressions.  */
9799   saved_integral_constant_expression_p
9800     = parser->integral_constant_expression_p;
9801   saved_non_integral_constant_expression_p
9802     = parser->non_integral_constant_expression_p;
9803   parser->integral_constant_expression_p = false;
9804
9805   /* Do not actually evaluate the expression.  */
9806   ++cp_unevaluated_operand;
9807
9808   /* Do not warn about problems with the expression.  */
9809   ++c_inhibit_evaluation_warnings;
9810
9811   /* Parse the opening `('.  */
9812   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9813     return error_mark_node;
9814   
9815   /* First, try parsing an id-expression.  */
9816   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9817   cp_parser_parse_tentatively (parser);
9818   expr = cp_parser_id_expression (parser,
9819                                   /*template_keyword_p=*/false,
9820                                   /*check_dependency_p=*/true,
9821                                   /*template_p=*/NULL,
9822                                   /*declarator_p=*/false,
9823                                   /*optional_p=*/false);
9824
9825   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9826     {
9827       bool non_integral_constant_expression_p = false;
9828       tree id_expression = expr;
9829       cp_id_kind idk;
9830       const char *error_msg;
9831
9832       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9833         /* Lookup the name we got back from the id-expression.  */
9834         expr = cp_parser_lookup_name (parser, expr,
9835                                       none_type,
9836                                       /*is_template=*/false,
9837                                       /*is_namespace=*/false,
9838                                       /*check_dependency=*/true,
9839                                       /*ambiguous_decls=*/NULL,
9840                                       id_expr_start_token->location);
9841
9842       if (expr
9843           && expr != error_mark_node
9844           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9845           && TREE_CODE (expr) != TYPE_DECL
9846           && (TREE_CODE (expr) != BIT_NOT_EXPR
9847               || !TYPE_P (TREE_OPERAND (expr, 0)))
9848           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9849         {
9850           /* Complete lookup of the id-expression.  */
9851           expr = (finish_id_expression
9852                   (id_expression, expr, parser->scope, &idk,
9853                    /*integral_constant_expression_p=*/false,
9854                    /*allow_non_integral_constant_expression_p=*/true,
9855                    &non_integral_constant_expression_p,
9856                    /*template_p=*/false,
9857                    /*done=*/true,
9858                    /*address_p=*/false,
9859                    /*template_arg_p=*/false,
9860                    &error_msg,
9861                    id_expr_start_token->location));
9862
9863           if (expr == error_mark_node)
9864             /* We found an id-expression, but it was something that we
9865                should not have found. This is an error, not something
9866                we can recover from, so note that we found an
9867                id-expression and we'll recover as gracefully as
9868                possible.  */
9869             id_expression_or_member_access_p = true;
9870         }
9871
9872       if (expr 
9873           && expr != error_mark_node
9874           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9875         /* We have an id-expression.  */
9876         id_expression_or_member_access_p = true;
9877     }
9878
9879   if (!id_expression_or_member_access_p)
9880     {
9881       /* Abort the id-expression parse.  */
9882       cp_parser_abort_tentative_parse (parser);
9883
9884       /* Parsing tentatively, again.  */
9885       cp_parser_parse_tentatively (parser);
9886
9887       /* Parse a class member access.  */
9888       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9889                                            /*cast_p=*/false,
9890                                            /*member_access_only_p=*/true, NULL);
9891
9892       if (expr 
9893           && expr != error_mark_node
9894           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9895         /* We have an id-expression.  */
9896         id_expression_or_member_access_p = true;
9897     }
9898
9899   if (id_expression_or_member_access_p)
9900     /* We have parsed the complete id-expression or member access.  */
9901     cp_parser_parse_definitely (parser);
9902   else
9903     {
9904       bool saved_greater_than_is_operator_p;
9905
9906       /* Abort our attempt to parse an id-expression or member access
9907          expression.  */
9908       cp_parser_abort_tentative_parse (parser);
9909
9910       /* Within a parenthesized expression, a `>' token is always
9911          the greater-than operator.  */
9912       saved_greater_than_is_operator_p
9913         = parser->greater_than_is_operator_p;
9914       parser->greater_than_is_operator_p = true;
9915
9916       /* Parse a full expression.  */
9917       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9918
9919       /* The `>' token might be the end of a template-id or
9920          template-parameter-list now.  */
9921       parser->greater_than_is_operator_p
9922         = saved_greater_than_is_operator_p;
9923     }
9924
9925   /* Go back to evaluating expressions.  */
9926   --cp_unevaluated_operand;
9927   --c_inhibit_evaluation_warnings;
9928
9929   /* Restore the old message and the integral constant expression
9930      flags.  */
9931   parser->type_definition_forbidden_message = saved_message;
9932   parser->integral_constant_expression_p
9933     = saved_integral_constant_expression_p;
9934   parser->non_integral_constant_expression_p
9935     = saved_non_integral_constant_expression_p;
9936
9937   if (expr == error_mark_node)
9938     {
9939       /* Skip everything up to the closing `)'.  */
9940       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9941                                              /*consume_paren=*/true);
9942       return error_mark_node;
9943     }
9944   
9945   /* Parse to the closing `)'.  */
9946   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9947     {
9948       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9949                                              /*consume_paren=*/true);
9950       return error_mark_node;
9951     }
9952
9953   return finish_decltype_type (expr, id_expression_or_member_access_p);
9954 }
9955
9956 /* Special member functions [gram.special] */
9957
9958 /* Parse a conversion-function-id.
9959
9960    conversion-function-id:
9961      operator conversion-type-id
9962
9963    Returns an IDENTIFIER_NODE representing the operator.  */
9964
9965 static tree
9966 cp_parser_conversion_function_id (cp_parser* parser)
9967 {
9968   tree type;
9969   tree saved_scope;
9970   tree saved_qualifying_scope;
9971   tree saved_object_scope;
9972   tree pushed_scope = NULL_TREE;
9973
9974   /* Look for the `operator' token.  */
9975   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
9976     return error_mark_node;
9977   /* When we parse the conversion-type-id, the current scope will be
9978      reset.  However, we need that information in able to look up the
9979      conversion function later, so we save it here.  */
9980   saved_scope = parser->scope;
9981   saved_qualifying_scope = parser->qualifying_scope;
9982   saved_object_scope = parser->object_scope;
9983   /* We must enter the scope of the class so that the names of
9984      entities declared within the class are available in the
9985      conversion-type-id.  For example, consider:
9986
9987        struct S {
9988          typedef int I;
9989          operator I();
9990        };
9991
9992        S::operator I() { ... }
9993
9994      In order to see that `I' is a type-name in the definition, we
9995      must be in the scope of `S'.  */
9996   if (saved_scope)
9997     pushed_scope = push_scope (saved_scope);
9998   /* Parse the conversion-type-id.  */
9999   type = cp_parser_conversion_type_id (parser);
10000   /* Leave the scope of the class, if any.  */
10001   if (pushed_scope)
10002     pop_scope (pushed_scope);
10003   /* Restore the saved scope.  */
10004   parser->scope = saved_scope;
10005   parser->qualifying_scope = saved_qualifying_scope;
10006   parser->object_scope = saved_object_scope;
10007   /* If the TYPE is invalid, indicate failure.  */
10008   if (type == error_mark_node)
10009     return error_mark_node;
10010   return mangle_conv_op_name_for_type (type);
10011 }
10012
10013 /* Parse a conversion-type-id:
10014
10015    conversion-type-id:
10016      type-specifier-seq conversion-declarator [opt]
10017
10018    Returns the TYPE specified.  */
10019
10020 static tree
10021 cp_parser_conversion_type_id (cp_parser* parser)
10022 {
10023   tree attributes;
10024   cp_decl_specifier_seq type_specifiers;
10025   cp_declarator *declarator;
10026   tree type_specified;
10027
10028   /* Parse the attributes.  */
10029   attributes = cp_parser_attributes_opt (parser);
10030   /* Parse the type-specifiers.  */
10031   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10032                                 /*is_trailing_return=*/false,
10033                                 &type_specifiers);
10034   /* If that didn't work, stop.  */
10035   if (type_specifiers.type == error_mark_node)
10036     return error_mark_node;
10037   /* Parse the conversion-declarator.  */
10038   declarator = cp_parser_conversion_declarator_opt (parser);
10039
10040   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10041                                     /*initialized=*/0, &attributes);
10042   if (attributes)
10043     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10044
10045   /* Don't give this error when parsing tentatively.  This happens to
10046      work because we always parse this definitively once.  */
10047   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10048       && type_uses_auto (type_specified))
10049     {
10050       error ("invalid use of %<auto%> in conversion operator");
10051       return error_mark_node;
10052     }
10053
10054   return type_specified;
10055 }
10056
10057 /* Parse an (optional) conversion-declarator.
10058
10059    conversion-declarator:
10060      ptr-operator conversion-declarator [opt]
10061
10062    */
10063
10064 static cp_declarator *
10065 cp_parser_conversion_declarator_opt (cp_parser* parser)
10066 {
10067   enum tree_code code;
10068   tree class_type;
10069   cp_cv_quals cv_quals;
10070
10071   /* We don't know if there's a ptr-operator next, or not.  */
10072   cp_parser_parse_tentatively (parser);
10073   /* Try the ptr-operator.  */
10074   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10075   /* If it worked, look for more conversion-declarators.  */
10076   if (cp_parser_parse_definitely (parser))
10077     {
10078       cp_declarator *declarator;
10079
10080       /* Parse another optional declarator.  */
10081       declarator = cp_parser_conversion_declarator_opt (parser);
10082
10083       return cp_parser_make_indirect_declarator
10084         (code, class_type, cv_quals, declarator);
10085    }
10086
10087   return NULL;
10088 }
10089
10090 /* Parse an (optional) ctor-initializer.
10091
10092    ctor-initializer:
10093      : mem-initializer-list
10094
10095    Returns TRUE iff the ctor-initializer was actually present.  */
10096
10097 static bool
10098 cp_parser_ctor_initializer_opt (cp_parser* parser)
10099 {
10100   /* If the next token is not a `:', then there is no
10101      ctor-initializer.  */
10102   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10103     {
10104       /* Do default initialization of any bases and members.  */
10105       if (DECL_CONSTRUCTOR_P (current_function_decl))
10106         finish_mem_initializers (NULL_TREE);
10107
10108       return false;
10109     }
10110
10111   /* Consume the `:' token.  */
10112   cp_lexer_consume_token (parser->lexer);
10113   /* And the mem-initializer-list.  */
10114   cp_parser_mem_initializer_list (parser);
10115
10116   return true;
10117 }
10118
10119 /* Parse a mem-initializer-list.
10120
10121    mem-initializer-list:
10122      mem-initializer ... [opt]
10123      mem-initializer ... [opt] , mem-initializer-list  */
10124
10125 static void
10126 cp_parser_mem_initializer_list (cp_parser* parser)
10127 {
10128   tree mem_initializer_list = NULL_TREE;
10129   cp_token *token = cp_lexer_peek_token (parser->lexer);
10130
10131   /* Let the semantic analysis code know that we are starting the
10132      mem-initializer-list.  */
10133   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10134     error_at (token->location,
10135               "only constructors take base initializers");
10136
10137   /* Loop through the list.  */
10138   while (true)
10139     {
10140       tree mem_initializer;
10141
10142       token = cp_lexer_peek_token (parser->lexer);
10143       /* Parse the mem-initializer.  */
10144       mem_initializer = cp_parser_mem_initializer (parser);
10145       /* If the next token is a `...', we're expanding member initializers. */
10146       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10147         {
10148           /* Consume the `...'. */
10149           cp_lexer_consume_token (parser->lexer);
10150
10151           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10152              can be expanded but members cannot. */
10153           if (mem_initializer != error_mark_node
10154               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10155             {
10156               error_at (token->location,
10157                         "cannot expand initializer for member %<%D%>",
10158                         TREE_PURPOSE (mem_initializer));
10159               mem_initializer = error_mark_node;
10160             }
10161
10162           /* Construct the pack expansion type. */
10163           if (mem_initializer != error_mark_node)
10164             mem_initializer = make_pack_expansion (mem_initializer);
10165         }
10166       /* Add it to the list, unless it was erroneous.  */
10167       if (mem_initializer != error_mark_node)
10168         {
10169           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10170           mem_initializer_list = mem_initializer;
10171         }
10172       /* If the next token is not a `,', we're done.  */
10173       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10174         break;
10175       /* Consume the `,' token.  */
10176       cp_lexer_consume_token (parser->lexer);
10177     }
10178
10179   /* Perform semantic analysis.  */
10180   if (DECL_CONSTRUCTOR_P (current_function_decl))
10181     finish_mem_initializers (mem_initializer_list);
10182 }
10183
10184 /* Parse a mem-initializer.
10185
10186    mem-initializer:
10187      mem-initializer-id ( expression-list [opt] )
10188      mem-initializer-id braced-init-list
10189
10190    GNU extension:
10191
10192    mem-initializer:
10193      ( expression-list [opt] )
10194
10195    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10196    class) or FIELD_DECL (for a non-static data member) to initialize;
10197    the TREE_VALUE is the expression-list.  An empty initialization
10198    list is represented by void_list_node.  */
10199
10200 static tree
10201 cp_parser_mem_initializer (cp_parser* parser)
10202 {
10203   tree mem_initializer_id;
10204   tree expression_list;
10205   tree member;
10206   cp_token *token = cp_lexer_peek_token (parser->lexer);
10207
10208   /* Find out what is being initialized.  */
10209   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10210     {
10211       permerror (token->location,
10212                  "anachronistic old-style base class initializer");
10213       mem_initializer_id = NULL_TREE;
10214     }
10215   else
10216     {
10217       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10218       if (mem_initializer_id == error_mark_node)
10219         return mem_initializer_id;
10220     }
10221   member = expand_member_init (mem_initializer_id);
10222   if (member && !DECL_P (member))
10223     in_base_initializer = 1;
10224
10225   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10226     {
10227       bool expr_non_constant_p;
10228       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10229       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10230       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10231       expression_list = build_tree_list (NULL_TREE, expression_list);
10232     }
10233   else
10234     {
10235       VEC(tree,gc)* vec;
10236       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10237                                                      /*cast_p=*/false,
10238                                                      /*allow_expansion_p=*/true,
10239                                                      /*non_constant_p=*/NULL);
10240       if (vec == NULL)
10241         return error_mark_node;
10242       expression_list = build_tree_list_vec (vec);
10243       release_tree_vector (vec);
10244     }
10245
10246   if (expression_list == error_mark_node)
10247     return error_mark_node;
10248   if (!expression_list)
10249     expression_list = void_type_node;
10250
10251   in_base_initializer = 0;
10252
10253   return member ? build_tree_list (member, expression_list) : error_mark_node;
10254 }
10255
10256 /* Parse a mem-initializer-id.
10257
10258    mem-initializer-id:
10259      :: [opt] nested-name-specifier [opt] class-name
10260      identifier
10261
10262    Returns a TYPE indicating the class to be initializer for the first
10263    production.  Returns an IDENTIFIER_NODE indicating the data member
10264    to be initialized for the second production.  */
10265
10266 static tree
10267 cp_parser_mem_initializer_id (cp_parser* parser)
10268 {
10269   bool global_scope_p;
10270   bool nested_name_specifier_p;
10271   bool template_p = false;
10272   tree id;
10273
10274   cp_token *token = cp_lexer_peek_token (parser->lexer);
10275
10276   /* `typename' is not allowed in this context ([temp.res]).  */
10277   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10278     {
10279       error_at (token->location, 
10280                 "keyword %<typename%> not allowed in this context (a qualified "
10281                 "member initializer is implicitly a type)");
10282       cp_lexer_consume_token (parser->lexer);
10283     }
10284   /* Look for the optional `::' operator.  */
10285   global_scope_p
10286     = (cp_parser_global_scope_opt (parser,
10287                                    /*current_scope_valid_p=*/false)
10288        != NULL_TREE);
10289   /* Look for the optional nested-name-specifier.  The simplest way to
10290      implement:
10291
10292        [temp.res]
10293
10294        The keyword `typename' is not permitted in a base-specifier or
10295        mem-initializer; in these contexts a qualified name that
10296        depends on a template-parameter is implicitly assumed to be a
10297        type name.
10298
10299      is to assume that we have seen the `typename' keyword at this
10300      point.  */
10301   nested_name_specifier_p
10302     = (cp_parser_nested_name_specifier_opt (parser,
10303                                             /*typename_keyword_p=*/true,
10304                                             /*check_dependency_p=*/true,
10305                                             /*type_p=*/true,
10306                                             /*is_declaration=*/true)
10307        != NULL_TREE);
10308   if (nested_name_specifier_p)
10309     template_p = cp_parser_optional_template_keyword (parser);
10310   /* If there is a `::' operator or a nested-name-specifier, then we
10311      are definitely looking for a class-name.  */
10312   if (global_scope_p || nested_name_specifier_p)
10313     return cp_parser_class_name (parser,
10314                                  /*typename_keyword_p=*/true,
10315                                  /*template_keyword_p=*/template_p,
10316                                  typename_type,
10317                                  /*check_dependency_p=*/true,
10318                                  /*class_head_p=*/false,
10319                                  /*is_declaration=*/true);
10320   /* Otherwise, we could also be looking for an ordinary identifier.  */
10321   cp_parser_parse_tentatively (parser);
10322   /* Try a class-name.  */
10323   id = cp_parser_class_name (parser,
10324                              /*typename_keyword_p=*/true,
10325                              /*template_keyword_p=*/false,
10326                              none_type,
10327                              /*check_dependency_p=*/true,
10328                              /*class_head_p=*/false,
10329                              /*is_declaration=*/true);
10330   /* If we found one, we're done.  */
10331   if (cp_parser_parse_definitely (parser))
10332     return id;
10333   /* Otherwise, look for an ordinary identifier.  */
10334   return cp_parser_identifier (parser);
10335 }
10336
10337 /* Overloading [gram.over] */
10338
10339 /* Parse an operator-function-id.
10340
10341    operator-function-id:
10342      operator operator
10343
10344    Returns an IDENTIFIER_NODE for the operator which is a
10345    human-readable spelling of the identifier, e.g., `operator +'.  */
10346
10347 static tree
10348 cp_parser_operator_function_id (cp_parser* parser)
10349 {
10350   /* Look for the `operator' keyword.  */
10351   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10352     return error_mark_node;
10353   /* And then the name of the operator itself.  */
10354   return cp_parser_operator (parser);
10355 }
10356
10357 /* Parse an operator.
10358
10359    operator:
10360      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10361      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10362      || ++ -- , ->* -> () []
10363
10364    GNU Extensions:
10365
10366    operator:
10367      <? >? <?= >?=
10368
10369    Returns an IDENTIFIER_NODE for the operator which is a
10370    human-readable spelling of the identifier, e.g., `operator +'.  */
10371
10372 static tree
10373 cp_parser_operator (cp_parser* parser)
10374 {
10375   tree id = NULL_TREE;
10376   cp_token *token;
10377
10378   /* Peek at the next token.  */
10379   token = cp_lexer_peek_token (parser->lexer);
10380   /* Figure out which operator we have.  */
10381   switch (token->type)
10382     {
10383     case CPP_KEYWORD:
10384       {
10385         enum tree_code op;
10386
10387         /* The keyword should be either `new' or `delete'.  */
10388         if (token->keyword == RID_NEW)
10389           op = NEW_EXPR;
10390         else if (token->keyword == RID_DELETE)
10391           op = DELETE_EXPR;
10392         else
10393           break;
10394
10395         /* Consume the `new' or `delete' token.  */
10396         cp_lexer_consume_token (parser->lexer);
10397
10398         /* Peek at the next token.  */
10399         token = cp_lexer_peek_token (parser->lexer);
10400         /* If it's a `[' token then this is the array variant of the
10401            operator.  */
10402         if (token->type == CPP_OPEN_SQUARE)
10403           {
10404             /* Consume the `[' token.  */
10405             cp_lexer_consume_token (parser->lexer);
10406             /* Look for the `]' token.  */
10407             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10408             id = ansi_opname (op == NEW_EXPR
10409                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10410           }
10411         /* Otherwise, we have the non-array variant.  */
10412         else
10413           id = ansi_opname (op);
10414
10415         return id;
10416       }
10417
10418     case CPP_PLUS:
10419       id = ansi_opname (PLUS_EXPR);
10420       break;
10421
10422     case CPP_MINUS:
10423       id = ansi_opname (MINUS_EXPR);
10424       break;
10425
10426     case CPP_MULT:
10427       id = ansi_opname (MULT_EXPR);
10428       break;
10429
10430     case CPP_DIV:
10431       id = ansi_opname (TRUNC_DIV_EXPR);
10432       break;
10433
10434     case CPP_MOD:
10435       id = ansi_opname (TRUNC_MOD_EXPR);
10436       break;
10437
10438     case CPP_XOR:
10439       id = ansi_opname (BIT_XOR_EXPR);
10440       break;
10441
10442     case CPP_AND:
10443       id = ansi_opname (BIT_AND_EXPR);
10444       break;
10445
10446     case CPP_OR:
10447       id = ansi_opname (BIT_IOR_EXPR);
10448       break;
10449
10450     case CPP_COMPL:
10451       id = ansi_opname (BIT_NOT_EXPR);
10452       break;
10453
10454     case CPP_NOT:
10455       id = ansi_opname (TRUTH_NOT_EXPR);
10456       break;
10457
10458     case CPP_EQ:
10459       id = ansi_assopname (NOP_EXPR);
10460       break;
10461
10462     case CPP_LESS:
10463       id = ansi_opname (LT_EXPR);
10464       break;
10465
10466     case CPP_GREATER:
10467       id = ansi_opname (GT_EXPR);
10468       break;
10469
10470     case CPP_PLUS_EQ:
10471       id = ansi_assopname (PLUS_EXPR);
10472       break;
10473
10474     case CPP_MINUS_EQ:
10475       id = ansi_assopname (MINUS_EXPR);
10476       break;
10477
10478     case CPP_MULT_EQ:
10479       id = ansi_assopname (MULT_EXPR);
10480       break;
10481
10482     case CPP_DIV_EQ:
10483       id = ansi_assopname (TRUNC_DIV_EXPR);
10484       break;
10485
10486     case CPP_MOD_EQ:
10487       id = ansi_assopname (TRUNC_MOD_EXPR);
10488       break;
10489
10490     case CPP_XOR_EQ:
10491       id = ansi_assopname (BIT_XOR_EXPR);
10492       break;
10493
10494     case CPP_AND_EQ:
10495       id = ansi_assopname (BIT_AND_EXPR);
10496       break;
10497
10498     case CPP_OR_EQ:
10499       id = ansi_assopname (BIT_IOR_EXPR);
10500       break;
10501
10502     case CPP_LSHIFT:
10503       id = ansi_opname (LSHIFT_EXPR);
10504       break;
10505
10506     case CPP_RSHIFT:
10507       id = ansi_opname (RSHIFT_EXPR);
10508       break;
10509
10510     case CPP_LSHIFT_EQ:
10511       id = ansi_assopname (LSHIFT_EXPR);
10512       break;
10513
10514     case CPP_RSHIFT_EQ:
10515       id = ansi_assopname (RSHIFT_EXPR);
10516       break;
10517
10518     case CPP_EQ_EQ:
10519       id = ansi_opname (EQ_EXPR);
10520       break;
10521
10522     case CPP_NOT_EQ:
10523       id = ansi_opname (NE_EXPR);
10524       break;
10525
10526     case CPP_LESS_EQ:
10527       id = ansi_opname (LE_EXPR);
10528       break;
10529
10530     case CPP_GREATER_EQ:
10531       id = ansi_opname (GE_EXPR);
10532       break;
10533
10534     case CPP_AND_AND:
10535       id = ansi_opname (TRUTH_ANDIF_EXPR);
10536       break;
10537
10538     case CPP_OR_OR:
10539       id = ansi_opname (TRUTH_ORIF_EXPR);
10540       break;
10541
10542     case CPP_PLUS_PLUS:
10543       id = ansi_opname (POSTINCREMENT_EXPR);
10544       break;
10545
10546     case CPP_MINUS_MINUS:
10547       id = ansi_opname (PREDECREMENT_EXPR);
10548       break;
10549
10550     case CPP_COMMA:
10551       id = ansi_opname (COMPOUND_EXPR);
10552       break;
10553
10554     case CPP_DEREF_STAR:
10555       id = ansi_opname (MEMBER_REF);
10556       break;
10557
10558     case CPP_DEREF:
10559       id = ansi_opname (COMPONENT_REF);
10560       break;
10561
10562     case CPP_OPEN_PAREN:
10563       /* Consume the `('.  */
10564       cp_lexer_consume_token (parser->lexer);
10565       /* Look for the matching `)'.  */
10566       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10567       return ansi_opname (CALL_EXPR);
10568
10569     case CPP_OPEN_SQUARE:
10570       /* Consume the `['.  */
10571       cp_lexer_consume_token (parser->lexer);
10572       /* Look for the matching `]'.  */
10573       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10574       return ansi_opname (ARRAY_REF);
10575
10576     default:
10577       /* Anything else is an error.  */
10578       break;
10579     }
10580
10581   /* If we have selected an identifier, we need to consume the
10582      operator token.  */
10583   if (id)
10584     cp_lexer_consume_token (parser->lexer);
10585   /* Otherwise, no valid operator name was present.  */
10586   else
10587     {
10588       cp_parser_error (parser, "expected operator");
10589       id = error_mark_node;
10590     }
10591
10592   return id;
10593 }
10594
10595 /* Parse a template-declaration.
10596
10597    template-declaration:
10598      export [opt] template < template-parameter-list > declaration
10599
10600    If MEMBER_P is TRUE, this template-declaration occurs within a
10601    class-specifier.
10602
10603    The grammar rule given by the standard isn't correct.  What
10604    is really meant is:
10605
10606    template-declaration:
10607      export [opt] template-parameter-list-seq
10608        decl-specifier-seq [opt] init-declarator [opt] ;
10609      export [opt] template-parameter-list-seq
10610        function-definition
10611
10612    template-parameter-list-seq:
10613      template-parameter-list-seq [opt]
10614      template < template-parameter-list >  */
10615
10616 static void
10617 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10618 {
10619   /* Check for `export'.  */
10620   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10621     {
10622       /* Consume the `export' token.  */
10623       cp_lexer_consume_token (parser->lexer);
10624       /* Warn that we do not support `export'.  */
10625       warning (0, "keyword %<export%> not implemented, and will be ignored");
10626     }
10627
10628   cp_parser_template_declaration_after_export (parser, member_p);
10629 }
10630
10631 /* Parse a template-parameter-list.
10632
10633    template-parameter-list:
10634      template-parameter
10635      template-parameter-list , template-parameter
10636
10637    Returns a TREE_LIST.  Each node represents a template parameter.
10638    The nodes are connected via their TREE_CHAINs.  */
10639
10640 static tree
10641 cp_parser_template_parameter_list (cp_parser* parser)
10642 {
10643   tree parameter_list = NULL_TREE;
10644
10645   begin_template_parm_list ();
10646   while (true)
10647     {
10648       tree parameter;
10649       bool is_non_type;
10650       bool is_parameter_pack;
10651       location_t parm_loc;
10652
10653       /* Parse the template-parameter.  */
10654       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10655       parameter = cp_parser_template_parameter (parser, 
10656                                                 &is_non_type,
10657                                                 &is_parameter_pack);
10658       /* Add it to the list.  */
10659       if (parameter != error_mark_node)
10660         parameter_list = process_template_parm (parameter_list,
10661                                                 parm_loc,
10662                                                 parameter,
10663                                                 is_non_type,
10664                                                 is_parameter_pack);
10665       else
10666        {
10667          tree err_parm = build_tree_list (parameter, parameter);
10668          TREE_VALUE (err_parm) = error_mark_node;
10669          parameter_list = chainon (parameter_list, err_parm);
10670        }
10671
10672       /* If the next token is not a `,', we're done.  */
10673       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10674         break;
10675       /* Otherwise, consume the `,' token.  */
10676       cp_lexer_consume_token (parser->lexer);
10677     }
10678
10679   return end_template_parm_list (parameter_list);
10680 }
10681
10682 /* Parse a template-parameter.
10683
10684    template-parameter:
10685      type-parameter
10686      parameter-declaration
10687
10688    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10689    the parameter.  The TREE_PURPOSE is the default value, if any.
10690    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10691    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10692    set to true iff this parameter is a parameter pack. */
10693
10694 static tree
10695 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10696                               bool *is_parameter_pack)
10697 {
10698   cp_token *token;
10699   cp_parameter_declarator *parameter_declarator;
10700   cp_declarator *id_declarator;
10701   tree parm;
10702
10703   /* Assume it is a type parameter or a template parameter.  */
10704   *is_non_type = false;
10705   /* Assume it not a parameter pack. */
10706   *is_parameter_pack = false;
10707   /* Peek at the next token.  */
10708   token = cp_lexer_peek_token (parser->lexer);
10709   /* If it is `class' or `template', we have a type-parameter.  */
10710   if (token->keyword == RID_TEMPLATE)
10711     return cp_parser_type_parameter (parser, is_parameter_pack);
10712   /* If it is `class' or `typename' we do not know yet whether it is a
10713      type parameter or a non-type parameter.  Consider:
10714
10715        template <typename T, typename T::X X> ...
10716
10717      or:
10718
10719        template <class C, class D*> ...
10720
10721      Here, the first parameter is a type parameter, and the second is
10722      a non-type parameter.  We can tell by looking at the token after
10723      the identifier -- if it is a `,', `=', or `>' then we have a type
10724      parameter.  */
10725   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10726     {
10727       /* Peek at the token after `class' or `typename'.  */
10728       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10729       /* If it's an ellipsis, we have a template type parameter
10730          pack. */
10731       if (token->type == CPP_ELLIPSIS)
10732         return cp_parser_type_parameter (parser, is_parameter_pack);
10733       /* If it's an identifier, skip it.  */
10734       if (token->type == CPP_NAME)
10735         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10736       /* Now, see if the token looks like the end of a template
10737          parameter.  */
10738       if (token->type == CPP_COMMA
10739           || token->type == CPP_EQ
10740           || token->type == CPP_GREATER)
10741         return cp_parser_type_parameter (parser, is_parameter_pack);
10742     }
10743
10744   /* Otherwise, it is a non-type parameter.
10745
10746      [temp.param]
10747
10748      When parsing a default template-argument for a non-type
10749      template-parameter, the first non-nested `>' is taken as the end
10750      of the template parameter-list rather than a greater-than
10751      operator.  */
10752   *is_non_type = true;
10753   parameter_declarator
10754      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10755                                         /*parenthesized_p=*/NULL);
10756
10757   /* If the parameter declaration is marked as a parameter pack, set
10758      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10759      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10760      grokdeclarator. */
10761   if (parameter_declarator
10762       && parameter_declarator->declarator
10763       && parameter_declarator->declarator->parameter_pack_p)
10764     {
10765       *is_parameter_pack = true;
10766       parameter_declarator->declarator->parameter_pack_p = false;
10767     }
10768
10769   /* If the next token is an ellipsis, and we don't already have it
10770      marked as a parameter pack, then we have a parameter pack (that
10771      has no declarator).  */
10772   if (!*is_parameter_pack
10773       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10774       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10775     {
10776       /* Consume the `...'.  */
10777       cp_lexer_consume_token (parser->lexer);
10778       maybe_warn_variadic_templates ();
10779       
10780       *is_parameter_pack = true;
10781     }
10782   /* We might end up with a pack expansion as the type of the non-type
10783      template parameter, in which case this is a non-type template
10784      parameter pack.  */
10785   else if (parameter_declarator
10786            && parameter_declarator->decl_specifiers.type
10787            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10788     {
10789       *is_parameter_pack = true;
10790       parameter_declarator->decl_specifiers.type = 
10791         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10792     }
10793
10794   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10795     {
10796       /* Parameter packs cannot have default arguments.  However, a
10797          user may try to do so, so we'll parse them and give an
10798          appropriate diagnostic here.  */
10799
10800       /* Consume the `='.  */
10801       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10802       cp_lexer_consume_token (parser->lexer);
10803       
10804       /* Find the name of the parameter pack.  */     
10805       id_declarator = parameter_declarator->declarator;
10806       while (id_declarator && id_declarator->kind != cdk_id)
10807         id_declarator = id_declarator->declarator;
10808       
10809       if (id_declarator && id_declarator->kind == cdk_id)
10810         error_at (start_token->location,
10811                   "template parameter pack %qD cannot have a default argument",
10812                   id_declarator->u.id.unqualified_name);
10813       else
10814         error_at (start_token->location,
10815                   "template parameter pack cannot have a default argument");
10816       
10817       /* Parse the default argument, but throw away the result.  */
10818       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10819     }
10820
10821   parm = grokdeclarator (parameter_declarator->declarator,
10822                          &parameter_declarator->decl_specifiers,
10823                          TPARM, /*initialized=*/0,
10824                          /*attrlist=*/NULL);
10825   if (parm == error_mark_node)
10826     return error_mark_node;
10827
10828   return build_tree_list (parameter_declarator->default_argument, parm);
10829 }
10830
10831 /* Parse a type-parameter.
10832
10833    type-parameter:
10834      class identifier [opt]
10835      class identifier [opt] = type-id
10836      typename identifier [opt]
10837      typename identifier [opt] = type-id
10838      template < template-parameter-list > class identifier [opt]
10839      template < template-parameter-list > class identifier [opt]
10840        = id-expression
10841
10842    GNU Extension (variadic templates):
10843
10844    type-parameter:
10845      class ... identifier [opt]
10846      typename ... identifier [opt]
10847
10848    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10849    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10850    the declaration of the parameter.
10851
10852    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10853
10854 static tree
10855 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10856 {
10857   cp_token *token;
10858   tree parameter;
10859
10860   /* Look for a keyword to tell us what kind of parameter this is.  */
10861   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
10862   if (!token)
10863     return error_mark_node;
10864
10865   switch (token->keyword)
10866     {
10867     case RID_CLASS:
10868     case RID_TYPENAME:
10869       {
10870         tree identifier;
10871         tree default_argument;
10872
10873         /* If the next token is an ellipsis, we have a template
10874            argument pack. */
10875         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10876           {
10877             /* Consume the `...' token. */
10878             cp_lexer_consume_token (parser->lexer);
10879             maybe_warn_variadic_templates ();
10880
10881             *is_parameter_pack = true;
10882           }
10883
10884         /* If the next token is an identifier, then it names the
10885            parameter.  */
10886         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10887           identifier = cp_parser_identifier (parser);
10888         else
10889           identifier = NULL_TREE;
10890
10891         /* Create the parameter.  */
10892         parameter = finish_template_type_parm (class_type_node, identifier);
10893
10894         /* If the next token is an `=', we have a default argument.  */
10895         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10896           {
10897             /* Consume the `=' token.  */
10898             cp_lexer_consume_token (parser->lexer);
10899             /* Parse the default-argument.  */
10900             push_deferring_access_checks (dk_no_deferred);
10901             default_argument = cp_parser_type_id (parser);
10902
10903             /* Template parameter packs cannot have default
10904                arguments. */
10905             if (*is_parameter_pack)
10906               {
10907                 if (identifier)
10908                   error_at (token->location,
10909                             "template parameter pack %qD cannot have a "
10910                             "default argument", identifier);
10911                 else
10912                   error_at (token->location,
10913                             "template parameter packs cannot have "
10914                             "default arguments");
10915                 default_argument = NULL_TREE;
10916               }
10917             pop_deferring_access_checks ();
10918           }
10919         else
10920           default_argument = NULL_TREE;
10921
10922         /* Create the combined representation of the parameter and the
10923            default argument.  */
10924         parameter = build_tree_list (default_argument, parameter);
10925       }
10926       break;
10927
10928     case RID_TEMPLATE:
10929       {
10930         tree identifier;
10931         tree default_argument;
10932
10933         /* Look for the `<'.  */
10934         cp_parser_require (parser, CPP_LESS, RT_LESS);
10935         /* Parse the template-parameter-list.  */
10936         cp_parser_template_parameter_list (parser);
10937         /* Look for the `>'.  */
10938         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
10939         /* Look for the `class' keyword.  */
10940         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
10941         /* If the next token is an ellipsis, we have a template
10942            argument pack. */
10943         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10944           {
10945             /* Consume the `...' token. */
10946             cp_lexer_consume_token (parser->lexer);
10947             maybe_warn_variadic_templates ();
10948
10949             *is_parameter_pack = true;
10950           }
10951         /* If the next token is an `=', then there is a
10952            default-argument.  If the next token is a `>', we are at
10953            the end of the parameter-list.  If the next token is a `,',
10954            then we are at the end of this parameter.  */
10955         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10956             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10957             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10958           {
10959             identifier = cp_parser_identifier (parser);
10960             /* Treat invalid names as if the parameter were nameless.  */
10961             if (identifier == error_mark_node)
10962               identifier = NULL_TREE;
10963           }
10964         else
10965           identifier = NULL_TREE;
10966
10967         /* Create the template parameter.  */
10968         parameter = finish_template_template_parm (class_type_node,
10969                                                    identifier);
10970
10971         /* If the next token is an `=', then there is a
10972            default-argument.  */
10973         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10974           {
10975             bool is_template;
10976
10977             /* Consume the `='.  */
10978             cp_lexer_consume_token (parser->lexer);
10979             /* Parse the id-expression.  */
10980             push_deferring_access_checks (dk_no_deferred);
10981             /* save token before parsing the id-expression, for error
10982                reporting */
10983             token = cp_lexer_peek_token (parser->lexer);
10984             default_argument
10985               = cp_parser_id_expression (parser,
10986                                          /*template_keyword_p=*/false,
10987                                          /*check_dependency_p=*/true,
10988                                          /*template_p=*/&is_template,
10989                                          /*declarator_p=*/false,
10990                                          /*optional_p=*/false);
10991             if (TREE_CODE (default_argument) == TYPE_DECL)
10992               /* If the id-expression was a template-id that refers to
10993                  a template-class, we already have the declaration here,
10994                  so no further lookup is needed.  */
10995                  ;
10996             else
10997               /* Look up the name.  */
10998               default_argument
10999                 = cp_parser_lookup_name (parser, default_argument,
11000                                          none_type,
11001                                          /*is_template=*/is_template,
11002                                          /*is_namespace=*/false,
11003                                          /*check_dependency=*/true,
11004                                          /*ambiguous_decls=*/NULL,
11005                                          token->location);
11006             /* See if the default argument is valid.  */
11007             default_argument
11008               = check_template_template_default_arg (default_argument);
11009
11010             /* Template parameter packs cannot have default
11011                arguments. */
11012             if (*is_parameter_pack)
11013               {
11014                 if (identifier)
11015                   error_at (token->location,
11016                             "template parameter pack %qD cannot "
11017                             "have a default argument",
11018                             identifier);
11019                 else
11020                   error_at (token->location, "template parameter packs cannot "
11021                             "have default arguments");
11022                 default_argument = NULL_TREE;
11023               }
11024             pop_deferring_access_checks ();
11025           }
11026         else
11027           default_argument = NULL_TREE;
11028
11029         /* Create the combined representation of the parameter and the
11030            default argument.  */
11031         parameter = build_tree_list (default_argument, parameter);
11032       }
11033       break;
11034
11035     default:
11036       gcc_unreachable ();
11037       break;
11038     }
11039
11040   return parameter;
11041 }
11042
11043 /* Parse a template-id.
11044
11045    template-id:
11046      template-name < template-argument-list [opt] >
11047
11048    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11049    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11050    returned.  Otherwise, if the template-name names a function, or set
11051    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11052    names a class, returns a TYPE_DECL for the specialization.
11053
11054    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11055    uninstantiated templates.  */
11056
11057 static tree
11058 cp_parser_template_id (cp_parser *parser,
11059                        bool template_keyword_p,
11060                        bool check_dependency_p,
11061                        bool is_declaration)
11062 {
11063   int i;
11064   tree templ;
11065   tree arguments;
11066   tree template_id;
11067   cp_token_position start_of_id = 0;
11068   deferred_access_check *chk;
11069   VEC (deferred_access_check,gc) *access_check;
11070   cp_token *next_token = NULL, *next_token_2 = NULL;
11071   bool is_identifier;
11072
11073   /* If the next token corresponds to a template-id, there is no need
11074      to reparse it.  */
11075   next_token = cp_lexer_peek_token (parser->lexer);
11076   if (next_token->type == CPP_TEMPLATE_ID)
11077     {
11078       struct tree_check *check_value;
11079
11080       /* Get the stored value.  */
11081       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11082       /* Perform any access checks that were deferred.  */
11083       access_check = check_value->checks;
11084       if (access_check)
11085         {
11086           for (i = 0 ;
11087                VEC_iterate (deferred_access_check, access_check, i, chk) ;
11088                ++i)
11089             {
11090               perform_or_defer_access_check (chk->binfo,
11091                                              chk->decl,
11092                                              chk->diag_decl);
11093             }
11094         }
11095       /* Return the stored value.  */
11096       return check_value->value;
11097     }
11098
11099   /* Avoid performing name lookup if there is no possibility of
11100      finding a template-id.  */
11101   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11102       || (next_token->type == CPP_NAME
11103           && !cp_parser_nth_token_starts_template_argument_list_p
11104                (parser, 2)))
11105     {
11106       cp_parser_error (parser, "expected template-id");
11107       return error_mark_node;
11108     }
11109
11110   /* Remember where the template-id starts.  */
11111   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11112     start_of_id = cp_lexer_token_position (parser->lexer, false);
11113
11114   push_deferring_access_checks (dk_deferred);
11115
11116   /* Parse the template-name.  */
11117   is_identifier = false;
11118   templ = cp_parser_template_name (parser, template_keyword_p,
11119                                    check_dependency_p,
11120                                    is_declaration,
11121                                    &is_identifier);
11122   if (templ == error_mark_node || is_identifier)
11123     {
11124       pop_deferring_access_checks ();
11125       return templ;
11126     }
11127
11128   /* If we find the sequence `[:' after a template-name, it's probably
11129      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11130      parse correctly the argument list.  */
11131   next_token = cp_lexer_peek_token (parser->lexer);
11132   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11133   if (next_token->type == CPP_OPEN_SQUARE
11134       && next_token->flags & DIGRAPH
11135       && next_token_2->type == CPP_COLON
11136       && !(next_token_2->flags & PREV_WHITE))
11137     {
11138       cp_parser_parse_tentatively (parser);
11139       /* Change `:' into `::'.  */
11140       next_token_2->type = CPP_SCOPE;
11141       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11142          CPP_LESS.  */
11143       cp_lexer_consume_token (parser->lexer);
11144
11145       /* Parse the arguments.  */
11146       arguments = cp_parser_enclosed_template_argument_list (parser);
11147       if (!cp_parser_parse_definitely (parser))
11148         {
11149           /* If we couldn't parse an argument list, then we revert our changes
11150              and return simply an error. Maybe this is not a template-id
11151              after all.  */
11152           next_token_2->type = CPP_COLON;
11153           cp_parser_error (parser, "expected %<<%>");
11154           pop_deferring_access_checks ();
11155           return error_mark_node;
11156         }
11157       /* Otherwise, emit an error about the invalid digraph, but continue
11158          parsing because we got our argument list.  */
11159       if (permerror (next_token->location,
11160                      "%<<::%> cannot begin a template-argument list"))
11161         {
11162           static bool hint = false;
11163           inform (next_token->location,
11164                   "%<<:%> is an alternate spelling for %<[%>."
11165                   " Insert whitespace between %<<%> and %<::%>");
11166           if (!hint && !flag_permissive)
11167             {
11168               inform (next_token->location, "(if you use %<-fpermissive%>"
11169                       " G++ will accept your code)");
11170               hint = true;
11171             }
11172         }
11173     }
11174   else
11175     {
11176       /* Look for the `<' that starts the template-argument-list.  */
11177       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11178         {
11179           pop_deferring_access_checks ();
11180           return error_mark_node;
11181         }
11182       /* Parse the arguments.  */
11183       arguments = cp_parser_enclosed_template_argument_list (parser);
11184     }
11185
11186   /* Build a representation of the specialization.  */
11187   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11188     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11189   else if (DECL_CLASS_TEMPLATE_P (templ)
11190            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11191     {
11192       bool entering_scope;
11193       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11194          template (rather than some instantiation thereof) only if
11195          is not nested within some other construct.  For example, in
11196          "template <typename T> void f(T) { A<T>::", A<T> is just an
11197          instantiation of A.  */
11198       entering_scope = (template_parm_scope_p ()
11199                         && cp_lexer_next_token_is (parser->lexer,
11200                                                    CPP_SCOPE));
11201       template_id
11202         = finish_template_type (templ, arguments, entering_scope);
11203     }
11204   else
11205     {
11206       /* If it's not a class-template or a template-template, it should be
11207          a function-template.  */
11208       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11209                    || TREE_CODE (templ) == OVERLOAD
11210                    || BASELINK_P (templ)));
11211
11212       template_id = lookup_template_function (templ, arguments);
11213     }
11214
11215   /* If parsing tentatively, replace the sequence of tokens that makes
11216      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11217      should we re-parse the token stream, we will not have to repeat
11218      the effort required to do the parse, nor will we issue duplicate
11219      error messages about problems during instantiation of the
11220      template.  */
11221   if (start_of_id)
11222     {
11223       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11224
11225       /* Reset the contents of the START_OF_ID token.  */
11226       token->type = CPP_TEMPLATE_ID;
11227       /* Retrieve any deferred checks.  Do not pop this access checks yet
11228          so the memory will not be reclaimed during token replacing below.  */
11229       token->u.tree_check_value = GGC_CNEW (struct tree_check);
11230       token->u.tree_check_value->value = template_id;
11231       token->u.tree_check_value->checks = get_deferred_access_checks ();
11232       token->keyword = RID_MAX;
11233
11234       /* Purge all subsequent tokens.  */
11235       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11236
11237       /* ??? Can we actually assume that, if template_id ==
11238          error_mark_node, we will have issued a diagnostic to the
11239          user, as opposed to simply marking the tentative parse as
11240          failed?  */
11241       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11242         error_at (token->location, "parse error in template argument list");
11243     }
11244
11245   pop_deferring_access_checks ();
11246   return template_id;
11247 }
11248
11249 /* Parse a template-name.
11250
11251    template-name:
11252      identifier
11253
11254    The standard should actually say:
11255
11256    template-name:
11257      identifier
11258      operator-function-id
11259
11260    A defect report has been filed about this issue.
11261
11262    A conversion-function-id cannot be a template name because they cannot
11263    be part of a template-id. In fact, looking at this code:
11264
11265    a.operator K<int>()
11266
11267    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11268    It is impossible to call a templated conversion-function-id with an
11269    explicit argument list, since the only allowed template parameter is
11270    the type to which it is converting.
11271
11272    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11273    `template' keyword, in a construction like:
11274
11275      T::template f<3>()
11276
11277    In that case `f' is taken to be a template-name, even though there
11278    is no way of knowing for sure.
11279
11280    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11281    name refers to a set of overloaded functions, at least one of which
11282    is a template, or an IDENTIFIER_NODE with the name of the template,
11283    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11284    names are looked up inside uninstantiated templates.  */
11285
11286 static tree
11287 cp_parser_template_name (cp_parser* parser,
11288                          bool template_keyword_p,
11289                          bool check_dependency_p,
11290                          bool is_declaration,
11291                          bool *is_identifier)
11292 {
11293   tree identifier;
11294   tree decl;
11295   tree fns;
11296   cp_token *token = cp_lexer_peek_token (parser->lexer);
11297
11298   /* If the next token is `operator', then we have either an
11299      operator-function-id or a conversion-function-id.  */
11300   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11301     {
11302       /* We don't know whether we're looking at an
11303          operator-function-id or a conversion-function-id.  */
11304       cp_parser_parse_tentatively (parser);
11305       /* Try an operator-function-id.  */
11306       identifier = cp_parser_operator_function_id (parser);
11307       /* If that didn't work, try a conversion-function-id.  */
11308       if (!cp_parser_parse_definitely (parser))
11309         {
11310           cp_parser_error (parser, "expected template-name");
11311           return error_mark_node;
11312         }
11313     }
11314   /* Look for the identifier.  */
11315   else
11316     identifier = cp_parser_identifier (parser);
11317
11318   /* If we didn't find an identifier, we don't have a template-id.  */
11319   if (identifier == error_mark_node)
11320     return error_mark_node;
11321
11322   /* If the name immediately followed the `template' keyword, then it
11323      is a template-name.  However, if the next token is not `<', then
11324      we do not treat it as a template-name, since it is not being used
11325      as part of a template-id.  This enables us to handle constructs
11326      like:
11327
11328        template <typename T> struct S { S(); };
11329        template <typename T> S<T>::S();
11330
11331      correctly.  We would treat `S' as a template -- if it were `S<T>'
11332      -- but we do not if there is no `<'.  */
11333
11334   if (processing_template_decl
11335       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11336     {
11337       /* In a declaration, in a dependent context, we pretend that the
11338          "template" keyword was present in order to improve error
11339          recovery.  For example, given:
11340
11341            template <typename T> void f(T::X<int>);
11342
11343          we want to treat "X<int>" as a template-id.  */
11344       if (is_declaration
11345           && !template_keyword_p
11346           && parser->scope && TYPE_P (parser->scope)
11347           && check_dependency_p
11348           && dependent_scope_p (parser->scope)
11349           /* Do not do this for dtors (or ctors), since they never
11350              need the template keyword before their name.  */
11351           && !constructor_name_p (identifier, parser->scope))
11352         {
11353           cp_token_position start = 0;
11354
11355           /* Explain what went wrong.  */
11356           error_at (token->location, "non-template %qD used as template",
11357                     identifier);
11358           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11359                   parser->scope, identifier);
11360           /* If parsing tentatively, find the location of the "<" token.  */
11361           if (cp_parser_simulate_error (parser))
11362             start = cp_lexer_token_position (parser->lexer, true);
11363           /* Parse the template arguments so that we can issue error
11364              messages about them.  */
11365           cp_lexer_consume_token (parser->lexer);
11366           cp_parser_enclosed_template_argument_list (parser);
11367           /* Skip tokens until we find a good place from which to
11368              continue parsing.  */
11369           cp_parser_skip_to_closing_parenthesis (parser,
11370                                                  /*recovering=*/true,
11371                                                  /*or_comma=*/true,
11372                                                  /*consume_paren=*/false);
11373           /* If parsing tentatively, permanently remove the
11374              template argument list.  That will prevent duplicate
11375              error messages from being issued about the missing
11376              "template" keyword.  */
11377           if (start)
11378             cp_lexer_purge_tokens_after (parser->lexer, start);
11379           if (is_identifier)
11380             *is_identifier = true;
11381           return identifier;
11382         }
11383
11384       /* If the "template" keyword is present, then there is generally
11385          no point in doing name-lookup, so we just return IDENTIFIER.
11386          But, if the qualifying scope is non-dependent then we can
11387          (and must) do name-lookup normally.  */
11388       if (template_keyword_p
11389           && (!parser->scope
11390               || (TYPE_P (parser->scope)
11391                   && dependent_type_p (parser->scope))))
11392         return identifier;
11393     }
11394
11395   /* Look up the name.  */
11396   decl = cp_parser_lookup_name (parser, identifier,
11397                                 none_type,
11398                                 /*is_template=*/true,
11399                                 /*is_namespace=*/false,
11400                                 check_dependency_p,
11401                                 /*ambiguous_decls=*/NULL,
11402                                 token->location);
11403
11404   /* If DECL is a template, then the name was a template-name.  */
11405   if (TREE_CODE (decl) == TEMPLATE_DECL)
11406     ;
11407   else
11408     {
11409       tree fn = NULL_TREE;
11410
11411       /* The standard does not explicitly indicate whether a name that
11412          names a set of overloaded declarations, some of which are
11413          templates, is a template-name.  However, such a name should
11414          be a template-name; otherwise, there is no way to form a
11415          template-id for the overloaded templates.  */
11416       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11417       if (TREE_CODE (fns) == OVERLOAD)
11418         for (fn = fns; fn; fn = OVL_NEXT (fn))
11419           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11420             break;
11421
11422       if (!fn)
11423         {
11424           /* The name does not name a template.  */
11425           cp_parser_error (parser, "expected template-name");
11426           return error_mark_node;
11427         }
11428     }
11429
11430   /* If DECL is dependent, and refers to a function, then just return
11431      its name; we will look it up again during template instantiation.  */
11432   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11433     {
11434       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11435       if (TYPE_P (scope) && dependent_type_p (scope))
11436         return identifier;
11437     }
11438
11439   return decl;
11440 }
11441
11442 /* Parse a template-argument-list.
11443
11444    template-argument-list:
11445      template-argument ... [opt]
11446      template-argument-list , template-argument ... [opt]
11447
11448    Returns a TREE_VEC containing the arguments.  */
11449
11450 static tree
11451 cp_parser_template_argument_list (cp_parser* parser)
11452 {
11453   tree fixed_args[10];
11454   unsigned n_args = 0;
11455   unsigned alloced = 10;
11456   tree *arg_ary = fixed_args;
11457   tree vec;
11458   bool saved_in_template_argument_list_p;
11459   bool saved_ice_p;
11460   bool saved_non_ice_p;
11461
11462   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11463   parser->in_template_argument_list_p = true;
11464   /* Even if the template-id appears in an integral
11465      constant-expression, the contents of the argument list do
11466      not.  */
11467   saved_ice_p = parser->integral_constant_expression_p;
11468   parser->integral_constant_expression_p = false;
11469   saved_non_ice_p = parser->non_integral_constant_expression_p;
11470   parser->non_integral_constant_expression_p = false;
11471   /* Parse the arguments.  */
11472   do
11473     {
11474       tree argument;
11475
11476       if (n_args)
11477         /* Consume the comma.  */
11478         cp_lexer_consume_token (parser->lexer);
11479
11480       /* Parse the template-argument.  */
11481       argument = cp_parser_template_argument (parser);
11482
11483       /* If the next token is an ellipsis, we're expanding a template
11484          argument pack. */
11485       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11486         {
11487           if (argument == error_mark_node)
11488             {
11489               cp_token *token = cp_lexer_peek_token (parser->lexer);
11490               error_at (token->location,
11491                         "expected parameter pack before %<...%>");
11492             }
11493           /* Consume the `...' token. */
11494           cp_lexer_consume_token (parser->lexer);
11495
11496           /* Make the argument into a TYPE_PACK_EXPANSION or
11497              EXPR_PACK_EXPANSION. */
11498           argument = make_pack_expansion (argument);
11499         }
11500
11501       if (n_args == alloced)
11502         {
11503           alloced *= 2;
11504
11505           if (arg_ary == fixed_args)
11506             {
11507               arg_ary = XNEWVEC (tree, alloced);
11508               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11509             }
11510           else
11511             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11512         }
11513       arg_ary[n_args++] = argument;
11514     }
11515   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11516
11517   vec = make_tree_vec (n_args);
11518
11519   while (n_args--)
11520     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11521
11522   if (arg_ary != fixed_args)
11523     free (arg_ary);
11524   parser->non_integral_constant_expression_p = saved_non_ice_p;
11525   parser->integral_constant_expression_p = saved_ice_p;
11526   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11527 #ifdef ENABLE_CHECKING
11528   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11529 #endif
11530   return vec;
11531 }
11532
11533 /* Parse a template-argument.
11534
11535    template-argument:
11536      assignment-expression
11537      type-id
11538      id-expression
11539
11540    The representation is that of an assignment-expression, type-id, or
11541    id-expression -- except that the qualified id-expression is
11542    evaluated, so that the value returned is either a DECL or an
11543    OVERLOAD.
11544
11545    Although the standard says "assignment-expression", it forbids
11546    throw-expressions or assignments in the template argument.
11547    Therefore, we use "conditional-expression" instead.  */
11548
11549 static tree
11550 cp_parser_template_argument (cp_parser* parser)
11551 {
11552   tree argument;
11553   bool template_p;
11554   bool address_p;
11555   bool maybe_type_id = false;
11556   cp_token *token = NULL, *argument_start_token = NULL;
11557   cp_id_kind idk;
11558
11559   /* There's really no way to know what we're looking at, so we just
11560      try each alternative in order.
11561
11562        [temp.arg]
11563
11564        In a template-argument, an ambiguity between a type-id and an
11565        expression is resolved to a type-id, regardless of the form of
11566        the corresponding template-parameter.
11567
11568      Therefore, we try a type-id first.  */
11569   cp_parser_parse_tentatively (parser);
11570   argument = cp_parser_template_type_arg (parser);
11571   /* If there was no error parsing the type-id but the next token is a
11572      '>>', our behavior depends on which dialect of C++ we're
11573      parsing. In C++98, we probably found a typo for '> >'. But there
11574      are type-id which are also valid expressions. For instance:
11575
11576      struct X { int operator >> (int); };
11577      template <int V> struct Foo {};
11578      Foo<X () >> 5> r;
11579
11580      Here 'X()' is a valid type-id of a function type, but the user just
11581      wanted to write the expression "X() >> 5". Thus, we remember that we
11582      found a valid type-id, but we still try to parse the argument as an
11583      expression to see what happens. 
11584
11585      In C++0x, the '>>' will be considered two separate '>'
11586      tokens.  */
11587   if (!cp_parser_error_occurred (parser)
11588       && cxx_dialect == cxx98
11589       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11590     {
11591       maybe_type_id = true;
11592       cp_parser_abort_tentative_parse (parser);
11593     }
11594   else
11595     {
11596       /* If the next token isn't a `,' or a `>', then this argument wasn't
11597       really finished. This means that the argument is not a valid
11598       type-id.  */
11599       if (!cp_parser_next_token_ends_template_argument_p (parser))
11600         cp_parser_error (parser, "expected template-argument");
11601       /* If that worked, we're done.  */
11602       if (cp_parser_parse_definitely (parser))
11603         return argument;
11604     }
11605   /* We're still not sure what the argument will be.  */
11606   cp_parser_parse_tentatively (parser);
11607   /* Try a template.  */
11608   argument_start_token = cp_lexer_peek_token (parser->lexer);
11609   argument = cp_parser_id_expression (parser,
11610                                       /*template_keyword_p=*/false,
11611                                       /*check_dependency_p=*/true,
11612                                       &template_p,
11613                                       /*declarator_p=*/false,
11614                                       /*optional_p=*/false);
11615   /* If the next token isn't a `,' or a `>', then this argument wasn't
11616      really finished.  */
11617   if (!cp_parser_next_token_ends_template_argument_p (parser))
11618     cp_parser_error (parser, "expected template-argument");
11619   if (!cp_parser_error_occurred (parser))
11620     {
11621       /* Figure out what is being referred to.  If the id-expression
11622          was for a class template specialization, then we will have a
11623          TYPE_DECL at this point.  There is no need to do name lookup
11624          at this point in that case.  */
11625       if (TREE_CODE (argument) != TYPE_DECL)
11626         argument = cp_parser_lookup_name (parser, argument,
11627                                           none_type,
11628                                           /*is_template=*/template_p,
11629                                           /*is_namespace=*/false,
11630                                           /*check_dependency=*/true,
11631                                           /*ambiguous_decls=*/NULL,
11632                                           argument_start_token->location);
11633       if (TREE_CODE (argument) != TEMPLATE_DECL
11634           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11635         cp_parser_error (parser, "expected template-name");
11636     }
11637   if (cp_parser_parse_definitely (parser))
11638     return argument;
11639   /* It must be a non-type argument.  There permitted cases are given
11640      in [temp.arg.nontype]:
11641
11642      -- an integral constant-expression of integral or enumeration
11643         type; or
11644
11645      -- the name of a non-type template-parameter; or
11646
11647      -- the name of an object or function with external linkage...
11648
11649      -- the address of an object or function with external linkage...
11650
11651      -- a pointer to member...  */
11652   /* Look for a non-type template parameter.  */
11653   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11654     {
11655       cp_parser_parse_tentatively (parser);
11656       argument = cp_parser_primary_expression (parser,
11657                                                /*address_p=*/false,
11658                                                /*cast_p=*/false,
11659                                                /*template_arg_p=*/true,
11660                                                &idk);
11661       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11662           || !cp_parser_next_token_ends_template_argument_p (parser))
11663         cp_parser_simulate_error (parser);
11664       if (cp_parser_parse_definitely (parser))
11665         return argument;
11666     }
11667
11668   /* If the next token is "&", the argument must be the address of an
11669      object or function with external linkage.  */
11670   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11671   if (address_p)
11672     cp_lexer_consume_token (parser->lexer);
11673   /* See if we might have an id-expression.  */
11674   token = cp_lexer_peek_token (parser->lexer);
11675   if (token->type == CPP_NAME
11676       || token->keyword == RID_OPERATOR
11677       || token->type == CPP_SCOPE
11678       || token->type == CPP_TEMPLATE_ID
11679       || token->type == CPP_NESTED_NAME_SPECIFIER)
11680     {
11681       cp_parser_parse_tentatively (parser);
11682       argument = cp_parser_primary_expression (parser,
11683                                                address_p,
11684                                                /*cast_p=*/false,
11685                                                /*template_arg_p=*/true,
11686                                                &idk);
11687       if (cp_parser_error_occurred (parser)
11688           || !cp_parser_next_token_ends_template_argument_p (parser))
11689         cp_parser_abort_tentative_parse (parser);
11690       else
11691         {
11692           tree probe;
11693
11694           if (TREE_CODE (argument) == INDIRECT_REF)
11695             {
11696               gcc_assert (REFERENCE_REF_P (argument));
11697               argument = TREE_OPERAND (argument, 0);
11698             }
11699
11700           /* If we're in a template, we represent a qualified-id referring
11701              to a static data member as a SCOPE_REF even if the scope isn't
11702              dependent so that we can check access control later.  */
11703           probe = argument;
11704           if (TREE_CODE (probe) == SCOPE_REF)
11705             probe = TREE_OPERAND (probe, 1);
11706           if (TREE_CODE (probe) == VAR_DECL)
11707             {
11708               /* A variable without external linkage might still be a
11709                  valid constant-expression, so no error is issued here
11710                  if the external-linkage check fails.  */
11711               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11712                 cp_parser_simulate_error (parser);
11713             }
11714           else if (is_overloaded_fn (argument))
11715             /* All overloaded functions are allowed; if the external
11716                linkage test does not pass, an error will be issued
11717                later.  */
11718             ;
11719           else if (address_p
11720                    && (TREE_CODE (argument) == OFFSET_REF
11721                        || TREE_CODE (argument) == SCOPE_REF))
11722             /* A pointer-to-member.  */
11723             ;
11724           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11725             ;
11726           else
11727             cp_parser_simulate_error (parser);
11728
11729           if (cp_parser_parse_definitely (parser))
11730             {
11731               if (address_p)
11732                 argument = build_x_unary_op (ADDR_EXPR, argument,
11733                                              tf_warning_or_error);
11734               return argument;
11735             }
11736         }
11737     }
11738   /* If the argument started with "&", there are no other valid
11739      alternatives at this point.  */
11740   if (address_p)
11741     {
11742       cp_parser_error (parser, "invalid non-type template argument");
11743       return error_mark_node;
11744     }
11745
11746   /* If the argument wasn't successfully parsed as a type-id followed
11747      by '>>', the argument can only be a constant expression now.
11748      Otherwise, we try parsing the constant-expression tentatively,
11749      because the argument could really be a type-id.  */
11750   if (maybe_type_id)
11751     cp_parser_parse_tentatively (parser);
11752   argument = cp_parser_constant_expression (parser,
11753                                             /*allow_non_constant_p=*/false,
11754                                             /*non_constant_p=*/NULL);
11755   argument = fold_non_dependent_expr (argument);
11756   if (!maybe_type_id)
11757     return argument;
11758   if (!cp_parser_next_token_ends_template_argument_p (parser))
11759     cp_parser_error (parser, "expected template-argument");
11760   if (cp_parser_parse_definitely (parser))
11761     return argument;
11762   /* We did our best to parse the argument as a non type-id, but that
11763      was the only alternative that matched (albeit with a '>' after
11764      it). We can assume it's just a typo from the user, and a
11765      diagnostic will then be issued.  */
11766   return cp_parser_template_type_arg (parser);
11767 }
11768
11769 /* Parse an explicit-instantiation.
11770
11771    explicit-instantiation:
11772      template declaration
11773
11774    Although the standard says `declaration', what it really means is:
11775
11776    explicit-instantiation:
11777      template decl-specifier-seq [opt] declarator [opt] ;
11778
11779    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11780    supposed to be allowed.  A defect report has been filed about this
11781    issue.
11782
11783    GNU Extension:
11784
11785    explicit-instantiation:
11786      storage-class-specifier template
11787        decl-specifier-seq [opt] declarator [opt] ;
11788      function-specifier template
11789        decl-specifier-seq [opt] declarator [opt] ;  */
11790
11791 static void
11792 cp_parser_explicit_instantiation (cp_parser* parser)
11793 {
11794   int declares_class_or_enum;
11795   cp_decl_specifier_seq decl_specifiers;
11796   tree extension_specifier = NULL_TREE;
11797
11798   /* Look for an (optional) storage-class-specifier or
11799      function-specifier.  */
11800   if (cp_parser_allow_gnu_extensions_p (parser))
11801     {
11802       extension_specifier
11803         = cp_parser_storage_class_specifier_opt (parser);
11804       if (!extension_specifier)
11805         extension_specifier
11806           = cp_parser_function_specifier_opt (parser,
11807                                               /*decl_specs=*/NULL);
11808     }
11809
11810   /* Look for the `template' keyword.  */
11811   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
11812   /* Let the front end know that we are processing an explicit
11813      instantiation.  */
11814   begin_explicit_instantiation ();
11815   /* [temp.explicit] says that we are supposed to ignore access
11816      control while processing explicit instantiation directives.  */
11817   push_deferring_access_checks (dk_no_check);
11818   /* Parse a decl-specifier-seq.  */
11819   cp_parser_decl_specifier_seq (parser,
11820                                 CP_PARSER_FLAGS_OPTIONAL,
11821                                 &decl_specifiers,
11822                                 &declares_class_or_enum);
11823   /* If there was exactly one decl-specifier, and it declared a class,
11824      and there's no declarator, then we have an explicit type
11825      instantiation.  */
11826   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11827     {
11828       tree type;
11829
11830       type = check_tag_decl (&decl_specifiers);
11831       /* Turn access control back on for names used during
11832          template instantiation.  */
11833       pop_deferring_access_checks ();
11834       if (type)
11835         do_type_instantiation (type, extension_specifier,
11836                                /*complain=*/tf_error);
11837     }
11838   else
11839     {
11840       cp_declarator *declarator;
11841       tree decl;
11842
11843       /* Parse the declarator.  */
11844       declarator
11845         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11846                                 /*ctor_dtor_or_conv_p=*/NULL,
11847                                 /*parenthesized_p=*/NULL,
11848                                 /*member_p=*/false);
11849       if (declares_class_or_enum & 2)
11850         cp_parser_check_for_definition_in_return_type (declarator,
11851                                                        decl_specifiers.type,
11852                                                        decl_specifiers.type_location);
11853       if (declarator != cp_error_declarator)
11854         {
11855           decl = grokdeclarator (declarator, &decl_specifiers,
11856                                  NORMAL, 0, &decl_specifiers.attributes);
11857           /* Turn access control back on for names used during
11858              template instantiation.  */
11859           pop_deferring_access_checks ();
11860           /* Do the explicit instantiation.  */
11861           do_decl_instantiation (decl, extension_specifier);
11862         }
11863       else
11864         {
11865           pop_deferring_access_checks ();
11866           /* Skip the body of the explicit instantiation.  */
11867           cp_parser_skip_to_end_of_statement (parser);
11868         }
11869     }
11870   /* We're done with the instantiation.  */
11871   end_explicit_instantiation ();
11872
11873   cp_parser_consume_semicolon_at_end_of_statement (parser);
11874 }
11875
11876 /* Parse an explicit-specialization.
11877
11878    explicit-specialization:
11879      template < > declaration
11880
11881    Although the standard says `declaration', what it really means is:
11882
11883    explicit-specialization:
11884      template <> decl-specifier [opt] init-declarator [opt] ;
11885      template <> function-definition
11886      template <> explicit-specialization
11887      template <> template-declaration  */
11888
11889 static void
11890 cp_parser_explicit_specialization (cp_parser* parser)
11891 {
11892   bool need_lang_pop;
11893   cp_token *token = cp_lexer_peek_token (parser->lexer);
11894
11895   /* Look for the `template' keyword.  */
11896   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
11897   /* Look for the `<'.  */
11898   cp_parser_require (parser, CPP_LESS, RT_LESS);
11899   /* Look for the `>'.  */
11900   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11901   /* We have processed another parameter list.  */
11902   ++parser->num_template_parameter_lists;
11903   /* [temp]
11904
11905      A template ... explicit specialization ... shall not have C
11906      linkage.  */
11907   if (current_lang_name == lang_name_c)
11908     {
11909       error_at (token->location, "template specialization with C linkage");
11910       /* Give it C++ linkage to avoid confusing other parts of the
11911          front end.  */
11912       push_lang_context (lang_name_cplusplus);
11913       need_lang_pop = true;
11914     }
11915   else
11916     need_lang_pop = false;
11917   /* Let the front end know that we are beginning a specialization.  */
11918   if (!begin_specialization ())
11919     {
11920       end_specialization ();
11921       return;
11922     }
11923
11924   /* If the next keyword is `template', we need to figure out whether
11925      or not we're looking a template-declaration.  */
11926   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11927     {
11928       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11929           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11930         cp_parser_template_declaration_after_export (parser,
11931                                                      /*member_p=*/false);
11932       else
11933         cp_parser_explicit_specialization (parser);
11934     }
11935   else
11936     /* Parse the dependent declaration.  */
11937     cp_parser_single_declaration (parser,
11938                                   /*checks=*/NULL,
11939                                   /*member_p=*/false,
11940                                   /*explicit_specialization_p=*/true,
11941                                   /*friend_p=*/NULL);
11942   /* We're done with the specialization.  */
11943   end_specialization ();
11944   /* For the erroneous case of a template with C linkage, we pushed an
11945      implicit C++ linkage scope; exit that scope now.  */
11946   if (need_lang_pop)
11947     pop_lang_context ();
11948   /* We're done with this parameter list.  */
11949   --parser->num_template_parameter_lists;
11950 }
11951
11952 /* Parse a type-specifier.
11953
11954    type-specifier:
11955      simple-type-specifier
11956      class-specifier
11957      enum-specifier
11958      elaborated-type-specifier
11959      cv-qualifier
11960
11961    GNU Extension:
11962
11963    type-specifier:
11964      __complex__
11965
11966    Returns a representation of the type-specifier.  For a
11967    class-specifier, enum-specifier, or elaborated-type-specifier, a
11968    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11969
11970    The parser flags FLAGS is used to control type-specifier parsing.
11971
11972    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11973    in a decl-specifier-seq.
11974
11975    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11976    class-specifier, enum-specifier, or elaborated-type-specifier, then
11977    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11978    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11979    zero.
11980
11981    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11982    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11983    is set to FALSE.  */
11984
11985 static tree
11986 cp_parser_type_specifier (cp_parser* parser,
11987                           cp_parser_flags flags,
11988                           cp_decl_specifier_seq *decl_specs,
11989                           bool is_declaration,
11990                           int* declares_class_or_enum,
11991                           bool* is_cv_qualifier)
11992 {
11993   tree type_spec = NULL_TREE;
11994   cp_token *token;
11995   enum rid keyword;
11996   cp_decl_spec ds = ds_last;
11997
11998   /* Assume this type-specifier does not declare a new type.  */
11999   if (declares_class_or_enum)
12000     *declares_class_or_enum = 0;
12001   /* And that it does not specify a cv-qualifier.  */
12002   if (is_cv_qualifier)
12003     *is_cv_qualifier = false;
12004   /* Peek at the next token.  */
12005   token = cp_lexer_peek_token (parser->lexer);
12006
12007   /* If we're looking at a keyword, we can use that to guide the
12008      production we choose.  */
12009   keyword = token->keyword;
12010   switch (keyword)
12011     {
12012     case RID_ENUM:
12013       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12014         goto elaborated_type_specifier;
12015
12016       /* Look for the enum-specifier.  */
12017       type_spec = cp_parser_enum_specifier (parser);
12018       /* If that worked, we're done.  */
12019       if (type_spec)
12020         {
12021           if (declares_class_or_enum)
12022             *declares_class_or_enum = 2;
12023           if (decl_specs)
12024             cp_parser_set_decl_spec_type (decl_specs,
12025                                           type_spec,
12026                                           token->location,
12027                                           /*user_defined_p=*/true);
12028           return type_spec;
12029         }
12030       else
12031         goto elaborated_type_specifier;
12032
12033       /* Any of these indicate either a class-specifier, or an
12034          elaborated-type-specifier.  */
12035     case RID_CLASS:
12036     case RID_STRUCT:
12037     case RID_UNION:
12038       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12039         goto elaborated_type_specifier;
12040
12041       /* Parse tentatively so that we can back up if we don't find a
12042          class-specifier.  */
12043       cp_parser_parse_tentatively (parser);
12044       /* Look for the class-specifier.  */
12045       type_spec = cp_parser_class_specifier (parser);
12046       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12047       /* If that worked, we're done.  */
12048       if (cp_parser_parse_definitely (parser))
12049         {
12050           if (declares_class_or_enum)
12051             *declares_class_or_enum = 2;
12052           if (decl_specs)
12053             cp_parser_set_decl_spec_type (decl_specs,
12054                                           type_spec,
12055                                           token->location,
12056                                           /*user_defined_p=*/true);
12057           return type_spec;
12058         }
12059
12060       /* Fall through.  */
12061     elaborated_type_specifier:
12062       /* We're declaring (not defining) a class or enum.  */
12063       if (declares_class_or_enum)
12064         *declares_class_or_enum = 1;
12065
12066       /* Fall through.  */
12067     case RID_TYPENAME:
12068       /* Look for an elaborated-type-specifier.  */
12069       type_spec
12070         = (cp_parser_elaborated_type_specifier
12071            (parser,
12072             decl_specs && decl_specs->specs[(int) ds_friend],
12073             is_declaration));
12074       if (decl_specs)
12075         cp_parser_set_decl_spec_type (decl_specs,
12076                                       type_spec,
12077                                       token->location,
12078                                       /*user_defined_p=*/true);
12079       return type_spec;
12080
12081     case RID_CONST:
12082       ds = ds_const;
12083       if (is_cv_qualifier)
12084         *is_cv_qualifier = true;
12085       break;
12086
12087     case RID_VOLATILE:
12088       ds = ds_volatile;
12089       if (is_cv_qualifier)
12090         *is_cv_qualifier = true;
12091       break;
12092
12093     case RID_RESTRICT:
12094       ds = ds_restrict;
12095       if (is_cv_qualifier)
12096         *is_cv_qualifier = true;
12097       break;
12098
12099     case RID_COMPLEX:
12100       /* The `__complex__' keyword is a GNU extension.  */
12101       ds = ds_complex;
12102       break;
12103
12104     default:
12105       break;
12106     }
12107
12108   /* Handle simple keywords.  */
12109   if (ds != ds_last)
12110     {
12111       if (decl_specs)
12112         {
12113           ++decl_specs->specs[(int)ds];
12114           decl_specs->any_specifiers_p = true;
12115         }
12116       return cp_lexer_consume_token (parser->lexer)->u.value;
12117     }
12118
12119   /* If we do not already have a type-specifier, assume we are looking
12120      at a simple-type-specifier.  */
12121   type_spec = cp_parser_simple_type_specifier (parser,
12122                                                decl_specs,
12123                                                flags);
12124
12125   /* If we didn't find a type-specifier, and a type-specifier was not
12126      optional in this context, issue an error message.  */
12127   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12128     {
12129       cp_parser_error (parser, "expected type specifier");
12130       return error_mark_node;
12131     }
12132
12133   return type_spec;
12134 }
12135
12136 /* Parse a simple-type-specifier.
12137
12138    simple-type-specifier:
12139      :: [opt] nested-name-specifier [opt] type-name
12140      :: [opt] nested-name-specifier template template-id
12141      char
12142      wchar_t
12143      bool
12144      short
12145      int
12146      long
12147      signed
12148      unsigned
12149      float
12150      double
12151      void
12152
12153    C++0x Extension:
12154
12155    simple-type-specifier:
12156      auto
12157      decltype ( expression )   
12158      char16_t
12159      char32_t
12160
12161    GNU Extension:
12162
12163    simple-type-specifier:
12164      __typeof__ unary-expression
12165      __typeof__ ( type-id )
12166
12167    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12168    appropriately updated.  */
12169
12170 static tree
12171 cp_parser_simple_type_specifier (cp_parser* parser,
12172                                  cp_decl_specifier_seq *decl_specs,
12173                                  cp_parser_flags flags)
12174 {
12175   tree type = NULL_TREE;
12176   cp_token *token;
12177
12178   /* Peek at the next token.  */
12179   token = cp_lexer_peek_token (parser->lexer);
12180
12181   /* If we're looking at a keyword, things are easy.  */
12182   switch (token->keyword)
12183     {
12184     case RID_CHAR:
12185       if (decl_specs)
12186         decl_specs->explicit_char_p = true;
12187       type = char_type_node;
12188       break;
12189     case RID_CHAR16:
12190       type = char16_type_node;
12191       break;
12192     case RID_CHAR32:
12193       type = char32_type_node;
12194       break;
12195     case RID_WCHAR:
12196       type = wchar_type_node;
12197       break;
12198     case RID_BOOL:
12199       type = boolean_type_node;
12200       break;
12201     case RID_SHORT:
12202       if (decl_specs)
12203         ++decl_specs->specs[(int) ds_short];
12204       type = short_integer_type_node;
12205       break;
12206     case RID_INT:
12207       if (decl_specs)
12208         decl_specs->explicit_int_p = true;
12209       type = integer_type_node;
12210       break;
12211     case RID_LONG:
12212       if (decl_specs)
12213         ++decl_specs->specs[(int) ds_long];
12214       type = long_integer_type_node;
12215       break;
12216     case RID_SIGNED:
12217       if (decl_specs)
12218         ++decl_specs->specs[(int) ds_signed];
12219       type = integer_type_node;
12220       break;
12221     case RID_UNSIGNED:
12222       if (decl_specs)
12223         ++decl_specs->specs[(int) ds_unsigned];
12224       type = unsigned_type_node;
12225       break;
12226     case RID_FLOAT:
12227       type = float_type_node;
12228       break;
12229     case RID_DOUBLE:
12230       type = double_type_node;
12231       break;
12232     case RID_VOID:
12233       type = void_type_node;
12234       break;
12235       
12236     case RID_AUTO:
12237       maybe_warn_cpp0x (CPP0X_AUTO);
12238       type = make_auto ();
12239       break;
12240
12241     case RID_DECLTYPE:
12242       /* Parse the `decltype' type.  */
12243       type = cp_parser_decltype (parser);
12244
12245       if (decl_specs)
12246         cp_parser_set_decl_spec_type (decl_specs, type,
12247                                       token->location,
12248                                       /*user_defined_p=*/true);
12249
12250       return type;
12251
12252     case RID_TYPEOF:
12253       /* Consume the `typeof' token.  */
12254       cp_lexer_consume_token (parser->lexer);
12255       /* Parse the operand to `typeof'.  */
12256       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12257       /* If it is not already a TYPE, take its type.  */
12258       if (!TYPE_P (type))
12259         type = finish_typeof (type);
12260
12261       if (decl_specs)
12262         cp_parser_set_decl_spec_type (decl_specs, type,
12263                                       token->location,
12264                                       /*user_defined_p=*/true);
12265
12266       return type;
12267
12268     default:
12269       break;
12270     }
12271
12272   /* If the type-specifier was for a built-in type, we're done.  */
12273   if (type)
12274     {
12275       /* Record the type.  */
12276       if (decl_specs
12277           && (token->keyword != RID_SIGNED
12278               && token->keyword != RID_UNSIGNED
12279               && token->keyword != RID_SHORT
12280               && token->keyword != RID_LONG))
12281         cp_parser_set_decl_spec_type (decl_specs,
12282                                       type,
12283                                       token->location,
12284                                       /*user_defined=*/false);
12285       if (decl_specs)
12286         decl_specs->any_specifiers_p = true;
12287
12288       /* Consume the token.  */
12289       cp_lexer_consume_token (parser->lexer);
12290
12291       /* There is no valid C++ program where a non-template type is
12292          followed by a "<".  That usually indicates that the user thought
12293          that the type was a template.  */
12294       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12295
12296       return TYPE_NAME (type);
12297     }
12298
12299   /* The type-specifier must be a user-defined type.  */
12300   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12301     {
12302       bool qualified_p;
12303       bool global_p;
12304
12305       /* Don't gobble tokens or issue error messages if this is an
12306          optional type-specifier.  */
12307       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12308         cp_parser_parse_tentatively (parser);
12309
12310       /* Look for the optional `::' operator.  */
12311       global_p
12312         = (cp_parser_global_scope_opt (parser,
12313                                        /*current_scope_valid_p=*/false)
12314            != NULL_TREE);
12315       /* Look for the nested-name specifier.  */
12316       qualified_p
12317         = (cp_parser_nested_name_specifier_opt (parser,
12318                                                 /*typename_keyword_p=*/false,
12319                                                 /*check_dependency_p=*/true,
12320                                                 /*type_p=*/false,
12321                                                 /*is_declaration=*/false)
12322            != NULL_TREE);
12323       token = cp_lexer_peek_token (parser->lexer);
12324       /* If we have seen a nested-name-specifier, and the next token
12325          is `template', then we are using the template-id production.  */
12326       if (parser->scope
12327           && cp_parser_optional_template_keyword (parser))
12328         {
12329           /* Look for the template-id.  */
12330           type = cp_parser_template_id (parser,
12331                                         /*template_keyword_p=*/true,
12332                                         /*check_dependency_p=*/true,
12333                                         /*is_declaration=*/false);
12334           /* If the template-id did not name a type, we are out of
12335              luck.  */
12336           if (TREE_CODE (type) != TYPE_DECL)
12337             {
12338               cp_parser_error (parser, "expected template-id for type");
12339               type = NULL_TREE;
12340             }
12341         }
12342       /* Otherwise, look for a type-name.  */
12343       else
12344         type = cp_parser_type_name (parser);
12345       /* Keep track of all name-lookups performed in class scopes.  */
12346       if (type
12347           && !global_p
12348           && !qualified_p
12349           && TREE_CODE (type) == TYPE_DECL
12350           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12351         maybe_note_name_used_in_class (DECL_NAME (type), type);
12352       /* If it didn't work out, we don't have a TYPE.  */
12353       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12354           && !cp_parser_parse_definitely (parser))
12355         type = NULL_TREE;
12356       if (type && decl_specs)
12357         cp_parser_set_decl_spec_type (decl_specs, type,
12358                                       token->location,
12359                                       /*user_defined=*/true);
12360     }
12361
12362   /* If we didn't get a type-name, issue an error message.  */
12363   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12364     {
12365       cp_parser_error (parser, "expected type-name");
12366       return error_mark_node;
12367     }
12368
12369   /* There is no valid C++ program where a non-template type is
12370      followed by a "<".  That usually indicates that the user thought
12371      that the type was a template.  */
12372   if (type && type != error_mark_node)
12373     {
12374       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12375          If it is, then the '<'...'>' enclose protocol names rather than
12376          template arguments, and so everything is fine.  */
12377       if (c_dialect_objc ()
12378           && (objc_is_id (type) || objc_is_class_name (type)))
12379         {
12380           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12381           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12382
12383           /* Clobber the "unqualified" type previously entered into
12384              DECL_SPECS with the new, improved protocol-qualified version.  */
12385           if (decl_specs)
12386             decl_specs->type = qual_type;
12387
12388           return qual_type;
12389         }
12390
12391       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12392                                                token->location);
12393     }
12394
12395   return type;
12396 }
12397
12398 /* Parse a type-name.
12399
12400    type-name:
12401      class-name
12402      enum-name
12403      typedef-name
12404
12405    enum-name:
12406      identifier
12407
12408    typedef-name:
12409      identifier
12410
12411    Returns a TYPE_DECL for the type.  */
12412
12413 static tree
12414 cp_parser_type_name (cp_parser* parser)
12415 {
12416   tree type_decl;
12417
12418   /* We can't know yet whether it is a class-name or not.  */
12419   cp_parser_parse_tentatively (parser);
12420   /* Try a class-name.  */
12421   type_decl = cp_parser_class_name (parser,
12422                                     /*typename_keyword_p=*/false,
12423                                     /*template_keyword_p=*/false,
12424                                     none_type,
12425                                     /*check_dependency_p=*/true,
12426                                     /*class_head_p=*/false,
12427                                     /*is_declaration=*/false);
12428   /* If it's not a class-name, keep looking.  */
12429   if (!cp_parser_parse_definitely (parser))
12430     {
12431       /* It must be a typedef-name or an enum-name.  */
12432       return cp_parser_nonclass_name (parser);
12433     }
12434
12435   return type_decl;
12436 }
12437
12438 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12439
12440    enum-name:
12441      identifier
12442
12443    typedef-name:
12444      identifier
12445
12446    Returns a TYPE_DECL for the type.  */
12447
12448 static tree
12449 cp_parser_nonclass_name (cp_parser* parser)
12450 {
12451   tree type_decl;
12452   tree identifier;
12453
12454   cp_token *token = cp_lexer_peek_token (parser->lexer);
12455   identifier = cp_parser_identifier (parser);
12456   if (identifier == error_mark_node)
12457     return error_mark_node;
12458
12459   /* Look up the type-name.  */
12460   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12461
12462   if (TREE_CODE (type_decl) != TYPE_DECL
12463       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12464     {
12465       /* See if this is an Objective-C type.  */
12466       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12467       tree type = objc_get_protocol_qualified_type (identifier, protos);
12468       if (type)
12469         type_decl = TYPE_NAME (type);
12470     }
12471   
12472   /* Issue an error if we did not find a type-name.  */
12473   if (TREE_CODE (type_decl) != TYPE_DECL)
12474     {
12475       if (!cp_parser_simulate_error (parser))
12476         cp_parser_name_lookup_error (parser, identifier, type_decl,
12477                                      NLE_TYPE, token->location);
12478       return error_mark_node;
12479     }
12480   /* Remember that the name was used in the definition of the
12481      current class so that we can check later to see if the
12482      meaning would have been different after the class was
12483      entirely defined.  */
12484   else if (type_decl != error_mark_node
12485            && !parser->scope)
12486     maybe_note_name_used_in_class (identifier, type_decl);
12487   
12488   return type_decl;
12489 }
12490
12491 /* Parse an elaborated-type-specifier.  Note that the grammar given
12492    here incorporates the resolution to DR68.
12493
12494    elaborated-type-specifier:
12495      class-key :: [opt] nested-name-specifier [opt] identifier
12496      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12497      enum-key :: [opt] nested-name-specifier [opt] identifier
12498      typename :: [opt] nested-name-specifier identifier
12499      typename :: [opt] nested-name-specifier template [opt]
12500        template-id
12501
12502    GNU extension:
12503
12504    elaborated-type-specifier:
12505      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12506      class-key attributes :: [opt] nested-name-specifier [opt]
12507                template [opt] template-id
12508      enum attributes :: [opt] nested-name-specifier [opt] identifier
12509
12510    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12511    declared `friend'.  If IS_DECLARATION is TRUE, then this
12512    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12513    something is being declared.
12514
12515    Returns the TYPE specified.  */
12516
12517 static tree
12518 cp_parser_elaborated_type_specifier (cp_parser* parser,
12519                                      bool is_friend,
12520                                      bool is_declaration)
12521 {
12522   enum tag_types tag_type;
12523   tree identifier;
12524   tree type = NULL_TREE;
12525   tree attributes = NULL_TREE;
12526   tree globalscope;
12527   cp_token *token = NULL;
12528
12529   /* See if we're looking at the `enum' keyword.  */
12530   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12531     {
12532       /* Consume the `enum' token.  */
12533       cp_lexer_consume_token (parser->lexer);
12534       /* Remember that it's an enumeration type.  */
12535       tag_type = enum_type;
12536       /* Parse the optional `struct' or `class' key (for C++0x scoped
12537          enums).  */
12538       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12539           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12540         {
12541           if (cxx_dialect == cxx98)
12542             maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12543
12544           /* Consume the `struct' or `class'.  */
12545           cp_lexer_consume_token (parser->lexer);
12546         }
12547       /* Parse the attributes.  */
12548       attributes = cp_parser_attributes_opt (parser);
12549     }
12550   /* Or, it might be `typename'.  */
12551   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12552                                            RID_TYPENAME))
12553     {
12554       /* Consume the `typename' token.  */
12555       cp_lexer_consume_token (parser->lexer);
12556       /* Remember that it's a `typename' type.  */
12557       tag_type = typename_type;
12558     }
12559   /* Otherwise it must be a class-key.  */
12560   else
12561     {
12562       tag_type = cp_parser_class_key (parser);
12563       if (tag_type == none_type)
12564         return error_mark_node;
12565       /* Parse the attributes.  */
12566       attributes = cp_parser_attributes_opt (parser);
12567     }
12568
12569   /* Look for the `::' operator.  */
12570   globalscope =  cp_parser_global_scope_opt (parser,
12571                                              /*current_scope_valid_p=*/false);
12572   /* Look for the nested-name-specifier.  */
12573   if (tag_type == typename_type && !globalscope)
12574     {
12575       if (!cp_parser_nested_name_specifier (parser,
12576                                            /*typename_keyword_p=*/true,
12577                                            /*check_dependency_p=*/true,
12578                                            /*type_p=*/true,
12579                                             is_declaration))
12580         return error_mark_node;
12581     }
12582   else
12583     /* Even though `typename' is not present, the proposed resolution
12584        to Core Issue 180 says that in `class A<T>::B', `B' should be
12585        considered a type-name, even if `A<T>' is dependent.  */
12586     cp_parser_nested_name_specifier_opt (parser,
12587                                          /*typename_keyword_p=*/true,
12588                                          /*check_dependency_p=*/true,
12589                                          /*type_p=*/true,
12590                                          is_declaration);
12591  /* For everything but enumeration types, consider a template-id.
12592     For an enumeration type, consider only a plain identifier.  */
12593   if (tag_type != enum_type)
12594     {
12595       bool template_p = false;
12596       tree decl;
12597
12598       /* Allow the `template' keyword.  */
12599       template_p = cp_parser_optional_template_keyword (parser);
12600       /* If we didn't see `template', we don't know if there's a
12601          template-id or not.  */
12602       if (!template_p)
12603         cp_parser_parse_tentatively (parser);
12604       /* Parse the template-id.  */
12605       token = cp_lexer_peek_token (parser->lexer);
12606       decl = cp_parser_template_id (parser, template_p,
12607                                     /*check_dependency_p=*/true,
12608                                     is_declaration);
12609       /* If we didn't find a template-id, look for an ordinary
12610          identifier.  */
12611       if (!template_p && !cp_parser_parse_definitely (parser))
12612         ;
12613       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12614          in effect, then we must assume that, upon instantiation, the
12615          template will correspond to a class.  */
12616       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12617                && tag_type == typename_type)
12618         type = make_typename_type (parser->scope, decl,
12619                                    typename_type,
12620                                    /*complain=*/tf_error);
12621       /* If the `typename' keyword is in effect and DECL is not a type
12622          decl. Then type is non existant.   */
12623       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12624         type = NULL_TREE; 
12625       else 
12626         type = TREE_TYPE (decl);
12627     }
12628
12629   if (!type)
12630     {
12631       token = cp_lexer_peek_token (parser->lexer);
12632       identifier = cp_parser_identifier (parser);
12633
12634       if (identifier == error_mark_node)
12635         {
12636           parser->scope = NULL_TREE;
12637           return error_mark_node;
12638         }
12639
12640       /* For a `typename', we needn't call xref_tag.  */
12641       if (tag_type == typename_type
12642           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12643         return cp_parser_make_typename_type (parser, parser->scope,
12644                                              identifier,
12645                                              token->location);
12646       /* Look up a qualified name in the usual way.  */
12647       if (parser->scope)
12648         {
12649           tree decl;
12650           tree ambiguous_decls;
12651
12652           decl = cp_parser_lookup_name (parser, identifier,
12653                                         tag_type,
12654                                         /*is_template=*/false,
12655                                         /*is_namespace=*/false,
12656                                         /*check_dependency=*/true,
12657                                         &ambiguous_decls,
12658                                         token->location);
12659
12660           /* If the lookup was ambiguous, an error will already have been
12661              issued.  */
12662           if (ambiguous_decls)
12663             return error_mark_node;
12664
12665           /* If we are parsing friend declaration, DECL may be a
12666              TEMPLATE_DECL tree node here.  However, we need to check
12667              whether this TEMPLATE_DECL results in valid code.  Consider
12668              the following example:
12669
12670                namespace N {
12671                  template <class T> class C {};
12672                }
12673                class X {
12674                  template <class T> friend class N::C; // #1, valid code
12675                };
12676                template <class T> class Y {
12677                  friend class N::C;                    // #2, invalid code
12678                };
12679
12680              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12681              name lookup of `N::C'.  We see that friend declaration must
12682              be template for the code to be valid.  Note that
12683              processing_template_decl does not work here since it is
12684              always 1 for the above two cases.  */
12685
12686           decl = (cp_parser_maybe_treat_template_as_class
12687                   (decl, /*tag_name_p=*/is_friend
12688                          && parser->num_template_parameter_lists));
12689
12690           if (TREE_CODE (decl) != TYPE_DECL)
12691             {
12692               cp_parser_diagnose_invalid_type_name (parser,
12693                                                     parser->scope,
12694                                                     identifier,
12695                                                     token->location);
12696               return error_mark_node;
12697             }
12698
12699           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12700             {
12701               bool allow_template = (parser->num_template_parameter_lists
12702                                       || DECL_SELF_REFERENCE_P (decl));
12703               type = check_elaborated_type_specifier (tag_type, decl, 
12704                                                       allow_template);
12705
12706               if (type == error_mark_node)
12707                 return error_mark_node;
12708             }
12709
12710           /* Forward declarations of nested types, such as
12711
12712                class C1::C2;
12713                class C1::C2::C3;
12714
12715              are invalid unless all components preceding the final '::'
12716              are complete.  If all enclosing types are complete, these
12717              declarations become merely pointless.
12718
12719              Invalid forward declarations of nested types are errors
12720              caught elsewhere in parsing.  Those that are pointless arrive
12721              here.  */
12722
12723           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12724               && !is_friend && !processing_explicit_instantiation)
12725             warning (0, "declaration %qD does not declare anything", decl);
12726
12727           type = TREE_TYPE (decl);
12728         }
12729       else
12730         {
12731           /* An elaborated-type-specifier sometimes introduces a new type and
12732              sometimes names an existing type.  Normally, the rule is that it
12733              introduces a new type only if there is not an existing type of
12734              the same name already in scope.  For example, given:
12735
12736                struct S {};
12737                void f() { struct S s; }
12738
12739              the `struct S' in the body of `f' is the same `struct S' as in
12740              the global scope; the existing definition is used.  However, if
12741              there were no global declaration, this would introduce a new
12742              local class named `S'.
12743
12744              An exception to this rule applies to the following code:
12745
12746                namespace N { struct S; }
12747
12748              Here, the elaborated-type-specifier names a new type
12749              unconditionally; even if there is already an `S' in the
12750              containing scope this declaration names a new type.
12751              This exception only applies if the elaborated-type-specifier
12752              forms the complete declaration:
12753
12754                [class.name]
12755
12756                A declaration consisting solely of `class-key identifier ;' is
12757                either a redeclaration of the name in the current scope or a
12758                forward declaration of the identifier as a class name.  It
12759                introduces the name into the current scope.
12760
12761              We are in this situation precisely when the next token is a `;'.
12762
12763              An exception to the exception is that a `friend' declaration does
12764              *not* name a new type; i.e., given:
12765
12766                struct S { friend struct T; };
12767
12768              `T' is not a new type in the scope of `S'.
12769
12770              Also, `new struct S' or `sizeof (struct S)' never results in the
12771              definition of a new type; a new type can only be declared in a
12772              declaration context.  */
12773
12774           tag_scope ts;
12775           bool template_p;
12776
12777           if (is_friend)
12778             /* Friends have special name lookup rules.  */
12779             ts = ts_within_enclosing_non_class;
12780           else if (is_declaration
12781                    && cp_lexer_next_token_is (parser->lexer,
12782                                               CPP_SEMICOLON))
12783             /* This is a `class-key identifier ;' */
12784             ts = ts_current;
12785           else
12786             ts = ts_global;
12787
12788           template_p =
12789             (parser->num_template_parameter_lists
12790              && (cp_parser_next_token_starts_class_definition_p (parser)
12791                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12792           /* An unqualified name was used to reference this type, so
12793              there were no qualifying templates.  */
12794           if (!cp_parser_check_template_parameters (parser,
12795                                                     /*num_templates=*/0,
12796                                                     token->location,
12797                                                     /*declarator=*/NULL))
12798             return error_mark_node;
12799           type = xref_tag (tag_type, identifier, ts, template_p);
12800         }
12801     }
12802
12803   if (type == error_mark_node)
12804     return error_mark_node;
12805
12806   /* Allow attributes on forward declarations of classes.  */
12807   if (attributes)
12808     {
12809       if (TREE_CODE (type) == TYPENAME_TYPE)
12810         warning (OPT_Wattributes,
12811                  "attributes ignored on uninstantiated type");
12812       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12813                && ! processing_explicit_instantiation)
12814         warning (OPT_Wattributes,
12815                  "attributes ignored on template instantiation");
12816       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12817         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12818       else
12819         warning (OPT_Wattributes,
12820                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12821     }
12822
12823   if (tag_type != enum_type)
12824     cp_parser_check_class_key (tag_type, type);
12825
12826   /* A "<" cannot follow an elaborated type specifier.  If that
12827      happens, the user was probably trying to form a template-id.  */
12828   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12829
12830   return type;
12831 }
12832
12833 /* Parse an enum-specifier.
12834
12835    enum-specifier:
12836      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12837
12838    enum-key:
12839      enum
12840      enum class   [C++0x]
12841      enum struct  [C++0x]
12842
12843    enum-base:   [C++0x]
12844      : type-specifier-seq
12845
12846    GNU Extensions:
12847      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12848        { enumerator-list [opt] }attributes[opt]
12849
12850    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12851    if the token stream isn't an enum-specifier after all.  */
12852
12853 static tree
12854 cp_parser_enum_specifier (cp_parser* parser)
12855 {
12856   tree identifier;
12857   tree type;
12858   tree attributes;
12859   bool scoped_enum_p = false;
12860   bool has_underlying_type = false;
12861   tree underlying_type = NULL_TREE;
12862
12863   /* Parse tentatively so that we can back up if we don't find a
12864      enum-specifier.  */
12865   cp_parser_parse_tentatively (parser);
12866
12867   /* Caller guarantees that the current token is 'enum', an identifier
12868      possibly follows, and the token after that is an opening brace.
12869      If we don't have an identifier, fabricate an anonymous name for
12870      the enumeration being defined.  */
12871   cp_lexer_consume_token (parser->lexer);
12872
12873   /* Parse the "class" or "struct", which indicates a scoped
12874      enumeration type in C++0x.  */
12875   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12876       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12877     {
12878       if (cxx_dialect == cxx98)
12879         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12880
12881       /* Consume the `struct' or `class' token.  */
12882       cp_lexer_consume_token (parser->lexer);
12883
12884       scoped_enum_p = true;
12885     }
12886
12887   attributes = cp_parser_attributes_opt (parser);
12888
12889   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12890     identifier = cp_parser_identifier (parser);
12891   else
12892     identifier = make_anon_name ();
12893
12894   /* Check for the `:' that denotes a specified underlying type in C++0x.
12895      Note that a ':' could also indicate a bitfield width, however.  */
12896   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12897     {
12898       cp_decl_specifier_seq type_specifiers;
12899
12900       /* Consume the `:'.  */
12901       cp_lexer_consume_token (parser->lexer);
12902
12903       /* Parse the type-specifier-seq.  */
12904       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12905                                     /*is_trailing_return=*/false,
12906                                     &type_specifiers);
12907
12908       /* At this point this is surely not elaborated type specifier.  */
12909       if (!cp_parser_parse_definitely (parser))
12910         return NULL_TREE;
12911
12912       if (cxx_dialect == cxx98)
12913         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12914
12915       has_underlying_type = true;
12916
12917       /* If that didn't work, stop.  */
12918       if (type_specifiers.type != error_mark_node)
12919         {
12920           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12921                                             /*initialized=*/0, NULL);
12922           if (underlying_type == error_mark_node)
12923             underlying_type = NULL_TREE;
12924         }
12925     }
12926
12927   /* Look for the `{' but don't consume it yet.  */
12928   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12929     {
12930       cp_parser_error (parser, "expected %<{%>");
12931       if (has_underlying_type)
12932         return NULL_TREE;
12933     }
12934
12935   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12936     return NULL_TREE;
12937
12938   /* Issue an error message if type-definitions are forbidden here.  */
12939   if (!cp_parser_check_type_definition (parser))
12940     type = error_mark_node;
12941   else
12942     /* Create the new type.  We do this before consuming the opening
12943        brace so the enum will be recorded as being on the line of its
12944        tag (or the 'enum' keyword, if there is no tag).  */
12945     type = start_enum (identifier, underlying_type, scoped_enum_p);
12946   
12947   /* Consume the opening brace.  */
12948   cp_lexer_consume_token (parser->lexer);
12949
12950   if (type == error_mark_node)
12951     {
12952       cp_parser_skip_to_end_of_block_or_statement (parser);
12953       return error_mark_node;
12954     }
12955
12956   /* If the next token is not '}', then there are some enumerators.  */
12957   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12958     cp_parser_enumerator_list (parser, type);
12959
12960   /* Consume the final '}'.  */
12961   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12962
12963   /* Look for trailing attributes to apply to this enumeration, and
12964      apply them if appropriate.  */
12965   if (cp_parser_allow_gnu_extensions_p (parser))
12966     {
12967       tree trailing_attr = cp_parser_attributes_opt (parser);
12968       trailing_attr = chainon (trailing_attr, attributes);
12969       cplus_decl_attributes (&type,
12970                              trailing_attr,
12971                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12972     }
12973
12974   /* Finish up the enumeration.  */
12975   finish_enum (type);
12976
12977   return type;
12978 }
12979
12980 /* Parse an enumerator-list.  The enumerators all have the indicated
12981    TYPE.
12982
12983    enumerator-list:
12984      enumerator-definition
12985      enumerator-list , enumerator-definition  */
12986
12987 static void
12988 cp_parser_enumerator_list (cp_parser* parser, tree type)
12989 {
12990   while (true)
12991     {
12992       /* Parse an enumerator-definition.  */
12993       cp_parser_enumerator_definition (parser, type);
12994
12995       /* If the next token is not a ',', we've reached the end of
12996          the list.  */
12997       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12998         break;
12999       /* Otherwise, consume the `,' and keep going.  */
13000       cp_lexer_consume_token (parser->lexer);
13001       /* If the next token is a `}', there is a trailing comma.  */
13002       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13003         {
13004           if (!in_system_header)
13005             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13006           break;
13007         }
13008     }
13009 }
13010
13011 /* Parse an enumerator-definition.  The enumerator has the indicated
13012    TYPE.
13013
13014    enumerator-definition:
13015      enumerator
13016      enumerator = constant-expression
13017
13018    enumerator:
13019      identifier  */
13020
13021 static void
13022 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13023 {
13024   tree identifier;
13025   tree value;
13026
13027   /* Look for the identifier.  */
13028   identifier = cp_parser_identifier (parser);
13029   if (identifier == error_mark_node)
13030     return;
13031
13032   /* If the next token is an '=', then there is an explicit value.  */
13033   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13034     {
13035       /* Consume the `=' token.  */
13036       cp_lexer_consume_token (parser->lexer);
13037       /* Parse the value.  */
13038       value = cp_parser_constant_expression (parser,
13039                                              /*allow_non_constant_p=*/false,
13040                                              NULL);
13041     }
13042   else
13043     value = NULL_TREE;
13044
13045   /* If we are processing a template, make sure the initializer of the
13046      enumerator doesn't contain any bare template parameter pack.  */
13047   if (check_for_bare_parameter_packs (value))
13048     value = error_mark_node;
13049
13050   /* Create the enumerator.  */
13051   build_enumerator (identifier, value, type);
13052 }
13053
13054 /* Parse a namespace-name.
13055
13056    namespace-name:
13057      original-namespace-name
13058      namespace-alias
13059
13060    Returns the NAMESPACE_DECL for the namespace.  */
13061
13062 static tree
13063 cp_parser_namespace_name (cp_parser* parser)
13064 {
13065   tree identifier;
13066   tree namespace_decl;
13067
13068   cp_token *token = cp_lexer_peek_token (parser->lexer);
13069
13070   /* Get the name of the namespace.  */
13071   identifier = cp_parser_identifier (parser);
13072   if (identifier == error_mark_node)
13073     return error_mark_node;
13074
13075   /* Look up the identifier in the currently active scope.  Look only
13076      for namespaces, due to:
13077
13078        [basic.lookup.udir]
13079
13080        When looking up a namespace-name in a using-directive or alias
13081        definition, only namespace names are considered.
13082
13083      And:
13084
13085        [basic.lookup.qual]
13086
13087        During the lookup of a name preceding the :: scope resolution
13088        operator, object, function, and enumerator names are ignored.
13089
13090      (Note that cp_parser_qualifying_entity only calls this
13091      function if the token after the name is the scope resolution
13092      operator.)  */
13093   namespace_decl = cp_parser_lookup_name (parser, identifier,
13094                                           none_type,
13095                                           /*is_template=*/false,
13096                                           /*is_namespace=*/true,
13097                                           /*check_dependency=*/true,
13098                                           /*ambiguous_decls=*/NULL,
13099                                           token->location);
13100   /* If it's not a namespace, issue an error.  */
13101   if (namespace_decl == error_mark_node
13102       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13103     {
13104       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13105         error_at (token->location, "%qD is not a namespace-name", identifier);
13106       cp_parser_error (parser, "expected namespace-name");
13107       namespace_decl = error_mark_node;
13108     }
13109
13110   return namespace_decl;
13111 }
13112
13113 /* Parse a namespace-definition.
13114
13115    namespace-definition:
13116      named-namespace-definition
13117      unnamed-namespace-definition
13118
13119    named-namespace-definition:
13120      original-namespace-definition
13121      extension-namespace-definition
13122
13123    original-namespace-definition:
13124      namespace identifier { namespace-body }
13125
13126    extension-namespace-definition:
13127      namespace original-namespace-name { namespace-body }
13128
13129    unnamed-namespace-definition:
13130      namespace { namespace-body } */
13131
13132 static void
13133 cp_parser_namespace_definition (cp_parser* parser)
13134 {
13135   tree identifier, attribs;
13136   bool has_visibility;
13137   bool is_inline;
13138
13139   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13140     {
13141       is_inline = true;
13142       cp_lexer_consume_token (parser->lexer);
13143     }
13144   else
13145     is_inline = false;
13146
13147   /* Look for the `namespace' keyword.  */
13148   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13149
13150   /* Get the name of the namespace.  We do not attempt to distinguish
13151      between an original-namespace-definition and an
13152      extension-namespace-definition at this point.  The semantic
13153      analysis routines are responsible for that.  */
13154   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13155     identifier = cp_parser_identifier (parser);
13156   else
13157     identifier = NULL_TREE;
13158
13159   /* Parse any specified attributes.  */
13160   attribs = cp_parser_attributes_opt (parser);
13161
13162   /* Look for the `{' to start the namespace.  */
13163   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13164   /* Start the namespace.  */
13165   push_namespace (identifier);
13166
13167   /* "inline namespace" is equivalent to a stub namespace definition
13168      followed by a strong using directive.  */
13169   if (is_inline)
13170     {
13171       tree name_space = current_namespace;
13172       /* Set up namespace association.  */
13173       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13174         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13175                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13176       /* Import the contents of the inline namespace.  */
13177       pop_namespace ();
13178       do_using_directive (name_space);
13179       push_namespace (identifier);
13180     }
13181
13182   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13183
13184   /* Parse the body of the namespace.  */
13185   cp_parser_namespace_body (parser);
13186
13187 #ifdef HANDLE_PRAGMA_VISIBILITY
13188   if (has_visibility)
13189     pop_visibility (1);
13190 #endif
13191
13192   /* Finish the namespace.  */
13193   pop_namespace ();
13194   /* Look for the final `}'.  */
13195   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13196 }
13197
13198 /* Parse a namespace-body.
13199
13200    namespace-body:
13201      declaration-seq [opt]  */
13202
13203 static void
13204 cp_parser_namespace_body (cp_parser* parser)
13205 {
13206   cp_parser_declaration_seq_opt (parser);
13207 }
13208
13209 /* Parse a namespace-alias-definition.
13210
13211    namespace-alias-definition:
13212      namespace identifier = qualified-namespace-specifier ;  */
13213
13214 static void
13215 cp_parser_namespace_alias_definition (cp_parser* parser)
13216 {
13217   tree identifier;
13218   tree namespace_specifier;
13219
13220   cp_token *token = cp_lexer_peek_token (parser->lexer);
13221
13222   /* Look for the `namespace' keyword.  */
13223   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13224   /* Look for the identifier.  */
13225   identifier = cp_parser_identifier (parser);
13226   if (identifier == error_mark_node)
13227     return;
13228   /* Look for the `=' token.  */
13229   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13230       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13231     {
13232       error_at (token->location, "%<namespace%> definition is not allowed here");
13233       /* Skip the definition.  */
13234       cp_lexer_consume_token (parser->lexer);
13235       if (cp_parser_skip_to_closing_brace (parser))
13236         cp_lexer_consume_token (parser->lexer);
13237       return;
13238     }
13239   cp_parser_require (parser, CPP_EQ, RT_EQ);
13240   /* Look for the qualified-namespace-specifier.  */
13241   namespace_specifier
13242     = cp_parser_qualified_namespace_specifier (parser);
13243   /* Look for the `;' token.  */
13244   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13245
13246   /* Register the alias in the symbol table.  */
13247   do_namespace_alias (identifier, namespace_specifier);
13248 }
13249
13250 /* Parse a qualified-namespace-specifier.
13251
13252    qualified-namespace-specifier:
13253      :: [opt] nested-name-specifier [opt] namespace-name
13254
13255    Returns a NAMESPACE_DECL corresponding to the specified
13256    namespace.  */
13257
13258 static tree
13259 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13260 {
13261   /* Look for the optional `::'.  */
13262   cp_parser_global_scope_opt (parser,
13263                               /*current_scope_valid_p=*/false);
13264
13265   /* Look for the optional nested-name-specifier.  */
13266   cp_parser_nested_name_specifier_opt (parser,
13267                                        /*typename_keyword_p=*/false,
13268                                        /*check_dependency_p=*/true,
13269                                        /*type_p=*/false,
13270                                        /*is_declaration=*/true);
13271
13272   return cp_parser_namespace_name (parser);
13273 }
13274
13275 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13276    access declaration.
13277
13278    using-declaration:
13279      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13280      using :: unqualified-id ;  
13281
13282    access-declaration:
13283      qualified-id ;  
13284
13285    */
13286
13287 static bool
13288 cp_parser_using_declaration (cp_parser* parser, 
13289                              bool access_declaration_p)
13290 {
13291   cp_token *token;
13292   bool typename_p = false;
13293   bool global_scope_p;
13294   tree decl;
13295   tree identifier;
13296   tree qscope;
13297
13298   if (access_declaration_p)
13299     cp_parser_parse_tentatively (parser);
13300   else
13301     {
13302       /* Look for the `using' keyword.  */
13303       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13304       
13305       /* Peek at the next token.  */
13306       token = cp_lexer_peek_token (parser->lexer);
13307       /* See if it's `typename'.  */
13308       if (token->keyword == RID_TYPENAME)
13309         {
13310           /* Remember that we've seen it.  */
13311           typename_p = true;
13312           /* Consume the `typename' token.  */
13313           cp_lexer_consume_token (parser->lexer);
13314         }
13315     }
13316
13317   /* Look for the optional global scope qualification.  */
13318   global_scope_p
13319     = (cp_parser_global_scope_opt (parser,
13320                                    /*current_scope_valid_p=*/false)
13321        != NULL_TREE);
13322
13323   /* If we saw `typename', or didn't see `::', then there must be a
13324      nested-name-specifier present.  */
13325   if (typename_p || !global_scope_p)
13326     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13327                                               /*check_dependency_p=*/true,
13328                                               /*type_p=*/false,
13329                                               /*is_declaration=*/true);
13330   /* Otherwise, we could be in either of the two productions.  In that
13331      case, treat the nested-name-specifier as optional.  */
13332   else
13333     qscope = cp_parser_nested_name_specifier_opt (parser,
13334                                                   /*typename_keyword_p=*/false,
13335                                                   /*check_dependency_p=*/true,
13336                                                   /*type_p=*/false,
13337                                                   /*is_declaration=*/true);
13338   if (!qscope)
13339     qscope = global_namespace;
13340
13341   if (access_declaration_p && cp_parser_error_occurred (parser))
13342     /* Something has already gone wrong; there's no need to parse
13343        further.  Since an error has occurred, the return value of
13344        cp_parser_parse_definitely will be false, as required.  */
13345     return cp_parser_parse_definitely (parser);
13346
13347   token = cp_lexer_peek_token (parser->lexer);
13348   /* Parse the unqualified-id.  */
13349   identifier = cp_parser_unqualified_id (parser,
13350                                          /*template_keyword_p=*/false,
13351                                          /*check_dependency_p=*/true,
13352                                          /*declarator_p=*/true,
13353                                          /*optional_p=*/false);
13354
13355   if (access_declaration_p)
13356     {
13357       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13358         cp_parser_simulate_error (parser);
13359       if (!cp_parser_parse_definitely (parser))
13360         return false;
13361     }
13362
13363   /* The function we call to handle a using-declaration is different
13364      depending on what scope we are in.  */
13365   if (qscope == error_mark_node || identifier == error_mark_node)
13366     ;
13367   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13368            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13369     /* [namespace.udecl]
13370
13371        A using declaration shall not name a template-id.  */
13372     error_at (token->location,
13373               "a template-id may not appear in a using-declaration");
13374   else
13375     {
13376       if (at_class_scope_p ())
13377         {
13378           /* Create the USING_DECL.  */
13379           decl = do_class_using_decl (parser->scope, identifier);
13380
13381           if (check_for_bare_parameter_packs (decl))
13382             return false;
13383           else
13384             /* Add it to the list of members in this class.  */
13385             finish_member_declaration (decl);
13386         }
13387       else
13388         {
13389           decl = cp_parser_lookup_name_simple (parser,
13390                                                identifier,
13391                                                token->location);
13392           if (decl == error_mark_node)
13393             cp_parser_name_lookup_error (parser, identifier,
13394                                          decl, NLE_NULL,
13395                                          token->location);
13396           else if (check_for_bare_parameter_packs (decl))
13397             return false;
13398           else if (!at_namespace_scope_p ())
13399             do_local_using_decl (decl, qscope, identifier);
13400           else
13401             do_toplevel_using_decl (decl, qscope, identifier);
13402         }
13403     }
13404
13405   /* Look for the final `;'.  */
13406   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13407   
13408   return true;
13409 }
13410
13411 /* Parse a using-directive.
13412
13413    using-directive:
13414      using namespace :: [opt] nested-name-specifier [opt]
13415        namespace-name ;  */
13416
13417 static void
13418 cp_parser_using_directive (cp_parser* parser)
13419 {
13420   tree namespace_decl;
13421   tree attribs;
13422
13423   /* Look for the `using' keyword.  */
13424   cp_parser_require_keyword (parser, RID_USING, RT_USING);
13425   /* And the `namespace' keyword.  */
13426   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13427   /* Look for the optional `::' operator.  */
13428   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13429   /* And the optional nested-name-specifier.  */
13430   cp_parser_nested_name_specifier_opt (parser,
13431                                        /*typename_keyword_p=*/false,
13432                                        /*check_dependency_p=*/true,
13433                                        /*type_p=*/false,
13434                                        /*is_declaration=*/true);
13435   /* Get the namespace being used.  */
13436   namespace_decl = cp_parser_namespace_name (parser);
13437   /* And any specified attributes.  */
13438   attribs = cp_parser_attributes_opt (parser);
13439   /* Update the symbol table.  */
13440   parse_using_directive (namespace_decl, attribs);
13441   /* Look for the final `;'.  */
13442   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13443 }
13444
13445 /* Parse an asm-definition.
13446
13447    asm-definition:
13448      asm ( string-literal ) ;
13449
13450    GNU Extension:
13451
13452    asm-definition:
13453      asm volatile [opt] ( string-literal ) ;
13454      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13455      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13456                           : asm-operand-list [opt] ) ;
13457      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13458                           : asm-operand-list [opt]
13459                           : asm-clobber-list [opt] ) ;
13460      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13461                                : asm-clobber-list [opt]
13462                                : asm-goto-list ) ;  */
13463
13464 static void
13465 cp_parser_asm_definition (cp_parser* parser)
13466 {
13467   tree string;
13468   tree outputs = NULL_TREE;
13469   tree inputs = NULL_TREE;
13470   tree clobbers = NULL_TREE;
13471   tree labels = NULL_TREE;
13472   tree asm_stmt;
13473   bool volatile_p = false;
13474   bool extended_p = false;
13475   bool invalid_inputs_p = false;
13476   bool invalid_outputs_p = false;
13477   bool goto_p = false;
13478   required_token missing = 0;
13479
13480   /* Look for the `asm' keyword.  */
13481   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
13482   /* See if the next token is `volatile'.  */
13483   if (cp_parser_allow_gnu_extensions_p (parser)
13484       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13485     {
13486       /* Remember that we saw the `volatile' keyword.  */
13487       volatile_p = true;
13488       /* Consume the token.  */
13489       cp_lexer_consume_token (parser->lexer);
13490     }
13491   if (cp_parser_allow_gnu_extensions_p (parser)
13492       && parser->in_function_body
13493       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13494     {
13495       /* Remember that we saw the `goto' keyword.  */
13496       goto_p = true;
13497       /* Consume the token.  */
13498       cp_lexer_consume_token (parser->lexer);
13499     }
13500   /* Look for the opening `('.  */
13501   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13502     return;
13503   /* Look for the string.  */
13504   string = cp_parser_string_literal (parser, false, false);
13505   if (string == error_mark_node)
13506     {
13507       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13508                                              /*consume_paren=*/true);
13509       return;
13510     }
13511
13512   /* If we're allowing GNU extensions, check for the extended assembly
13513      syntax.  Unfortunately, the `:' tokens need not be separated by
13514      a space in C, and so, for compatibility, we tolerate that here
13515      too.  Doing that means that we have to treat the `::' operator as
13516      two `:' tokens.  */
13517   if (cp_parser_allow_gnu_extensions_p (parser)
13518       && parser->in_function_body
13519       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13520           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13521     {
13522       bool inputs_p = false;
13523       bool clobbers_p = false;
13524       bool labels_p = false;
13525
13526       /* The extended syntax was used.  */
13527       extended_p = true;
13528
13529       /* Look for outputs.  */
13530       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13531         {
13532           /* Consume the `:'.  */
13533           cp_lexer_consume_token (parser->lexer);
13534           /* Parse the output-operands.  */
13535           if (cp_lexer_next_token_is_not (parser->lexer,
13536                                           CPP_COLON)
13537               && cp_lexer_next_token_is_not (parser->lexer,
13538                                              CPP_SCOPE)
13539               && cp_lexer_next_token_is_not (parser->lexer,
13540                                              CPP_CLOSE_PAREN)
13541               && !goto_p)
13542             outputs = cp_parser_asm_operand_list (parser);
13543
13544             if (outputs == error_mark_node)
13545               invalid_outputs_p = true;
13546         }
13547       /* If the next token is `::', there are no outputs, and the
13548          next token is the beginning of the inputs.  */
13549       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13550         /* The inputs are coming next.  */
13551         inputs_p = true;
13552
13553       /* Look for inputs.  */
13554       if (inputs_p
13555           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13556         {
13557           /* Consume the `:' or `::'.  */
13558           cp_lexer_consume_token (parser->lexer);
13559           /* Parse the output-operands.  */
13560           if (cp_lexer_next_token_is_not (parser->lexer,
13561                                           CPP_COLON)
13562               && cp_lexer_next_token_is_not (parser->lexer,
13563                                              CPP_SCOPE)
13564               && cp_lexer_next_token_is_not (parser->lexer,
13565                                              CPP_CLOSE_PAREN))
13566             inputs = cp_parser_asm_operand_list (parser);
13567
13568             if (inputs == error_mark_node)
13569               invalid_inputs_p = true;
13570         }
13571       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13572         /* The clobbers are coming next.  */
13573         clobbers_p = true;
13574
13575       /* Look for clobbers.  */
13576       if (clobbers_p
13577           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13578         {
13579           clobbers_p = true;
13580           /* Consume the `:' or `::'.  */
13581           cp_lexer_consume_token (parser->lexer);
13582           /* Parse the clobbers.  */
13583           if (cp_lexer_next_token_is_not (parser->lexer,
13584                                           CPP_COLON)
13585               && cp_lexer_next_token_is_not (parser->lexer,
13586                                              CPP_CLOSE_PAREN))
13587             clobbers = cp_parser_asm_clobber_list (parser);
13588         }
13589       else if (goto_p
13590                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13591         /* The labels are coming next.  */
13592         labels_p = true;
13593
13594       /* Look for labels.  */
13595       if (labels_p
13596           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13597         {
13598           labels_p = true;
13599           /* Consume the `:' or `::'.  */
13600           cp_lexer_consume_token (parser->lexer);
13601           /* Parse the labels.  */
13602           labels = cp_parser_asm_label_list (parser);
13603         }
13604
13605       if (goto_p && !labels_p)
13606         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
13607     }
13608   else if (goto_p)
13609     missing = RT_COLON_SCOPE;
13610
13611   /* Look for the closing `)'.  */
13612   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13613                           missing ? missing : RT_CLOSE_PAREN))
13614     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13615                                            /*consume_paren=*/true);
13616   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13617
13618   if (!invalid_inputs_p && !invalid_outputs_p)
13619     {
13620       /* Create the ASM_EXPR.  */
13621       if (parser->in_function_body)
13622         {
13623           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13624                                       inputs, clobbers, labels);
13625           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13626           if (!extended_p)
13627             {
13628               tree temp = asm_stmt;
13629               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13630                 temp = TREE_OPERAND (temp, 0);
13631
13632               ASM_INPUT_P (temp) = 1;
13633             }
13634         }
13635       else
13636         cgraph_add_asm_node (string);
13637     }
13638 }
13639
13640 /* Declarators [gram.dcl.decl] */
13641
13642 /* Parse an init-declarator.
13643
13644    init-declarator:
13645      declarator initializer [opt]
13646
13647    GNU Extension:
13648
13649    init-declarator:
13650      declarator asm-specification [opt] attributes [opt] initializer [opt]
13651
13652    function-definition:
13653      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13654        function-body
13655      decl-specifier-seq [opt] declarator function-try-block
13656
13657    GNU Extension:
13658
13659    function-definition:
13660      __extension__ function-definition
13661
13662    The DECL_SPECIFIERS apply to this declarator.  Returns a
13663    representation of the entity declared.  If MEMBER_P is TRUE, then
13664    this declarator appears in a class scope.  The new DECL created by
13665    this declarator is returned.
13666
13667    The CHECKS are access checks that should be performed once we know
13668    what entity is being declared (and, therefore, what classes have
13669    befriended it).
13670
13671    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13672    for a function-definition here as well.  If the declarator is a
13673    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13674    be TRUE upon return.  By that point, the function-definition will
13675    have been completely parsed.
13676
13677    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13678    is FALSE.  */
13679
13680 static tree
13681 cp_parser_init_declarator (cp_parser* parser,
13682                            cp_decl_specifier_seq *decl_specifiers,
13683                            VEC (deferred_access_check,gc)* checks,
13684                            bool function_definition_allowed_p,
13685                            bool member_p,
13686                            int declares_class_or_enum,
13687                            bool* function_definition_p)
13688 {
13689   cp_token *token = NULL, *asm_spec_start_token = NULL,
13690            *attributes_start_token = NULL;
13691   cp_declarator *declarator;
13692   tree prefix_attributes;
13693   tree attributes;
13694   tree asm_specification;
13695   tree initializer;
13696   tree decl = NULL_TREE;
13697   tree scope;
13698   int is_initialized;
13699   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13700      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13701      "(...)".  */
13702   enum cpp_ttype initialization_kind;
13703   bool is_direct_init = false;
13704   bool is_non_constant_init;
13705   int ctor_dtor_or_conv_p;
13706   bool friend_p;
13707   tree pushed_scope = NULL;
13708
13709   /* Gather the attributes that were provided with the
13710      decl-specifiers.  */
13711   prefix_attributes = decl_specifiers->attributes;
13712
13713   /* Assume that this is not the declarator for a function
13714      definition.  */
13715   if (function_definition_p)
13716     *function_definition_p = false;
13717
13718   /* Defer access checks while parsing the declarator; we cannot know
13719      what names are accessible until we know what is being
13720      declared.  */
13721   resume_deferring_access_checks ();
13722
13723   /* Parse the declarator.  */
13724   token = cp_lexer_peek_token (parser->lexer);
13725   declarator
13726     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13727                             &ctor_dtor_or_conv_p,
13728                             /*parenthesized_p=*/NULL,
13729                             /*member_p=*/false);
13730   /* Gather up the deferred checks.  */
13731   stop_deferring_access_checks ();
13732
13733   /* If the DECLARATOR was erroneous, there's no need to go
13734      further.  */
13735   if (declarator == cp_error_declarator)
13736     return error_mark_node;
13737
13738   /* Check that the number of template-parameter-lists is OK.  */
13739   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13740                                                        token->location))
13741     return error_mark_node;
13742
13743   if (declares_class_or_enum & 2)
13744     cp_parser_check_for_definition_in_return_type (declarator,
13745                                                    decl_specifiers->type,
13746                                                    decl_specifiers->type_location);
13747
13748   /* Figure out what scope the entity declared by the DECLARATOR is
13749      located in.  `grokdeclarator' sometimes changes the scope, so
13750      we compute it now.  */
13751   scope = get_scope_of_declarator (declarator);
13752
13753   /* Perform any lookups in the declared type which were thought to be
13754      dependent, but are not in the scope of the declarator.  */
13755   decl_specifiers->type
13756     = maybe_update_decl_type (decl_specifiers->type, scope);
13757
13758   /* If we're allowing GNU extensions, look for an asm-specification
13759      and attributes.  */
13760   if (cp_parser_allow_gnu_extensions_p (parser))
13761     {
13762       /* Look for an asm-specification.  */
13763       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13764       asm_specification = cp_parser_asm_specification_opt (parser);
13765       /* And attributes.  */
13766       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13767       attributes = cp_parser_attributes_opt (parser);
13768     }
13769   else
13770     {
13771       asm_specification = NULL_TREE;
13772       attributes = NULL_TREE;
13773     }
13774
13775   /* Peek at the next token.  */
13776   token = cp_lexer_peek_token (parser->lexer);
13777   /* Check to see if the token indicates the start of a
13778      function-definition.  */
13779   if (function_declarator_p (declarator)
13780       && cp_parser_token_starts_function_definition_p (token))
13781     {
13782       if (!function_definition_allowed_p)
13783         {
13784           /* If a function-definition should not appear here, issue an
13785              error message.  */
13786           cp_parser_error (parser,
13787                            "a function-definition is not allowed here");
13788           return error_mark_node;
13789         }
13790       else
13791         {
13792           location_t func_brace_location
13793             = cp_lexer_peek_token (parser->lexer)->location;
13794
13795           /* Neither attributes nor an asm-specification are allowed
13796              on a function-definition.  */
13797           if (asm_specification)
13798             error_at (asm_spec_start_token->location,
13799                       "an asm-specification is not allowed "
13800                       "on a function-definition");
13801           if (attributes)
13802             error_at (attributes_start_token->location,
13803                       "attributes are not allowed on a function-definition");
13804           /* This is a function-definition.  */
13805           *function_definition_p = true;
13806
13807           /* Parse the function definition.  */
13808           if (member_p)
13809             decl = cp_parser_save_member_function_body (parser,
13810                                                         decl_specifiers,
13811                                                         declarator,
13812                                                         prefix_attributes);
13813           else
13814             decl
13815               = (cp_parser_function_definition_from_specifiers_and_declarator
13816                  (parser, decl_specifiers, prefix_attributes, declarator));
13817
13818           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13819             {
13820               /* This is where the prologue starts...  */
13821               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13822                 = func_brace_location;
13823             }
13824
13825           return decl;
13826         }
13827     }
13828
13829   /* [dcl.dcl]
13830
13831      Only in function declarations for constructors, destructors, and
13832      type conversions can the decl-specifier-seq be omitted.
13833
13834      We explicitly postpone this check past the point where we handle
13835      function-definitions because we tolerate function-definitions
13836      that are missing their return types in some modes.  */
13837   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13838     {
13839       cp_parser_error (parser,
13840                        "expected constructor, destructor, or type conversion");
13841       return error_mark_node;
13842     }
13843
13844   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13845   if (token->type == CPP_EQ
13846       || token->type == CPP_OPEN_PAREN
13847       || token->type == CPP_OPEN_BRACE)
13848     {
13849       is_initialized = SD_INITIALIZED;
13850       initialization_kind = token->type;
13851
13852       if (token->type == CPP_EQ
13853           && function_declarator_p (declarator))
13854         {
13855           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13856           if (t2->keyword == RID_DEFAULT)
13857             is_initialized = SD_DEFAULTED;
13858           else if (t2->keyword == RID_DELETE)
13859             is_initialized = SD_DELETED;
13860         }
13861     }
13862   else
13863     {
13864       /* If the init-declarator isn't initialized and isn't followed by a
13865          `,' or `;', it's not a valid init-declarator.  */
13866       if (token->type != CPP_COMMA
13867           && token->type != CPP_SEMICOLON)
13868         {
13869           cp_parser_error (parser, "expected initializer");
13870           return error_mark_node;
13871         }
13872       is_initialized = SD_UNINITIALIZED;
13873       initialization_kind = CPP_EOF;
13874     }
13875
13876   /* Because start_decl has side-effects, we should only call it if we
13877      know we're going ahead.  By this point, we know that we cannot
13878      possibly be looking at any other construct.  */
13879   cp_parser_commit_to_tentative_parse (parser);
13880
13881   /* If the decl specifiers were bad, issue an error now that we're
13882      sure this was intended to be a declarator.  Then continue
13883      declaring the variable(s), as int, to try to cut down on further
13884      errors.  */
13885   if (decl_specifiers->any_specifiers_p
13886       && decl_specifiers->type == error_mark_node)
13887     {
13888       cp_parser_error (parser, "invalid type in declaration");
13889       decl_specifiers->type = integer_type_node;
13890     }
13891
13892   /* Check to see whether or not this declaration is a friend.  */
13893   friend_p = cp_parser_friend_p (decl_specifiers);
13894
13895   /* Enter the newly declared entry in the symbol table.  If we're
13896      processing a declaration in a class-specifier, we wait until
13897      after processing the initializer.  */
13898   if (!member_p)
13899     {
13900       if (parser->in_unbraced_linkage_specification_p)
13901         decl_specifiers->storage_class = sc_extern;
13902       decl = start_decl (declarator, decl_specifiers,
13903                          is_initialized, attributes, prefix_attributes,
13904                          &pushed_scope);
13905     }
13906   else if (scope)
13907     /* Enter the SCOPE.  That way unqualified names appearing in the
13908        initializer will be looked up in SCOPE.  */
13909     pushed_scope = push_scope (scope);
13910
13911   /* Perform deferred access control checks, now that we know in which
13912      SCOPE the declared entity resides.  */
13913   if (!member_p && decl)
13914     {
13915       tree saved_current_function_decl = NULL_TREE;
13916
13917       /* If the entity being declared is a function, pretend that we
13918          are in its scope.  If it is a `friend', it may have access to
13919          things that would not otherwise be accessible.  */
13920       if (TREE_CODE (decl) == FUNCTION_DECL)
13921         {
13922           saved_current_function_decl = current_function_decl;
13923           current_function_decl = decl;
13924         }
13925
13926       /* Perform access checks for template parameters.  */
13927       cp_parser_perform_template_parameter_access_checks (checks);
13928
13929       /* Perform the access control checks for the declarator and the
13930          decl-specifiers.  */
13931       perform_deferred_access_checks ();
13932
13933       /* Restore the saved value.  */
13934       if (TREE_CODE (decl) == FUNCTION_DECL)
13935         current_function_decl = saved_current_function_decl;
13936     }
13937
13938   /* Parse the initializer.  */
13939   initializer = NULL_TREE;
13940   is_direct_init = false;
13941   is_non_constant_init = true;
13942   if (is_initialized)
13943     {
13944       if (function_declarator_p (declarator))
13945         {
13946           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13947            if (initialization_kind == CPP_EQ)
13948              initializer = cp_parser_pure_specifier (parser);
13949            else
13950              {
13951                /* If the declaration was erroneous, we don't really
13952                   know what the user intended, so just silently
13953                   consume the initializer.  */
13954                if (decl != error_mark_node)
13955                  error_at (initializer_start_token->location,
13956                            "initializer provided for function");
13957                cp_parser_skip_to_closing_parenthesis (parser,
13958                                                       /*recovering=*/true,
13959                                                       /*or_comma=*/false,
13960                                                       /*consume_paren=*/true);
13961              }
13962         }
13963       else
13964         {
13965           /* We want to record the extra mangling scope for in-class
13966              initializers of class members and initializers of static data
13967              member templates.  The former is a C++0x feature which isn't
13968              implemented yet, and I expect it will involve deferring
13969              parsing of the initializer until end of class as with default
13970              arguments.  So right here we only handle the latter.  */
13971           if (!member_p && processing_template_decl)
13972             start_lambda_scope (decl);
13973           initializer = cp_parser_initializer (parser,
13974                                                &is_direct_init,
13975                                                &is_non_constant_init);
13976           if (!member_p && processing_template_decl)
13977             finish_lambda_scope ();
13978         }
13979     }
13980
13981   /* The old parser allows attributes to appear after a parenthesized
13982      initializer.  Mark Mitchell proposed removing this functionality
13983      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13984      attributes -- but ignores them.  */
13985   if (cp_parser_allow_gnu_extensions_p (parser)
13986       && initialization_kind == CPP_OPEN_PAREN)
13987     if (cp_parser_attributes_opt (parser))
13988       warning (OPT_Wattributes,
13989                "attributes after parenthesized initializer ignored");
13990
13991   /* For an in-class declaration, use `grokfield' to create the
13992      declaration.  */
13993   if (member_p)
13994     {
13995       if (pushed_scope)
13996         {
13997           pop_scope (pushed_scope);
13998           pushed_scope = false;
13999         }
14000       decl = grokfield (declarator, decl_specifiers,
14001                         initializer, !is_non_constant_init,
14002                         /*asmspec=*/NULL_TREE,
14003                         prefix_attributes);
14004       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14005         cp_parser_save_default_args (parser, decl);
14006     }
14007
14008   /* Finish processing the declaration.  But, skip friend
14009      declarations.  */
14010   if (!friend_p && decl && decl != error_mark_node)
14011     {
14012       cp_finish_decl (decl,
14013                       initializer, !is_non_constant_init,
14014                       asm_specification,
14015                       /* If the initializer is in parentheses, then this is
14016                          a direct-initialization, which means that an
14017                          `explicit' constructor is OK.  Otherwise, an
14018                          `explicit' constructor cannot be used.  */
14019                       ((is_direct_init || !is_initialized)
14020                        ? 0 : LOOKUP_ONLYCONVERTING));
14021     }
14022   else if ((cxx_dialect != cxx98) && friend_p
14023            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14024     /* Core issue #226 (C++0x only): A default template-argument
14025        shall not be specified in a friend class template
14026        declaration. */
14027     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14028                              /*is_partial=*/0, /*is_friend_decl=*/1);
14029
14030   if (!friend_p && pushed_scope)
14031     pop_scope (pushed_scope);
14032
14033   return decl;
14034 }
14035
14036 /* Parse a declarator.
14037
14038    declarator:
14039      direct-declarator
14040      ptr-operator declarator
14041
14042    abstract-declarator:
14043      ptr-operator abstract-declarator [opt]
14044      direct-abstract-declarator
14045
14046    GNU Extensions:
14047
14048    declarator:
14049      attributes [opt] direct-declarator
14050      attributes [opt] ptr-operator declarator
14051
14052    abstract-declarator:
14053      attributes [opt] ptr-operator abstract-declarator [opt]
14054      attributes [opt] direct-abstract-declarator
14055
14056    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14057    detect constructor, destructor or conversion operators. It is set
14058    to -1 if the declarator is a name, and +1 if it is a
14059    function. Otherwise it is set to zero. Usually you just want to
14060    test for >0, but internally the negative value is used.
14061
14062    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14063    a decl-specifier-seq unless it declares a constructor, destructor,
14064    or conversion.  It might seem that we could check this condition in
14065    semantic analysis, rather than parsing, but that makes it difficult
14066    to handle something like `f()'.  We want to notice that there are
14067    no decl-specifiers, and therefore realize that this is an
14068    expression, not a declaration.)
14069
14070    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14071    the declarator is a direct-declarator of the form "(...)".
14072
14073    MEMBER_P is true iff this declarator is a member-declarator.  */
14074
14075 static cp_declarator *
14076 cp_parser_declarator (cp_parser* parser,
14077                       cp_parser_declarator_kind dcl_kind,
14078                       int* ctor_dtor_or_conv_p,
14079                       bool* parenthesized_p,
14080                       bool member_p)
14081 {
14082   cp_declarator *declarator;
14083   enum tree_code code;
14084   cp_cv_quals cv_quals;
14085   tree class_type;
14086   tree attributes = NULL_TREE;
14087
14088   /* Assume this is not a constructor, destructor, or type-conversion
14089      operator.  */
14090   if (ctor_dtor_or_conv_p)
14091     *ctor_dtor_or_conv_p = 0;
14092
14093   if (cp_parser_allow_gnu_extensions_p (parser))
14094     attributes = cp_parser_attributes_opt (parser);
14095
14096   /* Check for the ptr-operator production.  */
14097   cp_parser_parse_tentatively (parser);
14098   /* Parse the ptr-operator.  */
14099   code = cp_parser_ptr_operator (parser,
14100                                  &class_type,
14101                                  &cv_quals);
14102   /* If that worked, then we have a ptr-operator.  */
14103   if (cp_parser_parse_definitely (parser))
14104     {
14105       /* If a ptr-operator was found, then this declarator was not
14106          parenthesized.  */
14107       if (parenthesized_p)
14108         *parenthesized_p = true;
14109       /* The dependent declarator is optional if we are parsing an
14110          abstract-declarator.  */
14111       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14112         cp_parser_parse_tentatively (parser);
14113
14114       /* Parse the dependent declarator.  */
14115       declarator = cp_parser_declarator (parser, dcl_kind,
14116                                          /*ctor_dtor_or_conv_p=*/NULL,
14117                                          /*parenthesized_p=*/NULL,
14118                                          /*member_p=*/false);
14119
14120       /* If we are parsing an abstract-declarator, we must handle the
14121          case where the dependent declarator is absent.  */
14122       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14123           && !cp_parser_parse_definitely (parser))
14124         declarator = NULL;
14125
14126       declarator = cp_parser_make_indirect_declarator
14127         (code, class_type, cv_quals, declarator);
14128     }
14129   /* Everything else is a direct-declarator.  */
14130   else
14131     {
14132       if (parenthesized_p)
14133         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14134                                                    CPP_OPEN_PAREN);
14135       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14136                                                 ctor_dtor_or_conv_p,
14137                                                 member_p);
14138     }
14139
14140   if (attributes && declarator && declarator != cp_error_declarator)
14141     declarator->attributes = attributes;
14142
14143   return declarator;
14144 }
14145
14146 /* Parse a direct-declarator or direct-abstract-declarator.
14147
14148    direct-declarator:
14149      declarator-id
14150      direct-declarator ( parameter-declaration-clause )
14151        cv-qualifier-seq [opt]
14152        exception-specification [opt]
14153      direct-declarator [ constant-expression [opt] ]
14154      ( declarator )
14155
14156    direct-abstract-declarator:
14157      direct-abstract-declarator [opt]
14158        ( parameter-declaration-clause )
14159        cv-qualifier-seq [opt]
14160        exception-specification [opt]
14161      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14162      ( abstract-declarator )
14163
14164    Returns a representation of the declarator.  DCL_KIND is
14165    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14166    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14167    we are parsing a direct-declarator.  It is
14168    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14169    of ambiguity we prefer an abstract declarator, as per
14170    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14171    cp_parser_declarator.  */
14172
14173 static cp_declarator *
14174 cp_parser_direct_declarator (cp_parser* parser,
14175                              cp_parser_declarator_kind dcl_kind,
14176                              int* ctor_dtor_or_conv_p,
14177                              bool member_p)
14178 {
14179   cp_token *token;
14180   cp_declarator *declarator = NULL;
14181   tree scope = NULL_TREE;
14182   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14183   bool saved_in_declarator_p = parser->in_declarator_p;
14184   bool first = true;
14185   tree pushed_scope = NULL_TREE;
14186
14187   while (true)
14188     {
14189       /* Peek at the next token.  */
14190       token = cp_lexer_peek_token (parser->lexer);
14191       if (token->type == CPP_OPEN_PAREN)
14192         {
14193           /* This is either a parameter-declaration-clause, or a
14194              parenthesized declarator. When we know we are parsing a
14195              named declarator, it must be a parenthesized declarator
14196              if FIRST is true. For instance, `(int)' is a
14197              parameter-declaration-clause, with an omitted
14198              direct-abstract-declarator. But `((*))', is a
14199              parenthesized abstract declarator. Finally, when T is a
14200              template parameter `(T)' is a
14201              parameter-declaration-clause, and not a parenthesized
14202              named declarator.
14203
14204              We first try and parse a parameter-declaration-clause,
14205              and then try a nested declarator (if FIRST is true).
14206
14207              It is not an error for it not to be a
14208              parameter-declaration-clause, even when FIRST is
14209              false. Consider,
14210
14211                int i (int);
14212                int i (3);
14213
14214              The first is the declaration of a function while the
14215              second is the definition of a variable, including its
14216              initializer.
14217
14218              Having seen only the parenthesis, we cannot know which of
14219              these two alternatives should be selected.  Even more
14220              complex are examples like:
14221
14222                int i (int (a));
14223                int i (int (3));
14224
14225              The former is a function-declaration; the latter is a
14226              variable initialization.
14227
14228              Thus again, we try a parameter-declaration-clause, and if
14229              that fails, we back out and return.  */
14230
14231           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14232             {
14233               tree params;
14234               unsigned saved_num_template_parameter_lists;
14235               bool is_declarator = false;
14236               tree t;
14237
14238               /* In a member-declarator, the only valid interpretation
14239                  of a parenthesis is the start of a
14240                  parameter-declaration-clause.  (It is invalid to
14241                  initialize a static data member with a parenthesized
14242                  initializer; only the "=" form of initialization is
14243                  permitted.)  */
14244               if (!member_p)
14245                 cp_parser_parse_tentatively (parser);
14246
14247               /* Consume the `('.  */
14248               cp_lexer_consume_token (parser->lexer);
14249               if (first)
14250                 {
14251                   /* If this is going to be an abstract declarator, we're
14252                      in a declarator and we can't have default args.  */
14253                   parser->default_arg_ok_p = false;
14254                   parser->in_declarator_p = true;
14255                 }
14256
14257               /* Inside the function parameter list, surrounding
14258                  template-parameter-lists do not apply.  */
14259               saved_num_template_parameter_lists
14260                 = parser->num_template_parameter_lists;
14261               parser->num_template_parameter_lists = 0;
14262
14263               begin_scope (sk_function_parms, NULL_TREE);
14264
14265               /* Parse the parameter-declaration-clause.  */
14266               params = cp_parser_parameter_declaration_clause (parser);
14267
14268               parser->num_template_parameter_lists
14269                 = saved_num_template_parameter_lists;
14270
14271               /* If all went well, parse the cv-qualifier-seq and the
14272                  exception-specification.  */
14273               if (member_p || cp_parser_parse_definitely (parser))
14274                 {
14275                   cp_cv_quals cv_quals;
14276                   tree exception_specification;
14277                   tree late_return;
14278
14279                   is_declarator = true;
14280
14281                   if (ctor_dtor_or_conv_p)
14282                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14283                   first = false;
14284                   /* Consume the `)'.  */
14285                   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14286
14287                   /* Parse the cv-qualifier-seq.  */
14288                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14289                   /* And the exception-specification.  */
14290                   exception_specification
14291                     = cp_parser_exception_specification_opt (parser);
14292
14293                   late_return
14294                     = cp_parser_late_return_type_opt (parser);
14295
14296                   /* Create the function-declarator.  */
14297                   declarator = make_call_declarator (declarator,
14298                                                      params,
14299                                                      cv_quals,
14300                                                      exception_specification,
14301                                                      late_return);
14302                   /* Any subsequent parameter lists are to do with
14303                      return type, so are not those of the declared
14304                      function.  */
14305                   parser->default_arg_ok_p = false;
14306                 }
14307
14308               /* Remove the function parms from scope.  */
14309               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14310                 pop_binding (DECL_NAME (t), t);
14311               leave_scope();
14312
14313               if (is_declarator)
14314                 /* Repeat the main loop.  */
14315                 continue;
14316             }
14317
14318           /* If this is the first, we can try a parenthesized
14319              declarator.  */
14320           if (first)
14321             {
14322               bool saved_in_type_id_in_expr_p;
14323
14324               parser->default_arg_ok_p = saved_default_arg_ok_p;
14325               parser->in_declarator_p = saved_in_declarator_p;
14326
14327               /* Consume the `('.  */
14328               cp_lexer_consume_token (parser->lexer);
14329               /* Parse the nested declarator.  */
14330               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14331               parser->in_type_id_in_expr_p = true;
14332               declarator
14333                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14334                                         /*parenthesized_p=*/NULL,
14335                                         member_p);
14336               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14337               first = false;
14338               /* Expect a `)'.  */
14339               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14340                 declarator = cp_error_declarator;
14341               if (declarator == cp_error_declarator)
14342                 break;
14343
14344               goto handle_declarator;
14345             }
14346           /* Otherwise, we must be done.  */
14347           else
14348             break;
14349         }
14350       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14351                && token->type == CPP_OPEN_SQUARE)
14352         {
14353           /* Parse an array-declarator.  */
14354           tree bounds;
14355
14356           if (ctor_dtor_or_conv_p)
14357             *ctor_dtor_or_conv_p = 0;
14358
14359           first = false;
14360           parser->default_arg_ok_p = false;
14361           parser->in_declarator_p = true;
14362           /* Consume the `['.  */
14363           cp_lexer_consume_token (parser->lexer);
14364           /* Peek at the next token.  */
14365           token = cp_lexer_peek_token (parser->lexer);
14366           /* If the next token is `]', then there is no
14367              constant-expression.  */
14368           if (token->type != CPP_CLOSE_SQUARE)
14369             {
14370               bool non_constant_p;
14371
14372               bounds
14373                 = cp_parser_constant_expression (parser,
14374                                                  /*allow_non_constant=*/true,
14375                                                  &non_constant_p);
14376               if (!non_constant_p)
14377                 bounds = fold_non_dependent_expr (bounds);
14378               /* Normally, the array bound must be an integral constant
14379                  expression.  However, as an extension, we allow VLAs
14380                  in function scopes as long as they aren't part of a
14381                  parameter declaration.  */
14382               else if (!parser->in_function_body
14383                        || current_binding_level->kind == sk_function_parms)
14384                 {
14385                   cp_parser_error (parser,
14386                                    "array bound is not an integer constant");
14387                   bounds = error_mark_node;
14388                 }
14389               else if (processing_template_decl && !error_operand_p (bounds))
14390                 {
14391                   /* Remember this wasn't a constant-expression.  */
14392                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14393                   TREE_SIDE_EFFECTS (bounds) = 1;
14394                 }
14395             }
14396           else
14397             bounds = NULL_TREE;
14398           /* Look for the closing `]'.  */
14399           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14400             {
14401               declarator = cp_error_declarator;
14402               break;
14403             }
14404
14405           declarator = make_array_declarator (declarator, bounds);
14406         }
14407       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14408         {
14409           {
14410             tree qualifying_scope;
14411             tree unqualified_name;
14412             special_function_kind sfk;
14413             bool abstract_ok;
14414             bool pack_expansion_p = false;
14415             cp_token *declarator_id_start_token;
14416
14417             /* Parse a declarator-id */
14418             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14419             if (abstract_ok)
14420               {
14421                 cp_parser_parse_tentatively (parser);
14422
14423                 /* If we see an ellipsis, we should be looking at a
14424                    parameter pack. */
14425                 if (token->type == CPP_ELLIPSIS)
14426                   {
14427                     /* Consume the `...' */
14428                     cp_lexer_consume_token (parser->lexer);
14429
14430                     pack_expansion_p = true;
14431                   }
14432               }
14433
14434             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14435             unqualified_name
14436               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14437             qualifying_scope = parser->scope;
14438             if (abstract_ok)
14439               {
14440                 bool okay = false;
14441
14442                 if (!unqualified_name && pack_expansion_p)
14443                   {
14444                     /* Check whether an error occurred. */
14445                     okay = !cp_parser_error_occurred (parser);
14446
14447                     /* We already consumed the ellipsis to mark a
14448                        parameter pack, but we have no way to report it,
14449                        so abort the tentative parse. We will be exiting
14450                        immediately anyway. */
14451                     cp_parser_abort_tentative_parse (parser);
14452                   }
14453                 else
14454                   okay = cp_parser_parse_definitely (parser);
14455
14456                 if (!okay)
14457                   unqualified_name = error_mark_node;
14458                 else if (unqualified_name
14459                          && (qualifying_scope
14460                              || (TREE_CODE (unqualified_name)
14461                                  != IDENTIFIER_NODE)))
14462                   {
14463                     cp_parser_error (parser, "expected unqualified-id");
14464                     unqualified_name = error_mark_node;
14465                   }
14466               }
14467
14468             if (!unqualified_name)
14469               return NULL;
14470             if (unqualified_name == error_mark_node)
14471               {
14472                 declarator = cp_error_declarator;
14473                 pack_expansion_p = false;
14474                 declarator->parameter_pack_p = false;
14475                 break;
14476               }
14477
14478             if (qualifying_scope && at_namespace_scope_p ()
14479                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14480               {
14481                 /* In the declaration of a member of a template class
14482                    outside of the class itself, the SCOPE will sometimes
14483                    be a TYPENAME_TYPE.  For example, given:
14484
14485                    template <typename T>
14486                    int S<T>::R::i = 3;
14487
14488                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14489                    this context, we must resolve S<T>::R to an ordinary
14490                    type, rather than a typename type.
14491
14492                    The reason we normally avoid resolving TYPENAME_TYPEs
14493                    is that a specialization of `S' might render
14494                    `S<T>::R' not a type.  However, if `S' is
14495                    specialized, then this `i' will not be used, so there
14496                    is no harm in resolving the types here.  */
14497                 tree type;
14498
14499                 /* Resolve the TYPENAME_TYPE.  */
14500                 type = resolve_typename_type (qualifying_scope,
14501                                               /*only_current_p=*/false);
14502                 /* If that failed, the declarator is invalid.  */
14503                 if (TREE_CODE (type) == TYPENAME_TYPE)
14504                   {
14505                     if (typedef_variant_p (type))
14506                       error_at (declarator_id_start_token->location,
14507                                 "cannot define member of dependent typedef "
14508                                 "%qT", type);
14509                     else
14510                       error_at (declarator_id_start_token->location,
14511                                 "%<%T::%E%> is not a type",
14512                                 TYPE_CONTEXT (qualifying_scope),
14513                                 TYPE_IDENTIFIER (qualifying_scope));
14514                   }
14515                 qualifying_scope = type;
14516               }
14517
14518             sfk = sfk_none;
14519
14520             if (unqualified_name)
14521               {
14522                 tree class_type;
14523
14524                 if (qualifying_scope
14525                     && CLASS_TYPE_P (qualifying_scope))
14526                   class_type = qualifying_scope;
14527                 else
14528                   class_type = current_class_type;
14529
14530                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14531                   {
14532                     tree name_type = TREE_TYPE (unqualified_name);
14533                     if (class_type && same_type_p (name_type, class_type))
14534                       {
14535                         if (qualifying_scope
14536                             && CLASSTYPE_USE_TEMPLATE (name_type))
14537                           {
14538                             error_at (declarator_id_start_token->location,
14539                                       "invalid use of constructor as a template");
14540                             inform (declarator_id_start_token->location,
14541                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14542                                     "name the constructor in a qualified name",
14543                                     class_type,
14544                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14545                                     class_type, name_type);
14546                             declarator = cp_error_declarator;
14547                             break;
14548                           }
14549                         else
14550                           unqualified_name = constructor_name (class_type);
14551                       }
14552                     else
14553                       {
14554                         /* We do not attempt to print the declarator
14555                            here because we do not have enough
14556                            information about its original syntactic
14557                            form.  */
14558                         cp_parser_error (parser, "invalid declarator");
14559                         declarator = cp_error_declarator;
14560                         break;
14561                       }
14562                   }
14563
14564                 if (class_type)
14565                   {
14566                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14567                       sfk = sfk_destructor;
14568                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14569                       sfk = sfk_conversion;
14570                     else if (/* There's no way to declare a constructor
14571                                 for an anonymous type, even if the type
14572                                 got a name for linkage purposes.  */
14573                              !TYPE_WAS_ANONYMOUS (class_type)
14574                              && constructor_name_p (unqualified_name,
14575                                                     class_type))
14576                       {
14577                         unqualified_name = constructor_name (class_type);
14578                         sfk = sfk_constructor;
14579                       }
14580                     else if (is_overloaded_fn (unqualified_name)
14581                              && DECL_CONSTRUCTOR_P (get_first_fn
14582                                                     (unqualified_name)))
14583                       sfk = sfk_constructor;
14584
14585                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14586                       *ctor_dtor_or_conv_p = -1;
14587                   }
14588               }
14589             declarator = make_id_declarator (qualifying_scope,
14590                                              unqualified_name,
14591                                              sfk);
14592             declarator->id_loc = token->location;
14593             declarator->parameter_pack_p = pack_expansion_p;
14594
14595             if (pack_expansion_p)
14596               maybe_warn_variadic_templates ();
14597           }
14598
14599         handle_declarator:;
14600           scope = get_scope_of_declarator (declarator);
14601           if (scope)
14602             /* Any names that appear after the declarator-id for a
14603                member are looked up in the containing scope.  */
14604             pushed_scope = push_scope (scope);
14605           parser->in_declarator_p = true;
14606           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14607               || (declarator && declarator->kind == cdk_id))
14608             /* Default args are only allowed on function
14609                declarations.  */
14610             parser->default_arg_ok_p = saved_default_arg_ok_p;
14611           else
14612             parser->default_arg_ok_p = false;
14613
14614           first = false;
14615         }
14616       /* We're done.  */
14617       else
14618         break;
14619     }
14620
14621   /* For an abstract declarator, we might wind up with nothing at this
14622      point.  That's an error; the declarator is not optional.  */
14623   if (!declarator)
14624     cp_parser_error (parser, "expected declarator");
14625
14626   /* If we entered a scope, we must exit it now.  */
14627   if (pushed_scope)
14628     pop_scope (pushed_scope);
14629
14630   parser->default_arg_ok_p = saved_default_arg_ok_p;
14631   parser->in_declarator_p = saved_in_declarator_p;
14632
14633   return declarator;
14634 }
14635
14636 /* Parse a ptr-operator.
14637
14638    ptr-operator:
14639      * cv-qualifier-seq [opt]
14640      &
14641      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14642
14643    GNU Extension:
14644
14645    ptr-operator:
14646      & cv-qualifier-seq [opt]
14647
14648    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14649    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14650    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14651    filled in with the TYPE containing the member.  *CV_QUALS is
14652    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14653    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14654    Note that the tree codes returned by this function have nothing
14655    to do with the types of trees that will be eventually be created
14656    to represent the pointer or reference type being parsed. They are
14657    just constants with suggestive names. */
14658 static enum tree_code
14659 cp_parser_ptr_operator (cp_parser* parser,
14660                         tree* type,
14661                         cp_cv_quals *cv_quals)
14662 {
14663   enum tree_code code = ERROR_MARK;
14664   cp_token *token;
14665
14666   /* Assume that it's not a pointer-to-member.  */
14667   *type = NULL_TREE;
14668   /* And that there are no cv-qualifiers.  */
14669   *cv_quals = TYPE_UNQUALIFIED;
14670
14671   /* Peek at the next token.  */
14672   token = cp_lexer_peek_token (parser->lexer);
14673
14674   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14675   if (token->type == CPP_MULT)
14676     code = INDIRECT_REF;
14677   else if (token->type == CPP_AND)
14678     code = ADDR_EXPR;
14679   else if ((cxx_dialect != cxx98) &&
14680            token->type == CPP_AND_AND) /* C++0x only */
14681     code = NON_LVALUE_EXPR;
14682
14683   if (code != ERROR_MARK)
14684     {
14685       /* Consume the `*', `&' or `&&'.  */
14686       cp_lexer_consume_token (parser->lexer);
14687
14688       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14689          `&', if we are allowing GNU extensions.  (The only qualifier
14690          that can legally appear after `&' is `restrict', but that is
14691          enforced during semantic analysis.  */
14692       if (code == INDIRECT_REF
14693           || cp_parser_allow_gnu_extensions_p (parser))
14694         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14695     }
14696   else
14697     {
14698       /* Try the pointer-to-member case.  */
14699       cp_parser_parse_tentatively (parser);
14700       /* Look for the optional `::' operator.  */
14701       cp_parser_global_scope_opt (parser,
14702                                   /*current_scope_valid_p=*/false);
14703       /* Look for the nested-name specifier.  */
14704       token = cp_lexer_peek_token (parser->lexer);
14705       cp_parser_nested_name_specifier (parser,
14706                                        /*typename_keyword_p=*/false,
14707                                        /*check_dependency_p=*/true,
14708                                        /*type_p=*/false,
14709                                        /*is_declaration=*/false);
14710       /* If we found it, and the next token is a `*', then we are
14711          indeed looking at a pointer-to-member operator.  */
14712       if (!cp_parser_error_occurred (parser)
14713           && cp_parser_require (parser, CPP_MULT, RT_MULT))
14714         {
14715           /* Indicate that the `*' operator was used.  */
14716           code = INDIRECT_REF;
14717
14718           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14719             error_at (token->location, "%qD is a namespace", parser->scope);
14720           else
14721             {
14722               /* The type of which the member is a member is given by the
14723                  current SCOPE.  */
14724               *type = parser->scope;
14725               /* The next name will not be qualified.  */
14726               parser->scope = NULL_TREE;
14727               parser->qualifying_scope = NULL_TREE;
14728               parser->object_scope = NULL_TREE;
14729               /* Look for the optional cv-qualifier-seq.  */
14730               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14731             }
14732         }
14733       /* If that didn't work we don't have a ptr-operator.  */
14734       if (!cp_parser_parse_definitely (parser))
14735         cp_parser_error (parser, "expected ptr-operator");
14736     }
14737
14738   return code;
14739 }
14740
14741 /* Parse an (optional) cv-qualifier-seq.
14742
14743    cv-qualifier-seq:
14744      cv-qualifier cv-qualifier-seq [opt]
14745
14746    cv-qualifier:
14747      const
14748      volatile
14749
14750    GNU Extension:
14751
14752    cv-qualifier:
14753      __restrict__
14754
14755    Returns a bitmask representing the cv-qualifiers.  */
14756
14757 static cp_cv_quals
14758 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14759 {
14760   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14761
14762   while (true)
14763     {
14764       cp_token *token;
14765       cp_cv_quals cv_qualifier;
14766
14767       /* Peek at the next token.  */
14768       token = cp_lexer_peek_token (parser->lexer);
14769       /* See if it's a cv-qualifier.  */
14770       switch (token->keyword)
14771         {
14772         case RID_CONST:
14773           cv_qualifier = TYPE_QUAL_CONST;
14774           break;
14775
14776         case RID_VOLATILE:
14777           cv_qualifier = TYPE_QUAL_VOLATILE;
14778           break;
14779
14780         case RID_RESTRICT:
14781           cv_qualifier = TYPE_QUAL_RESTRICT;
14782           break;
14783
14784         default:
14785           cv_qualifier = TYPE_UNQUALIFIED;
14786           break;
14787         }
14788
14789       if (!cv_qualifier)
14790         break;
14791
14792       if (cv_quals & cv_qualifier)
14793         {
14794           error_at (token->location, "duplicate cv-qualifier");
14795           cp_lexer_purge_token (parser->lexer);
14796         }
14797       else
14798         {
14799           cp_lexer_consume_token (parser->lexer);
14800           cv_quals |= cv_qualifier;
14801         }
14802     }
14803
14804   return cv_quals;
14805 }
14806
14807 /* Parse a late-specified return type, if any.  This is not a separate
14808    non-terminal, but part of a function declarator, which looks like
14809
14810    -> trailing-type-specifier-seq abstract-declarator(opt)
14811
14812    Returns the type indicated by the type-id.  */
14813
14814 static tree
14815 cp_parser_late_return_type_opt (cp_parser* parser)
14816 {
14817   cp_token *token;
14818
14819   /* Peek at the next token.  */
14820   token = cp_lexer_peek_token (parser->lexer);
14821   /* A late-specified return type is indicated by an initial '->'. */
14822   if (token->type != CPP_DEREF)
14823     return NULL_TREE;
14824
14825   /* Consume the ->.  */
14826   cp_lexer_consume_token (parser->lexer);
14827
14828   return cp_parser_trailing_type_id (parser);
14829 }
14830
14831 /* Parse a declarator-id.
14832
14833    declarator-id:
14834      id-expression
14835      :: [opt] nested-name-specifier [opt] type-name
14836
14837    In the `id-expression' case, the value returned is as for
14838    cp_parser_id_expression if the id-expression was an unqualified-id.
14839    If the id-expression was a qualified-id, then a SCOPE_REF is
14840    returned.  The first operand is the scope (either a NAMESPACE_DECL
14841    or TREE_TYPE), but the second is still just a representation of an
14842    unqualified-id.  */
14843
14844 static tree
14845 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14846 {
14847   tree id;
14848   /* The expression must be an id-expression.  Assume that qualified
14849      names are the names of types so that:
14850
14851        template <class T>
14852        int S<T>::R::i = 3;
14853
14854      will work; we must treat `S<T>::R' as the name of a type.
14855      Similarly, assume that qualified names are templates, where
14856      required, so that:
14857
14858        template <class T>
14859        int S<T>::R<T>::i = 3;
14860
14861      will work, too.  */
14862   id = cp_parser_id_expression (parser,
14863                                 /*template_keyword_p=*/false,
14864                                 /*check_dependency_p=*/false,
14865                                 /*template_p=*/NULL,
14866                                 /*declarator_p=*/true,
14867                                 optional_p);
14868   if (id && BASELINK_P (id))
14869     id = BASELINK_FUNCTIONS (id);
14870   return id;
14871 }
14872
14873 /* Parse a type-id.
14874
14875    type-id:
14876      type-specifier-seq abstract-declarator [opt]
14877
14878    Returns the TYPE specified.  */
14879
14880 static tree
14881 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14882                      bool is_trailing_return)
14883 {
14884   cp_decl_specifier_seq type_specifier_seq;
14885   cp_declarator *abstract_declarator;
14886
14887   /* Parse the type-specifier-seq.  */
14888   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14889                                 is_trailing_return,
14890                                 &type_specifier_seq);
14891   if (type_specifier_seq.type == error_mark_node)
14892     return error_mark_node;
14893
14894   /* There might or might not be an abstract declarator.  */
14895   cp_parser_parse_tentatively (parser);
14896   /* Look for the declarator.  */
14897   abstract_declarator
14898     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14899                             /*parenthesized_p=*/NULL,
14900                             /*member_p=*/false);
14901   /* Check to see if there really was a declarator.  */
14902   if (!cp_parser_parse_definitely (parser))
14903     abstract_declarator = NULL;
14904
14905   if (type_specifier_seq.type
14906       && type_uses_auto (type_specifier_seq.type))
14907     {
14908       /* A type-id with type 'auto' is only ok if the abstract declarator
14909          is a function declarator with a late-specified return type.  */
14910       if (abstract_declarator
14911           && abstract_declarator->kind == cdk_function
14912           && abstract_declarator->u.function.late_return_type)
14913         /* OK */;
14914       else
14915         {
14916           error ("invalid use of %<auto%>");
14917           return error_mark_node;
14918         }
14919     }
14920   
14921   return groktypename (&type_specifier_seq, abstract_declarator,
14922                        is_template_arg);
14923 }
14924
14925 static tree cp_parser_type_id (cp_parser *parser)
14926 {
14927   return cp_parser_type_id_1 (parser, false, false);
14928 }
14929
14930 static tree cp_parser_template_type_arg (cp_parser *parser)
14931 {
14932   return cp_parser_type_id_1 (parser, true, false);
14933 }
14934
14935 static tree cp_parser_trailing_type_id (cp_parser *parser)
14936 {
14937   return cp_parser_type_id_1 (parser, false, true);
14938 }
14939
14940 /* Parse a type-specifier-seq.
14941
14942    type-specifier-seq:
14943      type-specifier type-specifier-seq [opt]
14944
14945    GNU extension:
14946
14947    type-specifier-seq:
14948      attributes type-specifier-seq [opt]
14949
14950    If IS_DECLARATION is true, we are at the start of a "condition" or
14951    exception-declaration, so we might be followed by a declarator-id.
14952
14953    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14954    i.e. we've just seen "->".
14955
14956    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14957
14958 static void
14959 cp_parser_type_specifier_seq (cp_parser* parser,
14960                               bool is_declaration,
14961                               bool is_trailing_return,
14962                               cp_decl_specifier_seq *type_specifier_seq)
14963 {
14964   bool seen_type_specifier = false;
14965   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14966   cp_token *start_token = NULL;
14967
14968   /* Clear the TYPE_SPECIFIER_SEQ.  */
14969   clear_decl_specs (type_specifier_seq);
14970
14971   /* In the context of a trailing return type, enum E { } is an
14972      elaborated-type-specifier followed by a function-body, not an
14973      enum-specifier.  */
14974   if (is_trailing_return)
14975     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14976
14977   /* Parse the type-specifiers and attributes.  */
14978   while (true)
14979     {
14980       tree type_specifier;
14981       bool is_cv_qualifier;
14982
14983       /* Check for attributes first.  */
14984       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14985         {
14986           type_specifier_seq->attributes =
14987             chainon (type_specifier_seq->attributes,
14988                      cp_parser_attributes_opt (parser));
14989           continue;
14990         }
14991
14992       /* record the token of the beginning of the type specifier seq,
14993          for error reporting purposes*/
14994      if (!start_token)
14995        start_token = cp_lexer_peek_token (parser->lexer);
14996
14997       /* Look for the type-specifier.  */
14998       type_specifier = cp_parser_type_specifier (parser,
14999                                                  flags,
15000                                                  type_specifier_seq,
15001                                                  /*is_declaration=*/false,
15002                                                  NULL,
15003                                                  &is_cv_qualifier);
15004       if (!type_specifier)
15005         {
15006           /* If the first type-specifier could not be found, this is not a
15007              type-specifier-seq at all.  */
15008           if (!seen_type_specifier)
15009             {
15010               cp_parser_error (parser, "expected type-specifier");
15011               type_specifier_seq->type = error_mark_node;
15012               return;
15013             }
15014           /* If subsequent type-specifiers could not be found, the
15015              type-specifier-seq is complete.  */
15016           break;
15017         }
15018
15019       seen_type_specifier = true;
15020       /* The standard says that a condition can be:
15021
15022             type-specifier-seq declarator = assignment-expression
15023
15024          However, given:
15025
15026            struct S {};
15027            if (int S = ...)
15028
15029          we should treat the "S" as a declarator, not as a
15030          type-specifier.  The standard doesn't say that explicitly for
15031          type-specifier-seq, but it does say that for
15032          decl-specifier-seq in an ordinary declaration.  Perhaps it
15033          would be clearer just to allow a decl-specifier-seq here, and
15034          then add a semantic restriction that if any decl-specifiers
15035          that are not type-specifiers appear, the program is invalid.  */
15036       if (is_declaration && !is_cv_qualifier)
15037         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15038     }
15039
15040   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15041 }
15042
15043 /* Parse a parameter-declaration-clause.
15044
15045    parameter-declaration-clause:
15046      parameter-declaration-list [opt] ... [opt]
15047      parameter-declaration-list , ...
15048
15049    Returns a representation for the parameter declarations.  A return
15050    value of NULL indicates a parameter-declaration-clause consisting
15051    only of an ellipsis.  */
15052
15053 static tree
15054 cp_parser_parameter_declaration_clause (cp_parser* parser)
15055 {
15056   tree parameters;
15057   cp_token *token;
15058   bool ellipsis_p;
15059   bool is_error;
15060
15061   /* Peek at the next token.  */
15062   token = cp_lexer_peek_token (parser->lexer);
15063   /* Check for trivial parameter-declaration-clauses.  */
15064   if (token->type == CPP_ELLIPSIS)
15065     {
15066       /* Consume the `...' token.  */
15067       cp_lexer_consume_token (parser->lexer);
15068       return NULL_TREE;
15069     }
15070   else if (token->type == CPP_CLOSE_PAREN)
15071     /* There are no parameters.  */
15072     {
15073 #ifndef NO_IMPLICIT_EXTERN_C
15074       if (in_system_header && current_class_type == NULL
15075           && current_lang_name == lang_name_c)
15076         return NULL_TREE;
15077       else
15078 #endif
15079         return void_list_node;
15080     }
15081   /* Check for `(void)', too, which is a special case.  */
15082   else if (token->keyword == RID_VOID
15083            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15084                == CPP_CLOSE_PAREN))
15085     {
15086       /* Consume the `void' token.  */
15087       cp_lexer_consume_token (parser->lexer);
15088       /* There are no parameters.  */
15089       return void_list_node;
15090     }
15091
15092   /* Parse the parameter-declaration-list.  */
15093   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15094   /* If a parse error occurred while parsing the
15095      parameter-declaration-list, then the entire
15096      parameter-declaration-clause is erroneous.  */
15097   if (is_error)
15098     return NULL;
15099
15100   /* Peek at the next token.  */
15101   token = cp_lexer_peek_token (parser->lexer);
15102   /* If it's a `,', the clause should terminate with an ellipsis.  */
15103   if (token->type == CPP_COMMA)
15104     {
15105       /* Consume the `,'.  */
15106       cp_lexer_consume_token (parser->lexer);
15107       /* Expect an ellipsis.  */
15108       ellipsis_p
15109         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15110     }
15111   /* It might also be `...' if the optional trailing `,' was
15112      omitted.  */
15113   else if (token->type == CPP_ELLIPSIS)
15114     {
15115       /* Consume the `...' token.  */
15116       cp_lexer_consume_token (parser->lexer);
15117       /* And remember that we saw it.  */
15118       ellipsis_p = true;
15119     }
15120   else
15121     ellipsis_p = false;
15122
15123   /* Finish the parameter list.  */
15124   if (!ellipsis_p)
15125     parameters = chainon (parameters, void_list_node);
15126
15127   return parameters;
15128 }
15129
15130 /* Parse a parameter-declaration-list.
15131
15132    parameter-declaration-list:
15133      parameter-declaration
15134      parameter-declaration-list , parameter-declaration
15135
15136    Returns a representation of the parameter-declaration-list, as for
15137    cp_parser_parameter_declaration_clause.  However, the
15138    `void_list_node' is never appended to the list.  Upon return,
15139    *IS_ERROR will be true iff an error occurred.  */
15140
15141 static tree
15142 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15143 {
15144   tree parameters = NULL_TREE;
15145   tree *tail = &parameters; 
15146   bool saved_in_unbraced_linkage_specification_p;
15147   int index = 0;
15148
15149   /* Assume all will go well.  */
15150   *is_error = false;
15151   /* The special considerations that apply to a function within an
15152      unbraced linkage specifications do not apply to the parameters
15153      to the function.  */
15154   saved_in_unbraced_linkage_specification_p 
15155     = parser->in_unbraced_linkage_specification_p;
15156   parser->in_unbraced_linkage_specification_p = false;
15157
15158   /* Look for more parameters.  */
15159   while (true)
15160     {
15161       cp_parameter_declarator *parameter;
15162       tree decl = error_mark_node;
15163       bool parenthesized_p;
15164       /* Parse the parameter.  */
15165       parameter
15166         = cp_parser_parameter_declaration (parser,
15167                                            /*template_parm_p=*/false,
15168                                            &parenthesized_p);
15169
15170       /* We don't know yet if the enclosing context is deprecated, so wait
15171          and warn in grokparms if appropriate.  */
15172       deprecated_state = DEPRECATED_SUPPRESS;
15173
15174       if (parameter)
15175         decl = grokdeclarator (parameter->declarator,
15176                                &parameter->decl_specifiers,
15177                                PARM,
15178                                parameter->default_argument != NULL_TREE,
15179                                &parameter->decl_specifiers.attributes);
15180
15181       deprecated_state = DEPRECATED_NORMAL;
15182
15183       /* If a parse error occurred parsing the parameter declaration,
15184          then the entire parameter-declaration-list is erroneous.  */
15185       if (decl == error_mark_node)
15186         {
15187           *is_error = true;
15188           parameters = error_mark_node;
15189           break;
15190         }
15191
15192       if (parameter->decl_specifiers.attributes)
15193         cplus_decl_attributes (&decl,
15194                                parameter->decl_specifiers.attributes,
15195                                0);
15196       if (DECL_NAME (decl))
15197         decl = pushdecl (decl);
15198
15199       if (decl != error_mark_node)
15200         {
15201           retrofit_lang_decl (decl);
15202           DECL_PARM_INDEX (decl) = ++index;
15203         }
15204
15205       /* Add the new parameter to the list.  */
15206       *tail = build_tree_list (parameter->default_argument, decl);
15207       tail = &TREE_CHAIN (*tail);
15208
15209       /* Peek at the next token.  */
15210       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15211           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15212           /* These are for Objective-C++ */
15213           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15214           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15215         /* The parameter-declaration-list is complete.  */
15216         break;
15217       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15218         {
15219           cp_token *token;
15220
15221           /* Peek at the next token.  */
15222           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15223           /* If it's an ellipsis, then the list is complete.  */
15224           if (token->type == CPP_ELLIPSIS)
15225             break;
15226           /* Otherwise, there must be more parameters.  Consume the
15227              `,'.  */
15228           cp_lexer_consume_token (parser->lexer);
15229           /* When parsing something like:
15230
15231                 int i(float f, double d)
15232
15233              we can tell after seeing the declaration for "f" that we
15234              are not looking at an initialization of a variable "i",
15235              but rather at the declaration of a function "i".
15236
15237              Due to the fact that the parsing of template arguments
15238              (as specified to a template-id) requires backtracking we
15239              cannot use this technique when inside a template argument
15240              list.  */
15241           if (!parser->in_template_argument_list_p
15242               && !parser->in_type_id_in_expr_p
15243               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15244               /* However, a parameter-declaration of the form
15245                  "foat(f)" (which is a valid declaration of a
15246                  parameter "f") can also be interpreted as an
15247                  expression (the conversion of "f" to "float").  */
15248               && !parenthesized_p)
15249             cp_parser_commit_to_tentative_parse (parser);
15250         }
15251       else
15252         {
15253           cp_parser_error (parser, "expected %<,%> or %<...%>");
15254           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15255             cp_parser_skip_to_closing_parenthesis (parser,
15256                                                    /*recovering=*/true,
15257                                                    /*or_comma=*/false,
15258                                                    /*consume_paren=*/false);
15259           break;
15260         }
15261     }
15262
15263   parser->in_unbraced_linkage_specification_p
15264     = saved_in_unbraced_linkage_specification_p;
15265
15266   return parameters;
15267 }
15268
15269 /* Parse a parameter declaration.
15270
15271    parameter-declaration:
15272      decl-specifier-seq ... [opt] declarator
15273      decl-specifier-seq declarator = assignment-expression
15274      decl-specifier-seq ... [opt] abstract-declarator [opt]
15275      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15276
15277    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15278    declares a template parameter.  (In that case, a non-nested `>'
15279    token encountered during the parsing of the assignment-expression
15280    is not interpreted as a greater-than operator.)
15281
15282    Returns a representation of the parameter, or NULL if an error
15283    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15284    true iff the declarator is of the form "(p)".  */
15285
15286 static cp_parameter_declarator *
15287 cp_parser_parameter_declaration (cp_parser *parser,
15288                                  bool template_parm_p,
15289                                  bool *parenthesized_p)
15290 {
15291   int declares_class_or_enum;
15292   cp_decl_specifier_seq decl_specifiers;
15293   cp_declarator *declarator;
15294   tree default_argument;
15295   cp_token *token = NULL, *declarator_token_start = NULL;
15296   const char *saved_message;
15297
15298   /* In a template parameter, `>' is not an operator.
15299
15300      [temp.param]
15301
15302      When parsing a default template-argument for a non-type
15303      template-parameter, the first non-nested `>' is taken as the end
15304      of the template parameter-list rather than a greater-than
15305      operator.  */
15306
15307   /* Type definitions may not appear in parameter types.  */
15308   saved_message = parser->type_definition_forbidden_message;
15309   parser->type_definition_forbidden_message
15310     = G_("types may not be defined in parameter types");
15311
15312   /* Parse the declaration-specifiers.  */
15313   cp_parser_decl_specifier_seq (parser,
15314                                 CP_PARSER_FLAGS_NONE,
15315                                 &decl_specifiers,
15316                                 &declares_class_or_enum);
15317
15318   /* Complain about missing 'typename' or other invalid type names.  */
15319   if (!decl_specifiers.any_type_specifiers_p)
15320     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15321
15322   /* If an error occurred, there's no reason to attempt to parse the
15323      rest of the declaration.  */
15324   if (cp_parser_error_occurred (parser))
15325     {
15326       parser->type_definition_forbidden_message = saved_message;
15327       return NULL;
15328     }
15329
15330   /* Peek at the next token.  */
15331   token = cp_lexer_peek_token (parser->lexer);
15332
15333   /* If the next token is a `)', `,', `=', `>', or `...', then there
15334      is no declarator. However, when variadic templates are enabled,
15335      there may be a declarator following `...'.  */
15336   if (token->type == CPP_CLOSE_PAREN
15337       || token->type == CPP_COMMA
15338       || token->type == CPP_EQ
15339       || token->type == CPP_GREATER)
15340     {
15341       declarator = NULL;
15342       if (parenthesized_p)
15343         *parenthesized_p = false;
15344     }
15345   /* Otherwise, there should be a declarator.  */
15346   else
15347     {
15348       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15349       parser->default_arg_ok_p = false;
15350
15351       /* After seeing a decl-specifier-seq, if the next token is not a
15352          "(", there is no possibility that the code is a valid
15353          expression.  Therefore, if parsing tentatively, we commit at
15354          this point.  */
15355       if (!parser->in_template_argument_list_p
15356           /* In an expression context, having seen:
15357
15358                (int((char ...
15359
15360              we cannot be sure whether we are looking at a
15361              function-type (taking a "char" as a parameter) or a cast
15362              of some object of type "char" to "int".  */
15363           && !parser->in_type_id_in_expr_p
15364           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15365           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15366         cp_parser_commit_to_tentative_parse (parser);
15367       /* Parse the declarator.  */
15368       declarator_token_start = token;
15369       declarator = cp_parser_declarator (parser,
15370                                          CP_PARSER_DECLARATOR_EITHER,
15371                                          /*ctor_dtor_or_conv_p=*/NULL,
15372                                          parenthesized_p,
15373                                          /*member_p=*/false);
15374       parser->default_arg_ok_p = saved_default_arg_ok_p;
15375       /* After the declarator, allow more attributes.  */
15376       decl_specifiers.attributes
15377         = chainon (decl_specifiers.attributes,
15378                    cp_parser_attributes_opt (parser));
15379     }
15380
15381   /* If the next token is an ellipsis, and we have not seen a
15382      declarator name, and the type of the declarator contains parameter
15383      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15384      a parameter pack expansion expression. Otherwise, leave the
15385      ellipsis for a C-style variadic function. */
15386   token = cp_lexer_peek_token (parser->lexer);
15387   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15388     {
15389       tree type = decl_specifiers.type;
15390
15391       if (type && DECL_P (type))
15392         type = TREE_TYPE (type);
15393
15394       if (type
15395           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15396           && declarator_can_be_parameter_pack (declarator)
15397           && (!declarator || !declarator->parameter_pack_p)
15398           && uses_parameter_packs (type))
15399         {
15400           /* Consume the `...'. */
15401           cp_lexer_consume_token (parser->lexer);
15402           maybe_warn_variadic_templates ();
15403           
15404           /* Build a pack expansion type */
15405           if (declarator)
15406             declarator->parameter_pack_p = true;
15407           else
15408             decl_specifiers.type = make_pack_expansion (type);
15409         }
15410     }
15411
15412   /* The restriction on defining new types applies only to the type
15413      of the parameter, not to the default argument.  */
15414   parser->type_definition_forbidden_message = saved_message;
15415
15416   /* If the next token is `=', then process a default argument.  */
15417   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15418     {
15419       /* Consume the `='.  */
15420       cp_lexer_consume_token (parser->lexer);
15421
15422       /* If we are defining a class, then the tokens that make up the
15423          default argument must be saved and processed later.  */
15424       if (!template_parm_p && at_class_scope_p ()
15425           && TYPE_BEING_DEFINED (current_class_type)
15426           && !LAMBDA_TYPE_P (current_class_type))
15427         {
15428           unsigned depth = 0;
15429           int maybe_template_id = 0;
15430           cp_token *first_token;
15431           cp_token *token;
15432
15433           /* Add tokens until we have processed the entire default
15434              argument.  We add the range [first_token, token).  */
15435           first_token = cp_lexer_peek_token (parser->lexer);
15436           while (true)
15437             {
15438               bool done = false;
15439
15440               /* Peek at the next token.  */
15441               token = cp_lexer_peek_token (parser->lexer);
15442               /* What we do depends on what token we have.  */
15443               switch (token->type)
15444                 {
15445                   /* In valid code, a default argument must be
15446                      immediately followed by a `,' `)', or `...'.  */
15447                 case CPP_COMMA:
15448                   if (depth == 0 && maybe_template_id)
15449                     {
15450                       /* If we've seen a '<', we might be in a
15451                          template-argument-list.  Until Core issue 325 is
15452                          resolved, we don't know how this situation ought
15453                          to be handled, so try to DTRT.  We check whether
15454                          what comes after the comma is a valid parameter
15455                          declaration list.  If it is, then the comma ends
15456                          the default argument; otherwise the default
15457                          argument continues.  */
15458                       bool error = false;
15459
15460                       /* Set ITALP so cp_parser_parameter_declaration_list
15461                          doesn't decide to commit to this parse.  */
15462                       bool saved_italp = parser->in_template_argument_list_p;
15463                       parser->in_template_argument_list_p = true;
15464
15465                       cp_parser_parse_tentatively (parser);
15466                       cp_lexer_consume_token (parser->lexer);
15467                       cp_parser_parameter_declaration_list (parser, &error);
15468                       if (!cp_parser_error_occurred (parser) && !error)
15469                         done = true;
15470                       cp_parser_abort_tentative_parse (parser);
15471
15472                       parser->in_template_argument_list_p = saved_italp;
15473                       break;
15474                     }
15475                 case CPP_CLOSE_PAREN:
15476                 case CPP_ELLIPSIS:
15477                   /* If we run into a non-nested `;', `}', or `]',
15478                      then the code is invalid -- but the default
15479                      argument is certainly over.  */
15480                 case CPP_SEMICOLON:
15481                 case CPP_CLOSE_BRACE:
15482                 case CPP_CLOSE_SQUARE:
15483                   if (depth == 0)
15484                     done = true;
15485                   /* Update DEPTH, if necessary.  */
15486                   else if (token->type == CPP_CLOSE_PAREN
15487                            || token->type == CPP_CLOSE_BRACE
15488                            || token->type == CPP_CLOSE_SQUARE)
15489                     --depth;
15490                   break;
15491
15492                 case CPP_OPEN_PAREN:
15493                 case CPP_OPEN_SQUARE:
15494                 case CPP_OPEN_BRACE:
15495                   ++depth;
15496                   break;
15497
15498                 case CPP_LESS:
15499                   if (depth == 0)
15500                     /* This might be the comparison operator, or it might
15501                        start a template argument list.  */
15502                     ++maybe_template_id;
15503                   break;
15504
15505                 case CPP_RSHIFT:
15506                   if (cxx_dialect == cxx98)
15507                     break;
15508                   /* Fall through for C++0x, which treats the `>>'
15509                      operator like two `>' tokens in certain
15510                      cases.  */
15511
15512                 case CPP_GREATER:
15513                   if (depth == 0)
15514                     {
15515                       /* This might be an operator, or it might close a
15516                          template argument list.  But if a previous '<'
15517                          started a template argument list, this will have
15518                          closed it, so we can't be in one anymore.  */
15519                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15520                       if (maybe_template_id < 0)
15521                         maybe_template_id = 0;
15522                     }
15523                   break;
15524
15525                   /* If we run out of tokens, issue an error message.  */
15526                 case CPP_EOF:
15527                 case CPP_PRAGMA_EOL:
15528                   error_at (token->location, "file ends in default argument");
15529                   done = true;
15530                   break;
15531
15532                 case CPP_NAME:
15533                 case CPP_SCOPE:
15534                   /* In these cases, we should look for template-ids.
15535                      For example, if the default argument is
15536                      `X<int, double>()', we need to do name lookup to
15537                      figure out whether or not `X' is a template; if
15538                      so, the `,' does not end the default argument.
15539
15540                      That is not yet done.  */
15541                   break;
15542
15543                 default:
15544                   break;
15545                 }
15546
15547               /* If we've reached the end, stop.  */
15548               if (done)
15549                 break;
15550
15551               /* Add the token to the token block.  */
15552               token = cp_lexer_consume_token (parser->lexer);
15553             }
15554
15555           /* Create a DEFAULT_ARG to represent the unparsed default
15556              argument.  */
15557           default_argument = make_node (DEFAULT_ARG);
15558           DEFARG_TOKENS (default_argument)
15559             = cp_token_cache_new (first_token, token);
15560           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15561         }
15562       /* Outside of a class definition, we can just parse the
15563          assignment-expression.  */
15564       else
15565         {
15566           token = cp_lexer_peek_token (parser->lexer);
15567           default_argument 
15568             = cp_parser_default_argument (parser, template_parm_p);
15569         }
15570
15571       if (!parser->default_arg_ok_p)
15572         {
15573           if (flag_permissive)
15574             warning (0, "deprecated use of default argument for parameter of non-function");
15575           else
15576             {
15577               error_at (token->location,
15578                         "default arguments are only "
15579                         "permitted for function parameters");
15580               default_argument = NULL_TREE;
15581             }
15582         }
15583       else if ((declarator && declarator->parameter_pack_p)
15584                || (decl_specifiers.type
15585                    && PACK_EXPANSION_P (decl_specifiers.type)))
15586         {
15587           /* Find the name of the parameter pack.  */     
15588           cp_declarator *id_declarator = declarator;
15589           while (id_declarator && id_declarator->kind != cdk_id)
15590             id_declarator = id_declarator->declarator;
15591           
15592           if (id_declarator && id_declarator->kind == cdk_id)
15593             error_at (declarator_token_start->location,
15594                       template_parm_p 
15595                       ? "template parameter pack %qD"
15596                       " cannot have a default argument"
15597                       : "parameter pack %qD cannot have a default argument",
15598                       id_declarator->u.id.unqualified_name);
15599           else
15600             error_at (declarator_token_start->location,
15601                       template_parm_p 
15602                       ? "template parameter pack cannot have a default argument"
15603                       : "parameter pack cannot have a default argument");
15604           
15605           default_argument = NULL_TREE;
15606         }
15607     }
15608   else
15609     default_argument = NULL_TREE;
15610
15611   return make_parameter_declarator (&decl_specifiers,
15612                                     declarator,
15613                                     default_argument);
15614 }
15615
15616 /* Parse a default argument and return it.
15617
15618    TEMPLATE_PARM_P is true if this is a default argument for a
15619    non-type template parameter.  */
15620 static tree
15621 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15622 {
15623   tree default_argument = NULL_TREE;
15624   bool saved_greater_than_is_operator_p;
15625   bool saved_local_variables_forbidden_p;
15626
15627   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15628      set correctly.  */
15629   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15630   parser->greater_than_is_operator_p = !template_parm_p;
15631   /* Local variable names (and the `this' keyword) may not
15632      appear in a default argument.  */
15633   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15634   parser->local_variables_forbidden_p = true;
15635   /* Parse the assignment-expression.  */
15636   if (template_parm_p)
15637     push_deferring_access_checks (dk_no_deferred);
15638   default_argument
15639     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15640   if (template_parm_p)
15641     pop_deferring_access_checks ();
15642   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15643   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15644
15645   return default_argument;
15646 }
15647
15648 /* Parse a function-body.
15649
15650    function-body:
15651      compound_statement  */
15652
15653 static void
15654 cp_parser_function_body (cp_parser *parser)
15655 {
15656   cp_parser_compound_statement (parser, NULL, false);
15657 }
15658
15659 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15660    true if a ctor-initializer was present.  */
15661
15662 static bool
15663 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15664 {
15665   tree body;
15666   bool ctor_initializer_p;
15667
15668   /* Begin the function body.  */
15669   body = begin_function_body ();
15670   /* Parse the optional ctor-initializer.  */
15671   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15672   /* Parse the function-body.  */
15673   cp_parser_function_body (parser);
15674   /* Finish the function body.  */
15675   finish_function_body (body);
15676
15677   return ctor_initializer_p;
15678 }
15679
15680 /* Parse an initializer.
15681
15682    initializer:
15683      = initializer-clause
15684      ( expression-list )
15685
15686    Returns an expression representing the initializer.  If no
15687    initializer is present, NULL_TREE is returned.
15688
15689    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15690    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15691    set to TRUE if there is no initializer present.  If there is an
15692    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15693    is set to true; otherwise it is set to false.  */
15694
15695 static tree
15696 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15697                        bool* non_constant_p)
15698 {
15699   cp_token *token;
15700   tree init;
15701
15702   /* Peek at the next token.  */
15703   token = cp_lexer_peek_token (parser->lexer);
15704
15705   /* Let our caller know whether or not this initializer was
15706      parenthesized.  */
15707   *is_direct_init = (token->type != CPP_EQ);
15708   /* Assume that the initializer is constant.  */
15709   *non_constant_p = false;
15710
15711   if (token->type == CPP_EQ)
15712     {
15713       /* Consume the `='.  */
15714       cp_lexer_consume_token (parser->lexer);
15715       /* Parse the initializer-clause.  */
15716       init = cp_parser_initializer_clause (parser, non_constant_p);
15717     }
15718   else if (token->type == CPP_OPEN_PAREN)
15719     {
15720       VEC(tree,gc) *vec;
15721       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15722                                                      /*cast_p=*/false,
15723                                                      /*allow_expansion_p=*/true,
15724                                                      non_constant_p);
15725       if (vec == NULL)
15726         return error_mark_node;
15727       init = build_tree_list_vec (vec);
15728       release_tree_vector (vec);
15729     }
15730   else if (token->type == CPP_OPEN_BRACE)
15731     {
15732       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15733       init = cp_parser_braced_list (parser, non_constant_p);
15734       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15735     }
15736   else
15737     {
15738       /* Anything else is an error.  */
15739       cp_parser_error (parser, "expected initializer");
15740       init = error_mark_node;
15741     }
15742
15743   return init;
15744 }
15745
15746 /* Parse an initializer-clause.
15747
15748    initializer-clause:
15749      assignment-expression
15750      braced-init-list
15751
15752    Returns an expression representing the initializer.
15753
15754    If the `assignment-expression' production is used the value
15755    returned is simply a representation for the expression.
15756
15757    Otherwise, calls cp_parser_braced_list.  */
15758
15759 static tree
15760 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15761 {
15762   tree initializer;
15763
15764   /* Assume the expression is constant.  */
15765   *non_constant_p = false;
15766
15767   /* If it is not a `{', then we are looking at an
15768      assignment-expression.  */
15769   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15770     {
15771       initializer
15772         = cp_parser_constant_expression (parser,
15773                                         /*allow_non_constant_p=*/true,
15774                                         non_constant_p);
15775       if (!*non_constant_p)
15776         initializer = fold_non_dependent_expr (initializer);
15777     }
15778   else
15779     initializer = cp_parser_braced_list (parser, non_constant_p);
15780
15781   return initializer;
15782 }
15783
15784 /* Parse a brace-enclosed initializer list.
15785
15786    braced-init-list:
15787      { initializer-list , [opt] }
15788      { }
15789
15790    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15791    the elements of the initializer-list (or NULL, if the last
15792    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15793    NULL_TREE.  There is no way to detect whether or not the optional
15794    trailing `,' was provided.  NON_CONSTANT_P is as for
15795    cp_parser_initializer.  */     
15796
15797 static tree
15798 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15799 {
15800   tree initializer;
15801
15802   /* Consume the `{' token.  */
15803   cp_lexer_consume_token (parser->lexer);
15804   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15805   initializer = make_node (CONSTRUCTOR);
15806   /* If it's not a `}', then there is a non-trivial initializer.  */
15807   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15808     {
15809       /* Parse the initializer list.  */
15810       CONSTRUCTOR_ELTS (initializer)
15811         = cp_parser_initializer_list (parser, non_constant_p);
15812       /* A trailing `,' token is allowed.  */
15813       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15814         cp_lexer_consume_token (parser->lexer);
15815     }
15816   /* Now, there should be a trailing `}'.  */
15817   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15818   TREE_TYPE (initializer) = init_list_type_node;
15819   return initializer;
15820 }
15821
15822 /* Parse an initializer-list.
15823
15824    initializer-list:
15825      initializer-clause ... [opt]
15826      initializer-list , initializer-clause ... [opt]
15827
15828    GNU Extension:
15829
15830    initializer-list:
15831      identifier : initializer-clause
15832      initializer-list, identifier : initializer-clause
15833
15834    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15835    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15836    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15837    as for cp_parser_initializer.  */
15838
15839 static VEC(constructor_elt,gc) *
15840 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15841 {
15842   VEC(constructor_elt,gc) *v = NULL;
15843
15844   /* Assume all of the expressions are constant.  */
15845   *non_constant_p = false;
15846
15847   /* Parse the rest of the list.  */
15848   while (true)
15849     {
15850       cp_token *token;
15851       tree identifier;
15852       tree initializer;
15853       bool clause_non_constant_p;
15854
15855       /* If the next token is an identifier and the following one is a
15856          colon, we are looking at the GNU designated-initializer
15857          syntax.  */
15858       if (cp_parser_allow_gnu_extensions_p (parser)
15859           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15860           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15861         {
15862           /* Warn the user that they are using an extension.  */
15863           pedwarn (input_location, OPT_pedantic, 
15864                    "ISO C++ does not allow designated initializers");
15865           /* Consume the identifier.  */
15866           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15867           /* Consume the `:'.  */
15868           cp_lexer_consume_token (parser->lexer);
15869         }
15870       else
15871         identifier = NULL_TREE;
15872
15873       /* Parse the initializer.  */
15874       initializer = cp_parser_initializer_clause (parser,
15875                                                   &clause_non_constant_p);
15876       /* If any clause is non-constant, so is the entire initializer.  */
15877       if (clause_non_constant_p)
15878         *non_constant_p = true;
15879
15880       /* If we have an ellipsis, this is an initializer pack
15881          expansion.  */
15882       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15883         {
15884           /* Consume the `...'.  */
15885           cp_lexer_consume_token (parser->lexer);
15886
15887           /* Turn the initializer into an initializer expansion.  */
15888           initializer = make_pack_expansion (initializer);
15889         }
15890
15891       /* Add it to the vector.  */
15892       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15893
15894       /* If the next token is not a comma, we have reached the end of
15895          the list.  */
15896       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15897         break;
15898
15899       /* Peek at the next token.  */
15900       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15901       /* If the next token is a `}', then we're still done.  An
15902          initializer-clause can have a trailing `,' after the
15903          initializer-list and before the closing `}'.  */
15904       if (token->type == CPP_CLOSE_BRACE)
15905         break;
15906
15907       /* Consume the `,' token.  */
15908       cp_lexer_consume_token (parser->lexer);
15909     }
15910
15911   return v;
15912 }
15913
15914 /* Classes [gram.class] */
15915
15916 /* Parse a class-name.
15917
15918    class-name:
15919      identifier
15920      template-id
15921
15922    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15923    to indicate that names looked up in dependent types should be
15924    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15925    keyword has been used to indicate that the name that appears next
15926    is a template.  TAG_TYPE indicates the explicit tag given before
15927    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15928    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15929    is the class being defined in a class-head.
15930
15931    Returns the TYPE_DECL representing the class.  */
15932
15933 static tree
15934 cp_parser_class_name (cp_parser *parser,
15935                       bool typename_keyword_p,
15936                       bool template_keyword_p,
15937                       enum tag_types tag_type,
15938                       bool check_dependency_p,
15939                       bool class_head_p,
15940                       bool is_declaration)
15941 {
15942   tree decl;
15943   tree scope;
15944   bool typename_p;
15945   cp_token *token;
15946   tree identifier = NULL_TREE;
15947
15948   /* All class-names start with an identifier.  */
15949   token = cp_lexer_peek_token (parser->lexer);
15950   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15951     {
15952       cp_parser_error (parser, "expected class-name");
15953       return error_mark_node;
15954     }
15955
15956   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15957      to a template-id, so we save it here.  */
15958   scope = parser->scope;
15959   if (scope == error_mark_node)
15960     return error_mark_node;
15961
15962   /* Any name names a type if we're following the `typename' keyword
15963      in a qualified name where the enclosing scope is type-dependent.  */
15964   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15965                 && dependent_type_p (scope));
15966   /* Handle the common case (an identifier, but not a template-id)
15967      efficiently.  */
15968   if (token->type == CPP_NAME
15969       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15970     {
15971       cp_token *identifier_token;
15972       bool ambiguous_p;
15973
15974       /* Look for the identifier.  */
15975       identifier_token = cp_lexer_peek_token (parser->lexer);
15976       ambiguous_p = identifier_token->ambiguous_p;
15977       identifier = cp_parser_identifier (parser);
15978       /* If the next token isn't an identifier, we are certainly not
15979          looking at a class-name.  */
15980       if (identifier == error_mark_node)
15981         decl = error_mark_node;
15982       /* If we know this is a type-name, there's no need to look it
15983          up.  */
15984       else if (typename_p)
15985         decl = identifier;
15986       else
15987         {
15988           tree ambiguous_decls;
15989           /* If we already know that this lookup is ambiguous, then
15990              we've already issued an error message; there's no reason
15991              to check again.  */
15992           if (ambiguous_p)
15993             {
15994               cp_parser_simulate_error (parser);
15995               return error_mark_node;
15996             }
15997           /* If the next token is a `::', then the name must be a type
15998              name.
15999
16000              [basic.lookup.qual]
16001
16002              During the lookup for a name preceding the :: scope
16003              resolution operator, object, function, and enumerator
16004              names are ignored.  */
16005           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16006             tag_type = typename_type;
16007           /* Look up the name.  */
16008           decl = cp_parser_lookup_name (parser, identifier,
16009                                         tag_type,
16010                                         /*is_template=*/false,
16011                                         /*is_namespace=*/false,
16012                                         check_dependency_p,
16013                                         &ambiguous_decls,
16014                                         identifier_token->location);
16015           if (ambiguous_decls)
16016             {
16017               if (cp_parser_parsing_tentatively (parser))
16018                 cp_parser_simulate_error (parser);
16019               return error_mark_node;
16020             }
16021         }
16022     }
16023   else
16024     {
16025       /* Try a template-id.  */
16026       decl = cp_parser_template_id (parser, template_keyword_p,
16027                                     check_dependency_p,
16028                                     is_declaration);
16029       if (decl == error_mark_node)
16030         return error_mark_node;
16031     }
16032
16033   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16034
16035   /* If this is a typename, create a TYPENAME_TYPE.  */
16036   if (typename_p && decl != error_mark_node)
16037     {
16038       decl = make_typename_type (scope, decl, typename_type,
16039                                  /*complain=*/tf_error);
16040       if (decl != error_mark_node)
16041         decl = TYPE_NAME (decl);
16042     }
16043
16044   /* Check to see that it is really the name of a class.  */
16045   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16046       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16047       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16048     /* Situations like this:
16049
16050          template <typename T> struct A {
16051            typename T::template X<int>::I i;
16052          };
16053
16054        are problematic.  Is `T::template X<int>' a class-name?  The
16055        standard does not seem to be definitive, but there is no other
16056        valid interpretation of the following `::'.  Therefore, those
16057        names are considered class-names.  */
16058     {
16059       decl = make_typename_type (scope, decl, tag_type, tf_error);
16060       if (decl != error_mark_node)
16061         decl = TYPE_NAME (decl);
16062     }
16063   else if (TREE_CODE (decl) != TYPE_DECL
16064            || TREE_TYPE (decl) == error_mark_node
16065            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16066     decl = error_mark_node;
16067
16068   if (decl == error_mark_node)
16069     cp_parser_error (parser, "expected class-name");
16070   else if (identifier && !parser->scope)
16071     maybe_note_name_used_in_class (identifier, decl);
16072
16073   return decl;
16074 }
16075
16076 /* Parse a class-specifier.
16077
16078    class-specifier:
16079      class-head { member-specification [opt] }
16080
16081    Returns the TREE_TYPE representing the class.  */
16082
16083 static tree
16084 cp_parser_class_specifier (cp_parser* parser)
16085 {
16086   tree type;
16087   tree attributes = NULL_TREE;
16088   bool nested_name_specifier_p;
16089   unsigned saved_num_template_parameter_lists;
16090   bool saved_in_function_body;
16091   bool saved_in_unbraced_linkage_specification_p;
16092   tree old_scope = NULL_TREE;
16093   tree scope = NULL_TREE;
16094   tree bases;
16095
16096   push_deferring_access_checks (dk_no_deferred);
16097
16098   /* Parse the class-head.  */
16099   type = cp_parser_class_head (parser,
16100                                &nested_name_specifier_p,
16101                                &attributes,
16102                                &bases);
16103   /* If the class-head was a semantic disaster, skip the entire body
16104      of the class.  */
16105   if (!type)
16106     {
16107       cp_parser_skip_to_end_of_block_or_statement (parser);
16108       pop_deferring_access_checks ();
16109       return error_mark_node;
16110     }
16111
16112   /* Look for the `{'.  */
16113   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16114     {
16115       pop_deferring_access_checks ();
16116       return error_mark_node;
16117     }
16118
16119   /* Process the base classes. If they're invalid, skip the 
16120      entire class body.  */
16121   if (!xref_basetypes (type, bases))
16122     {
16123       /* Consuming the closing brace yields better error messages
16124          later on.  */
16125       if (cp_parser_skip_to_closing_brace (parser))
16126         cp_lexer_consume_token (parser->lexer);
16127       pop_deferring_access_checks ();
16128       return error_mark_node;
16129     }
16130
16131   /* Issue an error message if type-definitions are forbidden here.  */
16132   cp_parser_check_type_definition (parser);
16133   /* Remember that we are defining one more class.  */
16134   ++parser->num_classes_being_defined;
16135   /* Inside the class, surrounding template-parameter-lists do not
16136      apply.  */
16137   saved_num_template_parameter_lists
16138     = parser->num_template_parameter_lists;
16139   parser->num_template_parameter_lists = 0;
16140   /* We are not in a function body.  */
16141   saved_in_function_body = parser->in_function_body;
16142   parser->in_function_body = false;
16143   /* We are not immediately inside an extern "lang" block.  */
16144   saved_in_unbraced_linkage_specification_p
16145     = parser->in_unbraced_linkage_specification_p;
16146   parser->in_unbraced_linkage_specification_p = false;
16147
16148   /* Start the class.  */
16149   if (nested_name_specifier_p)
16150     {
16151       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16152       old_scope = push_inner_scope (scope);
16153     }
16154   type = begin_class_definition (type, attributes);
16155
16156   if (type == error_mark_node)
16157     /* If the type is erroneous, skip the entire body of the class.  */
16158     cp_parser_skip_to_closing_brace (parser);
16159   else
16160     /* Parse the member-specification.  */
16161     cp_parser_member_specification_opt (parser);
16162
16163   /* Look for the trailing `}'.  */
16164   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16165   /* Look for trailing attributes to apply to this class.  */
16166   if (cp_parser_allow_gnu_extensions_p (parser))
16167     attributes = cp_parser_attributes_opt (parser);
16168   if (type != error_mark_node)
16169     type = finish_struct (type, attributes);
16170   if (nested_name_specifier_p)
16171     pop_inner_scope (old_scope, scope);
16172   /* If this class is not itself within the scope of another class,
16173      then we need to parse the bodies of all of the queued function
16174      definitions.  Note that the queued functions defined in a class
16175      are not always processed immediately following the
16176      class-specifier for that class.  Consider:
16177
16178        struct A {
16179          struct B { void f() { sizeof (A); } };
16180        };
16181
16182      If `f' were processed before the processing of `A' were
16183      completed, there would be no way to compute the size of `A'.
16184      Note that the nesting we are interested in here is lexical --
16185      not the semantic nesting given by TYPE_CONTEXT.  In particular,
16186      for:
16187
16188        struct A { struct B; };
16189        struct A::B { void f() { } };
16190
16191      there is no need to delay the parsing of `A::B::f'.  */
16192   if (--parser->num_classes_being_defined == 0)
16193     {
16194       tree queue_entry;
16195       tree fn;
16196       tree class_type = NULL_TREE;
16197       tree pushed_scope = NULL_TREE;
16198
16199       /* In a first pass, parse default arguments to the functions.
16200          Then, in a second pass, parse the bodies of the functions.
16201          This two-phased approach handles cases like:
16202
16203             struct S {
16204               void f() { g(); }
16205               void g(int i = 3);
16206             };
16207
16208          */
16209       for (TREE_PURPOSE (parser->unparsed_functions_queues)
16210              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
16211            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
16212            TREE_PURPOSE (parser->unparsed_functions_queues)
16213              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
16214         {
16215           fn = TREE_VALUE (queue_entry);
16216           /* If there are default arguments that have not yet been processed,
16217              take care of them now.  */
16218           if (class_type != TREE_PURPOSE (queue_entry))
16219             {
16220               if (pushed_scope)
16221                 pop_scope (pushed_scope);
16222               class_type = TREE_PURPOSE (queue_entry);
16223               pushed_scope = push_scope (class_type);
16224             }
16225           /* Make sure that any template parameters are in scope.  */
16226           maybe_begin_member_template_processing (fn);
16227           /* Parse the default argument expressions.  */
16228           cp_parser_late_parsing_default_args (parser, fn);
16229           /* Remove any template parameters from the symbol table.  */
16230           maybe_end_member_template_processing ();
16231         }
16232       if (pushed_scope)
16233         pop_scope (pushed_scope);
16234       /* Now parse the body of the functions.  */
16235       for (TREE_VALUE (parser->unparsed_functions_queues)
16236              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
16237            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
16238            TREE_VALUE (parser->unparsed_functions_queues)
16239              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
16240         {
16241           /* Figure out which function we need to process.  */
16242           fn = TREE_VALUE (queue_entry);
16243           /* Parse the function.  */
16244           cp_parser_late_parsing_for_member (parser, fn);
16245         }
16246     }
16247
16248   /* Put back any saved access checks.  */
16249   pop_deferring_access_checks ();
16250
16251   /* Restore saved state.  */
16252   parser->in_function_body = saved_in_function_body;
16253   parser->num_template_parameter_lists
16254     = saved_num_template_parameter_lists;
16255   parser->in_unbraced_linkage_specification_p
16256     = saved_in_unbraced_linkage_specification_p;
16257
16258   return type;
16259 }
16260
16261 /* Parse a class-head.
16262
16263    class-head:
16264      class-key identifier [opt] base-clause [opt]
16265      class-key nested-name-specifier identifier base-clause [opt]
16266      class-key nested-name-specifier [opt] template-id
16267        base-clause [opt]
16268
16269    GNU Extensions:
16270      class-key attributes identifier [opt] base-clause [opt]
16271      class-key attributes nested-name-specifier identifier base-clause [opt]
16272      class-key attributes nested-name-specifier [opt] template-id
16273        base-clause [opt]
16274
16275    Upon return BASES is initialized to the list of base classes (or
16276    NULL, if there are none) in the same form returned by
16277    cp_parser_base_clause.
16278
16279    Returns the TYPE of the indicated class.  Sets
16280    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16281    involving a nested-name-specifier was used, and FALSE otherwise.
16282
16283    Returns error_mark_node if this is not a class-head.
16284
16285    Returns NULL_TREE if the class-head is syntactically valid, but
16286    semantically invalid in a way that means we should skip the entire
16287    body of the class.  */
16288
16289 static tree
16290 cp_parser_class_head (cp_parser* parser,
16291                       bool* nested_name_specifier_p,
16292                       tree *attributes_p,
16293                       tree *bases)
16294 {
16295   tree nested_name_specifier;
16296   enum tag_types class_key;
16297   tree id = NULL_TREE;
16298   tree type = NULL_TREE;
16299   tree attributes;
16300   bool template_id_p = false;
16301   bool qualified_p = false;
16302   bool invalid_nested_name_p = false;
16303   bool invalid_explicit_specialization_p = false;
16304   tree pushed_scope = NULL_TREE;
16305   unsigned num_templates;
16306   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16307   /* Assume no nested-name-specifier will be present.  */
16308   *nested_name_specifier_p = false;
16309   /* Assume no template parameter lists will be used in defining the
16310      type.  */
16311   num_templates = 0;
16312
16313   *bases = NULL_TREE;
16314
16315   /* Look for the class-key.  */
16316   class_key = cp_parser_class_key (parser);
16317   if (class_key == none_type)
16318     return error_mark_node;
16319
16320   /* Parse the attributes.  */
16321   attributes = cp_parser_attributes_opt (parser);
16322
16323   /* If the next token is `::', that is invalid -- but sometimes
16324      people do try to write:
16325
16326        struct ::S {};
16327
16328      Handle this gracefully by accepting the extra qualifier, and then
16329      issuing an error about it later if this really is a
16330      class-head.  If it turns out just to be an elaborated type
16331      specifier, remain silent.  */
16332   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16333     qualified_p = true;
16334
16335   push_deferring_access_checks (dk_no_check);
16336
16337   /* Determine the name of the class.  Begin by looking for an
16338      optional nested-name-specifier.  */
16339   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16340   nested_name_specifier
16341     = cp_parser_nested_name_specifier_opt (parser,
16342                                            /*typename_keyword_p=*/false,
16343                                            /*check_dependency_p=*/false,
16344                                            /*type_p=*/false,
16345                                            /*is_declaration=*/false);
16346   /* If there was a nested-name-specifier, then there *must* be an
16347      identifier.  */
16348   if (nested_name_specifier)
16349     {
16350       type_start_token = cp_lexer_peek_token (parser->lexer);
16351       /* Although the grammar says `identifier', it really means
16352          `class-name' or `template-name'.  You are only allowed to
16353          define a class that has already been declared with this
16354          syntax.
16355
16356          The proposed resolution for Core Issue 180 says that wherever
16357          you see `class T::X' you should treat `X' as a type-name.
16358
16359          It is OK to define an inaccessible class; for example:
16360
16361            class A { class B; };
16362            class A::B {};
16363
16364          We do not know if we will see a class-name, or a
16365          template-name.  We look for a class-name first, in case the
16366          class-name is a template-id; if we looked for the
16367          template-name first we would stop after the template-name.  */
16368       cp_parser_parse_tentatively (parser);
16369       type = cp_parser_class_name (parser,
16370                                    /*typename_keyword_p=*/false,
16371                                    /*template_keyword_p=*/false,
16372                                    class_type,
16373                                    /*check_dependency_p=*/false,
16374                                    /*class_head_p=*/true,
16375                                    /*is_declaration=*/false);
16376       /* If that didn't work, ignore the nested-name-specifier.  */
16377       if (!cp_parser_parse_definitely (parser))
16378         {
16379           invalid_nested_name_p = true;
16380           type_start_token = cp_lexer_peek_token (parser->lexer);
16381           id = cp_parser_identifier (parser);
16382           if (id == error_mark_node)
16383             id = NULL_TREE;
16384         }
16385       /* If we could not find a corresponding TYPE, treat this
16386          declaration like an unqualified declaration.  */
16387       if (type == error_mark_node)
16388         nested_name_specifier = NULL_TREE;
16389       /* Otherwise, count the number of templates used in TYPE and its
16390          containing scopes.  */
16391       else
16392         {
16393           tree scope;
16394
16395           for (scope = TREE_TYPE (type);
16396                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16397                scope = (TYPE_P (scope)
16398                         ? TYPE_CONTEXT (scope)
16399                         : DECL_CONTEXT (scope)))
16400             if (TYPE_P (scope)
16401                 && CLASS_TYPE_P (scope)
16402                 && CLASSTYPE_TEMPLATE_INFO (scope)
16403                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16404                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16405               ++num_templates;
16406         }
16407     }
16408   /* Otherwise, the identifier is optional.  */
16409   else
16410     {
16411       /* We don't know whether what comes next is a template-id,
16412          an identifier, or nothing at all.  */
16413       cp_parser_parse_tentatively (parser);
16414       /* Check for a template-id.  */
16415       type_start_token = cp_lexer_peek_token (parser->lexer);
16416       id = cp_parser_template_id (parser,
16417                                   /*template_keyword_p=*/false,
16418                                   /*check_dependency_p=*/true,
16419                                   /*is_declaration=*/true);
16420       /* If that didn't work, it could still be an identifier.  */
16421       if (!cp_parser_parse_definitely (parser))
16422         {
16423           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16424             {
16425               type_start_token = cp_lexer_peek_token (parser->lexer);
16426               id = cp_parser_identifier (parser);
16427             }
16428           else
16429             id = NULL_TREE;
16430         }
16431       else
16432         {
16433           template_id_p = true;
16434           ++num_templates;
16435         }
16436     }
16437
16438   pop_deferring_access_checks ();
16439
16440   if (id)
16441     cp_parser_check_for_invalid_template_id (parser, id,
16442                                              type_start_token->location);
16443
16444   /* If it's not a `:' or a `{' then we can't really be looking at a
16445      class-head, since a class-head only appears as part of a
16446      class-specifier.  We have to detect this situation before calling
16447      xref_tag, since that has irreversible side-effects.  */
16448   if (!cp_parser_next_token_starts_class_definition_p (parser))
16449     {
16450       cp_parser_error (parser, "expected %<{%> or %<:%>");
16451       return error_mark_node;
16452     }
16453
16454   /* At this point, we're going ahead with the class-specifier, even
16455      if some other problem occurs.  */
16456   cp_parser_commit_to_tentative_parse (parser);
16457   /* Issue the error about the overly-qualified name now.  */
16458   if (qualified_p)
16459     {
16460       cp_parser_error (parser,
16461                        "global qualification of class name is invalid");
16462       return error_mark_node;
16463     }
16464   else if (invalid_nested_name_p)
16465     {
16466       cp_parser_error (parser,
16467                        "qualified name does not name a class");
16468       return error_mark_node;
16469     }
16470   else if (nested_name_specifier)
16471     {
16472       tree scope;
16473
16474       /* Reject typedef-names in class heads.  */
16475       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16476         {
16477           error_at (type_start_token->location,
16478                     "invalid class name in declaration of %qD",
16479                     type);
16480           type = NULL_TREE;
16481           goto done;
16482         }
16483
16484       /* Figure out in what scope the declaration is being placed.  */
16485       scope = current_scope ();
16486       /* If that scope does not contain the scope in which the
16487          class was originally declared, the program is invalid.  */
16488       if (scope && !is_ancestor (scope, nested_name_specifier))
16489         {
16490           if (at_namespace_scope_p ())
16491             error_at (type_start_token->location,
16492                       "declaration of %qD in namespace %qD which does not "
16493                       "enclose %qD",
16494                       type, scope, nested_name_specifier);
16495           else
16496             error_at (type_start_token->location,
16497                       "declaration of %qD in %qD which does not enclose %qD",
16498                       type, scope, nested_name_specifier);
16499           type = NULL_TREE;
16500           goto done;
16501         }
16502       /* [dcl.meaning]
16503
16504          A declarator-id shall not be qualified except for the
16505          definition of a ... nested class outside of its class
16506          ... [or] the definition or explicit instantiation of a
16507          class member of a namespace outside of its namespace.  */
16508       if (scope == nested_name_specifier)
16509         {
16510           permerror (nested_name_specifier_token_start->location,
16511                      "extra qualification not allowed");
16512           nested_name_specifier = NULL_TREE;
16513           num_templates = 0;
16514         }
16515     }
16516   /* An explicit-specialization must be preceded by "template <>".  If
16517      it is not, try to recover gracefully.  */
16518   if (at_namespace_scope_p ()
16519       && parser->num_template_parameter_lists == 0
16520       && template_id_p)
16521     {
16522       error_at (type_start_token->location,
16523                 "an explicit specialization must be preceded by %<template <>%>");
16524       invalid_explicit_specialization_p = true;
16525       /* Take the same action that would have been taken by
16526          cp_parser_explicit_specialization.  */
16527       ++parser->num_template_parameter_lists;
16528       begin_specialization ();
16529     }
16530   /* There must be no "return" statements between this point and the
16531      end of this function; set "type "to the correct return value and
16532      use "goto done;" to return.  */
16533   /* Make sure that the right number of template parameters were
16534      present.  */
16535   if (!cp_parser_check_template_parameters (parser, num_templates,
16536                                             type_start_token->location,
16537                                             /*declarator=*/NULL))
16538     {
16539       /* If something went wrong, there is no point in even trying to
16540          process the class-definition.  */
16541       type = NULL_TREE;
16542       goto done;
16543     }
16544
16545   /* Look up the type.  */
16546   if (template_id_p)
16547     {
16548       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16549           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16550               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16551         {
16552           error_at (type_start_token->location,
16553                     "function template %qD redeclared as a class template", id);
16554           type = error_mark_node;
16555         }
16556       else
16557         {
16558           type = TREE_TYPE (id);
16559           type = maybe_process_partial_specialization (type);
16560         }
16561       if (nested_name_specifier)
16562         pushed_scope = push_scope (nested_name_specifier);
16563     }
16564   else if (nested_name_specifier)
16565     {
16566       tree class_type;
16567
16568       /* Given:
16569
16570             template <typename T> struct S { struct T };
16571             template <typename T> struct S<T>::T { };
16572
16573          we will get a TYPENAME_TYPE when processing the definition of
16574          `S::T'.  We need to resolve it to the actual type before we
16575          try to define it.  */
16576       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16577         {
16578           class_type = resolve_typename_type (TREE_TYPE (type),
16579                                               /*only_current_p=*/false);
16580           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16581             type = TYPE_NAME (class_type);
16582           else
16583             {
16584               cp_parser_error (parser, "could not resolve typename type");
16585               type = error_mark_node;
16586             }
16587         }
16588
16589       if (maybe_process_partial_specialization (TREE_TYPE (type))
16590           == error_mark_node)
16591         {
16592           type = NULL_TREE;
16593           goto done;
16594         }
16595
16596       class_type = current_class_type;
16597       /* Enter the scope indicated by the nested-name-specifier.  */
16598       pushed_scope = push_scope (nested_name_specifier);
16599       /* Get the canonical version of this type.  */
16600       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16601       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16602           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16603         {
16604           type = push_template_decl (type);
16605           if (type == error_mark_node)
16606             {
16607               type = NULL_TREE;
16608               goto done;
16609             }
16610         }
16611
16612       type = TREE_TYPE (type);
16613       *nested_name_specifier_p = true;
16614     }
16615   else      /* The name is not a nested name.  */
16616     {
16617       /* If the class was unnamed, create a dummy name.  */
16618       if (!id)
16619         id = make_anon_name ();
16620       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16621                        parser->num_template_parameter_lists);
16622     }
16623
16624   /* Indicate whether this class was declared as a `class' or as a
16625      `struct'.  */
16626   if (TREE_CODE (type) == RECORD_TYPE)
16627     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16628   cp_parser_check_class_key (class_key, type);
16629
16630   /* If this type was already complete, and we see another definition,
16631      that's an error.  */
16632   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16633     {
16634       error_at (type_start_token->location, "redefinition of %q#T",
16635                 type);
16636       error_at (type_start_token->location, "previous definition of %q+#T",
16637                 type);
16638       type = NULL_TREE;
16639       goto done;
16640     }
16641   else if (type == error_mark_node)
16642     type = NULL_TREE;
16643
16644   /* We will have entered the scope containing the class; the names of
16645      base classes should be looked up in that context.  For example:
16646
16647        struct A { struct B {}; struct C; };
16648        struct A::C : B {};
16649
16650      is valid.  */
16651
16652   /* Get the list of base-classes, if there is one.  */
16653   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16654     *bases = cp_parser_base_clause (parser);
16655
16656  done:
16657   /* Leave the scope given by the nested-name-specifier.  We will
16658      enter the class scope itself while processing the members.  */
16659   if (pushed_scope)
16660     pop_scope (pushed_scope);
16661
16662   if (invalid_explicit_specialization_p)
16663     {
16664       end_specialization ();
16665       --parser->num_template_parameter_lists;
16666     }
16667
16668   if (type)
16669     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16670   *attributes_p = attributes;
16671   return type;
16672 }
16673
16674 /* Parse a class-key.
16675
16676    class-key:
16677      class
16678      struct
16679      union
16680
16681    Returns the kind of class-key specified, or none_type to indicate
16682    error.  */
16683
16684 static enum tag_types
16685 cp_parser_class_key (cp_parser* parser)
16686 {
16687   cp_token *token;
16688   enum tag_types tag_type;
16689
16690   /* Look for the class-key.  */
16691   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
16692   if (!token)
16693     return none_type;
16694
16695   /* Check to see if the TOKEN is a class-key.  */
16696   tag_type = cp_parser_token_is_class_key (token);
16697   if (!tag_type)
16698     cp_parser_error (parser, "expected class-key");
16699   return tag_type;
16700 }
16701
16702 /* Parse an (optional) member-specification.
16703
16704    member-specification:
16705      member-declaration member-specification [opt]
16706      access-specifier : member-specification [opt]  */
16707
16708 static void
16709 cp_parser_member_specification_opt (cp_parser* parser)
16710 {
16711   while (true)
16712     {
16713       cp_token *token;
16714       enum rid keyword;
16715
16716       /* Peek at the next token.  */
16717       token = cp_lexer_peek_token (parser->lexer);
16718       /* If it's a `}', or EOF then we've seen all the members.  */
16719       if (token->type == CPP_CLOSE_BRACE
16720           || token->type == CPP_EOF
16721           || token->type == CPP_PRAGMA_EOL)
16722         break;
16723
16724       /* See if this token is a keyword.  */
16725       keyword = token->keyword;
16726       switch (keyword)
16727         {
16728         case RID_PUBLIC:
16729         case RID_PROTECTED:
16730         case RID_PRIVATE:
16731           /* Consume the access-specifier.  */
16732           cp_lexer_consume_token (parser->lexer);
16733           /* Remember which access-specifier is active.  */
16734           current_access_specifier = token->u.value;
16735           /* Look for the `:'.  */
16736           cp_parser_require (parser, CPP_COLON, RT_COLON);
16737           break;
16738
16739         default:
16740           /* Accept #pragmas at class scope.  */
16741           if (token->type == CPP_PRAGMA)
16742             {
16743               cp_parser_pragma (parser, pragma_external);
16744               break;
16745             }
16746
16747           /* Otherwise, the next construction must be a
16748              member-declaration.  */
16749           cp_parser_member_declaration (parser);
16750         }
16751     }
16752 }
16753
16754 /* Parse a member-declaration.
16755
16756    member-declaration:
16757      decl-specifier-seq [opt] member-declarator-list [opt] ;
16758      function-definition ; [opt]
16759      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16760      using-declaration
16761      template-declaration
16762
16763    member-declarator-list:
16764      member-declarator
16765      member-declarator-list , member-declarator
16766
16767    member-declarator:
16768      declarator pure-specifier [opt]
16769      declarator constant-initializer [opt]
16770      identifier [opt] : constant-expression
16771
16772    GNU Extensions:
16773
16774    member-declaration:
16775      __extension__ member-declaration
16776
16777    member-declarator:
16778      declarator attributes [opt] pure-specifier [opt]
16779      declarator attributes [opt] constant-initializer [opt]
16780      identifier [opt] attributes [opt] : constant-expression  
16781
16782    C++0x Extensions:
16783
16784    member-declaration:
16785      static_assert-declaration  */
16786
16787 static void
16788 cp_parser_member_declaration (cp_parser* parser)
16789 {
16790   cp_decl_specifier_seq decl_specifiers;
16791   tree prefix_attributes;
16792   tree decl;
16793   int declares_class_or_enum;
16794   bool friend_p;
16795   cp_token *token = NULL;
16796   cp_token *decl_spec_token_start = NULL;
16797   cp_token *initializer_token_start = NULL;
16798   int saved_pedantic;
16799
16800   /* Check for the `__extension__' keyword.  */
16801   if (cp_parser_extension_opt (parser, &saved_pedantic))
16802     {
16803       /* Recurse.  */
16804       cp_parser_member_declaration (parser);
16805       /* Restore the old value of the PEDANTIC flag.  */
16806       pedantic = saved_pedantic;
16807
16808       return;
16809     }
16810
16811   /* Check for a template-declaration.  */
16812   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16813     {
16814       /* An explicit specialization here is an error condition, and we
16815          expect the specialization handler to detect and report this.  */
16816       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16817           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16818         cp_parser_explicit_specialization (parser);
16819       else
16820         cp_parser_template_declaration (parser, /*member_p=*/true);
16821
16822       return;
16823     }
16824
16825   /* Check for a using-declaration.  */
16826   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16827     {
16828       /* Parse the using-declaration.  */
16829       cp_parser_using_declaration (parser,
16830                                    /*access_declaration_p=*/false);
16831       return;
16832     }
16833
16834   /* Check for @defs.  */
16835   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16836     {
16837       tree ivar, member;
16838       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16839       ivar = ivar_chains;
16840       while (ivar)
16841         {
16842           member = ivar;
16843           ivar = TREE_CHAIN (member);
16844           TREE_CHAIN (member) = NULL_TREE;
16845           finish_member_declaration (member);
16846         }
16847       return;
16848     }
16849
16850   /* If the next token is `static_assert' we have a static assertion.  */
16851   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16852     {
16853       cp_parser_static_assert (parser, /*member_p=*/true);
16854       return;
16855     }
16856
16857   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16858     return;
16859
16860   /* Parse the decl-specifier-seq.  */
16861   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16862   cp_parser_decl_specifier_seq (parser,
16863                                 CP_PARSER_FLAGS_OPTIONAL,
16864                                 &decl_specifiers,
16865                                 &declares_class_or_enum);
16866   prefix_attributes = decl_specifiers.attributes;
16867   decl_specifiers.attributes = NULL_TREE;
16868   /* Check for an invalid type-name.  */
16869   if (!decl_specifiers.any_type_specifiers_p
16870       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16871     return;
16872   /* If there is no declarator, then the decl-specifier-seq should
16873      specify a type.  */
16874   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16875     {
16876       /* If there was no decl-specifier-seq, and the next token is a
16877          `;', then we have something like:
16878
16879            struct S { ; };
16880
16881          [class.mem]
16882
16883          Each member-declaration shall declare at least one member
16884          name of the class.  */
16885       if (!decl_specifiers.any_specifiers_p)
16886         {
16887           cp_token *token = cp_lexer_peek_token (parser->lexer);
16888           if (!in_system_header_at (token->location))
16889             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16890         }
16891       else
16892         {
16893           tree type;
16894
16895           /* See if this declaration is a friend.  */
16896           friend_p = cp_parser_friend_p (&decl_specifiers);
16897           /* If there were decl-specifiers, check to see if there was
16898              a class-declaration.  */
16899           type = check_tag_decl (&decl_specifiers);
16900           /* Nested classes have already been added to the class, but
16901              a `friend' needs to be explicitly registered.  */
16902           if (friend_p)
16903             {
16904               /* If the `friend' keyword was present, the friend must
16905                  be introduced with a class-key.  */
16906                if (!declares_class_or_enum)
16907                  error_at (decl_spec_token_start->location,
16908                            "a class-key must be used when declaring a friend");
16909                /* In this case:
16910
16911                     template <typename T> struct A {
16912                       friend struct A<T>::B;
16913                     };
16914
16915                   A<T>::B will be represented by a TYPENAME_TYPE, and
16916                   therefore not recognized by check_tag_decl.  */
16917                if (!type
16918                    && decl_specifiers.type
16919                    && TYPE_P (decl_specifiers.type))
16920                  type = decl_specifiers.type;
16921                if (!type || !TYPE_P (type))
16922                  error_at (decl_spec_token_start->location,
16923                            "friend declaration does not name a class or "
16924                            "function");
16925                else
16926                  make_friend_class (current_class_type, type,
16927                                     /*complain=*/true);
16928             }
16929           /* If there is no TYPE, an error message will already have
16930              been issued.  */
16931           else if (!type || type == error_mark_node)
16932             ;
16933           /* An anonymous aggregate has to be handled specially; such
16934              a declaration really declares a data member (with a
16935              particular type), as opposed to a nested class.  */
16936           else if (ANON_AGGR_TYPE_P (type))
16937             {
16938               /* Remove constructors and such from TYPE, now that we
16939                  know it is an anonymous aggregate.  */
16940               fixup_anonymous_aggr (type);
16941               /* And make the corresponding data member.  */
16942               decl = build_decl (decl_spec_token_start->location,
16943                                  FIELD_DECL, NULL_TREE, type);
16944               /* Add it to the class.  */
16945               finish_member_declaration (decl);
16946             }
16947           else
16948             cp_parser_check_access_in_redeclaration
16949                                               (TYPE_NAME (type),
16950                                                decl_spec_token_start->location);
16951         }
16952     }
16953   else
16954     {
16955       /* See if these declarations will be friends.  */
16956       friend_p = cp_parser_friend_p (&decl_specifiers);
16957
16958       /* Keep going until we hit the `;' at the end of the
16959          declaration.  */
16960       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16961         {
16962           tree attributes = NULL_TREE;
16963           tree first_attribute;
16964
16965           /* Peek at the next token.  */
16966           token = cp_lexer_peek_token (parser->lexer);
16967
16968           /* Check for a bitfield declaration.  */
16969           if (token->type == CPP_COLON
16970               || (token->type == CPP_NAME
16971                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16972                   == CPP_COLON))
16973             {
16974               tree identifier;
16975               tree width;
16976
16977               /* Get the name of the bitfield.  Note that we cannot just
16978                  check TOKEN here because it may have been invalidated by
16979                  the call to cp_lexer_peek_nth_token above.  */
16980               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16981                 identifier = cp_parser_identifier (parser);
16982               else
16983                 identifier = NULL_TREE;
16984
16985               /* Consume the `:' token.  */
16986               cp_lexer_consume_token (parser->lexer);
16987               /* Get the width of the bitfield.  */
16988               width
16989                 = cp_parser_constant_expression (parser,
16990                                                  /*allow_non_constant=*/false,
16991                                                  NULL);
16992
16993               /* Look for attributes that apply to the bitfield.  */
16994               attributes = cp_parser_attributes_opt (parser);
16995               /* Remember which attributes are prefix attributes and
16996                  which are not.  */
16997               first_attribute = attributes;
16998               /* Combine the attributes.  */
16999               attributes = chainon (prefix_attributes, attributes);
17000
17001               /* Create the bitfield declaration.  */
17002               decl = grokbitfield (identifier
17003                                    ? make_id_declarator (NULL_TREE,
17004                                                          identifier,
17005                                                          sfk_none)
17006                                    : NULL,
17007                                    &decl_specifiers,
17008                                    width,
17009                                    attributes);
17010             }
17011           else
17012             {
17013               cp_declarator *declarator;
17014               tree initializer;
17015               tree asm_specification;
17016               int ctor_dtor_or_conv_p;
17017
17018               /* Parse the declarator.  */
17019               declarator
17020                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17021                                         &ctor_dtor_or_conv_p,
17022                                         /*parenthesized_p=*/NULL,
17023                                         /*member_p=*/true);
17024
17025               /* If something went wrong parsing the declarator, make sure
17026                  that we at least consume some tokens.  */
17027               if (declarator == cp_error_declarator)
17028                 {
17029                   /* Skip to the end of the statement.  */
17030                   cp_parser_skip_to_end_of_statement (parser);
17031                   /* If the next token is not a semicolon, that is
17032                      probably because we just skipped over the body of
17033                      a function.  So, we consume a semicolon if
17034                      present, but do not issue an error message if it
17035                      is not present.  */
17036                   if (cp_lexer_next_token_is (parser->lexer,
17037                                               CPP_SEMICOLON))
17038                     cp_lexer_consume_token (parser->lexer);
17039                   return;
17040                 }
17041
17042               if (declares_class_or_enum & 2)
17043                 cp_parser_check_for_definition_in_return_type
17044                                             (declarator, decl_specifiers.type,
17045                                              decl_specifiers.type_location);
17046
17047               /* Look for an asm-specification.  */
17048               asm_specification = cp_parser_asm_specification_opt (parser);
17049               /* Look for attributes that apply to the declaration.  */
17050               attributes = cp_parser_attributes_opt (parser);
17051               /* Remember which attributes are prefix attributes and
17052                  which are not.  */
17053               first_attribute = attributes;
17054               /* Combine the attributes.  */
17055               attributes = chainon (prefix_attributes, attributes);
17056
17057               /* If it's an `=', then we have a constant-initializer or a
17058                  pure-specifier.  It is not correct to parse the
17059                  initializer before registering the member declaration
17060                  since the member declaration should be in scope while
17061                  its initializer is processed.  However, the rest of the
17062                  front end does not yet provide an interface that allows
17063                  us to handle this correctly.  */
17064               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17065                 {
17066                   /* In [class.mem]:
17067
17068                      A pure-specifier shall be used only in the declaration of
17069                      a virtual function.
17070
17071                      A member-declarator can contain a constant-initializer
17072                      only if it declares a static member of integral or
17073                      enumeration type.
17074
17075                      Therefore, if the DECLARATOR is for a function, we look
17076                      for a pure-specifier; otherwise, we look for a
17077                      constant-initializer.  When we call `grokfield', it will
17078                      perform more stringent semantics checks.  */
17079                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17080                   if (function_declarator_p (declarator))
17081                     initializer = cp_parser_pure_specifier (parser);
17082                   else
17083                     /* Parse the initializer.  */
17084                     initializer = cp_parser_constant_initializer (parser);
17085                 }
17086               /* Otherwise, there is no initializer.  */
17087               else
17088                 initializer = NULL_TREE;
17089
17090               /* See if we are probably looking at a function
17091                  definition.  We are certainly not looking at a
17092                  member-declarator.  Calling `grokfield' has
17093                  side-effects, so we must not do it unless we are sure
17094                  that we are looking at a member-declarator.  */
17095               if (cp_parser_token_starts_function_definition_p
17096                   (cp_lexer_peek_token (parser->lexer)))
17097                 {
17098                   /* The grammar does not allow a pure-specifier to be
17099                      used when a member function is defined.  (It is
17100                      possible that this fact is an oversight in the
17101                      standard, since a pure function may be defined
17102                      outside of the class-specifier.  */
17103                   if (initializer)
17104                     error_at (initializer_token_start->location,
17105                               "pure-specifier on function-definition");
17106                   decl = cp_parser_save_member_function_body (parser,
17107                                                               &decl_specifiers,
17108                                                               declarator,
17109                                                               attributes);
17110                   /* If the member was not a friend, declare it here.  */
17111                   if (!friend_p)
17112                     finish_member_declaration (decl);
17113                   /* Peek at the next token.  */
17114                   token = cp_lexer_peek_token (parser->lexer);
17115                   /* If the next token is a semicolon, consume it.  */
17116                   if (token->type == CPP_SEMICOLON)
17117                     cp_lexer_consume_token (parser->lexer);
17118                   return;
17119                 }
17120               else
17121                 if (declarator->kind == cdk_function)
17122                   declarator->id_loc = token->location;
17123                 /* Create the declaration.  */
17124                 decl = grokfield (declarator, &decl_specifiers,
17125                                   initializer, /*init_const_expr_p=*/true,
17126                                   asm_specification,
17127                                   attributes);
17128             }
17129
17130           /* Reset PREFIX_ATTRIBUTES.  */
17131           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17132             attributes = TREE_CHAIN (attributes);
17133           if (attributes)
17134             TREE_CHAIN (attributes) = NULL_TREE;
17135
17136           /* If there is any qualification still in effect, clear it
17137              now; we will be starting fresh with the next declarator.  */
17138           parser->scope = NULL_TREE;
17139           parser->qualifying_scope = NULL_TREE;
17140           parser->object_scope = NULL_TREE;
17141           /* If it's a `,', then there are more declarators.  */
17142           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17143             cp_lexer_consume_token (parser->lexer);
17144           /* If the next token isn't a `;', then we have a parse error.  */
17145           else if (cp_lexer_next_token_is_not (parser->lexer,
17146                                                CPP_SEMICOLON))
17147             {
17148               cp_parser_error (parser, "expected %<;%>");
17149               /* Skip tokens until we find a `;'.  */
17150               cp_parser_skip_to_end_of_statement (parser);
17151
17152               break;
17153             }
17154
17155           if (decl)
17156             {
17157               /* Add DECL to the list of members.  */
17158               if (!friend_p)
17159                 finish_member_declaration (decl);
17160
17161               if (TREE_CODE (decl) == FUNCTION_DECL)
17162                 cp_parser_save_default_args (parser, decl);
17163             }
17164         }
17165     }
17166
17167   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17168 }
17169
17170 /* Parse a pure-specifier.
17171
17172    pure-specifier:
17173      = 0
17174
17175    Returns INTEGER_ZERO_NODE if a pure specifier is found.
17176    Otherwise, ERROR_MARK_NODE is returned.  */
17177
17178 static tree
17179 cp_parser_pure_specifier (cp_parser* parser)
17180 {
17181   cp_token *token;
17182
17183   /* Look for the `=' token.  */
17184   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17185     return error_mark_node;
17186   /* Look for the `0' token.  */
17187   token = cp_lexer_peek_token (parser->lexer);
17188
17189   if (token->type == CPP_EOF
17190       || token->type == CPP_PRAGMA_EOL)
17191     return error_mark_node;
17192
17193   cp_lexer_consume_token (parser->lexer);
17194
17195   /* Accept = default or = delete in c++0x mode.  */
17196   if (token->keyword == RID_DEFAULT
17197       || token->keyword == RID_DELETE)
17198     {
17199       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17200       return token->u.value;
17201     }
17202
17203   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
17204   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17205     {
17206       cp_parser_error (parser,
17207                        "invalid pure specifier (only %<= 0%> is allowed)");
17208       cp_parser_skip_to_end_of_statement (parser);
17209       return error_mark_node;
17210     }
17211   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17212     {
17213       error_at (token->location, "templates may not be %<virtual%>");
17214       return error_mark_node;
17215     }
17216
17217   return integer_zero_node;
17218 }
17219
17220 /* Parse a constant-initializer.
17221
17222    constant-initializer:
17223      = constant-expression
17224
17225    Returns a representation of the constant-expression.  */
17226
17227 static tree
17228 cp_parser_constant_initializer (cp_parser* parser)
17229 {
17230   /* Look for the `=' token.  */
17231   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17232     return error_mark_node;
17233
17234   /* It is invalid to write:
17235
17236        struct S { static const int i = { 7 }; };
17237
17238      */
17239   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17240     {
17241       cp_parser_error (parser,
17242                        "a brace-enclosed initializer is not allowed here");
17243       /* Consume the opening brace.  */
17244       cp_lexer_consume_token (parser->lexer);
17245       /* Skip the initializer.  */
17246       cp_parser_skip_to_closing_brace (parser);
17247       /* Look for the trailing `}'.  */
17248       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17249
17250       return error_mark_node;
17251     }
17252
17253   return cp_parser_constant_expression (parser,
17254                                         /*allow_non_constant=*/false,
17255                                         NULL);
17256 }
17257
17258 /* Derived classes [gram.class.derived] */
17259
17260 /* Parse a base-clause.
17261
17262    base-clause:
17263      : base-specifier-list
17264
17265    base-specifier-list:
17266      base-specifier ... [opt]
17267      base-specifier-list , base-specifier ... [opt]
17268
17269    Returns a TREE_LIST representing the base-classes, in the order in
17270    which they were declared.  The representation of each node is as
17271    described by cp_parser_base_specifier.
17272
17273    In the case that no bases are specified, this function will return
17274    NULL_TREE, not ERROR_MARK_NODE.  */
17275
17276 static tree
17277 cp_parser_base_clause (cp_parser* parser)
17278 {
17279   tree bases = NULL_TREE;
17280
17281   /* Look for the `:' that begins the list.  */
17282   cp_parser_require (parser, CPP_COLON, RT_COLON);
17283
17284   /* Scan the base-specifier-list.  */
17285   while (true)
17286     {
17287       cp_token *token;
17288       tree base;
17289       bool pack_expansion_p = false;
17290
17291       /* Look for the base-specifier.  */
17292       base = cp_parser_base_specifier (parser);
17293       /* Look for the (optional) ellipsis. */
17294       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17295         {
17296           /* Consume the `...'. */
17297           cp_lexer_consume_token (parser->lexer);
17298
17299           pack_expansion_p = true;
17300         }
17301
17302       /* Add BASE to the front of the list.  */
17303       if (base != error_mark_node)
17304         {
17305           if (pack_expansion_p)
17306             /* Make this a pack expansion type. */
17307             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17308           
17309
17310           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17311             {
17312               TREE_CHAIN (base) = bases;
17313               bases = base;
17314             }
17315         }
17316       /* Peek at the next token.  */
17317       token = cp_lexer_peek_token (parser->lexer);
17318       /* If it's not a comma, then the list is complete.  */
17319       if (token->type != CPP_COMMA)
17320         break;
17321       /* Consume the `,'.  */
17322       cp_lexer_consume_token (parser->lexer);
17323     }
17324
17325   /* PARSER->SCOPE may still be non-NULL at this point, if the last
17326      base class had a qualified name.  However, the next name that
17327      appears is certainly not qualified.  */
17328   parser->scope = NULL_TREE;
17329   parser->qualifying_scope = NULL_TREE;
17330   parser->object_scope = NULL_TREE;
17331
17332   return nreverse (bases);
17333 }
17334
17335 /* Parse a base-specifier.
17336
17337    base-specifier:
17338      :: [opt] nested-name-specifier [opt] class-name
17339      virtual access-specifier [opt] :: [opt] nested-name-specifier
17340        [opt] class-name
17341      access-specifier virtual [opt] :: [opt] nested-name-specifier
17342        [opt] class-name
17343
17344    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17345    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17346    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17347    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17348
17349 static tree
17350 cp_parser_base_specifier (cp_parser* parser)
17351 {
17352   cp_token *token;
17353   bool done = false;
17354   bool virtual_p = false;
17355   bool duplicate_virtual_error_issued_p = false;
17356   bool duplicate_access_error_issued_p = false;
17357   bool class_scope_p, template_p;
17358   tree access = access_default_node;
17359   tree type;
17360
17361   /* Process the optional `virtual' and `access-specifier'.  */
17362   while (!done)
17363     {
17364       /* Peek at the next token.  */
17365       token = cp_lexer_peek_token (parser->lexer);
17366       /* Process `virtual'.  */
17367       switch (token->keyword)
17368         {
17369         case RID_VIRTUAL:
17370           /* If `virtual' appears more than once, issue an error.  */
17371           if (virtual_p && !duplicate_virtual_error_issued_p)
17372             {
17373               cp_parser_error (parser,
17374                                "%<virtual%> specified more than once in base-specified");
17375               duplicate_virtual_error_issued_p = true;
17376             }
17377
17378           virtual_p = true;
17379
17380           /* Consume the `virtual' token.  */
17381           cp_lexer_consume_token (parser->lexer);
17382
17383           break;
17384
17385         case RID_PUBLIC:
17386         case RID_PROTECTED:
17387         case RID_PRIVATE:
17388           /* If more than one access specifier appears, issue an
17389              error.  */
17390           if (access != access_default_node
17391               && !duplicate_access_error_issued_p)
17392             {
17393               cp_parser_error (parser,
17394                                "more than one access specifier in base-specified");
17395               duplicate_access_error_issued_p = true;
17396             }
17397
17398           access = ridpointers[(int) token->keyword];
17399
17400           /* Consume the access-specifier.  */
17401           cp_lexer_consume_token (parser->lexer);
17402
17403           break;
17404
17405         default:
17406           done = true;
17407           break;
17408         }
17409     }
17410   /* It is not uncommon to see programs mechanically, erroneously, use
17411      the 'typename' keyword to denote (dependent) qualified types
17412      as base classes.  */
17413   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17414     {
17415       token = cp_lexer_peek_token (parser->lexer);
17416       if (!processing_template_decl)
17417         error_at (token->location,
17418                   "keyword %<typename%> not allowed outside of templates");
17419       else
17420         error_at (token->location,
17421                   "keyword %<typename%> not allowed in this context "
17422                   "(the base class is implicitly a type)");
17423       cp_lexer_consume_token (parser->lexer);
17424     }
17425
17426   /* Look for the optional `::' operator.  */
17427   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17428   /* Look for the nested-name-specifier.  The simplest way to
17429      implement:
17430
17431        [temp.res]
17432
17433        The keyword `typename' is not permitted in a base-specifier or
17434        mem-initializer; in these contexts a qualified name that
17435        depends on a template-parameter is implicitly assumed to be a
17436        type name.
17437
17438      is to pretend that we have seen the `typename' keyword at this
17439      point.  */
17440   cp_parser_nested_name_specifier_opt (parser,
17441                                        /*typename_keyword_p=*/true,
17442                                        /*check_dependency_p=*/true,
17443                                        typename_type,
17444                                        /*is_declaration=*/true);
17445   /* If the base class is given by a qualified name, assume that names
17446      we see are type names or templates, as appropriate.  */
17447   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17448   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17449
17450   /* Finally, look for the class-name.  */
17451   type = cp_parser_class_name (parser,
17452                                class_scope_p,
17453                                template_p,
17454                                typename_type,
17455                                /*check_dependency_p=*/true,
17456                                /*class_head_p=*/false,
17457                                /*is_declaration=*/true);
17458
17459   if (type == error_mark_node)
17460     return error_mark_node;
17461
17462   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17463 }
17464
17465 /* Exception handling [gram.exception] */
17466
17467 /* Parse an (optional) exception-specification.
17468
17469    exception-specification:
17470      throw ( type-id-list [opt] )
17471
17472    Returns a TREE_LIST representing the exception-specification.  The
17473    TREE_VALUE of each node is a type.  */
17474
17475 static tree
17476 cp_parser_exception_specification_opt (cp_parser* parser)
17477 {
17478   cp_token *token;
17479   tree type_id_list;
17480
17481   /* Peek at the next token.  */
17482   token = cp_lexer_peek_token (parser->lexer);
17483   /* If it's not `throw', then there's no exception-specification.  */
17484   if (!cp_parser_is_keyword (token, RID_THROW))
17485     return NULL_TREE;
17486
17487   /* Consume the `throw'.  */
17488   cp_lexer_consume_token (parser->lexer);
17489
17490   /* Look for the `('.  */
17491   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17492
17493   /* Peek at the next token.  */
17494   token = cp_lexer_peek_token (parser->lexer);
17495   /* If it's not a `)', then there is a type-id-list.  */
17496   if (token->type != CPP_CLOSE_PAREN)
17497     {
17498       const char *saved_message;
17499
17500       /* Types may not be defined in an exception-specification.  */
17501       saved_message = parser->type_definition_forbidden_message;
17502       parser->type_definition_forbidden_message
17503         = G_("types may not be defined in an exception-specification");
17504       /* Parse the type-id-list.  */
17505       type_id_list = cp_parser_type_id_list (parser);
17506       /* Restore the saved message.  */
17507       parser->type_definition_forbidden_message = saved_message;
17508     }
17509   else
17510     type_id_list = empty_except_spec;
17511
17512   /* Look for the `)'.  */
17513   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17514
17515   return type_id_list;
17516 }
17517
17518 /* Parse an (optional) type-id-list.
17519
17520    type-id-list:
17521      type-id ... [opt]
17522      type-id-list , type-id ... [opt]
17523
17524    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17525    in the order that the types were presented.  */
17526
17527 static tree
17528 cp_parser_type_id_list (cp_parser* parser)
17529 {
17530   tree types = NULL_TREE;
17531
17532   while (true)
17533     {
17534       cp_token *token;
17535       tree type;
17536
17537       /* Get the next type-id.  */
17538       type = cp_parser_type_id (parser);
17539       /* Parse the optional ellipsis. */
17540       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17541         {
17542           /* Consume the `...'. */
17543           cp_lexer_consume_token (parser->lexer);
17544
17545           /* Turn the type into a pack expansion expression. */
17546           type = make_pack_expansion (type);
17547         }
17548       /* Add it to the list.  */
17549       types = add_exception_specifier (types, type, /*complain=*/1);
17550       /* Peek at the next token.  */
17551       token = cp_lexer_peek_token (parser->lexer);
17552       /* If it is not a `,', we are done.  */
17553       if (token->type != CPP_COMMA)
17554         break;
17555       /* Consume the `,'.  */
17556       cp_lexer_consume_token (parser->lexer);
17557     }
17558
17559   return nreverse (types);
17560 }
17561
17562 /* Parse a try-block.
17563
17564    try-block:
17565      try compound-statement handler-seq  */
17566
17567 static tree
17568 cp_parser_try_block (cp_parser* parser)
17569 {
17570   tree try_block;
17571
17572   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
17573   try_block = begin_try_block ();
17574   cp_parser_compound_statement (parser, NULL, true);
17575   finish_try_block (try_block);
17576   cp_parser_handler_seq (parser);
17577   finish_handler_sequence (try_block);
17578
17579   return try_block;
17580 }
17581
17582 /* Parse a function-try-block.
17583
17584    function-try-block:
17585      try ctor-initializer [opt] function-body handler-seq  */
17586
17587 static bool
17588 cp_parser_function_try_block (cp_parser* parser)
17589 {
17590   tree compound_stmt;
17591   tree try_block;
17592   bool ctor_initializer_p;
17593
17594   /* Look for the `try' keyword.  */
17595   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
17596     return false;
17597   /* Let the rest of the front end know where we are.  */
17598   try_block = begin_function_try_block (&compound_stmt);
17599   /* Parse the function-body.  */
17600   ctor_initializer_p
17601     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17602   /* We're done with the `try' part.  */
17603   finish_function_try_block (try_block);
17604   /* Parse the handlers.  */
17605   cp_parser_handler_seq (parser);
17606   /* We're done with the handlers.  */
17607   finish_function_handler_sequence (try_block, compound_stmt);
17608
17609   return ctor_initializer_p;
17610 }
17611
17612 /* Parse a handler-seq.
17613
17614    handler-seq:
17615      handler handler-seq [opt]  */
17616
17617 static void
17618 cp_parser_handler_seq (cp_parser* parser)
17619 {
17620   while (true)
17621     {
17622       cp_token *token;
17623
17624       /* Parse the handler.  */
17625       cp_parser_handler (parser);
17626       /* Peek at the next token.  */
17627       token = cp_lexer_peek_token (parser->lexer);
17628       /* If it's not `catch' then there are no more handlers.  */
17629       if (!cp_parser_is_keyword (token, RID_CATCH))
17630         break;
17631     }
17632 }
17633
17634 /* Parse a handler.
17635
17636    handler:
17637      catch ( exception-declaration ) compound-statement  */
17638
17639 static void
17640 cp_parser_handler (cp_parser* parser)
17641 {
17642   tree handler;
17643   tree declaration;
17644
17645   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
17646   handler = begin_handler ();
17647   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17648   declaration = cp_parser_exception_declaration (parser);
17649   finish_handler_parms (declaration, handler);
17650   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17651   cp_parser_compound_statement (parser, NULL, false);
17652   finish_handler (handler);
17653 }
17654
17655 /* Parse an exception-declaration.
17656
17657    exception-declaration:
17658      type-specifier-seq declarator
17659      type-specifier-seq abstract-declarator
17660      type-specifier-seq
17661      ...
17662
17663    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17664    ellipsis variant is used.  */
17665
17666 static tree
17667 cp_parser_exception_declaration (cp_parser* parser)
17668 {
17669   cp_decl_specifier_seq type_specifiers;
17670   cp_declarator *declarator;
17671   const char *saved_message;
17672
17673   /* If it's an ellipsis, it's easy to handle.  */
17674   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17675     {
17676       /* Consume the `...' token.  */
17677       cp_lexer_consume_token (parser->lexer);
17678       return NULL_TREE;
17679     }
17680
17681   /* Types may not be defined in exception-declarations.  */
17682   saved_message = parser->type_definition_forbidden_message;
17683   parser->type_definition_forbidden_message
17684     = G_("types may not be defined in exception-declarations");
17685
17686   /* Parse the type-specifier-seq.  */
17687   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17688                                 /*is_trailing_return=*/false,
17689                                 &type_specifiers);
17690   /* If it's a `)', then there is no declarator.  */
17691   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17692     declarator = NULL;
17693   else
17694     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17695                                        /*ctor_dtor_or_conv_p=*/NULL,
17696                                        /*parenthesized_p=*/NULL,
17697                                        /*member_p=*/false);
17698
17699   /* Restore the saved message.  */
17700   parser->type_definition_forbidden_message = saved_message;
17701
17702   if (!type_specifiers.any_specifiers_p)
17703     return error_mark_node;
17704
17705   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17706 }
17707
17708 /* Parse a throw-expression.
17709
17710    throw-expression:
17711      throw assignment-expression [opt]
17712
17713    Returns a THROW_EXPR representing the throw-expression.  */
17714
17715 static tree
17716 cp_parser_throw_expression (cp_parser* parser)
17717 {
17718   tree expression;
17719   cp_token* token;
17720
17721   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
17722   token = cp_lexer_peek_token (parser->lexer);
17723   /* Figure out whether or not there is an assignment-expression
17724      following the "throw" keyword.  */
17725   if (token->type == CPP_COMMA
17726       || token->type == CPP_SEMICOLON
17727       || token->type == CPP_CLOSE_PAREN
17728       || token->type == CPP_CLOSE_SQUARE
17729       || token->type == CPP_CLOSE_BRACE
17730       || token->type == CPP_COLON)
17731     expression = NULL_TREE;
17732   else
17733     expression = cp_parser_assignment_expression (parser,
17734                                                   /*cast_p=*/false, NULL);
17735
17736   return build_throw (expression);
17737 }
17738
17739 /* GNU Extensions */
17740
17741 /* Parse an (optional) asm-specification.
17742
17743    asm-specification:
17744      asm ( string-literal )
17745
17746    If the asm-specification is present, returns a STRING_CST
17747    corresponding to the string-literal.  Otherwise, returns
17748    NULL_TREE.  */
17749
17750 static tree
17751 cp_parser_asm_specification_opt (cp_parser* parser)
17752 {
17753   cp_token *token;
17754   tree asm_specification;
17755
17756   /* Peek at the next token.  */
17757   token = cp_lexer_peek_token (parser->lexer);
17758   /* If the next token isn't the `asm' keyword, then there's no
17759      asm-specification.  */
17760   if (!cp_parser_is_keyword (token, RID_ASM))
17761     return NULL_TREE;
17762
17763   /* Consume the `asm' token.  */
17764   cp_lexer_consume_token (parser->lexer);
17765   /* Look for the `('.  */
17766   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17767
17768   /* Look for the string-literal.  */
17769   asm_specification = cp_parser_string_literal (parser, false, false);
17770
17771   /* Look for the `)'.  */
17772   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17773
17774   return asm_specification;
17775 }
17776
17777 /* Parse an asm-operand-list.
17778
17779    asm-operand-list:
17780      asm-operand
17781      asm-operand-list , asm-operand
17782
17783    asm-operand:
17784      string-literal ( expression )
17785      [ string-literal ] string-literal ( expression )
17786
17787    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17788    each node is the expression.  The TREE_PURPOSE is itself a
17789    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17790    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17791    is a STRING_CST for the string literal before the parenthesis. Returns
17792    ERROR_MARK_NODE if any of the operands are invalid.  */
17793
17794 static tree
17795 cp_parser_asm_operand_list (cp_parser* parser)
17796 {
17797   tree asm_operands = NULL_TREE;
17798   bool invalid_operands = false;
17799
17800   while (true)
17801     {
17802       tree string_literal;
17803       tree expression;
17804       tree name;
17805
17806       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17807         {
17808           /* Consume the `[' token.  */
17809           cp_lexer_consume_token (parser->lexer);
17810           /* Read the operand name.  */
17811           name = cp_parser_identifier (parser);
17812           if (name != error_mark_node)
17813             name = build_string (IDENTIFIER_LENGTH (name),
17814                                  IDENTIFIER_POINTER (name));
17815           /* Look for the closing `]'.  */
17816           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17817         }
17818       else
17819         name = NULL_TREE;
17820       /* Look for the string-literal.  */
17821       string_literal = cp_parser_string_literal (parser, false, false);
17822
17823       /* Look for the `('.  */
17824       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17825       /* Parse the expression.  */
17826       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17827       /* Look for the `)'.  */
17828       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17829
17830       if (name == error_mark_node 
17831           || string_literal == error_mark_node 
17832           || expression == error_mark_node)
17833         invalid_operands = true;
17834
17835       /* Add this operand to the list.  */
17836       asm_operands = tree_cons (build_tree_list (name, string_literal),
17837                                 expression,
17838                                 asm_operands);
17839       /* If the next token is not a `,', there are no more
17840          operands.  */
17841       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17842         break;
17843       /* Consume the `,'.  */
17844       cp_lexer_consume_token (parser->lexer);
17845     }
17846
17847   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17848 }
17849
17850 /* Parse an asm-clobber-list.
17851
17852    asm-clobber-list:
17853      string-literal
17854      asm-clobber-list , string-literal
17855
17856    Returns a TREE_LIST, indicating the clobbers in the order that they
17857    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17858
17859 static tree
17860 cp_parser_asm_clobber_list (cp_parser* parser)
17861 {
17862   tree clobbers = NULL_TREE;
17863
17864   while (true)
17865     {
17866       tree string_literal;
17867
17868       /* Look for the string literal.  */
17869       string_literal = cp_parser_string_literal (parser, false, false);
17870       /* Add it to the list.  */
17871       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17872       /* If the next token is not a `,', then the list is
17873          complete.  */
17874       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17875         break;
17876       /* Consume the `,' token.  */
17877       cp_lexer_consume_token (parser->lexer);
17878     }
17879
17880   return clobbers;
17881 }
17882
17883 /* Parse an asm-label-list.
17884
17885    asm-label-list:
17886      identifier
17887      asm-label-list , identifier
17888
17889    Returns a TREE_LIST, indicating the labels in the order that they
17890    appeared.  The TREE_VALUE of each node is a label.  */
17891
17892 static tree
17893 cp_parser_asm_label_list (cp_parser* parser)
17894 {
17895   tree labels = NULL_TREE;
17896
17897   while (true)
17898     {
17899       tree identifier, label, name;
17900
17901       /* Look for the identifier.  */
17902       identifier = cp_parser_identifier (parser);
17903       if (!error_operand_p (identifier))
17904         {
17905           label = lookup_label (identifier);
17906           if (TREE_CODE (label) == LABEL_DECL)
17907             {
17908               TREE_USED (label) = 1;
17909               check_goto (label);
17910               name = build_string (IDENTIFIER_LENGTH (identifier),
17911                                    IDENTIFIER_POINTER (identifier));
17912               labels = tree_cons (name, label, labels);
17913             }
17914         }
17915       /* If the next token is not a `,', then the list is
17916          complete.  */
17917       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17918         break;
17919       /* Consume the `,' token.  */
17920       cp_lexer_consume_token (parser->lexer);
17921     }
17922
17923   return nreverse (labels);
17924 }
17925
17926 /* Parse an (optional) series of attributes.
17927
17928    attributes:
17929      attributes attribute
17930
17931    attribute:
17932      __attribute__ (( attribute-list [opt] ))
17933
17934    The return value is as for cp_parser_attribute_list.  */
17935
17936 static tree
17937 cp_parser_attributes_opt (cp_parser* parser)
17938 {
17939   tree attributes = NULL_TREE;
17940
17941   while (true)
17942     {
17943       cp_token *token;
17944       tree attribute_list;
17945
17946       /* Peek at the next token.  */
17947       token = cp_lexer_peek_token (parser->lexer);
17948       /* If it's not `__attribute__', then we're done.  */
17949       if (token->keyword != RID_ATTRIBUTE)
17950         break;
17951
17952       /* Consume the `__attribute__' keyword.  */
17953       cp_lexer_consume_token (parser->lexer);
17954       /* Look for the two `(' tokens.  */
17955       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17956       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17957
17958       /* Peek at the next token.  */
17959       token = cp_lexer_peek_token (parser->lexer);
17960       if (token->type != CPP_CLOSE_PAREN)
17961         /* Parse the attribute-list.  */
17962         attribute_list = cp_parser_attribute_list (parser);
17963       else
17964         /* If the next token is a `)', then there is no attribute
17965            list.  */
17966         attribute_list = NULL;
17967
17968       /* Look for the two `)' tokens.  */
17969       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17970       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17971
17972       /* Add these new attributes to the list.  */
17973       attributes = chainon (attributes, attribute_list);
17974     }
17975
17976   return attributes;
17977 }
17978
17979 /* Parse an attribute-list.
17980
17981    attribute-list:
17982      attribute
17983      attribute-list , attribute
17984
17985    attribute:
17986      identifier
17987      identifier ( identifier )
17988      identifier ( identifier , expression-list )
17989      identifier ( expression-list )
17990
17991    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17992    to an attribute.  The TREE_PURPOSE of each node is the identifier
17993    indicating which attribute is in use.  The TREE_VALUE represents
17994    the arguments, if any.  */
17995
17996 static tree
17997 cp_parser_attribute_list (cp_parser* parser)
17998 {
17999   tree attribute_list = NULL_TREE;
18000   bool save_translate_strings_p = parser->translate_strings_p;
18001
18002   parser->translate_strings_p = false;
18003   while (true)
18004     {
18005       cp_token *token;
18006       tree identifier;
18007       tree attribute;
18008
18009       /* Look for the identifier.  We also allow keywords here; for
18010          example `__attribute__ ((const))' is legal.  */
18011       token = cp_lexer_peek_token (parser->lexer);
18012       if (token->type == CPP_NAME
18013           || token->type == CPP_KEYWORD)
18014         {
18015           tree arguments = NULL_TREE;
18016
18017           /* Consume the token.  */
18018           token = cp_lexer_consume_token (parser->lexer);
18019
18020           /* Save away the identifier that indicates which attribute
18021              this is.  */
18022           identifier = (token->type == CPP_KEYWORD) 
18023             /* For keywords, use the canonical spelling, not the
18024                parsed identifier.  */
18025             ? ridpointers[(int) token->keyword]
18026             : token->u.value;
18027           
18028           attribute = build_tree_list (identifier, NULL_TREE);
18029
18030           /* Peek at the next token.  */
18031           token = cp_lexer_peek_token (parser->lexer);
18032           /* If it's an `(', then parse the attribute arguments.  */
18033           if (token->type == CPP_OPEN_PAREN)
18034             {
18035               VEC(tree,gc) *vec;
18036               int attr_flag = (attribute_takes_identifier_p (identifier)
18037                                ? id_attr : normal_attr);
18038               vec = cp_parser_parenthesized_expression_list
18039                     (parser, attr_flag, /*cast_p=*/false,
18040                      /*allow_expansion_p=*/false,
18041                      /*non_constant_p=*/NULL);
18042               if (vec == NULL)
18043                 arguments = error_mark_node;
18044               else
18045                 {
18046                   arguments = build_tree_list_vec (vec);
18047                   release_tree_vector (vec);
18048                 }
18049               /* Save the arguments away.  */
18050               TREE_VALUE (attribute) = arguments;
18051             }
18052
18053           if (arguments != error_mark_node)
18054             {
18055               /* Add this attribute to the list.  */
18056               TREE_CHAIN (attribute) = attribute_list;
18057               attribute_list = attribute;
18058             }
18059
18060           token = cp_lexer_peek_token (parser->lexer);
18061         }
18062       /* Now, look for more attributes.  If the next token isn't a
18063          `,', we're done.  */
18064       if (token->type != CPP_COMMA)
18065         break;
18066
18067       /* Consume the comma and keep going.  */
18068       cp_lexer_consume_token (parser->lexer);
18069     }
18070   parser->translate_strings_p = save_translate_strings_p;
18071
18072   /* We built up the list in reverse order.  */
18073   return nreverse (attribute_list);
18074 }
18075
18076 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
18077    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
18078    current value of the PEDANTIC flag, regardless of whether or not
18079    the `__extension__' keyword is present.  The caller is responsible
18080    for restoring the value of the PEDANTIC flag.  */
18081
18082 static bool
18083 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18084 {
18085   /* Save the old value of the PEDANTIC flag.  */
18086   *saved_pedantic = pedantic;
18087
18088   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18089     {
18090       /* Consume the `__extension__' token.  */
18091       cp_lexer_consume_token (parser->lexer);
18092       /* We're not being pedantic while the `__extension__' keyword is
18093          in effect.  */
18094       pedantic = 0;
18095
18096       return true;
18097     }
18098
18099   return false;
18100 }
18101
18102 /* Parse a label declaration.
18103
18104    label-declaration:
18105      __label__ label-declarator-seq ;
18106
18107    label-declarator-seq:
18108      identifier , label-declarator-seq
18109      identifier  */
18110
18111 static void
18112 cp_parser_label_declaration (cp_parser* parser)
18113 {
18114   /* Look for the `__label__' keyword.  */
18115   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18116
18117   while (true)
18118     {
18119       tree identifier;
18120
18121       /* Look for an identifier.  */
18122       identifier = cp_parser_identifier (parser);
18123       /* If we failed, stop.  */
18124       if (identifier == error_mark_node)
18125         break;
18126       /* Declare it as a label.  */
18127       finish_label_decl (identifier);
18128       /* If the next token is a `;', stop.  */
18129       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18130         break;
18131       /* Look for the `,' separating the label declarations.  */
18132       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18133     }
18134
18135   /* Look for the final `;'.  */
18136   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18137 }
18138
18139 /* Support Functions */
18140
18141 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18142    NAME should have one of the representations used for an
18143    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18144    is returned.  If PARSER->SCOPE is a dependent type, then a
18145    SCOPE_REF is returned.
18146
18147    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18148    returned; the name was already resolved when the TEMPLATE_ID_EXPR
18149    was formed.  Abstractly, such entities should not be passed to this
18150    function, because they do not need to be looked up, but it is
18151    simpler to check for this special case here, rather than at the
18152    call-sites.
18153
18154    In cases not explicitly covered above, this function returns a
18155    DECL, OVERLOAD, or baselink representing the result of the lookup.
18156    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18157    is returned.
18158
18159    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18160    (e.g., "struct") that was used.  In that case bindings that do not
18161    refer to types are ignored.
18162
18163    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18164    ignored.
18165
18166    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18167    are ignored.
18168
18169    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18170    types.
18171
18172    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18173    TREE_LIST of candidates if name-lookup results in an ambiguity, and
18174    NULL_TREE otherwise.  */
18175
18176 static tree
18177 cp_parser_lookup_name (cp_parser *parser, tree name,
18178                        enum tag_types tag_type,
18179                        bool is_template,
18180                        bool is_namespace,
18181                        bool check_dependency,
18182                        tree *ambiguous_decls,
18183                        location_t name_location)
18184 {
18185   int flags = 0;
18186   tree decl;
18187   tree object_type = parser->context->object_type;
18188
18189   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18190     flags |= LOOKUP_COMPLAIN;
18191
18192   /* Assume that the lookup will be unambiguous.  */
18193   if (ambiguous_decls)
18194     *ambiguous_decls = NULL_TREE;
18195
18196   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18197      no longer valid.  Note that if we are parsing tentatively, and
18198      the parse fails, OBJECT_TYPE will be automatically restored.  */
18199   parser->context->object_type = NULL_TREE;
18200
18201   if (name == error_mark_node)
18202     return error_mark_node;
18203
18204   /* A template-id has already been resolved; there is no lookup to
18205      do.  */
18206   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18207     return name;
18208   if (BASELINK_P (name))
18209     {
18210       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18211                   == TEMPLATE_ID_EXPR);
18212       return name;
18213     }
18214
18215   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
18216      it should already have been checked to make sure that the name
18217      used matches the type being destroyed.  */
18218   if (TREE_CODE (name) == BIT_NOT_EXPR)
18219     {
18220       tree type;
18221
18222       /* Figure out to which type this destructor applies.  */
18223       if (parser->scope)
18224         type = parser->scope;
18225       else if (object_type)
18226         type = object_type;
18227       else
18228         type = current_class_type;
18229       /* If that's not a class type, there is no destructor.  */
18230       if (!type || !CLASS_TYPE_P (type))
18231         return error_mark_node;
18232       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18233         lazily_declare_fn (sfk_destructor, type);
18234       if (!CLASSTYPE_DESTRUCTORS (type))
18235           return error_mark_node;
18236       /* If it was a class type, return the destructor.  */
18237       return CLASSTYPE_DESTRUCTORS (type);
18238     }
18239
18240   /* By this point, the NAME should be an ordinary identifier.  If
18241      the id-expression was a qualified name, the qualifying scope is
18242      stored in PARSER->SCOPE at this point.  */
18243   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18244
18245   /* Perform the lookup.  */
18246   if (parser->scope)
18247     {
18248       bool dependent_p;
18249
18250       if (parser->scope == error_mark_node)
18251         return error_mark_node;
18252
18253       /* If the SCOPE is dependent, the lookup must be deferred until
18254          the template is instantiated -- unless we are explicitly
18255          looking up names in uninstantiated templates.  Even then, we
18256          cannot look up the name if the scope is not a class type; it
18257          might, for example, be a template type parameter.  */
18258       dependent_p = (TYPE_P (parser->scope)
18259                      && dependent_scope_p (parser->scope));
18260       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18261           && dependent_p)
18262         /* Defer lookup.  */
18263         decl = error_mark_node;
18264       else
18265         {
18266           tree pushed_scope = NULL_TREE;
18267
18268           /* If PARSER->SCOPE is a dependent type, then it must be a
18269              class type, and we must not be checking dependencies;
18270              otherwise, we would have processed this lookup above.  So
18271              that PARSER->SCOPE is not considered a dependent base by
18272              lookup_member, we must enter the scope here.  */
18273           if (dependent_p)
18274             pushed_scope = push_scope (parser->scope);
18275
18276           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18277              lookup result and the nested-name-specifier nominates a class C:
18278                * if the name specified after the nested-name-specifier, when
18279                looked up in C, is the injected-class-name of C (Clause 9), or
18280                * if the name specified after the nested-name-specifier is the
18281                same as the identifier or the simple-template-id's template-
18282                name in the last component of the nested-name-specifier,
18283              the name is instead considered to name the constructor of
18284              class C. [ Note: for example, the constructor is not an
18285              acceptable lookup result in an elaborated-type-specifier so
18286              the constructor would not be used in place of the
18287              injected-class-name. --end note ] Such a constructor name
18288              shall be used only in the declarator-id of a declaration that
18289              names a constructor or in a using-declaration.  */
18290           if (tag_type == none_type
18291               && CLASS_TYPE_P (parser->scope)
18292               && constructor_name_p (name, parser->scope))
18293             name = ctor_identifier;
18294
18295           /* If the PARSER->SCOPE is a template specialization, it
18296              may be instantiated during name lookup.  In that case,
18297              errors may be issued.  Even if we rollback the current
18298              tentative parse, those errors are valid.  */
18299           decl = lookup_qualified_name (parser->scope, name,
18300                                         tag_type != none_type,
18301                                         /*complain=*/true);
18302
18303           /* If we have a single function from a using decl, pull it out.  */
18304           if (TREE_CODE (decl) == OVERLOAD
18305               && !really_overloaded_fn (decl))
18306             decl = OVL_FUNCTION (decl);
18307
18308           if (pushed_scope)
18309             pop_scope (pushed_scope);
18310         }
18311
18312       /* If the scope is a dependent type and either we deferred lookup or
18313          we did lookup but didn't find the name, rememeber the name.  */
18314       if (decl == error_mark_node && TYPE_P (parser->scope)
18315           && dependent_type_p (parser->scope))
18316         {
18317           if (tag_type)
18318             {
18319               tree type;
18320
18321               /* The resolution to Core Issue 180 says that `struct
18322                  A::B' should be considered a type-name, even if `A'
18323                  is dependent.  */
18324               type = make_typename_type (parser->scope, name, tag_type,
18325                                          /*complain=*/tf_error);
18326               decl = TYPE_NAME (type);
18327             }
18328           else if (is_template
18329                    && (cp_parser_next_token_ends_template_argument_p (parser)
18330                        || cp_lexer_next_token_is (parser->lexer,
18331                                                   CPP_CLOSE_PAREN)))
18332             decl = make_unbound_class_template (parser->scope,
18333                                                 name, NULL_TREE,
18334                                                 /*complain=*/tf_error);
18335           else
18336             decl = build_qualified_name (/*type=*/NULL_TREE,
18337                                          parser->scope, name,
18338                                          is_template);
18339         }
18340       parser->qualifying_scope = parser->scope;
18341       parser->object_scope = NULL_TREE;
18342     }
18343   else if (object_type)
18344     {
18345       tree object_decl = NULL_TREE;
18346       /* Look up the name in the scope of the OBJECT_TYPE, unless the
18347          OBJECT_TYPE is not a class.  */
18348       if (CLASS_TYPE_P (object_type))
18349         /* If the OBJECT_TYPE is a template specialization, it may
18350            be instantiated during name lookup.  In that case, errors
18351            may be issued.  Even if we rollback the current tentative
18352            parse, those errors are valid.  */
18353         object_decl = lookup_member (object_type,
18354                                      name,
18355                                      /*protect=*/0,
18356                                      tag_type != none_type);
18357       /* Look it up in the enclosing context, too.  */
18358       decl = lookup_name_real (name, tag_type != none_type,
18359                                /*nonclass=*/0,
18360                                /*block_p=*/true, is_namespace, flags);
18361       parser->object_scope = object_type;
18362       parser->qualifying_scope = NULL_TREE;
18363       if (object_decl)
18364         decl = object_decl;
18365     }
18366   else
18367     {
18368       decl = lookup_name_real (name, tag_type != none_type,
18369                                /*nonclass=*/0,
18370                                /*block_p=*/true, is_namespace, flags);
18371       parser->qualifying_scope = NULL_TREE;
18372       parser->object_scope = NULL_TREE;
18373     }
18374
18375   /* If the lookup failed, let our caller know.  */
18376   if (!decl || decl == error_mark_node)
18377     return error_mark_node;
18378
18379   /* Pull out the template from an injected-class-name (or multiple).  */
18380   if (is_template)
18381     decl = maybe_get_template_decl_from_type_decl (decl);
18382
18383   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18384   if (TREE_CODE (decl) == TREE_LIST)
18385     {
18386       if (ambiguous_decls)
18387         *ambiguous_decls = decl;
18388       /* The error message we have to print is too complicated for
18389          cp_parser_error, so we incorporate its actions directly.  */
18390       if (!cp_parser_simulate_error (parser))
18391         {
18392           error_at (name_location, "reference to %qD is ambiguous",
18393                     name);
18394           print_candidates (decl);
18395         }
18396       return error_mark_node;
18397     }
18398
18399   gcc_assert (DECL_P (decl)
18400               || TREE_CODE (decl) == OVERLOAD
18401               || TREE_CODE (decl) == SCOPE_REF
18402               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18403               || BASELINK_P (decl));
18404
18405   /* If we have resolved the name of a member declaration, check to
18406      see if the declaration is accessible.  When the name resolves to
18407      set of overloaded functions, accessibility is checked when
18408      overload resolution is done.
18409
18410      During an explicit instantiation, access is not checked at all,
18411      as per [temp.explicit].  */
18412   if (DECL_P (decl))
18413     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18414
18415   return decl;
18416 }
18417
18418 /* Like cp_parser_lookup_name, but for use in the typical case where
18419    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18420    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18421
18422 static tree
18423 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18424 {
18425   return cp_parser_lookup_name (parser, name,
18426                                 none_type,
18427                                 /*is_template=*/false,
18428                                 /*is_namespace=*/false,
18429                                 /*check_dependency=*/true,
18430                                 /*ambiguous_decls=*/NULL,
18431                                 location);
18432 }
18433
18434 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18435    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18436    true, the DECL indicates the class being defined in a class-head,
18437    or declared in an elaborated-type-specifier.
18438
18439    Otherwise, return DECL.  */
18440
18441 static tree
18442 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18443 {
18444   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18445      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18446
18447        struct A {
18448          template <typename T> struct B;
18449        };
18450
18451        template <typename T> struct A::B {};
18452
18453      Similarly, in an elaborated-type-specifier:
18454
18455        namespace N { struct X{}; }
18456
18457        struct A {
18458          template <typename T> friend struct N::X;
18459        };
18460
18461      However, if the DECL refers to a class type, and we are in
18462      the scope of the class, then the name lookup automatically
18463      finds the TYPE_DECL created by build_self_reference rather
18464      than a TEMPLATE_DECL.  For example, in:
18465
18466        template <class T> struct S {
18467          S s;
18468        };
18469
18470      there is no need to handle such case.  */
18471
18472   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18473     return DECL_TEMPLATE_RESULT (decl);
18474
18475   return decl;
18476 }
18477
18478 /* If too many, or too few, template-parameter lists apply to the
18479    declarator, issue an error message.  Returns TRUE if all went well,
18480    and FALSE otherwise.  */
18481
18482 static bool
18483 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18484                                                 cp_declarator *declarator,
18485                                                 location_t declarator_location)
18486 {
18487   unsigned num_templates;
18488
18489   /* We haven't seen any classes that involve template parameters yet.  */
18490   num_templates = 0;
18491
18492   switch (declarator->kind)
18493     {
18494     case cdk_id:
18495       if (declarator->u.id.qualifying_scope)
18496         {
18497           tree scope;
18498
18499           scope = declarator->u.id.qualifying_scope;
18500
18501           while (scope && CLASS_TYPE_P (scope))
18502             {
18503               /* You're supposed to have one `template <...>'
18504                  for every template class, but you don't need one
18505                  for a full specialization.  For example:
18506
18507                  template <class T> struct S{};
18508                  template <> struct S<int> { void f(); };
18509                  void S<int>::f () {}
18510
18511                  is correct; there shouldn't be a `template <>' for
18512                  the definition of `S<int>::f'.  */
18513               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18514                 /* If SCOPE does not have template information of any
18515                    kind, then it is not a template, nor is it nested
18516                    within a template.  */
18517                 break;
18518               if (explicit_class_specialization_p (scope))
18519                 break;
18520               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18521                 ++num_templates;
18522
18523               scope = TYPE_CONTEXT (scope);
18524             }
18525         }
18526       else if (TREE_CODE (declarator->u.id.unqualified_name)
18527                == TEMPLATE_ID_EXPR)
18528         /* If the DECLARATOR has the form `X<y>' then it uses one
18529            additional level of template parameters.  */
18530         ++num_templates;
18531
18532       return cp_parser_check_template_parameters 
18533         (parser, num_templates, declarator_location, declarator);
18534
18535
18536     case cdk_function:
18537     case cdk_array:
18538     case cdk_pointer:
18539     case cdk_reference:
18540     case cdk_ptrmem:
18541       return (cp_parser_check_declarator_template_parameters
18542               (parser, declarator->declarator, declarator_location));
18543
18544     case cdk_error:
18545       return true;
18546
18547     default:
18548       gcc_unreachable ();
18549     }
18550   return false;
18551 }
18552
18553 /* NUM_TEMPLATES were used in the current declaration.  If that is
18554    invalid, return FALSE and issue an error messages.  Otherwise,
18555    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18556    declarator and we can print more accurate diagnostics.  */
18557
18558 static bool
18559 cp_parser_check_template_parameters (cp_parser* parser,
18560                                      unsigned num_templates,
18561                                      location_t location,
18562                                      cp_declarator *declarator)
18563 {
18564   /* If there are the same number of template classes and parameter
18565      lists, that's OK.  */
18566   if (parser->num_template_parameter_lists == num_templates)
18567     return true;
18568   /* If there are more, but only one more, then we are referring to a
18569      member template.  That's OK too.  */
18570   if (parser->num_template_parameter_lists == num_templates + 1)
18571     return true;
18572   /* If there are more template classes than parameter lists, we have
18573      something like:
18574
18575        template <class T> void S<T>::R<T>::f ();  */
18576   if (parser->num_template_parameter_lists < num_templates)
18577     {
18578       if (declarator && !current_function_decl)
18579         error_at (location, "specializing member %<%T::%E%> "
18580                   "requires %<template<>%> syntax", 
18581                   declarator->u.id.qualifying_scope,
18582                   declarator->u.id.unqualified_name);
18583       else if (declarator)
18584         error_at (location, "invalid declaration of %<%T::%E%>",
18585                   declarator->u.id.qualifying_scope,
18586                   declarator->u.id.unqualified_name);
18587       else 
18588         error_at (location, "too few template-parameter-lists");
18589       return false;
18590     }
18591   /* Otherwise, there are too many template parameter lists.  We have
18592      something like:
18593
18594      template <class T> template <class U> void S::f();  */
18595   error_at (location, "too many template-parameter-lists");
18596   return false;
18597 }
18598
18599 /* Parse an optional `::' token indicating that the following name is
18600    from the global namespace.  If so, PARSER->SCOPE is set to the
18601    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18602    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18603    Returns the new value of PARSER->SCOPE, if the `::' token is
18604    present, and NULL_TREE otherwise.  */
18605
18606 static tree
18607 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18608 {
18609   cp_token *token;
18610
18611   /* Peek at the next token.  */
18612   token = cp_lexer_peek_token (parser->lexer);
18613   /* If we're looking at a `::' token then we're starting from the
18614      global namespace, not our current location.  */
18615   if (token->type == CPP_SCOPE)
18616     {
18617       /* Consume the `::' token.  */
18618       cp_lexer_consume_token (parser->lexer);
18619       /* Set the SCOPE so that we know where to start the lookup.  */
18620       parser->scope = global_namespace;
18621       parser->qualifying_scope = global_namespace;
18622       parser->object_scope = NULL_TREE;
18623
18624       return parser->scope;
18625     }
18626   else if (!current_scope_valid_p)
18627     {
18628       parser->scope = NULL_TREE;
18629       parser->qualifying_scope = NULL_TREE;
18630       parser->object_scope = NULL_TREE;
18631     }
18632
18633   return NULL_TREE;
18634 }
18635
18636 /* Returns TRUE if the upcoming token sequence is the start of a
18637    constructor declarator.  If FRIEND_P is true, the declarator is
18638    preceded by the `friend' specifier.  */
18639
18640 static bool
18641 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18642 {
18643   bool constructor_p;
18644   tree nested_name_specifier;
18645   cp_token *next_token;
18646
18647   /* The common case is that this is not a constructor declarator, so
18648      try to avoid doing lots of work if at all possible.  It's not
18649      valid declare a constructor at function scope.  */
18650   if (parser->in_function_body)
18651     return false;
18652   /* And only certain tokens can begin a constructor declarator.  */
18653   next_token = cp_lexer_peek_token (parser->lexer);
18654   if (next_token->type != CPP_NAME
18655       && next_token->type != CPP_SCOPE
18656       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18657       && next_token->type != CPP_TEMPLATE_ID)
18658     return false;
18659
18660   /* Parse tentatively; we are going to roll back all of the tokens
18661      consumed here.  */
18662   cp_parser_parse_tentatively (parser);
18663   /* Assume that we are looking at a constructor declarator.  */
18664   constructor_p = true;
18665
18666   /* Look for the optional `::' operator.  */
18667   cp_parser_global_scope_opt (parser,
18668                               /*current_scope_valid_p=*/false);
18669   /* Look for the nested-name-specifier.  */
18670   nested_name_specifier
18671     = (cp_parser_nested_name_specifier_opt (parser,
18672                                             /*typename_keyword_p=*/false,
18673                                             /*check_dependency_p=*/false,
18674                                             /*type_p=*/false,
18675                                             /*is_declaration=*/false));
18676   /* Outside of a class-specifier, there must be a
18677      nested-name-specifier.  */
18678   if (!nested_name_specifier &&
18679       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18680        || friend_p))
18681     constructor_p = false;
18682   else if (nested_name_specifier == error_mark_node)
18683     constructor_p = false;
18684
18685   /* If we have a class scope, this is easy; DR 147 says that S::S always
18686      names the constructor, and no other qualified name could.  */
18687   if (constructor_p && nested_name_specifier
18688       && TYPE_P (nested_name_specifier))
18689     {
18690       tree id = cp_parser_unqualified_id (parser,
18691                                           /*template_keyword_p=*/false,
18692                                           /*check_dependency_p=*/false,
18693                                           /*declarator_p=*/true,
18694                                           /*optional_p=*/false);
18695       if (is_overloaded_fn (id))
18696         id = DECL_NAME (get_first_fn (id));
18697       if (!constructor_name_p (id, nested_name_specifier))
18698         constructor_p = false;
18699     }
18700   /* If we still think that this might be a constructor-declarator,
18701      look for a class-name.  */
18702   else if (constructor_p)
18703     {
18704       /* If we have:
18705
18706            template <typename T> struct S {
18707              S();
18708            };
18709
18710          we must recognize that the nested `S' names a class.  */
18711       tree type_decl;
18712       type_decl = cp_parser_class_name (parser,
18713                                         /*typename_keyword_p=*/false,
18714                                         /*template_keyword_p=*/false,
18715                                         none_type,
18716                                         /*check_dependency_p=*/false,
18717                                         /*class_head_p=*/false,
18718                                         /*is_declaration=*/false);
18719       /* If there was no class-name, then this is not a constructor.  */
18720       constructor_p = !cp_parser_error_occurred (parser);
18721
18722       /* If we're still considering a constructor, we have to see a `(',
18723          to begin the parameter-declaration-clause, followed by either a
18724          `)', an `...', or a decl-specifier.  We need to check for a
18725          type-specifier to avoid being fooled into thinking that:
18726
18727            S (f) (int);
18728
18729          is a constructor.  (It is actually a function named `f' that
18730          takes one parameter (of type `int') and returns a value of type
18731          `S'.  */
18732       if (constructor_p
18733           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18734         constructor_p = false;
18735
18736       if (constructor_p
18737           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18738           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18739           /* A parameter declaration begins with a decl-specifier,
18740              which is either the "attribute" keyword, a storage class
18741              specifier, or (usually) a type-specifier.  */
18742           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18743         {
18744           tree type;
18745           tree pushed_scope = NULL_TREE;
18746           unsigned saved_num_template_parameter_lists;
18747
18748           /* Names appearing in the type-specifier should be looked up
18749              in the scope of the class.  */
18750           if (current_class_type)
18751             type = NULL_TREE;
18752           else
18753             {
18754               type = TREE_TYPE (type_decl);
18755               if (TREE_CODE (type) == TYPENAME_TYPE)
18756                 {
18757                   type = resolve_typename_type (type,
18758                                                 /*only_current_p=*/false);
18759                   if (TREE_CODE (type) == TYPENAME_TYPE)
18760                     {
18761                       cp_parser_abort_tentative_parse (parser);
18762                       return false;
18763                     }
18764                 }
18765               pushed_scope = push_scope (type);
18766             }
18767
18768           /* Inside the constructor parameter list, surrounding
18769              template-parameter-lists do not apply.  */
18770           saved_num_template_parameter_lists
18771             = parser->num_template_parameter_lists;
18772           parser->num_template_parameter_lists = 0;
18773
18774           /* Look for the type-specifier.  */
18775           cp_parser_type_specifier (parser,
18776                                     CP_PARSER_FLAGS_NONE,
18777                                     /*decl_specs=*/NULL,
18778                                     /*is_declarator=*/true,
18779                                     /*declares_class_or_enum=*/NULL,
18780                                     /*is_cv_qualifier=*/NULL);
18781
18782           parser->num_template_parameter_lists
18783             = saved_num_template_parameter_lists;
18784
18785           /* Leave the scope of the class.  */
18786           if (pushed_scope)
18787             pop_scope (pushed_scope);
18788
18789           constructor_p = !cp_parser_error_occurred (parser);
18790         }
18791     }
18792
18793   /* We did not really want to consume any tokens.  */
18794   cp_parser_abort_tentative_parse (parser);
18795
18796   return constructor_p;
18797 }
18798
18799 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18800    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18801    they must be performed once we are in the scope of the function.
18802
18803    Returns the function defined.  */
18804
18805 static tree
18806 cp_parser_function_definition_from_specifiers_and_declarator
18807   (cp_parser* parser,
18808    cp_decl_specifier_seq *decl_specifiers,
18809    tree attributes,
18810    const cp_declarator *declarator)
18811 {
18812   tree fn;
18813   bool success_p;
18814
18815   /* Begin the function-definition.  */
18816   success_p = start_function (decl_specifiers, declarator, attributes);
18817
18818   /* The things we're about to see are not directly qualified by any
18819      template headers we've seen thus far.  */
18820   reset_specialization ();
18821
18822   /* If there were names looked up in the decl-specifier-seq that we
18823      did not check, check them now.  We must wait until we are in the
18824      scope of the function to perform the checks, since the function
18825      might be a friend.  */
18826   perform_deferred_access_checks ();
18827
18828   if (!success_p)
18829     {
18830       /* Skip the entire function.  */
18831       cp_parser_skip_to_end_of_block_or_statement (parser);
18832       fn = error_mark_node;
18833     }
18834   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18835     {
18836       /* Seen already, skip it.  An error message has already been output.  */
18837       cp_parser_skip_to_end_of_block_or_statement (parser);
18838       fn = current_function_decl;
18839       current_function_decl = NULL_TREE;
18840       /* If this is a function from a class, pop the nested class.  */
18841       if (current_class_name)
18842         pop_nested_class ();
18843     }
18844   else
18845     fn = cp_parser_function_definition_after_declarator (parser,
18846                                                          /*inline_p=*/false);
18847
18848   return fn;
18849 }
18850
18851 /* Parse the part of a function-definition that follows the
18852    declarator.  INLINE_P is TRUE iff this function is an inline
18853    function defined within a class-specifier.
18854
18855    Returns the function defined.  */
18856
18857 static tree
18858 cp_parser_function_definition_after_declarator (cp_parser* parser,
18859                                                 bool inline_p)
18860 {
18861   tree fn;
18862   bool ctor_initializer_p = false;
18863   bool saved_in_unbraced_linkage_specification_p;
18864   bool saved_in_function_body;
18865   unsigned saved_num_template_parameter_lists;
18866   cp_token *token;
18867
18868   saved_in_function_body = parser->in_function_body;
18869   parser->in_function_body = true;
18870   /* If the next token is `return', then the code may be trying to
18871      make use of the "named return value" extension that G++ used to
18872      support.  */
18873   token = cp_lexer_peek_token (parser->lexer);
18874   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18875     {
18876       /* Consume the `return' keyword.  */
18877       cp_lexer_consume_token (parser->lexer);
18878       /* Look for the identifier that indicates what value is to be
18879          returned.  */
18880       cp_parser_identifier (parser);
18881       /* Issue an error message.  */
18882       error_at (token->location,
18883                 "named return values are no longer supported");
18884       /* Skip tokens until we reach the start of the function body.  */
18885       while (true)
18886         {
18887           cp_token *token = cp_lexer_peek_token (parser->lexer);
18888           if (token->type == CPP_OPEN_BRACE
18889               || token->type == CPP_EOF
18890               || token->type == CPP_PRAGMA_EOL)
18891             break;
18892           cp_lexer_consume_token (parser->lexer);
18893         }
18894     }
18895   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18896      anything declared inside `f'.  */
18897   saved_in_unbraced_linkage_specification_p
18898     = parser->in_unbraced_linkage_specification_p;
18899   parser->in_unbraced_linkage_specification_p = false;
18900   /* Inside the function, surrounding template-parameter-lists do not
18901      apply.  */
18902   saved_num_template_parameter_lists
18903     = parser->num_template_parameter_lists;
18904   parser->num_template_parameter_lists = 0;
18905
18906   start_lambda_scope (current_function_decl);
18907
18908   /* If the next token is `try', then we are looking at a
18909      function-try-block.  */
18910   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18911     ctor_initializer_p = cp_parser_function_try_block (parser);
18912   /* A function-try-block includes the function-body, so we only do
18913      this next part if we're not processing a function-try-block.  */
18914   else
18915     ctor_initializer_p
18916       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18917
18918   finish_lambda_scope ();
18919
18920   /* Finish the function.  */
18921   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18922                         (inline_p ? 2 : 0));
18923   /* Generate code for it, if necessary.  */
18924   expand_or_defer_fn (fn);
18925   /* Restore the saved values.  */
18926   parser->in_unbraced_linkage_specification_p
18927     = saved_in_unbraced_linkage_specification_p;
18928   parser->num_template_parameter_lists
18929     = saved_num_template_parameter_lists;
18930   parser->in_function_body = saved_in_function_body;
18931
18932   return fn;
18933 }
18934
18935 /* Parse a template-declaration, assuming that the `export' (and
18936    `extern') keywords, if present, has already been scanned.  MEMBER_P
18937    is as for cp_parser_template_declaration.  */
18938
18939 static void
18940 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18941 {
18942   tree decl = NULL_TREE;
18943   VEC (deferred_access_check,gc) *checks;
18944   tree parameter_list;
18945   bool friend_p = false;
18946   bool need_lang_pop;
18947   cp_token *token;
18948
18949   /* Look for the `template' keyword.  */
18950   token = cp_lexer_peek_token (parser->lexer);
18951   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
18952     return;
18953
18954   /* And the `<'.  */
18955   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18956     return;
18957   if (at_class_scope_p () && current_function_decl)
18958     {
18959       /* 14.5.2.2 [temp.mem]
18960
18961          A local class shall not have member templates.  */
18962       error_at (token->location,
18963                 "invalid declaration of member template in local class");
18964       cp_parser_skip_to_end_of_block_or_statement (parser);
18965       return;
18966     }
18967   /* [temp]
18968
18969      A template ... shall not have C linkage.  */
18970   if (current_lang_name == lang_name_c)
18971     {
18972       error_at (token->location, "template with C linkage");
18973       /* Give it C++ linkage to avoid confusing other parts of the
18974          front end.  */
18975       push_lang_context (lang_name_cplusplus);
18976       need_lang_pop = true;
18977     }
18978   else
18979     need_lang_pop = false;
18980
18981   /* We cannot perform access checks on the template parameter
18982      declarations until we know what is being declared, just as we
18983      cannot check the decl-specifier list.  */
18984   push_deferring_access_checks (dk_deferred);
18985
18986   /* If the next token is `>', then we have an invalid
18987      specialization.  Rather than complain about an invalid template
18988      parameter, issue an error message here.  */
18989   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18990     {
18991       cp_parser_error (parser, "invalid explicit specialization");
18992       begin_specialization ();
18993       parameter_list = NULL_TREE;
18994     }
18995   else
18996     /* Parse the template parameters.  */
18997     parameter_list = cp_parser_template_parameter_list (parser);
18998
18999   /* Get the deferred access checks from the parameter list.  These
19000      will be checked once we know what is being declared, as for a
19001      member template the checks must be performed in the scope of the
19002      class containing the member.  */
19003   checks = get_deferred_access_checks ();
19004
19005   /* Look for the `>'.  */
19006   cp_parser_skip_to_end_of_template_parameter_list (parser);
19007   /* We just processed one more parameter list.  */
19008   ++parser->num_template_parameter_lists;
19009   /* If the next token is `template', there are more template
19010      parameters.  */
19011   if (cp_lexer_next_token_is_keyword (parser->lexer,
19012                                       RID_TEMPLATE))
19013     cp_parser_template_declaration_after_export (parser, member_p);
19014   else
19015     {
19016       /* There are no access checks when parsing a template, as we do not
19017          know if a specialization will be a friend.  */
19018       push_deferring_access_checks (dk_no_check);
19019       token = cp_lexer_peek_token (parser->lexer);
19020       decl = cp_parser_single_declaration (parser,
19021                                            checks,
19022                                            member_p,
19023                                            /*explicit_specialization_p=*/false,
19024                                            &friend_p);
19025       pop_deferring_access_checks ();
19026
19027       /* If this is a member template declaration, let the front
19028          end know.  */
19029       if (member_p && !friend_p && decl)
19030         {
19031           if (TREE_CODE (decl) == TYPE_DECL)
19032             cp_parser_check_access_in_redeclaration (decl, token->location);
19033
19034           decl = finish_member_template_decl (decl);
19035         }
19036       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19037         make_friend_class (current_class_type, TREE_TYPE (decl),
19038                            /*complain=*/true);
19039     }
19040   /* We are done with the current parameter list.  */
19041   --parser->num_template_parameter_lists;
19042
19043   pop_deferring_access_checks ();
19044
19045   /* Finish up.  */
19046   finish_template_decl (parameter_list);
19047
19048   /* Register member declarations.  */
19049   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19050     finish_member_declaration (decl);
19051   /* For the erroneous case of a template with C linkage, we pushed an
19052      implicit C++ linkage scope; exit that scope now.  */
19053   if (need_lang_pop)
19054     pop_lang_context ();
19055   /* If DECL is a function template, we must return to parse it later.
19056      (Even though there is no definition, there might be default
19057      arguments that need handling.)  */
19058   if (member_p && decl
19059       && (TREE_CODE (decl) == FUNCTION_DECL
19060           || DECL_FUNCTION_TEMPLATE_P (decl)))
19061     TREE_VALUE (parser->unparsed_functions_queues)
19062       = tree_cons (NULL_TREE, decl,
19063                    TREE_VALUE (parser->unparsed_functions_queues));
19064 }
19065
19066 /* Perform the deferred access checks from a template-parameter-list.
19067    CHECKS is a TREE_LIST of access checks, as returned by
19068    get_deferred_access_checks.  */
19069
19070 static void
19071 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19072 {
19073   ++processing_template_parmlist;
19074   perform_access_checks (checks);
19075   --processing_template_parmlist;
19076 }
19077
19078 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19079    `function-definition' sequence.  MEMBER_P is true, this declaration
19080    appears in a class scope.
19081
19082    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
19083    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
19084
19085 static tree
19086 cp_parser_single_declaration (cp_parser* parser,
19087                               VEC (deferred_access_check,gc)* checks,
19088                               bool member_p,
19089                               bool explicit_specialization_p,
19090                               bool* friend_p)
19091 {
19092   int declares_class_or_enum;
19093   tree decl = NULL_TREE;
19094   cp_decl_specifier_seq decl_specifiers;
19095   bool function_definition_p = false;
19096   cp_token *decl_spec_token_start;
19097
19098   /* This function is only used when processing a template
19099      declaration.  */
19100   gcc_assert (innermost_scope_kind () == sk_template_parms
19101               || innermost_scope_kind () == sk_template_spec);
19102
19103   /* Defer access checks until we know what is being declared.  */
19104   push_deferring_access_checks (dk_deferred);
19105
19106   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19107      alternative.  */
19108   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19109   cp_parser_decl_specifier_seq (parser,
19110                                 CP_PARSER_FLAGS_OPTIONAL,
19111                                 &decl_specifiers,
19112                                 &declares_class_or_enum);
19113   if (friend_p)
19114     *friend_p = cp_parser_friend_p (&decl_specifiers);
19115
19116   /* There are no template typedefs.  */
19117   if (decl_specifiers.specs[(int) ds_typedef])
19118     {
19119       error_at (decl_spec_token_start->location,
19120                 "template declaration of %<typedef%>");
19121       decl = error_mark_node;
19122     }
19123
19124   /* Gather up the access checks that occurred the
19125      decl-specifier-seq.  */
19126   stop_deferring_access_checks ();
19127
19128   /* Check for the declaration of a template class.  */
19129   if (declares_class_or_enum)
19130     {
19131       if (cp_parser_declares_only_class_p (parser))
19132         {
19133           decl = shadow_tag (&decl_specifiers);
19134
19135           /* In this case:
19136
19137                struct C {
19138                  friend template <typename T> struct A<T>::B;
19139                };
19140
19141              A<T>::B will be represented by a TYPENAME_TYPE, and
19142              therefore not recognized by shadow_tag.  */
19143           if (friend_p && *friend_p
19144               && !decl
19145               && decl_specifiers.type
19146               && TYPE_P (decl_specifiers.type))
19147             decl = decl_specifiers.type;
19148
19149           if (decl && decl != error_mark_node)
19150             decl = TYPE_NAME (decl);
19151           else
19152             decl = error_mark_node;
19153
19154           /* Perform access checks for template parameters.  */
19155           cp_parser_perform_template_parameter_access_checks (checks);
19156         }
19157     }
19158
19159   /* Complain about missing 'typename' or other invalid type names.  */
19160   if (!decl_specifiers.any_type_specifiers_p)
19161     cp_parser_parse_and_diagnose_invalid_type_name (parser);
19162
19163   /* If it's not a template class, try for a template function.  If
19164      the next token is a `;', then this declaration does not declare
19165      anything.  But, if there were errors in the decl-specifiers, then
19166      the error might well have come from an attempted class-specifier.
19167      In that case, there's no need to warn about a missing declarator.  */
19168   if (!decl
19169       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19170           || decl_specifiers.type != error_mark_node))
19171     {
19172       decl = cp_parser_init_declarator (parser,
19173                                         &decl_specifiers,
19174                                         checks,
19175                                         /*function_definition_allowed_p=*/true,
19176                                         member_p,
19177                                         declares_class_or_enum,
19178                                         &function_definition_p);
19179
19180     /* 7.1.1-1 [dcl.stc]
19181
19182        A storage-class-specifier shall not be specified in an explicit
19183        specialization...  */
19184     if (decl
19185         && explicit_specialization_p
19186         && decl_specifiers.storage_class != sc_none)
19187       {
19188         error_at (decl_spec_token_start->location,
19189                   "explicit template specialization cannot have a storage class");
19190         decl = error_mark_node;
19191       }
19192     }
19193
19194   pop_deferring_access_checks ();
19195
19196   /* Clear any current qualification; whatever comes next is the start
19197      of something new.  */
19198   parser->scope = NULL_TREE;
19199   parser->qualifying_scope = NULL_TREE;
19200   parser->object_scope = NULL_TREE;
19201   /* Look for a trailing `;' after the declaration.  */
19202   if (!function_definition_p
19203       && (decl == error_mark_node
19204           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19205     cp_parser_skip_to_end_of_block_or_statement (parser);
19206
19207   return decl;
19208 }
19209
19210 /* Parse a cast-expression that is not the operand of a unary "&".  */
19211
19212 static tree
19213 cp_parser_simple_cast_expression (cp_parser *parser)
19214 {
19215   return cp_parser_cast_expression (parser, /*address_p=*/false,
19216                                     /*cast_p=*/false, NULL);
19217 }
19218
19219 /* Parse a functional cast to TYPE.  Returns an expression
19220    representing the cast.  */
19221
19222 static tree
19223 cp_parser_functional_cast (cp_parser* parser, tree type)
19224 {
19225   VEC(tree,gc) *vec;
19226   tree expression_list;
19227   tree cast;
19228   bool nonconst_p;
19229
19230   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19231     {
19232       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19233       expression_list = cp_parser_braced_list (parser, &nonconst_p);
19234       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19235       if (TREE_CODE (type) == TYPE_DECL)
19236         type = TREE_TYPE (type);
19237       return finish_compound_literal (type, expression_list);
19238     }
19239
19240
19241   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19242                                                  /*cast_p=*/true,
19243                                                  /*allow_expansion_p=*/true,
19244                                                  /*non_constant_p=*/NULL);
19245   if (vec == NULL)
19246     expression_list = error_mark_node;
19247   else
19248     {
19249       expression_list = build_tree_list_vec (vec);
19250       release_tree_vector (vec);
19251     }
19252
19253   cast = build_functional_cast (type, expression_list,
19254                                 tf_warning_or_error);
19255   /* [expr.const]/1: In an integral constant expression "only type
19256      conversions to integral or enumeration type can be used".  */
19257   if (TREE_CODE (type) == TYPE_DECL)
19258     type = TREE_TYPE (type);
19259   if (cast != error_mark_node
19260       && !cast_valid_in_integral_constant_expression_p (type)
19261       && (cp_parser_non_integral_constant_expression (parser,
19262                                                       NIC_CONSTRUCTOR)))
19263     return error_mark_node;
19264   return cast;
19265 }
19266
19267 /* Save the tokens that make up the body of a member function defined
19268    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
19269    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
19270    specifiers applied to the declaration.  Returns the FUNCTION_DECL
19271    for the member function.  */
19272
19273 static tree
19274 cp_parser_save_member_function_body (cp_parser* parser,
19275                                      cp_decl_specifier_seq *decl_specifiers,
19276                                      cp_declarator *declarator,
19277                                      tree attributes)
19278 {
19279   cp_token *first;
19280   cp_token *last;
19281   tree fn;
19282
19283   /* Create the FUNCTION_DECL.  */
19284   fn = grokmethod (decl_specifiers, declarator, attributes);
19285   /* If something went badly wrong, bail out now.  */
19286   if (fn == error_mark_node)
19287     {
19288       /* If there's a function-body, skip it.  */
19289       if (cp_parser_token_starts_function_definition_p
19290           (cp_lexer_peek_token (parser->lexer)))
19291         cp_parser_skip_to_end_of_block_or_statement (parser);
19292       return error_mark_node;
19293     }
19294
19295   /* Remember it, if there default args to post process.  */
19296   cp_parser_save_default_args (parser, fn);
19297
19298   /* Save away the tokens that make up the body of the
19299      function.  */
19300   first = parser->lexer->next_token;
19301   /* We can have braced-init-list mem-initializers before the fn body.  */
19302   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19303     {
19304       cp_lexer_consume_token (parser->lexer);
19305       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19306              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19307         {
19308           /* cache_group will stop after an un-nested { } pair, too.  */
19309           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19310             break;
19311
19312           /* variadic mem-inits have ... after the ')'.  */
19313           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19314             cp_lexer_consume_token (parser->lexer);
19315         }
19316     }
19317   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19318   /* Handle function try blocks.  */
19319   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19320     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19321   last = parser->lexer->next_token;
19322
19323   /* Save away the inline definition; we will process it when the
19324      class is complete.  */
19325   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19326   DECL_PENDING_INLINE_P (fn) = 1;
19327
19328   /* We need to know that this was defined in the class, so that
19329      friend templates are handled correctly.  */
19330   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19331
19332   /* Add FN to the queue of functions to be parsed later.  */
19333   TREE_VALUE (parser->unparsed_functions_queues)
19334     = tree_cons (NULL_TREE, fn,
19335                  TREE_VALUE (parser->unparsed_functions_queues));
19336
19337   return fn;
19338 }
19339
19340 /* Parse a template-argument-list, as well as the trailing ">" (but
19341    not the opening ">").  See cp_parser_template_argument_list for the
19342    return value.  */
19343
19344 static tree
19345 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19346 {
19347   tree arguments;
19348   tree saved_scope;
19349   tree saved_qualifying_scope;
19350   tree saved_object_scope;
19351   bool saved_greater_than_is_operator_p;
19352   int saved_unevaluated_operand;
19353   int saved_inhibit_evaluation_warnings;
19354
19355   /* [temp.names]
19356
19357      When parsing a template-id, the first non-nested `>' is taken as
19358      the end of the template-argument-list rather than a greater-than
19359      operator.  */
19360   saved_greater_than_is_operator_p
19361     = parser->greater_than_is_operator_p;
19362   parser->greater_than_is_operator_p = false;
19363   /* Parsing the argument list may modify SCOPE, so we save it
19364      here.  */
19365   saved_scope = parser->scope;
19366   saved_qualifying_scope = parser->qualifying_scope;
19367   saved_object_scope = parser->object_scope;
19368   /* We need to evaluate the template arguments, even though this
19369      template-id may be nested within a "sizeof".  */
19370   saved_unevaluated_operand = cp_unevaluated_operand;
19371   cp_unevaluated_operand = 0;
19372   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19373   c_inhibit_evaluation_warnings = 0;
19374   /* Parse the template-argument-list itself.  */
19375   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19376       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19377     arguments = NULL_TREE;
19378   else
19379     arguments = cp_parser_template_argument_list (parser);
19380   /* Look for the `>' that ends the template-argument-list. If we find
19381      a '>>' instead, it's probably just a typo.  */
19382   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19383     {
19384       if (cxx_dialect != cxx98)
19385         {
19386           /* In C++0x, a `>>' in a template argument list or cast
19387              expression is considered to be two separate `>'
19388              tokens. So, change the current token to a `>', but don't
19389              consume it: it will be consumed later when the outer
19390              template argument list (or cast expression) is parsed.
19391              Note that this replacement of `>' for `>>' is necessary
19392              even if we are parsing tentatively: in the tentative
19393              case, after calling
19394              cp_parser_enclosed_template_argument_list we will always
19395              throw away all of the template arguments and the first
19396              closing `>', either because the template argument list
19397              was erroneous or because we are replacing those tokens
19398              with a CPP_TEMPLATE_ID token.  The second `>' (which will
19399              not have been thrown away) is needed either to close an
19400              outer template argument list or to complete a new-style
19401              cast.  */
19402           cp_token *token = cp_lexer_peek_token (parser->lexer);
19403           token->type = CPP_GREATER;
19404         }
19405       else if (!saved_greater_than_is_operator_p)
19406         {
19407           /* If we're in a nested template argument list, the '>>' has
19408             to be a typo for '> >'. We emit the error message, but we
19409             continue parsing and we push a '>' as next token, so that
19410             the argument list will be parsed correctly.  Note that the
19411             global source location is still on the token before the
19412             '>>', so we need to say explicitly where we want it.  */
19413           cp_token *token = cp_lexer_peek_token (parser->lexer);
19414           error_at (token->location, "%<>>%> should be %<> >%> "
19415                     "within a nested template argument list");
19416
19417           token->type = CPP_GREATER;
19418         }
19419       else
19420         {
19421           /* If this is not a nested template argument list, the '>>'
19422             is a typo for '>'. Emit an error message and continue.
19423             Same deal about the token location, but here we can get it
19424             right by consuming the '>>' before issuing the diagnostic.  */
19425           cp_token *token = cp_lexer_consume_token (parser->lexer);
19426           error_at (token->location,
19427                     "spurious %<>>%>, use %<>%> to terminate "
19428                     "a template argument list");
19429         }
19430     }
19431   else
19432     cp_parser_skip_to_end_of_template_parameter_list (parser);
19433   /* The `>' token might be a greater-than operator again now.  */
19434   parser->greater_than_is_operator_p
19435     = saved_greater_than_is_operator_p;
19436   /* Restore the SAVED_SCOPE.  */
19437   parser->scope = saved_scope;
19438   parser->qualifying_scope = saved_qualifying_scope;
19439   parser->object_scope = saved_object_scope;
19440   cp_unevaluated_operand = saved_unevaluated_operand;
19441   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19442
19443   return arguments;
19444 }
19445
19446 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19447    arguments, or the body of the function have not yet been parsed,
19448    parse them now.  */
19449
19450 static void
19451 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19452 {
19453   /* If this member is a template, get the underlying
19454      FUNCTION_DECL.  */
19455   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19456     member_function = DECL_TEMPLATE_RESULT (member_function);
19457
19458   /* There should not be any class definitions in progress at this
19459      point; the bodies of members are only parsed outside of all class
19460      definitions.  */
19461   gcc_assert (parser->num_classes_being_defined == 0);
19462   /* While we're parsing the member functions we might encounter more
19463      classes.  We want to handle them right away, but we don't want
19464      them getting mixed up with functions that are currently in the
19465      queue.  */
19466   parser->unparsed_functions_queues
19467     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19468
19469   /* Make sure that any template parameters are in scope.  */
19470   maybe_begin_member_template_processing (member_function);
19471
19472   /* If the body of the function has not yet been parsed, parse it
19473      now.  */
19474   if (DECL_PENDING_INLINE_P (member_function))
19475     {
19476       tree function_scope;
19477       cp_token_cache *tokens;
19478
19479       /* The function is no longer pending; we are processing it.  */
19480       tokens = DECL_PENDING_INLINE_INFO (member_function);
19481       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19482       DECL_PENDING_INLINE_P (member_function) = 0;
19483
19484       /* If this is a local class, enter the scope of the containing
19485          function.  */
19486       function_scope = current_function_decl;
19487       if (function_scope)
19488         push_function_context ();
19489
19490       /* Push the body of the function onto the lexer stack.  */
19491       cp_parser_push_lexer_for_tokens (parser, tokens);
19492
19493       /* Let the front end know that we going to be defining this
19494          function.  */
19495       start_preparsed_function (member_function, NULL_TREE,
19496                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19497
19498       /* Don't do access checking if it is a templated function.  */
19499       if (processing_template_decl)
19500         push_deferring_access_checks (dk_no_check);
19501
19502       /* Now, parse the body of the function.  */
19503       cp_parser_function_definition_after_declarator (parser,
19504                                                       /*inline_p=*/true);
19505
19506       if (processing_template_decl)
19507         pop_deferring_access_checks ();
19508
19509       /* Leave the scope of the containing function.  */
19510       if (function_scope)
19511         pop_function_context ();
19512       cp_parser_pop_lexer (parser);
19513     }
19514
19515   /* Remove any template parameters from the symbol table.  */
19516   maybe_end_member_template_processing ();
19517
19518   /* Restore the queue.  */
19519   parser->unparsed_functions_queues
19520     = TREE_CHAIN (parser->unparsed_functions_queues);
19521 }
19522
19523 /* If DECL contains any default args, remember it on the unparsed
19524    functions queue.  */
19525
19526 static void
19527 cp_parser_save_default_args (cp_parser* parser, tree decl)
19528 {
19529   tree probe;
19530
19531   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19532        probe;
19533        probe = TREE_CHAIN (probe))
19534     if (TREE_PURPOSE (probe))
19535       {
19536         TREE_PURPOSE (parser->unparsed_functions_queues)
19537           = tree_cons (current_class_type, decl,
19538                        TREE_PURPOSE (parser->unparsed_functions_queues));
19539         break;
19540       }
19541 }
19542
19543 /* FN is a FUNCTION_DECL which may contains a parameter with an
19544    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19545    assumes that the current scope is the scope in which the default
19546    argument should be processed.  */
19547
19548 static void
19549 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19550 {
19551   bool saved_local_variables_forbidden_p;
19552   tree parm, parmdecl;
19553
19554   /* While we're parsing the default args, we might (due to the
19555      statement expression extension) encounter more classes.  We want
19556      to handle them right away, but we don't want them getting mixed
19557      up with default args that are currently in the queue.  */
19558   parser->unparsed_functions_queues
19559     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19560
19561   /* Local variable names (and the `this' keyword) may not appear
19562      in a default argument.  */
19563   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19564   parser->local_variables_forbidden_p = true;
19565
19566   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19567          parmdecl = DECL_ARGUMENTS (fn);
19568        parm && parm != void_list_node;
19569        parm = TREE_CHAIN (parm),
19570          parmdecl = TREE_CHAIN (parmdecl))
19571     {
19572       cp_token_cache *tokens;
19573       tree default_arg = TREE_PURPOSE (parm);
19574       tree parsed_arg;
19575       VEC(tree,gc) *insts;
19576       tree copy;
19577       unsigned ix;
19578
19579       if (!default_arg)
19580         continue;
19581
19582       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19583         /* This can happen for a friend declaration for a function
19584            already declared with default arguments.  */
19585         continue;
19586
19587        /* Push the saved tokens for the default argument onto the parser's
19588           lexer stack.  */
19589       tokens = DEFARG_TOKENS (default_arg);
19590       cp_parser_push_lexer_for_tokens (parser, tokens);
19591
19592       start_lambda_scope (parmdecl);
19593
19594       /* Parse the assignment-expression.  */
19595       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19596       if (parsed_arg == error_mark_node)
19597         {
19598           cp_parser_pop_lexer (parser);
19599           continue;
19600         }
19601
19602       if (!processing_template_decl)
19603         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19604
19605       TREE_PURPOSE (parm) = parsed_arg;
19606
19607       /* Update any instantiations we've already created.  */
19608       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19609            VEC_iterate (tree, insts, ix, copy); ix++)
19610         TREE_PURPOSE (copy) = parsed_arg;
19611
19612       finish_lambda_scope ();
19613
19614       /* If the token stream has not been completely used up, then
19615          there was extra junk after the end of the default
19616          argument.  */
19617       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19618         cp_parser_error (parser, "expected %<,%>");
19619
19620       /* Revert to the main lexer.  */
19621       cp_parser_pop_lexer (parser);
19622     }
19623
19624   /* Make sure no default arg is missing.  */
19625   check_default_args (fn);
19626
19627   /* Restore the state of local_variables_forbidden_p.  */
19628   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19629
19630   /* Restore the queue.  */
19631   parser->unparsed_functions_queues
19632     = TREE_CHAIN (parser->unparsed_functions_queues);
19633 }
19634
19635 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19636    either a TYPE or an expression, depending on the form of the
19637    input.  The KEYWORD indicates which kind of expression we have
19638    encountered.  */
19639
19640 static tree
19641 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19642 {
19643   tree expr = NULL_TREE;
19644   const char *saved_message;
19645   char *tmp;
19646   bool saved_integral_constant_expression_p;
19647   bool saved_non_integral_constant_expression_p;
19648   bool pack_expansion_p = false;
19649
19650   /* Types cannot be defined in a `sizeof' expression.  Save away the
19651      old message.  */
19652   saved_message = parser->type_definition_forbidden_message;
19653   /* And create the new one.  */
19654   tmp = concat ("types may not be defined in %<",
19655                 IDENTIFIER_POINTER (ridpointers[keyword]),
19656                 "%> expressions", NULL);
19657   parser->type_definition_forbidden_message = tmp;
19658
19659   /* The restrictions on constant-expressions do not apply inside
19660      sizeof expressions.  */
19661   saved_integral_constant_expression_p
19662     = parser->integral_constant_expression_p;
19663   saved_non_integral_constant_expression_p
19664     = parser->non_integral_constant_expression_p;
19665   parser->integral_constant_expression_p = false;
19666
19667   /* If it's a `...', then we are computing the length of a parameter
19668      pack.  */
19669   if (keyword == RID_SIZEOF
19670       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19671     {
19672       /* Consume the `...'.  */
19673       cp_lexer_consume_token (parser->lexer);
19674       maybe_warn_variadic_templates ();
19675
19676       /* Note that this is an expansion.  */
19677       pack_expansion_p = true;
19678     }
19679
19680   /* Do not actually evaluate the expression.  */
19681   ++cp_unevaluated_operand;
19682   ++c_inhibit_evaluation_warnings;
19683   /* If it's a `(', then we might be looking at the type-id
19684      construction.  */
19685   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19686     {
19687       tree type;
19688       bool saved_in_type_id_in_expr_p;
19689
19690       /* We can't be sure yet whether we're looking at a type-id or an
19691          expression.  */
19692       cp_parser_parse_tentatively (parser);
19693       /* Consume the `('.  */
19694       cp_lexer_consume_token (parser->lexer);
19695       /* Parse the type-id.  */
19696       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19697       parser->in_type_id_in_expr_p = true;
19698       type = cp_parser_type_id (parser);
19699       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19700       /* Now, look for the trailing `)'.  */
19701       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19702       /* If all went well, then we're done.  */
19703       if (cp_parser_parse_definitely (parser))
19704         {
19705           cp_decl_specifier_seq decl_specs;
19706
19707           /* Build a trivial decl-specifier-seq.  */
19708           clear_decl_specs (&decl_specs);
19709           decl_specs.type = type;
19710
19711           /* Call grokdeclarator to figure out what type this is.  */
19712           expr = grokdeclarator (NULL,
19713                                  &decl_specs,
19714                                  TYPENAME,
19715                                  /*initialized=*/0,
19716                                  /*attrlist=*/NULL);
19717         }
19718     }
19719
19720   /* If the type-id production did not work out, then we must be
19721      looking at the unary-expression production.  */
19722   if (!expr)
19723     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19724                                        /*cast_p=*/false, NULL);
19725
19726   if (pack_expansion_p)
19727     /* Build a pack expansion. */
19728     expr = make_pack_expansion (expr);
19729
19730   /* Go back to evaluating expressions.  */
19731   --cp_unevaluated_operand;
19732   --c_inhibit_evaluation_warnings;
19733
19734   /* Free the message we created.  */
19735   free (tmp);
19736   /* And restore the old one.  */
19737   parser->type_definition_forbidden_message = saved_message;
19738   parser->integral_constant_expression_p
19739     = saved_integral_constant_expression_p;
19740   parser->non_integral_constant_expression_p
19741     = saved_non_integral_constant_expression_p;
19742
19743   return expr;
19744 }
19745
19746 /* If the current declaration has no declarator, return true.  */
19747
19748 static bool
19749 cp_parser_declares_only_class_p (cp_parser *parser)
19750 {
19751   /* If the next token is a `;' or a `,' then there is no
19752      declarator.  */
19753   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19754           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19755 }
19756
19757 /* Update the DECL_SPECS to reflect the storage class indicated by
19758    KEYWORD.  */
19759
19760 static void
19761 cp_parser_set_storage_class (cp_parser *parser,
19762                              cp_decl_specifier_seq *decl_specs,
19763                              enum rid keyword,
19764                              location_t location)
19765 {
19766   cp_storage_class storage_class;
19767
19768   if (parser->in_unbraced_linkage_specification_p)
19769     {
19770       error_at (location, "invalid use of %qD in linkage specification",
19771                 ridpointers[keyword]);
19772       return;
19773     }
19774   else if (decl_specs->storage_class != sc_none)
19775     {
19776       decl_specs->conflicting_specifiers_p = true;
19777       return;
19778     }
19779
19780   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19781       && decl_specs->specs[(int) ds_thread])
19782     {
19783       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19784       decl_specs->specs[(int) ds_thread] = 0;
19785     }
19786
19787   switch (keyword)
19788     {
19789     case RID_AUTO:
19790       storage_class = sc_auto;
19791       break;
19792     case RID_REGISTER:
19793       storage_class = sc_register;
19794       break;
19795     case RID_STATIC:
19796       storage_class = sc_static;
19797       break;
19798     case RID_EXTERN:
19799       storage_class = sc_extern;
19800       break;
19801     case RID_MUTABLE:
19802       storage_class = sc_mutable;
19803       break;
19804     default:
19805       gcc_unreachable ();
19806     }
19807   decl_specs->storage_class = storage_class;
19808
19809   /* A storage class specifier cannot be applied alongside a typedef 
19810      specifier. If there is a typedef specifier present then set 
19811      conflicting_specifiers_p which will trigger an error later
19812      on in grokdeclarator. */
19813   if (decl_specs->specs[(int)ds_typedef])
19814     decl_specs->conflicting_specifiers_p = true;
19815 }
19816
19817 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19818    is true, the type is a user-defined type; otherwise it is a
19819    built-in type specified by a keyword.  */
19820
19821 static void
19822 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19823                               tree type_spec,
19824                               location_t location,
19825                               bool user_defined_p)
19826 {
19827   decl_specs->any_specifiers_p = true;
19828
19829   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19830      (with, for example, in "typedef int wchar_t;") we remember that
19831      this is what happened.  In system headers, we ignore these
19832      declarations so that G++ can work with system headers that are not
19833      C++-safe.  */
19834   if (decl_specs->specs[(int) ds_typedef]
19835       && !user_defined_p
19836       && (type_spec == boolean_type_node
19837           || type_spec == char16_type_node
19838           || type_spec == char32_type_node
19839           || type_spec == wchar_type_node)
19840       && (decl_specs->type
19841           || decl_specs->specs[(int) ds_long]
19842           || decl_specs->specs[(int) ds_short]
19843           || decl_specs->specs[(int) ds_unsigned]
19844           || decl_specs->specs[(int) ds_signed]))
19845     {
19846       decl_specs->redefined_builtin_type = type_spec;
19847       if (!decl_specs->type)
19848         {
19849           decl_specs->type = type_spec;
19850           decl_specs->user_defined_type_p = false;
19851           decl_specs->type_location = location;
19852         }
19853     }
19854   else if (decl_specs->type)
19855     decl_specs->multiple_types_p = true;
19856   else
19857     {
19858       decl_specs->type = type_spec;
19859       decl_specs->user_defined_type_p = user_defined_p;
19860       decl_specs->redefined_builtin_type = NULL_TREE;
19861       decl_specs->type_location = location;
19862     }
19863 }
19864
19865 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19866    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19867
19868 static bool
19869 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19870 {
19871   return decl_specifiers->specs[(int) ds_friend] != 0;
19872 }
19873
19874 /* Issue an error message indicating that TOKEN_DESC was expected.
19875    If KEYWORD is true, it indicated this function is called by
19876    cp_parser_require_keword and the required token can only be
19877    a indicated keyword. */
19878
19879 static void
19880 cp_parser_required_error (cp_parser *parser,
19881                           required_token token_desc,
19882                           bool keyword)
19883 {
19884   switch (token_desc)
19885     {
19886       case RT_NEW:
19887         cp_parser_error (parser, "expected %<new%>");
19888         return;
19889       case RT_DELETE:
19890         cp_parser_error (parser, "expected %<delete%>");
19891         return;
19892       case RT_RETURN:
19893         cp_parser_error (parser, "expected %<return%>");
19894         return;
19895       case RT_WHILE:
19896         cp_parser_error (parser, "expected %<while%>");
19897         return;
19898       case RT_EXTERN:
19899         cp_parser_error (parser, "expected %<extern%>");
19900         return;
19901       case RT_STATIC_ASSERT:
19902         cp_parser_error (parser, "expected %<static_assert%>");
19903         return;
19904       case RT_DECLTYPE:
19905         cp_parser_error (parser, "expected %<decltype%>");
19906         return;
19907       case RT_OPERATOR:
19908         cp_parser_error (parser, "expected %<operator%>");
19909         return;
19910       case RT_CLASS:
19911         cp_parser_error (parser, "expected %<class%>");
19912         return;
19913       case RT_TEMPLATE:
19914         cp_parser_error (parser, "expected %<template%>");
19915         return;
19916       case RT_NAMESPACE:
19917         cp_parser_error (parser, "expected %<namespace%>");
19918         return;
19919       case RT_USING:
19920         cp_parser_error (parser, "expected %<using%>");
19921         return;
19922       case RT_ASM:
19923         cp_parser_error (parser, "expected %<asm%>");
19924         return;
19925       case RT_TRY:
19926         cp_parser_error (parser, "expected %<try%>");
19927         return;
19928       case RT_CATCH:
19929         cp_parser_error (parser, "expected %<catch%>");
19930         return;
19931       case RT_THROW:
19932         cp_parser_error (parser, "expected %<throw%>");
19933         return;
19934       case RT_LABEL:
19935         cp_parser_error (parser, "expected %<__label__%>");
19936         return;
19937       case RT_AT_TRY:
19938         cp_parser_error (parser, "expected %<@try%>");
19939         return;
19940       case RT_AT_SYNCHRONIZED:
19941         cp_parser_error (parser, "expected %<@synchronized%>");
19942         return;
19943       case RT_AT_THROW:
19944         cp_parser_error (parser, "expected %<@throw%>");
19945         return;
19946       default:
19947         break;
19948     }
19949   if (!keyword)
19950     {
19951       switch (token_desc)
19952         {
19953           case RT_SEMICOLON:
19954             cp_parser_error (parser, "expected %<;%>");
19955             return;
19956           case RT_OPEN_PAREN:
19957             cp_parser_error (parser, "expected %<(%>");
19958             return;
19959           case RT_CLOSE_BRACE:
19960             cp_parser_error (parser, "expected %<}%>");
19961             return;
19962           case RT_OPEN_BRACE:
19963             cp_parser_error (parser, "expected %<{%>");
19964             return;
19965           case RT_CLOSE_SQUARE:
19966             cp_parser_error (parser, "expected %<]%>");
19967             return;
19968           case RT_OPEN_SQUARE:
19969             cp_parser_error (parser, "expected %<[%>");
19970             return;
19971           case RT_COMMA:
19972             cp_parser_error (parser, "expected %<,%>");
19973             return;
19974           case RT_SCOPE:
19975             cp_parser_error (parser, "expected %<::%>");
19976             return;
19977           case RT_LESS:
19978             cp_parser_error (parser, "expected %<<%>");
19979             return;
19980           case RT_GREATER:
19981             cp_parser_error (parser, "expected %<>%>");
19982             return;
19983           case RT_EQ:
19984             cp_parser_error (parser, "expected %<=%>");
19985             return;
19986           case RT_ELLIPSIS:
19987             cp_parser_error (parser, "expected %<...%>");
19988             return;
19989           case RT_MULT:
19990             cp_parser_error (parser, "expected %<*%>");
19991             return;
19992           case RT_COMPL:
19993             cp_parser_error (parser, "expected %<~%>");
19994             return;
19995           case RT_COLON:
19996             cp_parser_error (parser, "expected %<:%>");
19997             return;
19998           case RT_COLON_SCOPE:
19999             cp_parser_error (parser, "expected %<:%> or %<::%>");
20000             return;
20001           case RT_CLOSE_PAREN:
20002             cp_parser_error (parser, "expected %<)%>");
20003             return;
20004           case RT_COMMA_CLOSE_PAREN:
20005             cp_parser_error (parser, "expected %<,%> or %<)%>");
20006             return;
20007           case RT_PRAGMA_EOL:
20008             cp_parser_error (parser, "expected end of line");
20009             return;
20010           case RT_NAME:
20011             cp_parser_error (parser, "expected identifier");
20012             return;
20013           case RT_SELECT:
20014             cp_parser_error (parser, "expected selection-statement");
20015             return;
20016           case RT_INTERATION:
20017             cp_parser_error (parser, "expected iteration-statement");
20018             return;
20019           case RT_JUMP:
20020             cp_parser_error (parser, "expected jump-statement");
20021             return;
20022           case RT_CLASS_KEY:
20023             cp_parser_error (parser, "expected class-key");
20024             return;
20025           case RT_CLASS_TYPENAME_TEMPLATE:
20026             cp_parser_error (parser,
20027                  "expected %<class%>, %<typename%>, or %<template%>");
20028             return;
20029           default:
20030             gcc_unreachable ();
20031         }
20032     }
20033   else
20034     gcc_unreachable ();
20035 }
20036
20037
20038
20039 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20040    issue an error message indicating that TOKEN_DESC was expected.
20041
20042    Returns the token consumed, if the token had the appropriate type.
20043    Otherwise, returns NULL.  */
20044
20045 static cp_token *
20046 cp_parser_require (cp_parser* parser,
20047                    enum cpp_ttype type,
20048                    required_token token_desc)
20049 {
20050   if (cp_lexer_next_token_is (parser->lexer, type))
20051     return cp_lexer_consume_token (parser->lexer);
20052   else
20053     {
20054       /* Output the MESSAGE -- unless we're parsing tentatively.  */
20055       if (!cp_parser_simulate_error (parser))
20056         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20057       return NULL;
20058     }
20059 }
20060
20061 /* An error message is produced if the next token is not '>'.
20062    All further tokens are skipped until the desired token is
20063    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
20064
20065 static void
20066 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20067 {
20068   /* Current level of '< ... >'.  */
20069   unsigned level = 0;
20070   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
20071   unsigned nesting_depth = 0;
20072
20073   /* Are we ready, yet?  If not, issue error message.  */
20074   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20075     return;
20076
20077   /* Skip tokens until the desired token is found.  */
20078   while (true)
20079     {
20080       /* Peek at the next token.  */
20081       switch (cp_lexer_peek_token (parser->lexer)->type)
20082         {
20083         case CPP_LESS:
20084           if (!nesting_depth)
20085             ++level;
20086           break;
20087
20088         case CPP_RSHIFT:
20089           if (cxx_dialect == cxx98)
20090             /* C++0x views the `>>' operator as two `>' tokens, but
20091                C++98 does not. */
20092             break;
20093           else if (!nesting_depth && level-- == 0)
20094             {
20095               /* We've hit a `>>' where the first `>' closes the
20096                  template argument list, and the second `>' is
20097                  spurious.  Just consume the `>>' and stop; we've
20098                  already produced at least one error.  */
20099               cp_lexer_consume_token (parser->lexer);
20100               return;
20101             }
20102           /* Fall through for C++0x, so we handle the second `>' in
20103              the `>>'.  */
20104
20105         case CPP_GREATER:
20106           if (!nesting_depth && level-- == 0)
20107             {
20108               /* We've reached the token we want, consume it and stop.  */
20109               cp_lexer_consume_token (parser->lexer);
20110               return;
20111             }
20112           break;
20113
20114         case CPP_OPEN_PAREN:
20115         case CPP_OPEN_SQUARE:
20116           ++nesting_depth;
20117           break;
20118
20119         case CPP_CLOSE_PAREN:
20120         case CPP_CLOSE_SQUARE:
20121           if (nesting_depth-- == 0)
20122             return;
20123           break;
20124
20125         case CPP_EOF:
20126         case CPP_PRAGMA_EOL:
20127         case CPP_SEMICOLON:
20128         case CPP_OPEN_BRACE:
20129         case CPP_CLOSE_BRACE:
20130           /* The '>' was probably forgotten, don't look further.  */
20131           return;
20132
20133         default:
20134           break;
20135         }
20136
20137       /* Consume this token.  */
20138       cp_lexer_consume_token (parser->lexer);
20139     }
20140 }
20141
20142 /* If the next token is the indicated keyword, consume it.  Otherwise,
20143    issue an error message indicating that TOKEN_DESC was expected.
20144
20145    Returns the token consumed, if the token had the appropriate type.
20146    Otherwise, returns NULL.  */
20147
20148 static cp_token *
20149 cp_parser_require_keyword (cp_parser* parser,
20150                            enum rid keyword,
20151                            required_token token_desc)
20152 {
20153   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20154
20155   if (token && token->keyword != keyword)
20156     {
20157       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
20158       return NULL;
20159     }
20160
20161   return token;
20162 }
20163
20164 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20165    function-definition.  */
20166
20167 static bool
20168 cp_parser_token_starts_function_definition_p (cp_token* token)
20169 {
20170   return (/* An ordinary function-body begins with an `{'.  */
20171           token->type == CPP_OPEN_BRACE
20172           /* A ctor-initializer begins with a `:'.  */
20173           || token->type == CPP_COLON
20174           /* A function-try-block begins with `try'.  */
20175           || token->keyword == RID_TRY
20176           /* The named return value extension begins with `return'.  */
20177           || token->keyword == RID_RETURN);
20178 }
20179
20180 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20181    definition.  */
20182
20183 static bool
20184 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20185 {
20186   cp_token *token;
20187
20188   token = cp_lexer_peek_token (parser->lexer);
20189   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20190 }
20191
20192 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20193    C++0x) ending a template-argument.  */
20194
20195 static bool
20196 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20197 {
20198   cp_token *token;
20199
20200   token = cp_lexer_peek_token (parser->lexer);
20201   return (token->type == CPP_COMMA 
20202           || token->type == CPP_GREATER
20203           || token->type == CPP_ELLIPSIS
20204           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20205 }
20206
20207 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20208    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
20209
20210 static bool
20211 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20212                                                      size_t n)
20213 {
20214   cp_token *token;
20215
20216   token = cp_lexer_peek_nth_token (parser->lexer, n);
20217   if (token->type == CPP_LESS)
20218     return true;
20219   /* Check for the sequence `<::' in the original code. It would be lexed as
20220      `[:', where `[' is a digraph, and there is no whitespace before
20221      `:'.  */
20222   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20223     {
20224       cp_token *token2;
20225       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20226       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20227         return true;
20228     }
20229   return false;
20230 }
20231
20232 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20233    or none_type otherwise.  */
20234
20235 static enum tag_types
20236 cp_parser_token_is_class_key (cp_token* token)
20237 {
20238   switch (token->keyword)
20239     {
20240     case RID_CLASS:
20241       return class_type;
20242     case RID_STRUCT:
20243       return record_type;
20244     case RID_UNION:
20245       return union_type;
20246
20247     default:
20248       return none_type;
20249     }
20250 }
20251
20252 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
20253
20254 static void
20255 cp_parser_check_class_key (enum tag_types class_key, tree type)
20256 {
20257   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20258     permerror (input_location, "%qs tag used in naming %q#T",
20259             class_key == union_type ? "union"
20260              : class_key == record_type ? "struct" : "class",
20261              type);
20262 }
20263
20264 /* Issue an error message if DECL is redeclared with different
20265    access than its original declaration [class.access.spec/3].
20266    This applies to nested classes and nested class templates.
20267    [class.mem/1].  */
20268
20269 static void
20270 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20271 {
20272   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20273     return;
20274
20275   if ((TREE_PRIVATE (decl)
20276        != (current_access_specifier == access_private_node))
20277       || (TREE_PROTECTED (decl)
20278           != (current_access_specifier == access_protected_node)))
20279     error_at (location, "%qD redeclared with different access", decl);
20280 }
20281
20282 /* Look for the `template' keyword, as a syntactic disambiguator.
20283    Return TRUE iff it is present, in which case it will be
20284    consumed.  */
20285
20286 static bool
20287 cp_parser_optional_template_keyword (cp_parser *parser)
20288 {
20289   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20290     {
20291       /* The `template' keyword can only be used within templates;
20292          outside templates the parser can always figure out what is a
20293          template and what is not.  */
20294       if (!processing_template_decl)
20295         {
20296           cp_token *token = cp_lexer_peek_token (parser->lexer);
20297           error_at (token->location,
20298                     "%<template%> (as a disambiguator) is only allowed "
20299                     "within templates");
20300           /* If this part of the token stream is rescanned, the same
20301              error message would be generated.  So, we purge the token
20302              from the stream.  */
20303           cp_lexer_purge_token (parser->lexer);
20304           return false;
20305         }
20306       else
20307         {
20308           /* Consume the `template' keyword.  */
20309           cp_lexer_consume_token (parser->lexer);
20310           return true;
20311         }
20312     }
20313
20314   return false;
20315 }
20316
20317 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
20318    set PARSER->SCOPE, and perform other related actions.  */
20319
20320 static void
20321 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20322 {
20323   int i;
20324   struct tree_check *check_value;
20325   deferred_access_check *chk;
20326   VEC (deferred_access_check,gc) *checks;
20327
20328   /* Get the stored value.  */
20329   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
20330   /* Perform any access checks that were deferred.  */
20331   checks = check_value->checks;
20332   if (checks)
20333     {
20334       for (i = 0 ;
20335            VEC_iterate (deferred_access_check, checks, i, chk) ;
20336            ++i)
20337         {
20338           perform_or_defer_access_check (chk->binfo,
20339                                          chk->decl,
20340                                          chk->diag_decl);
20341         }
20342     }
20343   /* Set the scope from the stored value.  */
20344   parser->scope = check_value->value;
20345   parser->qualifying_scope = check_value->qualifying_scope;
20346   parser->object_scope = NULL_TREE;
20347 }
20348
20349 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
20350    encounter the end of a block before what we were looking for.  */
20351
20352 static bool
20353 cp_parser_cache_group (cp_parser *parser,
20354                        enum cpp_ttype end,
20355                        unsigned depth)
20356 {
20357   while (true)
20358     {
20359       cp_token *token = cp_lexer_peek_token (parser->lexer);
20360
20361       /* Abort a parenthesized expression if we encounter a semicolon.  */
20362       if ((end == CPP_CLOSE_PAREN || depth == 0)
20363           && token->type == CPP_SEMICOLON)
20364         return true;
20365       /* If we've reached the end of the file, stop.  */
20366       if (token->type == CPP_EOF
20367           || (end != CPP_PRAGMA_EOL
20368               && token->type == CPP_PRAGMA_EOL))
20369         return true;
20370       if (token->type == CPP_CLOSE_BRACE && depth == 0)
20371         /* We've hit the end of an enclosing block, so there's been some
20372            kind of syntax error.  */
20373         return true;
20374
20375       /* Consume the token.  */
20376       cp_lexer_consume_token (parser->lexer);
20377       /* See if it starts a new group.  */
20378       if (token->type == CPP_OPEN_BRACE)
20379         {
20380           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
20381           /* In theory this should probably check end == '}', but
20382              cp_parser_save_member_function_body needs it to exit
20383              after either '}' or ')' when called with ')'.  */
20384           if (depth == 0)
20385             return false;
20386         }
20387       else if (token->type == CPP_OPEN_PAREN)
20388         {
20389           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
20390           if (depth == 0 && end == CPP_CLOSE_PAREN)
20391             return false;
20392         }
20393       else if (token->type == CPP_PRAGMA)
20394         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
20395       else if (token->type == end)
20396         return false;
20397     }
20398 }
20399
20400 /* Begin parsing tentatively.  We always save tokens while parsing
20401    tentatively so that if the tentative parsing fails we can restore the
20402    tokens.  */
20403
20404 static void
20405 cp_parser_parse_tentatively (cp_parser* parser)
20406 {
20407   /* Enter a new parsing context.  */
20408   parser->context = cp_parser_context_new (parser->context);
20409   /* Begin saving tokens.  */
20410   cp_lexer_save_tokens (parser->lexer);
20411   /* In order to avoid repetitive access control error messages,
20412      access checks are queued up until we are no longer parsing
20413      tentatively.  */
20414   push_deferring_access_checks (dk_deferred);
20415 }
20416
20417 /* Commit to the currently active tentative parse.  */
20418
20419 static void
20420 cp_parser_commit_to_tentative_parse (cp_parser* parser)
20421 {
20422   cp_parser_context *context;
20423   cp_lexer *lexer;
20424
20425   /* Mark all of the levels as committed.  */
20426   lexer = parser->lexer;
20427   for (context = parser->context; context->next; context = context->next)
20428     {
20429       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20430         break;
20431       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20432       while (!cp_lexer_saving_tokens (lexer))
20433         lexer = lexer->next;
20434       cp_lexer_commit_tokens (lexer);
20435     }
20436 }
20437
20438 /* Abort the currently active tentative parse.  All consumed tokens
20439    will be rolled back, and no diagnostics will be issued.  */
20440
20441 static void
20442 cp_parser_abort_tentative_parse (cp_parser* parser)
20443 {
20444   cp_parser_simulate_error (parser);
20445   /* Now, pretend that we want to see if the construct was
20446      successfully parsed.  */
20447   cp_parser_parse_definitely (parser);
20448 }
20449
20450 /* Stop parsing tentatively.  If a parse error has occurred, restore the
20451    token stream.  Otherwise, commit to the tokens we have consumed.
20452    Returns true if no error occurred; false otherwise.  */
20453
20454 static bool
20455 cp_parser_parse_definitely (cp_parser* parser)
20456 {
20457   bool error_occurred;
20458   cp_parser_context *context;
20459
20460   /* Remember whether or not an error occurred, since we are about to
20461      destroy that information.  */
20462   error_occurred = cp_parser_error_occurred (parser);
20463   /* Remove the topmost context from the stack.  */
20464   context = parser->context;
20465   parser->context = context->next;
20466   /* If no parse errors occurred, commit to the tentative parse.  */
20467   if (!error_occurred)
20468     {
20469       /* Commit to the tokens read tentatively, unless that was
20470          already done.  */
20471       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20472         cp_lexer_commit_tokens (parser->lexer);
20473
20474       pop_to_parent_deferring_access_checks ();
20475     }
20476   /* Otherwise, if errors occurred, roll back our state so that things
20477      are just as they were before we began the tentative parse.  */
20478   else
20479     {
20480       cp_lexer_rollback_tokens (parser->lexer);
20481       pop_deferring_access_checks ();
20482     }
20483   /* Add the context to the front of the free list.  */
20484   context->next = cp_parser_context_free_list;
20485   cp_parser_context_free_list = context;
20486
20487   return !error_occurred;
20488 }
20489
20490 /* Returns true if we are parsing tentatively and are not committed to
20491    this tentative parse.  */
20492
20493 static bool
20494 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20495 {
20496   return (cp_parser_parsing_tentatively (parser)
20497           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20498 }
20499
20500 /* Returns nonzero iff an error has occurred during the most recent
20501    tentative parse.  */
20502
20503 static bool
20504 cp_parser_error_occurred (cp_parser* parser)
20505 {
20506   return (cp_parser_parsing_tentatively (parser)
20507           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20508 }
20509
20510 /* Returns nonzero if GNU extensions are allowed.  */
20511
20512 static bool
20513 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20514 {
20515   return parser->allow_gnu_extensions_p;
20516 }
20517 \f
20518 /* Objective-C++ Productions */
20519
20520
20521 /* Parse an Objective-C expression, which feeds into a primary-expression
20522    above.
20523
20524    objc-expression:
20525      objc-message-expression
20526      objc-string-literal
20527      objc-encode-expression
20528      objc-protocol-expression
20529      objc-selector-expression
20530
20531   Returns a tree representation of the expression.  */
20532
20533 static tree
20534 cp_parser_objc_expression (cp_parser* parser)
20535 {
20536   /* Try to figure out what kind of declaration is present.  */
20537   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20538
20539   switch (kwd->type)
20540     {
20541     case CPP_OPEN_SQUARE:
20542       return cp_parser_objc_message_expression (parser);
20543
20544     case CPP_OBJC_STRING:
20545       kwd = cp_lexer_consume_token (parser->lexer);
20546       return objc_build_string_object (kwd->u.value);
20547
20548     case CPP_KEYWORD:
20549       switch (kwd->keyword)
20550         {
20551         case RID_AT_ENCODE:
20552           return cp_parser_objc_encode_expression (parser);
20553
20554         case RID_AT_PROTOCOL:
20555           return cp_parser_objc_protocol_expression (parser);
20556
20557         case RID_AT_SELECTOR:
20558           return cp_parser_objc_selector_expression (parser);
20559
20560         default:
20561           break;
20562         }
20563     default:
20564       error_at (kwd->location,
20565                 "misplaced %<@%D%> Objective-C++ construct",
20566                 kwd->u.value);
20567       cp_parser_skip_to_end_of_block_or_statement (parser);
20568     }
20569
20570   return error_mark_node;
20571 }
20572
20573 /* Parse an Objective-C message expression.
20574
20575    objc-message-expression:
20576      [ objc-message-receiver objc-message-args ]
20577
20578    Returns a representation of an Objective-C message.  */
20579
20580 static tree
20581 cp_parser_objc_message_expression (cp_parser* parser)
20582 {
20583   tree receiver, messageargs;
20584
20585   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20586   receiver = cp_parser_objc_message_receiver (parser);
20587   messageargs = cp_parser_objc_message_args (parser);
20588   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20589
20590   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20591 }
20592
20593 /* Parse an objc-message-receiver.
20594
20595    objc-message-receiver:
20596      expression
20597      simple-type-specifier
20598
20599   Returns a representation of the type or expression.  */
20600
20601 static tree
20602 cp_parser_objc_message_receiver (cp_parser* parser)
20603 {
20604   tree rcv;
20605
20606   /* An Objective-C message receiver may be either (1) a type
20607      or (2) an expression.  */
20608   cp_parser_parse_tentatively (parser);
20609   rcv = cp_parser_expression (parser, false, NULL);
20610
20611   if (cp_parser_parse_definitely (parser))
20612     return rcv;
20613
20614   rcv = cp_parser_simple_type_specifier (parser,
20615                                          /*decl_specs=*/NULL,
20616                                          CP_PARSER_FLAGS_NONE);
20617
20618   return objc_get_class_reference (rcv);
20619 }
20620
20621 /* Parse the arguments and selectors comprising an Objective-C message.
20622
20623    objc-message-args:
20624      objc-selector
20625      objc-selector-args
20626      objc-selector-args , objc-comma-args
20627
20628    objc-selector-args:
20629      objc-selector [opt] : assignment-expression
20630      objc-selector-args objc-selector [opt] : assignment-expression
20631
20632    objc-comma-args:
20633      assignment-expression
20634      objc-comma-args , assignment-expression
20635
20636    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20637    selector arguments and TREE_VALUE containing a list of comma
20638    arguments.  */
20639
20640 static tree
20641 cp_parser_objc_message_args (cp_parser* parser)
20642 {
20643   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20644   bool maybe_unary_selector_p = true;
20645   cp_token *token = cp_lexer_peek_token (parser->lexer);
20646
20647   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20648     {
20649       tree selector = NULL_TREE, arg;
20650
20651       if (token->type != CPP_COLON)
20652         selector = cp_parser_objc_selector (parser);
20653
20654       /* Detect if we have a unary selector.  */
20655       if (maybe_unary_selector_p
20656           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20657         return build_tree_list (selector, NULL_TREE);
20658
20659       maybe_unary_selector_p = false;
20660       cp_parser_require (parser, CPP_COLON, RT_COLON);
20661       arg = cp_parser_assignment_expression (parser, false, NULL);
20662
20663       sel_args
20664         = chainon (sel_args,
20665                    build_tree_list (selector, arg));
20666
20667       token = cp_lexer_peek_token (parser->lexer);
20668     }
20669
20670   /* Handle non-selector arguments, if any. */
20671   while (token->type == CPP_COMMA)
20672     {
20673       tree arg;
20674
20675       cp_lexer_consume_token (parser->lexer);
20676       arg = cp_parser_assignment_expression (parser, false, NULL);
20677
20678       addl_args
20679         = chainon (addl_args,
20680                    build_tree_list (NULL_TREE, arg));
20681
20682       token = cp_lexer_peek_token (parser->lexer);
20683     }
20684
20685   return build_tree_list (sel_args, addl_args);
20686 }
20687
20688 /* Parse an Objective-C encode expression.
20689
20690    objc-encode-expression:
20691      @encode objc-typename
20692
20693    Returns an encoded representation of the type argument.  */
20694
20695 static tree
20696 cp_parser_objc_encode_expression (cp_parser* parser)
20697 {
20698   tree type;
20699   cp_token *token;
20700
20701   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20702   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20703   token = cp_lexer_peek_token (parser->lexer);
20704   type = complete_type (cp_parser_type_id (parser));
20705   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20706
20707   if (!type)
20708     {
20709       error_at (token->location, 
20710                 "%<@encode%> must specify a type as an argument");
20711       return error_mark_node;
20712     }
20713
20714   return objc_build_encode_expr (type);
20715 }
20716
20717 /* Parse an Objective-C @defs expression.  */
20718
20719 static tree
20720 cp_parser_objc_defs_expression (cp_parser *parser)
20721 {
20722   tree name;
20723
20724   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20725   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20726   name = cp_parser_identifier (parser);
20727   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20728
20729   return objc_get_class_ivars (name);
20730 }
20731
20732 /* Parse an Objective-C protocol expression.
20733
20734   objc-protocol-expression:
20735     @protocol ( identifier )
20736
20737   Returns a representation of the protocol expression.  */
20738
20739 static tree
20740 cp_parser_objc_protocol_expression (cp_parser* parser)
20741 {
20742   tree proto;
20743
20744   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20745   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20746   proto = cp_parser_identifier (parser);
20747   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20748
20749   return objc_build_protocol_expr (proto);
20750 }
20751
20752 /* Parse an Objective-C selector expression.
20753
20754    objc-selector-expression:
20755      @selector ( objc-method-signature )
20756
20757    objc-method-signature:
20758      objc-selector
20759      objc-selector-seq
20760
20761    objc-selector-seq:
20762      objc-selector :
20763      objc-selector-seq objc-selector :
20764
20765   Returns a representation of the method selector.  */
20766
20767 static tree
20768 cp_parser_objc_selector_expression (cp_parser* parser)
20769 {
20770   tree sel_seq = NULL_TREE;
20771   bool maybe_unary_selector_p = true;
20772   cp_token *token;
20773   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20774
20775   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20776   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20777   token = cp_lexer_peek_token (parser->lexer);
20778
20779   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20780          || token->type == CPP_SCOPE)
20781     {
20782       tree selector = NULL_TREE;
20783
20784       if (token->type != CPP_COLON
20785           || token->type == CPP_SCOPE)
20786         selector = cp_parser_objc_selector (parser);
20787
20788       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20789           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20790         {
20791           /* Detect if we have a unary selector.  */
20792           if (maybe_unary_selector_p)
20793             {
20794               sel_seq = selector;
20795               goto finish_selector;
20796             }
20797           else
20798             {
20799               cp_parser_error (parser, "expected %<:%>");
20800             }
20801         }
20802       maybe_unary_selector_p = false;
20803       token = cp_lexer_consume_token (parser->lexer);
20804
20805       if (token->type == CPP_SCOPE)
20806         {
20807           sel_seq
20808             = chainon (sel_seq,
20809                        build_tree_list (selector, NULL_TREE));
20810           sel_seq
20811             = chainon (sel_seq,
20812                        build_tree_list (NULL_TREE, NULL_TREE));
20813         }
20814       else
20815         sel_seq
20816           = chainon (sel_seq,
20817                      build_tree_list (selector, NULL_TREE));
20818
20819       token = cp_lexer_peek_token (parser->lexer);
20820     }
20821
20822  finish_selector:
20823   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20824
20825   return objc_build_selector_expr (loc, sel_seq);
20826 }
20827
20828 /* Parse a list of identifiers.
20829
20830    objc-identifier-list:
20831      identifier
20832      objc-identifier-list , identifier
20833
20834    Returns a TREE_LIST of identifier nodes.  */
20835
20836 static tree
20837 cp_parser_objc_identifier_list (cp_parser* parser)
20838 {
20839   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20840   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20841
20842   while (sep->type == CPP_COMMA)
20843     {
20844       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20845       list = chainon (list,
20846                       build_tree_list (NULL_TREE,
20847                                        cp_parser_identifier (parser)));
20848       sep = cp_lexer_peek_token (parser->lexer);
20849     }
20850
20851   return list;
20852 }
20853
20854 /* Parse an Objective-C alias declaration.
20855
20856    objc-alias-declaration:
20857      @compatibility_alias identifier identifier ;
20858
20859    This function registers the alias mapping with the Objective-C front end.
20860    It returns nothing.  */
20861
20862 static void
20863 cp_parser_objc_alias_declaration (cp_parser* parser)
20864 {
20865   tree alias, orig;
20866
20867   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20868   alias = cp_parser_identifier (parser);
20869   orig = cp_parser_identifier (parser);
20870   objc_declare_alias (alias, orig);
20871   cp_parser_consume_semicolon_at_end_of_statement (parser);
20872 }
20873
20874 /* Parse an Objective-C class forward-declaration.
20875
20876    objc-class-declaration:
20877      @class objc-identifier-list ;
20878
20879    The function registers the forward declarations with the Objective-C
20880    front end.  It returns nothing.  */
20881
20882 static void
20883 cp_parser_objc_class_declaration (cp_parser* parser)
20884 {
20885   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20886   objc_declare_class (cp_parser_objc_identifier_list (parser));
20887   cp_parser_consume_semicolon_at_end_of_statement (parser);
20888 }
20889
20890 /* Parse a list of Objective-C protocol references.
20891
20892    objc-protocol-refs-opt:
20893      objc-protocol-refs [opt]
20894
20895    objc-protocol-refs:
20896      < objc-identifier-list >
20897
20898    Returns a TREE_LIST of identifiers, if any.  */
20899
20900 static tree
20901 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20902 {
20903   tree protorefs = NULL_TREE;
20904
20905   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20906     {
20907       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20908       protorefs = cp_parser_objc_identifier_list (parser);
20909       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
20910     }
20911
20912   return protorefs;
20913 }
20914
20915 /* Parse a Objective-C visibility specification.  */
20916
20917 static void
20918 cp_parser_objc_visibility_spec (cp_parser* parser)
20919 {
20920   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20921
20922   switch (vis->keyword)
20923     {
20924     case RID_AT_PRIVATE:
20925       objc_set_visibility (2);
20926       break;
20927     case RID_AT_PROTECTED:
20928       objc_set_visibility (0);
20929       break;
20930     case RID_AT_PUBLIC:
20931       objc_set_visibility (1);
20932       break;
20933     default:
20934       return;
20935     }
20936
20937   /* Eat '@private'/'@protected'/'@public'.  */
20938   cp_lexer_consume_token (parser->lexer);
20939 }
20940
20941 /* Parse an Objective-C method type.  */
20942
20943 static void
20944 cp_parser_objc_method_type (cp_parser* parser)
20945 {
20946   objc_set_method_type
20947    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20948     ? PLUS_EXPR
20949     : MINUS_EXPR);
20950 }
20951
20952 /* Parse an Objective-C protocol qualifier.  */
20953
20954 static tree
20955 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20956 {
20957   tree quals = NULL_TREE, node;
20958   cp_token *token = cp_lexer_peek_token (parser->lexer);
20959
20960   node = token->u.value;
20961
20962   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20963          && (node == ridpointers [(int) RID_IN]
20964              || node == ridpointers [(int) RID_OUT]
20965              || node == ridpointers [(int) RID_INOUT]
20966              || node == ridpointers [(int) RID_BYCOPY]
20967              || node == ridpointers [(int) RID_BYREF]
20968              || node == ridpointers [(int) RID_ONEWAY]))
20969     {
20970       quals = tree_cons (NULL_TREE, node, quals);
20971       cp_lexer_consume_token (parser->lexer);
20972       token = cp_lexer_peek_token (parser->lexer);
20973       node = token->u.value;
20974     }
20975
20976   return quals;
20977 }
20978
20979 /* Parse an Objective-C typename.  */
20980
20981 static tree
20982 cp_parser_objc_typename (cp_parser* parser)
20983 {
20984   tree type_name = NULL_TREE;
20985
20986   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20987     {
20988       tree proto_quals, cp_type = NULL_TREE;
20989
20990       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20991       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20992
20993       /* An ObjC type name may consist of just protocol qualifiers, in which
20994          case the type shall default to 'id'.  */
20995       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20996         cp_type = cp_parser_type_id (parser);
20997
20998       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20999       type_name = build_tree_list (proto_quals, cp_type);
21000     }
21001
21002   return type_name;
21003 }
21004
21005 /* Check to see if TYPE refers to an Objective-C selector name.  */
21006
21007 static bool
21008 cp_parser_objc_selector_p (enum cpp_ttype type)
21009 {
21010   return (type == CPP_NAME || type == CPP_KEYWORD
21011           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21012           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21013           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21014           || type == CPP_XOR || type == CPP_XOR_EQ);
21015 }
21016
21017 /* Parse an Objective-C selector.  */
21018
21019 static tree
21020 cp_parser_objc_selector (cp_parser* parser)
21021 {
21022   cp_token *token = cp_lexer_consume_token (parser->lexer);
21023
21024   if (!cp_parser_objc_selector_p (token->type))
21025     {
21026       error_at (token->location, "invalid Objective-C++ selector name");
21027       return error_mark_node;
21028     }
21029
21030   /* C++ operator names are allowed to appear in ObjC selectors.  */
21031   switch (token->type)
21032     {
21033     case CPP_AND_AND: return get_identifier ("and");
21034     case CPP_AND_EQ: return get_identifier ("and_eq");
21035     case CPP_AND: return get_identifier ("bitand");
21036     case CPP_OR: return get_identifier ("bitor");
21037     case CPP_COMPL: return get_identifier ("compl");
21038     case CPP_NOT: return get_identifier ("not");
21039     case CPP_NOT_EQ: return get_identifier ("not_eq");
21040     case CPP_OR_OR: return get_identifier ("or");
21041     case CPP_OR_EQ: return get_identifier ("or_eq");
21042     case CPP_XOR: return get_identifier ("xor");
21043     case CPP_XOR_EQ: return get_identifier ("xor_eq");
21044     default: return token->u.value;
21045     }
21046 }
21047
21048 /* Parse an Objective-C params list.  */
21049
21050 static tree
21051 cp_parser_objc_method_keyword_params (cp_parser* parser)
21052 {
21053   tree params = NULL_TREE;
21054   bool maybe_unary_selector_p = true;
21055   cp_token *token = cp_lexer_peek_token (parser->lexer);
21056
21057   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21058     {
21059       tree selector = NULL_TREE, type_name, identifier;
21060
21061       if (token->type != CPP_COLON)
21062         selector = cp_parser_objc_selector (parser);
21063
21064       /* Detect if we have a unary selector.  */
21065       if (maybe_unary_selector_p
21066           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21067         return selector;
21068
21069       maybe_unary_selector_p = false;
21070       cp_parser_require (parser, CPP_COLON, RT_COLON);
21071       type_name = cp_parser_objc_typename (parser);
21072       identifier = cp_parser_identifier (parser);
21073
21074       params
21075         = chainon (params,
21076                    objc_build_keyword_decl (selector,
21077                                             type_name,
21078                                             identifier));
21079
21080       token = cp_lexer_peek_token (parser->lexer);
21081     }
21082
21083   return params;
21084 }
21085
21086 /* Parse the non-keyword Objective-C params.  */
21087
21088 static tree
21089 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
21090 {
21091   tree params = make_node (TREE_LIST);
21092   cp_token *token = cp_lexer_peek_token (parser->lexer);
21093   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
21094
21095   while (token->type == CPP_COMMA)
21096     {
21097       cp_parameter_declarator *parmdecl;
21098       tree parm;
21099
21100       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21101       token = cp_lexer_peek_token (parser->lexer);
21102
21103       if (token->type == CPP_ELLIPSIS)
21104         {
21105           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
21106           *ellipsisp = true;
21107           break;
21108         }
21109
21110       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21111       parm = grokdeclarator (parmdecl->declarator,
21112                              &parmdecl->decl_specifiers,
21113                              PARM, /*initialized=*/0,
21114                              /*attrlist=*/NULL);
21115
21116       chainon (params, build_tree_list (NULL_TREE, parm));
21117       token = cp_lexer_peek_token (parser->lexer);
21118     }
21119
21120   return params;
21121 }
21122
21123 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
21124
21125 static void
21126 cp_parser_objc_interstitial_code (cp_parser* parser)
21127 {
21128   cp_token *token = cp_lexer_peek_token (parser->lexer);
21129
21130   /* If the next token is `extern' and the following token is a string
21131      literal, then we have a linkage specification.  */
21132   if (token->keyword == RID_EXTERN
21133       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21134     cp_parser_linkage_specification (parser);
21135   /* Handle #pragma, if any.  */
21136   else if (token->type == CPP_PRAGMA)
21137     cp_parser_pragma (parser, pragma_external);
21138   /* Allow stray semicolons.  */
21139   else if (token->type == CPP_SEMICOLON)
21140     cp_lexer_consume_token (parser->lexer);
21141   /* Finally, try to parse a block-declaration, or a function-definition.  */
21142   else
21143     cp_parser_block_declaration (parser, /*statement_p=*/false);
21144 }
21145
21146 /* Parse a method signature.  */
21147
21148 static tree
21149 cp_parser_objc_method_signature (cp_parser* parser)
21150 {
21151   tree rettype, kwdparms, optparms;
21152   bool ellipsis = false;
21153
21154   cp_parser_objc_method_type (parser);
21155   rettype = cp_parser_objc_typename (parser);
21156   kwdparms = cp_parser_objc_method_keyword_params (parser);
21157   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
21158
21159   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
21160 }
21161
21162 /* Pars an Objective-C method prototype list.  */
21163
21164 static void
21165 cp_parser_objc_method_prototype_list (cp_parser* parser)
21166 {
21167   cp_token *token = cp_lexer_peek_token (parser->lexer);
21168
21169   while (token->keyword != RID_AT_END)
21170     {
21171       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21172         {
21173           objc_add_method_declaration
21174            (cp_parser_objc_method_signature (parser));
21175           cp_parser_consume_semicolon_at_end_of_statement (parser);
21176         }
21177       else
21178         /* Allow for interspersed non-ObjC++ code.  */
21179         cp_parser_objc_interstitial_code (parser);
21180
21181       token = cp_lexer_peek_token (parser->lexer);
21182     }
21183
21184   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21185   objc_finish_interface ();
21186 }
21187
21188 /* Parse an Objective-C method definition list.  */
21189
21190 static void
21191 cp_parser_objc_method_definition_list (cp_parser* parser)
21192 {
21193   cp_token *token = cp_lexer_peek_token (parser->lexer);
21194
21195   while (token->keyword != RID_AT_END)
21196     {
21197       tree meth;
21198
21199       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21200         {
21201           push_deferring_access_checks (dk_deferred);
21202           objc_start_method_definition
21203            (cp_parser_objc_method_signature (parser));
21204
21205           /* For historical reasons, we accept an optional semicolon.  */
21206           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21207             cp_lexer_consume_token (parser->lexer);
21208
21209           perform_deferred_access_checks ();
21210           stop_deferring_access_checks ();
21211           meth = cp_parser_function_definition_after_declarator (parser,
21212                                                                  false);
21213           pop_deferring_access_checks ();
21214           objc_finish_method_definition (meth);
21215         }
21216       else
21217         /* Allow for interspersed non-ObjC++ code.  */
21218         cp_parser_objc_interstitial_code (parser);
21219
21220       token = cp_lexer_peek_token (parser->lexer);
21221     }
21222
21223   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21224   objc_finish_implementation ();
21225 }
21226
21227 /* Parse Objective-C ivars.  */
21228
21229 static void
21230 cp_parser_objc_class_ivars (cp_parser* parser)
21231 {
21232   cp_token *token = cp_lexer_peek_token (parser->lexer);
21233
21234   if (token->type != CPP_OPEN_BRACE)
21235     return;     /* No ivars specified.  */
21236
21237   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
21238   token = cp_lexer_peek_token (parser->lexer);
21239
21240   while (token->type != CPP_CLOSE_BRACE)
21241     {
21242       cp_decl_specifier_seq declspecs;
21243       int decl_class_or_enum_p;
21244       tree prefix_attributes;
21245
21246       cp_parser_objc_visibility_spec (parser);
21247
21248       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21249         break;
21250
21251       cp_parser_decl_specifier_seq (parser,
21252                                     CP_PARSER_FLAGS_OPTIONAL,
21253                                     &declspecs,
21254                                     &decl_class_or_enum_p);
21255       prefix_attributes = declspecs.attributes;
21256       declspecs.attributes = NULL_TREE;
21257
21258       /* Keep going until we hit the `;' at the end of the
21259          declaration.  */
21260       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21261         {
21262           tree width = NULL_TREE, attributes, first_attribute, decl;
21263           cp_declarator *declarator = NULL;
21264           int ctor_dtor_or_conv_p;
21265
21266           /* Check for a (possibly unnamed) bitfield declaration.  */
21267           token = cp_lexer_peek_token (parser->lexer);
21268           if (token->type == CPP_COLON)
21269             goto eat_colon;
21270
21271           if (token->type == CPP_NAME
21272               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21273                   == CPP_COLON))
21274             {
21275               /* Get the name of the bitfield.  */
21276               declarator = make_id_declarator (NULL_TREE,
21277                                                cp_parser_identifier (parser),
21278                                                sfk_none);
21279
21280              eat_colon:
21281               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
21282               /* Get the width of the bitfield.  */
21283               width
21284                 = cp_parser_constant_expression (parser,
21285                                                  /*allow_non_constant=*/false,
21286                                                  NULL);
21287             }
21288           else
21289             {
21290               /* Parse the declarator.  */
21291               declarator
21292                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21293                                         &ctor_dtor_or_conv_p,
21294                                         /*parenthesized_p=*/NULL,
21295                                         /*member_p=*/false);
21296             }
21297
21298           /* Look for attributes that apply to the ivar.  */
21299           attributes = cp_parser_attributes_opt (parser);
21300           /* Remember which attributes are prefix attributes and
21301              which are not.  */
21302           first_attribute = attributes;
21303           /* Combine the attributes.  */
21304           attributes = chainon (prefix_attributes, attributes);
21305
21306           if (width)
21307               /* Create the bitfield declaration.  */
21308               decl = grokbitfield (declarator, &declspecs,
21309                                    width,
21310                                    attributes);
21311           else
21312             decl = grokfield (declarator, &declspecs,
21313                               NULL_TREE, /*init_const_expr_p=*/false,
21314                               NULL_TREE, attributes);
21315
21316           /* Add the instance variable.  */
21317           objc_add_instance_variable (decl);
21318
21319           /* Reset PREFIX_ATTRIBUTES.  */
21320           while (attributes && TREE_CHAIN (attributes) != first_attribute)
21321             attributes = TREE_CHAIN (attributes);
21322           if (attributes)
21323             TREE_CHAIN (attributes) = NULL_TREE;
21324
21325           token = cp_lexer_peek_token (parser->lexer);
21326
21327           if (token->type == CPP_COMMA)
21328             {
21329               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21330               continue;
21331             }
21332           break;
21333         }
21334
21335       cp_parser_consume_semicolon_at_end_of_statement (parser);
21336       token = cp_lexer_peek_token (parser->lexer);
21337     }
21338
21339   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
21340   /* For historical reasons, we accept an optional semicolon.  */
21341   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21342     cp_lexer_consume_token (parser->lexer);
21343 }
21344
21345 /* Parse an Objective-C protocol declaration.  */
21346
21347 static void
21348 cp_parser_objc_protocol_declaration (cp_parser* parser)
21349 {
21350   tree proto, protorefs;
21351   cp_token *tok;
21352
21353   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21354   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
21355     {
21356       tok = cp_lexer_peek_token (parser->lexer);
21357       error_at (tok->location, "identifier expected after %<@protocol%>");
21358       goto finish;
21359     }
21360
21361   /* See if we have a forward declaration or a definition.  */
21362   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
21363
21364   /* Try a forward declaration first.  */
21365   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
21366     {
21367       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
21368      finish:
21369       cp_parser_consume_semicolon_at_end_of_statement (parser);
21370     }
21371
21372   /* Ok, we got a full-fledged definition (or at least should).  */
21373   else
21374     {
21375       proto = cp_parser_identifier (parser);
21376       protorefs = cp_parser_objc_protocol_refs_opt (parser);
21377       objc_start_protocol (proto, protorefs);
21378       cp_parser_objc_method_prototype_list (parser);
21379     }
21380 }
21381
21382 /* Parse an Objective-C superclass or category.  */
21383
21384 static void
21385 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
21386                                                           tree *categ)
21387 {
21388   cp_token *next = cp_lexer_peek_token (parser->lexer);
21389
21390   *super = *categ = NULL_TREE;
21391   if (next->type == CPP_COLON)
21392     {
21393       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
21394       *super = cp_parser_identifier (parser);
21395     }
21396   else if (next->type == CPP_OPEN_PAREN)
21397     {
21398       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21399       *categ = cp_parser_identifier (parser);
21400       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21401     }
21402 }
21403
21404 /* Parse an Objective-C class interface.  */
21405
21406 static void
21407 cp_parser_objc_class_interface (cp_parser* parser)
21408 {
21409   tree name, super, categ, protos;
21410
21411   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
21412   name = cp_parser_identifier (parser);
21413   cp_parser_objc_superclass_or_category (parser, &super, &categ);
21414   protos = cp_parser_objc_protocol_refs_opt (parser);
21415
21416   /* We have either a class or a category on our hands.  */
21417   if (categ)
21418     objc_start_category_interface (name, categ, protos);
21419   else
21420     {
21421       objc_start_class_interface (name, super, protos);
21422       /* Handle instance variable declarations, if any.  */
21423       cp_parser_objc_class_ivars (parser);
21424       objc_continue_interface ();
21425     }
21426
21427   cp_parser_objc_method_prototype_list (parser);
21428 }
21429
21430 /* Parse an Objective-C class implementation.  */
21431
21432 static void
21433 cp_parser_objc_class_implementation (cp_parser* parser)
21434 {
21435   tree name, super, categ;
21436
21437   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
21438   name = cp_parser_identifier (parser);
21439   cp_parser_objc_superclass_or_category (parser, &super, &categ);
21440
21441   /* We have either a class or a category on our hands.  */
21442   if (categ)
21443     objc_start_category_implementation (name, categ);
21444   else
21445     {
21446       objc_start_class_implementation (name, super);
21447       /* Handle instance variable declarations, if any.  */
21448       cp_parser_objc_class_ivars (parser);
21449       objc_continue_implementation ();
21450     }
21451
21452   cp_parser_objc_method_definition_list (parser);
21453 }
21454
21455 /* Consume the @end token and finish off the implementation.  */
21456
21457 static void
21458 cp_parser_objc_end_implementation (cp_parser* parser)
21459 {
21460   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21461   objc_finish_implementation ();
21462 }
21463
21464 /* Parse an Objective-C declaration.  */
21465
21466 static void
21467 cp_parser_objc_declaration (cp_parser* parser)
21468 {
21469   /* Try to figure out what kind of declaration is present.  */
21470   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21471
21472   switch (kwd->keyword)
21473     {
21474     case RID_AT_ALIAS:
21475       cp_parser_objc_alias_declaration (parser);
21476       break;
21477     case RID_AT_CLASS:
21478       cp_parser_objc_class_declaration (parser);
21479       break;
21480     case RID_AT_PROTOCOL:
21481       cp_parser_objc_protocol_declaration (parser);
21482       break;
21483     case RID_AT_INTERFACE:
21484       cp_parser_objc_class_interface (parser);
21485       break;
21486     case RID_AT_IMPLEMENTATION:
21487       cp_parser_objc_class_implementation (parser);
21488       break;
21489     case RID_AT_END:
21490       cp_parser_objc_end_implementation (parser);
21491       break;
21492     default:
21493       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21494                 kwd->u.value);
21495       cp_parser_skip_to_end_of_block_or_statement (parser);
21496     }
21497 }
21498
21499 /* Parse an Objective-C try-catch-finally statement.
21500
21501    objc-try-catch-finally-stmt:
21502      @try compound-statement objc-catch-clause-seq [opt]
21503        objc-finally-clause [opt]
21504
21505    objc-catch-clause-seq:
21506      objc-catch-clause objc-catch-clause-seq [opt]
21507
21508    objc-catch-clause:
21509      @catch ( exception-declaration ) compound-statement
21510
21511    objc-finally-clause
21512      @finally compound-statement
21513
21514    Returns NULL_TREE.  */
21515
21516 static tree
21517 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21518   location_t location;
21519   tree stmt;
21520
21521   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
21522   location = cp_lexer_peek_token (parser->lexer)->location;
21523   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21524      node, lest it get absorbed into the surrounding block.  */
21525   stmt = push_stmt_list ();
21526   cp_parser_compound_statement (parser, NULL, false);
21527   objc_begin_try_stmt (location, pop_stmt_list (stmt));
21528
21529   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21530     {
21531       cp_parameter_declarator *parmdecl;
21532       tree parm;
21533
21534       cp_lexer_consume_token (parser->lexer);
21535       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21536       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21537       parm = grokdeclarator (parmdecl->declarator,
21538                              &parmdecl->decl_specifiers,
21539                              PARM, /*initialized=*/0,
21540                              /*attrlist=*/NULL);
21541       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21542       objc_begin_catch_clause (parm);
21543       cp_parser_compound_statement (parser, NULL, false);
21544       objc_finish_catch_clause ();
21545     }
21546
21547   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21548     {
21549       cp_lexer_consume_token (parser->lexer);
21550       location = cp_lexer_peek_token (parser->lexer)->location;
21551       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21552          node, lest it get absorbed into the surrounding block.  */
21553       stmt = push_stmt_list ();
21554       cp_parser_compound_statement (parser, NULL, false);
21555       objc_build_finally_clause (location, pop_stmt_list (stmt));
21556     }
21557
21558   return objc_finish_try_stmt ();
21559 }
21560
21561 /* Parse an Objective-C synchronized statement.
21562
21563    objc-synchronized-stmt:
21564      @synchronized ( expression ) compound-statement
21565
21566    Returns NULL_TREE.  */
21567
21568 static tree
21569 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21570   location_t location;
21571   tree lock, stmt;
21572
21573   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
21574
21575   location = cp_lexer_peek_token (parser->lexer)->location;
21576   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21577   lock = cp_parser_expression (parser, false, NULL);
21578   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21579
21580   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21581      node, lest it get absorbed into the surrounding block.  */
21582   stmt = push_stmt_list ();
21583   cp_parser_compound_statement (parser, NULL, false);
21584
21585   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21586 }
21587
21588 /* Parse an Objective-C throw statement.
21589
21590    objc-throw-stmt:
21591      @throw assignment-expression [opt] ;
21592
21593    Returns a constructed '@throw' statement.  */
21594
21595 static tree
21596 cp_parser_objc_throw_statement (cp_parser *parser) {
21597   tree expr = NULL_TREE;
21598   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21599
21600   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
21601
21602   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21603     expr = cp_parser_assignment_expression (parser, false, NULL);
21604
21605   cp_parser_consume_semicolon_at_end_of_statement (parser);
21606
21607   return objc_build_throw_stmt (loc, expr);
21608 }
21609
21610 /* Parse an Objective-C statement.  */
21611
21612 static tree
21613 cp_parser_objc_statement (cp_parser * parser) {
21614   /* Try to figure out what kind of declaration is present.  */
21615   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21616
21617   switch (kwd->keyword)
21618     {
21619     case RID_AT_TRY:
21620       return cp_parser_objc_try_catch_finally_statement (parser);
21621     case RID_AT_SYNCHRONIZED:
21622       return cp_parser_objc_synchronized_statement (parser);
21623     case RID_AT_THROW:
21624       return cp_parser_objc_throw_statement (parser);
21625     default:
21626       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21627                kwd->u.value);
21628       cp_parser_skip_to_end_of_block_or_statement (parser);
21629     }
21630
21631   return error_mark_node;
21632 }
21633 \f
21634 /* OpenMP 2.5 parsing routines.  */
21635
21636 /* Returns name of the next clause.
21637    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21638    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21639    returned and the token is consumed.  */
21640
21641 static pragma_omp_clause
21642 cp_parser_omp_clause_name (cp_parser *parser)
21643 {
21644   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21645
21646   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21647     result = PRAGMA_OMP_CLAUSE_IF;
21648   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21649     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21650   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21651     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21652   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21653     {
21654       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21655       const char *p = IDENTIFIER_POINTER (id);
21656
21657       switch (p[0])
21658         {
21659         case 'c':
21660           if (!strcmp ("collapse", p))
21661             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21662           else if (!strcmp ("copyin", p))
21663             result = PRAGMA_OMP_CLAUSE_COPYIN;
21664           else if (!strcmp ("copyprivate", p))
21665             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21666           break;
21667         case 'f':
21668           if (!strcmp ("firstprivate", p))
21669             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21670           break;
21671         case 'l':
21672           if (!strcmp ("lastprivate", p))
21673             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21674           break;
21675         case 'n':
21676           if (!strcmp ("nowait", p))
21677             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21678           else if (!strcmp ("num_threads", p))
21679             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21680           break;
21681         case 'o':
21682           if (!strcmp ("ordered", p))
21683             result = PRAGMA_OMP_CLAUSE_ORDERED;
21684           break;
21685         case 'r':
21686           if (!strcmp ("reduction", p))
21687             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21688           break;
21689         case 's':
21690           if (!strcmp ("schedule", p))
21691             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21692           else if (!strcmp ("shared", p))
21693             result = PRAGMA_OMP_CLAUSE_SHARED;
21694           break;
21695         case 'u':
21696           if (!strcmp ("untied", p))
21697             result = PRAGMA_OMP_CLAUSE_UNTIED;
21698           break;
21699         }
21700     }
21701
21702   if (result != PRAGMA_OMP_CLAUSE_NONE)
21703     cp_lexer_consume_token (parser->lexer);
21704
21705   return result;
21706 }
21707
21708 /* Validate that a clause of the given type does not already exist.  */
21709
21710 static void
21711 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21712                            const char *name, location_t location)
21713 {
21714   tree c;
21715
21716   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21717     if (OMP_CLAUSE_CODE (c) == code)
21718       {
21719         error_at (location, "too many %qs clauses", name);
21720         break;
21721       }
21722 }
21723
21724 /* OpenMP 2.5:
21725    variable-list:
21726      identifier
21727      variable-list , identifier
21728
21729    In addition, we match a closing parenthesis.  An opening parenthesis
21730    will have been consumed by the caller.
21731
21732    If KIND is nonzero, create the appropriate node and install the decl
21733    in OMP_CLAUSE_DECL and add the node to the head of the list.
21734
21735    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21736    return the list created.  */
21737
21738 static tree
21739 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21740                                 tree list)
21741 {
21742   cp_token *token;
21743   while (1)
21744     {
21745       tree name, decl;
21746
21747       token = cp_lexer_peek_token (parser->lexer);
21748       name = cp_parser_id_expression (parser, /*template_p=*/false,
21749                                       /*check_dependency_p=*/true,
21750                                       /*template_p=*/NULL,
21751                                       /*declarator_p=*/false,
21752                                       /*optional_p=*/false);
21753       if (name == error_mark_node)
21754         goto skip_comma;
21755
21756       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21757       if (decl == error_mark_node)
21758         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
21759                                      token->location);
21760       else if (kind != 0)
21761         {
21762           tree u = build_omp_clause (token->location, kind);
21763           OMP_CLAUSE_DECL (u) = decl;
21764           OMP_CLAUSE_CHAIN (u) = list;
21765           list = u;
21766         }
21767       else
21768         list = tree_cons (decl, NULL_TREE, list);
21769
21770     get_comma:
21771       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21772         break;
21773       cp_lexer_consume_token (parser->lexer);
21774     }
21775
21776   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21777     {
21778       int ending;
21779
21780       /* Try to resync to an unnested comma.  Copied from
21781          cp_parser_parenthesized_expression_list.  */
21782     skip_comma:
21783       ending = cp_parser_skip_to_closing_parenthesis (parser,
21784                                                       /*recovering=*/true,
21785                                                       /*or_comma=*/true,
21786                                                       /*consume_paren=*/true);
21787       if (ending < 0)
21788         goto get_comma;
21789     }
21790
21791   return list;
21792 }
21793
21794 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21795    common case for omp clauses.  */
21796
21797 static tree
21798 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21799 {
21800   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21801     return cp_parser_omp_var_list_no_open (parser, kind, list);
21802   return list;
21803 }
21804
21805 /* OpenMP 3.0:
21806    collapse ( constant-expression ) */
21807
21808 static tree
21809 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21810 {
21811   tree c, num;
21812   location_t loc;
21813   HOST_WIDE_INT n;
21814
21815   loc = cp_lexer_peek_token (parser->lexer)->location;
21816   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21817     return list;
21818
21819   num = cp_parser_constant_expression (parser, false, NULL);
21820
21821   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21822     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21823                                            /*or_comma=*/false,
21824                                            /*consume_paren=*/true);
21825
21826   if (num == error_mark_node)
21827     return list;
21828   num = fold_non_dependent_expr (num);
21829   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21830       || !host_integerp (num, 0)
21831       || (n = tree_low_cst (num, 0)) <= 0
21832       || (int) n != n)
21833     {
21834       error_at (loc, "collapse argument needs positive constant integer expression");
21835       return list;
21836     }
21837
21838   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21839   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21840   OMP_CLAUSE_CHAIN (c) = list;
21841   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21842
21843   return c;
21844 }
21845
21846 /* OpenMP 2.5:
21847    default ( shared | none ) */
21848
21849 static tree
21850 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21851 {
21852   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21853   tree c;
21854
21855   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21856     return list;
21857   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21858     {
21859       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21860       const char *p = IDENTIFIER_POINTER (id);
21861
21862       switch (p[0])
21863         {
21864         case 'n':
21865           if (strcmp ("none", p) != 0)
21866             goto invalid_kind;
21867           kind = OMP_CLAUSE_DEFAULT_NONE;
21868           break;
21869
21870         case 's':
21871           if (strcmp ("shared", p) != 0)
21872             goto invalid_kind;
21873           kind = OMP_CLAUSE_DEFAULT_SHARED;
21874           break;
21875
21876         default:
21877           goto invalid_kind;
21878         }
21879
21880       cp_lexer_consume_token (parser->lexer);
21881     }
21882   else
21883     {
21884     invalid_kind:
21885       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21886     }
21887
21888   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21889     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21890                                            /*or_comma=*/false,
21891                                            /*consume_paren=*/true);
21892
21893   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21894     return list;
21895
21896   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21897   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21898   OMP_CLAUSE_CHAIN (c) = list;
21899   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21900
21901   return c;
21902 }
21903
21904 /* OpenMP 2.5:
21905    if ( expression ) */
21906
21907 static tree
21908 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21909 {
21910   tree t, c;
21911
21912   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21913     return list;
21914
21915   t = cp_parser_condition (parser);
21916
21917   if (t == error_mark_node
21918       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21919     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21920                                            /*or_comma=*/false,
21921                                            /*consume_paren=*/true);
21922
21923   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21924
21925   c = build_omp_clause (location, OMP_CLAUSE_IF);
21926   OMP_CLAUSE_IF_EXPR (c) = t;
21927   OMP_CLAUSE_CHAIN (c) = list;
21928
21929   return c;
21930 }
21931
21932 /* OpenMP 2.5:
21933    nowait */
21934
21935 static tree
21936 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21937                              tree list, location_t location)
21938 {
21939   tree c;
21940
21941   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21942
21943   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21944   OMP_CLAUSE_CHAIN (c) = list;
21945   return c;
21946 }
21947
21948 /* OpenMP 2.5:
21949    num_threads ( expression ) */
21950
21951 static tree
21952 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21953                                   location_t location)
21954 {
21955   tree t, c;
21956
21957   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21958     return list;
21959
21960   t = cp_parser_expression (parser, false, NULL);
21961
21962   if (t == error_mark_node
21963       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21964     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21965                                            /*or_comma=*/false,
21966                                            /*consume_paren=*/true);
21967
21968   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21969                              "num_threads", location);
21970
21971   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21972   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21973   OMP_CLAUSE_CHAIN (c) = list;
21974
21975   return c;
21976 }
21977
21978 /* OpenMP 2.5:
21979    ordered */
21980
21981 static tree
21982 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21983                               tree list, location_t location)
21984 {
21985   tree c;
21986
21987   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21988                              "ordered", location);
21989
21990   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21991   OMP_CLAUSE_CHAIN (c) = list;
21992   return c;
21993 }
21994
21995 /* OpenMP 2.5:
21996    reduction ( reduction-operator : variable-list )
21997
21998    reduction-operator:
21999      One of: + * - & ^ | && || */
22000
22001 static tree
22002 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
22003 {
22004   enum tree_code code;
22005   tree nlist, c;
22006
22007   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22008     return list;
22009
22010   switch (cp_lexer_peek_token (parser->lexer)->type)
22011     {
22012     case CPP_PLUS:
22013       code = PLUS_EXPR;
22014       break;
22015     case CPP_MULT:
22016       code = MULT_EXPR;
22017       break;
22018     case CPP_MINUS:
22019       code = MINUS_EXPR;
22020       break;
22021     case CPP_AND:
22022       code = BIT_AND_EXPR;
22023       break;
22024     case CPP_XOR:
22025       code = BIT_XOR_EXPR;
22026       break;
22027     case CPP_OR:
22028       code = BIT_IOR_EXPR;
22029       break;
22030     case CPP_AND_AND:
22031       code = TRUTH_ANDIF_EXPR;
22032       break;
22033     case CPP_OR_OR:
22034       code = TRUTH_ORIF_EXPR;
22035       break;
22036     default:
22037       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
22038                                "%<|%>, %<&&%>, or %<||%>");
22039     resync_fail:
22040       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22041                                              /*or_comma=*/false,
22042                                              /*consume_paren=*/true);
22043       return list;
22044     }
22045   cp_lexer_consume_token (parser->lexer);
22046
22047   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22048     goto resync_fail;
22049
22050   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
22051   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
22052     OMP_CLAUSE_REDUCTION_CODE (c) = code;
22053
22054   return nlist;
22055 }
22056
22057 /* OpenMP 2.5:
22058    schedule ( schedule-kind )
22059    schedule ( schedule-kind , expression )
22060
22061    schedule-kind:
22062      static | dynamic | guided | runtime | auto  */
22063
22064 static tree
22065 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
22066 {
22067   tree c, t;
22068
22069   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22070     return list;
22071
22072   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
22073
22074   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22075     {
22076       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22077       const char *p = IDENTIFIER_POINTER (id);
22078
22079       switch (p[0])
22080         {
22081         case 'd':
22082           if (strcmp ("dynamic", p) != 0)
22083             goto invalid_kind;
22084           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
22085           break;
22086
22087         case 'g':
22088           if (strcmp ("guided", p) != 0)
22089             goto invalid_kind;
22090           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
22091           break;
22092
22093         case 'r':
22094           if (strcmp ("runtime", p) != 0)
22095             goto invalid_kind;
22096           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
22097           break;
22098
22099         default:
22100           goto invalid_kind;
22101         }
22102     }
22103   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
22104     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
22105   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
22106     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
22107   else
22108     goto invalid_kind;
22109   cp_lexer_consume_token (parser->lexer);
22110
22111   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22112     {
22113       cp_token *token;
22114       cp_lexer_consume_token (parser->lexer);
22115
22116       token = cp_lexer_peek_token (parser->lexer);
22117       t = cp_parser_assignment_expression (parser, false, NULL);
22118
22119       if (t == error_mark_node)
22120         goto resync_fail;
22121       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
22122         error_at (token->location, "schedule %<runtime%> does not take "
22123                   "a %<chunk_size%> parameter");
22124       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
22125         error_at (token->location, "schedule %<auto%> does not take "
22126                   "a %<chunk_size%> parameter");
22127       else
22128         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
22129
22130       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22131         goto resync_fail;
22132     }
22133   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
22134     goto resync_fail;
22135
22136   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
22137   OMP_CLAUSE_CHAIN (c) = list;
22138   return c;
22139
22140  invalid_kind:
22141   cp_parser_error (parser, "invalid schedule kind");
22142  resync_fail:
22143   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22144                                          /*or_comma=*/false,
22145                                          /*consume_paren=*/true);
22146   return list;
22147 }
22148
22149 /* OpenMP 3.0:
22150    untied */
22151
22152 static tree
22153 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
22154                              tree list, location_t location)
22155 {
22156   tree c;
22157
22158   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
22159
22160   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
22161   OMP_CLAUSE_CHAIN (c) = list;
22162   return c;
22163 }
22164
22165 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
22166    is a bitmask in MASK.  Return the list of clauses found; the result
22167    of clause default goes in *pdefault.  */
22168
22169 static tree
22170 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
22171                            const char *where, cp_token *pragma_tok)
22172 {
22173   tree clauses = NULL;
22174   bool first = true;
22175   cp_token *token = NULL;
22176
22177   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
22178     {
22179       pragma_omp_clause c_kind;
22180       const char *c_name;
22181       tree prev = clauses;
22182
22183       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22184         cp_lexer_consume_token (parser->lexer);
22185
22186       token = cp_lexer_peek_token (parser->lexer);
22187       c_kind = cp_parser_omp_clause_name (parser);
22188       first = false;
22189
22190       switch (c_kind)
22191         {
22192         case PRAGMA_OMP_CLAUSE_COLLAPSE:
22193           clauses = cp_parser_omp_clause_collapse (parser, clauses,
22194                                                    token->location);
22195           c_name = "collapse";
22196           break;
22197         case PRAGMA_OMP_CLAUSE_COPYIN:
22198           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
22199           c_name = "copyin";
22200           break;
22201         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
22202           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
22203                                             clauses);
22204           c_name = "copyprivate";
22205           break;
22206         case PRAGMA_OMP_CLAUSE_DEFAULT:
22207           clauses = cp_parser_omp_clause_default (parser, clauses,
22208                                                   token->location);
22209           c_name = "default";
22210           break;
22211         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
22212           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
22213                                             clauses);
22214           c_name = "firstprivate";
22215           break;
22216         case PRAGMA_OMP_CLAUSE_IF:
22217           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
22218           c_name = "if";
22219           break;
22220         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
22221           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
22222                                             clauses);
22223           c_name = "lastprivate";
22224           break;
22225         case PRAGMA_OMP_CLAUSE_NOWAIT:
22226           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
22227           c_name = "nowait";
22228           break;
22229         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
22230           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
22231                                                       token->location);
22232           c_name = "num_threads";
22233           break;
22234         case PRAGMA_OMP_CLAUSE_ORDERED:
22235           clauses = cp_parser_omp_clause_ordered (parser, clauses,
22236                                                   token->location);
22237           c_name = "ordered";
22238           break;
22239         case PRAGMA_OMP_CLAUSE_PRIVATE:
22240           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
22241                                             clauses);
22242           c_name = "private";
22243           break;
22244         case PRAGMA_OMP_CLAUSE_REDUCTION:
22245           clauses = cp_parser_omp_clause_reduction (parser, clauses);
22246           c_name = "reduction";
22247           break;
22248         case PRAGMA_OMP_CLAUSE_SCHEDULE:
22249           clauses = cp_parser_omp_clause_schedule (parser, clauses,
22250                                                    token->location);
22251           c_name = "schedule";
22252           break;
22253         case PRAGMA_OMP_CLAUSE_SHARED:
22254           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
22255                                             clauses);
22256           c_name = "shared";
22257           break;
22258         case PRAGMA_OMP_CLAUSE_UNTIED:
22259           clauses = cp_parser_omp_clause_untied (parser, clauses,
22260                                                  token->location);
22261           c_name = "nowait";
22262           break;
22263         default:
22264           cp_parser_error (parser, "expected %<#pragma omp%> clause");
22265           goto saw_error;
22266         }
22267
22268       if (((mask >> c_kind) & 1) == 0)
22269         {
22270           /* Remove the invalid clause(s) from the list to avoid
22271              confusing the rest of the compiler.  */
22272           clauses = prev;
22273           error_at (token->location, "%qs is not valid for %qs", c_name, where);
22274         }
22275     }
22276  saw_error:
22277   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22278   return finish_omp_clauses (clauses);
22279 }
22280
22281 /* OpenMP 2.5:
22282    structured-block:
22283      statement
22284
22285    In practice, we're also interested in adding the statement to an
22286    outer node.  So it is convenient if we work around the fact that
22287    cp_parser_statement calls add_stmt.  */
22288
22289 static unsigned
22290 cp_parser_begin_omp_structured_block (cp_parser *parser)
22291 {
22292   unsigned save = parser->in_statement;
22293
22294   /* Only move the values to IN_OMP_BLOCK if they weren't false.
22295      This preserves the "not within loop or switch" style error messages
22296      for nonsense cases like
22297         void foo() {
22298         #pragma omp single
22299           break;
22300         }
22301   */
22302   if (parser->in_statement)
22303     parser->in_statement = IN_OMP_BLOCK;
22304
22305   return save;
22306 }
22307
22308 static void
22309 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
22310 {
22311   parser->in_statement = save;
22312 }
22313
22314 static tree
22315 cp_parser_omp_structured_block (cp_parser *parser)
22316 {
22317   tree stmt = begin_omp_structured_block ();
22318   unsigned int save = cp_parser_begin_omp_structured_block (parser);
22319
22320   cp_parser_statement (parser, NULL_TREE, false, NULL);
22321
22322   cp_parser_end_omp_structured_block (parser, save);
22323   return finish_omp_structured_block (stmt);
22324 }
22325
22326 /* OpenMP 2.5:
22327    # pragma omp atomic new-line
22328      expression-stmt
22329
22330    expression-stmt:
22331      x binop= expr | x++ | ++x | x-- | --x
22332    binop:
22333      +, *, -, /, &, ^, |, <<, >>
22334
22335   where x is an lvalue expression with scalar type.  */
22336
22337 static void
22338 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
22339 {
22340   tree lhs, rhs;
22341   enum tree_code code;
22342
22343   cp_parser_require_pragma_eol (parser, pragma_tok);
22344
22345   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
22346                                     /*cast_p=*/false, NULL);
22347   switch (TREE_CODE (lhs))
22348     {
22349     case ERROR_MARK:
22350       goto saw_error;
22351
22352     case PREINCREMENT_EXPR:
22353     case POSTINCREMENT_EXPR:
22354       lhs = TREE_OPERAND (lhs, 0);
22355       code = PLUS_EXPR;
22356       rhs = integer_one_node;
22357       break;
22358
22359     case PREDECREMENT_EXPR:
22360     case POSTDECREMENT_EXPR:
22361       lhs = TREE_OPERAND (lhs, 0);
22362       code = MINUS_EXPR;
22363       rhs = integer_one_node;
22364       break;
22365
22366     default:
22367       switch (cp_lexer_peek_token (parser->lexer)->type)
22368         {
22369         case CPP_MULT_EQ:
22370           code = MULT_EXPR;
22371           break;
22372         case CPP_DIV_EQ:
22373           code = TRUNC_DIV_EXPR;
22374           break;
22375         case CPP_PLUS_EQ:
22376           code = PLUS_EXPR;
22377           break;
22378         case CPP_MINUS_EQ:
22379           code = MINUS_EXPR;
22380           break;
22381         case CPP_LSHIFT_EQ:
22382           code = LSHIFT_EXPR;
22383           break;
22384         case CPP_RSHIFT_EQ:
22385           code = RSHIFT_EXPR;
22386           break;
22387         case CPP_AND_EQ:
22388           code = BIT_AND_EXPR;
22389           break;
22390         case CPP_OR_EQ:
22391           code = BIT_IOR_EXPR;
22392           break;
22393         case CPP_XOR_EQ:
22394           code = BIT_XOR_EXPR;
22395           break;
22396         default:
22397           cp_parser_error (parser,
22398                            "invalid operator for %<#pragma omp atomic%>");
22399           goto saw_error;
22400         }
22401       cp_lexer_consume_token (parser->lexer);
22402
22403       rhs = cp_parser_expression (parser, false, NULL);
22404       if (rhs == error_mark_node)
22405         goto saw_error;
22406       break;
22407     }
22408   finish_omp_atomic (code, lhs, rhs);
22409   cp_parser_consume_semicolon_at_end_of_statement (parser);
22410   return;
22411
22412  saw_error:
22413   cp_parser_skip_to_end_of_block_or_statement (parser);
22414 }
22415
22416
22417 /* OpenMP 2.5:
22418    # pragma omp barrier new-line  */
22419
22420 static void
22421 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
22422 {
22423   cp_parser_require_pragma_eol (parser, pragma_tok);
22424   finish_omp_barrier ();
22425 }
22426
22427 /* OpenMP 2.5:
22428    # pragma omp critical [(name)] new-line
22429      structured-block  */
22430
22431 static tree
22432 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22433 {
22434   tree stmt, name = NULL;
22435
22436   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22437     {
22438       cp_lexer_consume_token (parser->lexer);
22439
22440       name = cp_parser_identifier (parser);
22441
22442       if (name == error_mark_node
22443           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22444         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22445                                                /*or_comma=*/false,
22446                                                /*consume_paren=*/true);
22447       if (name == error_mark_node)
22448         name = NULL;
22449     }
22450   cp_parser_require_pragma_eol (parser, pragma_tok);
22451
22452   stmt = cp_parser_omp_structured_block (parser);
22453   return c_finish_omp_critical (input_location, stmt, name);
22454 }
22455
22456 /* OpenMP 2.5:
22457    # pragma omp flush flush-vars[opt] new-line
22458
22459    flush-vars:
22460      ( variable-list ) */
22461
22462 static void
22463 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22464 {
22465   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22466     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22467   cp_parser_require_pragma_eol (parser, pragma_tok);
22468
22469   finish_omp_flush ();
22470 }
22471
22472 /* Helper function, to parse omp for increment expression.  */
22473
22474 static tree
22475 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22476 {
22477   tree cond = cp_parser_binary_expression (parser, false, true,
22478                                            PREC_NOT_OPERATOR, NULL);
22479   bool overloaded_p;
22480
22481   if (cond == error_mark_node
22482       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22483     {
22484       cp_parser_skip_to_end_of_statement (parser);
22485       return error_mark_node;
22486     }
22487
22488   switch (TREE_CODE (cond))
22489     {
22490     case GT_EXPR:
22491     case GE_EXPR:
22492     case LT_EXPR:
22493     case LE_EXPR:
22494       break;
22495     default:
22496       return error_mark_node;
22497     }
22498
22499   /* If decl is an iterator, preserve LHS and RHS of the relational
22500      expr until finish_omp_for.  */
22501   if (decl
22502       && (type_dependent_expression_p (decl)
22503           || CLASS_TYPE_P (TREE_TYPE (decl))))
22504     return cond;
22505
22506   return build_x_binary_op (TREE_CODE (cond),
22507                             TREE_OPERAND (cond, 0), ERROR_MARK,
22508                             TREE_OPERAND (cond, 1), ERROR_MARK,
22509                             &overloaded_p, tf_warning_or_error);
22510 }
22511
22512 /* Helper function, to parse omp for increment expression.  */
22513
22514 static tree
22515 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22516 {
22517   cp_token *token = cp_lexer_peek_token (parser->lexer);
22518   enum tree_code op;
22519   tree lhs, rhs;
22520   cp_id_kind idk;
22521   bool decl_first;
22522
22523   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22524     {
22525       op = (token->type == CPP_PLUS_PLUS
22526             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22527       cp_lexer_consume_token (parser->lexer);
22528       lhs = cp_parser_cast_expression (parser, false, false, NULL);
22529       if (lhs != decl)
22530         return error_mark_node;
22531       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22532     }
22533
22534   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22535   if (lhs != decl)
22536     return error_mark_node;
22537
22538   token = cp_lexer_peek_token (parser->lexer);
22539   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22540     {
22541       op = (token->type == CPP_PLUS_PLUS
22542             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22543       cp_lexer_consume_token (parser->lexer);
22544       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22545     }
22546
22547   op = cp_parser_assignment_operator_opt (parser);
22548   if (op == ERROR_MARK)
22549     return error_mark_node;
22550
22551   if (op != NOP_EXPR)
22552     {
22553       rhs = cp_parser_assignment_expression (parser, false, NULL);
22554       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22555       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22556     }
22557
22558   lhs = cp_parser_binary_expression (parser, false, false,
22559                                      PREC_ADDITIVE_EXPRESSION, NULL);
22560   token = cp_lexer_peek_token (parser->lexer);
22561   decl_first = lhs == decl;
22562   if (decl_first)
22563     lhs = NULL_TREE;
22564   if (token->type != CPP_PLUS
22565       && token->type != CPP_MINUS)
22566     return error_mark_node;
22567
22568   do
22569     {
22570       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22571       cp_lexer_consume_token (parser->lexer);
22572       rhs = cp_parser_binary_expression (parser, false, false,
22573                                          PREC_ADDITIVE_EXPRESSION, NULL);
22574       token = cp_lexer_peek_token (parser->lexer);
22575       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22576         {
22577           if (lhs == NULL_TREE)
22578             {
22579               if (op == PLUS_EXPR)
22580                 lhs = rhs;
22581               else
22582                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22583             }
22584           else
22585             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22586                                      NULL, tf_warning_or_error);
22587         }
22588     }
22589   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22590
22591   if (!decl_first)
22592     {
22593       if (rhs != decl || op == MINUS_EXPR)
22594         return error_mark_node;
22595       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22596     }
22597   else
22598     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22599
22600   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22601 }
22602
22603 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22604
22605 static tree
22606 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22607 {
22608   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22609   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22610   tree this_pre_body, cl;
22611   location_t loc_first;
22612   bool collapse_err = false;
22613   int i, collapse = 1, nbraces = 0;
22614
22615   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22616     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22617       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22618
22619   gcc_assert (collapse >= 1);
22620
22621   declv = make_tree_vec (collapse);
22622   initv = make_tree_vec (collapse);
22623   condv = make_tree_vec (collapse);
22624   incrv = make_tree_vec (collapse);
22625
22626   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22627
22628   for (i = 0; i < collapse; i++)
22629     {
22630       int bracecount = 0;
22631       bool add_private_clause = false;
22632       location_t loc;
22633
22634       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22635         {
22636           cp_parser_error (parser, "for statement expected");
22637           return NULL;
22638         }
22639       loc = cp_lexer_consume_token (parser->lexer)->location;
22640
22641       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22642         return NULL;
22643
22644       init = decl = real_decl = NULL;
22645       this_pre_body = push_stmt_list ();
22646       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22647         {
22648           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22649
22650              init-expr:
22651                        var = lb
22652                        integer-type var = lb
22653                        random-access-iterator-type var = lb
22654                        pointer-type var = lb
22655           */
22656           cp_decl_specifier_seq type_specifiers;
22657
22658           /* First, try to parse as an initialized declaration.  See
22659              cp_parser_condition, from whence the bulk of this is copied.  */
22660
22661           cp_parser_parse_tentatively (parser);
22662           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22663                                         /*is_trailing_return=*/false,
22664                                         &type_specifiers);
22665           if (cp_parser_parse_definitely (parser))
22666             {
22667               /* If parsing a type specifier seq succeeded, then this
22668                  MUST be a initialized declaration.  */
22669               tree asm_specification, attributes;
22670               cp_declarator *declarator;
22671
22672               declarator = cp_parser_declarator (parser,
22673                                                  CP_PARSER_DECLARATOR_NAMED,
22674                                                  /*ctor_dtor_or_conv_p=*/NULL,
22675                                                  /*parenthesized_p=*/NULL,
22676                                                  /*member_p=*/false);
22677               attributes = cp_parser_attributes_opt (parser);
22678               asm_specification = cp_parser_asm_specification_opt (parser);
22679
22680               if (declarator == cp_error_declarator) 
22681                 cp_parser_skip_to_end_of_statement (parser);
22682
22683               else 
22684                 {
22685                   tree pushed_scope, auto_node;
22686
22687                   decl = start_decl (declarator, &type_specifiers,
22688                                      SD_INITIALIZED, attributes,
22689                                      /*prefix_attributes=*/NULL_TREE,
22690                                      &pushed_scope);
22691
22692                   auto_node = type_uses_auto (TREE_TYPE (decl));
22693                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22694                     {
22695                       if (cp_lexer_next_token_is (parser->lexer, 
22696                                                   CPP_OPEN_PAREN))
22697                         error ("parenthesized initialization is not allowed in "
22698                                "OpenMP %<for%> loop");
22699                       else
22700                         /* Trigger an error.  */
22701                         cp_parser_require (parser, CPP_EQ, RT_EQ);
22702
22703                       init = error_mark_node;
22704                       cp_parser_skip_to_end_of_statement (parser);
22705                     }
22706                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22707                            || type_dependent_expression_p (decl)
22708                            || auto_node)
22709                     {
22710                       bool is_direct_init, is_non_constant_init;
22711
22712                       init = cp_parser_initializer (parser,
22713                                                     &is_direct_init,
22714                                                     &is_non_constant_init);
22715
22716                       if (auto_node && describable_type (init))
22717                         {
22718                           TREE_TYPE (decl)
22719                             = do_auto_deduction (TREE_TYPE (decl), init,
22720                                                  auto_node);
22721
22722                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22723                               && !type_dependent_expression_p (decl))
22724                             goto non_class;
22725                         }
22726                       
22727                       cp_finish_decl (decl, init, !is_non_constant_init,
22728                                       asm_specification,
22729                                       LOOKUP_ONLYCONVERTING);
22730                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22731                         {
22732                           for_block
22733                             = tree_cons (NULL, this_pre_body, for_block);
22734                           init = NULL_TREE;
22735                         }
22736                       else
22737                         init = pop_stmt_list (this_pre_body);
22738                       this_pre_body = NULL_TREE;
22739                     }
22740                   else
22741                     {
22742                       /* Consume '='.  */
22743                       cp_lexer_consume_token (parser->lexer);
22744                       init = cp_parser_assignment_expression (parser, false, NULL);
22745
22746                     non_class:
22747                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22748                         init = error_mark_node;
22749                       else
22750                         cp_finish_decl (decl, NULL_TREE,
22751                                         /*init_const_expr_p=*/false,
22752                                         asm_specification,
22753                                         LOOKUP_ONLYCONVERTING);
22754                     }
22755
22756                   if (pushed_scope)
22757                     pop_scope (pushed_scope);
22758                 }
22759             }
22760           else 
22761             {
22762               cp_id_kind idk;
22763               /* If parsing a type specifier sequence failed, then
22764                  this MUST be a simple expression.  */
22765               cp_parser_parse_tentatively (parser);
22766               decl = cp_parser_primary_expression (parser, false, false,
22767                                                    false, &idk);
22768               if (!cp_parser_error_occurred (parser)
22769                   && decl
22770                   && DECL_P (decl)
22771                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22772                 {
22773                   tree rhs;
22774
22775                   cp_parser_parse_definitely (parser);
22776                   cp_parser_require (parser, CPP_EQ, RT_EQ);
22777                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22778                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22779                                                          rhs,
22780                                                          tf_warning_or_error));
22781                   add_private_clause = true;
22782                 }
22783               else
22784                 {
22785                   decl = NULL;
22786                   cp_parser_abort_tentative_parse (parser);
22787                   init = cp_parser_expression (parser, false, NULL);
22788                   if (init)
22789                     {
22790                       if (TREE_CODE (init) == MODIFY_EXPR
22791                           || TREE_CODE (init) == MODOP_EXPR)
22792                         real_decl = TREE_OPERAND (init, 0);
22793                     }
22794                 }
22795             }
22796         }
22797       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22798       if (this_pre_body)
22799         {
22800           this_pre_body = pop_stmt_list (this_pre_body);
22801           if (pre_body)
22802             {
22803               tree t = pre_body;
22804               pre_body = push_stmt_list ();
22805               add_stmt (t);
22806               add_stmt (this_pre_body);
22807               pre_body = pop_stmt_list (pre_body);
22808             }
22809           else
22810             pre_body = this_pre_body;
22811         }
22812
22813       if (decl)
22814         real_decl = decl;
22815       if (par_clauses != NULL && real_decl != NULL_TREE)
22816         {
22817           tree *c;
22818           for (c = par_clauses; *c ; )
22819             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22820                 && OMP_CLAUSE_DECL (*c) == real_decl)
22821               {
22822                 error_at (loc, "iteration variable %qD"
22823                           " should not be firstprivate", real_decl);
22824                 *c = OMP_CLAUSE_CHAIN (*c);
22825               }
22826             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22827                      && OMP_CLAUSE_DECL (*c) == real_decl)
22828               {
22829                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22830                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22831                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22832                 OMP_CLAUSE_DECL (l) = real_decl;
22833                 OMP_CLAUSE_CHAIN (l) = clauses;
22834                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22835                 clauses = l;
22836                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22837                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22838                 add_private_clause = false;
22839               }
22840             else
22841               {
22842                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22843                     && OMP_CLAUSE_DECL (*c) == real_decl)
22844                   add_private_clause = false;
22845                 c = &OMP_CLAUSE_CHAIN (*c);
22846               }
22847         }
22848
22849       if (add_private_clause)
22850         {
22851           tree c;
22852           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22853             {
22854               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22855                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22856                   && OMP_CLAUSE_DECL (c) == decl)
22857                 break;
22858               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22859                        && OMP_CLAUSE_DECL (c) == decl)
22860                 error_at (loc, "iteration variable %qD "
22861                           "should not be firstprivate",
22862                           decl);
22863               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22864                        && OMP_CLAUSE_DECL (c) == decl)
22865                 error_at (loc, "iteration variable %qD should not be reduction",
22866                           decl);
22867             }
22868           if (c == NULL)
22869             {
22870               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22871               OMP_CLAUSE_DECL (c) = decl;
22872               c = finish_omp_clauses (c);
22873               if (c)
22874                 {
22875                   OMP_CLAUSE_CHAIN (c) = clauses;
22876                   clauses = c;
22877                 }
22878             }
22879         }
22880
22881       cond = NULL;
22882       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22883         cond = cp_parser_omp_for_cond (parser, decl);
22884       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22885
22886       incr = NULL;
22887       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22888         {
22889           /* If decl is an iterator, preserve the operator on decl
22890              until finish_omp_for.  */
22891           if (decl
22892               && (type_dependent_expression_p (decl)
22893                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22894             incr = cp_parser_omp_for_incr (parser, decl);
22895           else
22896             incr = cp_parser_expression (parser, false, NULL);
22897         }
22898
22899       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22900         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22901                                                /*or_comma=*/false,
22902                                                /*consume_paren=*/true);
22903
22904       TREE_VEC_ELT (declv, i) = decl;
22905       TREE_VEC_ELT (initv, i) = init;
22906       TREE_VEC_ELT (condv, i) = cond;
22907       TREE_VEC_ELT (incrv, i) = incr;
22908
22909       if (i == collapse - 1)
22910         break;
22911
22912       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22913          in between the collapsed for loops to be still considered perfectly
22914          nested.  Hopefully the final version clarifies this.
22915          For now handle (multiple) {'s and empty statements.  */
22916       cp_parser_parse_tentatively (parser);
22917       do
22918         {
22919           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22920             break;
22921           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22922             {
22923               cp_lexer_consume_token (parser->lexer);
22924               bracecount++;
22925             }
22926           else if (bracecount
22927                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22928             cp_lexer_consume_token (parser->lexer);
22929           else
22930             {
22931               loc = cp_lexer_peek_token (parser->lexer)->location;
22932               error_at (loc, "not enough collapsed for loops");
22933               collapse_err = true;
22934               cp_parser_abort_tentative_parse (parser);
22935               declv = NULL_TREE;
22936               break;
22937             }
22938         }
22939       while (1);
22940
22941       if (declv)
22942         {
22943           cp_parser_parse_definitely (parser);
22944           nbraces += bracecount;
22945         }
22946     }
22947
22948   /* Note that we saved the original contents of this flag when we entered
22949      the structured block, and so we don't need to re-save it here.  */
22950   parser->in_statement = IN_OMP_FOR;
22951
22952   /* Note that the grammar doesn't call for a structured block here,
22953      though the loop as a whole is a structured block.  */
22954   body = push_stmt_list ();
22955   cp_parser_statement (parser, NULL_TREE, false, NULL);
22956   body = pop_stmt_list (body);
22957
22958   if (declv == NULL_TREE)
22959     ret = NULL_TREE;
22960   else
22961     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22962                           pre_body, clauses);
22963
22964   while (nbraces)
22965     {
22966       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22967         {
22968           cp_lexer_consume_token (parser->lexer);
22969           nbraces--;
22970         }
22971       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22972         cp_lexer_consume_token (parser->lexer);
22973       else
22974         {
22975           if (!collapse_err)
22976             {
22977               error_at (cp_lexer_peek_token (parser->lexer)->location,
22978                         "collapsed loops not perfectly nested");
22979             }
22980           collapse_err = true;
22981           cp_parser_statement_seq_opt (parser, NULL);
22982           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22983             break;
22984         }
22985     }
22986
22987   while (for_block)
22988     {
22989       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22990       for_block = TREE_CHAIN (for_block);
22991     }
22992
22993   return ret;
22994 }
22995
22996 /* OpenMP 2.5:
22997    #pragma omp for for-clause[optseq] new-line
22998      for-loop  */
22999
23000 #define OMP_FOR_CLAUSE_MASK                             \
23001         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23002         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23003         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
23004         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
23005         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
23006         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
23007         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
23008         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
23009
23010 static tree
23011 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
23012 {
23013   tree clauses, sb, ret;
23014   unsigned int save;
23015
23016   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
23017                                        "#pragma omp for", pragma_tok);
23018
23019   sb = begin_omp_structured_block ();
23020   save = cp_parser_begin_omp_structured_block (parser);
23021
23022   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
23023
23024   cp_parser_end_omp_structured_block (parser, save);
23025   add_stmt (finish_omp_structured_block (sb));
23026
23027   return ret;
23028 }
23029
23030 /* OpenMP 2.5:
23031    # pragma omp master new-line
23032      structured-block  */
23033
23034 static tree
23035 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
23036 {
23037   cp_parser_require_pragma_eol (parser, pragma_tok);
23038   return c_finish_omp_master (input_location,
23039                               cp_parser_omp_structured_block (parser));
23040 }
23041
23042 /* OpenMP 2.5:
23043    # pragma omp ordered new-line
23044      structured-block  */
23045
23046 static tree
23047 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
23048 {
23049   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23050   cp_parser_require_pragma_eol (parser, pragma_tok);
23051   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
23052 }
23053
23054 /* OpenMP 2.5:
23055
23056    section-scope:
23057      { section-sequence }
23058
23059    section-sequence:
23060      section-directive[opt] structured-block
23061      section-sequence section-directive structured-block  */
23062
23063 static tree
23064 cp_parser_omp_sections_scope (cp_parser *parser)
23065 {
23066   tree stmt, substmt;
23067   bool error_suppress = false;
23068   cp_token *tok;
23069
23070   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23071     return NULL_TREE;
23072
23073   stmt = push_stmt_list ();
23074
23075   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
23076     {
23077       unsigned save;
23078
23079       substmt = begin_omp_structured_block ();
23080       save = cp_parser_begin_omp_structured_block (parser);
23081
23082       while (1)
23083         {
23084           cp_parser_statement (parser, NULL_TREE, false, NULL);
23085
23086           tok = cp_lexer_peek_token (parser->lexer);
23087           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23088             break;
23089           if (tok->type == CPP_CLOSE_BRACE)
23090             break;
23091           if (tok->type == CPP_EOF)
23092             break;
23093         }
23094
23095       cp_parser_end_omp_structured_block (parser, save);
23096       substmt = finish_omp_structured_block (substmt);
23097       substmt = build1 (OMP_SECTION, void_type_node, substmt);
23098       add_stmt (substmt);
23099     }
23100
23101   while (1)
23102     {
23103       tok = cp_lexer_peek_token (parser->lexer);
23104       if (tok->type == CPP_CLOSE_BRACE)
23105         break;
23106       if (tok->type == CPP_EOF)
23107         break;
23108
23109       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23110         {
23111           cp_lexer_consume_token (parser->lexer);
23112           cp_parser_require_pragma_eol (parser, tok);
23113           error_suppress = false;
23114         }
23115       else if (!error_suppress)
23116         {
23117           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
23118           error_suppress = true;
23119         }
23120
23121       substmt = cp_parser_omp_structured_block (parser);
23122       substmt = build1 (OMP_SECTION, void_type_node, substmt);
23123       add_stmt (substmt);
23124     }
23125   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23126
23127   substmt = pop_stmt_list (stmt);
23128
23129   stmt = make_node (OMP_SECTIONS);
23130   TREE_TYPE (stmt) = void_type_node;
23131   OMP_SECTIONS_BODY (stmt) = substmt;
23132
23133   add_stmt (stmt);
23134   return stmt;
23135 }
23136
23137 /* OpenMP 2.5:
23138    # pragma omp sections sections-clause[optseq] newline
23139      sections-scope  */
23140
23141 #define OMP_SECTIONS_CLAUSE_MASK                        \
23142         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23143         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23144         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
23145         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
23146         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23147
23148 static tree
23149 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
23150 {
23151   tree clauses, ret;
23152
23153   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
23154                                        "#pragma omp sections", pragma_tok);
23155
23156   ret = cp_parser_omp_sections_scope (parser);
23157   if (ret)
23158     OMP_SECTIONS_CLAUSES (ret) = clauses;
23159
23160   return ret;
23161 }
23162
23163 /* OpenMP 2.5:
23164    # pragma parallel parallel-clause new-line
23165    # pragma parallel for parallel-for-clause new-line
23166    # pragma parallel sections parallel-sections-clause new-line  */
23167
23168 #define OMP_PARALLEL_CLAUSE_MASK                        \
23169         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
23170         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23171         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23172         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
23173         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
23174         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
23175         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
23176         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
23177
23178 static tree
23179 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
23180 {
23181   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
23182   const char *p_name = "#pragma omp parallel";
23183   tree stmt, clauses, par_clause, ws_clause, block;
23184   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
23185   unsigned int save;
23186   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23187
23188   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23189     {
23190       cp_lexer_consume_token (parser->lexer);
23191       p_kind = PRAGMA_OMP_PARALLEL_FOR;
23192       p_name = "#pragma omp parallel for";
23193       mask |= OMP_FOR_CLAUSE_MASK;
23194       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23195     }
23196   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23197     {
23198       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23199       const char *p = IDENTIFIER_POINTER (id);
23200       if (strcmp (p, "sections") == 0)
23201         {
23202           cp_lexer_consume_token (parser->lexer);
23203           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
23204           p_name = "#pragma omp parallel sections";
23205           mask |= OMP_SECTIONS_CLAUSE_MASK;
23206           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23207         }
23208     }
23209
23210   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
23211   block = begin_omp_parallel ();
23212   save = cp_parser_begin_omp_structured_block (parser);
23213
23214   switch (p_kind)
23215     {
23216     case PRAGMA_OMP_PARALLEL:
23217       cp_parser_statement (parser, NULL_TREE, false, NULL);
23218       par_clause = clauses;
23219       break;
23220
23221     case PRAGMA_OMP_PARALLEL_FOR:
23222       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23223       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
23224       break;
23225
23226     case PRAGMA_OMP_PARALLEL_SECTIONS:
23227       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23228       stmt = cp_parser_omp_sections_scope (parser);
23229       if (stmt)
23230         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
23231       break;
23232
23233     default:
23234       gcc_unreachable ();
23235     }
23236
23237   cp_parser_end_omp_structured_block (parser, save);
23238   stmt = finish_omp_parallel (par_clause, block);
23239   if (p_kind != PRAGMA_OMP_PARALLEL)
23240     OMP_PARALLEL_COMBINED (stmt) = 1;
23241   return stmt;
23242 }
23243
23244 /* OpenMP 2.5:
23245    # pragma omp single single-clause[optseq] new-line
23246      structured-block  */
23247
23248 #define OMP_SINGLE_CLAUSE_MASK                          \
23249         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23250         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23251         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
23252         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23253
23254 static tree
23255 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
23256 {
23257   tree stmt = make_node (OMP_SINGLE);
23258   TREE_TYPE (stmt) = void_type_node;
23259
23260   OMP_SINGLE_CLAUSES (stmt)
23261     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
23262                                  "#pragma omp single", pragma_tok);
23263   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
23264
23265   return add_stmt (stmt);
23266 }
23267
23268 /* OpenMP 3.0:
23269    # pragma omp task task-clause[optseq] new-line
23270      structured-block  */
23271
23272 #define OMP_TASK_CLAUSE_MASK                            \
23273         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
23274         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
23275         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
23276         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23277         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23278         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
23279
23280 static tree
23281 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
23282 {
23283   tree clauses, block;
23284   unsigned int save;
23285
23286   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
23287                                        "#pragma omp task", pragma_tok);
23288   block = begin_omp_task ();
23289   save = cp_parser_begin_omp_structured_block (parser);
23290   cp_parser_statement (parser, NULL_TREE, false, NULL);
23291   cp_parser_end_omp_structured_block (parser, save);
23292   return finish_omp_task (clauses, block);
23293 }
23294
23295 /* OpenMP 3.0:
23296    # pragma omp taskwait new-line  */
23297
23298 static void
23299 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
23300 {
23301   cp_parser_require_pragma_eol (parser, pragma_tok);
23302   finish_omp_taskwait ();
23303 }
23304
23305 /* OpenMP 2.5:
23306    # pragma omp threadprivate (variable-list) */
23307
23308 static void
23309 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
23310 {
23311   tree vars;
23312
23313   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23314   cp_parser_require_pragma_eol (parser, pragma_tok);
23315
23316   finish_omp_threadprivate (vars);
23317 }
23318
23319 /* Main entry point to OpenMP statement pragmas.  */
23320
23321 static void
23322 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
23323 {
23324   tree stmt;
23325
23326   switch (pragma_tok->pragma_kind)
23327     {
23328     case PRAGMA_OMP_ATOMIC:
23329       cp_parser_omp_atomic (parser, pragma_tok);
23330       return;
23331     case PRAGMA_OMP_CRITICAL:
23332       stmt = cp_parser_omp_critical (parser, pragma_tok);
23333       break;
23334     case PRAGMA_OMP_FOR:
23335       stmt = cp_parser_omp_for (parser, pragma_tok);
23336       break;
23337     case PRAGMA_OMP_MASTER:
23338       stmt = cp_parser_omp_master (parser, pragma_tok);
23339       break;
23340     case PRAGMA_OMP_ORDERED:
23341       stmt = cp_parser_omp_ordered (parser, pragma_tok);
23342       break;
23343     case PRAGMA_OMP_PARALLEL:
23344       stmt = cp_parser_omp_parallel (parser, pragma_tok);
23345       break;
23346     case PRAGMA_OMP_SECTIONS:
23347       stmt = cp_parser_omp_sections (parser, pragma_tok);
23348       break;
23349     case PRAGMA_OMP_SINGLE:
23350       stmt = cp_parser_omp_single (parser, pragma_tok);
23351       break;
23352     case PRAGMA_OMP_TASK:
23353       stmt = cp_parser_omp_task (parser, pragma_tok);
23354       break;
23355     default:
23356       gcc_unreachable ();
23357     }
23358
23359   if (stmt)
23360     SET_EXPR_LOCATION (stmt, pragma_tok->location);
23361 }
23362 \f
23363 /* The parser.  */
23364
23365 static GTY (()) cp_parser *the_parser;
23366
23367 \f
23368 /* Special handling for the first token or line in the file.  The first
23369    thing in the file might be #pragma GCC pch_preprocess, which loads a
23370    PCH file, which is a GC collection point.  So we need to handle this
23371    first pragma without benefit of an existing lexer structure.
23372
23373    Always returns one token to the caller in *FIRST_TOKEN.  This is
23374    either the true first token of the file, or the first token after
23375    the initial pragma.  */
23376
23377 static void
23378 cp_parser_initial_pragma (cp_token *first_token)
23379 {
23380   tree name = NULL;
23381
23382   cp_lexer_get_preprocessor_token (NULL, first_token);
23383   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
23384     return;
23385
23386   cp_lexer_get_preprocessor_token (NULL, first_token);
23387   if (first_token->type == CPP_STRING)
23388     {
23389       name = first_token->u.value;
23390
23391       cp_lexer_get_preprocessor_token (NULL, first_token);
23392       if (first_token->type != CPP_PRAGMA_EOL)
23393         error_at (first_token->location,
23394                   "junk at end of %<#pragma GCC pch_preprocess%>");
23395     }
23396   else
23397     error_at (first_token->location, "expected string literal");
23398
23399   /* Skip to the end of the pragma.  */
23400   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
23401     cp_lexer_get_preprocessor_token (NULL, first_token);
23402
23403   /* Now actually load the PCH file.  */
23404   if (name)
23405     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
23406
23407   /* Read one more token to return to our caller.  We have to do this
23408      after reading the PCH file in, since its pointers have to be
23409      live.  */
23410   cp_lexer_get_preprocessor_token (NULL, first_token);
23411 }
23412
23413 /* Normal parsing of a pragma token.  Here we can (and must) use the
23414    regular lexer.  */
23415
23416 static bool
23417 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
23418 {
23419   cp_token *pragma_tok;
23420   unsigned int id;
23421
23422   pragma_tok = cp_lexer_consume_token (parser->lexer);
23423   gcc_assert (pragma_tok->type == CPP_PRAGMA);
23424   parser->lexer->in_pragma = true;
23425
23426   id = pragma_tok->pragma_kind;
23427   switch (id)
23428     {
23429     case PRAGMA_GCC_PCH_PREPROCESS:
23430       error_at (pragma_tok->location,
23431                 "%<#pragma GCC pch_preprocess%> must be first");
23432       break;
23433
23434     case PRAGMA_OMP_BARRIER:
23435       switch (context)
23436         {
23437         case pragma_compound:
23438           cp_parser_omp_barrier (parser, pragma_tok);
23439           return false;
23440         case pragma_stmt:
23441           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23442                     "used in compound statements");
23443           break;
23444         default:
23445           goto bad_stmt;
23446         }
23447       break;
23448
23449     case PRAGMA_OMP_FLUSH:
23450       switch (context)
23451         {
23452         case pragma_compound:
23453           cp_parser_omp_flush (parser, pragma_tok);
23454           return false;
23455         case pragma_stmt:
23456           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23457                     "used in compound statements");
23458           break;
23459         default:
23460           goto bad_stmt;
23461         }
23462       break;
23463
23464     case PRAGMA_OMP_TASKWAIT:
23465       switch (context)
23466         {
23467         case pragma_compound:
23468           cp_parser_omp_taskwait (parser, pragma_tok);
23469           return false;
23470         case pragma_stmt:
23471           error_at (pragma_tok->location,
23472                     "%<#pragma omp taskwait%> may only be "
23473                     "used in compound statements");
23474           break;
23475         default:
23476           goto bad_stmt;
23477         }
23478       break;
23479
23480     case PRAGMA_OMP_THREADPRIVATE:
23481       cp_parser_omp_threadprivate (parser, pragma_tok);
23482       return false;
23483
23484     case PRAGMA_OMP_ATOMIC:
23485     case PRAGMA_OMP_CRITICAL:
23486     case PRAGMA_OMP_FOR:
23487     case PRAGMA_OMP_MASTER:
23488     case PRAGMA_OMP_ORDERED:
23489     case PRAGMA_OMP_PARALLEL:
23490     case PRAGMA_OMP_SECTIONS:
23491     case PRAGMA_OMP_SINGLE:
23492     case PRAGMA_OMP_TASK:
23493       if (context == pragma_external)
23494         goto bad_stmt;
23495       cp_parser_omp_construct (parser, pragma_tok);
23496       return true;
23497
23498     case PRAGMA_OMP_SECTION:
23499       error_at (pragma_tok->location, 
23500                 "%<#pragma omp section%> may only be used in "
23501                 "%<#pragma omp sections%> construct");
23502       break;
23503
23504     default:
23505       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23506       c_invoke_pragma_handler (id);
23507       break;
23508
23509     bad_stmt:
23510       cp_parser_error (parser, "expected declaration specifiers");
23511       break;
23512     }
23513
23514   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23515   return false;
23516 }
23517
23518 /* The interface the pragma parsers have to the lexer.  */
23519
23520 enum cpp_ttype
23521 pragma_lex (tree *value)
23522 {
23523   cp_token *tok;
23524   enum cpp_ttype ret;
23525
23526   tok = cp_lexer_peek_token (the_parser->lexer);
23527
23528   ret = tok->type;
23529   *value = tok->u.value;
23530
23531   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23532     ret = CPP_EOF;
23533   else if (ret == CPP_STRING)
23534     *value = cp_parser_string_literal (the_parser, false, false);
23535   else
23536     {
23537       cp_lexer_consume_token (the_parser->lexer);
23538       if (ret == CPP_KEYWORD)
23539         ret = CPP_NAME;
23540     }
23541
23542   return ret;
23543 }
23544
23545 \f
23546 /* External interface.  */
23547
23548 /* Parse one entire translation unit.  */
23549
23550 void
23551 c_parse_file (void)
23552 {
23553   static bool already_called = false;
23554
23555   if (already_called)
23556     {
23557       sorry ("inter-module optimizations not implemented for C++");
23558       return;
23559     }
23560   already_called = true;
23561
23562   the_parser = cp_parser_new ();
23563   push_deferring_access_checks (flag_access_control
23564                                 ? dk_no_deferred : dk_no_check);
23565   cp_parser_translation_unit (the_parser);
23566   the_parser = NULL;
23567 }
23568
23569 #include "gt-cp-parser.h"