OSDN Git Service

* parser.c (cp_default_arg_entry): Declare. Declare a VEC of it.
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010  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-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "toplev.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/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   NIC_NONE,
154   /* floating-point literal */
155   NIC_FLOAT,
156   /* %<this%> */
157   NIC_THIS,
158   /* %<__FUNCTION__%> */
159   NIC_FUNC_NAME,
160   /* %<__PRETTY_FUNCTION__%> */
161   NIC_PRETTY_FUNC,
162   /* %<__func__%> */
163   NIC_C99_FUNC,
164   /* "%<va_arg%> */
165   NIC_VA_ARG,
166   /* a cast */
167   NIC_CAST,
168   /* %<typeid%> operator */
169   NIC_TYPEID,
170   /* non-constant compound literals */
171   NIC_NCC,
172   /* a function call */
173   NIC_FUNC_CALL,
174   /* an increment */
175   NIC_INC,
176   /* an decrement */
177   NIC_DEC,
178   /* an array reference */
179   NIC_ARRAY_REF,
180   /* %<->%> */
181   NIC_ARROW,
182   /* %<.%> */
183   NIC_POINT,
184   /* the address of a label */
185   NIC_ADDR_LABEL,
186   /* %<*%> */
187   NIC_STAR,
188   /* %<&%> */
189   NIC_ADDR,
190   /* %<++%> */
191   NIC_PREINCREMENT,
192   /* %<--%> */
193   NIC_PREDECREMENT,
194   /* %<new%> */
195   NIC_NEW,
196   /* %<delete%> */
197   NIC_DEL,
198   /* calls to overloaded operators */
199   NIC_OVERLOADED,
200   /* an assignment */
201   NIC_ASSIGNMENT,
202   /* a comma operator */
203   NIC_COMMA,
204   /* a call to a constructor */
205   NIC_CONSTRUCTOR
206 } non_integral_constant;
207
208 /* The various kinds of errors about name-lookup failing. */
209 typedef enum name_lookup_error {
210   /* NULL */
211   NLE_NULL,
212   /* is not a type */
213   NLE_TYPE,
214   /* is not a class or namespace */
215   NLE_CXX98,
216   /* is not a class, namespace, or enumeration */
217   NLE_NOT_CXX98
218 } name_lookup_error;
219
220 /* The various kinds of required token */
221 typedef enum required_token {
222   RT_NONE,
223   RT_SEMICOLON,  /* ';' */
224   RT_OPEN_PAREN, /* '(' */
225   RT_CLOSE_BRACE, /* '}' */
226   RT_OPEN_BRACE,  /* '{' */
227   RT_CLOSE_SQUARE, /* ']' */
228   RT_OPEN_SQUARE,  /* '[' */
229   RT_COMMA, /* ',' */
230   RT_SCOPE, /* '::' */
231   RT_LESS, /* '<' */
232   RT_GREATER, /* '>' */
233   RT_EQ, /* '=' */
234   RT_ELLIPSIS, /* '...' */
235   RT_MULT, /* '*' */
236   RT_COMPL, /* '~' */
237   RT_COLON, /* ':' */
238   RT_COLON_SCOPE, /* ':' or '::' */
239   RT_CLOSE_PAREN, /* ')' */
240   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
241   RT_PRAGMA_EOL, /* end of line */
242   RT_NAME, /* identifier */
243
244   /* The type is CPP_KEYWORD */
245   RT_NEW, /* new */
246   RT_DELETE, /* delete */
247   RT_RETURN, /* return */
248   RT_WHILE, /* while */
249   RT_EXTERN, /* extern */
250   RT_STATIC_ASSERT, /* static_assert */
251   RT_DECLTYPE, /* decltype */
252   RT_OPERATOR, /* operator */
253   RT_CLASS, /* class */
254   RT_TEMPLATE, /* template */
255   RT_NAMESPACE, /* namespace */
256   RT_USING, /* using */
257   RT_ASM, /* asm */
258   RT_TRY, /* try */
259   RT_CATCH, /* catch */
260   RT_THROW, /* throw */
261   RT_LABEL, /* __label__ */
262   RT_AT_TRY, /* @try */
263   RT_AT_SYNCHRONIZED, /* @synchronized */
264   RT_AT_THROW, /* @throw */
265
266   RT_SELECT,  /* selection-statement */
267   RT_INTERATION, /* iteration-statement */
268   RT_JUMP, /* jump-statement */
269   RT_CLASS_KEY, /* class-key */
270   RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
271 } required_token;
272
273 /* Prototypes.  */
274
275 static cp_lexer *cp_lexer_new_main
276   (void);
277 static cp_lexer *cp_lexer_new_from_tokens
278   (cp_token_cache *tokens);
279 static void cp_lexer_destroy
280   (cp_lexer *);
281 static int cp_lexer_saving_tokens
282   (const cp_lexer *);
283 static cp_token_position cp_lexer_token_position
284   (cp_lexer *, bool);
285 static cp_token *cp_lexer_token_at
286   (cp_lexer *, cp_token_position);
287 static void cp_lexer_get_preprocessor_token
288   (cp_lexer *, cp_token *);
289 static inline cp_token *cp_lexer_peek_token
290   (cp_lexer *);
291 static cp_token *cp_lexer_peek_nth_token
292   (cp_lexer *, size_t);
293 static inline bool cp_lexer_next_token_is
294   (cp_lexer *, enum cpp_ttype);
295 static bool cp_lexer_next_token_is_not
296   (cp_lexer *, enum cpp_ttype);
297 static bool cp_lexer_next_token_is_keyword
298   (cp_lexer *, enum rid);
299 static cp_token *cp_lexer_consume_token
300   (cp_lexer *);
301 static void cp_lexer_purge_token
302   (cp_lexer *);
303 static void cp_lexer_purge_tokens_after
304   (cp_lexer *, cp_token_position);
305 static void cp_lexer_save_tokens
306   (cp_lexer *);
307 static void cp_lexer_commit_tokens
308   (cp_lexer *);
309 static void cp_lexer_rollback_tokens
310   (cp_lexer *);
311 #ifdef ENABLE_CHECKING
312 static void cp_lexer_print_token
313   (FILE *, cp_token *);
314 static inline bool cp_lexer_debugging_p
315   (cp_lexer *);
316 static void cp_lexer_start_debugging
317   (cp_lexer *) ATTRIBUTE_UNUSED;
318 static void cp_lexer_stop_debugging
319   (cp_lexer *) ATTRIBUTE_UNUSED;
320 #else
321 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
322    about passing NULL to functions that require non-NULL arguments
323    (fputs, fprintf).  It will never be used, so all we need is a value
324    of the right type that's guaranteed not to be NULL.  */
325 #define cp_lexer_debug_stream stdout
326 #define cp_lexer_print_token(str, tok) (void) 0
327 #define cp_lexer_debugging_p(lexer) 0
328 #endif /* ENABLE_CHECKING */
329
330 static cp_token_cache *cp_token_cache_new
331   (cp_token *, cp_token *);
332
333 static void cp_parser_initial_pragma
334   (cp_token *);
335
336 /* Manifest constants.  */
337 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
338 #define CP_SAVED_TOKEN_STACK 5
339
340 /* A token type for keywords, as opposed to ordinary identifiers.  */
341 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
342
343 /* A token type for template-ids.  If a template-id is processed while
344    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
345    the value of the CPP_TEMPLATE_ID is whatever was returned by
346    cp_parser_template_id.  */
347 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
348
349 /* A token type for nested-name-specifiers.  If a
350    nested-name-specifier is processed while parsing tentatively, it is
351    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
352    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
353    cp_parser_nested_name_specifier_opt.  */
354 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
355
356 /* A token type for tokens that are not tokens at all; these are used
357    to represent slots in the array where there used to be a token
358    that has now been deleted.  */
359 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
360
361 /* The number of token types, including C++-specific ones.  */
362 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
363
364 /* Variables.  */
365
366 #ifdef ENABLE_CHECKING
367 /* The stream to which debugging output should be written.  */
368 static FILE *cp_lexer_debug_stream;
369 #endif /* ENABLE_CHECKING */
370
371 /* Nonzero if we are parsing an unevaluated operand: an operand to
372    sizeof, typeof, or alignof.  */
373 int cp_unevaluated_operand;
374
375 /* Create a new main C++ lexer, the lexer that gets tokens from the
376    preprocessor.  */
377
378 static cp_lexer *
379 cp_lexer_new_main (void)
380 {
381   cp_token first_token;
382   cp_lexer *lexer;
383   cp_token *pos;
384   size_t alloc;
385   size_t space;
386   cp_token *buffer;
387
388   /* It's possible that parsing the first pragma will load a PCH file,
389      which is a GC collection point.  So we have to do that before
390      allocating any memory.  */
391   cp_parser_initial_pragma (&first_token);
392
393   c_common_no_more_pch ();
394
395   /* Allocate the memory.  */
396   lexer = ggc_alloc_cleared_cp_lexer ();
397
398 #ifdef ENABLE_CHECKING
399   /* Initially we are not debugging.  */
400   lexer->debugging_p = false;
401 #endif /* ENABLE_CHECKING */
402   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
403                                    CP_SAVED_TOKEN_STACK);
404
405   /* Create the buffer.  */
406   alloc = CP_LEXER_BUFFER_SIZE;
407   buffer = ggc_alloc_vec_cp_token (alloc);
408
409   /* Put the first token in the buffer.  */
410   space = alloc;
411   pos = buffer;
412   *pos = first_token;
413
414   /* Get the remaining tokens from the preprocessor.  */
415   while (pos->type != CPP_EOF)
416     {
417       pos++;
418       if (!--space)
419         {
420           space = alloc;
421           alloc *= 2;
422           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
423           pos = buffer + space;
424         }
425       cp_lexer_get_preprocessor_token (lexer, pos);
426     }
427   lexer->buffer = buffer;
428   lexer->buffer_length = alloc - space;
429   lexer->last_token = pos;
430   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
431
432   /* Subsequent preprocessor diagnostics should use compiler
433      diagnostic functions to get the compiler source location.  */
434   done_lexing = true;
435
436   gcc_assert (lexer->next_token->type != CPP_PURGED);
437   return lexer;
438 }
439
440 /* Create a new lexer whose token stream is primed with the tokens in
441    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
442
443 static cp_lexer *
444 cp_lexer_new_from_tokens (cp_token_cache *cache)
445 {
446   cp_token *first = cache->first;
447   cp_token *last = cache->last;
448   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
449
450   /* We do not own the buffer.  */
451   lexer->buffer = NULL;
452   lexer->buffer_length = 0;
453   lexer->next_token = first == last ? &eof_token : first;
454   lexer->last_token = last;
455
456   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
457                                    CP_SAVED_TOKEN_STACK);
458
459 #ifdef ENABLE_CHECKING
460   /* Initially we are not debugging.  */
461   lexer->debugging_p = false;
462 #endif
463
464   gcc_assert (lexer->next_token->type != CPP_PURGED);
465   return lexer;
466 }
467
468 /* Frees all resources associated with LEXER.  */
469
470 static void
471 cp_lexer_destroy (cp_lexer *lexer)
472 {
473   if (lexer->buffer)
474     ggc_free (lexer->buffer);
475   VEC_free (cp_token_position, heap, lexer->saved_tokens);
476   ggc_free (lexer);
477 }
478
479 /* Returns nonzero if debugging information should be output.  */
480
481 #ifdef ENABLE_CHECKING
482
483 static inline bool
484 cp_lexer_debugging_p (cp_lexer *lexer)
485 {
486   return lexer->debugging_p;
487 }
488
489 #endif /* ENABLE_CHECKING */
490
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
493 {
494   gcc_assert (!previous_p || lexer->next_token != &eof_token);
495
496   return lexer->next_token - previous_p;
497 }
498
499 static inline cp_token *
500 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
501 {
502   return pos;
503 }
504
505 /* nonzero if we are presently saving tokens.  */
506
507 static inline int
508 cp_lexer_saving_tokens (const cp_lexer* lexer)
509 {
510   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
511 }
512
513 /* Store the next token from the preprocessor in *TOKEN.  Return true
514    if we reach EOF.  If LEXER is NULL, assume we are handling an
515    initial #pragma pch_preprocess, and thus want the lexer to return
516    processed strings.  */
517
518 static void
519 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
520 {
521   static int is_extern_c = 0;
522
523    /* Get a new token from the preprocessor.  */
524   token->type
525     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
526                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
527   token->keyword = RID_MAX;
528   token->pragma_kind = PRAGMA_NONE;
529
530   /* On some systems, some header files are surrounded by an
531      implicit extern "C" block.  Set a flag in the token if it
532      comes from such a header.  */
533   is_extern_c += pending_lang_change;
534   pending_lang_change = 0;
535   token->implicit_extern_c = is_extern_c > 0;
536
537   /* Check to see if this token is a keyword.  */
538   if (token->type == CPP_NAME)
539     {
540       if (C_IS_RESERVED_WORD (token->u.value))
541         {
542           /* Mark this token as a keyword.  */
543           token->type = CPP_KEYWORD;
544           /* Record which keyword.  */
545           token->keyword = C_RID_CODE (token->u.value);
546         }
547       else
548         {
549           if (warn_cxx0x_compat
550               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
551               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
552             {
553               /* Warn about the C++0x keyword (but still treat it as
554                  an identifier).  */
555               warning (OPT_Wc__0x_compat, 
556                        "identifier %qE will become a keyword in C++0x",
557                        token->u.value);
558
559               /* Clear out the C_RID_CODE so we don't warn about this
560                  particular identifier-turned-keyword again.  */
561               C_SET_RID_CODE (token->u.value, RID_MAX);
562             }
563
564           token->ambiguous_p = false;
565           token->keyword = RID_MAX;
566         }
567     }
568   /* Handle Objective-C++ keywords.  */
569   else if (token->type == CPP_AT_NAME)
570     {
571       token->type = CPP_KEYWORD;
572       switch (C_RID_CODE (token->u.value))
573         {
574         /* Map 'class' to '@class', 'private' to '@private', etc.  */
575         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
576         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
577         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
578         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
579         case RID_THROW: token->keyword = RID_AT_THROW; break;
580         case RID_TRY: token->keyword = RID_AT_TRY; break;
581         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
582         default: token->keyword = C_RID_CODE (token->u.value);
583         }
584     }
585   else if (token->type == CPP_PRAGMA)
586     {
587       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
588       token->pragma_kind = ((enum pragma_kind)
589                             TREE_INT_CST_LOW (token->u.value));
590       token->u.value = NULL_TREE;
591     }
592 }
593
594 /* Update the globals input_location and the input file stack from TOKEN.  */
595 static inline void
596 cp_lexer_set_source_position_from_token (cp_token *token)
597 {
598   if (token->type != CPP_EOF)
599     {
600       input_location = token->location;
601     }
602 }
603
604 /* Return a pointer to the next token in the token stream, but do not
605    consume it.  */
606
607 static inline cp_token *
608 cp_lexer_peek_token (cp_lexer *lexer)
609 {
610   if (cp_lexer_debugging_p (lexer))
611     {
612       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
613       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
614       putc ('\n', cp_lexer_debug_stream);
615     }
616   return lexer->next_token;
617 }
618
619 /* Return true if the next token has the indicated TYPE.  */
620
621 static inline bool
622 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
623 {
624   return cp_lexer_peek_token (lexer)->type == type;
625 }
626
627 /* Return true if the next token does not have the indicated TYPE.  */
628
629 static inline bool
630 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
631 {
632   return !cp_lexer_next_token_is (lexer, type);
633 }
634
635 /* Return true if the next token is the indicated KEYWORD.  */
636
637 static inline bool
638 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
639 {
640   return cp_lexer_peek_token (lexer)->keyword == keyword;
641 }
642
643 /* Return true if the next token is not the indicated KEYWORD.  */
644
645 static inline bool
646 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
647 {
648   return cp_lexer_peek_token (lexer)->keyword != keyword;
649 }
650
651 /* Return true if the next token is a keyword for a decl-specifier.  */
652
653 static bool
654 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
655 {
656   cp_token *token;
657
658   token = cp_lexer_peek_token (lexer);
659   switch (token->keyword) 
660     {
661       /* auto specifier: storage-class-specifier in C++,
662          simple-type-specifier in C++0x.  */
663     case RID_AUTO:
664       /* Storage classes.  */
665     case RID_REGISTER:
666     case RID_STATIC:
667     case RID_EXTERN:
668     case RID_MUTABLE:
669     case RID_THREAD:
670       /* Elaborated type specifiers.  */
671     case RID_ENUM:
672     case RID_CLASS:
673     case RID_STRUCT:
674     case RID_UNION:
675     case RID_TYPENAME:
676       /* Simple type specifiers.  */
677     case RID_CHAR:
678     case RID_CHAR16:
679     case RID_CHAR32:
680     case RID_WCHAR:
681     case RID_BOOL:
682     case RID_SHORT:
683     case RID_INT:
684     case RID_LONG:
685     case RID_INT128:
686     case RID_SIGNED:
687     case RID_UNSIGNED:
688     case RID_FLOAT:
689     case RID_DOUBLE:
690     case RID_VOID:
691       /* GNU extensions.  */ 
692     case RID_ATTRIBUTE:
693     case RID_TYPEOF:
694       /* C++0x extensions.  */
695     case RID_DECLTYPE:
696       return true;
697
698     default:
699       return false;
700     }
701 }
702
703 /* Return a pointer to the Nth token in the token stream.  If N is 1,
704    then this is precisely equivalent to cp_lexer_peek_token (except
705    that it is not inline).  One would like to disallow that case, but
706    there is one case (cp_parser_nth_token_starts_template_id) where
707    the caller passes a variable for N and it might be 1.  */
708
709 static cp_token *
710 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
711 {
712   cp_token *token;
713
714   /* N is 1-based, not zero-based.  */
715   gcc_assert (n > 0);
716
717   if (cp_lexer_debugging_p (lexer))
718     fprintf (cp_lexer_debug_stream,
719              "cp_lexer: peeking ahead %ld at token: ", (long)n);
720
721   --n;
722   token = lexer->next_token;
723   gcc_assert (!n || token != &eof_token);
724   while (n != 0)
725     {
726       ++token;
727       if (token == lexer->last_token)
728         {
729           token = &eof_token;
730           break;
731         }
732
733       if (token->type != CPP_PURGED)
734         --n;
735     }
736
737   if (cp_lexer_debugging_p (lexer))
738     {
739       cp_lexer_print_token (cp_lexer_debug_stream, token);
740       putc ('\n', cp_lexer_debug_stream);
741     }
742
743   return token;
744 }
745
746 /* Return the next token, and advance the lexer's next_token pointer
747    to point to the next non-purged token.  */
748
749 static cp_token *
750 cp_lexer_consume_token (cp_lexer* lexer)
751 {
752   cp_token *token = lexer->next_token;
753
754   gcc_assert (token != &eof_token);
755   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
756
757   do
758     {
759       lexer->next_token++;
760       if (lexer->next_token == lexer->last_token)
761         {
762           lexer->next_token = &eof_token;
763           break;
764         }
765
766     }
767   while (lexer->next_token->type == CPP_PURGED);
768
769   cp_lexer_set_source_position_from_token (token);
770
771   /* Provide debugging output.  */
772   if (cp_lexer_debugging_p (lexer))
773     {
774       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
775       cp_lexer_print_token (cp_lexer_debug_stream, token);
776       putc ('\n', cp_lexer_debug_stream);
777     }
778
779   return token;
780 }
781
782 /* Permanently remove the next token from the token stream, and
783    advance the next_token pointer to refer to the next non-purged
784    token.  */
785
786 static void
787 cp_lexer_purge_token (cp_lexer *lexer)
788 {
789   cp_token *tok = lexer->next_token;
790
791   gcc_assert (tok != &eof_token);
792   tok->type = CPP_PURGED;
793   tok->location = UNKNOWN_LOCATION;
794   tok->u.value = NULL_TREE;
795   tok->keyword = RID_MAX;
796
797   do
798     {
799       tok++;
800       if (tok == lexer->last_token)
801         {
802           tok = &eof_token;
803           break;
804         }
805     }
806   while (tok->type == CPP_PURGED);
807   lexer->next_token = tok;
808 }
809
810 /* Permanently remove all tokens after TOK, up to, but not
811    including, the token that will be returned next by
812    cp_lexer_peek_token.  */
813
814 static void
815 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
816 {
817   cp_token *peek = lexer->next_token;
818
819   if (peek == &eof_token)
820     peek = lexer->last_token;
821
822   gcc_assert (tok < peek);
823
824   for ( tok += 1; tok != peek; tok += 1)
825     {
826       tok->type = CPP_PURGED;
827       tok->location = UNKNOWN_LOCATION;
828       tok->u.value = NULL_TREE;
829       tok->keyword = RID_MAX;
830     }
831 }
832
833 /* Begin saving tokens.  All tokens consumed after this point will be
834    preserved.  */
835
836 static void
837 cp_lexer_save_tokens (cp_lexer* lexer)
838 {
839   /* Provide debugging output.  */
840   if (cp_lexer_debugging_p (lexer))
841     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
842
843   VEC_safe_push (cp_token_position, heap,
844                  lexer->saved_tokens, lexer->next_token);
845 }
846
847 /* Commit to the portion of the token stream most recently saved.  */
848
849 static void
850 cp_lexer_commit_tokens (cp_lexer* lexer)
851 {
852   /* Provide debugging output.  */
853   if (cp_lexer_debugging_p (lexer))
854     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
855
856   VEC_pop (cp_token_position, lexer->saved_tokens);
857 }
858
859 /* Return all tokens saved since the last call to cp_lexer_save_tokens
860    to the token stream.  Stop saving tokens.  */
861
862 static void
863 cp_lexer_rollback_tokens (cp_lexer* lexer)
864 {
865   /* Provide debugging output.  */
866   if (cp_lexer_debugging_p (lexer))
867     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
868
869   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
870 }
871
872 /* Print a representation of the TOKEN on the STREAM.  */
873
874 #ifdef ENABLE_CHECKING
875
876 static void
877 cp_lexer_print_token (FILE * stream, cp_token *token)
878 {
879   /* We don't use cpp_type2name here because the parser defines
880      a few tokens of its own.  */
881   static const char *const token_names[] = {
882     /* cpplib-defined token types */
883 #define OP(e, s) #e,
884 #define TK(e, s) #e,
885     TTYPE_TABLE
886 #undef OP
887 #undef TK
888     /* C++ parser token types - see "Manifest constants", above.  */
889     "KEYWORD",
890     "TEMPLATE_ID",
891     "NESTED_NAME_SPECIFIER",
892     "PURGED"
893   };
894
895   /* If we have a name for the token, print it out.  Otherwise, we
896      simply give the numeric code.  */
897   gcc_assert (token->type < ARRAY_SIZE(token_names));
898   fputs (token_names[token->type], stream);
899
900   /* For some tokens, print the associated data.  */
901   switch (token->type)
902     {
903     case CPP_KEYWORD:
904       /* Some keywords have a value that is not an IDENTIFIER_NODE.
905          For example, `struct' is mapped to an INTEGER_CST.  */
906       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
907         break;
908       /* else fall through */
909     case CPP_NAME:
910       fputs (IDENTIFIER_POINTER (token->u.value), stream);
911       break;
912
913     case CPP_STRING:
914     case CPP_STRING16:
915     case CPP_STRING32:
916     case CPP_WSTRING:
917     case CPP_UTF8STRING:
918       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
919       break;
920
921     default:
922       break;
923     }
924 }
925
926 /* Start emitting debugging information.  */
927
928 static void
929 cp_lexer_start_debugging (cp_lexer* lexer)
930 {
931   lexer->debugging_p = true;
932 }
933
934 /* Stop emitting debugging information.  */
935
936 static void
937 cp_lexer_stop_debugging (cp_lexer* lexer)
938 {
939   lexer->debugging_p = false;
940 }
941
942 #endif /* ENABLE_CHECKING */
943
944 /* Create a new cp_token_cache, representing a range of tokens.  */
945
946 static cp_token_cache *
947 cp_token_cache_new (cp_token *first, cp_token *last)
948 {
949   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
950   cache->first = first;
951   cache->last = last;
952   return cache;
953 }
954
955 \f
956 /* Decl-specifiers.  */
957
958 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
959
960 static void
961 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
962 {
963   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
964 }
965
966 /* Declarators.  */
967
968 /* Nothing other than the parser should be creating declarators;
969    declarators are a semi-syntactic representation of C++ entities.
970    Other parts of the front end that need to create entities (like
971    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
972
973 static cp_declarator *make_call_declarator
974   (cp_declarator *, tree, cp_cv_quals, tree, tree);
975 static cp_declarator *make_array_declarator
976   (cp_declarator *, tree);
977 static cp_declarator *make_pointer_declarator
978   (cp_cv_quals, cp_declarator *);
979 static cp_declarator *make_reference_declarator
980   (cp_cv_quals, cp_declarator *, bool);
981 static cp_parameter_declarator *make_parameter_declarator
982   (cp_decl_specifier_seq *, cp_declarator *, tree);
983 static cp_declarator *make_ptrmem_declarator
984   (cp_cv_quals, tree, cp_declarator *);
985
986 /* An erroneous declarator.  */
987 static cp_declarator *cp_error_declarator;
988
989 /* The obstack on which declarators and related data structures are
990    allocated.  */
991 static struct obstack declarator_obstack;
992
993 /* Alloc BYTES from the declarator memory pool.  */
994
995 static inline void *
996 alloc_declarator (size_t bytes)
997 {
998   return obstack_alloc (&declarator_obstack, bytes);
999 }
1000
1001 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1002    common to all declarators.  */
1003
1004 static cp_declarator *
1005 make_declarator (cp_declarator_kind kind)
1006 {
1007   cp_declarator *declarator;
1008
1009   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1010   declarator->kind = kind;
1011   declarator->attributes = NULL_TREE;
1012   declarator->declarator = NULL;
1013   declarator->parameter_pack_p = false;
1014   declarator->id_loc = UNKNOWN_LOCATION;
1015
1016   return declarator;
1017 }
1018
1019 /* Make a declarator for a generalized identifier.  If
1020    QUALIFYING_SCOPE is non-NULL, the identifier is
1021    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1022    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1023    is, if any.   */
1024
1025 static cp_declarator *
1026 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1027                     special_function_kind sfk)
1028 {
1029   cp_declarator *declarator;
1030
1031   /* It is valid to write:
1032
1033        class C { void f(); };
1034        typedef C D;
1035        void D::f();
1036
1037      The standard is not clear about whether `typedef const C D' is
1038      legal; as of 2002-09-15 the committee is considering that
1039      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1040      well.  */
1041   if (qualifying_scope && TYPE_P (qualifying_scope))
1042     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1043
1044   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1045               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1046               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1047
1048   declarator = make_declarator (cdk_id);
1049   declarator->u.id.qualifying_scope = qualifying_scope;
1050   declarator->u.id.unqualified_name = unqualified_name;
1051   declarator->u.id.sfk = sfk;
1052   
1053   return declarator;
1054 }
1055
1056 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1057    of modifiers such as const or volatile to apply to the pointer
1058    type, represented as identifiers.  */
1059
1060 cp_declarator *
1061 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1062 {
1063   cp_declarator *declarator;
1064
1065   declarator = make_declarator (cdk_pointer);
1066   declarator->declarator = target;
1067   declarator->u.pointer.qualifiers = cv_qualifiers;
1068   declarator->u.pointer.class_type = NULL_TREE;
1069   if (target)
1070     {
1071       declarator->parameter_pack_p = target->parameter_pack_p;
1072       target->parameter_pack_p = false;
1073     }
1074   else
1075     declarator->parameter_pack_p = false;
1076
1077   return declarator;
1078 }
1079
1080 /* Like make_pointer_declarator -- but for references.  */
1081
1082 cp_declarator *
1083 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1084                            bool rvalue_ref)
1085 {
1086   cp_declarator *declarator;
1087
1088   declarator = make_declarator (cdk_reference);
1089   declarator->declarator = target;
1090   declarator->u.reference.qualifiers = cv_qualifiers;
1091   declarator->u.reference.rvalue_ref = rvalue_ref;
1092   if (target)
1093     {
1094       declarator->parameter_pack_p = target->parameter_pack_p;
1095       target->parameter_pack_p = false;
1096     }
1097   else
1098     declarator->parameter_pack_p = false;
1099
1100   return declarator;
1101 }
1102
1103 /* Like make_pointer_declarator -- but for a pointer to a non-static
1104    member of CLASS_TYPE.  */
1105
1106 cp_declarator *
1107 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1108                         cp_declarator *pointee)
1109 {
1110   cp_declarator *declarator;
1111
1112   declarator = make_declarator (cdk_ptrmem);
1113   declarator->declarator = pointee;
1114   declarator->u.pointer.qualifiers = cv_qualifiers;
1115   declarator->u.pointer.class_type = class_type;
1116
1117   if (pointee)
1118     {
1119       declarator->parameter_pack_p = pointee->parameter_pack_p;
1120       pointee->parameter_pack_p = false;
1121     }
1122   else
1123     declarator->parameter_pack_p = false;
1124
1125   return declarator;
1126 }
1127
1128 /* Make a declarator for the function given by TARGET, with the
1129    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1130    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1131    indicates what exceptions can be thrown.  */
1132
1133 cp_declarator *
1134 make_call_declarator (cp_declarator *target,
1135                       tree parms,
1136                       cp_cv_quals cv_qualifiers,
1137                       tree exception_specification,
1138                       tree late_return_type)
1139 {
1140   cp_declarator *declarator;
1141
1142   declarator = make_declarator (cdk_function);
1143   declarator->declarator = target;
1144   declarator->u.function.parameters = parms;
1145   declarator->u.function.qualifiers = cv_qualifiers;
1146   declarator->u.function.exception_specification = exception_specification;
1147   declarator->u.function.late_return_type = late_return_type;
1148   if (target)
1149     {
1150       declarator->parameter_pack_p = target->parameter_pack_p;
1151       target->parameter_pack_p = false;
1152     }
1153   else
1154     declarator->parameter_pack_p = false;
1155
1156   return declarator;
1157 }
1158
1159 /* Make a declarator for an array of BOUNDS elements, each of which is
1160    defined by ELEMENT.  */
1161
1162 cp_declarator *
1163 make_array_declarator (cp_declarator *element, tree bounds)
1164 {
1165   cp_declarator *declarator;
1166
1167   declarator = make_declarator (cdk_array);
1168   declarator->declarator = element;
1169   declarator->u.array.bounds = bounds;
1170   if (element)
1171     {
1172       declarator->parameter_pack_p = element->parameter_pack_p;
1173       element->parameter_pack_p = false;
1174     }
1175   else
1176     declarator->parameter_pack_p = false;
1177
1178   return declarator;
1179 }
1180
1181 /* Determine whether the declarator we've seen so far can be a
1182    parameter pack, when followed by an ellipsis.  */
1183 static bool 
1184 declarator_can_be_parameter_pack (cp_declarator *declarator)
1185 {
1186   /* Search for a declarator name, or any other declarator that goes
1187      after the point where the ellipsis could appear in a parameter
1188      pack. If we find any of these, then this declarator can not be
1189      made into a parameter pack.  */
1190   bool found = false;
1191   while (declarator && !found)
1192     {
1193       switch ((int)declarator->kind)
1194         {
1195         case cdk_id:
1196         case cdk_array:
1197           found = true;
1198           break;
1199
1200         case cdk_error:
1201           return true;
1202
1203         default:
1204           declarator = declarator->declarator;
1205           break;
1206         }
1207     }
1208
1209   return !found;
1210 }
1211
1212 cp_parameter_declarator *no_parameters;
1213
1214 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1215    DECLARATOR and DEFAULT_ARGUMENT.  */
1216
1217 cp_parameter_declarator *
1218 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1219                            cp_declarator *declarator,
1220                            tree default_argument)
1221 {
1222   cp_parameter_declarator *parameter;
1223
1224   parameter = ((cp_parameter_declarator *)
1225                alloc_declarator (sizeof (cp_parameter_declarator)));
1226   parameter->next = NULL;
1227   if (decl_specifiers)
1228     parameter->decl_specifiers = *decl_specifiers;
1229   else
1230     clear_decl_specs (&parameter->decl_specifiers);
1231   parameter->declarator = declarator;
1232   parameter->default_argument = default_argument;
1233   parameter->ellipsis_p = false;
1234
1235   return parameter;
1236 }
1237
1238 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1239
1240 static bool
1241 function_declarator_p (const cp_declarator *declarator)
1242 {
1243   while (declarator)
1244     {
1245       if (declarator->kind == cdk_function
1246           && declarator->declarator->kind == cdk_id)
1247         return true;
1248       if (declarator->kind == cdk_id
1249           || declarator->kind == cdk_error)
1250         return false;
1251       declarator = declarator->declarator;
1252     }
1253   return false;
1254 }
1255  
1256 /* The parser.  */
1257
1258 /* Overview
1259    --------
1260
1261    A cp_parser parses the token stream as specified by the C++
1262    grammar.  Its job is purely parsing, not semantic analysis.  For
1263    example, the parser breaks the token stream into declarators,
1264    expressions, statements, and other similar syntactic constructs.
1265    It does not check that the types of the expressions on either side
1266    of an assignment-statement are compatible, or that a function is
1267    not declared with a parameter of type `void'.
1268
1269    The parser invokes routines elsewhere in the compiler to perform
1270    semantic analysis and to build up the abstract syntax tree for the
1271    code processed.
1272
1273    The parser (and the template instantiation code, which is, in a
1274    way, a close relative of parsing) are the only parts of the
1275    compiler that should be calling push_scope and pop_scope, or
1276    related functions.  The parser (and template instantiation code)
1277    keeps track of what scope is presently active; everything else
1278    should simply honor that.  (The code that generates static
1279    initializers may also need to set the scope, in order to check
1280    access control correctly when emitting the initializers.)
1281
1282    Methodology
1283    -----------
1284
1285    The parser is of the standard recursive-descent variety.  Upcoming
1286    tokens in the token stream are examined in order to determine which
1287    production to use when parsing a non-terminal.  Some C++ constructs
1288    require arbitrary look ahead to disambiguate.  For example, it is
1289    impossible, in the general case, to tell whether a statement is an
1290    expression or declaration without scanning the entire statement.
1291    Therefore, the parser is capable of "parsing tentatively."  When the
1292    parser is not sure what construct comes next, it enters this mode.
1293    Then, while we attempt to parse the construct, the parser queues up
1294    error messages, rather than issuing them immediately, and saves the
1295    tokens it consumes.  If the construct is parsed successfully, the
1296    parser "commits", i.e., it issues any queued error messages and
1297    the tokens that were being preserved are permanently discarded.
1298    If, however, the construct is not parsed successfully, the parser
1299    rolls back its state completely so that it can resume parsing using
1300    a different alternative.
1301
1302    Future Improvements
1303    -------------------
1304
1305    The performance of the parser could probably be improved substantially.
1306    We could often eliminate the need to parse tentatively by looking ahead
1307    a little bit.  In some places, this approach might not entirely eliminate
1308    the need to parse tentatively, but it might still speed up the average
1309    case.  */
1310
1311 /* Flags that are passed to some parsing functions.  These values can
1312    be bitwise-ored together.  */
1313
1314 enum
1315 {
1316   /* No flags.  */
1317   CP_PARSER_FLAGS_NONE = 0x0,
1318   /* The construct is optional.  If it is not present, then no error
1319      should be issued.  */
1320   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1321   /* When parsing a type-specifier, treat user-defined type-names
1322      as non-type identifiers.  */
1323   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1324   /* When parsing a type-specifier, do not try to parse a class-specifier
1325      or enum-specifier.  */
1326   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1327 };
1328
1329 /* This type is used for parameters and variables which hold
1330    combinations of the above flags.  */
1331 typedef int cp_parser_flags;
1332
1333 /* The different kinds of declarators we want to parse.  */
1334
1335 typedef enum cp_parser_declarator_kind
1336 {
1337   /* We want an abstract declarator.  */
1338   CP_PARSER_DECLARATOR_ABSTRACT,
1339   /* We want a named declarator.  */
1340   CP_PARSER_DECLARATOR_NAMED,
1341   /* We don't mind, but the name must be an unqualified-id.  */
1342   CP_PARSER_DECLARATOR_EITHER
1343 } cp_parser_declarator_kind;
1344
1345 /* The precedence values used to parse binary expressions.  The minimum value
1346    of PREC must be 1, because zero is reserved to quickly discriminate
1347    binary operators from other tokens.  */
1348
1349 enum cp_parser_prec
1350 {
1351   PREC_NOT_OPERATOR,
1352   PREC_LOGICAL_OR_EXPRESSION,
1353   PREC_LOGICAL_AND_EXPRESSION,
1354   PREC_INCLUSIVE_OR_EXPRESSION,
1355   PREC_EXCLUSIVE_OR_EXPRESSION,
1356   PREC_AND_EXPRESSION,
1357   PREC_EQUALITY_EXPRESSION,
1358   PREC_RELATIONAL_EXPRESSION,
1359   PREC_SHIFT_EXPRESSION,
1360   PREC_ADDITIVE_EXPRESSION,
1361   PREC_MULTIPLICATIVE_EXPRESSION,
1362   PREC_PM_EXPRESSION,
1363   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1364 };
1365
1366 /* A mapping from a token type to a corresponding tree node type, with a
1367    precedence value.  */
1368
1369 typedef struct cp_parser_binary_operations_map_node
1370 {
1371   /* The token type.  */
1372   enum cpp_ttype token_type;
1373   /* The corresponding tree code.  */
1374   enum tree_code tree_type;
1375   /* The precedence of this operator.  */
1376   enum cp_parser_prec prec;
1377 } cp_parser_binary_operations_map_node;
1378
1379 /* The status of a tentative parse.  */
1380
1381 typedef enum cp_parser_status_kind
1382 {
1383   /* No errors have occurred.  */
1384   CP_PARSER_STATUS_KIND_NO_ERROR,
1385   /* An error has occurred.  */
1386   CP_PARSER_STATUS_KIND_ERROR,
1387   /* We are committed to this tentative parse, whether or not an error
1388      has occurred.  */
1389   CP_PARSER_STATUS_KIND_COMMITTED
1390 } cp_parser_status_kind;
1391
1392 typedef struct cp_parser_expression_stack_entry
1393 {
1394   /* Left hand side of the binary operation we are currently
1395      parsing.  */
1396   tree lhs;
1397   /* Original tree code for left hand side, if it was a binary
1398      expression itself (used for -Wparentheses).  */
1399   enum tree_code lhs_type;
1400   /* Tree code for the binary operation we are parsing.  */
1401   enum tree_code tree_type;
1402   /* Precedence of the binary operation we are parsing.  */
1403   enum cp_parser_prec prec;
1404 } cp_parser_expression_stack_entry;
1405
1406 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1407    entries because precedence levels on the stack are monotonically
1408    increasing.  */
1409 typedef struct cp_parser_expression_stack_entry
1410   cp_parser_expression_stack[NUM_PREC_VALUES];
1411
1412 /* Context that is saved and restored when parsing tentatively.  */
1413 typedef struct GTY (()) cp_parser_context {
1414   /* If this is a tentative parsing context, the status of the
1415      tentative parse.  */
1416   enum cp_parser_status_kind status;
1417   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1418      that are looked up in this context must be looked up both in the
1419      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1420      the context of the containing expression.  */
1421   tree object_type;
1422
1423   /* The next parsing context in the stack.  */
1424   struct cp_parser_context *next;
1425 } cp_parser_context;
1426
1427 /* Prototypes.  */
1428
1429 /* Constructors and destructors.  */
1430
1431 static cp_parser_context *cp_parser_context_new
1432   (cp_parser_context *);
1433
1434 /* Class variables.  */
1435
1436 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1437
1438 /* The operator-precedence table used by cp_parser_binary_expression.
1439    Transformed into an associative array (binops_by_token) by
1440    cp_parser_new.  */
1441
1442 static const cp_parser_binary_operations_map_node binops[] = {
1443   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1444   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1445
1446   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1447   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1448   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1449
1450   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1451   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1452
1453   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1454   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1455
1456   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1457   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1458   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1459   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1460
1461   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1462   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1463
1464   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1465
1466   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1467
1468   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1469
1470   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1471
1472   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1473 };
1474
1475 /* The same as binops, but initialized by cp_parser_new so that
1476    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1477    for speed.  */
1478 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1479
1480 /* Constructors and destructors.  */
1481
1482 /* Construct a new context.  The context below this one on the stack
1483    is given by NEXT.  */
1484
1485 static cp_parser_context *
1486 cp_parser_context_new (cp_parser_context* next)
1487 {
1488   cp_parser_context *context;
1489
1490   /* Allocate the storage.  */
1491   if (cp_parser_context_free_list != NULL)
1492     {
1493       /* Pull the first entry from the free list.  */
1494       context = cp_parser_context_free_list;
1495       cp_parser_context_free_list = context->next;
1496       memset (context, 0, sizeof (*context));
1497     }
1498   else
1499     context = ggc_alloc_cleared_cp_parser_context ();
1500
1501   /* No errors have occurred yet in this context.  */
1502   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1503   /* If this is not the bottommost context, copy information that we
1504      need from the previous context.  */
1505   if (next)
1506     {
1507       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1508          expression, then we are parsing one in this context, too.  */
1509       context->object_type = next->object_type;
1510       /* Thread the stack.  */
1511       context->next = next;
1512     }
1513
1514   return context;
1515 }
1516
1517 /* An entry in a queue of function arguments that require post-processing.  */
1518
1519 typedef struct GTY(()) cp_default_arg_entry_d {
1520   /* The current_class_type when we parsed this arg.  */
1521   tree class_type;
1522
1523   /* The function decl itself.  */
1524   tree decl;
1525 } cp_default_arg_entry;
1526
1527 DEF_VEC_O(cp_default_arg_entry);
1528 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1529
1530 /* An entry in a stack for member functions of local classes.  */
1531
1532 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1533   /* Functions with default arguments that require post-processing.
1534      Functions appear in this list in declaration order.  */
1535   VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1536
1537   /* Functions with defintions that require post-processing.  Functions
1538      appear in this list in declaration order.  */
1539   VEC(tree,gc) *funs_with_definitions;
1540 } cp_unparsed_functions_entry;
1541
1542 DEF_VEC_O(cp_unparsed_functions_entry);
1543 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1544
1545 /* The cp_parser structure represents the C++ parser.  */
1546
1547 typedef struct GTY(()) cp_parser {
1548   /* The lexer from which we are obtaining tokens.  */
1549   cp_lexer *lexer;
1550
1551   /* The scope in which names should be looked up.  If NULL_TREE, then
1552      we look up names in the scope that is currently open in the
1553      source program.  If non-NULL, this is either a TYPE or
1554      NAMESPACE_DECL for the scope in which we should look.  It can
1555      also be ERROR_MARK, when we've parsed a bogus scope.
1556
1557      This value is not cleared automatically after a name is looked
1558      up, so we must be careful to clear it before starting a new look
1559      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1560      will look up `Z' in the scope of `X', rather than the current
1561      scope.)  Unfortunately, it is difficult to tell when name lookup
1562      is complete, because we sometimes peek at a token, look it up,
1563      and then decide not to consume it.   */
1564   tree scope;
1565
1566   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1567      last lookup took place.  OBJECT_SCOPE is used if an expression
1568      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1569      respectively.  QUALIFYING_SCOPE is used for an expression of the
1570      form "X::Y"; it refers to X.  */
1571   tree object_scope;
1572   tree qualifying_scope;
1573
1574   /* A stack of parsing contexts.  All but the bottom entry on the
1575      stack will be tentative contexts.
1576
1577      We parse tentatively in order to determine which construct is in
1578      use in some situations.  For example, in order to determine
1579      whether a statement is an expression-statement or a
1580      declaration-statement we parse it tentatively as a
1581      declaration-statement.  If that fails, we then reparse the same
1582      token stream as an expression-statement.  */
1583   cp_parser_context *context;
1584
1585   /* True if we are parsing GNU C++.  If this flag is not set, then
1586      GNU extensions are not recognized.  */
1587   bool allow_gnu_extensions_p;
1588
1589   /* TRUE if the `>' token should be interpreted as the greater-than
1590      operator.  FALSE if it is the end of a template-id or
1591      template-parameter-list. In C++0x mode, this flag also applies to
1592      `>>' tokens, which are viewed as two consecutive `>' tokens when
1593      this flag is FALSE.  */
1594   bool greater_than_is_operator_p;
1595
1596   /* TRUE if default arguments are allowed within a parameter list
1597      that starts at this point. FALSE if only a gnu extension makes
1598      them permissible.  */
1599   bool default_arg_ok_p;
1600
1601   /* TRUE if we are parsing an integral constant-expression.  See
1602      [expr.const] for a precise definition.  */
1603   bool integral_constant_expression_p;
1604
1605   /* TRUE if we are parsing an integral constant-expression -- but a
1606      non-constant expression should be permitted as well.  This flag
1607      is used when parsing an array bound so that GNU variable-length
1608      arrays are tolerated.  */
1609   bool allow_non_integral_constant_expression_p;
1610
1611   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1612      been seen that makes the expression non-constant.  */
1613   bool non_integral_constant_expression_p;
1614
1615   /* TRUE if local variable names and `this' are forbidden in the
1616      current context.  */
1617   bool local_variables_forbidden_p;
1618
1619   /* TRUE if the declaration we are parsing is part of a
1620      linkage-specification of the form `extern string-literal
1621      declaration'.  */
1622   bool in_unbraced_linkage_specification_p;
1623
1624   /* TRUE if we are presently parsing a declarator, after the
1625      direct-declarator.  */
1626   bool in_declarator_p;
1627
1628   /* TRUE if we are presently parsing a template-argument-list.  */
1629   bool in_template_argument_list_p;
1630
1631   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1632      to IN_OMP_BLOCK if parsing OpenMP structured block and
1633      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1634      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1635      iteration-statement, OpenMP block or loop within that switch.  */
1636 #define IN_SWITCH_STMT          1
1637 #define IN_ITERATION_STMT       2
1638 #define IN_OMP_BLOCK            4
1639 #define IN_OMP_FOR              8
1640 #define IN_IF_STMT             16
1641   unsigned char in_statement;
1642
1643   /* TRUE if we are presently parsing the body of a switch statement.
1644      Note that this doesn't quite overlap with in_statement above.
1645      The difference relates to giving the right sets of error messages:
1646      "case not in switch" vs "break statement used with OpenMP...".  */
1647   bool in_switch_statement_p;
1648
1649   /* TRUE if we are parsing a type-id in an expression context.  In
1650      such a situation, both "type (expr)" and "type (type)" are valid
1651      alternatives.  */
1652   bool in_type_id_in_expr_p;
1653
1654   /* TRUE if we are currently in a header file where declarations are
1655      implicitly extern "C".  */
1656   bool implicit_extern_c;
1657
1658   /* TRUE if strings in expressions should be translated to the execution
1659      character set.  */
1660   bool translate_strings_p;
1661
1662   /* TRUE if we are presently parsing the body of a function, but not
1663      a local class.  */
1664   bool in_function_body;
1665
1666   /* If non-NULL, then we are parsing a construct where new type
1667      definitions are not permitted.  The string stored here will be
1668      issued as an error message if a type is defined.  */
1669   const char *type_definition_forbidden_message;
1670
1671   /* A stack used for member functions of local classes.  The lists
1672      contained in an individual entry can only be processed once the
1673      outermost class being defined is complete.  */
1674   VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1675
1676   /* The number of classes whose definitions are currently in
1677      progress.  */
1678   unsigned num_classes_being_defined;
1679
1680   /* The number of template parameter lists that apply directly to the
1681      current declaration.  */
1682   unsigned num_template_parameter_lists;
1683 } cp_parser;
1684
1685 /* Managing the unparsed function queues.  */
1686
1687 #define unparsed_funs_with_default_args \
1688   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1689 #define unparsed_funs_with_definitions \
1690   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1691
1692 static void
1693 push_unparsed_function_queues (cp_parser *parser)
1694 {
1695   VEC_safe_push (cp_unparsed_functions_entry, gc,
1696                  parser->unparsed_queues, NULL);
1697   unparsed_funs_with_default_args = NULL;
1698   unparsed_funs_with_definitions = make_tree_vector ();
1699 }
1700
1701 static void
1702 pop_unparsed_function_queues (cp_parser *parser)
1703 {
1704   release_tree_vector (unparsed_funs_with_definitions);
1705   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1706 }
1707
1708 /* Prototypes.  */
1709
1710 /* Constructors and destructors.  */
1711
1712 static cp_parser *cp_parser_new
1713   (void);
1714
1715 /* Routines to parse various constructs.
1716
1717    Those that return `tree' will return the error_mark_node (rather
1718    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1719    Sometimes, they will return an ordinary node if error-recovery was
1720    attempted, even though a parse error occurred.  So, to check
1721    whether or not a parse error occurred, you should always use
1722    cp_parser_error_occurred.  If the construct is optional (indicated
1723    either by an `_opt' in the name of the function that does the
1724    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1725    the construct is not present.  */
1726
1727 /* Lexical conventions [gram.lex]  */
1728
1729 static tree cp_parser_identifier
1730   (cp_parser *);
1731 static tree cp_parser_string_literal
1732   (cp_parser *, bool, bool);
1733
1734 /* Basic concepts [gram.basic]  */
1735
1736 static bool cp_parser_translation_unit
1737   (cp_parser *);
1738
1739 /* Expressions [gram.expr]  */
1740
1741 static tree cp_parser_primary_expression
1742   (cp_parser *, bool, bool, bool, cp_id_kind *);
1743 static tree cp_parser_id_expression
1744   (cp_parser *, bool, bool, bool *, bool, bool);
1745 static tree cp_parser_unqualified_id
1746   (cp_parser *, bool, bool, bool, bool);
1747 static tree cp_parser_nested_name_specifier_opt
1748   (cp_parser *, bool, bool, bool, bool);
1749 static tree cp_parser_nested_name_specifier
1750   (cp_parser *, bool, bool, bool, bool);
1751 static tree cp_parser_qualifying_entity
1752   (cp_parser *, bool, bool, bool, bool, bool);
1753 static tree cp_parser_postfix_expression
1754   (cp_parser *, bool, bool, bool, cp_id_kind *);
1755 static tree cp_parser_postfix_open_square_expression
1756   (cp_parser *, tree, bool);
1757 static tree cp_parser_postfix_dot_deref_expression
1758   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1759 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1760   (cp_parser *, int, bool, bool, bool *);
1761 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1762 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1763 static void cp_parser_pseudo_destructor_name
1764   (cp_parser *, tree *, tree *);
1765 static tree cp_parser_unary_expression
1766   (cp_parser *, bool, bool, cp_id_kind *);
1767 static enum tree_code cp_parser_unary_operator
1768   (cp_token *);
1769 static tree cp_parser_new_expression
1770   (cp_parser *);
1771 static VEC(tree,gc) *cp_parser_new_placement
1772   (cp_parser *);
1773 static tree cp_parser_new_type_id
1774   (cp_parser *, tree *);
1775 static cp_declarator *cp_parser_new_declarator_opt
1776   (cp_parser *);
1777 static cp_declarator *cp_parser_direct_new_declarator
1778   (cp_parser *);
1779 static VEC(tree,gc) *cp_parser_new_initializer
1780   (cp_parser *);
1781 static tree cp_parser_delete_expression
1782   (cp_parser *);
1783 static tree cp_parser_cast_expression
1784   (cp_parser *, bool, bool, cp_id_kind *);
1785 static tree cp_parser_binary_expression
1786   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1787 static tree cp_parser_question_colon_clause
1788   (cp_parser *, tree);
1789 static tree cp_parser_assignment_expression
1790   (cp_parser *, bool, cp_id_kind *);
1791 static enum tree_code cp_parser_assignment_operator_opt
1792   (cp_parser *);
1793 static tree cp_parser_expression
1794   (cp_parser *, bool, cp_id_kind *);
1795 static tree cp_parser_constant_expression
1796   (cp_parser *, bool, bool *);
1797 static tree cp_parser_builtin_offsetof
1798   (cp_parser *);
1799 static tree cp_parser_lambda_expression
1800   (cp_parser *);
1801 static void cp_parser_lambda_introducer
1802   (cp_parser *, tree);
1803 static void cp_parser_lambda_declarator_opt
1804   (cp_parser *, tree);
1805 static void cp_parser_lambda_body
1806   (cp_parser *, tree);
1807
1808 /* Statements [gram.stmt.stmt]  */
1809
1810 static void cp_parser_statement
1811   (cp_parser *, tree, bool, bool *);
1812 static void cp_parser_label_for_labeled_statement
1813   (cp_parser *);
1814 static tree cp_parser_expression_statement
1815   (cp_parser *, tree);
1816 static tree cp_parser_compound_statement
1817   (cp_parser *, tree, bool);
1818 static void cp_parser_statement_seq_opt
1819   (cp_parser *, tree);
1820 static tree cp_parser_selection_statement
1821   (cp_parser *, bool *);
1822 static tree cp_parser_condition
1823   (cp_parser *);
1824 static tree cp_parser_iteration_statement
1825   (cp_parser *);
1826 static void cp_parser_for_init_statement
1827   (cp_parser *);
1828 static tree cp_parser_jump_statement
1829   (cp_parser *);
1830 static void cp_parser_declaration_statement
1831   (cp_parser *);
1832
1833 static tree cp_parser_implicitly_scoped_statement
1834   (cp_parser *, bool *);
1835 static void cp_parser_already_scoped_statement
1836   (cp_parser *);
1837
1838 /* Declarations [gram.dcl.dcl] */
1839
1840 static void cp_parser_declaration_seq_opt
1841   (cp_parser *);
1842 static void cp_parser_declaration
1843   (cp_parser *);
1844 static void cp_parser_block_declaration
1845   (cp_parser *, bool);
1846 static void cp_parser_simple_declaration
1847   (cp_parser *, bool);
1848 static void cp_parser_decl_specifier_seq
1849   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1850 static tree cp_parser_storage_class_specifier_opt
1851   (cp_parser *);
1852 static tree cp_parser_function_specifier_opt
1853   (cp_parser *, cp_decl_specifier_seq *);
1854 static tree cp_parser_type_specifier
1855   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1856    int *, bool *);
1857 static tree cp_parser_simple_type_specifier
1858   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1859 static tree cp_parser_type_name
1860   (cp_parser *);
1861 static tree cp_parser_nonclass_name 
1862   (cp_parser* parser);
1863 static tree cp_parser_elaborated_type_specifier
1864   (cp_parser *, bool, bool);
1865 static tree cp_parser_enum_specifier
1866   (cp_parser *);
1867 static void cp_parser_enumerator_list
1868   (cp_parser *, tree);
1869 static void cp_parser_enumerator_definition
1870   (cp_parser *, tree);
1871 static tree cp_parser_namespace_name
1872   (cp_parser *);
1873 static void cp_parser_namespace_definition
1874   (cp_parser *);
1875 static void cp_parser_namespace_body
1876   (cp_parser *);
1877 static tree cp_parser_qualified_namespace_specifier
1878   (cp_parser *);
1879 static void cp_parser_namespace_alias_definition
1880   (cp_parser *);
1881 static bool cp_parser_using_declaration
1882   (cp_parser *, bool);
1883 static void cp_parser_using_directive
1884   (cp_parser *);
1885 static void cp_parser_asm_definition
1886   (cp_parser *);
1887 static void cp_parser_linkage_specification
1888   (cp_parser *);
1889 static void cp_parser_static_assert
1890   (cp_parser *, bool);
1891 static tree cp_parser_decltype
1892   (cp_parser *);
1893
1894 /* Declarators [gram.dcl.decl] */
1895
1896 static tree cp_parser_init_declarator
1897   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1898 static cp_declarator *cp_parser_declarator
1899   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1900 static cp_declarator *cp_parser_direct_declarator
1901   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1902 static enum tree_code cp_parser_ptr_operator
1903   (cp_parser *, tree *, cp_cv_quals *);
1904 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1905   (cp_parser *);
1906 static tree cp_parser_late_return_type_opt
1907   (cp_parser *);
1908 static tree cp_parser_declarator_id
1909   (cp_parser *, bool);
1910 static tree cp_parser_type_id
1911   (cp_parser *);
1912 static tree cp_parser_template_type_arg
1913   (cp_parser *);
1914 static tree cp_parser_trailing_type_id (cp_parser *);
1915 static tree cp_parser_type_id_1
1916   (cp_parser *, bool, bool);
1917 static void cp_parser_type_specifier_seq
1918   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1919 static tree cp_parser_parameter_declaration_clause
1920   (cp_parser *);
1921 static tree cp_parser_parameter_declaration_list
1922   (cp_parser *, bool *);
1923 static cp_parameter_declarator *cp_parser_parameter_declaration
1924   (cp_parser *, bool, bool *);
1925 static tree cp_parser_default_argument 
1926   (cp_parser *, bool);
1927 static void cp_parser_function_body
1928   (cp_parser *);
1929 static tree cp_parser_initializer
1930   (cp_parser *, bool *, bool *);
1931 static tree cp_parser_initializer_clause
1932   (cp_parser *, bool *);
1933 static tree cp_parser_braced_list
1934   (cp_parser*, bool*);
1935 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1936   (cp_parser *, bool *);
1937
1938 static bool cp_parser_ctor_initializer_opt_and_function_body
1939   (cp_parser *);
1940
1941 /* Classes [gram.class] */
1942
1943 static tree cp_parser_class_name
1944   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1945 static tree cp_parser_class_specifier
1946   (cp_parser *);
1947 static tree cp_parser_class_head
1948   (cp_parser *, bool *, tree *, tree *);
1949 static enum tag_types cp_parser_class_key
1950   (cp_parser *);
1951 static void cp_parser_member_specification_opt
1952   (cp_parser *);
1953 static void cp_parser_member_declaration
1954   (cp_parser *);
1955 static tree cp_parser_pure_specifier
1956   (cp_parser *);
1957 static tree cp_parser_constant_initializer
1958   (cp_parser *);
1959
1960 /* Derived classes [gram.class.derived] */
1961
1962 static tree cp_parser_base_clause
1963   (cp_parser *);
1964 static tree cp_parser_base_specifier
1965   (cp_parser *);
1966
1967 /* Special member functions [gram.special] */
1968
1969 static tree cp_parser_conversion_function_id
1970   (cp_parser *);
1971 static tree cp_parser_conversion_type_id
1972   (cp_parser *);
1973 static cp_declarator *cp_parser_conversion_declarator_opt
1974   (cp_parser *);
1975 static bool cp_parser_ctor_initializer_opt
1976   (cp_parser *);
1977 static void cp_parser_mem_initializer_list
1978   (cp_parser *);
1979 static tree cp_parser_mem_initializer
1980   (cp_parser *);
1981 static tree cp_parser_mem_initializer_id
1982   (cp_parser *);
1983
1984 /* Overloading [gram.over] */
1985
1986 static tree cp_parser_operator_function_id
1987   (cp_parser *);
1988 static tree cp_parser_operator
1989   (cp_parser *);
1990
1991 /* Templates [gram.temp] */
1992
1993 static void cp_parser_template_declaration
1994   (cp_parser *, bool);
1995 static tree cp_parser_template_parameter_list
1996   (cp_parser *);
1997 static tree cp_parser_template_parameter
1998   (cp_parser *, bool *, bool *);
1999 static tree cp_parser_type_parameter
2000   (cp_parser *, bool *);
2001 static tree cp_parser_template_id
2002   (cp_parser *, bool, bool, bool);
2003 static tree cp_parser_template_name
2004   (cp_parser *, bool, bool, bool, bool *);
2005 static tree cp_parser_template_argument_list
2006   (cp_parser *);
2007 static tree cp_parser_template_argument
2008   (cp_parser *);
2009 static void cp_parser_explicit_instantiation
2010   (cp_parser *);
2011 static void cp_parser_explicit_specialization
2012   (cp_parser *);
2013
2014 /* Exception handling [gram.exception] */
2015
2016 static tree cp_parser_try_block
2017   (cp_parser *);
2018 static bool cp_parser_function_try_block
2019   (cp_parser *);
2020 static void cp_parser_handler_seq
2021   (cp_parser *);
2022 static void cp_parser_handler
2023   (cp_parser *);
2024 static tree cp_parser_exception_declaration
2025   (cp_parser *);
2026 static tree cp_parser_throw_expression
2027   (cp_parser *);
2028 static tree cp_parser_exception_specification_opt
2029   (cp_parser *);
2030 static tree cp_parser_type_id_list
2031   (cp_parser *);
2032
2033 /* GNU Extensions */
2034
2035 static tree cp_parser_asm_specification_opt
2036   (cp_parser *);
2037 static tree cp_parser_asm_operand_list
2038   (cp_parser *);
2039 static tree cp_parser_asm_clobber_list
2040   (cp_parser *);
2041 static tree cp_parser_asm_label_list
2042   (cp_parser *);
2043 static tree cp_parser_attributes_opt
2044   (cp_parser *);
2045 static tree cp_parser_attribute_list
2046   (cp_parser *);
2047 static bool cp_parser_extension_opt
2048   (cp_parser *, int *);
2049 static void cp_parser_label_declaration
2050   (cp_parser *);
2051
2052 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2053 static bool cp_parser_pragma
2054   (cp_parser *, enum pragma_context);
2055
2056 /* Objective-C++ Productions */
2057
2058 static tree cp_parser_objc_message_receiver
2059   (cp_parser *);
2060 static tree cp_parser_objc_message_args
2061   (cp_parser *);
2062 static tree cp_parser_objc_message_expression
2063   (cp_parser *);
2064 static tree cp_parser_objc_encode_expression
2065   (cp_parser *);
2066 static tree cp_parser_objc_defs_expression
2067   (cp_parser *);
2068 static tree cp_parser_objc_protocol_expression
2069   (cp_parser *);
2070 static tree cp_parser_objc_selector_expression
2071   (cp_parser *);
2072 static tree cp_parser_objc_expression
2073   (cp_parser *);
2074 static bool cp_parser_objc_selector_p
2075   (enum cpp_ttype);
2076 static tree cp_parser_objc_selector
2077   (cp_parser *);
2078 static tree cp_parser_objc_protocol_refs_opt
2079   (cp_parser *);
2080 static void cp_parser_objc_declaration
2081   (cp_parser *);
2082 static tree cp_parser_objc_statement
2083   (cp_parser *);
2084
2085 /* Utility Routines */
2086
2087 static tree cp_parser_lookup_name
2088   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2089 static tree cp_parser_lookup_name_simple
2090   (cp_parser *, tree, location_t);
2091 static tree cp_parser_maybe_treat_template_as_class
2092   (tree, bool);
2093 static bool cp_parser_check_declarator_template_parameters
2094   (cp_parser *, cp_declarator *, location_t);
2095 static bool cp_parser_check_template_parameters
2096   (cp_parser *, unsigned, location_t, cp_declarator *);
2097 static tree cp_parser_simple_cast_expression
2098   (cp_parser *);
2099 static tree cp_parser_global_scope_opt
2100   (cp_parser *, bool);
2101 static bool cp_parser_constructor_declarator_p
2102   (cp_parser *, bool);
2103 static tree cp_parser_function_definition_from_specifiers_and_declarator
2104   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2105 static tree cp_parser_function_definition_after_declarator
2106   (cp_parser *, bool);
2107 static void cp_parser_template_declaration_after_export
2108   (cp_parser *, bool);
2109 static void cp_parser_perform_template_parameter_access_checks
2110   (VEC (deferred_access_check,gc)*);
2111 static tree cp_parser_single_declaration
2112   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2113 static tree cp_parser_functional_cast
2114   (cp_parser *, tree);
2115 static tree cp_parser_save_member_function_body
2116   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2117 static tree cp_parser_enclosed_template_argument_list
2118   (cp_parser *);
2119 static void cp_parser_save_default_args
2120   (cp_parser *, tree);
2121 static void cp_parser_late_parsing_for_member
2122   (cp_parser *, tree);
2123 static void cp_parser_late_parsing_default_args
2124   (cp_parser *, tree);
2125 static tree cp_parser_sizeof_operand
2126   (cp_parser *, enum rid);
2127 static tree cp_parser_trait_expr
2128   (cp_parser *, enum rid);
2129 static bool cp_parser_declares_only_class_p
2130   (cp_parser *);
2131 static void cp_parser_set_storage_class
2132   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2133 static void cp_parser_set_decl_spec_type
2134   (cp_decl_specifier_seq *, tree, location_t, bool);
2135 static bool cp_parser_friend_p
2136   (const cp_decl_specifier_seq *);
2137 static void cp_parser_required_error
2138   (cp_parser *, required_token, bool);
2139 static cp_token *cp_parser_require
2140   (cp_parser *, enum cpp_ttype, required_token);
2141 static cp_token *cp_parser_require_keyword
2142   (cp_parser *, enum rid, required_token);
2143 static bool cp_parser_token_starts_function_definition_p
2144   (cp_token *);
2145 static bool cp_parser_next_token_starts_class_definition_p
2146   (cp_parser *);
2147 static bool cp_parser_next_token_ends_template_argument_p
2148   (cp_parser *);
2149 static bool cp_parser_nth_token_starts_template_argument_list_p
2150   (cp_parser *, size_t);
2151 static enum tag_types cp_parser_token_is_class_key
2152   (cp_token *);
2153 static void cp_parser_check_class_key
2154   (enum tag_types, tree type);
2155 static void cp_parser_check_access_in_redeclaration
2156   (tree type, location_t location);
2157 static bool cp_parser_optional_template_keyword
2158   (cp_parser *);
2159 static void cp_parser_pre_parsed_nested_name_specifier
2160   (cp_parser *);
2161 static bool cp_parser_cache_group
2162   (cp_parser *, enum cpp_ttype, unsigned);
2163 static void cp_parser_parse_tentatively
2164   (cp_parser *);
2165 static void cp_parser_commit_to_tentative_parse
2166   (cp_parser *);
2167 static void cp_parser_abort_tentative_parse
2168   (cp_parser *);
2169 static bool cp_parser_parse_definitely
2170   (cp_parser *);
2171 static inline bool cp_parser_parsing_tentatively
2172   (cp_parser *);
2173 static bool cp_parser_uncommitted_to_tentative_parse_p
2174   (cp_parser *);
2175 static void cp_parser_error
2176   (cp_parser *, const char *);
2177 static void cp_parser_name_lookup_error
2178   (cp_parser *, tree, tree, name_lookup_error, location_t);
2179 static bool cp_parser_simulate_error
2180   (cp_parser *);
2181 static bool cp_parser_check_type_definition
2182   (cp_parser *);
2183 static void cp_parser_check_for_definition_in_return_type
2184   (cp_declarator *, tree, location_t type_location);
2185 static void cp_parser_check_for_invalid_template_id
2186   (cp_parser *, tree, location_t location);
2187 static bool cp_parser_non_integral_constant_expression
2188   (cp_parser *, non_integral_constant);
2189 static void cp_parser_diagnose_invalid_type_name
2190   (cp_parser *, tree, tree, location_t);
2191 static bool cp_parser_parse_and_diagnose_invalid_type_name
2192   (cp_parser *);
2193 static int cp_parser_skip_to_closing_parenthesis
2194   (cp_parser *, bool, bool, bool);
2195 static void cp_parser_skip_to_end_of_statement
2196   (cp_parser *);
2197 static void cp_parser_consume_semicolon_at_end_of_statement
2198   (cp_parser *);
2199 static void cp_parser_skip_to_end_of_block_or_statement
2200   (cp_parser *);
2201 static bool cp_parser_skip_to_closing_brace
2202   (cp_parser *);
2203 static void cp_parser_skip_to_end_of_template_parameter_list
2204   (cp_parser *);
2205 static void cp_parser_skip_to_pragma_eol
2206   (cp_parser*, cp_token *);
2207 static bool cp_parser_error_occurred
2208   (cp_parser *);
2209 static bool cp_parser_allow_gnu_extensions_p
2210   (cp_parser *);
2211 static bool cp_parser_is_string_literal
2212   (cp_token *);
2213 static bool cp_parser_is_keyword
2214   (cp_token *, enum rid);
2215 static tree cp_parser_make_typename_type
2216   (cp_parser *, tree, tree, location_t location);
2217 static cp_declarator * cp_parser_make_indirect_declarator
2218   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2219
2220 /* Returns nonzero if we are parsing tentatively.  */
2221
2222 static inline bool
2223 cp_parser_parsing_tentatively (cp_parser* parser)
2224 {
2225   return parser->context->next != NULL;
2226 }
2227
2228 /* Returns nonzero if TOKEN is a string literal.  */
2229
2230 static bool
2231 cp_parser_is_string_literal (cp_token* token)
2232 {
2233   return (token->type == CPP_STRING ||
2234           token->type == CPP_STRING16 ||
2235           token->type == CPP_STRING32 ||
2236           token->type == CPP_WSTRING ||
2237           token->type == CPP_UTF8STRING);
2238 }
2239
2240 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2241
2242 static bool
2243 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2244 {
2245   return token->keyword == keyword;
2246 }
2247
2248 /* If not parsing tentatively, issue a diagnostic of the form
2249       FILE:LINE: MESSAGE before TOKEN
2250    where TOKEN is the next token in the input stream.  MESSAGE
2251    (specified by the caller) is usually of the form "expected
2252    OTHER-TOKEN".  */
2253
2254 static void
2255 cp_parser_error (cp_parser* parser, const char* gmsgid)
2256 {
2257   if (!cp_parser_simulate_error (parser))
2258     {
2259       cp_token *token = cp_lexer_peek_token (parser->lexer);
2260       /* This diagnostic makes more sense if it is tagged to the line
2261          of the token we just peeked at.  */
2262       cp_lexer_set_source_position_from_token (token);
2263
2264       if (token->type == CPP_PRAGMA)
2265         {
2266           error_at (token->location,
2267                     "%<#pragma%> is not allowed here");
2268           cp_parser_skip_to_pragma_eol (parser, token);
2269           return;
2270         }
2271
2272       c_parse_error (gmsgid,
2273                      /* Because c_parser_error does not understand
2274                         CPP_KEYWORD, keywords are treated like
2275                         identifiers.  */
2276                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2277                      token->u.value, token->flags);
2278     }
2279 }
2280
2281 /* Issue an error about name-lookup failing.  NAME is the
2282    IDENTIFIER_NODE DECL is the result of
2283    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2284    the thing that we hoped to find.  */
2285
2286 static void
2287 cp_parser_name_lookup_error (cp_parser* parser,
2288                              tree name,
2289                              tree decl,
2290                              name_lookup_error desired,
2291                              location_t location)
2292 {
2293   /* If name lookup completely failed, tell the user that NAME was not
2294      declared.  */
2295   if (decl == error_mark_node)
2296     {
2297       if (parser->scope && parser->scope != global_namespace)
2298         error_at (location, "%<%E::%E%> has not been declared",
2299                   parser->scope, name);
2300       else if (parser->scope == global_namespace)
2301         error_at (location, "%<::%E%> has not been declared", name);
2302       else if (parser->object_scope
2303                && !CLASS_TYPE_P (parser->object_scope))
2304         error_at (location, "request for member %qE in non-class type %qT",
2305                   name, parser->object_scope);
2306       else if (parser->object_scope)
2307         error_at (location, "%<%T::%E%> has not been declared",
2308                   parser->object_scope, name);
2309       else
2310         error_at (location, "%qE has not been declared", name);
2311     }
2312   else if (parser->scope && parser->scope != global_namespace)
2313     {
2314       switch (desired)
2315         {
2316           case NLE_TYPE:
2317             error_at (location, "%<%E::%E%> is not a type",
2318                                 parser->scope, name);
2319             break;
2320           case NLE_CXX98:
2321             error_at (location, "%<%E::%E%> is not a class or namespace",
2322                                 parser->scope, name);
2323             break;
2324           case NLE_NOT_CXX98:
2325             error_at (location,
2326                       "%<%E::%E%> is not a class, namespace, or enumeration",
2327                       parser->scope, name);
2328             break;
2329           default:
2330             gcc_unreachable ();
2331             
2332         }
2333     }
2334   else if (parser->scope == global_namespace)
2335     {
2336       switch (desired)
2337         {
2338           case NLE_TYPE:
2339             error_at (location, "%<::%E%> is not a type", name);
2340             break;
2341           case NLE_CXX98:
2342             error_at (location, "%<::%E%> is not a class or namespace", name);
2343             break;
2344           case NLE_NOT_CXX98:
2345             error_at (location,
2346                       "%<::%E%> is not a class, namespace, or enumeration",
2347                       name);
2348             break;
2349           default:
2350             gcc_unreachable ();
2351         }
2352     }
2353   else
2354     {
2355       switch (desired)
2356         {
2357           case NLE_TYPE:
2358             error_at (location, "%qE is not a type", name);
2359             break;
2360           case NLE_CXX98:
2361             error_at (location, "%qE is not a class or namespace", name);
2362             break;
2363           case NLE_NOT_CXX98:
2364             error_at (location,
2365                       "%qE is not a class, namespace, or enumeration", name);
2366             break;
2367           default:
2368             gcc_unreachable ();
2369         }
2370     }
2371 }
2372
2373 /* If we are parsing tentatively, remember that an error has occurred
2374    during this tentative parse.  Returns true if the error was
2375    simulated; false if a message should be issued by the caller.  */
2376
2377 static bool
2378 cp_parser_simulate_error (cp_parser* parser)
2379 {
2380   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2381     {
2382       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2383       return true;
2384     }
2385   return false;
2386 }
2387
2388 /* Check for repeated decl-specifiers.  */
2389
2390 static void
2391 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2392                            location_t location)
2393 {
2394   int ds;
2395
2396   for (ds = ds_first; ds != ds_last; ++ds)
2397     {
2398       unsigned count = decl_specs->specs[ds];
2399       if (count < 2)
2400         continue;
2401       /* The "long" specifier is a special case because of "long long".  */
2402       if (ds == ds_long)
2403         {
2404           if (count > 2)
2405             error_at (location, "%<long long long%> is too long for GCC");
2406           else 
2407             pedwarn_cxx98 (location, OPT_Wlong_long, 
2408                            "ISO C++ 1998 does not support %<long long%>");
2409         }
2410       else if (count > 1)
2411         {
2412           static const char *const decl_spec_names[] = {
2413             "signed",
2414             "unsigned",
2415             "short",
2416             "long",
2417             "const",
2418             "volatile",
2419             "restrict",
2420             "inline",
2421             "virtual",
2422             "explicit",
2423             "friend",
2424             "typedef",
2425             "constexpr",
2426             "__complex",
2427             "__thread"
2428           };
2429           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2430         }
2431     }
2432 }
2433
2434 /* This function is called when a type is defined.  If type
2435    definitions are forbidden at this point, an error message is
2436    issued.  */
2437
2438 static bool
2439 cp_parser_check_type_definition (cp_parser* parser)
2440 {
2441   /* If types are forbidden here, issue a message.  */
2442   if (parser->type_definition_forbidden_message)
2443     {
2444       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2445          in the message need to be interpreted.  */
2446       error (parser->type_definition_forbidden_message);
2447       return false;
2448     }
2449   return true;
2450 }
2451
2452 /* This function is called when the DECLARATOR is processed.  The TYPE
2453    was a type defined in the decl-specifiers.  If it is invalid to
2454    define a type in the decl-specifiers for DECLARATOR, an error is
2455    issued. TYPE_LOCATION is the location of TYPE and is used
2456    for error reporting.  */
2457
2458 static void
2459 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2460                                                tree type, location_t type_location)
2461 {
2462   /* [dcl.fct] forbids type definitions in return types.
2463      Unfortunately, it's not easy to know whether or not we are
2464      processing a return type until after the fact.  */
2465   while (declarator
2466          && (declarator->kind == cdk_pointer
2467              || declarator->kind == cdk_reference
2468              || declarator->kind == cdk_ptrmem))
2469     declarator = declarator->declarator;
2470   if (declarator
2471       && declarator->kind == cdk_function)
2472     {
2473       error_at (type_location,
2474                 "new types may not be defined in a return type");
2475       inform (type_location, 
2476               "(perhaps a semicolon is missing after the definition of %qT)",
2477               type);
2478     }
2479 }
2480
2481 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2482    "<" in any valid C++ program.  If the next token is indeed "<",
2483    issue a message warning the user about what appears to be an
2484    invalid attempt to form a template-id. LOCATION is the location
2485    of the type-specifier (TYPE) */
2486
2487 static void
2488 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2489                                          tree type, location_t location)
2490 {
2491   cp_token_position start = 0;
2492
2493   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2494     {
2495       if (TYPE_P (type))
2496         error_at (location, "%qT is not a template", type);
2497       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2498         error_at (location, "%qE is not a template", type);
2499       else
2500         error_at (location, "invalid template-id");
2501       /* Remember the location of the invalid "<".  */
2502       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2503         start = cp_lexer_token_position (parser->lexer, true);
2504       /* Consume the "<".  */
2505       cp_lexer_consume_token (parser->lexer);
2506       /* Parse the template arguments.  */
2507       cp_parser_enclosed_template_argument_list (parser);
2508       /* Permanently remove the invalid template arguments so that
2509          this error message is not issued again.  */
2510       if (start)
2511         cp_lexer_purge_tokens_after (parser->lexer, start);
2512     }
2513 }
2514
2515 /* If parsing an integral constant-expression, issue an error message
2516    about the fact that THING appeared and return true.  Otherwise,
2517    return false.  In either case, set
2518    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2519
2520 static bool
2521 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2522                                             non_integral_constant thing)
2523 {
2524   parser->non_integral_constant_expression_p = true;
2525   if (parser->integral_constant_expression_p)
2526     {
2527       if (!parser->allow_non_integral_constant_expression_p)
2528         {
2529           const char *msg = NULL;
2530           switch (thing)
2531             {
2532               case NIC_FLOAT:
2533                 error ("floating-point literal "
2534                        "cannot appear in a constant-expression");
2535                 return true;
2536               case NIC_CAST:
2537                 error ("a cast to a type other than an integral or "
2538                        "enumeration type cannot appear in a "
2539                        "constant-expression");
2540                 return true;
2541               case NIC_TYPEID:
2542                 error ("%<typeid%> operator "
2543                        "cannot appear in a constant-expression");
2544                 return true;
2545               case NIC_NCC:
2546                 error ("non-constant compound literals "
2547                        "cannot appear in a constant-expression");
2548                 return true;
2549               case NIC_FUNC_CALL:
2550                 error ("a function call "
2551                        "cannot appear in a constant-expression");
2552                 return true;
2553               case NIC_INC:
2554                 error ("an increment "
2555                        "cannot appear in a constant-expression");
2556                 return true;
2557               case NIC_DEC:
2558                 error ("an decrement "
2559                        "cannot appear in a constant-expression");
2560                 return true;
2561               case NIC_ARRAY_REF:
2562                 error ("an array reference "
2563                        "cannot appear in a constant-expression");
2564                 return true;
2565               case NIC_ADDR_LABEL:
2566                 error ("the address of a label "
2567                        "cannot appear in a constant-expression");
2568                 return true;
2569               case NIC_OVERLOADED:
2570                 error ("calls to overloaded operators "
2571                        "cannot appear in a constant-expression");
2572                 return true;
2573               case NIC_ASSIGNMENT:
2574                 error ("an assignment cannot appear in a constant-expression");
2575                 return true;
2576               case NIC_COMMA:
2577                 error ("a comma operator "
2578                        "cannot appear in a constant-expression");
2579                 return true;
2580               case NIC_CONSTRUCTOR:
2581                 error ("a call to a constructor "
2582                        "cannot appear in a constant-expression");
2583                 return true;
2584               case NIC_THIS:
2585                 msg = "this";
2586                 break;
2587               case NIC_FUNC_NAME:
2588                 msg = "__FUNCTION__";
2589                 break;
2590               case NIC_PRETTY_FUNC:
2591                 msg = "__PRETTY_FUNCTION__";
2592                 break;
2593               case NIC_C99_FUNC:
2594                 msg = "__func__";
2595                 break;
2596               case NIC_VA_ARG:
2597                 msg = "va_arg";
2598                 break;
2599               case NIC_ARROW:
2600                 msg = "->";
2601                 break;
2602               case NIC_POINT:
2603                 msg = ".";
2604                 break;
2605               case NIC_STAR:
2606                 msg = "*";
2607                 break;
2608               case NIC_ADDR:
2609                 msg = "&";
2610                 break;
2611               case NIC_PREINCREMENT:
2612                 msg = "++";
2613                 break;
2614               case NIC_PREDECREMENT:
2615                 msg = "--";
2616                 break;
2617               case NIC_NEW:
2618                 msg = "new";
2619                 break;
2620               case NIC_DEL:
2621                 msg = "delete";
2622                 break;
2623               default:
2624                 gcc_unreachable ();
2625             }
2626           if (msg)
2627             error ("%qs cannot appear in a constant-expression", msg);
2628           return true;
2629         }
2630     }
2631   return false;
2632 }
2633
2634 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2635    qualifying scope (or NULL, if none) for ID.  This function commits
2636    to the current active tentative parse, if any.  (Otherwise, the
2637    problematic construct might be encountered again later, resulting
2638    in duplicate error messages.) LOCATION is the location of ID.  */
2639
2640 static void
2641 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2642                                       tree scope, tree id,
2643                                       location_t location)
2644 {
2645   tree decl, old_scope;
2646   /* Try to lookup the identifier.  */
2647   old_scope = parser->scope;
2648   parser->scope = scope;
2649   decl = cp_parser_lookup_name_simple (parser, id, location);
2650   parser->scope = old_scope;
2651   /* If the lookup found a template-name, it means that the user forgot
2652   to specify an argument list. Emit a useful error message.  */
2653   if (TREE_CODE (decl) == TEMPLATE_DECL)
2654     error_at (location,
2655               "invalid use of template-name %qE without an argument list",
2656               decl);
2657   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2658     error_at (location, "invalid use of destructor %qD as a type", id);
2659   else if (TREE_CODE (decl) == TYPE_DECL)
2660     /* Something like 'unsigned A a;'  */
2661     error_at (location, "invalid combination of multiple type-specifiers");
2662   else if (!parser->scope)
2663     {
2664       /* Issue an error message.  */
2665       error_at (location, "%qE does not name a type", id);
2666       /* If we're in a template class, it's possible that the user was
2667          referring to a type from a base class.  For example:
2668
2669            template <typename T> struct A { typedef T X; };
2670            template <typename T> struct B : public A<T> { X x; };
2671
2672          The user should have said "typename A<T>::X".  */
2673       if (processing_template_decl && current_class_type
2674           && TYPE_BINFO (current_class_type))
2675         {
2676           tree b;
2677
2678           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2679                b;
2680                b = TREE_CHAIN (b))
2681             {
2682               tree base_type = BINFO_TYPE (b);
2683               if (CLASS_TYPE_P (base_type)
2684                   && dependent_type_p (base_type))
2685                 {
2686                   tree field;
2687                   /* Go from a particular instantiation of the
2688                      template (which will have an empty TYPE_FIELDs),
2689                      to the main version.  */
2690                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2691                   for (field = TYPE_FIELDS (base_type);
2692                        field;
2693                        field = DECL_CHAIN (field))
2694                     if (TREE_CODE (field) == TYPE_DECL
2695                         && DECL_NAME (field) == id)
2696                       {
2697                         inform (location, 
2698                                 "(perhaps %<typename %T::%E%> was intended)",
2699                                 BINFO_TYPE (b), id);
2700                         break;
2701                       }
2702                   if (field)
2703                     break;
2704                 }
2705             }
2706         }
2707     }
2708   /* Here we diagnose qualified-ids where the scope is actually correct,
2709      but the identifier does not resolve to a valid type name.  */
2710   else if (parser->scope != error_mark_node)
2711     {
2712       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2713         error_at (location, "%qE in namespace %qE does not name a type",
2714                   id, parser->scope);
2715       else if (CLASS_TYPE_P (parser->scope)
2716                && constructor_name_p (id, parser->scope))
2717         {
2718           /* A<T>::A<T>() */
2719           error_at (location, "%<%T::%E%> names the constructor, not"
2720                     " the type", parser->scope, id);
2721           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2722             error_at (location, "and %qT has no template constructors",
2723                       parser->scope);
2724         }
2725       else if (TYPE_P (parser->scope)
2726                && dependent_scope_p (parser->scope))
2727         error_at (location, "need %<typename%> before %<%T::%E%> because "
2728                   "%qT is a dependent scope",
2729                   parser->scope, id, parser->scope);
2730       else if (TYPE_P (parser->scope))
2731         error_at (location, "%qE in class %qT does not name a type",
2732                   id, parser->scope);
2733       else
2734         gcc_unreachable ();
2735     }
2736   cp_parser_commit_to_tentative_parse (parser);
2737 }
2738
2739 /* Check for a common situation where a type-name should be present,
2740    but is not, and issue a sensible error message.  Returns true if an
2741    invalid type-name was detected.
2742
2743    The situation handled by this function are variable declarations of the
2744    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2745    Usually, `ID' should name a type, but if we got here it means that it
2746    does not. We try to emit the best possible error message depending on
2747    how exactly the id-expression looks like.  */
2748
2749 static bool
2750 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2751 {
2752   tree id;
2753   cp_token *token = cp_lexer_peek_token (parser->lexer);
2754
2755   /* Avoid duplicate error about ambiguous lookup.  */
2756   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2757     {
2758       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2759       if (next->type == CPP_NAME && next->ambiguous_p)
2760         goto out;
2761     }
2762
2763   cp_parser_parse_tentatively (parser);
2764   id = cp_parser_id_expression (parser,
2765                                 /*template_keyword_p=*/false,
2766                                 /*check_dependency_p=*/true,
2767                                 /*template_p=*/NULL,
2768                                 /*declarator_p=*/true,
2769                                 /*optional_p=*/false);
2770   /* If the next token is a (, this is a function with no explicit return
2771      type, i.e. constructor, destructor or conversion op.  */
2772   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2773       || TREE_CODE (id) == TYPE_DECL)
2774     {
2775       cp_parser_abort_tentative_parse (parser);
2776       return false;
2777     }
2778   if (!cp_parser_parse_definitely (parser))
2779     return false;
2780
2781   /* Emit a diagnostic for the invalid type.  */
2782   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2783                                         id, token->location);
2784  out:
2785   /* If we aren't in the middle of a declarator (i.e. in a
2786      parameter-declaration-clause), skip to the end of the declaration;
2787      there's no point in trying to process it.  */
2788   if (!parser->in_declarator_p)
2789     cp_parser_skip_to_end_of_block_or_statement (parser);
2790   return true;
2791 }
2792
2793 /* Consume tokens up to, and including, the next non-nested closing `)'.
2794    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2795    are doing error recovery. Returns -1 if OR_COMMA is true and we
2796    found an unnested comma.  */
2797
2798 static int
2799 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2800                                        bool recovering,
2801                                        bool or_comma,
2802                                        bool consume_paren)
2803 {
2804   unsigned paren_depth = 0;
2805   unsigned brace_depth = 0;
2806   unsigned square_depth = 0;
2807
2808   if (recovering && !or_comma
2809       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2810     return 0;
2811
2812   while (true)
2813     {
2814       cp_token * token = cp_lexer_peek_token (parser->lexer);
2815
2816       switch (token->type)
2817         {
2818         case CPP_EOF:
2819         case CPP_PRAGMA_EOL:
2820           /* If we've run out of tokens, then there is no closing `)'.  */
2821           return 0;
2822
2823         /* This is good for lambda expression capture-lists.  */
2824         case CPP_OPEN_SQUARE:
2825           ++square_depth;
2826           break;
2827         case CPP_CLOSE_SQUARE:
2828           if (!square_depth--)
2829             return 0;
2830           break;
2831
2832         case CPP_SEMICOLON:
2833           /* This matches the processing in skip_to_end_of_statement.  */
2834           if (!brace_depth)
2835             return 0;
2836           break;
2837
2838         case CPP_OPEN_BRACE:
2839           ++brace_depth;
2840           break;
2841         case CPP_CLOSE_BRACE:
2842           if (!brace_depth--)
2843             return 0;
2844           break;
2845
2846         case CPP_COMMA:
2847           if (recovering && or_comma && !brace_depth && !paren_depth
2848               && !square_depth)
2849             return -1;
2850           break;
2851
2852         case CPP_OPEN_PAREN:
2853           if (!brace_depth)
2854             ++paren_depth;
2855           break;
2856
2857         case CPP_CLOSE_PAREN:
2858           if (!brace_depth && !paren_depth--)
2859             {
2860               if (consume_paren)
2861                 cp_lexer_consume_token (parser->lexer);
2862               return 1;
2863             }
2864           break;
2865
2866         default:
2867           break;
2868         }
2869
2870       /* Consume the token.  */
2871       cp_lexer_consume_token (parser->lexer);
2872     }
2873 }
2874
2875 /* Consume tokens until we reach the end of the current statement.
2876    Normally, that will be just before consuming a `;'.  However, if a
2877    non-nested `}' comes first, then we stop before consuming that.  */
2878
2879 static void
2880 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2881 {
2882   unsigned nesting_depth = 0;
2883
2884   while (true)
2885     {
2886       cp_token *token = cp_lexer_peek_token (parser->lexer);
2887
2888       switch (token->type)
2889         {
2890         case CPP_EOF:
2891         case CPP_PRAGMA_EOL:
2892           /* If we've run out of tokens, stop.  */
2893           return;
2894
2895         case CPP_SEMICOLON:
2896           /* If the next token is a `;', we have reached the end of the
2897              statement.  */
2898           if (!nesting_depth)
2899             return;
2900           break;
2901
2902         case CPP_CLOSE_BRACE:
2903           /* If this is a non-nested '}', stop before consuming it.
2904              That way, when confronted with something like:
2905
2906                { 3 + }
2907
2908              we stop before consuming the closing '}', even though we
2909              have not yet reached a `;'.  */
2910           if (nesting_depth == 0)
2911             return;
2912
2913           /* If it is the closing '}' for a block that we have
2914              scanned, stop -- but only after consuming the token.
2915              That way given:
2916
2917                 void f g () { ... }
2918                 typedef int I;
2919
2920              we will stop after the body of the erroneously declared
2921              function, but before consuming the following `typedef'
2922              declaration.  */
2923           if (--nesting_depth == 0)
2924             {
2925               cp_lexer_consume_token (parser->lexer);
2926               return;
2927             }
2928
2929         case CPP_OPEN_BRACE:
2930           ++nesting_depth;
2931           break;
2932
2933         default:
2934           break;
2935         }
2936
2937       /* Consume the token.  */
2938       cp_lexer_consume_token (parser->lexer);
2939     }
2940 }
2941
2942 /* This function is called at the end of a statement or declaration.
2943    If the next token is a semicolon, it is consumed; otherwise, error
2944    recovery is attempted.  */
2945
2946 static void
2947 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2948 {
2949   /* Look for the trailing `;'.  */
2950   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2951     {
2952       /* If there is additional (erroneous) input, skip to the end of
2953          the statement.  */
2954       cp_parser_skip_to_end_of_statement (parser);
2955       /* If the next token is now a `;', consume it.  */
2956       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2957         cp_lexer_consume_token (parser->lexer);
2958     }
2959 }
2960
2961 /* Skip tokens until we have consumed an entire block, or until we
2962    have consumed a non-nested `;'.  */
2963
2964 static void
2965 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2966 {
2967   int nesting_depth = 0;
2968
2969   while (nesting_depth >= 0)
2970     {
2971       cp_token *token = cp_lexer_peek_token (parser->lexer);
2972
2973       switch (token->type)
2974         {
2975         case CPP_EOF:
2976         case CPP_PRAGMA_EOL:
2977           /* If we've run out of tokens, stop.  */
2978           return;
2979
2980         case CPP_SEMICOLON:
2981           /* Stop if this is an unnested ';'. */
2982           if (!nesting_depth)
2983             nesting_depth = -1;
2984           break;
2985
2986         case CPP_CLOSE_BRACE:
2987           /* Stop if this is an unnested '}', or closes the outermost
2988              nesting level.  */
2989           nesting_depth--;
2990           if (nesting_depth < 0)
2991             return;
2992           if (!nesting_depth)
2993             nesting_depth = -1;
2994           break;
2995
2996         case CPP_OPEN_BRACE:
2997           /* Nest. */
2998           nesting_depth++;
2999           break;
3000
3001         default:
3002           break;
3003         }
3004
3005       /* Consume the token.  */
3006       cp_lexer_consume_token (parser->lexer);
3007     }
3008 }
3009
3010 /* Skip tokens until a non-nested closing curly brace is the next
3011    token, or there are no more tokens. Return true in the first case,
3012    false otherwise.  */
3013
3014 static bool
3015 cp_parser_skip_to_closing_brace (cp_parser *parser)
3016 {
3017   unsigned nesting_depth = 0;
3018
3019   while (true)
3020     {
3021       cp_token *token = cp_lexer_peek_token (parser->lexer);
3022
3023       switch (token->type)
3024         {
3025         case CPP_EOF:
3026         case CPP_PRAGMA_EOL:
3027           /* If we've run out of tokens, stop.  */
3028           return false;
3029
3030         case CPP_CLOSE_BRACE:
3031           /* If the next token is a non-nested `}', then we have reached
3032              the end of the current block.  */
3033           if (nesting_depth-- == 0)
3034             return true;
3035           break;
3036
3037         case CPP_OPEN_BRACE:
3038           /* If it the next token is a `{', then we are entering a new
3039              block.  Consume the entire block.  */
3040           ++nesting_depth;
3041           break;
3042
3043         default:
3044           break;
3045         }
3046
3047       /* Consume the token.  */
3048       cp_lexer_consume_token (parser->lexer);
3049     }
3050 }
3051
3052 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3053    parameter is the PRAGMA token, allowing us to purge the entire pragma
3054    sequence.  */
3055
3056 static void
3057 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3058 {
3059   cp_token *token;
3060
3061   parser->lexer->in_pragma = false;
3062
3063   do
3064     token = cp_lexer_consume_token (parser->lexer);
3065   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3066
3067   /* Ensure that the pragma is not parsed again.  */
3068   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3069 }
3070
3071 /* Require pragma end of line, resyncing with it as necessary.  The
3072    arguments are as for cp_parser_skip_to_pragma_eol.  */
3073
3074 static void
3075 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3076 {
3077   parser->lexer->in_pragma = false;
3078   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3079     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3080 }
3081
3082 /* This is a simple wrapper around make_typename_type. When the id is
3083    an unresolved identifier node, we can provide a superior diagnostic
3084    using cp_parser_diagnose_invalid_type_name.  */
3085
3086 static tree
3087 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3088                               tree id, location_t id_location)
3089 {
3090   tree result;
3091   if (TREE_CODE (id) == IDENTIFIER_NODE)
3092     {
3093       result = make_typename_type (scope, id, typename_type,
3094                                    /*complain=*/tf_none);
3095       if (result == error_mark_node)
3096         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3097       return result;
3098     }
3099   return make_typename_type (scope, id, typename_type, tf_error);
3100 }
3101
3102 /* This is a wrapper around the
3103    make_{pointer,ptrmem,reference}_declarator functions that decides
3104    which one to call based on the CODE and CLASS_TYPE arguments. The
3105    CODE argument should be one of the values returned by
3106    cp_parser_ptr_operator. */
3107 static cp_declarator *
3108 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3109                                     cp_cv_quals cv_qualifiers,
3110                                     cp_declarator *target)
3111 {
3112   if (code == ERROR_MARK)
3113     return cp_error_declarator;
3114
3115   if (code == INDIRECT_REF)
3116     if (class_type == NULL_TREE)
3117       return make_pointer_declarator (cv_qualifiers, target);
3118     else
3119       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3120   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3121     return make_reference_declarator (cv_qualifiers, target, false);
3122   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3123     return make_reference_declarator (cv_qualifiers, target, true);
3124   gcc_unreachable ();
3125 }
3126
3127 /* Create a new C++ parser.  */
3128
3129 static cp_parser *
3130 cp_parser_new (void)
3131 {
3132   cp_parser *parser;
3133   cp_lexer *lexer;
3134   unsigned i;
3135
3136   /* cp_lexer_new_main is called before doing GC allocation because
3137      cp_lexer_new_main might load a PCH file.  */
3138   lexer = cp_lexer_new_main ();
3139
3140   /* Initialize the binops_by_token so that we can get the tree
3141      directly from the token.  */
3142   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3143     binops_by_token[binops[i].token_type] = binops[i];
3144
3145   parser = ggc_alloc_cleared_cp_parser ();
3146   parser->lexer = lexer;
3147   parser->context = cp_parser_context_new (NULL);
3148
3149   /* For now, we always accept GNU extensions.  */
3150   parser->allow_gnu_extensions_p = 1;
3151
3152   /* The `>' token is a greater-than operator, not the end of a
3153      template-id.  */
3154   parser->greater_than_is_operator_p = true;
3155
3156   parser->default_arg_ok_p = true;
3157
3158   /* We are not parsing a constant-expression.  */
3159   parser->integral_constant_expression_p = false;
3160   parser->allow_non_integral_constant_expression_p = false;
3161   parser->non_integral_constant_expression_p = false;
3162
3163   /* Local variable names are not forbidden.  */
3164   parser->local_variables_forbidden_p = false;
3165
3166   /* We are not processing an `extern "C"' declaration.  */
3167   parser->in_unbraced_linkage_specification_p = false;
3168
3169   /* We are not processing a declarator.  */
3170   parser->in_declarator_p = false;
3171
3172   /* We are not processing a template-argument-list.  */
3173   parser->in_template_argument_list_p = false;
3174
3175   /* We are not in an iteration statement.  */
3176   parser->in_statement = 0;
3177
3178   /* We are not in a switch statement.  */
3179   parser->in_switch_statement_p = false;
3180
3181   /* We are not parsing a type-id inside an expression.  */
3182   parser->in_type_id_in_expr_p = false;
3183
3184   /* Declarations aren't implicitly extern "C".  */
3185   parser->implicit_extern_c = false;
3186
3187   /* String literals should be translated to the execution character set.  */
3188   parser->translate_strings_p = true;
3189
3190   /* We are not parsing a function body.  */
3191   parser->in_function_body = false;
3192
3193   /* The unparsed function queue is empty.  */
3194   push_unparsed_function_queues (parser);
3195
3196   /* There are no classes being defined.  */
3197   parser->num_classes_being_defined = 0;
3198
3199   /* No template parameters apply.  */
3200   parser->num_template_parameter_lists = 0;
3201
3202   return parser;
3203 }
3204
3205 /* Create a cp_lexer structure which will emit the tokens in CACHE
3206    and push it onto the parser's lexer stack.  This is used for delayed
3207    parsing of in-class method bodies and default arguments, and should
3208    not be confused with tentative parsing.  */
3209 static void
3210 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3211 {
3212   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3213   lexer->next = parser->lexer;
3214   parser->lexer = lexer;
3215
3216   /* Move the current source position to that of the first token in the
3217      new lexer.  */
3218   cp_lexer_set_source_position_from_token (lexer->next_token);
3219 }
3220
3221 /* Pop the top lexer off the parser stack.  This is never used for the
3222    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3223 static void
3224 cp_parser_pop_lexer (cp_parser *parser)
3225 {
3226   cp_lexer *lexer = parser->lexer;
3227   parser->lexer = lexer->next;
3228   cp_lexer_destroy (lexer);
3229
3230   /* Put the current source position back where it was before this
3231      lexer was pushed.  */
3232   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3233 }
3234
3235 /* Lexical conventions [gram.lex]  */
3236
3237 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3238    identifier.  */
3239
3240 static tree
3241 cp_parser_identifier (cp_parser* parser)
3242 {
3243   cp_token *token;
3244
3245   /* Look for the identifier.  */
3246   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3247   /* Return the value.  */
3248   return token ? token->u.value : error_mark_node;
3249 }
3250
3251 /* Parse a sequence of adjacent string constants.  Returns a
3252    TREE_STRING representing the combined, nul-terminated string
3253    constant.  If TRANSLATE is true, translate the string to the
3254    execution character set.  If WIDE_OK is true, a wide string is
3255    invalid here.
3256
3257    C++98 [lex.string] says that if a narrow string literal token is
3258    adjacent to a wide string literal token, the behavior is undefined.
3259    However, C99 6.4.5p4 says that this results in a wide string literal.
3260    We follow C99 here, for consistency with the C front end.
3261
3262    This code is largely lifted from lex_string() in c-lex.c.
3263
3264    FUTURE: ObjC++ will need to handle @-strings here.  */
3265 static tree
3266 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3267 {
3268   tree value;
3269   size_t count;
3270   struct obstack str_ob;
3271   cpp_string str, istr, *strs;
3272   cp_token *tok;
3273   enum cpp_ttype type;
3274
3275   tok = cp_lexer_peek_token (parser->lexer);
3276   if (!cp_parser_is_string_literal (tok))
3277     {
3278       cp_parser_error (parser, "expected string-literal");
3279       return error_mark_node;
3280     }
3281
3282   type = tok->type;
3283
3284   /* Try to avoid the overhead of creating and destroying an obstack
3285      for the common case of just one string.  */
3286   if (!cp_parser_is_string_literal
3287       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3288     {
3289       cp_lexer_consume_token (parser->lexer);
3290
3291       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3292       str.len = TREE_STRING_LENGTH (tok->u.value);
3293       count = 1;
3294
3295       strs = &str;
3296     }
3297   else
3298     {
3299       gcc_obstack_init (&str_ob);
3300       count = 0;
3301
3302       do
3303         {
3304           cp_lexer_consume_token (parser->lexer);
3305           count++;
3306           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3307           str.len = TREE_STRING_LENGTH (tok->u.value);
3308
3309           if (type != tok->type)
3310             {
3311               if (type == CPP_STRING)
3312                 type = tok->type;
3313               else if (tok->type != CPP_STRING)
3314                 error_at (tok->location,
3315                           "unsupported non-standard concatenation "
3316                           "of string literals");
3317             }
3318
3319           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3320
3321           tok = cp_lexer_peek_token (parser->lexer);
3322         }
3323       while (cp_parser_is_string_literal (tok));
3324
3325       strs = (cpp_string *) obstack_finish (&str_ob);
3326     }
3327
3328   if (type != CPP_STRING && !wide_ok)
3329     {
3330       cp_parser_error (parser, "a wide string is invalid in this context");
3331       type = CPP_STRING;
3332     }
3333
3334   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3335       (parse_in, strs, count, &istr, type))
3336     {
3337       value = build_string (istr.len, (const char *)istr.text);
3338       free (CONST_CAST (unsigned char *, istr.text));
3339
3340       switch (type)
3341         {
3342         default:
3343         case CPP_STRING:
3344         case CPP_UTF8STRING:
3345           TREE_TYPE (value) = char_array_type_node;
3346           break;
3347         case CPP_STRING16:
3348           TREE_TYPE (value) = char16_array_type_node;
3349           break;
3350         case CPP_STRING32:
3351           TREE_TYPE (value) = char32_array_type_node;
3352           break;
3353         case CPP_WSTRING:
3354           TREE_TYPE (value) = wchar_array_type_node;
3355           break;
3356         }
3357
3358       value = fix_string_type (value);
3359     }
3360   else
3361     /* cpp_interpret_string has issued an error.  */
3362     value = error_mark_node;
3363
3364   if (count > 1)
3365     obstack_free (&str_ob, 0);
3366
3367   return value;
3368 }
3369
3370
3371 /* Basic concepts [gram.basic]  */
3372
3373 /* Parse a translation-unit.
3374
3375    translation-unit:
3376      declaration-seq [opt]
3377
3378    Returns TRUE if all went well.  */
3379
3380 static bool
3381 cp_parser_translation_unit (cp_parser* parser)
3382 {
3383   /* The address of the first non-permanent object on the declarator
3384      obstack.  */
3385   static void *declarator_obstack_base;
3386
3387   bool success;
3388
3389   /* Create the declarator obstack, if necessary.  */
3390   if (!cp_error_declarator)
3391     {
3392       gcc_obstack_init (&declarator_obstack);
3393       /* Create the error declarator.  */
3394       cp_error_declarator = make_declarator (cdk_error);
3395       /* Create the empty parameter list.  */
3396       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3397       /* Remember where the base of the declarator obstack lies.  */
3398       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3399     }
3400
3401   cp_parser_declaration_seq_opt (parser);
3402
3403   /* If there are no tokens left then all went well.  */
3404   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3405     {
3406       /* Get rid of the token array; we don't need it any more.  */
3407       cp_lexer_destroy (parser->lexer);
3408       parser->lexer = NULL;
3409
3410       /* This file might have been a context that's implicitly extern
3411          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3412       if (parser->implicit_extern_c)
3413         {
3414           pop_lang_context ();
3415           parser->implicit_extern_c = false;
3416         }
3417
3418       /* Finish up.  */
3419       finish_translation_unit ();
3420
3421       success = true;
3422     }
3423   else
3424     {
3425       cp_parser_error (parser, "expected declaration");
3426       success = false;
3427     }
3428
3429   /* Make sure the declarator obstack was fully cleaned up.  */
3430   gcc_assert (obstack_next_free (&declarator_obstack)
3431               == declarator_obstack_base);
3432
3433   /* All went well.  */
3434   return success;
3435 }
3436
3437 /* Expressions [gram.expr] */
3438
3439 /* Parse a primary-expression.
3440
3441    primary-expression:
3442      literal
3443      this
3444      ( expression )
3445      id-expression
3446
3447    GNU Extensions:
3448
3449    primary-expression:
3450      ( compound-statement )
3451      __builtin_va_arg ( assignment-expression , type-id )
3452      __builtin_offsetof ( type-id , offsetof-expression )
3453
3454    C++ Extensions:
3455      __has_nothrow_assign ( type-id )   
3456      __has_nothrow_constructor ( type-id )
3457      __has_nothrow_copy ( type-id )
3458      __has_trivial_assign ( type-id )   
3459      __has_trivial_constructor ( type-id )
3460      __has_trivial_copy ( type-id )
3461      __has_trivial_destructor ( type-id )
3462      __has_virtual_destructor ( type-id )     
3463      __is_abstract ( type-id )
3464      __is_base_of ( type-id , type-id )
3465      __is_class ( type-id )
3466      __is_convertible_to ( type-id , type-id )     
3467      __is_empty ( type-id )
3468      __is_enum ( type-id )
3469      __is_pod ( type-id )
3470      __is_polymorphic ( type-id )
3471      __is_union ( type-id )
3472
3473    Objective-C++ Extension:
3474
3475    primary-expression:
3476      objc-expression
3477
3478    literal:
3479      __null
3480
3481    ADDRESS_P is true iff this expression was immediately preceded by
3482    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3483    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3484    true iff this expression is a template argument.
3485
3486    Returns a representation of the expression.  Upon return, *IDK
3487    indicates what kind of id-expression (if any) was present.  */
3488
3489 static tree
3490 cp_parser_primary_expression (cp_parser *parser,
3491                               bool address_p,
3492                               bool cast_p,
3493                               bool template_arg_p,
3494                               cp_id_kind *idk)
3495 {
3496   cp_token *token = NULL;
3497
3498   /* Assume the primary expression is not an id-expression.  */
3499   *idk = CP_ID_KIND_NONE;
3500
3501   /* Peek at the next token.  */
3502   token = cp_lexer_peek_token (parser->lexer);
3503   switch (token->type)
3504     {
3505       /* literal:
3506            integer-literal
3507            character-literal
3508            floating-literal
3509            string-literal
3510            boolean-literal  */
3511     case CPP_CHAR:
3512     case CPP_CHAR16:
3513     case CPP_CHAR32:
3514     case CPP_WCHAR:
3515     case CPP_NUMBER:
3516       token = cp_lexer_consume_token (parser->lexer);
3517       if (TREE_CODE (token->u.value) == FIXED_CST)
3518         {
3519           error_at (token->location,
3520                     "fixed-point types not supported in C++");
3521           return error_mark_node;
3522         }
3523       /* Floating-point literals are only allowed in an integral
3524          constant expression if they are cast to an integral or
3525          enumeration type.  */
3526       if (TREE_CODE (token->u.value) == REAL_CST
3527           && parser->integral_constant_expression_p
3528           && pedantic)
3529         {
3530           /* CAST_P will be set even in invalid code like "int(2.7 +
3531              ...)".   Therefore, we have to check that the next token
3532              is sure to end the cast.  */
3533           if (cast_p)
3534             {
3535               cp_token *next_token;
3536
3537               next_token = cp_lexer_peek_token (parser->lexer);
3538               if (/* The comma at the end of an
3539                      enumerator-definition.  */
3540                   next_token->type != CPP_COMMA
3541                   /* The curly brace at the end of an enum-specifier.  */
3542                   && next_token->type != CPP_CLOSE_BRACE
3543                   /* The end of a statement.  */
3544                   && next_token->type != CPP_SEMICOLON
3545                   /* The end of the cast-expression.  */
3546                   && next_token->type != CPP_CLOSE_PAREN
3547                   /* The end of an array bound.  */
3548                   && next_token->type != CPP_CLOSE_SQUARE
3549                   /* The closing ">" in a template-argument-list.  */
3550                   && (next_token->type != CPP_GREATER
3551                       || parser->greater_than_is_operator_p)
3552                   /* C++0x only: A ">>" treated like two ">" tokens,
3553                      in a template-argument-list.  */
3554                   && (next_token->type != CPP_RSHIFT
3555                       || (cxx_dialect == cxx98)
3556                       || parser->greater_than_is_operator_p))
3557                 cast_p = false;
3558             }
3559
3560           /* If we are within a cast, then the constraint that the
3561              cast is to an integral or enumeration type will be
3562              checked at that point.  If we are not within a cast, then
3563              this code is invalid.  */
3564           if (!cast_p)
3565             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3566         }
3567       return token->u.value;
3568
3569     case CPP_STRING:
3570     case CPP_STRING16:
3571     case CPP_STRING32:
3572     case CPP_WSTRING:
3573     case CPP_UTF8STRING:
3574       /* ??? Should wide strings be allowed when parser->translate_strings_p
3575          is false (i.e. in attributes)?  If not, we can kill the third
3576          argument to cp_parser_string_literal.  */
3577       return cp_parser_string_literal (parser,
3578                                        parser->translate_strings_p,
3579                                        true);
3580
3581     case CPP_OPEN_PAREN:
3582       {
3583         tree expr;
3584         bool saved_greater_than_is_operator_p;
3585
3586         /* Consume the `('.  */
3587         cp_lexer_consume_token (parser->lexer);
3588         /* Within a parenthesized expression, a `>' token is always
3589            the greater-than operator.  */
3590         saved_greater_than_is_operator_p
3591           = parser->greater_than_is_operator_p;
3592         parser->greater_than_is_operator_p = true;
3593         /* If we see `( { ' then we are looking at the beginning of
3594            a GNU statement-expression.  */
3595         if (cp_parser_allow_gnu_extensions_p (parser)
3596             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3597           {
3598             /* Statement-expressions are not allowed by the standard.  */
3599             pedwarn (token->location, OPT_pedantic, 
3600                      "ISO C++ forbids braced-groups within expressions");
3601
3602             /* And they're not allowed outside of a function-body; you
3603                cannot, for example, write:
3604
3605                  int i = ({ int j = 3; j + 1; });
3606
3607                at class or namespace scope.  */
3608             if (!parser->in_function_body
3609                 || parser->in_template_argument_list_p)
3610               {
3611                 error_at (token->location,
3612                           "statement-expressions are not allowed outside "
3613                           "functions nor in template-argument lists");
3614                 cp_parser_skip_to_end_of_block_or_statement (parser);
3615                 expr = error_mark_node;
3616               }
3617             else
3618               {
3619                 /* Start the statement-expression.  */
3620                 expr = begin_stmt_expr ();
3621                 /* Parse the compound-statement.  */
3622                 cp_parser_compound_statement (parser, expr, false);
3623                 /* Finish up.  */
3624                 expr = finish_stmt_expr (expr, false);
3625               }
3626           }
3627         else
3628           {
3629             /* Parse the parenthesized expression.  */
3630             expr = cp_parser_expression (parser, cast_p, idk);
3631             /* Let the front end know that this expression was
3632                enclosed in parentheses. This matters in case, for
3633                example, the expression is of the form `A::B', since
3634                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3635                not.  */
3636             finish_parenthesized_expr (expr);
3637           }
3638         /* The `>' token might be the end of a template-id or
3639            template-parameter-list now.  */
3640         parser->greater_than_is_operator_p
3641           = saved_greater_than_is_operator_p;
3642         /* Consume the `)'.  */
3643         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3644           cp_parser_skip_to_end_of_statement (parser);
3645
3646         return expr;
3647       }
3648
3649     case CPP_OPEN_SQUARE:
3650       if (c_dialect_objc ())
3651         /* We have an Objective-C++ message. */
3652         return cp_parser_objc_expression (parser);
3653       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3654       return cp_parser_lambda_expression (parser);
3655
3656     case CPP_OBJC_STRING:
3657       if (c_dialect_objc ())
3658         /* We have an Objective-C++ string literal. */
3659         return cp_parser_objc_expression (parser);
3660       cp_parser_error (parser, "expected primary-expression");
3661       return error_mark_node;
3662
3663     case CPP_KEYWORD:
3664       switch (token->keyword)
3665         {
3666           /* These two are the boolean literals.  */
3667         case RID_TRUE:
3668           cp_lexer_consume_token (parser->lexer);
3669           return boolean_true_node;
3670         case RID_FALSE:
3671           cp_lexer_consume_token (parser->lexer);
3672           return boolean_false_node;
3673
3674           /* The `__null' literal.  */
3675         case RID_NULL:
3676           cp_lexer_consume_token (parser->lexer);
3677           return null_node;
3678
3679           /* The `nullptr' literal.  */
3680         case RID_NULLPTR:
3681           cp_lexer_consume_token (parser->lexer);
3682           return nullptr_node;
3683
3684           /* Recognize the `this' keyword.  */
3685         case RID_THIS:
3686           cp_lexer_consume_token (parser->lexer);
3687           if (parser->local_variables_forbidden_p)
3688             {
3689               error_at (token->location,
3690                         "%<this%> may not be used in this context");
3691               return error_mark_node;
3692             }
3693           /* Pointers cannot appear in constant-expressions.  */
3694           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3695             return error_mark_node;
3696           return finish_this_expr ();
3697
3698           /* The `operator' keyword can be the beginning of an
3699              id-expression.  */
3700         case RID_OPERATOR:
3701           goto id_expression;
3702
3703         case RID_FUNCTION_NAME:
3704         case RID_PRETTY_FUNCTION_NAME:
3705         case RID_C99_FUNCTION_NAME:
3706           {
3707             non_integral_constant name;
3708
3709             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3710                __func__ are the names of variables -- but they are
3711                treated specially.  Therefore, they are handled here,
3712                rather than relying on the generic id-expression logic
3713                below.  Grammatically, these names are id-expressions.
3714
3715                Consume the token.  */
3716             token = cp_lexer_consume_token (parser->lexer);
3717
3718             switch (token->keyword)
3719               {
3720               case RID_FUNCTION_NAME:
3721                 name = NIC_FUNC_NAME;
3722                 break;
3723               case RID_PRETTY_FUNCTION_NAME:
3724                 name = NIC_PRETTY_FUNC;
3725                 break;
3726               case RID_C99_FUNCTION_NAME:
3727                 name = NIC_C99_FUNC;
3728                 break;
3729               default:
3730                 gcc_unreachable ();
3731               }
3732
3733             if (cp_parser_non_integral_constant_expression (parser, name))
3734               return error_mark_node;
3735
3736             /* Look up the name.  */
3737             return finish_fname (token->u.value);
3738           }
3739
3740         case RID_VA_ARG:
3741           {
3742             tree expression;
3743             tree type;
3744
3745             /* The `__builtin_va_arg' construct is used to handle
3746                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3747             cp_lexer_consume_token (parser->lexer);
3748             /* Look for the opening `('.  */
3749             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3750             /* Now, parse the assignment-expression.  */
3751             expression = cp_parser_assignment_expression (parser,
3752                                                           /*cast_p=*/false, NULL);
3753             /* Look for the `,'.  */
3754             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3755             /* Parse the type-id.  */
3756             type = cp_parser_type_id (parser);
3757             /* Look for the closing `)'.  */
3758             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3759             /* Using `va_arg' in a constant-expression is not
3760                allowed.  */
3761             if (cp_parser_non_integral_constant_expression (parser,
3762                                                             NIC_VA_ARG))
3763               return error_mark_node;
3764             return build_x_va_arg (expression, type);
3765           }
3766
3767         case RID_OFFSETOF:
3768           return cp_parser_builtin_offsetof (parser);
3769
3770         case RID_HAS_NOTHROW_ASSIGN:
3771         case RID_HAS_NOTHROW_CONSTRUCTOR:
3772         case RID_HAS_NOTHROW_COPY:        
3773         case RID_HAS_TRIVIAL_ASSIGN:
3774         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3775         case RID_HAS_TRIVIAL_COPY:        
3776         case RID_HAS_TRIVIAL_DESTRUCTOR:
3777         case RID_HAS_VIRTUAL_DESTRUCTOR:
3778         case RID_IS_ABSTRACT:
3779         case RID_IS_BASE_OF:
3780         case RID_IS_CLASS:
3781         case RID_IS_CONVERTIBLE_TO:
3782         case RID_IS_EMPTY:
3783         case RID_IS_ENUM:
3784         case RID_IS_POD:
3785         case RID_IS_POLYMORPHIC:
3786         case RID_IS_STD_LAYOUT:
3787         case RID_IS_TRIVIAL:
3788         case RID_IS_UNION:
3789           return cp_parser_trait_expr (parser, token->keyword);
3790
3791         /* Objective-C++ expressions.  */
3792         case RID_AT_ENCODE:
3793         case RID_AT_PROTOCOL:
3794         case RID_AT_SELECTOR:
3795           return cp_parser_objc_expression (parser);
3796
3797         case RID_TEMPLATE:
3798           if (parser->in_function_body
3799               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3800                   == CPP_LESS))
3801             {
3802               error_at (token->location,
3803                         "a template declaration cannot appear at block scope");
3804               cp_parser_skip_to_end_of_block_or_statement (parser);
3805               return error_mark_node;
3806             }
3807         default:
3808           cp_parser_error (parser, "expected primary-expression");
3809           return error_mark_node;
3810         }
3811
3812       /* An id-expression can start with either an identifier, a
3813          `::' as the beginning of a qualified-id, or the "operator"
3814          keyword.  */
3815     case CPP_NAME:
3816     case CPP_SCOPE:
3817     case CPP_TEMPLATE_ID:
3818     case CPP_NESTED_NAME_SPECIFIER:
3819       {
3820         tree id_expression;
3821         tree decl;
3822         const char *error_msg;
3823         bool template_p;
3824         bool done;
3825         cp_token *id_expr_token;
3826
3827       id_expression:
3828         /* Parse the id-expression.  */
3829         id_expression
3830           = cp_parser_id_expression (parser,
3831                                      /*template_keyword_p=*/false,
3832                                      /*check_dependency_p=*/true,
3833                                      &template_p,
3834                                      /*declarator_p=*/false,
3835                                      /*optional_p=*/false);
3836         if (id_expression == error_mark_node)
3837           return error_mark_node;
3838         id_expr_token = token;
3839         token = cp_lexer_peek_token (parser->lexer);
3840         done = (token->type != CPP_OPEN_SQUARE
3841                 && token->type != CPP_OPEN_PAREN
3842                 && token->type != CPP_DOT
3843                 && token->type != CPP_DEREF
3844                 && token->type != CPP_PLUS_PLUS
3845                 && token->type != CPP_MINUS_MINUS);
3846         /* If we have a template-id, then no further lookup is
3847            required.  If the template-id was for a template-class, we
3848            will sometimes have a TYPE_DECL at this point.  */
3849         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3850                  || TREE_CODE (id_expression) == TYPE_DECL)
3851           decl = id_expression;
3852         /* Look up the name.  */
3853         else
3854           {
3855             tree ambiguous_decls;
3856
3857             /* If we already know that this lookup is ambiguous, then
3858                we've already issued an error message; there's no reason
3859                to check again.  */
3860             if (id_expr_token->type == CPP_NAME
3861                 && id_expr_token->ambiguous_p)
3862               {
3863                 cp_parser_simulate_error (parser);
3864                 return error_mark_node;
3865               }
3866
3867             decl = cp_parser_lookup_name (parser, id_expression,
3868                                           none_type,
3869                                           template_p,
3870                                           /*is_namespace=*/false,
3871                                           /*check_dependency=*/true,
3872                                           &ambiguous_decls,
3873                                           id_expr_token->location);
3874             /* If the lookup was ambiguous, an error will already have
3875                been issued.  */
3876             if (ambiguous_decls)
3877               return error_mark_node;
3878
3879             /* In Objective-C++, an instance variable (ivar) may be preferred
3880                to whatever cp_parser_lookup_name() found.  */
3881             decl = objc_lookup_ivar (decl, id_expression);
3882
3883             /* If name lookup gives us a SCOPE_REF, then the
3884                qualifying scope was dependent.  */
3885             if (TREE_CODE (decl) == SCOPE_REF)
3886               {
3887                 /* At this point, we do not know if DECL is a valid
3888                    integral constant expression.  We assume that it is
3889                    in fact such an expression, so that code like:
3890
3891                       template <int N> struct A {
3892                         int a[B<N>::i];
3893                       };
3894                      
3895                    is accepted.  At template-instantiation time, we
3896                    will check that B<N>::i is actually a constant.  */
3897                 return decl;
3898               }
3899             /* Check to see if DECL is a local variable in a context
3900                where that is forbidden.  */
3901             if (parser->local_variables_forbidden_p
3902                 && local_variable_p (decl))
3903               {
3904                 /* It might be that we only found DECL because we are
3905                    trying to be generous with pre-ISO scoping rules.
3906                    For example, consider:
3907
3908                      int i;
3909                      void g() {
3910                        for (int i = 0; i < 10; ++i) {}
3911                        extern void f(int j = i);
3912                      }
3913
3914                    Here, name look up will originally find the out
3915                    of scope `i'.  We need to issue a warning message,
3916                    but then use the global `i'.  */
3917                 decl = check_for_out_of_scope_variable (decl);
3918                 if (local_variable_p (decl))
3919                   {
3920                     error_at (id_expr_token->location,
3921                               "local variable %qD may not appear in this context",
3922                               decl);
3923                     return error_mark_node;
3924                   }
3925               }
3926           }
3927
3928         decl = (finish_id_expression
3929                 (id_expression, decl, parser->scope,
3930                  idk,
3931                  parser->integral_constant_expression_p,
3932                  parser->allow_non_integral_constant_expression_p,
3933                  &parser->non_integral_constant_expression_p,
3934                  template_p, done, address_p,
3935                  template_arg_p,
3936                  &error_msg,
3937                  id_expr_token->location));
3938         if (error_msg)
3939           cp_parser_error (parser, error_msg);
3940         return decl;
3941       }
3942
3943       /* Anything else is an error.  */
3944     default:
3945       cp_parser_error (parser, "expected primary-expression");
3946       return error_mark_node;
3947     }
3948 }
3949
3950 /* Parse an id-expression.
3951
3952    id-expression:
3953      unqualified-id
3954      qualified-id
3955
3956    qualified-id:
3957      :: [opt] nested-name-specifier template [opt] unqualified-id
3958      :: identifier
3959      :: operator-function-id
3960      :: template-id
3961
3962    Return a representation of the unqualified portion of the
3963    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3964    a `::' or nested-name-specifier.
3965
3966    Often, if the id-expression was a qualified-id, the caller will
3967    want to make a SCOPE_REF to represent the qualified-id.  This
3968    function does not do this in order to avoid wastefully creating
3969    SCOPE_REFs when they are not required.
3970
3971    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3972    `template' keyword.
3973
3974    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3975    uninstantiated templates.
3976
3977    If *TEMPLATE_P is non-NULL, it is set to true iff the
3978    `template' keyword is used to explicitly indicate that the entity
3979    named is a template.
3980
3981    If DECLARATOR_P is true, the id-expression is appearing as part of
3982    a declarator, rather than as part of an expression.  */
3983
3984 static tree
3985 cp_parser_id_expression (cp_parser *parser,
3986                          bool template_keyword_p,
3987                          bool check_dependency_p,
3988                          bool *template_p,
3989                          bool declarator_p,
3990                          bool optional_p)
3991 {
3992   bool global_scope_p;
3993   bool nested_name_specifier_p;
3994
3995   /* Assume the `template' keyword was not used.  */
3996   if (template_p)
3997     *template_p = template_keyword_p;
3998
3999   /* Look for the optional `::' operator.  */
4000   global_scope_p
4001     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4002        != NULL_TREE);
4003   /* Look for the optional nested-name-specifier.  */
4004   nested_name_specifier_p
4005     = (cp_parser_nested_name_specifier_opt (parser,
4006                                             /*typename_keyword_p=*/false,
4007                                             check_dependency_p,
4008                                             /*type_p=*/false,
4009                                             declarator_p)
4010        != NULL_TREE);
4011   /* If there is a nested-name-specifier, then we are looking at
4012      the first qualified-id production.  */
4013   if (nested_name_specifier_p)
4014     {
4015       tree saved_scope;
4016       tree saved_object_scope;
4017       tree saved_qualifying_scope;
4018       tree unqualified_id;
4019       bool is_template;
4020
4021       /* See if the next token is the `template' keyword.  */
4022       if (!template_p)
4023         template_p = &is_template;
4024       *template_p = cp_parser_optional_template_keyword (parser);
4025       /* Name lookup we do during the processing of the
4026          unqualified-id might obliterate SCOPE.  */
4027       saved_scope = parser->scope;
4028       saved_object_scope = parser->object_scope;
4029       saved_qualifying_scope = parser->qualifying_scope;
4030       /* Process the final unqualified-id.  */
4031       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4032                                                  check_dependency_p,
4033                                                  declarator_p,
4034                                                  /*optional_p=*/false);
4035       /* Restore the SAVED_SCOPE for our caller.  */
4036       parser->scope = saved_scope;
4037       parser->object_scope = saved_object_scope;
4038       parser->qualifying_scope = saved_qualifying_scope;
4039
4040       return unqualified_id;
4041     }
4042   /* Otherwise, if we are in global scope, then we are looking at one
4043      of the other qualified-id productions.  */
4044   else if (global_scope_p)
4045     {
4046       cp_token *token;
4047       tree id;
4048
4049       /* Peek at the next token.  */
4050       token = cp_lexer_peek_token (parser->lexer);
4051
4052       /* If it's an identifier, and the next token is not a "<", then
4053          we can avoid the template-id case.  This is an optimization
4054          for this common case.  */
4055       if (token->type == CPP_NAME
4056           && !cp_parser_nth_token_starts_template_argument_list_p
4057                (parser, 2))
4058         return cp_parser_identifier (parser);
4059
4060       cp_parser_parse_tentatively (parser);
4061       /* Try a template-id.  */
4062       id = cp_parser_template_id (parser,
4063                                   /*template_keyword_p=*/false,
4064                                   /*check_dependency_p=*/true,
4065                                   declarator_p);
4066       /* If that worked, we're done.  */
4067       if (cp_parser_parse_definitely (parser))
4068         return id;
4069
4070       /* Peek at the next token.  (Changes in the token buffer may
4071          have invalidated the pointer obtained above.)  */
4072       token = cp_lexer_peek_token (parser->lexer);
4073
4074       switch (token->type)
4075         {
4076         case CPP_NAME:
4077           return cp_parser_identifier (parser);
4078
4079         case CPP_KEYWORD:
4080           if (token->keyword == RID_OPERATOR)
4081             return cp_parser_operator_function_id (parser);
4082           /* Fall through.  */
4083
4084         default:
4085           cp_parser_error (parser, "expected id-expression");
4086           return error_mark_node;
4087         }
4088     }
4089   else
4090     return cp_parser_unqualified_id (parser, template_keyword_p,
4091                                      /*check_dependency_p=*/true,
4092                                      declarator_p,
4093                                      optional_p);
4094 }
4095
4096 /* Parse an unqualified-id.
4097
4098    unqualified-id:
4099      identifier
4100      operator-function-id
4101      conversion-function-id
4102      ~ class-name
4103      template-id
4104
4105    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4106    keyword, in a construct like `A::template ...'.
4107
4108    Returns a representation of unqualified-id.  For the `identifier'
4109    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4110    production a BIT_NOT_EXPR is returned; the operand of the
4111    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4112    other productions, see the documentation accompanying the
4113    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4114    names are looked up in uninstantiated templates.  If DECLARATOR_P
4115    is true, the unqualified-id is appearing as part of a declarator,
4116    rather than as part of an expression.  */
4117
4118 static tree
4119 cp_parser_unqualified_id (cp_parser* parser,
4120                           bool template_keyword_p,
4121                           bool check_dependency_p,
4122                           bool declarator_p,
4123                           bool optional_p)
4124 {
4125   cp_token *token;
4126
4127   /* Peek at the next token.  */
4128   token = cp_lexer_peek_token (parser->lexer);
4129
4130   switch (token->type)
4131     {
4132     case CPP_NAME:
4133       {
4134         tree id;
4135
4136         /* We don't know yet whether or not this will be a
4137            template-id.  */
4138         cp_parser_parse_tentatively (parser);
4139         /* Try a template-id.  */
4140         id = cp_parser_template_id (parser, template_keyword_p,
4141                                     check_dependency_p,
4142                                     declarator_p);
4143         /* If it worked, we're done.  */
4144         if (cp_parser_parse_definitely (parser))
4145           return id;
4146         /* Otherwise, it's an ordinary identifier.  */
4147         return cp_parser_identifier (parser);
4148       }
4149
4150     case CPP_TEMPLATE_ID:
4151       return cp_parser_template_id (parser, template_keyword_p,
4152                                     check_dependency_p,
4153                                     declarator_p);
4154
4155     case CPP_COMPL:
4156       {
4157         tree type_decl;
4158         tree qualifying_scope;
4159         tree object_scope;
4160         tree scope;
4161         bool done;
4162
4163         /* Consume the `~' token.  */
4164         cp_lexer_consume_token (parser->lexer);
4165         /* Parse the class-name.  The standard, as written, seems to
4166            say that:
4167
4168              template <typename T> struct S { ~S (); };
4169              template <typename T> S<T>::~S() {}
4170
4171            is invalid, since `~' must be followed by a class-name, but
4172            `S<T>' is dependent, and so not known to be a class.
4173            That's not right; we need to look in uninstantiated
4174            templates.  A further complication arises from:
4175
4176              template <typename T> void f(T t) {
4177                t.T::~T();
4178              }
4179
4180            Here, it is not possible to look up `T' in the scope of `T'
4181            itself.  We must look in both the current scope, and the
4182            scope of the containing complete expression.
4183
4184            Yet another issue is:
4185
4186              struct S {
4187                int S;
4188                ~S();
4189              };
4190
4191              S::~S() {}
4192
4193            The standard does not seem to say that the `S' in `~S'
4194            should refer to the type `S' and not the data member
4195            `S::S'.  */
4196
4197         /* DR 244 says that we look up the name after the "~" in the
4198            same scope as we looked up the qualifying name.  That idea
4199            isn't fully worked out; it's more complicated than that.  */
4200         scope = parser->scope;
4201         object_scope = parser->object_scope;
4202         qualifying_scope = parser->qualifying_scope;
4203
4204         /* Check for invalid scopes.  */
4205         if (scope == error_mark_node)
4206           {
4207             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4208               cp_lexer_consume_token (parser->lexer);
4209             return error_mark_node;
4210           }
4211         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4212           {
4213             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4214               error_at (token->location,
4215                         "scope %qT before %<~%> is not a class-name",
4216                         scope);
4217             cp_parser_simulate_error (parser);
4218             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4219               cp_lexer_consume_token (parser->lexer);
4220             return error_mark_node;
4221           }
4222         gcc_assert (!scope || TYPE_P (scope));
4223
4224         /* If the name is of the form "X::~X" it's OK even if X is a
4225            typedef.  */
4226         token = cp_lexer_peek_token (parser->lexer);
4227         if (scope
4228             && token->type == CPP_NAME
4229             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4230                 != CPP_LESS)
4231             && (token->u.value == TYPE_IDENTIFIER (scope)
4232                 || constructor_name_p (token->u.value, scope)))
4233           {
4234             cp_lexer_consume_token (parser->lexer);
4235             return build_nt (BIT_NOT_EXPR, scope);
4236           }
4237
4238         /* If there was an explicit qualification (S::~T), first look
4239            in the scope given by the qualification (i.e., S).
4240
4241            Note: in the calls to cp_parser_class_name below we pass
4242            typename_type so that lookup finds the injected-class-name
4243            rather than the constructor.  */
4244         done = false;
4245         type_decl = NULL_TREE;
4246         if (scope)
4247           {
4248             cp_parser_parse_tentatively (parser);
4249             type_decl = cp_parser_class_name (parser,
4250                                               /*typename_keyword_p=*/false,
4251                                               /*template_keyword_p=*/false,
4252                                               typename_type,
4253                                               /*check_dependency=*/false,
4254                                               /*class_head_p=*/false,
4255                                               declarator_p);
4256             if (cp_parser_parse_definitely (parser))
4257               done = true;
4258           }
4259         /* In "N::S::~S", look in "N" as well.  */
4260         if (!done && scope && qualifying_scope)
4261           {
4262             cp_parser_parse_tentatively (parser);
4263             parser->scope = qualifying_scope;
4264             parser->object_scope = NULL_TREE;
4265             parser->qualifying_scope = NULL_TREE;
4266             type_decl
4267               = cp_parser_class_name (parser,
4268                                       /*typename_keyword_p=*/false,
4269                                       /*template_keyword_p=*/false,
4270                                       typename_type,
4271                                       /*check_dependency=*/false,
4272                                       /*class_head_p=*/false,
4273                                       declarator_p);
4274             if (cp_parser_parse_definitely (parser))
4275               done = true;
4276           }
4277         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4278         else if (!done && object_scope)
4279           {
4280             cp_parser_parse_tentatively (parser);
4281             parser->scope = object_scope;
4282             parser->object_scope = NULL_TREE;
4283             parser->qualifying_scope = NULL_TREE;
4284             type_decl
4285               = cp_parser_class_name (parser,
4286                                       /*typename_keyword_p=*/false,
4287                                       /*template_keyword_p=*/false,
4288                                       typename_type,
4289                                       /*check_dependency=*/false,
4290                                       /*class_head_p=*/false,
4291                                       declarator_p);
4292             if (cp_parser_parse_definitely (parser))
4293               done = true;
4294           }
4295         /* Look in the surrounding context.  */
4296         if (!done)
4297           {
4298             parser->scope = NULL_TREE;
4299             parser->object_scope = NULL_TREE;
4300             parser->qualifying_scope = NULL_TREE;
4301             if (processing_template_decl)
4302               cp_parser_parse_tentatively (parser);
4303             type_decl
4304               = cp_parser_class_name (parser,
4305                                       /*typename_keyword_p=*/false,
4306                                       /*template_keyword_p=*/false,
4307                                       typename_type,
4308                                       /*check_dependency=*/false,
4309                                       /*class_head_p=*/false,
4310                                       declarator_p);
4311             if (processing_template_decl
4312                 && ! cp_parser_parse_definitely (parser))
4313               {
4314                 /* We couldn't find a type with this name, so just accept
4315                    it and check for a match at instantiation time.  */
4316                 type_decl = cp_parser_identifier (parser);
4317                 if (type_decl != error_mark_node)
4318                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4319                 return type_decl;
4320               }
4321           }
4322         /* If an error occurred, assume that the name of the
4323            destructor is the same as the name of the qualifying
4324            class.  That allows us to keep parsing after running
4325            into ill-formed destructor names.  */
4326         if (type_decl == error_mark_node && scope)
4327           return build_nt (BIT_NOT_EXPR, scope);
4328         else if (type_decl == error_mark_node)
4329           return error_mark_node;
4330
4331         /* Check that destructor name and scope match.  */
4332         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4333           {
4334             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4335               error_at (token->location,
4336                         "declaration of %<~%T%> as member of %qT",
4337                         type_decl, scope);
4338             cp_parser_simulate_error (parser);
4339             return error_mark_node;
4340           }
4341
4342         /* [class.dtor]
4343
4344            A typedef-name that names a class shall not be used as the
4345            identifier in the declarator for a destructor declaration.  */
4346         if (declarator_p
4347             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4348             && !DECL_SELF_REFERENCE_P (type_decl)
4349             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4350           error_at (token->location,
4351                     "typedef-name %qD used as destructor declarator",
4352                     type_decl);
4353
4354         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4355       }
4356
4357     case CPP_KEYWORD:
4358       if (token->keyword == RID_OPERATOR)
4359         {
4360           tree id;
4361
4362           /* This could be a template-id, so we try that first.  */
4363           cp_parser_parse_tentatively (parser);
4364           /* Try a template-id.  */
4365           id = cp_parser_template_id (parser, template_keyword_p,
4366                                       /*check_dependency_p=*/true,
4367                                       declarator_p);
4368           /* If that worked, we're done.  */
4369           if (cp_parser_parse_definitely (parser))
4370             return id;
4371           /* We still don't know whether we're looking at an
4372              operator-function-id or a conversion-function-id.  */
4373           cp_parser_parse_tentatively (parser);
4374           /* Try an operator-function-id.  */
4375           id = cp_parser_operator_function_id (parser);
4376           /* If that didn't work, try a conversion-function-id.  */
4377           if (!cp_parser_parse_definitely (parser))
4378             id = cp_parser_conversion_function_id (parser);
4379
4380           return id;
4381         }
4382       /* Fall through.  */
4383
4384     default:
4385       if (optional_p)
4386         return NULL_TREE;
4387       cp_parser_error (parser, "expected unqualified-id");
4388       return error_mark_node;
4389     }
4390 }
4391
4392 /* Parse an (optional) nested-name-specifier.
4393
4394    nested-name-specifier: [C++98]
4395      class-or-namespace-name :: nested-name-specifier [opt]
4396      class-or-namespace-name :: template nested-name-specifier [opt]
4397
4398    nested-name-specifier: [C++0x]
4399      type-name ::
4400      namespace-name ::
4401      nested-name-specifier identifier ::
4402      nested-name-specifier template [opt] simple-template-id ::
4403
4404    PARSER->SCOPE should be set appropriately before this function is
4405    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4406    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4407    in name lookups.
4408
4409    Sets PARSER->SCOPE to the class (TYPE) or namespace
4410    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4411    it unchanged if there is no nested-name-specifier.  Returns the new
4412    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4413
4414    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4415    part of a declaration and/or decl-specifier.  */
4416
4417 static tree
4418 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4419                                      bool typename_keyword_p,
4420                                      bool check_dependency_p,
4421                                      bool type_p,
4422                                      bool is_declaration)
4423 {
4424   bool success = false;
4425   cp_token_position start = 0;
4426   cp_token *token;
4427
4428   /* Remember where the nested-name-specifier starts.  */
4429   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4430     {
4431       start = cp_lexer_token_position (parser->lexer, false);
4432       push_deferring_access_checks (dk_deferred);
4433     }
4434
4435   while (true)
4436     {
4437       tree new_scope;
4438       tree old_scope;
4439       tree saved_qualifying_scope;
4440       bool template_keyword_p;
4441
4442       /* Spot cases that cannot be the beginning of a
4443          nested-name-specifier.  */
4444       token = cp_lexer_peek_token (parser->lexer);
4445
4446       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4447          the already parsed nested-name-specifier.  */
4448       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4449         {
4450           /* Grab the nested-name-specifier and continue the loop.  */
4451           cp_parser_pre_parsed_nested_name_specifier (parser);
4452           /* If we originally encountered this nested-name-specifier
4453              with IS_DECLARATION set to false, we will not have
4454              resolved TYPENAME_TYPEs, so we must do so here.  */
4455           if (is_declaration
4456               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4457             {
4458               new_scope = resolve_typename_type (parser->scope,
4459                                                  /*only_current_p=*/false);
4460               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4461                 parser->scope = new_scope;
4462             }
4463           success = true;
4464           continue;
4465         }
4466
4467       /* Spot cases that cannot be the beginning of a
4468          nested-name-specifier.  On the second and subsequent times
4469          through the loop, we look for the `template' keyword.  */
4470       if (success && token->keyword == RID_TEMPLATE)
4471         ;
4472       /* A template-id can start a nested-name-specifier.  */
4473       else if (token->type == CPP_TEMPLATE_ID)
4474         ;
4475       else
4476         {
4477           /* If the next token is not an identifier, then it is
4478              definitely not a type-name or namespace-name.  */
4479           if (token->type != CPP_NAME)
4480             break;
4481           /* If the following token is neither a `<' (to begin a
4482              template-id), nor a `::', then we are not looking at a
4483              nested-name-specifier.  */
4484           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4485           if (token->type != CPP_SCOPE
4486               && !cp_parser_nth_token_starts_template_argument_list_p
4487                   (parser, 2))
4488             break;
4489         }
4490
4491       /* The nested-name-specifier is optional, so we parse
4492          tentatively.  */
4493       cp_parser_parse_tentatively (parser);
4494
4495       /* Look for the optional `template' keyword, if this isn't the
4496          first time through the loop.  */
4497       if (success)
4498         template_keyword_p = cp_parser_optional_template_keyword (parser);
4499       else
4500         template_keyword_p = false;
4501
4502       /* Save the old scope since the name lookup we are about to do
4503          might destroy it.  */
4504       old_scope = parser->scope;
4505       saved_qualifying_scope = parser->qualifying_scope;
4506       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4507          look up names in "X<T>::I" in order to determine that "Y" is
4508          a template.  So, if we have a typename at this point, we make
4509          an effort to look through it.  */
4510       if (is_declaration
4511           && !typename_keyword_p
4512           && parser->scope
4513           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4514         parser->scope = resolve_typename_type (parser->scope,
4515                                                /*only_current_p=*/false);
4516       /* Parse the qualifying entity.  */
4517       new_scope
4518         = cp_parser_qualifying_entity (parser,
4519                                        typename_keyword_p,
4520                                        template_keyword_p,
4521                                        check_dependency_p,
4522                                        type_p,
4523                                        is_declaration);
4524       /* Look for the `::' token.  */
4525       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4526
4527       /* If we found what we wanted, we keep going; otherwise, we're
4528          done.  */
4529       if (!cp_parser_parse_definitely (parser))
4530         {
4531           bool error_p = false;
4532
4533           /* Restore the OLD_SCOPE since it was valid before the
4534              failed attempt at finding the last
4535              class-or-namespace-name.  */
4536           parser->scope = old_scope;
4537           parser->qualifying_scope = saved_qualifying_scope;
4538           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4539             break;
4540           /* If the next token is an identifier, and the one after
4541              that is a `::', then any valid interpretation would have
4542              found a class-or-namespace-name.  */
4543           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4544                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4545                      == CPP_SCOPE)
4546                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4547                      != CPP_COMPL))
4548             {
4549               token = cp_lexer_consume_token (parser->lexer);
4550               if (!error_p)
4551                 {
4552                   if (!token->ambiguous_p)
4553                     {
4554                       tree decl;
4555                       tree ambiguous_decls;
4556
4557                       decl = cp_parser_lookup_name (parser, token->u.value,
4558                                                     none_type,
4559                                                     /*is_template=*/false,
4560                                                     /*is_namespace=*/false,
4561                                                     /*check_dependency=*/true,
4562                                                     &ambiguous_decls,
4563                                                     token->location);
4564                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4565                         error_at (token->location,
4566                                   "%qD used without template parameters",
4567                                   decl);
4568                       else if (ambiguous_decls)
4569                         {
4570                           error_at (token->location,
4571                                     "reference to %qD is ambiguous",
4572                                     token->u.value);
4573                           print_candidates (ambiguous_decls);
4574                           decl = error_mark_node;
4575                         }
4576                       else
4577                         {
4578                           if (cxx_dialect != cxx98)
4579                             cp_parser_name_lookup_error
4580                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4581                              token->location);
4582                           else
4583                             cp_parser_name_lookup_error
4584                             (parser, token->u.value, decl, NLE_CXX98,
4585                              token->location);
4586                         }
4587                     }
4588                   parser->scope = error_mark_node;
4589                   error_p = true;
4590                   /* Treat this as a successful nested-name-specifier
4591                      due to:
4592
4593                      [basic.lookup.qual]
4594
4595                      If the name found is not a class-name (clause
4596                      _class_) or namespace-name (_namespace.def_), the
4597                      program is ill-formed.  */
4598                   success = true;
4599                 }
4600               cp_lexer_consume_token (parser->lexer);
4601             }
4602           break;
4603         }
4604       /* We've found one valid nested-name-specifier.  */
4605       success = true;
4606       /* Name lookup always gives us a DECL.  */
4607       if (TREE_CODE (new_scope) == TYPE_DECL)
4608         new_scope = TREE_TYPE (new_scope);
4609       /* Uses of "template" must be followed by actual templates.  */
4610       if (template_keyword_p
4611           && !(CLASS_TYPE_P (new_scope)
4612                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4613                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4614                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4615           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4616                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4617                    == TEMPLATE_ID_EXPR)))
4618         permerror (input_location, TYPE_P (new_scope)
4619                    ? "%qT is not a template"
4620                    : "%qD is not a template",
4621                    new_scope);
4622       /* If it is a class scope, try to complete it; we are about to
4623          be looking up names inside the class.  */
4624       if (TYPE_P (new_scope)
4625           /* Since checking types for dependency can be expensive,
4626              avoid doing it if the type is already complete.  */
4627           && !COMPLETE_TYPE_P (new_scope)
4628           /* Do not try to complete dependent types.  */
4629           && !dependent_type_p (new_scope))
4630         {
4631           new_scope = complete_type (new_scope);
4632           /* If it is a typedef to current class, use the current
4633              class instead, as the typedef won't have any names inside
4634              it yet.  */
4635           if (!COMPLETE_TYPE_P (new_scope)
4636               && currently_open_class (new_scope))
4637             new_scope = TYPE_MAIN_VARIANT (new_scope);
4638         }
4639       /* Make sure we look in the right scope the next time through
4640          the loop.  */
4641       parser->scope = new_scope;
4642     }
4643
4644   /* If parsing tentatively, replace the sequence of tokens that makes
4645      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4646      token.  That way, should we re-parse the token stream, we will
4647      not have to repeat the effort required to do the parse, nor will
4648      we issue duplicate error messages.  */
4649   if (success && start)
4650     {
4651       cp_token *token;
4652
4653       token = cp_lexer_token_at (parser->lexer, start);
4654       /* Reset the contents of the START token.  */
4655       token->type = CPP_NESTED_NAME_SPECIFIER;
4656       /* Retrieve any deferred checks.  Do not pop this access checks yet
4657          so the memory will not be reclaimed during token replacing below.  */
4658       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4659       token->u.tree_check_value->value = parser->scope;
4660       token->u.tree_check_value->checks = get_deferred_access_checks ();
4661       token->u.tree_check_value->qualifying_scope =
4662         parser->qualifying_scope;
4663       token->keyword = RID_MAX;
4664
4665       /* Purge all subsequent tokens.  */
4666       cp_lexer_purge_tokens_after (parser->lexer, start);
4667     }
4668
4669   if (start)
4670     pop_to_parent_deferring_access_checks ();
4671
4672   return success ? parser->scope : NULL_TREE;
4673 }
4674
4675 /* Parse a nested-name-specifier.  See
4676    cp_parser_nested_name_specifier_opt for details.  This function
4677    behaves identically, except that it will an issue an error if no
4678    nested-name-specifier is present.  */
4679
4680 static tree
4681 cp_parser_nested_name_specifier (cp_parser *parser,
4682                                  bool typename_keyword_p,
4683                                  bool check_dependency_p,
4684                                  bool type_p,
4685                                  bool is_declaration)
4686 {
4687   tree scope;
4688
4689   /* Look for the nested-name-specifier.  */
4690   scope = cp_parser_nested_name_specifier_opt (parser,
4691                                                typename_keyword_p,
4692                                                check_dependency_p,
4693                                                type_p,
4694                                                is_declaration);
4695   /* If it was not present, issue an error message.  */
4696   if (!scope)
4697     {
4698       cp_parser_error (parser, "expected nested-name-specifier");
4699       parser->scope = NULL_TREE;
4700     }
4701
4702   return scope;
4703 }
4704
4705 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4706    this is either a class-name or a namespace-name (which corresponds
4707    to the class-or-namespace-name production in the grammar). For
4708    C++0x, it can also be a type-name that refers to an enumeration
4709    type.
4710
4711    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4712    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4713    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4714    TYPE_P is TRUE iff the next name should be taken as a class-name,
4715    even the same name is declared to be another entity in the same
4716    scope.
4717
4718    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4719    specified by the class-or-namespace-name.  If neither is found the
4720    ERROR_MARK_NODE is returned.  */
4721
4722 static tree
4723 cp_parser_qualifying_entity (cp_parser *parser,
4724                              bool typename_keyword_p,
4725                              bool template_keyword_p,
4726                              bool check_dependency_p,
4727                              bool type_p,
4728                              bool is_declaration)
4729 {
4730   tree saved_scope;
4731   tree saved_qualifying_scope;
4732   tree saved_object_scope;
4733   tree scope;
4734   bool only_class_p;
4735   bool successful_parse_p;
4736
4737   /* Before we try to parse the class-name, we must save away the
4738      current PARSER->SCOPE since cp_parser_class_name will destroy
4739      it.  */
4740   saved_scope = parser->scope;
4741   saved_qualifying_scope = parser->qualifying_scope;
4742   saved_object_scope = parser->object_scope;
4743   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4744      there is no need to look for a namespace-name.  */
4745   only_class_p = template_keyword_p 
4746     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4747   if (!only_class_p)
4748     cp_parser_parse_tentatively (parser);
4749   scope = cp_parser_class_name (parser,
4750                                 typename_keyword_p,
4751                                 template_keyword_p,
4752                                 type_p ? class_type : none_type,
4753                                 check_dependency_p,
4754                                 /*class_head_p=*/false,
4755                                 is_declaration);
4756   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4757   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4758   if (!only_class_p 
4759       && cxx_dialect != cxx98
4760       && !successful_parse_p)
4761     {
4762       /* Restore the saved scope.  */
4763       parser->scope = saved_scope;
4764       parser->qualifying_scope = saved_qualifying_scope;
4765       parser->object_scope = saved_object_scope;
4766
4767       /* Parse tentatively.  */
4768       cp_parser_parse_tentatively (parser);
4769      
4770       /* Parse a typedef-name or enum-name.  */
4771       scope = cp_parser_nonclass_name (parser);
4772
4773       /* "If the name found does not designate a namespace or a class,
4774          enumeration, or dependent type, the program is ill-formed."
4775
4776          We cover classes and dependent types above and namespaces below,
4777          so this code is only looking for enums.  */
4778       if (!scope || TREE_CODE (scope) != TYPE_DECL
4779           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4780         cp_parser_simulate_error (parser);
4781
4782       successful_parse_p = cp_parser_parse_definitely (parser);
4783     }
4784   /* If that didn't work, try for a namespace-name.  */
4785   if (!only_class_p && !successful_parse_p)
4786     {
4787       /* Restore the saved scope.  */
4788       parser->scope = saved_scope;
4789       parser->qualifying_scope = saved_qualifying_scope;
4790       parser->object_scope = saved_object_scope;
4791       /* If we are not looking at an identifier followed by the scope
4792          resolution operator, then this is not part of a
4793          nested-name-specifier.  (Note that this function is only used
4794          to parse the components of a nested-name-specifier.)  */
4795       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4796           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4797         return error_mark_node;
4798       scope = cp_parser_namespace_name (parser);
4799     }
4800
4801   return scope;
4802 }
4803
4804 /* Parse a postfix-expression.
4805
4806    postfix-expression:
4807      primary-expression
4808      postfix-expression [ expression ]
4809      postfix-expression ( expression-list [opt] )
4810      simple-type-specifier ( expression-list [opt] )
4811      typename :: [opt] nested-name-specifier identifier
4812        ( expression-list [opt] )
4813      typename :: [opt] nested-name-specifier template [opt] template-id
4814        ( expression-list [opt] )
4815      postfix-expression . template [opt] id-expression
4816      postfix-expression -> template [opt] id-expression
4817      postfix-expression . pseudo-destructor-name
4818      postfix-expression -> pseudo-destructor-name
4819      postfix-expression ++
4820      postfix-expression --
4821      dynamic_cast < type-id > ( expression )
4822      static_cast < type-id > ( expression )
4823      reinterpret_cast < type-id > ( expression )
4824      const_cast < type-id > ( expression )
4825      typeid ( expression )
4826      typeid ( type-id )
4827
4828    GNU Extension:
4829
4830    postfix-expression:
4831      ( type-id ) { initializer-list , [opt] }
4832
4833    This extension is a GNU version of the C99 compound-literal
4834    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4835    but they are essentially the same concept.)
4836
4837    If ADDRESS_P is true, the postfix expression is the operand of the
4838    `&' operator.  CAST_P is true if this expression is the target of a
4839    cast.
4840
4841    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4842    class member access expressions [expr.ref].
4843
4844    Returns a representation of the expression.  */
4845
4846 static tree
4847 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4848                               bool member_access_only_p,
4849                               cp_id_kind * pidk_return)
4850 {
4851   cp_token *token;
4852   enum rid keyword;
4853   cp_id_kind idk = CP_ID_KIND_NONE;
4854   tree postfix_expression = NULL_TREE;
4855   bool is_member_access = false;
4856
4857   /* Peek at the next token.  */
4858   token = cp_lexer_peek_token (parser->lexer);
4859   /* Some of the productions are determined by keywords.  */
4860   keyword = token->keyword;
4861   switch (keyword)
4862     {
4863     case RID_DYNCAST:
4864     case RID_STATCAST:
4865     case RID_REINTCAST:
4866     case RID_CONSTCAST:
4867       {
4868         tree type;
4869         tree expression;
4870         const char *saved_message;
4871
4872         /* All of these can be handled in the same way from the point
4873            of view of parsing.  Begin by consuming the token
4874            identifying the cast.  */
4875         cp_lexer_consume_token (parser->lexer);
4876
4877         /* New types cannot be defined in the cast.  */
4878         saved_message = parser->type_definition_forbidden_message;
4879         parser->type_definition_forbidden_message
4880           = G_("types may not be defined in casts");
4881
4882         /* Look for the opening `<'.  */
4883         cp_parser_require (parser, CPP_LESS, RT_LESS);
4884         /* Parse the type to which we are casting.  */
4885         type = cp_parser_type_id (parser);
4886         /* Look for the closing `>'.  */
4887         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4888         /* Restore the old message.  */
4889         parser->type_definition_forbidden_message = saved_message;
4890
4891         /* And the expression which is being cast.  */
4892         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4893         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4894         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4895
4896         /* Only type conversions to integral or enumeration types
4897            can be used in constant-expressions.  */
4898         if (!cast_valid_in_integral_constant_expression_p (type)
4899             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4900           return error_mark_node;
4901
4902         switch (keyword)
4903           {
4904           case RID_DYNCAST:
4905             postfix_expression
4906               = build_dynamic_cast (type, expression, tf_warning_or_error);
4907             break;
4908           case RID_STATCAST:
4909             postfix_expression
4910               = build_static_cast (type, expression, tf_warning_or_error);
4911             break;
4912           case RID_REINTCAST:
4913             postfix_expression
4914               = build_reinterpret_cast (type, expression, 
4915                                         tf_warning_or_error);
4916             break;
4917           case RID_CONSTCAST:
4918             postfix_expression
4919               = build_const_cast (type, expression, tf_warning_or_error);
4920             break;
4921           default:
4922             gcc_unreachable ();
4923           }
4924       }
4925       break;
4926
4927     case RID_TYPEID:
4928       {
4929         tree type;
4930         const char *saved_message;
4931         bool saved_in_type_id_in_expr_p;
4932
4933         /* Consume the `typeid' token.  */
4934         cp_lexer_consume_token (parser->lexer);
4935         /* Look for the `(' token.  */
4936         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4937         /* Types cannot be defined in a `typeid' expression.  */
4938         saved_message = parser->type_definition_forbidden_message;
4939         parser->type_definition_forbidden_message
4940           = G_("types may not be defined in a %<typeid%> expression");
4941         /* We can't be sure yet whether we're looking at a type-id or an
4942            expression.  */
4943         cp_parser_parse_tentatively (parser);
4944         /* Try a type-id first.  */
4945         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4946         parser->in_type_id_in_expr_p = true;
4947         type = cp_parser_type_id (parser);
4948         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4949         /* Look for the `)' token.  Otherwise, we can't be sure that
4950            we're not looking at an expression: consider `typeid (int
4951            (3))', for example.  */
4952         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4953         /* If all went well, simply lookup the type-id.  */
4954         if (cp_parser_parse_definitely (parser))
4955           postfix_expression = get_typeid (type);
4956         /* Otherwise, fall back to the expression variant.  */
4957         else
4958           {
4959             tree expression;
4960
4961             /* Look for an expression.  */
4962             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4963             /* Compute its typeid.  */
4964             postfix_expression = build_typeid (expression);
4965             /* Look for the `)' token.  */
4966             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4967           }
4968         /* Restore the saved message.  */
4969         parser->type_definition_forbidden_message = saved_message;
4970         /* `typeid' may not appear in an integral constant expression.  */
4971         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4972           return error_mark_node;
4973       }
4974       break;
4975
4976     case RID_TYPENAME:
4977       {
4978         tree type;
4979         /* The syntax permitted here is the same permitted for an
4980            elaborated-type-specifier.  */
4981         type = cp_parser_elaborated_type_specifier (parser,
4982                                                     /*is_friend=*/false,
4983                                                     /*is_declaration=*/false);
4984         postfix_expression = cp_parser_functional_cast (parser, type);
4985       }
4986       break;
4987
4988     default:
4989       {
4990         tree type;
4991
4992         /* If the next thing is a simple-type-specifier, we may be
4993            looking at a functional cast.  We could also be looking at
4994            an id-expression.  So, we try the functional cast, and if
4995            that doesn't work we fall back to the primary-expression.  */
4996         cp_parser_parse_tentatively (parser);
4997         /* Look for the simple-type-specifier.  */
4998         type = cp_parser_simple_type_specifier (parser,
4999                                                 /*decl_specs=*/NULL,
5000                                                 CP_PARSER_FLAGS_NONE);
5001         /* Parse the cast itself.  */
5002         if (!cp_parser_error_occurred (parser))
5003           postfix_expression
5004             = cp_parser_functional_cast (parser, type);
5005         /* If that worked, we're done.  */
5006         if (cp_parser_parse_definitely (parser))
5007           break;
5008
5009         /* If the functional-cast didn't work out, try a
5010            compound-literal.  */
5011         if (cp_parser_allow_gnu_extensions_p (parser)
5012             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5013           {
5014             VEC(constructor_elt,gc) *initializer_list = NULL;
5015             bool saved_in_type_id_in_expr_p;
5016
5017             cp_parser_parse_tentatively (parser);
5018             /* Consume the `('.  */
5019             cp_lexer_consume_token (parser->lexer);
5020             /* Parse the type.  */
5021             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5022             parser->in_type_id_in_expr_p = true;
5023             type = cp_parser_type_id (parser);
5024             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5025             /* Look for the `)'.  */
5026             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5027             /* Look for the `{'.  */
5028             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5029             /* If things aren't going well, there's no need to
5030                keep going.  */
5031             if (!cp_parser_error_occurred (parser))
5032               {
5033                 bool non_constant_p;
5034                 /* Parse the initializer-list.  */
5035                 initializer_list
5036                   = cp_parser_initializer_list (parser, &non_constant_p);
5037                 /* Allow a trailing `,'.  */
5038                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5039                   cp_lexer_consume_token (parser->lexer);
5040                 /* Look for the final `}'.  */
5041                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5042               }
5043             /* If that worked, we're definitely looking at a
5044                compound-literal expression.  */
5045             if (cp_parser_parse_definitely (parser))
5046               {
5047                 /* Warn the user that a compound literal is not
5048                    allowed in standard C++.  */
5049                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5050                 /* For simplicity, we disallow compound literals in
5051                    constant-expressions.  We could
5052                    allow compound literals of integer type, whose
5053                    initializer was a constant, in constant
5054                    expressions.  Permitting that usage, as a further
5055                    extension, would not change the meaning of any
5056                    currently accepted programs.  (Of course, as
5057                    compound literals are not part of ISO C++, the
5058                    standard has nothing to say.)  */
5059                 if (cp_parser_non_integral_constant_expression (parser,
5060                                                                 NIC_NCC))
5061                   {
5062                     postfix_expression = error_mark_node;
5063                     break;
5064                   }
5065                 /* Form the representation of the compound-literal.  */
5066                 postfix_expression
5067                   = (finish_compound_literal
5068                      (type, build_constructor (init_list_type_node,
5069                                                initializer_list)));
5070                 break;
5071               }
5072           }
5073
5074         /* It must be a primary-expression.  */
5075         postfix_expression
5076           = cp_parser_primary_expression (parser, address_p, cast_p,
5077                                           /*template_arg_p=*/false,
5078                                           &idk);
5079       }
5080       break;
5081     }
5082
5083   /* Keep looping until the postfix-expression is complete.  */
5084   while (true)
5085     {
5086       if (idk == CP_ID_KIND_UNQUALIFIED
5087           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5088           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5089         /* It is not a Koenig lookup function call.  */
5090         postfix_expression
5091           = unqualified_name_lookup_error (postfix_expression);
5092
5093       /* Peek at the next token.  */
5094       token = cp_lexer_peek_token (parser->lexer);
5095
5096       switch (token->type)
5097         {
5098         case CPP_OPEN_SQUARE:
5099           postfix_expression
5100             = cp_parser_postfix_open_square_expression (parser,
5101                                                         postfix_expression,
5102                                                         false);
5103           idk = CP_ID_KIND_NONE;
5104           is_member_access = false;
5105           break;
5106
5107         case CPP_OPEN_PAREN:
5108           /* postfix-expression ( expression-list [opt] ) */
5109           {
5110             bool koenig_p;
5111             bool is_builtin_constant_p;
5112             bool saved_integral_constant_expression_p = false;
5113             bool saved_non_integral_constant_expression_p = false;
5114             VEC(tree,gc) *args;
5115
5116             is_member_access = false;
5117
5118             is_builtin_constant_p
5119               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5120             if (is_builtin_constant_p)
5121               {
5122                 /* The whole point of __builtin_constant_p is to allow
5123                    non-constant expressions to appear as arguments.  */
5124                 saved_integral_constant_expression_p
5125                   = parser->integral_constant_expression_p;
5126                 saved_non_integral_constant_expression_p
5127                   = parser->non_integral_constant_expression_p;
5128                 parser->integral_constant_expression_p = false;
5129               }
5130             args = (cp_parser_parenthesized_expression_list
5131                     (parser, non_attr,
5132                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5133                      /*non_constant_p=*/NULL));
5134             if (is_builtin_constant_p)
5135               {
5136                 parser->integral_constant_expression_p
5137                   = saved_integral_constant_expression_p;
5138                 parser->non_integral_constant_expression_p
5139                   = saved_non_integral_constant_expression_p;
5140               }
5141
5142             if (args == NULL)
5143               {
5144                 postfix_expression = error_mark_node;
5145                 break;
5146               }
5147
5148             /* Function calls are not permitted in
5149                constant-expressions.  */
5150             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5151                 && cp_parser_non_integral_constant_expression (parser,
5152                                                                NIC_FUNC_CALL))
5153               {
5154                 postfix_expression = error_mark_node;
5155                 release_tree_vector (args);
5156                 break;
5157               }
5158
5159             koenig_p = false;
5160             if (idk == CP_ID_KIND_UNQUALIFIED
5161                 || idk == CP_ID_KIND_TEMPLATE_ID)
5162               {
5163                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5164                   {
5165                     if (!VEC_empty (tree, args))
5166                       {
5167                         koenig_p = true;
5168                         if (!any_type_dependent_arguments_p (args))
5169                           postfix_expression
5170                             = perform_koenig_lookup (postfix_expression, args);
5171                       }
5172                     else
5173                       postfix_expression
5174                         = unqualified_fn_lookup_error (postfix_expression);
5175                   }
5176                 /* We do not perform argument-dependent lookup if
5177                    normal lookup finds a non-function, in accordance
5178                    with the expected resolution of DR 218.  */
5179                 else if (!VEC_empty (tree, args)
5180                          && is_overloaded_fn (postfix_expression))
5181                   {
5182                     tree fn = get_first_fn (postfix_expression);
5183                     fn = STRIP_TEMPLATE (fn);
5184
5185                     /* Do not do argument dependent lookup if regular
5186                        lookup finds a member function or a block-scope
5187                        function declaration.  [basic.lookup.argdep]/3  */
5188                     if (!DECL_FUNCTION_MEMBER_P (fn)
5189                         && !DECL_LOCAL_FUNCTION_P (fn))
5190                       {
5191                         koenig_p = true;
5192                         if (!any_type_dependent_arguments_p (args))
5193                           postfix_expression
5194                             = perform_koenig_lookup (postfix_expression, args);
5195                       }
5196                   }
5197               }
5198
5199             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5200               {
5201                 tree instance = TREE_OPERAND (postfix_expression, 0);
5202                 tree fn = TREE_OPERAND (postfix_expression, 1);
5203
5204                 if (processing_template_decl
5205                     && (type_dependent_expression_p (instance)
5206                         || (!BASELINK_P (fn)
5207                             && TREE_CODE (fn) != FIELD_DECL)
5208                         || type_dependent_expression_p (fn)
5209                         || any_type_dependent_arguments_p (args)))
5210                   {
5211                     postfix_expression
5212                       = build_nt_call_vec (postfix_expression, args);
5213                     release_tree_vector (args);
5214                     break;
5215                   }
5216
5217                 if (BASELINK_P (fn))
5218                   {
5219                   postfix_expression
5220                     = (build_new_method_call
5221                        (instance, fn, &args, NULL_TREE,
5222                         (idk == CP_ID_KIND_QUALIFIED
5223                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5224                         /*fn_p=*/NULL,
5225                         tf_warning_or_error));
5226                   }
5227                 else
5228                   postfix_expression
5229                     = finish_call_expr (postfix_expression, &args,
5230                                         /*disallow_virtual=*/false,
5231                                         /*koenig_p=*/false,
5232                                         tf_warning_or_error);
5233               }
5234             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5235                      || TREE_CODE (postfix_expression) == MEMBER_REF
5236                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5237               postfix_expression = (build_offset_ref_call_from_tree
5238                                     (postfix_expression, &args));
5239             else if (idk == CP_ID_KIND_QUALIFIED)
5240               /* A call to a static class member, or a namespace-scope
5241                  function.  */
5242               postfix_expression
5243                 = finish_call_expr (postfix_expression, &args,
5244                                     /*disallow_virtual=*/true,
5245                                     koenig_p,
5246                                     tf_warning_or_error);
5247             else
5248               /* All other function calls.  */
5249               postfix_expression
5250                 = finish_call_expr (postfix_expression, &args,
5251                                     /*disallow_virtual=*/false,
5252                                     koenig_p,
5253                                     tf_warning_or_error);
5254
5255             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5256             idk = CP_ID_KIND_NONE;
5257
5258             release_tree_vector (args);
5259           }
5260           break;
5261
5262         case CPP_DOT:
5263         case CPP_DEREF:
5264           /* postfix-expression . template [opt] id-expression
5265              postfix-expression . pseudo-destructor-name
5266              postfix-expression -> template [opt] id-expression
5267              postfix-expression -> pseudo-destructor-name */
5268
5269           /* Consume the `.' or `->' operator.  */
5270           cp_lexer_consume_token (parser->lexer);
5271
5272           postfix_expression
5273             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5274                                                       postfix_expression,
5275                                                       false, &idk,
5276                                                       token->location);
5277
5278           is_member_access = true;
5279           break;
5280
5281         case CPP_PLUS_PLUS:
5282           /* postfix-expression ++  */
5283           /* Consume the `++' token.  */
5284           cp_lexer_consume_token (parser->lexer);
5285           /* Generate a representation for the complete expression.  */
5286           postfix_expression
5287             = finish_increment_expr (postfix_expression,
5288                                      POSTINCREMENT_EXPR);
5289           /* Increments may not appear in constant-expressions.  */
5290           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5291             postfix_expression = error_mark_node;
5292           idk = CP_ID_KIND_NONE;
5293           is_member_access = false;
5294           break;
5295
5296         case CPP_MINUS_MINUS:
5297           /* postfix-expression -- */
5298           /* Consume the `--' token.  */
5299           cp_lexer_consume_token (parser->lexer);
5300           /* Generate a representation for the complete expression.  */
5301           postfix_expression
5302             = finish_increment_expr (postfix_expression,
5303                                      POSTDECREMENT_EXPR);
5304           /* Decrements may not appear in constant-expressions.  */
5305           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5306             postfix_expression = error_mark_node;
5307           idk = CP_ID_KIND_NONE;
5308           is_member_access = false;
5309           break;
5310
5311         default:
5312           if (pidk_return != NULL)
5313             * pidk_return = idk;
5314           if (member_access_only_p)
5315             return is_member_access? postfix_expression : error_mark_node;
5316           else
5317             return postfix_expression;
5318         }
5319     }
5320
5321   /* We should never get here.  */
5322   gcc_unreachable ();
5323   return error_mark_node;
5324 }
5325
5326 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5327    by cp_parser_builtin_offsetof.  We're looking for
5328
5329      postfix-expression [ expression ]
5330
5331    FOR_OFFSETOF is set if we're being called in that context, which
5332    changes how we deal with integer constant expressions.  */
5333
5334 static tree
5335 cp_parser_postfix_open_square_expression (cp_parser *parser,
5336                                           tree postfix_expression,
5337                                           bool for_offsetof)
5338 {
5339   tree index;
5340
5341   /* Consume the `[' token.  */
5342   cp_lexer_consume_token (parser->lexer);
5343
5344   /* Parse the index expression.  */
5345   /* ??? For offsetof, there is a question of what to allow here.  If
5346      offsetof is not being used in an integral constant expression context,
5347      then we *could* get the right answer by computing the value at runtime.
5348      If we are in an integral constant expression context, then we might
5349      could accept any constant expression; hard to say without analysis.
5350      Rather than open the barn door too wide right away, allow only integer
5351      constant expressions here.  */
5352   if (for_offsetof)
5353     index = cp_parser_constant_expression (parser, false, NULL);
5354   else
5355     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5356
5357   /* Look for the closing `]'.  */
5358   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5359
5360   /* Build the ARRAY_REF.  */
5361   postfix_expression = grok_array_decl (postfix_expression, index);
5362
5363   /* When not doing offsetof, array references are not permitted in
5364      constant-expressions.  */
5365   if (!for_offsetof
5366       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5367     postfix_expression = error_mark_node;
5368
5369   return postfix_expression;
5370 }
5371
5372 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5373    by cp_parser_builtin_offsetof.  We're looking for
5374
5375      postfix-expression . template [opt] id-expression
5376      postfix-expression . pseudo-destructor-name
5377      postfix-expression -> template [opt] id-expression
5378      postfix-expression -> pseudo-destructor-name
5379
5380    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5381    limits what of the above we'll actually accept, but nevermind.
5382    TOKEN_TYPE is the "." or "->" token, which will already have been
5383    removed from the stream.  */
5384
5385 static tree
5386 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5387                                         enum cpp_ttype token_type,
5388                                         tree postfix_expression,
5389                                         bool for_offsetof, cp_id_kind *idk,
5390                                         location_t location)
5391 {
5392   tree name;
5393   bool dependent_p;
5394   bool pseudo_destructor_p;
5395   tree scope = NULL_TREE;
5396
5397   /* If this is a `->' operator, dereference the pointer.  */
5398   if (token_type == CPP_DEREF)
5399     postfix_expression = build_x_arrow (postfix_expression);
5400   /* Check to see whether or not the expression is type-dependent.  */
5401   dependent_p = type_dependent_expression_p (postfix_expression);
5402   /* The identifier following the `->' or `.' is not qualified.  */
5403   parser->scope = NULL_TREE;
5404   parser->qualifying_scope = NULL_TREE;
5405   parser->object_scope = NULL_TREE;
5406   *idk = CP_ID_KIND_NONE;
5407
5408   /* Enter the scope corresponding to the type of the object
5409      given by the POSTFIX_EXPRESSION.  */
5410   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5411     {
5412       scope = TREE_TYPE (postfix_expression);
5413       /* According to the standard, no expression should ever have
5414          reference type.  Unfortunately, we do not currently match
5415          the standard in this respect in that our internal representation
5416          of an expression may have reference type even when the standard
5417          says it does not.  Therefore, we have to manually obtain the
5418          underlying type here.  */
5419       scope = non_reference (scope);
5420       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5421       if (scope == unknown_type_node)
5422         {
5423           error_at (location, "%qE does not have class type",
5424                     postfix_expression);
5425           scope = NULL_TREE;
5426         }
5427       else
5428         scope = complete_type_or_else (scope, NULL_TREE);
5429       /* Let the name lookup machinery know that we are processing a
5430          class member access expression.  */
5431       parser->context->object_type = scope;
5432       /* If something went wrong, we want to be able to discern that case,
5433          as opposed to the case where there was no SCOPE due to the type
5434          of expression being dependent.  */
5435       if (!scope)
5436         scope = error_mark_node;
5437       /* If the SCOPE was erroneous, make the various semantic analysis
5438          functions exit quickly -- and without issuing additional error
5439          messages.  */
5440       if (scope == error_mark_node)
5441         postfix_expression = error_mark_node;
5442     }
5443
5444   /* Assume this expression is not a pseudo-destructor access.  */
5445   pseudo_destructor_p = false;
5446
5447   /* If the SCOPE is a scalar type, then, if this is a valid program,
5448      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5449      is type dependent, it can be pseudo-destructor-name or something else.
5450      Try to parse it as pseudo-destructor-name first.  */
5451   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5452     {
5453       tree s;
5454       tree type;
5455
5456       cp_parser_parse_tentatively (parser);
5457       /* Parse the pseudo-destructor-name.  */
5458       s = NULL_TREE;
5459       cp_parser_pseudo_destructor_name (parser, &s, &type);
5460       if (dependent_p
5461           && (cp_parser_error_occurred (parser)
5462               || TREE_CODE (type) != TYPE_DECL
5463               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5464         cp_parser_abort_tentative_parse (parser);
5465       else if (cp_parser_parse_definitely (parser))
5466         {
5467           pseudo_destructor_p = true;
5468           postfix_expression
5469             = finish_pseudo_destructor_expr (postfix_expression,
5470                                              s, TREE_TYPE (type));
5471         }
5472     }
5473
5474   if (!pseudo_destructor_p)
5475     {
5476       /* If the SCOPE is not a scalar type, we are looking at an
5477          ordinary class member access expression, rather than a
5478          pseudo-destructor-name.  */
5479       bool template_p;
5480       cp_token *token = cp_lexer_peek_token (parser->lexer);
5481       /* Parse the id-expression.  */
5482       name = (cp_parser_id_expression
5483               (parser,
5484                cp_parser_optional_template_keyword (parser),
5485                /*check_dependency_p=*/true,
5486                &template_p,
5487                /*declarator_p=*/false,
5488                /*optional_p=*/false));
5489       /* In general, build a SCOPE_REF if the member name is qualified.
5490          However, if the name was not dependent and has already been
5491          resolved; there is no need to build the SCOPE_REF.  For example;
5492
5493              struct X { void f(); };
5494              template <typename T> void f(T* t) { t->X::f(); }
5495
5496          Even though "t" is dependent, "X::f" is not and has been resolved
5497          to a BASELINK; there is no need to include scope information.  */
5498
5499       /* But we do need to remember that there was an explicit scope for
5500          virtual function calls.  */
5501       if (parser->scope)
5502         *idk = CP_ID_KIND_QUALIFIED;
5503
5504       /* If the name is a template-id that names a type, we will get a
5505          TYPE_DECL here.  That is invalid code.  */
5506       if (TREE_CODE (name) == TYPE_DECL)
5507         {
5508           error_at (token->location, "invalid use of %qD", name);
5509           postfix_expression = error_mark_node;
5510         }
5511       else
5512         {
5513           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5514             {
5515               name = build_qualified_name (/*type=*/NULL_TREE,
5516                                            parser->scope,
5517                                            name,
5518                                            template_p);
5519               parser->scope = NULL_TREE;
5520               parser->qualifying_scope = NULL_TREE;
5521               parser->object_scope = NULL_TREE;
5522             }
5523           if (scope && name && BASELINK_P (name))
5524             adjust_result_of_qualified_name_lookup
5525               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5526           postfix_expression
5527             = finish_class_member_access_expr (postfix_expression, name,
5528                                                template_p, 
5529                                                tf_warning_or_error);
5530         }
5531     }
5532
5533   /* We no longer need to look up names in the scope of the object on
5534      the left-hand side of the `.' or `->' operator.  */
5535   parser->context->object_type = NULL_TREE;
5536
5537   /* Outside of offsetof, these operators may not appear in
5538      constant-expressions.  */
5539   if (!for_offsetof
5540       && (cp_parser_non_integral_constant_expression
5541           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5542     postfix_expression = error_mark_node;
5543
5544   return postfix_expression;
5545 }
5546
5547 /* Parse a parenthesized expression-list.
5548
5549    expression-list:
5550      assignment-expression
5551      expression-list, assignment-expression
5552
5553    attribute-list:
5554      expression-list
5555      identifier
5556      identifier, expression-list
5557
5558    CAST_P is true if this expression is the target of a cast.
5559
5560    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5561    argument pack.
5562
5563    Returns a vector of trees.  Each element is a representation of an
5564    assignment-expression.  NULL is returned if the ( and or ) are
5565    missing.  An empty, but allocated, vector is returned on no
5566    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5567    if we are parsing an attribute list for an attribute that wants a
5568    plain identifier argument, normal_attr for an attribute that wants
5569    an expression, or non_attr if we aren't parsing an attribute list.  If
5570    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5571    not all of the expressions in the list were constant.  */
5572
5573 static VEC(tree,gc) *
5574 cp_parser_parenthesized_expression_list (cp_parser* parser,
5575                                          int is_attribute_list,
5576                                          bool cast_p,
5577                                          bool allow_expansion_p,
5578                                          bool *non_constant_p)
5579 {
5580   VEC(tree,gc) *expression_list;
5581   bool fold_expr_p = is_attribute_list != non_attr;
5582   tree identifier = NULL_TREE;
5583   bool saved_greater_than_is_operator_p;
5584
5585   /* Assume all the expressions will be constant.  */
5586   if (non_constant_p)
5587     *non_constant_p = false;
5588
5589   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5590     return NULL;
5591
5592   expression_list = make_tree_vector ();
5593
5594   /* Within a parenthesized expression, a `>' token is always
5595      the greater-than operator.  */
5596   saved_greater_than_is_operator_p
5597     = parser->greater_than_is_operator_p;
5598   parser->greater_than_is_operator_p = true;
5599
5600   /* Consume expressions until there are no more.  */
5601   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5602     while (true)
5603       {
5604         tree expr;
5605
5606         /* At the beginning of attribute lists, check to see if the
5607            next token is an identifier.  */
5608         if (is_attribute_list == id_attr
5609             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5610           {
5611             cp_token *token;
5612
5613             /* Consume the identifier.  */
5614             token = cp_lexer_consume_token (parser->lexer);
5615             /* Save the identifier.  */
5616             identifier = token->u.value;
5617           }
5618         else
5619           {
5620             bool expr_non_constant_p;
5621
5622             /* Parse the next assignment-expression.  */
5623             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5624               {
5625                 /* A braced-init-list.  */
5626                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5627                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5628                 if (non_constant_p && expr_non_constant_p)
5629                   *non_constant_p = true;
5630               }
5631             else if (non_constant_p)
5632               {
5633                 expr = (cp_parser_constant_expression
5634                         (parser, /*allow_non_constant_p=*/true,
5635                          &expr_non_constant_p));
5636                 if (expr_non_constant_p)
5637                   *non_constant_p = true;
5638               }
5639             else
5640               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5641
5642             if (fold_expr_p)
5643               expr = fold_non_dependent_expr (expr);
5644
5645             /* If we have an ellipsis, then this is an expression
5646                expansion.  */
5647             if (allow_expansion_p
5648                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5649               {
5650                 /* Consume the `...'.  */
5651                 cp_lexer_consume_token (parser->lexer);
5652
5653                 /* Build the argument pack.  */
5654                 expr = make_pack_expansion (expr);
5655               }
5656
5657              /* Add it to the list.  We add error_mark_node
5658                 expressions to the list, so that we can still tell if
5659                 the correct form for a parenthesized expression-list
5660                 is found. That gives better errors.  */
5661             VEC_safe_push (tree, gc, expression_list, expr);
5662
5663             if (expr == error_mark_node)
5664               goto skip_comma;
5665           }
5666
5667         /* After the first item, attribute lists look the same as
5668            expression lists.  */
5669         is_attribute_list = non_attr;
5670
5671       get_comma:;
5672         /* If the next token isn't a `,', then we are done.  */
5673         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5674           break;
5675
5676         /* Otherwise, consume the `,' and keep going.  */
5677         cp_lexer_consume_token (parser->lexer);
5678       }
5679
5680   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5681     {
5682       int ending;
5683
5684     skip_comma:;
5685       /* We try and resync to an unnested comma, as that will give the
5686          user better diagnostics.  */
5687       ending = cp_parser_skip_to_closing_parenthesis (parser,
5688                                                       /*recovering=*/true,
5689                                                       /*or_comma=*/true,
5690                                                       /*consume_paren=*/true);
5691       if (ending < 0)
5692         goto get_comma;
5693       if (!ending)
5694         {
5695           parser->greater_than_is_operator_p
5696             = saved_greater_than_is_operator_p;
5697           return NULL;
5698         }
5699     }
5700
5701   parser->greater_than_is_operator_p
5702     = saved_greater_than_is_operator_p;
5703
5704   if (identifier)
5705     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5706
5707   return expression_list;
5708 }
5709
5710 /* Parse a pseudo-destructor-name.
5711
5712    pseudo-destructor-name:
5713      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5714      :: [opt] nested-name-specifier template template-id :: ~ type-name
5715      :: [opt] nested-name-specifier [opt] ~ type-name
5716
5717    If either of the first two productions is used, sets *SCOPE to the
5718    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5719    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5720    or ERROR_MARK_NODE if the parse fails.  */
5721
5722 static void
5723 cp_parser_pseudo_destructor_name (cp_parser* parser,
5724                                   tree* scope,
5725                                   tree* type)
5726 {
5727   bool nested_name_specifier_p;
5728
5729   /* Assume that things will not work out.  */
5730   *type = error_mark_node;
5731
5732   /* Look for the optional `::' operator.  */
5733   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5734   /* Look for the optional nested-name-specifier.  */
5735   nested_name_specifier_p
5736     = (cp_parser_nested_name_specifier_opt (parser,
5737                                             /*typename_keyword_p=*/false,
5738                                             /*check_dependency_p=*/true,
5739                                             /*type_p=*/false,
5740                                             /*is_declaration=*/false)
5741        != NULL_TREE);
5742   /* Now, if we saw a nested-name-specifier, we might be doing the
5743      second production.  */
5744   if (nested_name_specifier_p
5745       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5746     {
5747       /* Consume the `template' keyword.  */
5748       cp_lexer_consume_token (parser->lexer);
5749       /* Parse the template-id.  */
5750       cp_parser_template_id (parser,
5751                              /*template_keyword_p=*/true,
5752                              /*check_dependency_p=*/false,
5753                              /*is_declaration=*/true);
5754       /* Look for the `::' token.  */
5755       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5756     }
5757   /* If the next token is not a `~', then there might be some
5758      additional qualification.  */
5759   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5760     {
5761       /* At this point, we're looking for "type-name :: ~".  The type-name
5762          must not be a class-name, since this is a pseudo-destructor.  So,
5763          it must be either an enum-name, or a typedef-name -- both of which
5764          are just identifiers.  So, we peek ahead to check that the "::"
5765          and "~" tokens are present; if they are not, then we can avoid
5766          calling type_name.  */
5767       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5768           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5769           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5770         {
5771           cp_parser_error (parser, "non-scalar type");
5772           return;
5773         }
5774
5775       /* Look for the type-name.  */
5776       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5777       if (*scope == error_mark_node)
5778         return;
5779
5780       /* Look for the `::' token.  */
5781       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5782     }
5783   else
5784     *scope = NULL_TREE;
5785
5786   /* Look for the `~'.  */
5787   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5788   /* Look for the type-name again.  We are not responsible for
5789      checking that it matches the first type-name.  */
5790   *type = cp_parser_nonclass_name (parser);
5791 }
5792
5793 /* Parse a unary-expression.
5794
5795    unary-expression:
5796      postfix-expression
5797      ++ cast-expression
5798      -- cast-expression
5799      unary-operator cast-expression
5800      sizeof unary-expression
5801      sizeof ( type-id )
5802      new-expression
5803      delete-expression
5804
5805    GNU Extensions:
5806
5807    unary-expression:
5808      __extension__ cast-expression
5809      __alignof__ unary-expression
5810      __alignof__ ( type-id )
5811      __real__ cast-expression
5812      __imag__ cast-expression
5813      && identifier
5814
5815    ADDRESS_P is true iff the unary-expression is appearing as the
5816    operand of the `&' operator.   CAST_P is true if this expression is
5817    the target of a cast.
5818
5819    Returns a representation of the expression.  */
5820
5821 static tree
5822 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5823                             cp_id_kind * pidk)
5824 {
5825   cp_token *token;
5826   enum tree_code unary_operator;
5827
5828   /* Peek at the next token.  */
5829   token = cp_lexer_peek_token (parser->lexer);
5830   /* Some keywords give away the kind of expression.  */
5831   if (token->type == CPP_KEYWORD)
5832     {
5833       enum rid keyword = token->keyword;
5834
5835       switch (keyword)
5836         {
5837         case RID_ALIGNOF:
5838         case RID_SIZEOF:
5839           {
5840             tree operand;
5841             enum tree_code op;
5842
5843             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5844             /* Consume the token.  */
5845             cp_lexer_consume_token (parser->lexer);
5846             /* Parse the operand.  */
5847             operand = cp_parser_sizeof_operand (parser, keyword);
5848
5849             if (TYPE_P (operand))
5850               return cxx_sizeof_or_alignof_type (operand, op, true);
5851             else
5852               return cxx_sizeof_or_alignof_expr (operand, op, true);
5853           }
5854
5855         case RID_NEW:
5856           return cp_parser_new_expression (parser);
5857
5858         case RID_DELETE:
5859           return cp_parser_delete_expression (parser);
5860
5861         case RID_EXTENSION:
5862           {
5863             /* The saved value of the PEDANTIC flag.  */
5864             int saved_pedantic;
5865             tree expr;
5866
5867             /* Save away the PEDANTIC flag.  */
5868             cp_parser_extension_opt (parser, &saved_pedantic);
5869             /* Parse the cast-expression.  */
5870             expr = cp_parser_simple_cast_expression (parser);
5871             /* Restore the PEDANTIC flag.  */
5872             pedantic = saved_pedantic;
5873
5874             return expr;
5875           }
5876
5877         case RID_REALPART:
5878         case RID_IMAGPART:
5879           {
5880             tree expression;
5881
5882             /* Consume the `__real__' or `__imag__' token.  */
5883             cp_lexer_consume_token (parser->lexer);
5884             /* Parse the cast-expression.  */
5885             expression = cp_parser_simple_cast_expression (parser);
5886             /* Create the complete representation.  */
5887             return build_x_unary_op ((keyword == RID_REALPART
5888                                       ? REALPART_EXPR : IMAGPART_EXPR),
5889                                      expression,
5890                                      tf_warning_or_error);
5891           }
5892           break;
5893
5894         case RID_NOEXCEPT:
5895           {
5896             tree expr;
5897             const char *saved_message;
5898             bool saved_integral_constant_expression_p;
5899             bool saved_non_integral_constant_expression_p;
5900             bool saved_greater_than_is_operator_p;
5901
5902             cp_lexer_consume_token (parser->lexer);
5903             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5904
5905             saved_message = parser->type_definition_forbidden_message;
5906             parser->type_definition_forbidden_message
5907               = G_("types may not be defined in %<noexcept%> expressions");
5908
5909             saved_integral_constant_expression_p
5910               = parser->integral_constant_expression_p;
5911             saved_non_integral_constant_expression_p
5912               = parser->non_integral_constant_expression_p;
5913             parser->integral_constant_expression_p = false;
5914
5915             saved_greater_than_is_operator_p
5916               = parser->greater_than_is_operator_p;
5917             parser->greater_than_is_operator_p = true;
5918
5919             ++cp_unevaluated_operand;
5920             ++c_inhibit_evaluation_warnings;
5921             expr = cp_parser_expression (parser, false, NULL);
5922             --c_inhibit_evaluation_warnings;
5923             --cp_unevaluated_operand;
5924
5925             parser->greater_than_is_operator_p
5926               = saved_greater_than_is_operator_p;
5927
5928             parser->integral_constant_expression_p
5929               = saved_integral_constant_expression_p;
5930             parser->non_integral_constant_expression_p
5931               = saved_non_integral_constant_expression_p;
5932
5933             parser->type_definition_forbidden_message = saved_message;
5934
5935             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5936             return finish_noexcept_expr (expr, tf_warning_or_error);
5937           }
5938
5939         default:
5940           break;
5941         }
5942     }
5943
5944   /* Look for the `:: new' and `:: delete', which also signal the
5945      beginning of a new-expression, or delete-expression,
5946      respectively.  If the next token is `::', then it might be one of
5947      these.  */
5948   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5949     {
5950       enum rid keyword;
5951
5952       /* See if the token after the `::' is one of the keywords in
5953          which we're interested.  */
5954       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5955       /* If it's `new', we have a new-expression.  */
5956       if (keyword == RID_NEW)
5957         return cp_parser_new_expression (parser);
5958       /* Similarly, for `delete'.  */
5959       else if (keyword == RID_DELETE)
5960         return cp_parser_delete_expression (parser);
5961     }
5962
5963   /* Look for a unary operator.  */
5964   unary_operator = cp_parser_unary_operator (token);
5965   /* The `++' and `--' operators can be handled similarly, even though
5966      they are not technically unary-operators in the grammar.  */
5967   if (unary_operator == ERROR_MARK)
5968     {
5969       if (token->type == CPP_PLUS_PLUS)
5970         unary_operator = PREINCREMENT_EXPR;
5971       else if (token->type == CPP_MINUS_MINUS)
5972         unary_operator = PREDECREMENT_EXPR;
5973       /* Handle the GNU address-of-label extension.  */
5974       else if (cp_parser_allow_gnu_extensions_p (parser)
5975                && token->type == CPP_AND_AND)
5976         {
5977           tree identifier;
5978           tree expression;
5979           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5980
5981           /* Consume the '&&' token.  */
5982           cp_lexer_consume_token (parser->lexer);
5983           /* Look for the identifier.  */
5984           identifier = cp_parser_identifier (parser);
5985           /* Create an expression representing the address.  */
5986           expression = finish_label_address_expr (identifier, loc);
5987           if (cp_parser_non_integral_constant_expression (parser,
5988                                                           NIC_ADDR_LABEL))
5989             expression = error_mark_node;
5990           return expression;
5991         }
5992     }
5993   if (unary_operator != ERROR_MARK)
5994     {
5995       tree cast_expression;
5996       tree expression = error_mark_node;
5997       non_integral_constant non_constant_p = NIC_NONE;
5998
5999       /* Consume the operator token.  */
6000       token = cp_lexer_consume_token (parser->lexer);
6001       /* Parse the cast-expression.  */
6002       cast_expression
6003         = cp_parser_cast_expression (parser,
6004                                      unary_operator == ADDR_EXPR,
6005                                      /*cast_p=*/false, pidk);
6006       /* Now, build an appropriate representation.  */
6007       switch (unary_operator)
6008         {
6009         case INDIRECT_REF:
6010           non_constant_p = NIC_STAR;
6011           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6012                                              tf_warning_or_error);
6013           break;
6014
6015         case ADDR_EXPR:
6016            non_constant_p = NIC_ADDR;
6017           /* Fall through.  */
6018         case BIT_NOT_EXPR:
6019           expression = build_x_unary_op (unary_operator, cast_expression,
6020                                          tf_warning_or_error);
6021           break;
6022
6023         case PREINCREMENT_EXPR:
6024         case PREDECREMENT_EXPR:
6025           non_constant_p = unary_operator == PREINCREMENT_EXPR
6026                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6027           /* Fall through.  */
6028         case UNARY_PLUS_EXPR:
6029         case NEGATE_EXPR:
6030         case TRUTH_NOT_EXPR:
6031           expression = finish_unary_op_expr (unary_operator, cast_expression);
6032           break;
6033
6034         default:
6035           gcc_unreachable ();
6036         }
6037
6038       if (non_constant_p != NIC_NONE
6039           && cp_parser_non_integral_constant_expression (parser,
6040                                                          non_constant_p))
6041         expression = error_mark_node;
6042
6043       return expression;
6044     }
6045
6046   return cp_parser_postfix_expression (parser, address_p, cast_p,
6047                                        /*member_access_only_p=*/false,
6048                                        pidk);
6049 }
6050
6051 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6052    unary-operator, the corresponding tree code is returned.  */
6053
6054 static enum tree_code
6055 cp_parser_unary_operator (cp_token* token)
6056 {
6057   switch (token->type)
6058     {
6059     case CPP_MULT:
6060       return INDIRECT_REF;
6061
6062     case CPP_AND:
6063       return ADDR_EXPR;
6064
6065     case CPP_PLUS:
6066       return UNARY_PLUS_EXPR;
6067
6068     case CPP_MINUS:
6069       return NEGATE_EXPR;
6070
6071     case CPP_NOT:
6072       return TRUTH_NOT_EXPR;
6073
6074     case CPP_COMPL:
6075       return BIT_NOT_EXPR;
6076
6077     default:
6078       return ERROR_MARK;
6079     }
6080 }
6081
6082 /* Parse a new-expression.
6083
6084    new-expression:
6085      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6086      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6087
6088    Returns a representation of the expression.  */
6089
6090 static tree
6091 cp_parser_new_expression (cp_parser* parser)
6092 {
6093   bool global_scope_p;
6094   VEC(tree,gc) *placement;
6095   tree type;
6096   VEC(tree,gc) *initializer;
6097   tree nelts;
6098   tree ret;
6099
6100   /* Look for the optional `::' operator.  */
6101   global_scope_p
6102     = (cp_parser_global_scope_opt (parser,
6103                                    /*current_scope_valid_p=*/false)
6104        != NULL_TREE);
6105   /* Look for the `new' operator.  */
6106   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6107   /* There's no easy way to tell a new-placement from the
6108      `( type-id )' construct.  */
6109   cp_parser_parse_tentatively (parser);
6110   /* Look for a new-placement.  */
6111   placement = cp_parser_new_placement (parser);
6112   /* If that didn't work out, there's no new-placement.  */
6113   if (!cp_parser_parse_definitely (parser))
6114     {
6115       if (placement != NULL)
6116         release_tree_vector (placement);
6117       placement = NULL;
6118     }
6119
6120   /* If the next token is a `(', then we have a parenthesized
6121      type-id.  */
6122   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6123     {
6124       cp_token *token;
6125       /* Consume the `('.  */
6126       cp_lexer_consume_token (parser->lexer);
6127       /* Parse the type-id.  */
6128       type = cp_parser_type_id (parser);
6129       /* Look for the closing `)'.  */
6130       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6131       token = cp_lexer_peek_token (parser->lexer);
6132       /* There should not be a direct-new-declarator in this production,
6133          but GCC used to allowed this, so we check and emit a sensible error
6134          message for this case.  */
6135       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6136         {
6137           error_at (token->location,
6138                     "array bound forbidden after parenthesized type-id");
6139           inform (token->location, 
6140                   "try removing the parentheses around the type-id");
6141           cp_parser_direct_new_declarator (parser);
6142         }
6143       nelts = NULL_TREE;
6144     }
6145   /* Otherwise, there must be a new-type-id.  */
6146   else
6147     type = cp_parser_new_type_id (parser, &nelts);
6148
6149   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6150   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6151       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6152     initializer = cp_parser_new_initializer (parser);
6153   else
6154     initializer = NULL;
6155
6156   /* A new-expression may not appear in an integral constant
6157      expression.  */
6158   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6159     ret = error_mark_node;
6160   else
6161     {
6162       /* Create a representation of the new-expression.  */
6163       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6164                        tf_warning_or_error);
6165     }
6166
6167   if (placement != NULL)
6168     release_tree_vector (placement);
6169   if (initializer != NULL)
6170     release_tree_vector (initializer);
6171
6172   return ret;
6173 }
6174
6175 /* Parse a new-placement.
6176
6177    new-placement:
6178      ( expression-list )
6179
6180    Returns the same representation as for an expression-list.  */
6181
6182 static VEC(tree,gc) *
6183 cp_parser_new_placement (cp_parser* parser)
6184 {
6185   VEC(tree,gc) *expression_list;
6186
6187   /* Parse the expression-list.  */
6188   expression_list = (cp_parser_parenthesized_expression_list
6189                      (parser, non_attr, /*cast_p=*/false,
6190                       /*allow_expansion_p=*/true,
6191                       /*non_constant_p=*/NULL));
6192
6193   return expression_list;
6194 }
6195
6196 /* Parse a new-type-id.
6197
6198    new-type-id:
6199      type-specifier-seq new-declarator [opt]
6200
6201    Returns the TYPE allocated.  If the new-type-id indicates an array
6202    type, *NELTS is set to the number of elements in the last array
6203    bound; the TYPE will not include the last array bound.  */
6204
6205 static tree
6206 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6207 {
6208   cp_decl_specifier_seq type_specifier_seq;
6209   cp_declarator *new_declarator;
6210   cp_declarator *declarator;
6211   cp_declarator *outer_declarator;
6212   const char *saved_message;
6213   tree type;
6214
6215   /* The type-specifier sequence must not contain type definitions.
6216      (It cannot contain declarations of new types either, but if they
6217      are not definitions we will catch that because they are not
6218      complete.)  */
6219   saved_message = parser->type_definition_forbidden_message;
6220   parser->type_definition_forbidden_message
6221     = G_("types may not be defined in a new-type-id");
6222   /* Parse the type-specifier-seq.  */
6223   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6224                                 /*is_trailing_return=*/false,
6225                                 &type_specifier_seq);
6226   /* Restore the old message.  */
6227   parser->type_definition_forbidden_message = saved_message;
6228   /* Parse the new-declarator.  */
6229   new_declarator = cp_parser_new_declarator_opt (parser);
6230
6231   /* Determine the number of elements in the last array dimension, if
6232      any.  */
6233   *nelts = NULL_TREE;
6234   /* Skip down to the last array dimension.  */
6235   declarator = new_declarator;
6236   outer_declarator = NULL;
6237   while (declarator && (declarator->kind == cdk_pointer
6238                         || declarator->kind == cdk_ptrmem))
6239     {
6240       outer_declarator = declarator;
6241       declarator = declarator->declarator;
6242     }
6243   while (declarator
6244          && declarator->kind == cdk_array
6245          && declarator->declarator
6246          && declarator->declarator->kind == cdk_array)
6247     {
6248       outer_declarator = declarator;
6249       declarator = declarator->declarator;
6250     }
6251
6252   if (declarator && declarator->kind == cdk_array)
6253     {
6254       *nelts = declarator->u.array.bounds;
6255       if (*nelts == error_mark_node)
6256         *nelts = integer_one_node;
6257
6258       if (outer_declarator)
6259         outer_declarator->declarator = declarator->declarator;
6260       else
6261         new_declarator = NULL;
6262     }
6263
6264   type = groktypename (&type_specifier_seq, new_declarator, false);
6265   return type;
6266 }
6267
6268 /* Parse an (optional) new-declarator.
6269
6270    new-declarator:
6271      ptr-operator new-declarator [opt]
6272      direct-new-declarator
6273
6274    Returns the declarator.  */
6275
6276 static cp_declarator *
6277 cp_parser_new_declarator_opt (cp_parser* parser)
6278 {
6279   enum tree_code code;
6280   tree type;
6281   cp_cv_quals cv_quals;
6282
6283   /* We don't know if there's a ptr-operator next, or not.  */
6284   cp_parser_parse_tentatively (parser);
6285   /* Look for a ptr-operator.  */
6286   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6287   /* If that worked, look for more new-declarators.  */
6288   if (cp_parser_parse_definitely (parser))
6289     {
6290       cp_declarator *declarator;
6291
6292       /* Parse another optional declarator.  */
6293       declarator = cp_parser_new_declarator_opt (parser);
6294
6295       return cp_parser_make_indirect_declarator
6296         (code, type, cv_quals, declarator);
6297     }
6298
6299   /* If the next token is a `[', there is a direct-new-declarator.  */
6300   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6301     return cp_parser_direct_new_declarator (parser);
6302
6303   return NULL;
6304 }
6305
6306 /* Parse a direct-new-declarator.
6307
6308    direct-new-declarator:
6309      [ expression ]
6310      direct-new-declarator [constant-expression]
6311
6312    */
6313
6314 static cp_declarator *
6315 cp_parser_direct_new_declarator (cp_parser* parser)
6316 {
6317   cp_declarator *declarator = NULL;
6318
6319   while (true)
6320     {
6321       tree expression;
6322
6323       /* Look for the opening `['.  */
6324       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6325       /* The first expression is not required to be constant.  */
6326       if (!declarator)
6327         {
6328           cp_token *token = cp_lexer_peek_token (parser->lexer);
6329           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6330           /* The standard requires that the expression have integral
6331              type.  DR 74 adds enumeration types.  We believe that the
6332              real intent is that these expressions be handled like the
6333              expression in a `switch' condition, which also allows
6334              classes with a single conversion to integral or
6335              enumeration type.  */
6336           if (!processing_template_decl)
6337             {
6338               expression
6339                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6340                                               expression,
6341                                               /*complain=*/true);
6342               if (!expression)
6343                 {
6344                   error_at (token->location,
6345                             "expression in new-declarator must have integral "
6346                             "or enumeration type");
6347                   expression = error_mark_node;
6348                 }
6349             }
6350         }
6351       /* But all the other expressions must be.  */
6352       else
6353         expression
6354           = cp_parser_constant_expression (parser,
6355                                            /*allow_non_constant=*/false,
6356                                            NULL);
6357       /* Look for the closing `]'.  */
6358       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6359
6360       /* Add this bound to the declarator.  */
6361       declarator = make_array_declarator (declarator, expression);
6362
6363       /* If the next token is not a `[', then there are no more
6364          bounds.  */
6365       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6366         break;
6367     }
6368
6369   return declarator;
6370 }
6371
6372 /* Parse a new-initializer.
6373
6374    new-initializer:
6375      ( expression-list [opt] )
6376      braced-init-list
6377
6378    Returns a representation of the expression-list.  */
6379
6380 static VEC(tree,gc) *
6381 cp_parser_new_initializer (cp_parser* parser)
6382 {
6383   VEC(tree,gc) *expression_list;
6384
6385   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6386     {
6387       tree t;
6388       bool expr_non_constant_p;
6389       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6390       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6391       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6392       expression_list = make_tree_vector_single (t);
6393     }
6394   else
6395     expression_list = (cp_parser_parenthesized_expression_list
6396                        (parser, non_attr, /*cast_p=*/false,
6397                         /*allow_expansion_p=*/true,
6398                         /*non_constant_p=*/NULL));
6399
6400   return expression_list;
6401 }
6402
6403 /* Parse a delete-expression.
6404
6405    delete-expression:
6406      :: [opt] delete cast-expression
6407      :: [opt] delete [ ] cast-expression
6408
6409    Returns a representation of the expression.  */
6410
6411 static tree
6412 cp_parser_delete_expression (cp_parser* parser)
6413 {
6414   bool global_scope_p;
6415   bool array_p;
6416   tree expression;
6417
6418   /* Look for the optional `::' operator.  */
6419   global_scope_p
6420     = (cp_parser_global_scope_opt (parser,
6421                                    /*current_scope_valid_p=*/false)
6422        != NULL_TREE);
6423   /* Look for the `delete' keyword.  */
6424   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6425   /* See if the array syntax is in use.  */
6426   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6427     {
6428       /* Consume the `[' token.  */
6429       cp_lexer_consume_token (parser->lexer);
6430       /* Look for the `]' token.  */
6431       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6432       /* Remember that this is the `[]' construct.  */
6433       array_p = true;
6434     }
6435   else
6436     array_p = false;
6437
6438   /* Parse the cast-expression.  */
6439   expression = cp_parser_simple_cast_expression (parser);
6440
6441   /* A delete-expression may not appear in an integral constant
6442      expression.  */
6443   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6444     return error_mark_node;
6445
6446   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6447 }
6448
6449 /* Returns true if TOKEN may start a cast-expression and false
6450    otherwise.  */
6451
6452 static bool
6453 cp_parser_token_starts_cast_expression (cp_token *token)
6454 {
6455   switch (token->type)
6456     {
6457     case CPP_COMMA:
6458     case CPP_SEMICOLON:
6459     case CPP_QUERY:
6460     case CPP_COLON:
6461     case CPP_CLOSE_SQUARE:
6462     case CPP_CLOSE_PAREN:
6463     case CPP_CLOSE_BRACE:
6464     case CPP_DOT:
6465     case CPP_DOT_STAR:
6466     case CPP_DEREF:
6467     case CPP_DEREF_STAR:
6468     case CPP_DIV:
6469     case CPP_MOD:
6470     case CPP_LSHIFT:
6471     case CPP_RSHIFT:
6472     case CPP_LESS:
6473     case CPP_GREATER:
6474     case CPP_LESS_EQ:
6475     case CPP_GREATER_EQ:
6476     case CPP_EQ_EQ:
6477     case CPP_NOT_EQ:
6478     case CPP_EQ:
6479     case CPP_MULT_EQ:
6480     case CPP_DIV_EQ:
6481     case CPP_MOD_EQ:
6482     case CPP_PLUS_EQ:
6483     case CPP_MINUS_EQ:
6484     case CPP_RSHIFT_EQ:
6485     case CPP_LSHIFT_EQ:
6486     case CPP_AND_EQ:
6487     case CPP_XOR_EQ:
6488     case CPP_OR_EQ:
6489     case CPP_XOR:
6490     case CPP_OR:
6491     case CPP_OR_OR:
6492     case CPP_EOF:
6493       return false;
6494
6495       /* '[' may start a primary-expression in obj-c++.  */
6496     case CPP_OPEN_SQUARE:
6497       return c_dialect_objc ();
6498
6499     default:
6500       return true;
6501     }
6502 }
6503
6504 /* Parse a cast-expression.
6505
6506    cast-expression:
6507      unary-expression
6508      ( type-id ) cast-expression
6509
6510    ADDRESS_P is true iff the unary-expression is appearing as the
6511    operand of the `&' operator.   CAST_P is true if this expression is
6512    the target of a cast.
6513
6514    Returns a representation of the expression.  */
6515
6516 static tree
6517 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6518                            cp_id_kind * pidk)
6519 {
6520   /* If it's a `(', then we might be looking at a cast.  */
6521   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6522     {
6523       tree type = NULL_TREE;
6524       tree expr = NULL_TREE;
6525       bool compound_literal_p;
6526       const char *saved_message;
6527
6528       /* There's no way to know yet whether or not this is a cast.
6529          For example, `(int (3))' is a unary-expression, while `(int)
6530          3' is a cast.  So, we resort to parsing tentatively.  */
6531       cp_parser_parse_tentatively (parser);
6532       /* Types may not be defined in a cast.  */
6533       saved_message = parser->type_definition_forbidden_message;
6534       parser->type_definition_forbidden_message
6535         = G_("types may not be defined in casts");
6536       /* Consume the `('.  */
6537       cp_lexer_consume_token (parser->lexer);
6538       /* A very tricky bit is that `(struct S) { 3 }' is a
6539          compound-literal (which we permit in C++ as an extension).
6540          But, that construct is not a cast-expression -- it is a
6541          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6542          is legal; if the compound-literal were a cast-expression,
6543          you'd need an extra set of parentheses.)  But, if we parse
6544          the type-id, and it happens to be a class-specifier, then we
6545          will commit to the parse at that point, because we cannot
6546          undo the action that is done when creating a new class.  So,
6547          then we cannot back up and do a postfix-expression.
6548
6549          Therefore, we scan ahead to the closing `)', and check to see
6550          if the token after the `)' is a `{'.  If so, we are not
6551          looking at a cast-expression.
6552
6553          Save tokens so that we can put them back.  */
6554       cp_lexer_save_tokens (parser->lexer);
6555       /* Skip tokens until the next token is a closing parenthesis.
6556          If we find the closing `)', and the next token is a `{', then
6557          we are looking at a compound-literal.  */
6558       compound_literal_p
6559         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6560                                                   /*consume_paren=*/true)
6561            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6562       /* Roll back the tokens we skipped.  */
6563       cp_lexer_rollback_tokens (parser->lexer);
6564       /* If we were looking at a compound-literal, simulate an error
6565          so that the call to cp_parser_parse_definitely below will
6566          fail.  */
6567       if (compound_literal_p)
6568         cp_parser_simulate_error (parser);
6569       else
6570         {
6571           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6572           parser->in_type_id_in_expr_p = true;
6573           /* Look for the type-id.  */
6574           type = cp_parser_type_id (parser);
6575           /* Look for the closing `)'.  */
6576           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6577           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6578         }
6579
6580       /* Restore the saved message.  */
6581       parser->type_definition_forbidden_message = saved_message;
6582
6583       /* At this point this can only be either a cast or a
6584          parenthesized ctor such as `(T ())' that looks like a cast to
6585          function returning T.  */
6586       if (!cp_parser_error_occurred (parser)
6587           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6588                                                      (parser->lexer)))
6589         {
6590           cp_parser_parse_definitely (parser);
6591           expr = cp_parser_cast_expression (parser,
6592                                             /*address_p=*/false,
6593                                             /*cast_p=*/true, pidk);
6594
6595           /* Warn about old-style casts, if so requested.  */
6596           if (warn_old_style_cast
6597               && !in_system_header
6598               && !VOID_TYPE_P (type)
6599               && current_lang_name != lang_name_c)
6600             warning (OPT_Wold_style_cast, "use of old-style cast");
6601
6602           /* Only type conversions to integral or enumeration types
6603              can be used in constant-expressions.  */
6604           if (!cast_valid_in_integral_constant_expression_p (type)
6605               && cp_parser_non_integral_constant_expression (parser,
6606                                                              NIC_CAST))
6607             return error_mark_node;
6608
6609           /* Perform the cast.  */
6610           expr = build_c_cast (input_location, type, expr);
6611           return expr;
6612         }
6613       else 
6614         cp_parser_abort_tentative_parse (parser);
6615     }
6616
6617   /* If we get here, then it's not a cast, so it must be a
6618      unary-expression.  */
6619   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6620 }
6621
6622 /* Parse a binary expression of the general form:
6623
6624    pm-expression:
6625      cast-expression
6626      pm-expression .* cast-expression
6627      pm-expression ->* cast-expression
6628
6629    multiplicative-expression:
6630      pm-expression
6631      multiplicative-expression * pm-expression
6632      multiplicative-expression / pm-expression
6633      multiplicative-expression % pm-expression
6634
6635    additive-expression:
6636      multiplicative-expression
6637      additive-expression + multiplicative-expression
6638      additive-expression - multiplicative-expression
6639
6640    shift-expression:
6641      additive-expression
6642      shift-expression << additive-expression
6643      shift-expression >> additive-expression
6644
6645    relational-expression:
6646      shift-expression
6647      relational-expression < shift-expression
6648      relational-expression > shift-expression
6649      relational-expression <= shift-expression
6650      relational-expression >= shift-expression
6651
6652   GNU Extension:
6653
6654    relational-expression:
6655      relational-expression <? shift-expression
6656      relational-expression >? shift-expression
6657
6658    equality-expression:
6659      relational-expression
6660      equality-expression == relational-expression
6661      equality-expression != relational-expression
6662
6663    and-expression:
6664      equality-expression
6665      and-expression & equality-expression
6666
6667    exclusive-or-expression:
6668      and-expression
6669      exclusive-or-expression ^ and-expression
6670
6671    inclusive-or-expression:
6672      exclusive-or-expression
6673      inclusive-or-expression | exclusive-or-expression
6674
6675    logical-and-expression:
6676      inclusive-or-expression
6677      logical-and-expression && inclusive-or-expression
6678
6679    logical-or-expression:
6680      logical-and-expression
6681      logical-or-expression || logical-and-expression
6682
6683    All these are implemented with a single function like:
6684
6685    binary-expression:
6686      simple-cast-expression
6687      binary-expression <token> binary-expression
6688
6689    CAST_P is true if this expression is the target of a cast.
6690
6691    The binops_by_token map is used to get the tree codes for each <token> type.
6692    binary-expressions are associated according to a precedence table.  */
6693
6694 #define TOKEN_PRECEDENCE(token)                              \
6695 (((token->type == CPP_GREATER                                \
6696    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6697   && !parser->greater_than_is_operator_p)                    \
6698  ? PREC_NOT_OPERATOR                                         \
6699  : binops_by_token[token->type].prec)
6700
6701 static tree
6702 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6703                              bool no_toplevel_fold_p,
6704                              enum cp_parser_prec prec,
6705                              cp_id_kind * pidk)
6706 {
6707   cp_parser_expression_stack stack;
6708   cp_parser_expression_stack_entry *sp = &stack[0];
6709   tree lhs, rhs;
6710   cp_token *token;
6711   enum tree_code tree_type, lhs_type, rhs_type;
6712   enum cp_parser_prec new_prec, lookahead_prec;
6713   bool overloaded_p;
6714
6715   /* Parse the first expression.  */
6716   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6717   lhs_type = ERROR_MARK;
6718
6719   for (;;)
6720     {
6721       /* Get an operator token.  */
6722       token = cp_lexer_peek_token (parser->lexer);
6723
6724       if (warn_cxx0x_compat
6725           && token->type == CPP_RSHIFT
6726           && !parser->greater_than_is_operator_p)
6727         {
6728           if (warning_at (token->location, OPT_Wc__0x_compat, 
6729                           "%<>>%> operator will be treated as"
6730                           " two right angle brackets in C++0x"))
6731             inform (token->location,
6732                     "suggest parentheses around %<>>%> expression");
6733         }
6734
6735       new_prec = TOKEN_PRECEDENCE (token);
6736
6737       /* Popping an entry off the stack means we completed a subexpression:
6738          - either we found a token which is not an operator (`>' where it is not
6739            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6740            will happen repeatedly;
6741          - or, we found an operator which has lower priority.  This is the case
6742            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6743            parsing `3 * 4'.  */
6744       if (new_prec <= prec)
6745         {
6746           if (sp == stack)
6747             break;
6748           else
6749             goto pop;
6750         }
6751
6752      get_rhs:
6753       tree_type = binops_by_token[token->type].tree_type;
6754
6755       /* We used the operator token.  */
6756       cp_lexer_consume_token (parser->lexer);
6757
6758       /* For "false && x" or "true || x", x will never be executed;
6759          disable warnings while evaluating it.  */
6760       if (tree_type == TRUTH_ANDIF_EXPR)
6761         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6762       else if (tree_type == TRUTH_ORIF_EXPR)
6763         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6764
6765       /* Extract another operand.  It may be the RHS of this expression
6766          or the LHS of a new, higher priority expression.  */
6767       rhs = cp_parser_simple_cast_expression (parser);
6768       rhs_type = ERROR_MARK;
6769
6770       /* Get another operator token.  Look up its precedence to avoid
6771          building a useless (immediately popped) stack entry for common
6772          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6773       token = cp_lexer_peek_token (parser->lexer);
6774       lookahead_prec = TOKEN_PRECEDENCE (token);
6775       if (lookahead_prec > new_prec)
6776         {
6777           /* ... and prepare to parse the RHS of the new, higher priority
6778              expression.  Since precedence levels on the stack are
6779              monotonically increasing, we do not have to care about
6780              stack overflows.  */
6781           sp->prec = prec;
6782           sp->tree_type = tree_type;
6783           sp->lhs = lhs;
6784           sp->lhs_type = lhs_type;
6785           sp++;
6786           lhs = rhs;
6787           lhs_type = rhs_type;
6788           prec = new_prec;
6789           new_prec = lookahead_prec;
6790           goto get_rhs;
6791
6792          pop:
6793           lookahead_prec = new_prec;
6794           /* If the stack is not empty, we have parsed into LHS the right side
6795              (`4' in the example above) of an expression we had suspended.
6796              We can use the information on the stack to recover the LHS (`3')
6797              from the stack together with the tree code (`MULT_EXPR'), and
6798              the precedence of the higher level subexpression
6799              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6800              which will be used to actually build the additive expression.  */
6801           --sp;
6802           prec = sp->prec;
6803           tree_type = sp->tree_type;
6804           rhs = lhs;
6805           rhs_type = lhs_type;
6806           lhs = sp->lhs;
6807           lhs_type = sp->lhs_type;
6808         }
6809
6810       /* Undo the disabling of warnings done above.  */
6811       if (tree_type == TRUTH_ANDIF_EXPR)
6812         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6813       else if (tree_type == TRUTH_ORIF_EXPR)
6814         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6815
6816       overloaded_p = false;
6817       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6818          ERROR_MARK for everything that is not a binary expression.
6819          This makes warn_about_parentheses miss some warnings that
6820          involve unary operators.  For unary expressions we should
6821          pass the correct tree_code unless the unary expression was
6822          surrounded by parentheses.
6823       */
6824       if (no_toplevel_fold_p
6825           && lookahead_prec <= prec
6826           && sp == stack
6827           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6828         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6829       else
6830         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6831                                  &overloaded_p, tf_warning_or_error);
6832       lhs_type = tree_type;
6833
6834       /* If the binary operator required the use of an overloaded operator,
6835          then this expression cannot be an integral constant-expression.
6836          An overloaded operator can be used even if both operands are
6837          otherwise permissible in an integral constant-expression if at
6838          least one of the operands is of enumeration type.  */
6839
6840       if (overloaded_p
6841           && cp_parser_non_integral_constant_expression (parser,
6842                                                          NIC_OVERLOADED))
6843         return error_mark_node;
6844     }
6845
6846   return lhs;
6847 }
6848
6849
6850 /* Parse the `? expression : assignment-expression' part of a
6851    conditional-expression.  The LOGICAL_OR_EXPR is the
6852    logical-or-expression that started the conditional-expression.
6853    Returns a representation of the entire conditional-expression.
6854
6855    This routine is used by cp_parser_assignment_expression.
6856
6857      ? expression : assignment-expression
6858
6859    GNU Extensions:
6860
6861      ? : assignment-expression */
6862
6863 static tree
6864 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6865 {
6866   tree expr;
6867   tree assignment_expr;
6868   struct cp_token *token;
6869
6870   /* Consume the `?' token.  */
6871   cp_lexer_consume_token (parser->lexer);
6872   token = cp_lexer_peek_token (parser->lexer);
6873   if (cp_parser_allow_gnu_extensions_p (parser)
6874       && token->type == CPP_COLON)
6875     {
6876       pedwarn (token->location, OPT_pedantic, 
6877                "ISO C++ does not allow ?: with omitted middle operand");
6878       /* Implicit true clause.  */
6879       expr = NULL_TREE;
6880       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6881       warn_for_omitted_condop (token->location, logical_or_expr);
6882     }
6883   else
6884     {
6885       /* Parse the expression.  */
6886       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6887       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6888       c_inhibit_evaluation_warnings +=
6889         ((logical_or_expr == truthvalue_true_node)
6890          - (logical_or_expr == truthvalue_false_node));
6891     }
6892
6893   /* The next token should be a `:'.  */
6894   cp_parser_require (parser, CPP_COLON, RT_COLON);
6895   /* Parse the assignment-expression.  */
6896   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6897   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6898
6899   /* Build the conditional-expression.  */
6900   return build_x_conditional_expr (logical_or_expr,
6901                                    expr,
6902                                    assignment_expr,
6903                                    tf_warning_or_error);
6904 }
6905
6906 /* Parse an assignment-expression.
6907
6908    assignment-expression:
6909      conditional-expression
6910      logical-or-expression assignment-operator assignment_expression
6911      throw-expression
6912
6913    CAST_P is true if this expression is the target of a cast.
6914
6915    Returns a representation for the expression.  */
6916
6917 static tree
6918 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6919                                  cp_id_kind * pidk)
6920 {
6921   tree expr;
6922
6923   /* If the next token is the `throw' keyword, then we're looking at
6924      a throw-expression.  */
6925   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6926     expr = cp_parser_throw_expression (parser);
6927   /* Otherwise, it must be that we are looking at a
6928      logical-or-expression.  */
6929   else
6930     {
6931       /* Parse the binary expressions (logical-or-expression).  */
6932       expr = cp_parser_binary_expression (parser, cast_p, false,
6933                                           PREC_NOT_OPERATOR, pidk);
6934       /* If the next token is a `?' then we're actually looking at a
6935          conditional-expression.  */
6936       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6937         return cp_parser_question_colon_clause (parser, expr);
6938       else
6939         {
6940           enum tree_code assignment_operator;
6941
6942           /* If it's an assignment-operator, we're using the second
6943              production.  */
6944           assignment_operator
6945             = cp_parser_assignment_operator_opt (parser);
6946           if (assignment_operator != ERROR_MARK)
6947             {
6948               bool non_constant_p;
6949
6950               /* Parse the right-hand side of the assignment.  */
6951               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6952
6953               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6954                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6955
6956               /* An assignment may not appear in a
6957                  constant-expression.  */
6958               if (cp_parser_non_integral_constant_expression (parser,
6959                                                               NIC_ASSIGNMENT))
6960                 return error_mark_node;
6961               /* Build the assignment expression.  */
6962               expr = build_x_modify_expr (expr,
6963                                           assignment_operator,
6964                                           rhs,
6965                                           tf_warning_or_error);
6966             }
6967         }
6968     }
6969
6970   return expr;
6971 }
6972
6973 /* Parse an (optional) assignment-operator.
6974
6975    assignment-operator: one of
6976      = *= /= %= += -= >>= <<= &= ^= |=
6977
6978    GNU Extension:
6979
6980    assignment-operator: one of
6981      <?= >?=
6982
6983    If the next token is an assignment operator, the corresponding tree
6984    code is returned, and the token is consumed.  For example, for
6985    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6986    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6987    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6988    operator, ERROR_MARK is returned.  */
6989
6990 static enum tree_code
6991 cp_parser_assignment_operator_opt (cp_parser* parser)
6992 {
6993   enum tree_code op;
6994   cp_token *token;
6995
6996   /* Peek at the next token.  */
6997   token = cp_lexer_peek_token (parser->lexer);
6998
6999   switch (token->type)
7000     {
7001     case CPP_EQ:
7002       op = NOP_EXPR;
7003       break;
7004
7005     case CPP_MULT_EQ:
7006       op = MULT_EXPR;
7007       break;
7008
7009     case CPP_DIV_EQ:
7010       op = TRUNC_DIV_EXPR;
7011       break;
7012
7013     case CPP_MOD_EQ:
7014       op = TRUNC_MOD_EXPR;
7015       break;
7016
7017     case CPP_PLUS_EQ:
7018       op = PLUS_EXPR;
7019       break;
7020
7021     case CPP_MINUS_EQ:
7022       op = MINUS_EXPR;
7023       break;
7024
7025     case CPP_RSHIFT_EQ:
7026       op = RSHIFT_EXPR;
7027       break;
7028
7029     case CPP_LSHIFT_EQ:
7030       op = LSHIFT_EXPR;
7031       break;
7032
7033     case CPP_AND_EQ:
7034       op = BIT_AND_EXPR;
7035       break;
7036
7037     case CPP_XOR_EQ:
7038       op = BIT_XOR_EXPR;
7039       break;
7040
7041     case CPP_OR_EQ:
7042       op = BIT_IOR_EXPR;
7043       break;
7044
7045     default:
7046       /* Nothing else is an assignment operator.  */
7047       op = ERROR_MARK;
7048     }
7049
7050   /* If it was an assignment operator, consume it.  */
7051   if (op != ERROR_MARK)
7052     cp_lexer_consume_token (parser->lexer);
7053
7054   return op;
7055 }
7056
7057 /* Parse an expression.
7058
7059    expression:
7060      assignment-expression
7061      expression , assignment-expression
7062
7063    CAST_P is true if this expression is the target of a cast.
7064
7065    Returns a representation of the expression.  */
7066
7067 static tree
7068 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7069 {
7070   tree expression = NULL_TREE;
7071
7072   while (true)
7073     {
7074       tree assignment_expression;
7075
7076       /* Parse the next assignment-expression.  */
7077       assignment_expression
7078         = cp_parser_assignment_expression (parser, cast_p, pidk);
7079       /* If this is the first assignment-expression, we can just
7080          save it away.  */
7081       if (!expression)
7082         expression = assignment_expression;
7083       else
7084         expression = build_x_compound_expr (expression,
7085                                             assignment_expression,
7086                                             tf_warning_or_error);
7087       /* If the next token is not a comma, then we are done with the
7088          expression.  */
7089       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7090         break;
7091       /* Consume the `,'.  */
7092       cp_lexer_consume_token (parser->lexer);
7093       /* A comma operator cannot appear in a constant-expression.  */
7094       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7095         expression = error_mark_node;
7096     }
7097
7098   return expression;
7099 }
7100
7101 /* Parse a constant-expression.
7102
7103    constant-expression:
7104      conditional-expression
7105
7106   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7107   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7108   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7109   is false, NON_CONSTANT_P should be NULL.  */
7110
7111 static tree
7112 cp_parser_constant_expression (cp_parser* parser,
7113                                bool allow_non_constant_p,
7114                                bool *non_constant_p)
7115 {
7116   bool saved_integral_constant_expression_p;
7117   bool saved_allow_non_integral_constant_expression_p;
7118   bool saved_non_integral_constant_expression_p;
7119   tree expression;
7120
7121   /* It might seem that we could simply parse the
7122      conditional-expression, and then check to see if it were
7123      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7124      one that the compiler can figure out is constant, possibly after
7125      doing some simplifications or optimizations.  The standard has a
7126      precise definition of constant-expression, and we must honor
7127      that, even though it is somewhat more restrictive.
7128
7129      For example:
7130
7131        int i[(2, 3)];
7132
7133      is not a legal declaration, because `(2, 3)' is not a
7134      constant-expression.  The `,' operator is forbidden in a
7135      constant-expression.  However, GCC's constant-folding machinery
7136      will fold this operation to an INTEGER_CST for `3'.  */
7137
7138   /* Save the old settings.  */
7139   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7140   saved_allow_non_integral_constant_expression_p
7141     = parser->allow_non_integral_constant_expression_p;
7142   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7143   /* We are now parsing a constant-expression.  */
7144   parser->integral_constant_expression_p = true;
7145   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
7146   parser->non_integral_constant_expression_p = false;
7147   /* Although the grammar says "conditional-expression", we parse an
7148      "assignment-expression", which also permits "throw-expression"
7149      and the use of assignment operators.  In the case that
7150      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7151      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7152      actually essential that we look for an assignment-expression.
7153      For example, cp_parser_initializer_clauses uses this function to
7154      determine whether a particular assignment-expression is in fact
7155      constant.  */
7156   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7157   /* Restore the old settings.  */
7158   parser->integral_constant_expression_p
7159     = saved_integral_constant_expression_p;
7160   parser->allow_non_integral_constant_expression_p
7161     = saved_allow_non_integral_constant_expression_p;
7162   if (allow_non_constant_p)
7163     *non_constant_p = parser->non_integral_constant_expression_p;
7164   else if (parser->non_integral_constant_expression_p)
7165     expression = error_mark_node;
7166   parser->non_integral_constant_expression_p
7167     = saved_non_integral_constant_expression_p;
7168
7169   return expression;
7170 }
7171
7172 /* Parse __builtin_offsetof.
7173
7174    offsetof-expression:
7175      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7176
7177    offsetof-member-designator:
7178      id-expression
7179      | offsetof-member-designator "." id-expression
7180      | offsetof-member-designator "[" expression "]"
7181      | offsetof-member-designator "->" id-expression  */
7182
7183 static tree
7184 cp_parser_builtin_offsetof (cp_parser *parser)
7185 {
7186   int save_ice_p, save_non_ice_p;
7187   tree type, expr;
7188   cp_id_kind dummy;
7189   cp_token *token;
7190
7191   /* We're about to accept non-integral-constant things, but will
7192      definitely yield an integral constant expression.  Save and
7193      restore these values around our local parsing.  */
7194   save_ice_p = parser->integral_constant_expression_p;
7195   save_non_ice_p = parser->non_integral_constant_expression_p;
7196
7197   /* Consume the "__builtin_offsetof" token.  */
7198   cp_lexer_consume_token (parser->lexer);
7199   /* Consume the opening `('.  */
7200   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7201   /* Parse the type-id.  */
7202   type = cp_parser_type_id (parser);
7203   /* Look for the `,'.  */
7204   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7205   token = cp_lexer_peek_token (parser->lexer);
7206
7207   /* Build the (type *)null that begins the traditional offsetof macro.  */
7208   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7209                             tf_warning_or_error);
7210
7211   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7212   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7213                                                  true, &dummy, token->location);
7214   while (true)
7215     {
7216       token = cp_lexer_peek_token (parser->lexer);
7217       switch (token->type)
7218         {
7219         case CPP_OPEN_SQUARE:
7220           /* offsetof-member-designator "[" expression "]" */
7221           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7222           break;
7223
7224         case CPP_DEREF:
7225           /* offsetof-member-designator "->" identifier */
7226           expr = grok_array_decl (expr, integer_zero_node);
7227           /* FALLTHRU */
7228
7229         case CPP_DOT:
7230           /* offsetof-member-designator "." identifier */
7231           cp_lexer_consume_token (parser->lexer);
7232           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7233                                                          expr, true, &dummy,
7234                                                          token->location);
7235           break;
7236
7237         case CPP_CLOSE_PAREN:
7238           /* Consume the ")" token.  */
7239           cp_lexer_consume_token (parser->lexer);
7240           goto success;
7241
7242         default:
7243           /* Error.  We know the following require will fail, but
7244              that gives the proper error message.  */
7245           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7246           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7247           expr = error_mark_node;
7248           goto failure;
7249         }
7250     }
7251
7252  success:
7253   /* If we're processing a template, we can't finish the semantics yet.
7254      Otherwise we can fold the entire expression now.  */
7255   if (processing_template_decl)
7256     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7257   else
7258     expr = finish_offsetof (expr);
7259
7260  failure:
7261   parser->integral_constant_expression_p = save_ice_p;
7262   parser->non_integral_constant_expression_p = save_non_ice_p;
7263
7264   return expr;
7265 }
7266
7267 /* Parse a trait expression.  */
7268
7269 static tree
7270 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7271 {
7272   cp_trait_kind kind;
7273   tree type1, type2 = NULL_TREE;
7274   bool binary = false;
7275   cp_decl_specifier_seq decl_specs;
7276
7277   switch (keyword)
7278     {
7279     case RID_HAS_NOTHROW_ASSIGN:
7280       kind = CPTK_HAS_NOTHROW_ASSIGN;
7281       break;
7282     case RID_HAS_NOTHROW_CONSTRUCTOR:
7283       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7284       break;
7285     case RID_HAS_NOTHROW_COPY:
7286       kind = CPTK_HAS_NOTHROW_COPY;
7287       break;
7288     case RID_HAS_TRIVIAL_ASSIGN:
7289       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7290       break;
7291     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7292       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7293       break;
7294     case RID_HAS_TRIVIAL_COPY:
7295       kind = CPTK_HAS_TRIVIAL_COPY;
7296       break;
7297     case RID_HAS_TRIVIAL_DESTRUCTOR:
7298       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7299       break;
7300     case RID_HAS_VIRTUAL_DESTRUCTOR:
7301       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7302       break;
7303     case RID_IS_ABSTRACT:
7304       kind = CPTK_IS_ABSTRACT;
7305       break;
7306     case RID_IS_BASE_OF:
7307       kind = CPTK_IS_BASE_OF;
7308       binary = true;
7309       break;
7310     case RID_IS_CLASS:
7311       kind = CPTK_IS_CLASS;
7312       break;
7313     case RID_IS_CONVERTIBLE_TO:
7314       kind = CPTK_IS_CONVERTIBLE_TO;
7315       binary = true;
7316       break;
7317     case RID_IS_EMPTY:
7318       kind = CPTK_IS_EMPTY;
7319       break;
7320     case RID_IS_ENUM:
7321       kind = CPTK_IS_ENUM;
7322       break;
7323     case RID_IS_POD:
7324       kind = CPTK_IS_POD;
7325       break;
7326     case RID_IS_POLYMORPHIC:
7327       kind = CPTK_IS_POLYMORPHIC;
7328       break;
7329     case RID_IS_STD_LAYOUT:
7330       kind = CPTK_IS_STD_LAYOUT;
7331       break;
7332     case RID_IS_TRIVIAL:
7333       kind = CPTK_IS_TRIVIAL;
7334       break;
7335     case RID_IS_UNION:
7336       kind = CPTK_IS_UNION;
7337       break;
7338     default:
7339       gcc_unreachable ();
7340     }
7341
7342   /* Consume the token.  */
7343   cp_lexer_consume_token (parser->lexer);
7344
7345   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7346
7347   type1 = cp_parser_type_id (parser);
7348
7349   if (type1 == error_mark_node)
7350     return error_mark_node;
7351
7352   /* Build a trivial decl-specifier-seq.  */
7353   clear_decl_specs (&decl_specs);
7354   decl_specs.type = type1;
7355
7356   /* Call grokdeclarator to figure out what type this is.  */
7357   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7358                           /*initialized=*/0, /*attrlist=*/NULL);
7359
7360   if (binary)
7361     {
7362       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7363  
7364       type2 = cp_parser_type_id (parser);
7365
7366       if (type2 == error_mark_node)
7367         return error_mark_node;
7368
7369       /* Build a trivial decl-specifier-seq.  */
7370       clear_decl_specs (&decl_specs);
7371       decl_specs.type = type2;
7372
7373       /* Call grokdeclarator to figure out what type this is.  */
7374       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7375                               /*initialized=*/0, /*attrlist=*/NULL);
7376     }
7377
7378   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7379
7380   /* Complete the trait expression, which may mean either processing
7381      the trait expr now or saving it for template instantiation.  */
7382   return finish_trait_expr (kind, type1, type2);
7383 }
7384
7385 /* Lambdas that appear in variable initializer or default argument scope
7386    get that in their mangling, so we need to record it.  We might as well
7387    use the count for function and namespace scopes as well.  */
7388 static GTY(()) tree lambda_scope;
7389 static GTY(()) int lambda_count;
7390 typedef struct GTY(()) tree_int
7391 {
7392   tree t;
7393   int i;
7394 } tree_int;
7395 DEF_VEC_O(tree_int);
7396 DEF_VEC_ALLOC_O(tree_int,gc);
7397 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7398
7399 static void
7400 start_lambda_scope (tree decl)
7401 {
7402   tree_int ti;
7403   gcc_assert (decl);
7404   /* Once we're inside a function, we ignore other scopes and just push
7405      the function again so that popping works properly.  */
7406   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7407     decl = current_function_decl;
7408   ti.t = lambda_scope;
7409   ti.i = lambda_count;
7410   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7411   if (lambda_scope != decl)
7412     {
7413       /* Don't reset the count if we're still in the same function.  */
7414       lambda_scope = decl;
7415       lambda_count = 0;
7416     }
7417 }
7418
7419 static void
7420 record_lambda_scope (tree lambda)
7421 {
7422   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7423   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7424 }
7425
7426 static void
7427 finish_lambda_scope (void)
7428 {
7429   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7430   if (lambda_scope != p->t)
7431     {
7432       lambda_scope = p->t;
7433       lambda_count = p->i;
7434     }
7435   VEC_pop (tree_int, lambda_scope_stack);
7436 }
7437
7438 /* Parse a lambda expression.
7439
7440    lambda-expression:
7441      lambda-introducer lambda-declarator [opt] compound-statement
7442
7443    Returns a representation of the expression.  */
7444
7445 static tree
7446 cp_parser_lambda_expression (cp_parser* parser)
7447 {
7448   tree lambda_expr = build_lambda_expr ();
7449   tree type;
7450
7451   LAMBDA_EXPR_LOCATION (lambda_expr)
7452     = cp_lexer_peek_token (parser->lexer)->location;
7453
7454   if (cp_unevaluated_operand)
7455     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7456               "lambda-expression in unevaluated context");
7457
7458   /* We may be in the middle of deferred access check.  Disable
7459      it now.  */
7460   push_deferring_access_checks (dk_no_deferred);
7461
7462   cp_parser_lambda_introducer (parser, lambda_expr);
7463
7464   type = begin_lambda_type (lambda_expr);
7465
7466   record_lambda_scope (lambda_expr);
7467
7468   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7469   determine_visibility (TYPE_NAME (type));
7470
7471   /* Now that we've started the type, add the capture fields for any
7472      explicit captures.  */
7473   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7474
7475   {
7476     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7477     unsigned int saved_num_template_parameter_lists
7478         = parser->num_template_parameter_lists;
7479
7480     parser->num_template_parameter_lists = 0;
7481
7482     /* By virtue of defining a local class, a lambda expression has access to
7483        the private variables of enclosing classes.  */
7484
7485     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7486
7487     cp_parser_lambda_body (parser, lambda_expr);
7488
7489     /* The capture list was built up in reverse order; fix that now.  */
7490     {
7491       tree newlist = NULL_TREE;
7492       tree elt, next;
7493
7494       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7495            elt; elt = next)
7496         {
7497           tree field = TREE_PURPOSE (elt);
7498           char *buf;
7499
7500           next = TREE_CHAIN (elt);
7501           TREE_CHAIN (elt) = newlist;
7502           newlist = elt;
7503
7504           /* Also add __ to the beginning of the field name so that code
7505              outside the lambda body can't see the captured name.  We could
7506              just remove the name entirely, but this is more useful for
7507              debugging.  */
7508           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7509             /* The 'this' capture already starts with __.  */
7510             continue;
7511
7512           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7513           buf[1] = buf[0] = '_';
7514           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7515                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7516           DECL_NAME (field) = get_identifier (buf);
7517         }
7518       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7519     }
7520
7521     maybe_add_lambda_conv_op (type);
7522
7523     type = finish_struct (type, /*attributes=*/NULL_TREE);
7524
7525     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7526   }
7527
7528   pop_deferring_access_checks ();
7529
7530   return build_lambda_object (lambda_expr);
7531 }
7532
7533 /* Parse the beginning of a lambda expression.
7534
7535    lambda-introducer:
7536      [ lambda-capture [opt] ]
7537
7538    LAMBDA_EXPR is the current representation of the lambda expression.  */
7539
7540 static void
7541 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7542 {
7543   /* Need commas after the first capture.  */
7544   bool first = true;
7545
7546   /* Eat the leading `['.  */
7547   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7548
7549   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7550   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7551       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7552     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7553   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7554     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7555
7556   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7557     {
7558       cp_lexer_consume_token (parser->lexer);
7559       first = false;
7560     }
7561
7562   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7563     {
7564       cp_token* capture_token;
7565       tree capture_id;
7566       tree capture_init_expr;
7567       cp_id_kind idk = CP_ID_KIND_NONE;
7568       bool explicit_init_p = false;
7569
7570       enum capture_kind_type
7571       {
7572         BY_COPY,
7573         BY_REFERENCE
7574       };
7575       enum capture_kind_type capture_kind = BY_COPY;
7576
7577       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7578         {
7579           error ("expected end of capture-list");
7580           return;
7581         }
7582
7583       if (first)
7584         first = false;
7585       else
7586         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7587
7588       /* Possibly capture `this'.  */
7589       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7590         {
7591           cp_lexer_consume_token (parser->lexer);
7592           add_capture (lambda_expr,
7593                        /*id=*/get_identifier ("__this"),
7594                        /*initializer=*/finish_this_expr(),
7595                        /*by_reference_p=*/false,
7596                        explicit_init_p);
7597           continue;
7598         }
7599
7600       /* Remember whether we want to capture as a reference or not.  */
7601       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7602         {
7603           capture_kind = BY_REFERENCE;
7604           cp_lexer_consume_token (parser->lexer);
7605         }
7606
7607       /* Get the identifier.  */
7608       capture_token = cp_lexer_peek_token (parser->lexer);
7609       capture_id = cp_parser_identifier (parser);
7610
7611       if (capture_id == error_mark_node)
7612         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7613            delimiters, but I modified this to stop on unnested ']' as well.  It
7614            was already changed to stop on unnested '}', so the
7615            "closing_parenthesis" name is no more misleading with my change.  */
7616         {
7617           cp_parser_skip_to_closing_parenthesis (parser,
7618                                                  /*recovering=*/true,
7619                                                  /*or_comma=*/true,
7620                                                  /*consume_paren=*/true);
7621           break;
7622         }
7623
7624       /* Find the initializer for this capture.  */
7625       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7626         {
7627           /* An explicit expression exists.  */
7628           cp_lexer_consume_token (parser->lexer);
7629           pedwarn (input_location, OPT_pedantic,
7630                    "ISO C++ does not allow initializers "
7631                    "in lambda expression capture lists");
7632           capture_init_expr = cp_parser_assignment_expression (parser,
7633                                                                /*cast_p=*/true,
7634                                                                &idk);
7635           explicit_init_p = true;
7636         }
7637       else
7638         {
7639           const char* error_msg;
7640
7641           /* Turn the identifier into an id-expression.  */
7642           capture_init_expr
7643             = cp_parser_lookup_name
7644                 (parser,
7645                  capture_id,
7646                  none_type,
7647                  /*is_template=*/false,
7648                  /*is_namespace=*/false,
7649                  /*check_dependency=*/true,
7650                  /*ambiguous_decls=*/NULL,
7651                  capture_token->location);
7652
7653           capture_init_expr
7654             = finish_id_expression
7655                 (capture_id,
7656                  capture_init_expr,
7657                  parser->scope,
7658                  &idk,
7659                  /*integral_constant_expression_p=*/false,
7660                  /*allow_non_integral_constant_expression_p=*/false,
7661                  /*non_integral_constant_expression_p=*/NULL,
7662                  /*template_p=*/false,
7663                  /*done=*/true,
7664                  /*address_p=*/false,
7665                  /*template_arg_p=*/false,
7666                  &error_msg,
7667                  capture_token->location);
7668         }
7669
7670       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7671         capture_init_expr
7672           = unqualified_name_lookup_error (capture_init_expr);
7673
7674       add_capture (lambda_expr,
7675                    capture_id,
7676                    capture_init_expr,
7677                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7678                    explicit_init_p);
7679     }
7680
7681   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7682 }
7683
7684 /* Parse the (optional) middle of a lambda expression.
7685
7686    lambda-declarator:
7687      ( parameter-declaration-clause [opt] )
7688        attribute-specifier [opt]
7689        mutable [opt]
7690        exception-specification [opt]
7691        lambda-return-type-clause [opt]
7692
7693    LAMBDA_EXPR is the current representation of the lambda expression.  */
7694
7695 static void
7696 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7697 {
7698   /* 5.1.1.4 of the standard says:
7699        If a lambda-expression does not include a lambda-declarator, it is as if
7700        the lambda-declarator were ().
7701      This means an empty parameter list, no attributes, and no exception
7702      specification.  */
7703   tree param_list = void_list_node;
7704   tree attributes = NULL_TREE;
7705   tree exception_spec = NULL_TREE;
7706   tree t;
7707
7708   /* The lambda-declarator is optional, but must begin with an opening
7709      parenthesis if present.  */
7710   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7711     {
7712       cp_lexer_consume_token (parser->lexer);
7713
7714       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7715
7716       /* Parse parameters.  */
7717       param_list = cp_parser_parameter_declaration_clause (parser);
7718
7719       /* Default arguments shall not be specified in the
7720          parameter-declaration-clause of a lambda-declarator.  */
7721       for (t = param_list; t; t = TREE_CHAIN (t))
7722         if (TREE_PURPOSE (t))
7723           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7724                    "default argument specified for lambda parameter");
7725
7726       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7727
7728       attributes = cp_parser_attributes_opt (parser);
7729
7730       /* Parse optional `mutable' keyword.  */
7731       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7732         {
7733           cp_lexer_consume_token (parser->lexer);
7734           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7735         }
7736
7737       /* Parse optional exception specification.  */
7738       exception_spec = cp_parser_exception_specification_opt (parser);
7739
7740       /* Parse optional trailing return type.  */
7741       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7742         {
7743           cp_lexer_consume_token (parser->lexer);
7744           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7745         }
7746
7747       /* The function parameters must be in scope all the way until after the
7748          trailing-return-type in case of decltype.  */
7749       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7750         pop_binding (DECL_NAME (t), t);
7751
7752       leave_scope ();
7753     }
7754
7755   /* Create the function call operator.
7756
7757      Messing with declarators like this is no uglier than building up the
7758      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7759      other code.  */
7760   {
7761     cp_decl_specifier_seq return_type_specs;
7762     cp_declarator* declarator;
7763     tree fco;
7764     int quals;
7765     void *p;
7766
7767     clear_decl_specs (&return_type_specs);
7768     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7769       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7770     else
7771       /* Maybe we will deduce the return type later, but we can use void
7772          as a placeholder return type anyways.  */
7773       return_type_specs.type = void_type_node;
7774
7775     p = obstack_alloc (&declarator_obstack, 0);
7776
7777     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7778                                      sfk_none);
7779
7780     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7781              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7782     declarator = make_call_declarator (declarator, param_list, quals,
7783                                        exception_spec,
7784                                        /*late_return_type=*/NULL_TREE);
7785     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7786
7787     fco = grokmethod (&return_type_specs,
7788                       declarator,
7789                       attributes);
7790     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7791     DECL_ARTIFICIAL (fco) = 1;
7792
7793     finish_member_declaration (fco);
7794
7795     obstack_free (&declarator_obstack, p);
7796   }
7797 }
7798
7799 /* Parse the body of a lambda expression, which is simply
7800
7801    compound-statement
7802
7803    but which requires special handling.
7804    LAMBDA_EXPR is the current representation of the lambda expression.  */
7805
7806 static void
7807 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7808 {
7809   bool nested = (current_function_decl != NULL_TREE);
7810   if (nested)
7811     push_function_context ();
7812
7813   /* Finish the function call operator
7814      - class_specifier
7815      + late_parsing_for_member
7816      + function_definition_after_declarator
7817      + ctor_initializer_opt_and_function_body  */
7818   {
7819     tree fco = lambda_function (lambda_expr);
7820     tree body;
7821     bool done = false;
7822
7823     /* Let the front end know that we are going to be defining this
7824        function.  */
7825     start_preparsed_function (fco,
7826                               NULL_TREE,
7827                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7828
7829     start_lambda_scope (fco);
7830     body = begin_function_body ();
7831
7832     /* 5.1.1.4 of the standard says:
7833          If a lambda-expression does not include a trailing-return-type, it
7834          is as if the trailing-return-type denotes the following type:
7835           * if the compound-statement is of the form
7836                { return attribute-specifier [opt] expression ; }
7837              the type of the returned expression after lvalue-to-rvalue
7838              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7839              (_conv.array_ 4.2), and function-to-pointer conversion
7840              (_conv.func_ 4.3);
7841           * otherwise, void.  */
7842
7843     /* In a lambda that has neither a lambda-return-type-clause
7844        nor a deducible form, errors should be reported for return statements
7845        in the body.  Since we used void as the placeholder return type, parsing
7846        the body as usual will give such desired behavior.  */
7847     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7848         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7849         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7850         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7851       {
7852         tree compound_stmt;
7853         tree expr = NULL_TREE;
7854         cp_id_kind idk = CP_ID_KIND_NONE;
7855
7856         /* Parse tentatively in case there's more after the initial return
7857            statement.  */
7858         cp_parser_parse_tentatively (parser);
7859
7860         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7861         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7862
7863         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7864
7865         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7866         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7867
7868         if (cp_parser_parse_definitely (parser))
7869           {
7870             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7871
7872             compound_stmt = begin_compound_stmt (0);
7873             /* Will get error here if type not deduced yet.  */
7874             finish_return_stmt (expr);
7875             finish_compound_stmt (compound_stmt);
7876
7877             done = true;
7878           }
7879       }
7880
7881     if (!done)
7882       {
7883         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7884           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7885         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7886            cp_parser_compound_stmt does not pass it.  */
7887         cp_parser_function_body (parser);
7888         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7889       }
7890
7891     finish_function_body (body);
7892     finish_lambda_scope ();
7893
7894     /* Finish the function and generate code for it if necessary.  */
7895     expand_or_defer_fn (finish_function (/*inline*/2));
7896   }
7897
7898   if (nested)
7899     pop_function_context();
7900 }
7901
7902 /* Statements [gram.stmt.stmt]  */
7903
7904 /* Parse a statement.
7905
7906    statement:
7907      labeled-statement
7908      expression-statement
7909      compound-statement
7910      selection-statement
7911      iteration-statement
7912      jump-statement
7913      declaration-statement
7914      try-block
7915
7916   IN_COMPOUND is true when the statement is nested inside a
7917   cp_parser_compound_statement; this matters for certain pragmas.
7918
7919   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7920   is a (possibly labeled) if statement which is not enclosed in braces
7921   and has an else clause.  This is used to implement -Wparentheses.  */
7922
7923 static void
7924 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7925                      bool in_compound, bool *if_p)
7926 {
7927   tree statement;
7928   cp_token *token;
7929   location_t statement_location;
7930
7931  restart:
7932   if (if_p != NULL)
7933     *if_p = false;
7934   /* There is no statement yet.  */
7935   statement = NULL_TREE;
7936   /* Peek at the next token.  */
7937   token = cp_lexer_peek_token (parser->lexer);
7938   /* Remember the location of the first token in the statement.  */
7939   statement_location = token->location;
7940   /* If this is a keyword, then that will often determine what kind of
7941      statement we have.  */
7942   if (token->type == CPP_KEYWORD)
7943     {
7944       enum rid keyword = token->keyword;
7945
7946       switch (keyword)
7947         {
7948         case RID_CASE:
7949         case RID_DEFAULT:
7950           /* Looks like a labeled-statement with a case label.
7951              Parse the label, and then use tail recursion to parse
7952              the statement.  */
7953           cp_parser_label_for_labeled_statement (parser);
7954           goto restart;
7955
7956         case RID_IF:
7957         case RID_SWITCH:
7958           statement = cp_parser_selection_statement (parser, if_p);
7959           break;
7960
7961         case RID_WHILE:
7962         case RID_DO:
7963         case RID_FOR:
7964           statement = cp_parser_iteration_statement (parser);
7965           break;
7966
7967         case RID_BREAK:
7968         case RID_CONTINUE:
7969         case RID_RETURN:
7970         case RID_GOTO:
7971           statement = cp_parser_jump_statement (parser);
7972           break;
7973
7974           /* Objective-C++ exception-handling constructs.  */
7975         case RID_AT_TRY:
7976         case RID_AT_CATCH:
7977         case RID_AT_FINALLY:
7978         case RID_AT_SYNCHRONIZED:
7979         case RID_AT_THROW:
7980           statement = cp_parser_objc_statement (parser);
7981           break;
7982
7983         case RID_TRY:
7984           statement = cp_parser_try_block (parser);
7985           break;
7986
7987         case RID_NAMESPACE:
7988           /* This must be a namespace alias definition.  */
7989           cp_parser_declaration_statement (parser);
7990           return;
7991           
7992         default:
7993           /* It might be a keyword like `int' that can start a
7994              declaration-statement.  */
7995           break;
7996         }
7997     }
7998   else if (token->type == CPP_NAME)
7999     {
8000       /* If the next token is a `:', then we are looking at a
8001          labeled-statement.  */
8002       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8003       if (token->type == CPP_COLON)
8004         {
8005           /* Looks like a labeled-statement with an ordinary label.
8006              Parse the label, and then use tail recursion to parse
8007              the statement.  */
8008           cp_parser_label_for_labeled_statement (parser);
8009           goto restart;
8010         }
8011     }
8012   /* Anything that starts with a `{' must be a compound-statement.  */
8013   else if (token->type == CPP_OPEN_BRACE)
8014     statement = cp_parser_compound_statement (parser, NULL, false);
8015   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8016      a statement all its own.  */
8017   else if (token->type == CPP_PRAGMA)
8018     {
8019       /* Only certain OpenMP pragmas are attached to statements, and thus
8020          are considered statements themselves.  All others are not.  In
8021          the context of a compound, accept the pragma as a "statement" and
8022          return so that we can check for a close brace.  Otherwise we
8023          require a real statement and must go back and read one.  */
8024       if (in_compound)
8025         cp_parser_pragma (parser, pragma_compound);
8026       else if (!cp_parser_pragma (parser, pragma_stmt))
8027         goto restart;
8028       return;
8029     }
8030   else if (token->type == CPP_EOF)
8031     {
8032       cp_parser_error (parser, "expected statement");
8033       return;
8034     }
8035
8036   /* Everything else must be a declaration-statement or an
8037      expression-statement.  Try for the declaration-statement
8038      first, unless we are looking at a `;', in which case we know that
8039      we have an expression-statement.  */
8040   if (!statement)
8041     {
8042       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8043         {
8044           cp_parser_parse_tentatively (parser);
8045           /* Try to parse the declaration-statement.  */
8046           cp_parser_declaration_statement (parser);
8047           /* If that worked, we're done.  */
8048           if (cp_parser_parse_definitely (parser))
8049             return;
8050         }
8051       /* Look for an expression-statement instead.  */
8052       statement = cp_parser_expression_statement (parser, in_statement_expr);
8053     }
8054
8055   /* Set the line number for the statement.  */
8056   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8057     SET_EXPR_LOCATION (statement, statement_location);
8058 }
8059
8060 /* Parse the label for a labeled-statement, i.e.
8061
8062    identifier :
8063    case constant-expression :
8064    default :
8065
8066    GNU Extension:
8067    case constant-expression ... constant-expression : statement
8068
8069    When a label is parsed without errors, the label is added to the
8070    parse tree by the finish_* functions, so this function doesn't
8071    have to return the label.  */
8072
8073 static void
8074 cp_parser_label_for_labeled_statement (cp_parser* parser)
8075 {
8076   cp_token *token;
8077   tree label = NULL_TREE;
8078
8079   /* The next token should be an identifier.  */
8080   token = cp_lexer_peek_token (parser->lexer);
8081   if (token->type != CPP_NAME
8082       && token->type != CPP_KEYWORD)
8083     {
8084       cp_parser_error (parser, "expected labeled-statement");
8085       return;
8086     }
8087
8088   switch (token->keyword)
8089     {
8090     case RID_CASE:
8091       {
8092         tree expr, expr_hi;
8093         cp_token *ellipsis;
8094
8095         /* Consume the `case' token.  */
8096         cp_lexer_consume_token (parser->lexer);
8097         /* Parse the constant-expression.  */
8098         expr = cp_parser_constant_expression (parser,
8099                                               /*allow_non_constant_p=*/false,
8100                                               NULL);
8101
8102         ellipsis = cp_lexer_peek_token (parser->lexer);
8103         if (ellipsis->type == CPP_ELLIPSIS)
8104           {
8105             /* Consume the `...' token.  */
8106             cp_lexer_consume_token (parser->lexer);
8107             expr_hi =
8108               cp_parser_constant_expression (parser,
8109                                              /*allow_non_constant_p=*/false,
8110                                              NULL);
8111             /* We don't need to emit warnings here, as the common code
8112                will do this for us.  */
8113           }
8114         else
8115           expr_hi = NULL_TREE;
8116
8117         if (parser->in_switch_statement_p)
8118           finish_case_label (token->location, expr, expr_hi);
8119         else
8120           error_at (token->location,
8121                     "case label %qE not within a switch statement",
8122                     expr);
8123       }
8124       break;
8125
8126     case RID_DEFAULT:
8127       /* Consume the `default' token.  */
8128       cp_lexer_consume_token (parser->lexer);
8129
8130       if (parser->in_switch_statement_p)
8131         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8132       else
8133         error_at (token->location, "case label not within a switch statement");
8134       break;
8135
8136     default:
8137       /* Anything else must be an ordinary label.  */
8138       label = finish_label_stmt (cp_parser_identifier (parser));
8139       break;
8140     }
8141
8142   /* Require the `:' token.  */
8143   cp_parser_require (parser, CPP_COLON, RT_COLON);
8144
8145   /* An ordinary label may optionally be followed by attributes.
8146      However, this is only permitted if the attributes are then
8147      followed by a semicolon.  This is because, for backward
8148      compatibility, when parsing
8149        lab: __attribute__ ((unused)) int i;
8150      we want the attribute to attach to "i", not "lab".  */
8151   if (label != NULL_TREE
8152       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8153     {
8154       tree attrs;
8155
8156       cp_parser_parse_tentatively (parser);
8157       attrs = cp_parser_attributes_opt (parser);
8158       if (attrs == NULL_TREE
8159           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8160         cp_parser_abort_tentative_parse (parser);
8161       else if (!cp_parser_parse_definitely (parser))
8162         ;
8163       else
8164         cplus_decl_attributes (&label, attrs, 0);
8165     }
8166 }
8167
8168 /* Parse an expression-statement.
8169
8170    expression-statement:
8171      expression [opt] ;
8172
8173    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8174    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8175    indicates whether this expression-statement is part of an
8176    expression statement.  */
8177
8178 static tree
8179 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8180 {
8181   tree statement = NULL_TREE;
8182   cp_token *token = cp_lexer_peek_token (parser->lexer);
8183
8184   /* If the next token is a ';', then there is no expression
8185      statement.  */
8186   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8187     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8188
8189   /* Give a helpful message for "A<T>::type t;" and the like.  */
8190   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8191       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8192     {
8193       if (TREE_CODE (statement) == SCOPE_REF)
8194         error_at (token->location, "need %<typename%> before %qE because "
8195                   "%qT is a dependent scope",
8196                   statement, TREE_OPERAND (statement, 0));
8197       else if (is_overloaded_fn (statement)
8198                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8199         {
8200           /* A::A a; */
8201           tree fn = get_first_fn (statement);
8202           error_at (token->location,
8203                     "%<%T::%D%> names the constructor, not the type",
8204                     DECL_CONTEXT (fn), DECL_NAME (fn));
8205         }
8206     }
8207
8208   /* Consume the final `;'.  */
8209   cp_parser_consume_semicolon_at_end_of_statement (parser);
8210
8211   if (in_statement_expr
8212       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8213     /* This is the final expression statement of a statement
8214        expression.  */
8215     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8216   else if (statement)
8217     statement = finish_expr_stmt (statement);
8218   else
8219     finish_stmt ();
8220
8221   return statement;
8222 }
8223
8224 /* Parse a compound-statement.
8225
8226    compound-statement:
8227      { statement-seq [opt] }
8228
8229    GNU extension:
8230
8231    compound-statement:
8232      { label-declaration-seq [opt] statement-seq [opt] }
8233
8234    label-declaration-seq:
8235      label-declaration
8236      label-declaration-seq label-declaration
8237
8238    Returns a tree representing the statement.  */
8239
8240 static tree
8241 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8242                               bool in_try)
8243 {
8244   tree compound_stmt;
8245
8246   /* Consume the `{'.  */
8247   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8248     return error_mark_node;
8249   /* Begin the compound-statement.  */
8250   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8251   /* If the next keyword is `__label__' we have a label declaration.  */
8252   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8253     cp_parser_label_declaration (parser);
8254   /* Parse an (optional) statement-seq.  */
8255   cp_parser_statement_seq_opt (parser, in_statement_expr);
8256   /* Finish the compound-statement.  */
8257   finish_compound_stmt (compound_stmt);
8258   /* Consume the `}'.  */
8259   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8260
8261   return compound_stmt;
8262 }
8263
8264 /* Parse an (optional) statement-seq.
8265
8266    statement-seq:
8267      statement
8268      statement-seq [opt] statement  */
8269
8270 static void
8271 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8272 {
8273   /* Scan statements until there aren't any more.  */
8274   while (true)
8275     {
8276       cp_token *token = cp_lexer_peek_token (parser->lexer);
8277
8278       /* If we're looking at a `}', then we've run out of statements.  */
8279       if (token->type == CPP_CLOSE_BRACE
8280           || token->type == CPP_EOF
8281           || token->type == CPP_PRAGMA_EOL)
8282         break;
8283       
8284       /* If we are in a compound statement and find 'else' then
8285          something went wrong.  */
8286       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8287         {
8288           if (parser->in_statement & IN_IF_STMT) 
8289             break;
8290           else
8291             {
8292               token = cp_lexer_consume_token (parser->lexer);
8293               error_at (token->location, "%<else%> without a previous %<if%>");
8294             }
8295         }
8296
8297       /* Parse the statement.  */
8298       cp_parser_statement (parser, in_statement_expr, true, NULL);
8299     }
8300 }
8301
8302 /* Parse a selection-statement.
8303
8304    selection-statement:
8305      if ( condition ) statement
8306      if ( condition ) statement else statement
8307      switch ( condition ) statement
8308
8309    Returns the new IF_STMT or SWITCH_STMT.
8310
8311    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8312    is a (possibly labeled) if statement which is not enclosed in
8313    braces and has an else clause.  This is used to implement
8314    -Wparentheses.  */
8315
8316 static tree
8317 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8318 {
8319   cp_token *token;
8320   enum rid keyword;
8321
8322   if (if_p != NULL)
8323     *if_p = false;
8324
8325   /* Peek at the next token.  */
8326   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8327
8328   /* See what kind of keyword it is.  */
8329   keyword = token->keyword;
8330   switch (keyword)
8331     {
8332     case RID_IF:
8333     case RID_SWITCH:
8334       {
8335         tree statement;
8336         tree condition;
8337
8338         /* Look for the `('.  */
8339         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8340           {
8341             cp_parser_skip_to_end_of_statement (parser);
8342             return error_mark_node;
8343           }
8344
8345         /* Begin the selection-statement.  */
8346         if (keyword == RID_IF)
8347           statement = begin_if_stmt ();
8348         else
8349           statement = begin_switch_stmt ();
8350
8351         /* Parse the condition.  */
8352         condition = cp_parser_condition (parser);
8353         /* Look for the `)'.  */
8354         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8355           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8356                                                  /*consume_paren=*/true);
8357
8358         if (keyword == RID_IF)
8359           {
8360             bool nested_if;
8361             unsigned char in_statement;
8362
8363             /* Add the condition.  */
8364             finish_if_stmt_cond (condition, statement);
8365
8366             /* Parse the then-clause.  */
8367             in_statement = parser->in_statement;
8368             parser->in_statement |= IN_IF_STMT;
8369             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8370               {
8371                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8372                 add_stmt (build_empty_stmt (loc));
8373                 cp_lexer_consume_token (parser->lexer);
8374                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8375                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8376                               "empty body in an %<if%> statement");
8377                 nested_if = false;
8378               }
8379             else
8380               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8381             parser->in_statement = in_statement;
8382
8383             finish_then_clause (statement);
8384
8385             /* If the next token is `else', parse the else-clause.  */
8386             if (cp_lexer_next_token_is_keyword (parser->lexer,
8387                                                 RID_ELSE))
8388               {
8389                 /* Consume the `else' keyword.  */
8390                 cp_lexer_consume_token (parser->lexer);
8391                 begin_else_clause (statement);
8392                 /* Parse the else-clause.  */
8393                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8394                   {
8395                     location_t loc;
8396                     loc = cp_lexer_peek_token (parser->lexer)->location;
8397                     warning_at (loc,
8398                                 OPT_Wempty_body, "suggest braces around "
8399                                 "empty body in an %<else%> statement");
8400                     add_stmt (build_empty_stmt (loc));
8401                     cp_lexer_consume_token (parser->lexer);
8402                   }
8403                 else
8404                   cp_parser_implicitly_scoped_statement (parser, NULL);
8405
8406                 finish_else_clause (statement);
8407
8408                 /* If we are currently parsing a then-clause, then
8409                    IF_P will not be NULL.  We set it to true to
8410                    indicate that this if statement has an else clause.
8411                    This may trigger the Wparentheses warning below
8412                    when we get back up to the parent if statement.  */
8413                 if (if_p != NULL)
8414                   *if_p = true;
8415               }
8416             else
8417               {
8418                 /* This if statement does not have an else clause.  If
8419                    NESTED_IF is true, then the then-clause is an if
8420                    statement which does have an else clause.  We warn
8421                    about the potential ambiguity.  */
8422                 if (nested_if)
8423                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8424                               "suggest explicit braces to avoid ambiguous"
8425                               " %<else%>");
8426               }
8427
8428             /* Now we're all done with the if-statement.  */
8429             finish_if_stmt (statement);
8430           }
8431         else
8432           {
8433             bool in_switch_statement_p;
8434             unsigned char in_statement;
8435
8436             /* Add the condition.  */
8437             finish_switch_cond (condition, statement);
8438
8439             /* Parse the body of the switch-statement.  */
8440             in_switch_statement_p = parser->in_switch_statement_p;
8441             in_statement = parser->in_statement;
8442             parser->in_switch_statement_p = true;
8443             parser->in_statement |= IN_SWITCH_STMT;
8444             cp_parser_implicitly_scoped_statement (parser, NULL);
8445             parser->in_switch_statement_p = in_switch_statement_p;
8446             parser->in_statement = in_statement;
8447
8448             /* Now we're all done with the switch-statement.  */
8449             finish_switch_stmt (statement);
8450           }
8451
8452         return statement;
8453       }
8454       break;
8455
8456     default:
8457       cp_parser_error (parser, "expected selection-statement");
8458       return error_mark_node;
8459     }
8460 }
8461
8462 /* Parse a condition.
8463
8464    condition:
8465      expression
8466      type-specifier-seq declarator = initializer-clause
8467      type-specifier-seq declarator braced-init-list
8468
8469    GNU Extension:
8470
8471    condition:
8472      type-specifier-seq declarator asm-specification [opt]
8473        attributes [opt] = assignment-expression
8474
8475    Returns the expression that should be tested.  */
8476
8477 static tree
8478 cp_parser_condition (cp_parser* parser)
8479 {
8480   cp_decl_specifier_seq type_specifiers;
8481   const char *saved_message;
8482
8483   /* Try the declaration first.  */
8484   cp_parser_parse_tentatively (parser);
8485   /* New types are not allowed in the type-specifier-seq for a
8486      condition.  */
8487   saved_message = parser->type_definition_forbidden_message;
8488   parser->type_definition_forbidden_message
8489     = G_("types may not be defined in conditions");
8490   /* Parse the type-specifier-seq.  */
8491   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8492                                 /*is_trailing_return=*/false,
8493                                 &type_specifiers);
8494   /* Restore the saved message.  */
8495   parser->type_definition_forbidden_message = saved_message;
8496   /* If all is well, we might be looking at a declaration.  */
8497   if (!cp_parser_error_occurred (parser))
8498     {
8499       tree decl;
8500       tree asm_specification;
8501       tree attributes;
8502       cp_declarator *declarator;
8503       tree initializer = NULL_TREE;
8504
8505       /* Parse the declarator.  */
8506       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8507                                          /*ctor_dtor_or_conv_p=*/NULL,
8508                                          /*parenthesized_p=*/NULL,
8509                                          /*member_p=*/false);
8510       /* Parse the attributes.  */
8511       attributes = cp_parser_attributes_opt (parser);
8512       /* Parse the asm-specification.  */
8513       asm_specification = cp_parser_asm_specification_opt (parser);
8514       /* If the next token is not an `=' or '{', then we might still be
8515          looking at an expression.  For example:
8516
8517            if (A(a).x)
8518
8519          looks like a decl-specifier-seq and a declarator -- but then
8520          there is no `=', so this is an expression.  */
8521       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8522           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8523         cp_parser_simulate_error (parser);
8524         
8525       /* If we did see an `=' or '{', then we are looking at a declaration
8526          for sure.  */
8527       if (cp_parser_parse_definitely (parser))
8528         {
8529           tree pushed_scope;
8530           bool non_constant_p;
8531           bool flags = LOOKUP_ONLYCONVERTING;
8532
8533           /* Create the declaration.  */
8534           decl = start_decl (declarator, &type_specifiers,
8535                              /*initialized_p=*/true,
8536                              attributes, /*prefix_attributes=*/NULL_TREE,
8537                              &pushed_scope);
8538
8539           /* Parse the initializer.  */
8540           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8541             {
8542               initializer = cp_parser_braced_list (parser, &non_constant_p);
8543               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8544               flags = 0;
8545             }
8546           else
8547             {
8548               /* Consume the `='.  */
8549               cp_parser_require (parser, CPP_EQ, RT_EQ);
8550               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8551             }
8552           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8553             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8554
8555           if (!non_constant_p)
8556             initializer = fold_non_dependent_expr (initializer);
8557
8558           /* Process the initializer.  */
8559           cp_finish_decl (decl,
8560                           initializer, !non_constant_p,
8561                           asm_specification,
8562                           flags);
8563
8564           if (pushed_scope)
8565             pop_scope (pushed_scope);
8566
8567           return convert_from_reference (decl);
8568         }
8569     }
8570   /* If we didn't even get past the declarator successfully, we are
8571      definitely not looking at a declaration.  */
8572   else
8573     cp_parser_abort_tentative_parse (parser);
8574
8575   /* Otherwise, we are looking at an expression.  */
8576   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8577 }
8578
8579 /* Parse an iteration-statement.
8580
8581    iteration-statement:
8582      while ( condition ) statement
8583      do statement while ( expression ) ;
8584      for ( for-init-statement condition [opt] ; expression [opt] )
8585        statement
8586
8587    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8588
8589 static tree
8590 cp_parser_iteration_statement (cp_parser* parser)
8591 {
8592   cp_token *token;
8593   enum rid keyword;
8594   tree statement;
8595   unsigned char in_statement;
8596
8597   /* Peek at the next token.  */
8598   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8599   if (!token)
8600     return error_mark_node;
8601
8602   /* Remember whether or not we are already within an iteration
8603      statement.  */
8604   in_statement = parser->in_statement;
8605
8606   /* See what kind of keyword it is.  */
8607   keyword = token->keyword;
8608   switch (keyword)
8609     {
8610     case RID_WHILE:
8611       {
8612         tree condition;
8613
8614         /* Begin the while-statement.  */
8615         statement = begin_while_stmt ();
8616         /* Look for the `('.  */
8617         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8618         /* Parse the condition.  */
8619         condition = cp_parser_condition (parser);
8620         finish_while_stmt_cond (condition, statement);
8621         /* Look for the `)'.  */
8622         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8623         /* Parse the dependent statement.  */
8624         parser->in_statement = IN_ITERATION_STMT;
8625         cp_parser_already_scoped_statement (parser);
8626         parser->in_statement = in_statement;
8627         /* We're done with the while-statement.  */
8628         finish_while_stmt (statement);
8629       }
8630       break;
8631
8632     case RID_DO:
8633       {
8634         tree expression;
8635
8636         /* Begin the do-statement.  */
8637         statement = begin_do_stmt ();
8638         /* Parse the body of the do-statement.  */
8639         parser->in_statement = IN_ITERATION_STMT;
8640         cp_parser_implicitly_scoped_statement (parser, NULL);
8641         parser->in_statement = in_statement;
8642         finish_do_body (statement);
8643         /* Look for the `while' keyword.  */
8644         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8645         /* Look for the `('.  */
8646         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8647         /* Parse the expression.  */
8648         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8649         /* We're done with the do-statement.  */
8650         finish_do_stmt (expression, statement);
8651         /* Look for the `)'.  */
8652         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8653         /* Look for the `;'.  */
8654         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8655       }
8656       break;
8657
8658     case RID_FOR:
8659       {
8660         tree condition = NULL_TREE;
8661         tree expression = NULL_TREE;
8662
8663         /* Begin the for-statement.  */
8664         statement = begin_for_stmt ();
8665         /* Look for the `('.  */
8666         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8667         /* Parse the initialization.  */
8668         cp_parser_for_init_statement (parser);
8669         finish_for_init_stmt (statement);
8670
8671         /* If there's a condition, process it.  */
8672         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8673           condition = cp_parser_condition (parser);
8674         finish_for_cond (condition, statement);
8675         /* Look for the `;'.  */
8676         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8677
8678         /* If there's an expression, process it.  */
8679         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8680           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8681         finish_for_expr (expression, statement);
8682         /* Look for the `)'.  */
8683         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8684
8685         /* Parse the body of the for-statement.  */
8686         parser->in_statement = IN_ITERATION_STMT;
8687         cp_parser_already_scoped_statement (parser);
8688         parser->in_statement = in_statement;
8689
8690         /* We're done with the for-statement.  */
8691         finish_for_stmt (statement);
8692       }
8693       break;
8694
8695     default:
8696       cp_parser_error (parser, "expected iteration-statement");
8697       statement = error_mark_node;
8698       break;
8699     }
8700
8701   return statement;
8702 }
8703
8704 /* Parse a for-init-statement.
8705
8706    for-init-statement:
8707      expression-statement
8708      simple-declaration  */
8709
8710 static void
8711 cp_parser_for_init_statement (cp_parser* parser)
8712 {
8713   /* If the next token is a `;', then we have an empty
8714      expression-statement.  Grammatically, this is also a
8715      simple-declaration, but an invalid one, because it does not
8716      declare anything.  Therefore, if we did not handle this case
8717      specially, we would issue an error message about an invalid
8718      declaration.  */
8719   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8720     {
8721       /* We're going to speculatively look for a declaration, falling back
8722          to an expression, if necessary.  */
8723       cp_parser_parse_tentatively (parser);
8724       /* Parse the declaration.  */
8725       cp_parser_simple_declaration (parser,
8726                                     /*function_definition_allowed_p=*/false);
8727       /* If the tentative parse failed, then we shall need to look for an
8728          expression-statement.  */
8729       if (cp_parser_parse_definitely (parser))
8730         return;
8731     }
8732
8733   cp_parser_expression_statement (parser, NULL_TREE);
8734 }
8735
8736 /* Parse a jump-statement.
8737
8738    jump-statement:
8739      break ;
8740      continue ;
8741      return expression [opt] ;
8742      return braced-init-list ;
8743      goto identifier ;
8744
8745    GNU extension:
8746
8747    jump-statement:
8748      goto * expression ;
8749
8750    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8751
8752 static tree
8753 cp_parser_jump_statement (cp_parser* parser)
8754 {
8755   tree statement = error_mark_node;
8756   cp_token *token;
8757   enum rid keyword;
8758   unsigned char in_statement;
8759
8760   /* Peek at the next token.  */
8761   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
8762   if (!token)
8763     return error_mark_node;
8764
8765   /* See what kind of keyword it is.  */
8766   keyword = token->keyword;
8767   switch (keyword)
8768     {
8769     case RID_BREAK:
8770       in_statement = parser->in_statement & ~IN_IF_STMT;      
8771       switch (in_statement)
8772         {
8773         case 0:
8774           error_at (token->location, "break statement not within loop or switch");
8775           break;
8776         default:
8777           gcc_assert ((in_statement & IN_SWITCH_STMT)
8778                       || in_statement == IN_ITERATION_STMT);
8779           statement = finish_break_stmt ();
8780           break;
8781         case IN_OMP_BLOCK:
8782           error_at (token->location, "invalid exit from OpenMP structured block");
8783           break;
8784         case IN_OMP_FOR:
8785           error_at (token->location, "break statement used with OpenMP for loop");
8786           break;
8787         }
8788       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8789       break;
8790
8791     case RID_CONTINUE:
8792       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8793         {
8794         case 0:
8795           error_at (token->location, "continue statement not within a loop");
8796           break;
8797         case IN_ITERATION_STMT:
8798         case IN_OMP_FOR:
8799           statement = finish_continue_stmt ();
8800           break;
8801         case IN_OMP_BLOCK:
8802           error_at (token->location, "invalid exit from OpenMP structured block");
8803           break;
8804         default:
8805           gcc_unreachable ();
8806         }
8807       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8808       break;
8809
8810     case RID_RETURN:
8811       {
8812         tree expr;
8813         bool expr_non_constant_p;
8814
8815         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8816           {
8817             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8818             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8819           }
8820         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8821           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8822         else
8823           /* If the next token is a `;', then there is no
8824              expression.  */
8825           expr = NULL_TREE;
8826         /* Build the return-statement.  */
8827         statement = finish_return_stmt (expr);
8828         /* Look for the final `;'.  */
8829         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8830       }
8831       break;
8832
8833     case RID_GOTO:
8834       /* Create the goto-statement.  */
8835       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8836         {
8837           /* Issue a warning about this use of a GNU extension.  */
8838           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8839           /* Consume the '*' token.  */
8840           cp_lexer_consume_token (parser->lexer);
8841           /* Parse the dependent expression.  */
8842           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8843         }
8844       else
8845         finish_goto_stmt (cp_parser_identifier (parser));
8846       /* Look for the final `;'.  */
8847       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8848       break;
8849
8850     default:
8851       cp_parser_error (parser, "expected jump-statement");
8852       break;
8853     }
8854
8855   return statement;
8856 }
8857
8858 /* Parse a declaration-statement.
8859
8860    declaration-statement:
8861      block-declaration  */
8862
8863 static void
8864 cp_parser_declaration_statement (cp_parser* parser)
8865 {
8866   void *p;
8867
8868   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8869   p = obstack_alloc (&declarator_obstack, 0);
8870
8871  /* Parse the block-declaration.  */
8872   cp_parser_block_declaration (parser, /*statement_p=*/true);
8873
8874   /* Free any declarators allocated.  */
8875   obstack_free (&declarator_obstack, p);
8876
8877   /* Finish off the statement.  */
8878   finish_stmt ();
8879 }
8880
8881 /* Some dependent statements (like `if (cond) statement'), are
8882    implicitly in their own scope.  In other words, if the statement is
8883    a single statement (as opposed to a compound-statement), it is
8884    none-the-less treated as if it were enclosed in braces.  Any
8885    declarations appearing in the dependent statement are out of scope
8886    after control passes that point.  This function parses a statement,
8887    but ensures that is in its own scope, even if it is not a
8888    compound-statement.
8889
8890    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8891    is a (possibly labeled) if statement which is not enclosed in
8892    braces and has an else clause.  This is used to implement
8893    -Wparentheses.
8894
8895    Returns the new statement.  */
8896
8897 static tree
8898 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8899 {
8900   tree statement;
8901
8902   if (if_p != NULL)
8903     *if_p = false;
8904
8905   /* Mark if () ; with a special NOP_EXPR.  */
8906   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8907     {
8908       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8909       cp_lexer_consume_token (parser->lexer);
8910       statement = add_stmt (build_empty_stmt (loc));
8911     }
8912   /* if a compound is opened, we simply parse the statement directly.  */
8913   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8914     statement = cp_parser_compound_statement (parser, NULL, false);
8915   /* If the token is not a `{', then we must take special action.  */
8916   else
8917     {
8918       /* Create a compound-statement.  */
8919       statement = begin_compound_stmt (0);
8920       /* Parse the dependent-statement.  */
8921       cp_parser_statement (parser, NULL_TREE, false, if_p);
8922       /* Finish the dummy compound-statement.  */
8923       finish_compound_stmt (statement);
8924     }
8925
8926   /* Return the statement.  */
8927   return statement;
8928 }
8929
8930 /* For some dependent statements (like `while (cond) statement'), we
8931    have already created a scope.  Therefore, even if the dependent
8932    statement is a compound-statement, we do not want to create another
8933    scope.  */
8934
8935 static void
8936 cp_parser_already_scoped_statement (cp_parser* parser)
8937 {
8938   /* If the token is a `{', then we must take special action.  */
8939   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8940     cp_parser_statement (parser, NULL_TREE, false, NULL);
8941   else
8942     {
8943       /* Avoid calling cp_parser_compound_statement, so that we
8944          don't create a new scope.  Do everything else by hand.  */
8945       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
8946       /* If the next keyword is `__label__' we have a label declaration.  */
8947       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8948         cp_parser_label_declaration (parser);
8949       /* Parse an (optional) statement-seq.  */
8950       cp_parser_statement_seq_opt (parser, NULL_TREE);
8951       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8952     }
8953 }
8954
8955 /* Declarations [gram.dcl.dcl] */
8956
8957 /* Parse an optional declaration-sequence.
8958
8959    declaration-seq:
8960      declaration
8961      declaration-seq declaration  */
8962
8963 static void
8964 cp_parser_declaration_seq_opt (cp_parser* parser)
8965 {
8966   while (true)
8967     {
8968       cp_token *token;
8969
8970       token = cp_lexer_peek_token (parser->lexer);
8971
8972       if (token->type == CPP_CLOSE_BRACE
8973           || token->type == CPP_EOF
8974           || token->type == CPP_PRAGMA_EOL)
8975         break;
8976
8977       if (token->type == CPP_SEMICOLON)
8978         {
8979           /* A declaration consisting of a single semicolon is
8980              invalid.  Allow it unless we're being pedantic.  */
8981           cp_lexer_consume_token (parser->lexer);
8982           if (!in_system_header)
8983             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8984           continue;
8985         }
8986
8987       /* If we're entering or exiting a region that's implicitly
8988          extern "C", modify the lang context appropriately.  */
8989       if (!parser->implicit_extern_c && token->implicit_extern_c)
8990         {
8991           push_lang_context (lang_name_c);
8992           parser->implicit_extern_c = true;
8993         }
8994       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8995         {
8996           pop_lang_context ();
8997           parser->implicit_extern_c = false;
8998         }
8999
9000       if (token->type == CPP_PRAGMA)
9001         {
9002           /* A top-level declaration can consist solely of a #pragma.
9003              A nested declaration cannot, so this is done here and not
9004              in cp_parser_declaration.  (A #pragma at block scope is
9005              handled in cp_parser_statement.)  */
9006           cp_parser_pragma (parser, pragma_external);
9007           continue;
9008         }
9009
9010       /* Parse the declaration itself.  */
9011       cp_parser_declaration (parser);
9012     }
9013 }
9014
9015 /* Parse a declaration.
9016
9017    declaration:
9018      block-declaration
9019      function-definition
9020      template-declaration
9021      explicit-instantiation
9022      explicit-specialization
9023      linkage-specification
9024      namespace-definition
9025
9026    GNU extension:
9027
9028    declaration:
9029       __extension__ declaration */
9030
9031 static void
9032 cp_parser_declaration (cp_parser* parser)
9033 {
9034   cp_token token1;
9035   cp_token token2;
9036   int saved_pedantic;
9037   void *p;
9038
9039   /* Check for the `__extension__' keyword.  */
9040   if (cp_parser_extension_opt (parser, &saved_pedantic))
9041     {
9042       /* Parse the qualified declaration.  */
9043       cp_parser_declaration (parser);
9044       /* Restore the PEDANTIC flag.  */
9045       pedantic = saved_pedantic;
9046
9047       return;
9048     }
9049
9050   /* Try to figure out what kind of declaration is present.  */
9051   token1 = *cp_lexer_peek_token (parser->lexer);
9052
9053   if (token1.type != CPP_EOF)
9054     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9055   else
9056     {
9057       token2.type = CPP_EOF;
9058       token2.keyword = RID_MAX;
9059     }
9060
9061   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9062   p = obstack_alloc (&declarator_obstack, 0);
9063
9064   /* If the next token is `extern' and the following token is a string
9065      literal, then we have a linkage specification.  */
9066   if (token1.keyword == RID_EXTERN
9067       && cp_parser_is_string_literal (&token2))
9068     cp_parser_linkage_specification (parser);
9069   /* If the next token is `template', then we have either a template
9070      declaration, an explicit instantiation, or an explicit
9071      specialization.  */
9072   else if (token1.keyword == RID_TEMPLATE)
9073     {
9074       /* `template <>' indicates a template specialization.  */
9075       if (token2.type == CPP_LESS
9076           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9077         cp_parser_explicit_specialization (parser);
9078       /* `template <' indicates a template declaration.  */
9079       else if (token2.type == CPP_LESS)
9080         cp_parser_template_declaration (parser, /*member_p=*/false);
9081       /* Anything else must be an explicit instantiation.  */
9082       else
9083         cp_parser_explicit_instantiation (parser);
9084     }
9085   /* If the next token is `export', then we have a template
9086      declaration.  */
9087   else if (token1.keyword == RID_EXPORT)
9088     cp_parser_template_declaration (parser, /*member_p=*/false);
9089   /* If the next token is `extern', 'static' or 'inline' and the one
9090      after that is `template', we have a GNU extended explicit
9091      instantiation directive.  */
9092   else if (cp_parser_allow_gnu_extensions_p (parser)
9093            && (token1.keyword == RID_EXTERN
9094                || token1.keyword == RID_STATIC
9095                || token1.keyword == RID_INLINE)
9096            && token2.keyword == RID_TEMPLATE)
9097     cp_parser_explicit_instantiation (parser);
9098   /* If the next token is `namespace', check for a named or unnamed
9099      namespace definition.  */
9100   else if (token1.keyword == RID_NAMESPACE
9101            && (/* A named namespace definition.  */
9102                (token2.type == CPP_NAME
9103                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9104                     != CPP_EQ))
9105                /* An unnamed namespace definition.  */
9106                || token2.type == CPP_OPEN_BRACE
9107                || token2.keyword == RID_ATTRIBUTE))
9108     cp_parser_namespace_definition (parser);
9109   /* An inline (associated) namespace definition.  */
9110   else if (token1.keyword == RID_INLINE
9111            && token2.keyword == RID_NAMESPACE)
9112     cp_parser_namespace_definition (parser);
9113   /* Objective-C++ declaration/definition.  */
9114   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9115     cp_parser_objc_declaration (parser);
9116   /* We must have either a block declaration or a function
9117      definition.  */
9118   else
9119     /* Try to parse a block-declaration, or a function-definition.  */
9120     cp_parser_block_declaration (parser, /*statement_p=*/false);
9121
9122   /* Free any declarators allocated.  */
9123   obstack_free (&declarator_obstack, p);
9124 }
9125
9126 /* Parse a block-declaration.
9127
9128    block-declaration:
9129      simple-declaration
9130      asm-definition
9131      namespace-alias-definition
9132      using-declaration
9133      using-directive
9134
9135    GNU Extension:
9136
9137    block-declaration:
9138      __extension__ block-declaration
9139
9140    C++0x Extension:
9141
9142    block-declaration:
9143      static_assert-declaration
9144
9145    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9146    part of a declaration-statement.  */
9147
9148 static void
9149 cp_parser_block_declaration (cp_parser *parser,
9150                              bool      statement_p)
9151 {
9152   cp_token *token1;
9153   int saved_pedantic;
9154
9155   /* Check for the `__extension__' keyword.  */
9156   if (cp_parser_extension_opt (parser, &saved_pedantic))
9157     {
9158       /* Parse the qualified declaration.  */
9159       cp_parser_block_declaration (parser, statement_p);
9160       /* Restore the PEDANTIC flag.  */
9161       pedantic = saved_pedantic;
9162
9163       return;
9164     }
9165
9166   /* Peek at the next token to figure out which kind of declaration is
9167      present.  */
9168   token1 = cp_lexer_peek_token (parser->lexer);
9169
9170   /* If the next keyword is `asm', we have an asm-definition.  */
9171   if (token1->keyword == RID_ASM)
9172     {
9173       if (statement_p)
9174         cp_parser_commit_to_tentative_parse (parser);
9175       cp_parser_asm_definition (parser);
9176     }
9177   /* If the next keyword is `namespace', we have a
9178      namespace-alias-definition.  */
9179   else if (token1->keyword == RID_NAMESPACE)
9180     cp_parser_namespace_alias_definition (parser);
9181   /* If the next keyword is `using', we have either a
9182      using-declaration or a using-directive.  */
9183   else if (token1->keyword == RID_USING)
9184     {
9185       cp_token *token2;
9186
9187       if (statement_p)
9188         cp_parser_commit_to_tentative_parse (parser);
9189       /* If the token after `using' is `namespace', then we have a
9190          using-directive.  */
9191       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9192       if (token2->keyword == RID_NAMESPACE)
9193         cp_parser_using_directive (parser);
9194       /* Otherwise, it's a using-declaration.  */
9195       else
9196         cp_parser_using_declaration (parser,
9197                                      /*access_declaration_p=*/false);
9198     }
9199   /* If the next keyword is `__label__' we have a misplaced label
9200      declaration.  */
9201   else if (token1->keyword == RID_LABEL)
9202     {
9203       cp_lexer_consume_token (parser->lexer);
9204       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9205       cp_parser_skip_to_end_of_statement (parser);
9206       /* If the next token is now a `;', consume it.  */
9207       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9208         cp_lexer_consume_token (parser->lexer);
9209     }
9210   /* If the next token is `static_assert' we have a static assertion.  */
9211   else if (token1->keyword == RID_STATIC_ASSERT)
9212     cp_parser_static_assert (parser, /*member_p=*/false);
9213   /* Anything else must be a simple-declaration.  */
9214   else
9215     cp_parser_simple_declaration (parser, !statement_p);
9216 }
9217
9218 /* Parse a simple-declaration.
9219
9220    simple-declaration:
9221      decl-specifier-seq [opt] init-declarator-list [opt] ;
9222
9223    init-declarator-list:
9224      init-declarator
9225      init-declarator-list , init-declarator
9226
9227    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9228    function-definition as a simple-declaration.  */
9229
9230 static void
9231 cp_parser_simple_declaration (cp_parser* parser,
9232                               bool function_definition_allowed_p)
9233 {
9234   cp_decl_specifier_seq decl_specifiers;
9235   int declares_class_or_enum;
9236   bool saw_declarator;
9237
9238   /* Defer access checks until we know what is being declared; the
9239      checks for names appearing in the decl-specifier-seq should be
9240      done as if we were in the scope of the thing being declared.  */
9241   push_deferring_access_checks (dk_deferred);
9242
9243   /* Parse the decl-specifier-seq.  We have to keep track of whether
9244      or not the decl-specifier-seq declares a named class or
9245      enumeration type, since that is the only case in which the
9246      init-declarator-list is allowed to be empty.
9247
9248      [dcl.dcl]
9249
9250      In a simple-declaration, the optional init-declarator-list can be
9251      omitted only when declaring a class or enumeration, that is when
9252      the decl-specifier-seq contains either a class-specifier, an
9253      elaborated-type-specifier, or an enum-specifier.  */
9254   cp_parser_decl_specifier_seq (parser,
9255                                 CP_PARSER_FLAGS_OPTIONAL,
9256                                 &decl_specifiers,
9257                                 &declares_class_or_enum);
9258   /* We no longer need to defer access checks.  */
9259   stop_deferring_access_checks ();
9260
9261   /* In a block scope, a valid declaration must always have a
9262      decl-specifier-seq.  By not trying to parse declarators, we can
9263      resolve the declaration/expression ambiguity more quickly.  */
9264   if (!function_definition_allowed_p
9265       && !decl_specifiers.any_specifiers_p)
9266     {
9267       cp_parser_error (parser, "expected declaration");
9268       goto done;
9269     }
9270
9271   /* If the next two tokens are both identifiers, the code is
9272      erroneous. The usual cause of this situation is code like:
9273
9274        T t;
9275
9276      where "T" should name a type -- but does not.  */
9277   if (!decl_specifiers.any_type_specifiers_p
9278       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9279     {
9280       /* If parsing tentatively, we should commit; we really are
9281          looking at a declaration.  */
9282       cp_parser_commit_to_tentative_parse (parser);
9283       /* Give up.  */
9284       goto done;
9285     }
9286
9287   /* If we have seen at least one decl-specifier, and the next token
9288      is not a parenthesis, then we must be looking at a declaration.
9289      (After "int (" we might be looking at a functional cast.)  */
9290   if (decl_specifiers.any_specifiers_p
9291       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9292       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9293       && !cp_parser_error_occurred (parser))
9294     cp_parser_commit_to_tentative_parse (parser);
9295
9296   /* Keep going until we hit the `;' at the end of the simple
9297      declaration.  */
9298   saw_declarator = false;
9299   while (cp_lexer_next_token_is_not (parser->lexer,
9300                                      CPP_SEMICOLON))
9301     {
9302       cp_token *token;
9303       bool function_definition_p;
9304       tree decl;
9305
9306       if (saw_declarator)
9307         {
9308           /* If we are processing next declarator, coma is expected */
9309           token = cp_lexer_peek_token (parser->lexer);
9310           gcc_assert (token->type == CPP_COMMA);
9311           cp_lexer_consume_token (parser->lexer);
9312         }
9313       else
9314         saw_declarator = true;
9315
9316       /* Parse the init-declarator.  */
9317       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9318                                         /*checks=*/NULL,
9319                                         function_definition_allowed_p,
9320                                         /*member_p=*/false,
9321                                         declares_class_or_enum,
9322                                         &function_definition_p);
9323       /* If an error occurred while parsing tentatively, exit quickly.
9324          (That usually happens when in the body of a function; each
9325          statement is treated as a declaration-statement until proven
9326          otherwise.)  */
9327       if (cp_parser_error_occurred (parser))
9328         goto done;
9329       /* Handle function definitions specially.  */
9330       if (function_definition_p)
9331         {
9332           /* If the next token is a `,', then we are probably
9333              processing something like:
9334
9335                void f() {}, *p;
9336
9337              which is erroneous.  */
9338           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9339             {
9340               cp_token *token = cp_lexer_peek_token (parser->lexer);
9341               error_at (token->location,
9342                         "mixing"
9343                         " declarations and function-definitions is forbidden");
9344             }
9345           /* Otherwise, we're done with the list of declarators.  */
9346           else
9347             {
9348               pop_deferring_access_checks ();
9349               return;
9350             }
9351         }
9352       /* The next token should be either a `,' or a `;'.  */
9353       token = cp_lexer_peek_token (parser->lexer);
9354       /* If it's a `,', there are more declarators to come.  */
9355       if (token->type == CPP_COMMA)
9356         /* will be consumed next time around */;
9357       /* If it's a `;', we are done.  */
9358       else if (token->type == CPP_SEMICOLON)
9359         break;
9360       /* Anything else is an error.  */
9361       else
9362         {
9363           /* If we have already issued an error message we don't need
9364              to issue another one.  */
9365           if (decl != error_mark_node
9366               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9367             cp_parser_error (parser, "expected %<,%> or %<;%>");
9368           /* Skip tokens until we reach the end of the statement.  */
9369           cp_parser_skip_to_end_of_statement (parser);
9370           /* If the next token is now a `;', consume it.  */
9371           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9372             cp_lexer_consume_token (parser->lexer);
9373           goto done;
9374         }
9375       /* After the first time around, a function-definition is not
9376          allowed -- even if it was OK at first.  For example:
9377
9378            int i, f() {}
9379
9380          is not valid.  */
9381       function_definition_allowed_p = false;
9382     }
9383
9384   /* Issue an error message if no declarators are present, and the
9385      decl-specifier-seq does not itself declare a class or
9386      enumeration.  */
9387   if (!saw_declarator)
9388     {
9389       if (cp_parser_declares_only_class_p (parser))
9390         shadow_tag (&decl_specifiers);
9391       /* Perform any deferred access checks.  */
9392       perform_deferred_access_checks ();
9393     }
9394
9395   /* Consume the `;'.  */
9396   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9397
9398  done:
9399   pop_deferring_access_checks ();
9400 }
9401
9402 /* Parse a decl-specifier-seq.
9403
9404    decl-specifier-seq:
9405      decl-specifier-seq [opt] decl-specifier
9406
9407    decl-specifier:
9408      storage-class-specifier
9409      type-specifier
9410      function-specifier
9411      friend
9412      typedef
9413
9414    GNU Extension:
9415
9416    decl-specifier:
9417      attributes
9418
9419    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9420
9421    The parser flags FLAGS is used to control type-specifier parsing.
9422
9423    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9424    flags:
9425
9426      1: one of the decl-specifiers is an elaborated-type-specifier
9427         (i.e., a type declaration)
9428      2: one of the decl-specifiers is an enum-specifier or a
9429         class-specifier (i.e., a type definition)
9430
9431    */
9432
9433 static void
9434 cp_parser_decl_specifier_seq (cp_parser* parser,
9435                               cp_parser_flags flags,
9436                               cp_decl_specifier_seq *decl_specs,
9437                               int* declares_class_or_enum)
9438 {
9439   bool constructor_possible_p = !parser->in_declarator_p;
9440   cp_token *start_token = NULL;
9441
9442   /* Clear DECL_SPECS.  */
9443   clear_decl_specs (decl_specs);
9444
9445   /* Assume no class or enumeration type is declared.  */
9446   *declares_class_or_enum = 0;
9447
9448   /* Keep reading specifiers until there are no more to read.  */
9449   while (true)
9450     {
9451       bool constructor_p;
9452       bool found_decl_spec;
9453       cp_token *token;
9454
9455       /* Peek at the next token.  */
9456       token = cp_lexer_peek_token (parser->lexer);
9457
9458       /* Save the first token of the decl spec list for error
9459          reporting.  */
9460       if (!start_token)
9461         start_token = token;
9462       /* Handle attributes.  */
9463       if (token->keyword == RID_ATTRIBUTE)
9464         {
9465           /* Parse the attributes.  */
9466           decl_specs->attributes
9467             = chainon (decl_specs->attributes,
9468                        cp_parser_attributes_opt (parser));
9469           continue;
9470         }
9471       /* Assume we will find a decl-specifier keyword.  */
9472       found_decl_spec = true;
9473       /* If the next token is an appropriate keyword, we can simply
9474          add it to the list.  */
9475       switch (token->keyword)
9476         {
9477           /* decl-specifier:
9478                friend
9479                constexpr */
9480         case RID_FRIEND:
9481           if (!at_class_scope_p ())
9482             {
9483               error_at (token->location, "%<friend%> used outside of class");
9484               cp_lexer_purge_token (parser->lexer);
9485             }
9486           else
9487             {
9488               ++decl_specs->specs[(int) ds_friend];
9489               /* Consume the token.  */
9490               cp_lexer_consume_token (parser->lexer);
9491             }
9492           break;
9493
9494         case RID_CONSTEXPR:
9495           ++decl_specs->specs[(int) ds_constexpr];
9496           cp_lexer_consume_token (parser->lexer);
9497           break;
9498
9499           /* function-specifier:
9500                inline
9501                virtual
9502                explicit  */
9503         case RID_INLINE:
9504         case RID_VIRTUAL:
9505         case RID_EXPLICIT:
9506           cp_parser_function_specifier_opt (parser, decl_specs);
9507           break;
9508
9509           /* decl-specifier:
9510                typedef  */
9511         case RID_TYPEDEF:
9512           ++decl_specs->specs[(int) ds_typedef];
9513           /* Consume the token.  */
9514           cp_lexer_consume_token (parser->lexer);
9515           /* A constructor declarator cannot appear in a typedef.  */
9516           constructor_possible_p = false;
9517           /* The "typedef" keyword can only occur in a declaration; we
9518              may as well commit at this point.  */
9519           cp_parser_commit_to_tentative_parse (parser);
9520
9521           if (decl_specs->storage_class != sc_none)
9522             decl_specs->conflicting_specifiers_p = true;
9523           break;
9524
9525           /* storage-class-specifier:
9526                auto
9527                register
9528                static
9529                extern
9530                mutable
9531
9532              GNU Extension:
9533                thread  */
9534         case RID_AUTO:
9535           if (cxx_dialect == cxx98) 
9536             {
9537               /* Consume the token.  */
9538               cp_lexer_consume_token (parser->lexer);
9539
9540               /* Complain about `auto' as a storage specifier, if
9541                  we're complaining about C++0x compatibility.  */
9542               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9543                           " will change meaning in C++0x; please remove it");
9544
9545               /* Set the storage class anyway.  */
9546               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9547                                            token->location);
9548             }
9549           else
9550             /* C++0x auto type-specifier.  */
9551             found_decl_spec = false;
9552           break;
9553
9554         case RID_REGISTER:
9555         case RID_STATIC:
9556         case RID_EXTERN:
9557         case RID_MUTABLE:
9558           /* Consume the token.  */
9559           cp_lexer_consume_token (parser->lexer);
9560           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9561                                        token->location);
9562           break;
9563         case RID_THREAD:
9564           /* Consume the token.  */
9565           cp_lexer_consume_token (parser->lexer);
9566           ++decl_specs->specs[(int) ds_thread];
9567           break;
9568
9569         default:
9570           /* We did not yet find a decl-specifier yet.  */
9571           found_decl_spec = false;
9572           break;
9573         }
9574
9575       /* Constructors are a special case.  The `S' in `S()' is not a
9576          decl-specifier; it is the beginning of the declarator.  */
9577       constructor_p
9578         = (!found_decl_spec
9579            && constructor_possible_p
9580            && (cp_parser_constructor_declarator_p
9581                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9582
9583       /* If we don't have a DECL_SPEC yet, then we must be looking at
9584          a type-specifier.  */
9585       if (!found_decl_spec && !constructor_p)
9586         {
9587           int decl_spec_declares_class_or_enum;
9588           bool is_cv_qualifier;
9589           tree type_spec;
9590
9591           type_spec
9592             = cp_parser_type_specifier (parser, flags,
9593                                         decl_specs,
9594                                         /*is_declaration=*/true,
9595                                         &decl_spec_declares_class_or_enum,
9596                                         &is_cv_qualifier);
9597           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9598
9599           /* If this type-specifier referenced a user-defined type
9600              (a typedef, class-name, etc.), then we can't allow any
9601              more such type-specifiers henceforth.
9602
9603              [dcl.spec]
9604
9605              The longest sequence of decl-specifiers that could
9606              possibly be a type name is taken as the
9607              decl-specifier-seq of a declaration.  The sequence shall
9608              be self-consistent as described below.
9609
9610              [dcl.type]
9611
9612              As a general rule, at most one type-specifier is allowed
9613              in the complete decl-specifier-seq of a declaration.  The
9614              only exceptions are the following:
9615
9616              -- const or volatile can be combined with any other
9617                 type-specifier.
9618
9619              -- signed or unsigned can be combined with char, long,
9620                 short, or int.
9621
9622              -- ..
9623
9624              Example:
9625
9626                typedef char* Pc;
9627                void g (const int Pc);
9628
9629              Here, Pc is *not* part of the decl-specifier seq; it's
9630              the declarator.  Therefore, once we see a type-specifier
9631              (other than a cv-qualifier), we forbid any additional
9632              user-defined types.  We *do* still allow things like `int
9633              int' to be considered a decl-specifier-seq, and issue the
9634              error message later.  */
9635           if (type_spec && !is_cv_qualifier)
9636             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9637           /* A constructor declarator cannot follow a type-specifier.  */
9638           if (type_spec)
9639             {
9640               constructor_possible_p = false;
9641               found_decl_spec = true;
9642               if (!is_cv_qualifier)
9643                 decl_specs->any_type_specifiers_p = true;
9644             }
9645         }
9646
9647       /* If we still do not have a DECL_SPEC, then there are no more
9648          decl-specifiers.  */
9649       if (!found_decl_spec)
9650         break;
9651
9652       decl_specs->any_specifiers_p = true;
9653       /* After we see one decl-specifier, further decl-specifiers are
9654          always optional.  */
9655       flags |= CP_PARSER_FLAGS_OPTIONAL;
9656     }
9657
9658   cp_parser_check_decl_spec (decl_specs, start_token->location);
9659
9660   /* Don't allow a friend specifier with a class definition.  */
9661   if (decl_specs->specs[(int) ds_friend] != 0
9662       && (*declares_class_or_enum & 2))
9663     error_at (start_token->location,
9664               "class definition may not be declared a friend");
9665 }
9666
9667 /* Parse an (optional) storage-class-specifier.
9668
9669    storage-class-specifier:
9670      auto
9671      register
9672      static
9673      extern
9674      mutable
9675
9676    GNU Extension:
9677
9678    storage-class-specifier:
9679      thread
9680
9681    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9682
9683 static tree
9684 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9685 {
9686   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9687     {
9688     case RID_AUTO:
9689       if (cxx_dialect != cxx98)
9690         return NULL_TREE;
9691       /* Fall through for C++98.  */
9692
9693     case RID_REGISTER:
9694     case RID_STATIC:
9695     case RID_EXTERN:
9696     case RID_MUTABLE:
9697     case RID_THREAD:
9698       /* Consume the token.  */
9699       return cp_lexer_consume_token (parser->lexer)->u.value;
9700
9701     default:
9702       return NULL_TREE;
9703     }
9704 }
9705
9706 /* Parse an (optional) function-specifier.
9707
9708    function-specifier:
9709      inline
9710      virtual
9711      explicit
9712
9713    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9714    Updates DECL_SPECS, if it is non-NULL.  */
9715
9716 static tree
9717 cp_parser_function_specifier_opt (cp_parser* parser,
9718                                   cp_decl_specifier_seq *decl_specs)
9719 {
9720   cp_token *token = cp_lexer_peek_token (parser->lexer);
9721   switch (token->keyword)
9722     {
9723     case RID_INLINE:
9724       if (decl_specs)
9725         ++decl_specs->specs[(int) ds_inline];
9726       break;
9727
9728     case RID_VIRTUAL:
9729       /* 14.5.2.3 [temp.mem]
9730
9731          A member function template shall not be virtual.  */
9732       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9733         error_at (token->location, "templates may not be %<virtual%>");
9734       else if (decl_specs)
9735         ++decl_specs->specs[(int) ds_virtual];
9736       break;
9737
9738     case RID_EXPLICIT:
9739       if (decl_specs)
9740         ++decl_specs->specs[(int) ds_explicit];
9741       break;
9742
9743     default:
9744       return NULL_TREE;
9745     }
9746
9747   /* Consume the token.  */
9748   return cp_lexer_consume_token (parser->lexer)->u.value;
9749 }
9750
9751 /* Parse a linkage-specification.
9752
9753    linkage-specification:
9754      extern string-literal { declaration-seq [opt] }
9755      extern string-literal declaration  */
9756
9757 static void
9758 cp_parser_linkage_specification (cp_parser* parser)
9759 {
9760   tree linkage;
9761
9762   /* Look for the `extern' keyword.  */
9763   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
9764
9765   /* Look for the string-literal.  */
9766   linkage = cp_parser_string_literal (parser, false, false);
9767
9768   /* Transform the literal into an identifier.  If the literal is a
9769      wide-character string, or contains embedded NULs, then we can't
9770      handle it as the user wants.  */
9771   if (strlen (TREE_STRING_POINTER (linkage))
9772       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9773     {
9774       cp_parser_error (parser, "invalid linkage-specification");
9775       /* Assume C++ linkage.  */
9776       linkage = lang_name_cplusplus;
9777     }
9778   else
9779     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9780
9781   /* We're now using the new linkage.  */
9782   push_lang_context (linkage);
9783
9784   /* If the next token is a `{', then we're using the first
9785      production.  */
9786   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9787     {
9788       /* Consume the `{' token.  */
9789       cp_lexer_consume_token (parser->lexer);
9790       /* Parse the declarations.  */
9791       cp_parser_declaration_seq_opt (parser);
9792       /* Look for the closing `}'.  */
9793       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9794     }
9795   /* Otherwise, there's just one declaration.  */
9796   else
9797     {
9798       bool saved_in_unbraced_linkage_specification_p;
9799
9800       saved_in_unbraced_linkage_specification_p
9801         = parser->in_unbraced_linkage_specification_p;
9802       parser->in_unbraced_linkage_specification_p = true;
9803       cp_parser_declaration (parser);
9804       parser->in_unbraced_linkage_specification_p
9805         = saved_in_unbraced_linkage_specification_p;
9806     }
9807
9808   /* We're done with the linkage-specification.  */
9809   pop_lang_context ();
9810 }
9811
9812 /* Parse a static_assert-declaration.
9813
9814    static_assert-declaration:
9815      static_assert ( constant-expression , string-literal ) ; 
9816
9817    If MEMBER_P, this static_assert is a class member.  */
9818
9819 static void 
9820 cp_parser_static_assert(cp_parser *parser, bool member_p)
9821 {
9822   tree condition;
9823   tree message;
9824   cp_token *token;
9825   location_t saved_loc;
9826
9827   /* Peek at the `static_assert' token so we can keep track of exactly
9828      where the static assertion started.  */
9829   token = cp_lexer_peek_token (parser->lexer);
9830   saved_loc = token->location;
9831
9832   /* Look for the `static_assert' keyword.  */
9833   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9834                                   RT_STATIC_ASSERT))
9835     return;
9836
9837   /*  We know we are in a static assertion; commit to any tentative
9838       parse.  */
9839   if (cp_parser_parsing_tentatively (parser))
9840     cp_parser_commit_to_tentative_parse (parser);
9841
9842   /* Parse the `(' starting the static assertion condition.  */
9843   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9844
9845   /* Parse the constant-expression.  */
9846   condition = 
9847     cp_parser_constant_expression (parser,
9848                                    /*allow_non_constant_p=*/false,
9849                                    /*non_constant_p=*/NULL);
9850
9851   /* Parse the separating `,'.  */
9852   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9853
9854   /* Parse the string-literal message.  */
9855   message = cp_parser_string_literal (parser, 
9856                                       /*translate=*/false,
9857                                       /*wide_ok=*/true);
9858
9859   /* A `)' completes the static assertion.  */
9860   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9861     cp_parser_skip_to_closing_parenthesis (parser, 
9862                                            /*recovering=*/true, 
9863                                            /*or_comma=*/false,
9864                                            /*consume_paren=*/true);
9865
9866   /* A semicolon terminates the declaration.  */
9867   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9868
9869   /* Complete the static assertion, which may mean either processing 
9870      the static assert now or saving it for template instantiation.  */
9871   finish_static_assert (condition, message, saved_loc, member_p);
9872 }
9873
9874 /* Parse a `decltype' type. Returns the type. 
9875
9876    simple-type-specifier:
9877      decltype ( expression )  */
9878
9879 static tree
9880 cp_parser_decltype (cp_parser *parser)
9881 {
9882   tree expr;
9883   bool id_expression_or_member_access_p = false;
9884   const char *saved_message;
9885   bool saved_integral_constant_expression_p;
9886   bool saved_non_integral_constant_expression_p;
9887   cp_token *id_expr_start_token;
9888
9889   /* Look for the `decltype' token.  */
9890   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
9891     return error_mark_node;
9892
9893   /* Types cannot be defined in a `decltype' expression.  Save away the
9894      old message.  */
9895   saved_message = parser->type_definition_forbidden_message;
9896
9897   /* And create the new one.  */
9898   parser->type_definition_forbidden_message
9899     = G_("types may not be defined in %<decltype%> expressions");
9900
9901   /* The restrictions on constant-expressions do not apply inside
9902      decltype expressions.  */
9903   saved_integral_constant_expression_p
9904     = parser->integral_constant_expression_p;
9905   saved_non_integral_constant_expression_p
9906     = parser->non_integral_constant_expression_p;
9907   parser->integral_constant_expression_p = false;
9908
9909   /* Do not actually evaluate the expression.  */
9910   ++cp_unevaluated_operand;
9911
9912   /* Do not warn about problems with the expression.  */
9913   ++c_inhibit_evaluation_warnings;
9914
9915   /* Parse the opening `('.  */
9916   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9917     return error_mark_node;
9918   
9919   /* First, try parsing an id-expression.  */
9920   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9921   cp_parser_parse_tentatively (parser);
9922   expr = cp_parser_id_expression (parser,
9923                                   /*template_keyword_p=*/false,
9924                                   /*check_dependency_p=*/true,
9925                                   /*template_p=*/NULL,
9926                                   /*declarator_p=*/false,
9927                                   /*optional_p=*/false);
9928
9929   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9930     {
9931       bool non_integral_constant_expression_p = false;
9932       tree id_expression = expr;
9933       cp_id_kind idk;
9934       const char *error_msg;
9935
9936       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9937         /* Lookup the name we got back from the id-expression.  */
9938         expr = cp_parser_lookup_name (parser, expr,
9939                                       none_type,
9940                                       /*is_template=*/false,
9941                                       /*is_namespace=*/false,
9942                                       /*check_dependency=*/true,
9943                                       /*ambiguous_decls=*/NULL,
9944                                       id_expr_start_token->location);
9945
9946       if (expr
9947           && expr != error_mark_node
9948           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9949           && TREE_CODE (expr) != TYPE_DECL
9950           && (TREE_CODE (expr) != BIT_NOT_EXPR
9951               || !TYPE_P (TREE_OPERAND (expr, 0)))
9952           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9953         {
9954           /* Complete lookup of the id-expression.  */
9955           expr = (finish_id_expression
9956                   (id_expression, expr, parser->scope, &idk,
9957                    /*integral_constant_expression_p=*/false,
9958                    /*allow_non_integral_constant_expression_p=*/true,
9959                    &non_integral_constant_expression_p,
9960                    /*template_p=*/false,
9961                    /*done=*/true,
9962                    /*address_p=*/false,
9963                    /*template_arg_p=*/false,
9964                    &error_msg,
9965                    id_expr_start_token->location));
9966
9967           if (expr == error_mark_node)
9968             /* We found an id-expression, but it was something that we
9969                should not have found. This is an error, not something
9970                we can recover from, so note that we found an
9971                id-expression and we'll recover as gracefully as
9972                possible.  */
9973             id_expression_or_member_access_p = true;
9974         }
9975
9976       if (expr 
9977           && expr != error_mark_node
9978           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9979         /* We have an id-expression.  */
9980         id_expression_or_member_access_p = true;
9981     }
9982
9983   if (!id_expression_or_member_access_p)
9984     {
9985       /* Abort the id-expression parse.  */
9986       cp_parser_abort_tentative_parse (parser);
9987
9988       /* Parsing tentatively, again.  */
9989       cp_parser_parse_tentatively (parser);
9990
9991       /* Parse a class member access.  */
9992       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9993                                            /*cast_p=*/false,
9994                                            /*member_access_only_p=*/true, NULL);
9995
9996       if (expr 
9997           && expr != error_mark_node
9998           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9999         /* We have an id-expression.  */
10000         id_expression_or_member_access_p = true;
10001     }
10002
10003   if (id_expression_or_member_access_p)
10004     /* We have parsed the complete id-expression or member access.  */
10005     cp_parser_parse_definitely (parser);
10006   else
10007     {
10008       bool saved_greater_than_is_operator_p;
10009
10010       /* Abort our attempt to parse an id-expression or member access
10011          expression.  */
10012       cp_parser_abort_tentative_parse (parser);
10013
10014       /* Within a parenthesized expression, a `>' token is always
10015          the greater-than operator.  */
10016       saved_greater_than_is_operator_p
10017         = parser->greater_than_is_operator_p;
10018       parser->greater_than_is_operator_p = true;
10019
10020       /* Parse a full expression.  */
10021       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10022
10023       /* The `>' token might be the end of a template-id or
10024          template-parameter-list now.  */
10025       parser->greater_than_is_operator_p
10026         = saved_greater_than_is_operator_p;
10027     }
10028
10029   /* Go back to evaluating expressions.  */
10030   --cp_unevaluated_operand;
10031   --c_inhibit_evaluation_warnings;
10032
10033   /* Restore the old message and the integral constant expression
10034      flags.  */
10035   parser->type_definition_forbidden_message = saved_message;
10036   parser->integral_constant_expression_p
10037     = saved_integral_constant_expression_p;
10038   parser->non_integral_constant_expression_p
10039     = saved_non_integral_constant_expression_p;
10040
10041   if (expr == error_mark_node)
10042     {
10043       /* Skip everything up to the closing `)'.  */
10044       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10045                                              /*consume_paren=*/true);
10046       return error_mark_node;
10047     }
10048   
10049   /* Parse to the closing `)'.  */
10050   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10051     {
10052       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10053                                              /*consume_paren=*/true);
10054       return error_mark_node;
10055     }
10056
10057   return finish_decltype_type (expr, id_expression_or_member_access_p);
10058 }
10059
10060 /* Special member functions [gram.special] */
10061
10062 /* Parse a conversion-function-id.
10063
10064    conversion-function-id:
10065      operator conversion-type-id
10066
10067    Returns an IDENTIFIER_NODE representing the operator.  */
10068
10069 static tree
10070 cp_parser_conversion_function_id (cp_parser* parser)
10071 {
10072   tree type;
10073   tree saved_scope;
10074   tree saved_qualifying_scope;
10075   tree saved_object_scope;
10076   tree pushed_scope = NULL_TREE;
10077
10078   /* Look for the `operator' token.  */
10079   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10080     return error_mark_node;
10081   /* When we parse the conversion-type-id, the current scope will be
10082      reset.  However, we need that information in able to look up the
10083      conversion function later, so we save it here.  */
10084   saved_scope = parser->scope;
10085   saved_qualifying_scope = parser->qualifying_scope;
10086   saved_object_scope = parser->object_scope;
10087   /* We must enter the scope of the class so that the names of
10088      entities declared within the class are available in the
10089      conversion-type-id.  For example, consider:
10090
10091        struct S {
10092          typedef int I;
10093          operator I();
10094        };
10095
10096        S::operator I() { ... }
10097
10098      In order to see that `I' is a type-name in the definition, we
10099      must be in the scope of `S'.  */
10100   if (saved_scope)
10101     pushed_scope = push_scope (saved_scope);
10102   /* Parse the conversion-type-id.  */
10103   type = cp_parser_conversion_type_id (parser);
10104   /* Leave the scope of the class, if any.  */
10105   if (pushed_scope)
10106     pop_scope (pushed_scope);
10107   /* Restore the saved scope.  */
10108   parser->scope = saved_scope;
10109   parser->qualifying_scope = saved_qualifying_scope;
10110   parser->object_scope = saved_object_scope;
10111   /* If the TYPE is invalid, indicate failure.  */
10112   if (type == error_mark_node)
10113     return error_mark_node;
10114   return mangle_conv_op_name_for_type (type);
10115 }
10116
10117 /* Parse a conversion-type-id:
10118
10119    conversion-type-id:
10120      type-specifier-seq conversion-declarator [opt]
10121
10122    Returns the TYPE specified.  */
10123
10124 static tree
10125 cp_parser_conversion_type_id (cp_parser* parser)
10126 {
10127   tree attributes;
10128   cp_decl_specifier_seq type_specifiers;
10129   cp_declarator *declarator;
10130   tree type_specified;
10131
10132   /* Parse the attributes.  */
10133   attributes = cp_parser_attributes_opt (parser);
10134   /* Parse the type-specifiers.  */
10135   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10136                                 /*is_trailing_return=*/false,
10137                                 &type_specifiers);
10138   /* If that didn't work, stop.  */
10139   if (type_specifiers.type == error_mark_node)
10140     return error_mark_node;
10141   /* Parse the conversion-declarator.  */
10142   declarator = cp_parser_conversion_declarator_opt (parser);
10143
10144   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10145                                     /*initialized=*/0, &attributes);
10146   if (attributes)
10147     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10148
10149   /* Don't give this error when parsing tentatively.  This happens to
10150      work because we always parse this definitively once.  */
10151   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10152       && type_uses_auto (type_specified))
10153     {
10154       error ("invalid use of %<auto%> in conversion operator");
10155       return error_mark_node;
10156     }
10157
10158   return type_specified;
10159 }
10160
10161 /* Parse an (optional) conversion-declarator.
10162
10163    conversion-declarator:
10164      ptr-operator conversion-declarator [opt]
10165
10166    */
10167
10168 static cp_declarator *
10169 cp_parser_conversion_declarator_opt (cp_parser* parser)
10170 {
10171   enum tree_code code;
10172   tree class_type;
10173   cp_cv_quals cv_quals;
10174
10175   /* We don't know if there's a ptr-operator next, or not.  */
10176   cp_parser_parse_tentatively (parser);
10177   /* Try the ptr-operator.  */
10178   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10179   /* If it worked, look for more conversion-declarators.  */
10180   if (cp_parser_parse_definitely (parser))
10181     {
10182       cp_declarator *declarator;
10183
10184       /* Parse another optional declarator.  */
10185       declarator = cp_parser_conversion_declarator_opt (parser);
10186
10187       return cp_parser_make_indirect_declarator
10188         (code, class_type, cv_quals, declarator);
10189    }
10190
10191   return NULL;
10192 }
10193
10194 /* Parse an (optional) ctor-initializer.
10195
10196    ctor-initializer:
10197      : mem-initializer-list
10198
10199    Returns TRUE iff the ctor-initializer was actually present.  */
10200
10201 static bool
10202 cp_parser_ctor_initializer_opt (cp_parser* parser)
10203 {
10204   /* If the next token is not a `:', then there is no
10205      ctor-initializer.  */
10206   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10207     {
10208       /* Do default initialization of any bases and members.  */
10209       if (DECL_CONSTRUCTOR_P (current_function_decl))
10210         finish_mem_initializers (NULL_TREE);
10211
10212       return false;
10213     }
10214
10215   /* Consume the `:' token.  */
10216   cp_lexer_consume_token (parser->lexer);
10217   /* And the mem-initializer-list.  */
10218   cp_parser_mem_initializer_list (parser);
10219
10220   return true;
10221 }
10222
10223 /* Parse a mem-initializer-list.
10224
10225    mem-initializer-list:
10226      mem-initializer ... [opt]
10227      mem-initializer ... [opt] , mem-initializer-list  */
10228
10229 static void
10230 cp_parser_mem_initializer_list (cp_parser* parser)
10231 {
10232   tree mem_initializer_list = NULL_TREE;
10233   cp_token *token = cp_lexer_peek_token (parser->lexer);
10234
10235   /* Let the semantic analysis code know that we are starting the
10236      mem-initializer-list.  */
10237   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10238     error_at (token->location,
10239               "only constructors take member initializers");
10240
10241   /* Loop through the list.  */
10242   while (true)
10243     {
10244       tree mem_initializer;
10245
10246       token = cp_lexer_peek_token (parser->lexer);
10247       /* Parse the mem-initializer.  */
10248       mem_initializer = cp_parser_mem_initializer (parser);
10249       /* If the next token is a `...', we're expanding member initializers. */
10250       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10251         {
10252           /* Consume the `...'. */
10253           cp_lexer_consume_token (parser->lexer);
10254
10255           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10256              can be expanded but members cannot. */
10257           if (mem_initializer != error_mark_node
10258               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10259             {
10260               error_at (token->location,
10261                         "cannot expand initializer for member %<%D%>",
10262                         TREE_PURPOSE (mem_initializer));
10263               mem_initializer = error_mark_node;
10264             }
10265
10266           /* Construct the pack expansion type. */
10267           if (mem_initializer != error_mark_node)
10268             mem_initializer = make_pack_expansion (mem_initializer);
10269         }
10270       /* Add it to the list, unless it was erroneous.  */
10271       if (mem_initializer != error_mark_node)
10272         {
10273           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10274           mem_initializer_list = mem_initializer;
10275         }
10276       /* If the next token is not a `,', we're done.  */
10277       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10278         break;
10279       /* Consume the `,' token.  */
10280       cp_lexer_consume_token (parser->lexer);
10281     }
10282
10283   /* Perform semantic analysis.  */
10284   if (DECL_CONSTRUCTOR_P (current_function_decl))
10285     finish_mem_initializers (mem_initializer_list);
10286 }
10287
10288 /* Parse a mem-initializer.
10289
10290    mem-initializer:
10291      mem-initializer-id ( expression-list [opt] )
10292      mem-initializer-id braced-init-list
10293
10294    GNU extension:
10295
10296    mem-initializer:
10297      ( expression-list [opt] )
10298
10299    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10300    class) or FIELD_DECL (for a non-static data member) to initialize;
10301    the TREE_VALUE is the expression-list.  An empty initialization
10302    list is represented by void_list_node.  */
10303
10304 static tree
10305 cp_parser_mem_initializer (cp_parser* parser)
10306 {
10307   tree mem_initializer_id;
10308   tree expression_list;
10309   tree member;
10310   cp_token *token = cp_lexer_peek_token (parser->lexer);
10311
10312   /* Find out what is being initialized.  */
10313   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10314     {
10315       permerror (token->location,
10316                  "anachronistic old-style base class initializer");
10317       mem_initializer_id = NULL_TREE;
10318     }
10319   else
10320     {
10321       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10322       if (mem_initializer_id == error_mark_node)
10323         return mem_initializer_id;
10324     }
10325   member = expand_member_init (mem_initializer_id);
10326   if (member && !DECL_P (member))
10327     in_base_initializer = 1;
10328
10329   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10330     {
10331       bool expr_non_constant_p;
10332       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10333       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10334       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10335       expression_list = build_tree_list (NULL_TREE, expression_list);
10336     }
10337   else
10338     {
10339       VEC(tree,gc)* vec;
10340       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10341                                                      /*cast_p=*/false,
10342                                                      /*allow_expansion_p=*/true,
10343                                                      /*non_constant_p=*/NULL);
10344       if (vec == NULL)
10345         return error_mark_node;
10346       expression_list = build_tree_list_vec (vec);
10347       release_tree_vector (vec);
10348     }
10349
10350   if (expression_list == error_mark_node)
10351     return error_mark_node;
10352   if (!expression_list)
10353     expression_list = void_type_node;
10354
10355   in_base_initializer = 0;
10356
10357   return member ? build_tree_list (member, expression_list) : error_mark_node;
10358 }
10359
10360 /* Parse a mem-initializer-id.
10361
10362    mem-initializer-id:
10363      :: [opt] nested-name-specifier [opt] class-name
10364      identifier
10365
10366    Returns a TYPE indicating the class to be initializer for the first
10367    production.  Returns an IDENTIFIER_NODE indicating the data member
10368    to be initialized for the second production.  */
10369
10370 static tree
10371 cp_parser_mem_initializer_id (cp_parser* parser)
10372 {
10373   bool global_scope_p;
10374   bool nested_name_specifier_p;
10375   bool template_p = false;
10376   tree id;
10377
10378   cp_token *token = cp_lexer_peek_token (parser->lexer);
10379
10380   /* `typename' is not allowed in this context ([temp.res]).  */
10381   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10382     {
10383       error_at (token->location, 
10384                 "keyword %<typename%> not allowed in this context (a qualified "
10385                 "member initializer is implicitly a type)");
10386       cp_lexer_consume_token (parser->lexer);
10387     }
10388   /* Look for the optional `::' operator.  */
10389   global_scope_p
10390     = (cp_parser_global_scope_opt (parser,
10391                                    /*current_scope_valid_p=*/false)
10392        != NULL_TREE);
10393   /* Look for the optional nested-name-specifier.  The simplest way to
10394      implement:
10395
10396        [temp.res]
10397
10398        The keyword `typename' is not permitted in a base-specifier or
10399        mem-initializer; in these contexts a qualified name that
10400        depends on a template-parameter is implicitly assumed to be a
10401        type name.
10402
10403      is to assume that we have seen the `typename' keyword at this
10404      point.  */
10405   nested_name_specifier_p
10406     = (cp_parser_nested_name_specifier_opt (parser,
10407                                             /*typename_keyword_p=*/true,
10408                                             /*check_dependency_p=*/true,
10409                                             /*type_p=*/true,
10410                                             /*is_declaration=*/true)
10411        != NULL_TREE);
10412   if (nested_name_specifier_p)
10413     template_p = cp_parser_optional_template_keyword (parser);
10414   /* If there is a `::' operator or a nested-name-specifier, then we
10415      are definitely looking for a class-name.  */
10416   if (global_scope_p || nested_name_specifier_p)
10417     return cp_parser_class_name (parser,
10418                                  /*typename_keyword_p=*/true,
10419                                  /*template_keyword_p=*/template_p,
10420                                  typename_type,
10421                                  /*check_dependency_p=*/true,
10422                                  /*class_head_p=*/false,
10423                                  /*is_declaration=*/true);
10424   /* Otherwise, we could also be looking for an ordinary identifier.  */
10425   cp_parser_parse_tentatively (parser);
10426   /* Try a class-name.  */
10427   id = cp_parser_class_name (parser,
10428                              /*typename_keyword_p=*/true,
10429                              /*template_keyword_p=*/false,
10430                              none_type,
10431                              /*check_dependency_p=*/true,
10432                              /*class_head_p=*/false,
10433                              /*is_declaration=*/true);
10434   /* If we found one, we're done.  */
10435   if (cp_parser_parse_definitely (parser))
10436     return id;
10437   /* Otherwise, look for an ordinary identifier.  */
10438   return cp_parser_identifier (parser);
10439 }
10440
10441 /* Overloading [gram.over] */
10442
10443 /* Parse an operator-function-id.
10444
10445    operator-function-id:
10446      operator operator
10447
10448    Returns an IDENTIFIER_NODE for the operator which is a
10449    human-readable spelling of the identifier, e.g., `operator +'.  */
10450
10451 static tree
10452 cp_parser_operator_function_id (cp_parser* parser)
10453 {
10454   /* Look for the `operator' keyword.  */
10455   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10456     return error_mark_node;
10457   /* And then the name of the operator itself.  */
10458   return cp_parser_operator (parser);
10459 }
10460
10461 /* Parse an operator.
10462
10463    operator:
10464      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10465      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10466      || ++ -- , ->* -> () []
10467
10468    GNU Extensions:
10469
10470    operator:
10471      <? >? <?= >?=
10472
10473    Returns an IDENTIFIER_NODE for the operator which is a
10474    human-readable spelling of the identifier, e.g., `operator +'.  */
10475
10476 static tree
10477 cp_parser_operator (cp_parser* parser)
10478 {
10479   tree id = NULL_TREE;
10480   cp_token *token;
10481
10482   /* Peek at the next token.  */
10483   token = cp_lexer_peek_token (parser->lexer);
10484   /* Figure out which operator we have.  */
10485   switch (token->type)
10486     {
10487     case CPP_KEYWORD:
10488       {
10489         enum tree_code op;
10490
10491         /* The keyword should be either `new' or `delete'.  */
10492         if (token->keyword == RID_NEW)
10493           op = NEW_EXPR;
10494         else if (token->keyword == RID_DELETE)
10495           op = DELETE_EXPR;
10496         else
10497           break;
10498
10499         /* Consume the `new' or `delete' token.  */
10500         cp_lexer_consume_token (parser->lexer);
10501
10502         /* Peek at the next token.  */
10503         token = cp_lexer_peek_token (parser->lexer);
10504         /* If it's a `[' token then this is the array variant of the
10505            operator.  */
10506         if (token->type == CPP_OPEN_SQUARE)
10507           {
10508             /* Consume the `[' token.  */
10509             cp_lexer_consume_token (parser->lexer);
10510             /* Look for the `]' token.  */
10511             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10512             id = ansi_opname (op == NEW_EXPR
10513                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10514           }
10515         /* Otherwise, we have the non-array variant.  */
10516         else
10517           id = ansi_opname (op);
10518
10519         return id;
10520       }
10521
10522     case CPP_PLUS:
10523       id = ansi_opname (PLUS_EXPR);
10524       break;
10525
10526     case CPP_MINUS:
10527       id = ansi_opname (MINUS_EXPR);
10528       break;
10529
10530     case CPP_MULT:
10531       id = ansi_opname (MULT_EXPR);
10532       break;
10533
10534     case CPP_DIV:
10535       id = ansi_opname (TRUNC_DIV_EXPR);
10536       break;
10537
10538     case CPP_MOD:
10539       id = ansi_opname (TRUNC_MOD_EXPR);
10540       break;
10541
10542     case CPP_XOR:
10543       id = ansi_opname (BIT_XOR_EXPR);
10544       break;
10545
10546     case CPP_AND:
10547       id = ansi_opname (BIT_AND_EXPR);
10548       break;
10549
10550     case CPP_OR:
10551       id = ansi_opname (BIT_IOR_EXPR);
10552       break;
10553
10554     case CPP_COMPL:
10555       id = ansi_opname (BIT_NOT_EXPR);
10556       break;
10557
10558     case CPP_NOT:
10559       id = ansi_opname (TRUTH_NOT_EXPR);
10560       break;
10561
10562     case CPP_EQ:
10563       id = ansi_assopname (NOP_EXPR);
10564       break;
10565
10566     case CPP_LESS:
10567       id = ansi_opname (LT_EXPR);
10568       break;
10569
10570     case CPP_GREATER:
10571       id = ansi_opname (GT_EXPR);
10572       break;
10573
10574     case CPP_PLUS_EQ:
10575       id = ansi_assopname (PLUS_EXPR);
10576       break;
10577
10578     case CPP_MINUS_EQ:
10579       id = ansi_assopname (MINUS_EXPR);
10580       break;
10581
10582     case CPP_MULT_EQ:
10583       id = ansi_assopname (MULT_EXPR);
10584       break;
10585
10586     case CPP_DIV_EQ:
10587       id = ansi_assopname (TRUNC_DIV_EXPR);
10588       break;
10589
10590     case CPP_MOD_EQ:
10591       id = ansi_assopname (TRUNC_MOD_EXPR);
10592       break;
10593
10594     case CPP_XOR_EQ:
10595       id = ansi_assopname (BIT_XOR_EXPR);
10596       break;
10597
10598     case CPP_AND_EQ:
10599       id = ansi_assopname (BIT_AND_EXPR);
10600       break;
10601
10602     case CPP_OR_EQ:
10603       id = ansi_assopname (BIT_IOR_EXPR);
10604       break;
10605
10606     case CPP_LSHIFT:
10607       id = ansi_opname (LSHIFT_EXPR);
10608       break;
10609
10610     case CPP_RSHIFT:
10611       id = ansi_opname (RSHIFT_EXPR);
10612       break;
10613
10614     case CPP_LSHIFT_EQ:
10615       id = ansi_assopname (LSHIFT_EXPR);
10616       break;
10617
10618     case CPP_RSHIFT_EQ:
10619       id = ansi_assopname (RSHIFT_EXPR);
10620       break;
10621
10622     case CPP_EQ_EQ:
10623       id = ansi_opname (EQ_EXPR);
10624       break;
10625
10626     case CPP_NOT_EQ:
10627       id = ansi_opname (NE_EXPR);
10628       break;
10629
10630     case CPP_LESS_EQ:
10631       id = ansi_opname (LE_EXPR);
10632       break;
10633
10634     case CPP_GREATER_EQ:
10635       id = ansi_opname (GE_EXPR);
10636       break;
10637
10638     case CPP_AND_AND:
10639       id = ansi_opname (TRUTH_ANDIF_EXPR);
10640       break;
10641
10642     case CPP_OR_OR:
10643       id = ansi_opname (TRUTH_ORIF_EXPR);
10644       break;
10645
10646     case CPP_PLUS_PLUS:
10647       id = ansi_opname (POSTINCREMENT_EXPR);
10648       break;
10649
10650     case CPP_MINUS_MINUS:
10651       id = ansi_opname (PREDECREMENT_EXPR);
10652       break;
10653
10654     case CPP_COMMA:
10655       id = ansi_opname (COMPOUND_EXPR);
10656       break;
10657
10658     case CPP_DEREF_STAR:
10659       id = ansi_opname (MEMBER_REF);
10660       break;
10661
10662     case CPP_DEREF:
10663       id = ansi_opname (COMPONENT_REF);
10664       break;
10665
10666     case CPP_OPEN_PAREN:
10667       /* Consume the `('.  */
10668       cp_lexer_consume_token (parser->lexer);
10669       /* Look for the matching `)'.  */
10670       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10671       return ansi_opname (CALL_EXPR);
10672
10673     case CPP_OPEN_SQUARE:
10674       /* Consume the `['.  */
10675       cp_lexer_consume_token (parser->lexer);
10676       /* Look for the matching `]'.  */
10677       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10678       return ansi_opname (ARRAY_REF);
10679
10680     default:
10681       /* Anything else is an error.  */
10682       break;
10683     }
10684
10685   /* If we have selected an identifier, we need to consume the
10686      operator token.  */
10687   if (id)
10688     cp_lexer_consume_token (parser->lexer);
10689   /* Otherwise, no valid operator name was present.  */
10690   else
10691     {
10692       cp_parser_error (parser, "expected operator");
10693       id = error_mark_node;
10694     }
10695
10696   return id;
10697 }
10698
10699 /* Parse a template-declaration.
10700
10701    template-declaration:
10702      export [opt] template < template-parameter-list > declaration
10703
10704    If MEMBER_P is TRUE, this template-declaration occurs within a
10705    class-specifier.
10706
10707    The grammar rule given by the standard isn't correct.  What
10708    is really meant is:
10709
10710    template-declaration:
10711      export [opt] template-parameter-list-seq
10712        decl-specifier-seq [opt] init-declarator [opt] ;
10713      export [opt] template-parameter-list-seq
10714        function-definition
10715
10716    template-parameter-list-seq:
10717      template-parameter-list-seq [opt]
10718      template < template-parameter-list >  */
10719
10720 static void
10721 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10722 {
10723   /* Check for `export'.  */
10724   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10725     {
10726       /* Consume the `export' token.  */
10727       cp_lexer_consume_token (parser->lexer);
10728       /* Warn that we do not support `export'.  */
10729       warning (0, "keyword %<export%> not implemented, and will be ignored");
10730     }
10731
10732   cp_parser_template_declaration_after_export (parser, member_p);
10733 }
10734
10735 /* Parse a template-parameter-list.
10736
10737    template-parameter-list:
10738      template-parameter
10739      template-parameter-list , template-parameter
10740
10741    Returns a TREE_LIST.  Each node represents a template parameter.
10742    The nodes are connected via their TREE_CHAINs.  */
10743
10744 static tree
10745 cp_parser_template_parameter_list (cp_parser* parser)
10746 {
10747   tree parameter_list = NULL_TREE;
10748
10749   begin_template_parm_list ();
10750   while (true)
10751     {
10752       tree parameter;
10753       bool is_non_type;
10754       bool is_parameter_pack;
10755       location_t parm_loc;
10756
10757       /* Parse the template-parameter.  */
10758       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10759       parameter = cp_parser_template_parameter (parser, 
10760                                                 &is_non_type,
10761                                                 &is_parameter_pack);
10762       /* Add it to the list.  */
10763       if (parameter != error_mark_node)
10764         parameter_list = process_template_parm (parameter_list,
10765                                                 parm_loc,
10766                                                 parameter,
10767                                                 is_non_type,
10768                                                 is_parameter_pack);
10769       else
10770        {
10771          tree err_parm = build_tree_list (parameter, parameter);
10772          TREE_VALUE (err_parm) = error_mark_node;
10773          parameter_list = chainon (parameter_list, err_parm);
10774        }
10775
10776       /* If the next token is not a `,', we're done.  */
10777       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10778         break;
10779       /* Otherwise, consume the `,' token.  */
10780       cp_lexer_consume_token (parser->lexer);
10781     }
10782
10783   return end_template_parm_list (parameter_list);
10784 }
10785
10786 /* Parse a template-parameter.
10787
10788    template-parameter:
10789      type-parameter
10790      parameter-declaration
10791
10792    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10793    the parameter.  The TREE_PURPOSE is the default value, if any.
10794    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10795    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10796    set to true iff this parameter is a parameter pack. */
10797
10798 static tree
10799 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10800                               bool *is_parameter_pack)
10801 {
10802   cp_token *token;
10803   cp_parameter_declarator *parameter_declarator;
10804   cp_declarator *id_declarator;
10805   tree parm;
10806
10807   /* Assume it is a type parameter or a template parameter.  */
10808   *is_non_type = false;
10809   /* Assume it not a parameter pack. */
10810   *is_parameter_pack = false;
10811   /* Peek at the next token.  */
10812   token = cp_lexer_peek_token (parser->lexer);
10813   /* If it is `class' or `template', we have a type-parameter.  */
10814   if (token->keyword == RID_TEMPLATE)
10815     return cp_parser_type_parameter (parser, is_parameter_pack);
10816   /* If it is `class' or `typename' we do not know yet whether it is a
10817      type parameter or a non-type parameter.  Consider:
10818
10819        template <typename T, typename T::X X> ...
10820
10821      or:
10822
10823        template <class C, class D*> ...
10824
10825      Here, the first parameter is a type parameter, and the second is
10826      a non-type parameter.  We can tell by looking at the token after
10827      the identifier -- if it is a `,', `=', or `>' then we have a type
10828      parameter.  */
10829   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10830     {
10831       /* Peek at the token after `class' or `typename'.  */
10832       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10833       /* If it's an ellipsis, we have a template type parameter
10834          pack. */
10835       if (token->type == CPP_ELLIPSIS)
10836         return cp_parser_type_parameter (parser, is_parameter_pack);
10837       /* If it's an identifier, skip it.  */
10838       if (token->type == CPP_NAME)
10839         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10840       /* Now, see if the token looks like the end of a template
10841          parameter.  */
10842       if (token->type == CPP_COMMA
10843           || token->type == CPP_EQ
10844           || token->type == CPP_GREATER)
10845         return cp_parser_type_parameter (parser, is_parameter_pack);
10846     }
10847
10848   /* Otherwise, it is a non-type parameter.
10849
10850      [temp.param]
10851
10852      When parsing a default template-argument for a non-type
10853      template-parameter, the first non-nested `>' is taken as the end
10854      of the template parameter-list rather than a greater-than
10855      operator.  */
10856   *is_non_type = true;
10857   parameter_declarator
10858      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10859                                         /*parenthesized_p=*/NULL);
10860
10861   /* If the parameter declaration is marked as a parameter pack, set
10862      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10863      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10864      grokdeclarator. */
10865   if (parameter_declarator
10866       && parameter_declarator->declarator
10867       && parameter_declarator->declarator->parameter_pack_p)
10868     {
10869       *is_parameter_pack = true;
10870       parameter_declarator->declarator->parameter_pack_p = false;
10871     }
10872
10873   /* If the next token is an ellipsis, and we don't already have it
10874      marked as a parameter pack, then we have a parameter pack (that
10875      has no declarator).  */
10876   if (!*is_parameter_pack
10877       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10878       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10879     {
10880       /* Consume the `...'.  */
10881       cp_lexer_consume_token (parser->lexer);
10882       maybe_warn_variadic_templates ();
10883       
10884       *is_parameter_pack = true;
10885     }
10886   /* We might end up with a pack expansion as the type of the non-type
10887      template parameter, in which case this is a non-type template
10888      parameter pack.  */
10889   else if (parameter_declarator
10890            && parameter_declarator->decl_specifiers.type
10891            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10892     {
10893       *is_parameter_pack = true;
10894       parameter_declarator->decl_specifiers.type = 
10895         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10896     }
10897
10898   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10899     {
10900       /* Parameter packs cannot have default arguments.  However, a
10901          user may try to do so, so we'll parse them and give an
10902          appropriate diagnostic here.  */
10903
10904       /* Consume the `='.  */
10905       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10906       cp_lexer_consume_token (parser->lexer);
10907       
10908       /* Find the name of the parameter pack.  */     
10909       id_declarator = parameter_declarator->declarator;
10910       while (id_declarator && id_declarator->kind != cdk_id)
10911         id_declarator = id_declarator->declarator;
10912       
10913       if (id_declarator && id_declarator->kind == cdk_id)
10914         error_at (start_token->location,
10915                   "template parameter pack %qD cannot have a default argument",
10916                   id_declarator->u.id.unqualified_name);
10917       else
10918         error_at (start_token->location,
10919                   "template parameter pack cannot have a default argument");
10920       
10921       /* Parse the default argument, but throw away the result.  */
10922       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10923     }
10924
10925   parm = grokdeclarator (parameter_declarator->declarator,
10926                          &parameter_declarator->decl_specifiers,
10927                          TPARM, /*initialized=*/0,
10928                          /*attrlist=*/NULL);
10929   if (parm == error_mark_node)
10930     return error_mark_node;
10931
10932   return build_tree_list (parameter_declarator->default_argument, parm);
10933 }
10934
10935 /* Parse a type-parameter.
10936
10937    type-parameter:
10938      class identifier [opt]
10939      class identifier [opt] = type-id
10940      typename identifier [opt]
10941      typename identifier [opt] = type-id
10942      template < template-parameter-list > class identifier [opt]
10943      template < template-parameter-list > class identifier [opt]
10944        = id-expression
10945
10946    GNU Extension (variadic templates):
10947
10948    type-parameter:
10949      class ... identifier [opt]
10950      typename ... identifier [opt]
10951
10952    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10953    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10954    the declaration of the parameter.
10955
10956    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10957
10958 static tree
10959 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10960 {
10961   cp_token *token;
10962   tree parameter;
10963
10964   /* Look for a keyword to tell us what kind of parameter this is.  */
10965   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
10966   if (!token)
10967     return error_mark_node;
10968
10969   switch (token->keyword)
10970     {
10971     case RID_CLASS:
10972     case RID_TYPENAME:
10973       {
10974         tree identifier;
10975         tree default_argument;
10976
10977         /* If the next token is an ellipsis, we have a template
10978            argument pack. */
10979         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10980           {
10981             /* Consume the `...' token. */
10982             cp_lexer_consume_token (parser->lexer);
10983             maybe_warn_variadic_templates ();
10984
10985             *is_parameter_pack = true;
10986           }
10987
10988         /* If the next token is an identifier, then it names the
10989            parameter.  */
10990         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10991           identifier = cp_parser_identifier (parser);
10992         else
10993           identifier = NULL_TREE;
10994
10995         /* Create the parameter.  */
10996         parameter = finish_template_type_parm (class_type_node, identifier);
10997
10998         /* If the next token is an `=', we have a default argument.  */
10999         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11000           {
11001             /* Consume the `=' token.  */
11002             cp_lexer_consume_token (parser->lexer);
11003             /* Parse the default-argument.  */
11004             push_deferring_access_checks (dk_no_deferred);
11005             default_argument = cp_parser_type_id (parser);
11006
11007             /* Template parameter packs cannot have default
11008                arguments. */
11009             if (*is_parameter_pack)
11010               {
11011                 if (identifier)
11012                   error_at (token->location,
11013                             "template parameter pack %qD cannot have a "
11014                             "default argument", identifier);
11015                 else
11016                   error_at (token->location,
11017                             "template parameter packs cannot have "
11018                             "default arguments");
11019                 default_argument = NULL_TREE;
11020               }
11021             pop_deferring_access_checks ();
11022           }
11023         else
11024           default_argument = NULL_TREE;
11025
11026         /* Create the combined representation of the parameter and the
11027            default argument.  */
11028         parameter = build_tree_list (default_argument, parameter);
11029       }
11030       break;
11031
11032     case RID_TEMPLATE:
11033       {
11034         tree identifier;
11035         tree default_argument;
11036
11037         /* Look for the `<'.  */
11038         cp_parser_require (parser, CPP_LESS, RT_LESS);
11039         /* Parse the template-parameter-list.  */
11040         cp_parser_template_parameter_list (parser);
11041         /* Look for the `>'.  */
11042         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11043         /* Look for the `class' keyword.  */
11044         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11045         /* If the next token is an ellipsis, we have a template
11046            argument pack. */
11047         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11048           {
11049             /* Consume the `...' token. */
11050             cp_lexer_consume_token (parser->lexer);
11051             maybe_warn_variadic_templates ();
11052
11053             *is_parameter_pack = true;
11054           }
11055         /* If the next token is an `=', then there is a
11056            default-argument.  If the next token is a `>', we are at
11057            the end of the parameter-list.  If the next token is a `,',
11058            then we are at the end of this parameter.  */
11059         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11060             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11061             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11062           {
11063             identifier = cp_parser_identifier (parser);
11064             /* Treat invalid names as if the parameter were nameless.  */
11065             if (identifier == error_mark_node)
11066               identifier = NULL_TREE;
11067           }
11068         else
11069           identifier = NULL_TREE;
11070
11071         /* Create the template parameter.  */
11072         parameter = finish_template_template_parm (class_type_node,
11073                                                    identifier);
11074
11075         /* If the next token is an `=', then there is a
11076            default-argument.  */
11077         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11078           {
11079             bool is_template;
11080
11081             /* Consume the `='.  */
11082             cp_lexer_consume_token (parser->lexer);
11083             /* Parse the id-expression.  */
11084             push_deferring_access_checks (dk_no_deferred);
11085             /* save token before parsing the id-expression, for error
11086                reporting */
11087             token = cp_lexer_peek_token (parser->lexer);
11088             default_argument
11089               = cp_parser_id_expression (parser,
11090                                          /*template_keyword_p=*/false,
11091                                          /*check_dependency_p=*/true,
11092                                          /*template_p=*/&is_template,
11093                                          /*declarator_p=*/false,
11094                                          /*optional_p=*/false);
11095             if (TREE_CODE (default_argument) == TYPE_DECL)
11096               /* If the id-expression was a template-id that refers to
11097                  a template-class, we already have the declaration here,
11098                  so no further lookup is needed.  */
11099                  ;
11100             else
11101               /* Look up the name.  */
11102               default_argument
11103                 = cp_parser_lookup_name (parser, default_argument,
11104                                          none_type,
11105                                          /*is_template=*/is_template,
11106                                          /*is_namespace=*/false,
11107                                          /*check_dependency=*/true,
11108                                          /*ambiguous_decls=*/NULL,
11109                                          token->location);
11110             /* See if the default argument is valid.  */
11111             default_argument
11112               = check_template_template_default_arg (default_argument);
11113
11114             /* Template parameter packs cannot have default
11115                arguments. */
11116             if (*is_parameter_pack)
11117               {
11118                 if (identifier)
11119                   error_at (token->location,
11120                             "template parameter pack %qD cannot "
11121                             "have a default argument",
11122                             identifier);
11123                 else
11124                   error_at (token->location, "template parameter packs cannot "
11125                             "have default arguments");
11126                 default_argument = NULL_TREE;
11127               }
11128             pop_deferring_access_checks ();
11129           }
11130         else
11131           default_argument = NULL_TREE;
11132
11133         /* Create the combined representation of the parameter and the
11134            default argument.  */
11135         parameter = build_tree_list (default_argument, parameter);
11136       }
11137       break;
11138
11139     default:
11140       gcc_unreachable ();
11141       break;
11142     }
11143
11144   return parameter;
11145 }
11146
11147 /* Parse a template-id.
11148
11149    template-id:
11150      template-name < template-argument-list [opt] >
11151
11152    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11153    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11154    returned.  Otherwise, if the template-name names a function, or set
11155    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11156    names a class, returns a TYPE_DECL for the specialization.
11157
11158    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11159    uninstantiated templates.  */
11160
11161 static tree
11162 cp_parser_template_id (cp_parser *parser,
11163                        bool template_keyword_p,
11164                        bool check_dependency_p,
11165                        bool is_declaration)
11166 {
11167   int i;
11168   tree templ;
11169   tree arguments;
11170   tree template_id;
11171   cp_token_position start_of_id = 0;
11172   deferred_access_check *chk;
11173   VEC (deferred_access_check,gc) *access_check;
11174   cp_token *next_token = NULL, *next_token_2 = NULL;
11175   bool is_identifier;
11176
11177   /* If the next token corresponds to a template-id, there is no need
11178      to reparse it.  */
11179   next_token = cp_lexer_peek_token (parser->lexer);
11180   if (next_token->type == CPP_TEMPLATE_ID)
11181     {
11182       struct tree_check *check_value;
11183
11184       /* Get the stored value.  */
11185       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11186       /* Perform any access checks that were deferred.  */
11187       access_check = check_value->checks;
11188       if (access_check)
11189         {
11190           for (i = 0 ;
11191                VEC_iterate (deferred_access_check, access_check, i, chk) ;
11192                ++i)
11193             {
11194               perform_or_defer_access_check (chk->binfo,
11195                                              chk->decl,
11196                                              chk->diag_decl);
11197             }
11198         }
11199       /* Return the stored value.  */
11200       return check_value->value;
11201     }
11202
11203   /* Avoid performing name lookup if there is no possibility of
11204      finding a template-id.  */
11205   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11206       || (next_token->type == CPP_NAME
11207           && !cp_parser_nth_token_starts_template_argument_list_p
11208                (parser, 2)))
11209     {
11210       cp_parser_error (parser, "expected template-id");
11211       return error_mark_node;
11212     }
11213
11214   /* Remember where the template-id starts.  */
11215   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11216     start_of_id = cp_lexer_token_position (parser->lexer, false);
11217
11218   push_deferring_access_checks (dk_deferred);
11219
11220   /* Parse the template-name.  */
11221   is_identifier = false;
11222   templ = cp_parser_template_name (parser, template_keyword_p,
11223                                    check_dependency_p,
11224                                    is_declaration,
11225                                    &is_identifier);
11226   if (templ == error_mark_node || is_identifier)
11227     {
11228       pop_deferring_access_checks ();
11229       return templ;
11230     }
11231
11232   /* If we find the sequence `[:' after a template-name, it's probably
11233      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11234      parse correctly the argument list.  */
11235   next_token = cp_lexer_peek_token (parser->lexer);
11236   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11237   if (next_token->type == CPP_OPEN_SQUARE
11238       && next_token->flags & DIGRAPH
11239       && next_token_2->type == CPP_COLON
11240       && !(next_token_2->flags & PREV_WHITE))
11241     {
11242       cp_parser_parse_tentatively (parser);
11243       /* Change `:' into `::'.  */
11244       next_token_2->type = CPP_SCOPE;
11245       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11246          CPP_LESS.  */
11247       cp_lexer_consume_token (parser->lexer);
11248
11249       /* Parse the arguments.  */
11250       arguments = cp_parser_enclosed_template_argument_list (parser);
11251       if (!cp_parser_parse_definitely (parser))
11252         {
11253           /* If we couldn't parse an argument list, then we revert our changes
11254              and return simply an error. Maybe this is not a template-id
11255              after all.  */
11256           next_token_2->type = CPP_COLON;
11257           cp_parser_error (parser, "expected %<<%>");
11258           pop_deferring_access_checks ();
11259           return error_mark_node;
11260         }
11261       /* Otherwise, emit an error about the invalid digraph, but continue
11262          parsing because we got our argument list.  */
11263       if (permerror (next_token->location,
11264                      "%<<::%> cannot begin a template-argument list"))
11265         {
11266           static bool hint = false;
11267           inform (next_token->location,
11268                   "%<<:%> is an alternate spelling for %<[%>."
11269                   " Insert whitespace between %<<%> and %<::%>");
11270           if (!hint && !flag_permissive)
11271             {
11272               inform (next_token->location, "(if you use %<-fpermissive%>"
11273                       " G++ will accept your code)");
11274               hint = true;
11275             }
11276         }
11277     }
11278   else
11279     {
11280       /* Look for the `<' that starts the template-argument-list.  */
11281       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11282         {
11283           pop_deferring_access_checks ();
11284           return error_mark_node;
11285         }
11286       /* Parse the arguments.  */
11287       arguments = cp_parser_enclosed_template_argument_list (parser);
11288     }
11289
11290   /* Build a representation of the specialization.  */
11291   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11292     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11293   else if (DECL_CLASS_TEMPLATE_P (templ)
11294            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11295     {
11296       bool entering_scope;
11297       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11298          template (rather than some instantiation thereof) only if
11299          is not nested within some other construct.  For example, in
11300          "template <typename T> void f(T) { A<T>::", A<T> is just an
11301          instantiation of A.  */
11302       entering_scope = (template_parm_scope_p ()
11303                         && cp_lexer_next_token_is (parser->lexer,
11304                                                    CPP_SCOPE));
11305       template_id
11306         = finish_template_type (templ, arguments, entering_scope);
11307     }
11308   else
11309     {
11310       /* If it's not a class-template or a template-template, it should be
11311          a function-template.  */
11312       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11313                    || TREE_CODE (templ) == OVERLOAD
11314                    || BASELINK_P (templ)));
11315
11316       template_id = lookup_template_function (templ, arguments);
11317     }
11318
11319   /* If parsing tentatively, replace the sequence of tokens that makes
11320      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11321      should we re-parse the token stream, we will not have to repeat
11322      the effort required to do the parse, nor will we issue duplicate
11323      error messages about problems during instantiation of the
11324      template.  */
11325   if (start_of_id)
11326     {
11327       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11328
11329       /* Reset the contents of the START_OF_ID token.  */
11330       token->type = CPP_TEMPLATE_ID;
11331       /* Retrieve any deferred checks.  Do not pop this access checks yet
11332          so the memory will not be reclaimed during token replacing below.  */
11333       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11334       token->u.tree_check_value->value = template_id;
11335       token->u.tree_check_value->checks = get_deferred_access_checks ();
11336       token->keyword = RID_MAX;
11337
11338       /* Purge all subsequent tokens.  */
11339       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11340
11341       /* ??? Can we actually assume that, if template_id ==
11342          error_mark_node, we will have issued a diagnostic to the
11343          user, as opposed to simply marking the tentative parse as
11344          failed?  */
11345       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11346         error_at (token->location, "parse error in template argument list");
11347     }
11348
11349   pop_deferring_access_checks ();
11350   return template_id;
11351 }
11352
11353 /* Parse a template-name.
11354
11355    template-name:
11356      identifier
11357
11358    The standard should actually say:
11359
11360    template-name:
11361      identifier
11362      operator-function-id
11363
11364    A defect report has been filed about this issue.
11365
11366    A conversion-function-id cannot be a template name because they cannot
11367    be part of a template-id. In fact, looking at this code:
11368
11369    a.operator K<int>()
11370
11371    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11372    It is impossible to call a templated conversion-function-id with an
11373    explicit argument list, since the only allowed template parameter is
11374    the type to which it is converting.
11375
11376    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11377    `template' keyword, in a construction like:
11378
11379      T::template f<3>()
11380
11381    In that case `f' is taken to be a template-name, even though there
11382    is no way of knowing for sure.
11383
11384    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11385    name refers to a set of overloaded functions, at least one of which
11386    is a template, or an IDENTIFIER_NODE with the name of the template,
11387    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11388    names are looked up inside uninstantiated templates.  */
11389
11390 static tree
11391 cp_parser_template_name (cp_parser* parser,
11392                          bool template_keyword_p,
11393                          bool check_dependency_p,
11394                          bool is_declaration,
11395                          bool *is_identifier)
11396 {
11397   tree identifier;
11398   tree decl;
11399   tree fns;
11400   cp_token *token = cp_lexer_peek_token (parser->lexer);
11401
11402   /* If the next token is `operator', then we have either an
11403      operator-function-id or a conversion-function-id.  */
11404   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11405     {
11406       /* We don't know whether we're looking at an
11407          operator-function-id or a conversion-function-id.  */
11408       cp_parser_parse_tentatively (parser);
11409       /* Try an operator-function-id.  */
11410       identifier = cp_parser_operator_function_id (parser);
11411       /* If that didn't work, try a conversion-function-id.  */
11412       if (!cp_parser_parse_definitely (parser))
11413         {
11414           cp_parser_error (parser, "expected template-name");
11415           return error_mark_node;
11416         }
11417     }
11418   /* Look for the identifier.  */
11419   else
11420     identifier = cp_parser_identifier (parser);
11421
11422   /* If we didn't find an identifier, we don't have a template-id.  */
11423   if (identifier == error_mark_node)
11424     return error_mark_node;
11425
11426   /* If the name immediately followed the `template' keyword, then it
11427      is a template-name.  However, if the next token is not `<', then
11428      we do not treat it as a template-name, since it is not being used
11429      as part of a template-id.  This enables us to handle constructs
11430      like:
11431
11432        template <typename T> struct S { S(); };
11433        template <typename T> S<T>::S();
11434
11435      correctly.  We would treat `S' as a template -- if it were `S<T>'
11436      -- but we do not if there is no `<'.  */
11437
11438   if (processing_template_decl
11439       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11440     {
11441       /* In a declaration, in a dependent context, we pretend that the
11442          "template" keyword was present in order to improve error
11443          recovery.  For example, given:
11444
11445            template <typename T> void f(T::X<int>);
11446
11447          we want to treat "X<int>" as a template-id.  */
11448       if (is_declaration
11449           && !template_keyword_p
11450           && parser->scope && TYPE_P (parser->scope)
11451           && check_dependency_p
11452           && dependent_scope_p (parser->scope)
11453           /* Do not do this for dtors (or ctors), since they never
11454              need the template keyword before their name.  */
11455           && !constructor_name_p (identifier, parser->scope))
11456         {
11457           cp_token_position start = 0;
11458
11459           /* Explain what went wrong.  */
11460           error_at (token->location, "non-template %qD used as template",
11461                     identifier);
11462           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11463                   parser->scope, identifier);
11464           /* If parsing tentatively, find the location of the "<" token.  */
11465           if (cp_parser_simulate_error (parser))
11466             start = cp_lexer_token_position (parser->lexer, true);
11467           /* Parse the template arguments so that we can issue error
11468              messages about them.  */
11469           cp_lexer_consume_token (parser->lexer);
11470           cp_parser_enclosed_template_argument_list (parser);
11471           /* Skip tokens until we find a good place from which to
11472              continue parsing.  */
11473           cp_parser_skip_to_closing_parenthesis (parser,
11474                                                  /*recovering=*/true,
11475                                                  /*or_comma=*/true,
11476                                                  /*consume_paren=*/false);
11477           /* If parsing tentatively, permanently remove the
11478              template argument list.  That will prevent duplicate
11479              error messages from being issued about the missing
11480              "template" keyword.  */
11481           if (start)
11482             cp_lexer_purge_tokens_after (parser->lexer, start);
11483           if (is_identifier)
11484             *is_identifier = true;
11485           return identifier;
11486         }
11487
11488       /* If the "template" keyword is present, then there is generally
11489          no point in doing name-lookup, so we just return IDENTIFIER.
11490          But, if the qualifying scope is non-dependent then we can
11491          (and must) do name-lookup normally.  */
11492       if (template_keyword_p
11493           && (!parser->scope
11494               || (TYPE_P (parser->scope)
11495                   && dependent_type_p (parser->scope))))
11496         return identifier;
11497     }
11498
11499   /* Look up the name.  */
11500   decl = cp_parser_lookup_name (parser, identifier,
11501                                 none_type,
11502                                 /*is_template=*/true,
11503                                 /*is_namespace=*/false,
11504                                 check_dependency_p,
11505                                 /*ambiguous_decls=*/NULL,
11506                                 token->location);
11507
11508   /* If DECL is a template, then the name was a template-name.  */
11509   if (TREE_CODE (decl) == TEMPLATE_DECL)
11510     ;
11511   else
11512     {
11513       tree fn = NULL_TREE;
11514
11515       /* The standard does not explicitly indicate whether a name that
11516          names a set of overloaded declarations, some of which are
11517          templates, is a template-name.  However, such a name should
11518          be a template-name; otherwise, there is no way to form a
11519          template-id for the overloaded templates.  */
11520       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11521       if (TREE_CODE (fns) == OVERLOAD)
11522         for (fn = fns; fn; fn = OVL_NEXT (fn))
11523           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11524             break;
11525
11526       if (!fn)
11527         {
11528           /* The name does not name a template.  */
11529           cp_parser_error (parser, "expected template-name");
11530           return error_mark_node;
11531         }
11532     }
11533
11534   /* If DECL is dependent, and refers to a function, then just return
11535      its name; we will look it up again during template instantiation.  */
11536   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11537     {
11538       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11539       if (TYPE_P (scope) && dependent_type_p (scope))
11540         return identifier;
11541     }
11542
11543   return decl;
11544 }
11545
11546 /* Parse a template-argument-list.
11547
11548    template-argument-list:
11549      template-argument ... [opt]
11550      template-argument-list , template-argument ... [opt]
11551
11552    Returns a TREE_VEC containing the arguments.  */
11553
11554 static tree
11555 cp_parser_template_argument_list (cp_parser* parser)
11556 {
11557   tree fixed_args[10];
11558   unsigned n_args = 0;
11559   unsigned alloced = 10;
11560   tree *arg_ary = fixed_args;
11561   tree vec;
11562   bool saved_in_template_argument_list_p;
11563   bool saved_ice_p;
11564   bool saved_non_ice_p;
11565
11566   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11567   parser->in_template_argument_list_p = true;
11568   /* Even if the template-id appears in an integral
11569      constant-expression, the contents of the argument list do
11570      not.  */
11571   saved_ice_p = parser->integral_constant_expression_p;
11572   parser->integral_constant_expression_p = false;
11573   saved_non_ice_p = parser->non_integral_constant_expression_p;
11574   parser->non_integral_constant_expression_p = false;
11575   /* Parse the arguments.  */
11576   do
11577     {
11578       tree argument;
11579
11580       if (n_args)
11581         /* Consume the comma.  */
11582         cp_lexer_consume_token (parser->lexer);
11583
11584       /* Parse the template-argument.  */
11585       argument = cp_parser_template_argument (parser);
11586
11587       /* If the next token is an ellipsis, we're expanding a template
11588          argument pack. */
11589       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11590         {
11591           if (argument == error_mark_node)
11592             {
11593               cp_token *token = cp_lexer_peek_token (parser->lexer);
11594               error_at (token->location,
11595                         "expected parameter pack before %<...%>");
11596             }
11597           /* Consume the `...' token. */
11598           cp_lexer_consume_token (parser->lexer);
11599
11600           /* Make the argument into a TYPE_PACK_EXPANSION or
11601              EXPR_PACK_EXPANSION. */
11602           argument = make_pack_expansion (argument);
11603         }
11604
11605       if (n_args == alloced)
11606         {
11607           alloced *= 2;
11608
11609           if (arg_ary == fixed_args)
11610             {
11611               arg_ary = XNEWVEC (tree, alloced);
11612               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11613             }
11614           else
11615             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11616         }
11617       arg_ary[n_args++] = argument;
11618     }
11619   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11620
11621   vec = make_tree_vec (n_args);
11622
11623   while (n_args--)
11624     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11625
11626   if (arg_ary != fixed_args)
11627     free (arg_ary);
11628   parser->non_integral_constant_expression_p = saved_non_ice_p;
11629   parser->integral_constant_expression_p = saved_ice_p;
11630   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11631 #ifdef ENABLE_CHECKING
11632   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11633 #endif
11634   return vec;
11635 }
11636
11637 /* Parse a template-argument.
11638
11639    template-argument:
11640      assignment-expression
11641      type-id
11642      id-expression
11643
11644    The representation is that of an assignment-expression, type-id, or
11645    id-expression -- except that the qualified id-expression is
11646    evaluated, so that the value returned is either a DECL or an
11647    OVERLOAD.
11648
11649    Although the standard says "assignment-expression", it forbids
11650    throw-expressions or assignments in the template argument.
11651    Therefore, we use "conditional-expression" instead.  */
11652
11653 static tree
11654 cp_parser_template_argument (cp_parser* parser)
11655 {
11656   tree argument;
11657   bool template_p;
11658   bool address_p;
11659   bool maybe_type_id = false;
11660   cp_token *token = NULL, *argument_start_token = NULL;
11661   cp_id_kind idk;
11662
11663   /* There's really no way to know what we're looking at, so we just
11664      try each alternative in order.
11665
11666        [temp.arg]
11667
11668        In a template-argument, an ambiguity between a type-id and an
11669        expression is resolved to a type-id, regardless of the form of
11670        the corresponding template-parameter.
11671
11672      Therefore, we try a type-id first.  */
11673   cp_parser_parse_tentatively (parser);
11674   argument = cp_parser_template_type_arg (parser);
11675   /* If there was no error parsing the type-id but the next token is a
11676      '>>', our behavior depends on which dialect of C++ we're
11677      parsing. In C++98, we probably found a typo for '> >'. But there
11678      are type-id which are also valid expressions. For instance:
11679
11680      struct X { int operator >> (int); };
11681      template <int V> struct Foo {};
11682      Foo<X () >> 5> r;
11683
11684      Here 'X()' is a valid type-id of a function type, but the user just
11685      wanted to write the expression "X() >> 5". Thus, we remember that we
11686      found a valid type-id, but we still try to parse the argument as an
11687      expression to see what happens. 
11688
11689      In C++0x, the '>>' will be considered two separate '>'
11690      tokens.  */
11691   if (!cp_parser_error_occurred (parser)
11692       && cxx_dialect == cxx98
11693       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11694     {
11695       maybe_type_id = true;
11696       cp_parser_abort_tentative_parse (parser);
11697     }
11698   else
11699     {
11700       /* If the next token isn't a `,' or a `>', then this argument wasn't
11701       really finished. This means that the argument is not a valid
11702       type-id.  */
11703       if (!cp_parser_next_token_ends_template_argument_p (parser))
11704         cp_parser_error (parser, "expected template-argument");
11705       /* If that worked, we're done.  */
11706       if (cp_parser_parse_definitely (parser))
11707         return argument;
11708     }
11709   /* We're still not sure what the argument will be.  */
11710   cp_parser_parse_tentatively (parser);
11711   /* Try a template.  */
11712   argument_start_token = cp_lexer_peek_token (parser->lexer);
11713   argument = cp_parser_id_expression (parser,
11714                                       /*template_keyword_p=*/false,
11715                                       /*check_dependency_p=*/true,
11716                                       &template_p,
11717                                       /*declarator_p=*/false,
11718                                       /*optional_p=*/false);
11719   /* If the next token isn't a `,' or a `>', then this argument wasn't
11720      really finished.  */
11721   if (!cp_parser_next_token_ends_template_argument_p (parser))
11722     cp_parser_error (parser, "expected template-argument");
11723   if (!cp_parser_error_occurred (parser))
11724     {
11725       /* Figure out what is being referred to.  If the id-expression
11726          was for a class template specialization, then we will have a
11727          TYPE_DECL at this point.  There is no need to do name lookup
11728          at this point in that case.  */
11729       if (TREE_CODE (argument) != TYPE_DECL)
11730         argument = cp_parser_lookup_name (parser, argument,
11731                                           none_type,
11732                                           /*is_template=*/template_p,
11733                                           /*is_namespace=*/false,
11734                                           /*check_dependency=*/true,
11735                                           /*ambiguous_decls=*/NULL,
11736                                           argument_start_token->location);
11737       if (TREE_CODE (argument) != TEMPLATE_DECL
11738           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11739         cp_parser_error (parser, "expected template-name");
11740     }
11741   if (cp_parser_parse_definitely (parser))
11742     return argument;
11743   /* It must be a non-type argument.  There permitted cases are given
11744      in [temp.arg.nontype]:
11745
11746      -- an integral constant-expression of integral or enumeration
11747         type; or
11748
11749      -- the name of a non-type template-parameter; or
11750
11751      -- the name of an object or function with external linkage...
11752
11753      -- the address of an object or function with external linkage...
11754
11755      -- a pointer to member...  */
11756   /* Look for a non-type template parameter.  */
11757   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11758     {
11759       cp_parser_parse_tentatively (parser);
11760       argument = cp_parser_primary_expression (parser,
11761                                                /*address_p=*/false,
11762                                                /*cast_p=*/false,
11763                                                /*template_arg_p=*/true,
11764                                                &idk);
11765       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11766           || !cp_parser_next_token_ends_template_argument_p (parser))
11767         cp_parser_simulate_error (parser);
11768       if (cp_parser_parse_definitely (parser))
11769         return argument;
11770     }
11771
11772   /* If the next token is "&", the argument must be the address of an
11773      object or function with external linkage.  */
11774   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11775   if (address_p)
11776     cp_lexer_consume_token (parser->lexer);
11777   /* See if we might have an id-expression.  */
11778   token = cp_lexer_peek_token (parser->lexer);
11779   if (token->type == CPP_NAME
11780       || token->keyword == RID_OPERATOR
11781       || token->type == CPP_SCOPE
11782       || token->type == CPP_TEMPLATE_ID
11783       || token->type == CPP_NESTED_NAME_SPECIFIER)
11784     {
11785       cp_parser_parse_tentatively (parser);
11786       argument = cp_parser_primary_expression (parser,
11787                                                address_p,
11788                                                /*cast_p=*/false,
11789                                                /*template_arg_p=*/true,
11790                                                &idk);
11791       if (cp_parser_error_occurred (parser)
11792           || !cp_parser_next_token_ends_template_argument_p (parser))
11793         cp_parser_abort_tentative_parse (parser);
11794       else
11795         {
11796           tree probe;
11797
11798           if (TREE_CODE (argument) == INDIRECT_REF)
11799             {
11800               gcc_assert (REFERENCE_REF_P (argument));
11801               argument = TREE_OPERAND (argument, 0);
11802             }
11803
11804           /* If we're in a template, we represent a qualified-id referring
11805              to a static data member as a SCOPE_REF even if the scope isn't
11806              dependent so that we can check access control later.  */
11807           probe = argument;
11808           if (TREE_CODE (probe) == SCOPE_REF)
11809             probe = TREE_OPERAND (probe, 1);
11810           if (TREE_CODE (probe) == VAR_DECL)
11811             {
11812               /* A variable without external linkage might still be a
11813                  valid constant-expression, so no error is issued here
11814                  if the external-linkage check fails.  */
11815               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11816                 cp_parser_simulate_error (parser);
11817             }
11818           else if (is_overloaded_fn (argument))
11819             /* All overloaded functions are allowed; if the external
11820                linkage test does not pass, an error will be issued
11821                later.  */
11822             ;
11823           else if (address_p
11824                    && (TREE_CODE (argument) == OFFSET_REF
11825                        || TREE_CODE (argument) == SCOPE_REF))
11826             /* A pointer-to-member.  */
11827             ;
11828           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11829             ;
11830           else
11831             cp_parser_simulate_error (parser);
11832
11833           if (cp_parser_parse_definitely (parser))
11834             {
11835               if (address_p)
11836                 argument = build_x_unary_op (ADDR_EXPR, argument,
11837                                              tf_warning_or_error);
11838               return argument;
11839             }
11840         }
11841     }
11842   /* If the argument started with "&", there are no other valid
11843      alternatives at this point.  */
11844   if (address_p)
11845     {
11846       cp_parser_error (parser, "invalid non-type template argument");
11847       return error_mark_node;
11848     }
11849
11850   /* If the argument wasn't successfully parsed as a type-id followed
11851      by '>>', the argument can only be a constant expression now.
11852      Otherwise, we try parsing the constant-expression tentatively,
11853      because the argument could really be a type-id.  */
11854   if (maybe_type_id)
11855     cp_parser_parse_tentatively (parser);
11856   argument = cp_parser_constant_expression (parser,
11857                                             /*allow_non_constant_p=*/false,
11858                                             /*non_constant_p=*/NULL);
11859   argument = fold_non_dependent_expr (argument);
11860   if (!maybe_type_id)
11861     return argument;
11862   if (!cp_parser_next_token_ends_template_argument_p (parser))
11863     cp_parser_error (parser, "expected template-argument");
11864   if (cp_parser_parse_definitely (parser))
11865     return argument;
11866   /* We did our best to parse the argument as a non type-id, but that
11867      was the only alternative that matched (albeit with a '>' after
11868      it). We can assume it's just a typo from the user, and a
11869      diagnostic will then be issued.  */
11870   return cp_parser_template_type_arg (parser);
11871 }
11872
11873 /* Parse an explicit-instantiation.
11874
11875    explicit-instantiation:
11876      template declaration
11877
11878    Although the standard says `declaration', what it really means is:
11879
11880    explicit-instantiation:
11881      template decl-specifier-seq [opt] declarator [opt] ;
11882
11883    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11884    supposed to be allowed.  A defect report has been filed about this
11885    issue.
11886
11887    GNU Extension:
11888
11889    explicit-instantiation:
11890      storage-class-specifier template
11891        decl-specifier-seq [opt] declarator [opt] ;
11892      function-specifier template
11893        decl-specifier-seq [opt] declarator [opt] ;  */
11894
11895 static void
11896 cp_parser_explicit_instantiation (cp_parser* parser)
11897 {
11898   int declares_class_or_enum;
11899   cp_decl_specifier_seq decl_specifiers;
11900   tree extension_specifier = NULL_TREE;
11901
11902   /* Look for an (optional) storage-class-specifier or
11903      function-specifier.  */
11904   if (cp_parser_allow_gnu_extensions_p (parser))
11905     {
11906       extension_specifier
11907         = cp_parser_storage_class_specifier_opt (parser);
11908       if (!extension_specifier)
11909         extension_specifier
11910           = cp_parser_function_specifier_opt (parser,
11911                                               /*decl_specs=*/NULL);
11912     }
11913
11914   /* Look for the `template' keyword.  */
11915   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
11916   /* Let the front end know that we are processing an explicit
11917      instantiation.  */
11918   begin_explicit_instantiation ();
11919   /* [temp.explicit] says that we are supposed to ignore access
11920      control while processing explicit instantiation directives.  */
11921   push_deferring_access_checks (dk_no_check);
11922   /* Parse a decl-specifier-seq.  */
11923   cp_parser_decl_specifier_seq (parser,
11924                                 CP_PARSER_FLAGS_OPTIONAL,
11925                                 &decl_specifiers,
11926                                 &declares_class_or_enum);
11927   /* If there was exactly one decl-specifier, and it declared a class,
11928      and there's no declarator, then we have an explicit type
11929      instantiation.  */
11930   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11931     {
11932       tree type;
11933
11934       type = check_tag_decl (&decl_specifiers);
11935       /* Turn access control back on for names used during
11936          template instantiation.  */
11937       pop_deferring_access_checks ();
11938       if (type)
11939         do_type_instantiation (type, extension_specifier,
11940                                /*complain=*/tf_error);
11941     }
11942   else
11943     {
11944       cp_declarator *declarator;
11945       tree decl;
11946
11947       /* Parse the declarator.  */
11948       declarator
11949         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11950                                 /*ctor_dtor_or_conv_p=*/NULL,
11951                                 /*parenthesized_p=*/NULL,
11952                                 /*member_p=*/false);
11953       if (declares_class_or_enum & 2)
11954         cp_parser_check_for_definition_in_return_type (declarator,
11955                                                        decl_specifiers.type,
11956                                                        decl_specifiers.type_location);
11957       if (declarator != cp_error_declarator)
11958         {
11959           decl = grokdeclarator (declarator, &decl_specifiers,
11960                                  NORMAL, 0, &decl_specifiers.attributes);
11961           /* Turn access control back on for names used during
11962              template instantiation.  */
11963           pop_deferring_access_checks ();
11964           /* Do the explicit instantiation.  */
11965           do_decl_instantiation (decl, extension_specifier);
11966         }
11967       else
11968         {
11969           pop_deferring_access_checks ();
11970           /* Skip the body of the explicit instantiation.  */
11971           cp_parser_skip_to_end_of_statement (parser);
11972         }
11973     }
11974   /* We're done with the instantiation.  */
11975   end_explicit_instantiation ();
11976
11977   cp_parser_consume_semicolon_at_end_of_statement (parser);
11978 }
11979
11980 /* Parse an explicit-specialization.
11981
11982    explicit-specialization:
11983      template < > declaration
11984
11985    Although the standard says `declaration', what it really means is:
11986
11987    explicit-specialization:
11988      template <> decl-specifier [opt] init-declarator [opt] ;
11989      template <> function-definition
11990      template <> explicit-specialization
11991      template <> template-declaration  */
11992
11993 static void
11994 cp_parser_explicit_specialization (cp_parser* parser)
11995 {
11996   bool need_lang_pop;
11997   cp_token *token = cp_lexer_peek_token (parser->lexer);
11998
11999   /* Look for the `template' keyword.  */
12000   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12001   /* Look for the `<'.  */
12002   cp_parser_require (parser, CPP_LESS, RT_LESS);
12003   /* Look for the `>'.  */
12004   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12005   /* We have processed another parameter list.  */
12006   ++parser->num_template_parameter_lists;
12007   /* [temp]
12008
12009      A template ... explicit specialization ... shall not have C
12010      linkage.  */
12011   if (current_lang_name == lang_name_c)
12012     {
12013       error_at (token->location, "template specialization with C linkage");
12014       /* Give it C++ linkage to avoid confusing other parts of the
12015          front end.  */
12016       push_lang_context (lang_name_cplusplus);
12017       need_lang_pop = true;
12018     }
12019   else
12020     need_lang_pop = false;
12021   /* Let the front end know that we are beginning a specialization.  */
12022   if (!begin_specialization ())
12023     {
12024       end_specialization ();
12025       return;
12026     }
12027
12028   /* If the next keyword is `template', we need to figure out whether
12029      or not we're looking a template-declaration.  */
12030   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12031     {
12032       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12033           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12034         cp_parser_template_declaration_after_export (parser,
12035                                                      /*member_p=*/false);
12036       else
12037         cp_parser_explicit_specialization (parser);
12038     }
12039   else
12040     /* Parse the dependent declaration.  */
12041     cp_parser_single_declaration (parser,
12042                                   /*checks=*/NULL,
12043                                   /*member_p=*/false,
12044                                   /*explicit_specialization_p=*/true,
12045                                   /*friend_p=*/NULL);
12046   /* We're done with the specialization.  */
12047   end_specialization ();
12048   /* For the erroneous case of a template with C linkage, we pushed an
12049      implicit C++ linkage scope; exit that scope now.  */
12050   if (need_lang_pop)
12051     pop_lang_context ();
12052   /* We're done with this parameter list.  */
12053   --parser->num_template_parameter_lists;
12054 }
12055
12056 /* Parse a type-specifier.
12057
12058    type-specifier:
12059      simple-type-specifier
12060      class-specifier
12061      enum-specifier
12062      elaborated-type-specifier
12063      cv-qualifier
12064
12065    GNU Extension:
12066
12067    type-specifier:
12068      __complex__
12069
12070    Returns a representation of the type-specifier.  For a
12071    class-specifier, enum-specifier, or elaborated-type-specifier, a
12072    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12073
12074    The parser flags FLAGS is used to control type-specifier parsing.
12075
12076    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12077    in a decl-specifier-seq.
12078
12079    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12080    class-specifier, enum-specifier, or elaborated-type-specifier, then
12081    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12082    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12083    zero.
12084
12085    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12086    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12087    is set to FALSE.  */
12088
12089 static tree
12090 cp_parser_type_specifier (cp_parser* parser,
12091                           cp_parser_flags flags,
12092                           cp_decl_specifier_seq *decl_specs,
12093                           bool is_declaration,
12094                           int* declares_class_or_enum,
12095                           bool* is_cv_qualifier)
12096 {
12097   tree type_spec = NULL_TREE;
12098   cp_token *token;
12099   enum rid keyword;
12100   cp_decl_spec ds = ds_last;
12101
12102   /* Assume this type-specifier does not declare a new type.  */
12103   if (declares_class_or_enum)
12104     *declares_class_or_enum = 0;
12105   /* And that it does not specify a cv-qualifier.  */
12106   if (is_cv_qualifier)
12107     *is_cv_qualifier = false;
12108   /* Peek at the next token.  */
12109   token = cp_lexer_peek_token (parser->lexer);
12110
12111   /* If we're looking at a keyword, we can use that to guide the
12112      production we choose.  */
12113   keyword = token->keyword;
12114   switch (keyword)
12115     {
12116     case RID_ENUM:
12117       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12118         goto elaborated_type_specifier;
12119
12120       /* Look for the enum-specifier.  */
12121       type_spec = cp_parser_enum_specifier (parser);
12122       /* If that worked, we're done.  */
12123       if (type_spec)
12124         {
12125           if (declares_class_or_enum)
12126             *declares_class_or_enum = 2;
12127           if (decl_specs)
12128             cp_parser_set_decl_spec_type (decl_specs,
12129                                           type_spec,
12130                                           token->location,
12131                                           /*user_defined_p=*/true);
12132           return type_spec;
12133         }
12134       else
12135         goto elaborated_type_specifier;
12136
12137       /* Any of these indicate either a class-specifier, or an
12138          elaborated-type-specifier.  */
12139     case RID_CLASS:
12140     case RID_STRUCT:
12141     case RID_UNION:
12142       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12143         goto elaborated_type_specifier;
12144
12145       /* Parse tentatively so that we can back up if we don't find a
12146          class-specifier.  */
12147       cp_parser_parse_tentatively (parser);
12148       /* Look for the class-specifier.  */
12149       type_spec = cp_parser_class_specifier (parser);
12150       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12151       /* If that worked, we're done.  */
12152       if (cp_parser_parse_definitely (parser))
12153         {
12154           if (declares_class_or_enum)
12155             *declares_class_or_enum = 2;
12156           if (decl_specs)
12157             cp_parser_set_decl_spec_type (decl_specs,
12158                                           type_spec,
12159                                           token->location,
12160                                           /*user_defined_p=*/true);
12161           return type_spec;
12162         }
12163
12164       /* Fall through.  */
12165     elaborated_type_specifier:
12166       /* We're declaring (not defining) a class or enum.  */
12167       if (declares_class_or_enum)
12168         *declares_class_or_enum = 1;
12169
12170       /* Fall through.  */
12171     case RID_TYPENAME:
12172       /* Look for an elaborated-type-specifier.  */
12173       type_spec
12174         = (cp_parser_elaborated_type_specifier
12175            (parser,
12176             decl_specs && decl_specs->specs[(int) ds_friend],
12177             is_declaration));
12178       if (decl_specs)
12179         cp_parser_set_decl_spec_type (decl_specs,
12180                                       type_spec,
12181                                       token->location,
12182                                       /*user_defined_p=*/true);
12183       return type_spec;
12184
12185     case RID_CONST:
12186       ds = ds_const;
12187       if (is_cv_qualifier)
12188         *is_cv_qualifier = true;
12189       break;
12190
12191     case RID_VOLATILE:
12192       ds = ds_volatile;
12193       if (is_cv_qualifier)
12194         *is_cv_qualifier = true;
12195       break;
12196
12197     case RID_RESTRICT:
12198       ds = ds_restrict;
12199       if (is_cv_qualifier)
12200         *is_cv_qualifier = true;
12201       break;
12202
12203     case RID_COMPLEX:
12204       /* The `__complex__' keyword is a GNU extension.  */
12205       ds = ds_complex;
12206       break;
12207
12208     default:
12209       break;
12210     }
12211
12212   /* Handle simple keywords.  */
12213   if (ds != ds_last)
12214     {
12215       if (decl_specs)
12216         {
12217           ++decl_specs->specs[(int)ds];
12218           decl_specs->any_specifiers_p = true;
12219         }
12220       return cp_lexer_consume_token (parser->lexer)->u.value;
12221     }
12222
12223   /* If we do not already have a type-specifier, assume we are looking
12224      at a simple-type-specifier.  */
12225   type_spec = cp_parser_simple_type_specifier (parser,
12226                                                decl_specs,
12227                                                flags);
12228
12229   /* If we didn't find a type-specifier, and a type-specifier was not
12230      optional in this context, issue an error message.  */
12231   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12232     {
12233       cp_parser_error (parser, "expected type specifier");
12234       return error_mark_node;
12235     }
12236
12237   return type_spec;
12238 }
12239
12240 /* Parse a simple-type-specifier.
12241
12242    simple-type-specifier:
12243      :: [opt] nested-name-specifier [opt] type-name
12244      :: [opt] nested-name-specifier template template-id
12245      char
12246      wchar_t
12247      bool
12248      short
12249      int
12250      long
12251      signed
12252      unsigned
12253      float
12254      double
12255      void
12256
12257    C++0x Extension:
12258
12259    simple-type-specifier:
12260      auto
12261      decltype ( expression )   
12262      char16_t
12263      char32_t
12264
12265    GNU Extension:
12266
12267    simple-type-specifier:
12268      __int128
12269      __typeof__ unary-expression
12270      __typeof__ ( type-id )
12271
12272    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12273    appropriately updated.  */
12274
12275 static tree
12276 cp_parser_simple_type_specifier (cp_parser* parser,
12277                                  cp_decl_specifier_seq *decl_specs,
12278                                  cp_parser_flags flags)
12279 {
12280   tree type = NULL_TREE;
12281   cp_token *token;
12282
12283   /* Peek at the next token.  */
12284   token = cp_lexer_peek_token (parser->lexer);
12285
12286   /* If we're looking at a keyword, things are easy.  */
12287   switch (token->keyword)
12288     {
12289     case RID_CHAR:
12290       if (decl_specs)
12291         decl_specs->explicit_char_p = true;
12292       type = char_type_node;
12293       break;
12294     case RID_CHAR16:
12295       type = char16_type_node;
12296       break;
12297     case RID_CHAR32:
12298       type = char32_type_node;
12299       break;
12300     case RID_WCHAR:
12301       type = wchar_type_node;
12302       break;
12303     case RID_BOOL:
12304       type = boolean_type_node;
12305       break;
12306     case RID_SHORT:
12307       if (decl_specs)
12308         ++decl_specs->specs[(int) ds_short];
12309       type = short_integer_type_node;
12310       break;
12311     case RID_INT:
12312       if (decl_specs)
12313         decl_specs->explicit_int_p = true;
12314       type = integer_type_node;
12315       break;
12316     case RID_INT128:
12317       if (!int128_integer_type_node)
12318         break;
12319       if (decl_specs)
12320         decl_specs->explicit_int128_p = true;
12321       type = int128_integer_type_node;
12322       break;
12323     case RID_LONG:
12324       if (decl_specs)
12325         ++decl_specs->specs[(int) ds_long];
12326       type = long_integer_type_node;
12327       break;
12328     case RID_SIGNED:
12329       if (decl_specs)
12330         ++decl_specs->specs[(int) ds_signed];
12331       type = integer_type_node;
12332       break;
12333     case RID_UNSIGNED:
12334       if (decl_specs)
12335         ++decl_specs->specs[(int) ds_unsigned];
12336       type = unsigned_type_node;
12337       break;
12338     case RID_FLOAT:
12339       type = float_type_node;
12340       break;
12341     case RID_DOUBLE:
12342       type = double_type_node;
12343       break;
12344     case RID_VOID:
12345       type = void_type_node;
12346       break;
12347       
12348     case RID_AUTO:
12349       maybe_warn_cpp0x (CPP0X_AUTO);
12350       type = make_auto ();
12351       break;
12352
12353     case RID_DECLTYPE:
12354       /* Parse the `decltype' type.  */
12355       type = cp_parser_decltype (parser);
12356
12357       if (decl_specs)
12358         cp_parser_set_decl_spec_type (decl_specs, type,
12359                                       token->location,
12360                                       /*user_defined_p=*/true);
12361
12362       return type;
12363
12364     case RID_TYPEOF:
12365       /* Consume the `typeof' token.  */
12366       cp_lexer_consume_token (parser->lexer);
12367       /* Parse the operand to `typeof'.  */
12368       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12369       /* If it is not already a TYPE, take its type.  */
12370       if (!TYPE_P (type))
12371         type = finish_typeof (type);
12372
12373       if (decl_specs)
12374         cp_parser_set_decl_spec_type (decl_specs, type,
12375                                       token->location,
12376                                       /*user_defined_p=*/true);
12377
12378       return type;
12379
12380     default:
12381       break;
12382     }
12383
12384   /* If the type-specifier was for a built-in type, we're done.  */
12385   if (type)
12386     {
12387       /* Record the type.  */
12388       if (decl_specs
12389           && (token->keyword != RID_SIGNED
12390               && token->keyword != RID_UNSIGNED
12391               && token->keyword != RID_SHORT
12392               && token->keyword != RID_LONG))
12393         cp_parser_set_decl_spec_type (decl_specs,
12394                                       type,
12395                                       token->location,
12396                                       /*user_defined=*/false);
12397       if (decl_specs)
12398         decl_specs->any_specifiers_p = true;
12399
12400       /* Consume the token.  */
12401       cp_lexer_consume_token (parser->lexer);
12402
12403       /* There is no valid C++ program where a non-template type is
12404          followed by a "<".  That usually indicates that the user thought
12405          that the type was a template.  */
12406       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12407
12408       return TYPE_NAME (type);
12409     }
12410
12411   /* The type-specifier must be a user-defined type.  */
12412   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12413     {
12414       bool qualified_p;
12415       bool global_p;
12416
12417       /* Don't gobble tokens or issue error messages if this is an
12418          optional type-specifier.  */
12419       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12420         cp_parser_parse_tentatively (parser);
12421
12422       /* Look for the optional `::' operator.  */
12423       global_p
12424         = (cp_parser_global_scope_opt (parser,
12425                                        /*current_scope_valid_p=*/false)
12426            != NULL_TREE);
12427       /* Look for the nested-name specifier.  */
12428       qualified_p
12429         = (cp_parser_nested_name_specifier_opt (parser,
12430                                                 /*typename_keyword_p=*/false,
12431                                                 /*check_dependency_p=*/true,
12432                                                 /*type_p=*/false,
12433                                                 /*is_declaration=*/false)
12434            != NULL_TREE);
12435       token = cp_lexer_peek_token (parser->lexer);
12436       /* If we have seen a nested-name-specifier, and the next token
12437          is `template', then we are using the template-id production.  */
12438       if (parser->scope
12439           && cp_parser_optional_template_keyword (parser))
12440         {
12441           /* Look for the template-id.  */
12442           type = cp_parser_template_id (parser,
12443                                         /*template_keyword_p=*/true,
12444                                         /*check_dependency_p=*/true,
12445                                         /*is_declaration=*/false);
12446           /* If the template-id did not name a type, we are out of
12447              luck.  */
12448           if (TREE_CODE (type) != TYPE_DECL)
12449             {
12450               cp_parser_error (parser, "expected template-id for type");
12451               type = NULL_TREE;
12452             }
12453         }
12454       /* Otherwise, look for a type-name.  */
12455       else
12456         type = cp_parser_type_name (parser);
12457       /* Keep track of all name-lookups performed in class scopes.  */
12458       if (type
12459           && !global_p
12460           && !qualified_p
12461           && TREE_CODE (type) == TYPE_DECL
12462           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12463         maybe_note_name_used_in_class (DECL_NAME (type), type);
12464       /* If it didn't work out, we don't have a TYPE.  */
12465       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12466           && !cp_parser_parse_definitely (parser))
12467         type = NULL_TREE;
12468       if (type && decl_specs)
12469         cp_parser_set_decl_spec_type (decl_specs, type,
12470                                       token->location,
12471                                       /*user_defined=*/true);
12472     }
12473
12474   /* If we didn't get a type-name, issue an error message.  */
12475   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12476     {
12477       cp_parser_error (parser, "expected type-name");
12478       return error_mark_node;
12479     }
12480
12481   /* There is no valid C++ program where a non-template type is
12482      followed by a "<".  That usually indicates that the user thought
12483      that the type was a template.  */
12484   if (type && type != error_mark_node)
12485     {
12486       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12487          If it is, then the '<'...'>' enclose protocol names rather than
12488          template arguments, and so everything is fine.  */
12489       if (c_dialect_objc ()
12490           && (objc_is_id (type) || objc_is_class_name (type)))
12491         {
12492           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12493           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12494
12495           /* Clobber the "unqualified" type previously entered into
12496              DECL_SPECS with the new, improved protocol-qualified version.  */
12497           if (decl_specs)
12498             decl_specs->type = qual_type;
12499
12500           return qual_type;
12501         }
12502
12503       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12504                                                token->location);
12505     }
12506
12507   return type;
12508 }
12509
12510 /* Parse a type-name.
12511
12512    type-name:
12513      class-name
12514      enum-name
12515      typedef-name
12516
12517    enum-name:
12518      identifier
12519
12520    typedef-name:
12521      identifier
12522
12523    Returns a TYPE_DECL for the type.  */
12524
12525 static tree
12526 cp_parser_type_name (cp_parser* parser)
12527 {
12528   tree type_decl;
12529
12530   /* We can't know yet whether it is a class-name or not.  */
12531   cp_parser_parse_tentatively (parser);
12532   /* Try a class-name.  */
12533   type_decl = cp_parser_class_name (parser,
12534                                     /*typename_keyword_p=*/false,
12535                                     /*template_keyword_p=*/false,
12536                                     none_type,
12537                                     /*check_dependency_p=*/true,
12538                                     /*class_head_p=*/false,
12539                                     /*is_declaration=*/false);
12540   /* If it's not a class-name, keep looking.  */
12541   if (!cp_parser_parse_definitely (parser))
12542     {
12543       /* It must be a typedef-name or an enum-name.  */
12544       return cp_parser_nonclass_name (parser);
12545     }
12546
12547   return type_decl;
12548 }
12549
12550 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12551
12552    enum-name:
12553      identifier
12554
12555    typedef-name:
12556      identifier
12557
12558    Returns a TYPE_DECL for the type.  */
12559
12560 static tree
12561 cp_parser_nonclass_name (cp_parser* parser)
12562 {
12563   tree type_decl;
12564   tree identifier;
12565
12566   cp_token *token = cp_lexer_peek_token (parser->lexer);
12567   identifier = cp_parser_identifier (parser);
12568   if (identifier == error_mark_node)
12569     return error_mark_node;
12570
12571   /* Look up the type-name.  */
12572   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12573
12574   if (TREE_CODE (type_decl) != TYPE_DECL
12575       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12576     {
12577       /* See if this is an Objective-C type.  */
12578       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12579       tree type = objc_get_protocol_qualified_type (identifier, protos);
12580       if (type)
12581         type_decl = TYPE_NAME (type);
12582     }
12583   
12584   /* Issue an error if we did not find a type-name.  */
12585   if (TREE_CODE (type_decl) != TYPE_DECL)
12586     {
12587       if (!cp_parser_simulate_error (parser))
12588         cp_parser_name_lookup_error (parser, identifier, type_decl,
12589                                      NLE_TYPE, token->location);
12590       return error_mark_node;
12591     }
12592   /* Remember that the name was used in the definition of the
12593      current class so that we can check later to see if the
12594      meaning would have been different after the class was
12595      entirely defined.  */
12596   else if (type_decl != error_mark_node
12597            && !parser->scope)
12598     maybe_note_name_used_in_class (identifier, type_decl);
12599   
12600   return type_decl;
12601 }
12602
12603 /* Parse an elaborated-type-specifier.  Note that the grammar given
12604    here incorporates the resolution to DR68.
12605
12606    elaborated-type-specifier:
12607      class-key :: [opt] nested-name-specifier [opt] identifier
12608      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12609      enum-key :: [opt] nested-name-specifier [opt] identifier
12610      typename :: [opt] nested-name-specifier identifier
12611      typename :: [opt] nested-name-specifier template [opt]
12612        template-id
12613
12614    GNU extension:
12615
12616    elaborated-type-specifier:
12617      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12618      class-key attributes :: [opt] nested-name-specifier [opt]
12619                template [opt] template-id
12620      enum attributes :: [opt] nested-name-specifier [opt] identifier
12621
12622    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12623    declared `friend'.  If IS_DECLARATION is TRUE, then this
12624    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12625    something is being declared.
12626
12627    Returns the TYPE specified.  */
12628
12629 static tree
12630 cp_parser_elaborated_type_specifier (cp_parser* parser,
12631                                      bool is_friend,
12632                                      bool is_declaration)
12633 {
12634   enum tag_types tag_type;
12635   tree identifier;
12636   tree type = NULL_TREE;
12637   tree attributes = NULL_TREE;
12638   tree globalscope;
12639   cp_token *token = NULL;
12640
12641   /* See if we're looking at the `enum' keyword.  */
12642   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12643     {
12644       /* Consume the `enum' token.  */
12645       cp_lexer_consume_token (parser->lexer);
12646       /* Remember that it's an enumeration type.  */
12647       tag_type = enum_type;
12648       /* Parse the optional `struct' or `class' key (for C++0x scoped
12649          enums).  */
12650       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12651           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12652         {
12653           if (cxx_dialect == cxx98)
12654             maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12655
12656           /* Consume the `struct' or `class'.  */
12657           cp_lexer_consume_token (parser->lexer);
12658         }
12659       /* Parse the attributes.  */
12660       attributes = cp_parser_attributes_opt (parser);
12661     }
12662   /* Or, it might be `typename'.  */
12663   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12664                                            RID_TYPENAME))
12665     {
12666       /* Consume the `typename' token.  */
12667       cp_lexer_consume_token (parser->lexer);
12668       /* Remember that it's a `typename' type.  */
12669       tag_type = typename_type;
12670     }
12671   /* Otherwise it must be a class-key.  */
12672   else
12673     {
12674       tag_type = cp_parser_class_key (parser);
12675       if (tag_type == none_type)
12676         return error_mark_node;
12677       /* Parse the attributes.  */
12678       attributes = cp_parser_attributes_opt (parser);
12679     }
12680
12681   /* Look for the `::' operator.  */
12682   globalscope =  cp_parser_global_scope_opt (parser,
12683                                              /*current_scope_valid_p=*/false);
12684   /* Look for the nested-name-specifier.  */
12685   if (tag_type == typename_type && !globalscope)
12686     {
12687       if (!cp_parser_nested_name_specifier (parser,
12688                                            /*typename_keyword_p=*/true,
12689                                            /*check_dependency_p=*/true,
12690                                            /*type_p=*/true,
12691                                             is_declaration))
12692         return error_mark_node;
12693     }
12694   else
12695     /* Even though `typename' is not present, the proposed resolution
12696        to Core Issue 180 says that in `class A<T>::B', `B' should be
12697        considered a type-name, even if `A<T>' is dependent.  */
12698     cp_parser_nested_name_specifier_opt (parser,
12699                                          /*typename_keyword_p=*/true,
12700                                          /*check_dependency_p=*/true,
12701                                          /*type_p=*/true,
12702                                          is_declaration);
12703  /* For everything but enumeration types, consider a template-id.
12704     For an enumeration type, consider only a plain identifier.  */
12705   if (tag_type != enum_type)
12706     {
12707       bool template_p = false;
12708       tree decl;
12709
12710       /* Allow the `template' keyword.  */
12711       template_p = cp_parser_optional_template_keyword (parser);
12712       /* If we didn't see `template', we don't know if there's a
12713          template-id or not.  */
12714       if (!template_p)
12715         cp_parser_parse_tentatively (parser);
12716       /* Parse the template-id.  */
12717       token = cp_lexer_peek_token (parser->lexer);
12718       decl = cp_parser_template_id (parser, template_p,
12719                                     /*check_dependency_p=*/true,
12720                                     is_declaration);
12721       /* If we didn't find a template-id, look for an ordinary
12722          identifier.  */
12723       if (!template_p && !cp_parser_parse_definitely (parser))
12724         ;
12725       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12726          in effect, then we must assume that, upon instantiation, the
12727          template will correspond to a class.  */
12728       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12729                && tag_type == typename_type)
12730         type = make_typename_type (parser->scope, decl,
12731                                    typename_type,
12732                                    /*complain=*/tf_error);
12733       /* If the `typename' keyword is in effect and DECL is not a type
12734          decl. Then type is non existant.   */
12735       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12736         type = NULL_TREE; 
12737       else 
12738         type = TREE_TYPE (decl);
12739     }
12740
12741   if (!type)
12742     {
12743       token = cp_lexer_peek_token (parser->lexer);
12744       identifier = cp_parser_identifier (parser);
12745
12746       if (identifier == error_mark_node)
12747         {
12748           parser->scope = NULL_TREE;
12749           return error_mark_node;
12750         }
12751
12752       /* For a `typename', we needn't call xref_tag.  */
12753       if (tag_type == typename_type
12754           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12755         return cp_parser_make_typename_type (parser, parser->scope,
12756                                              identifier,
12757                                              token->location);
12758       /* Look up a qualified name in the usual way.  */
12759       if (parser->scope)
12760         {
12761           tree decl;
12762           tree ambiguous_decls;
12763
12764           decl = cp_parser_lookup_name (parser, identifier,
12765                                         tag_type,
12766                                         /*is_template=*/false,
12767                                         /*is_namespace=*/false,
12768                                         /*check_dependency=*/true,
12769                                         &ambiguous_decls,
12770                                         token->location);
12771
12772           /* If the lookup was ambiguous, an error will already have been
12773              issued.  */
12774           if (ambiguous_decls)
12775             return error_mark_node;
12776
12777           /* If we are parsing friend declaration, DECL may be a
12778              TEMPLATE_DECL tree node here.  However, we need to check
12779              whether this TEMPLATE_DECL results in valid code.  Consider
12780              the following example:
12781
12782                namespace N {
12783                  template <class T> class C {};
12784                }
12785                class X {
12786                  template <class T> friend class N::C; // #1, valid code
12787                };
12788                template <class T> class Y {
12789                  friend class N::C;                    // #2, invalid code
12790                };
12791
12792              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12793              name lookup of `N::C'.  We see that friend declaration must
12794              be template for the code to be valid.  Note that
12795              processing_template_decl does not work here since it is
12796              always 1 for the above two cases.  */
12797
12798           decl = (cp_parser_maybe_treat_template_as_class
12799                   (decl, /*tag_name_p=*/is_friend
12800                          && parser->num_template_parameter_lists));
12801
12802           if (TREE_CODE (decl) != TYPE_DECL)
12803             {
12804               cp_parser_diagnose_invalid_type_name (parser,
12805                                                     parser->scope,
12806                                                     identifier,
12807                                                     token->location);
12808               return error_mark_node;
12809             }
12810
12811           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12812             {
12813               bool allow_template = (parser->num_template_parameter_lists
12814                                       || DECL_SELF_REFERENCE_P (decl));
12815               type = check_elaborated_type_specifier (tag_type, decl, 
12816                                                       allow_template);
12817
12818               if (type == error_mark_node)
12819                 return error_mark_node;
12820             }
12821
12822           /* Forward declarations of nested types, such as
12823
12824                class C1::C2;
12825                class C1::C2::C3;
12826
12827              are invalid unless all components preceding the final '::'
12828              are complete.  If all enclosing types are complete, these
12829              declarations become merely pointless.
12830
12831              Invalid forward declarations of nested types are errors
12832              caught elsewhere in parsing.  Those that are pointless arrive
12833              here.  */
12834
12835           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12836               && !is_friend && !processing_explicit_instantiation)
12837             warning (0, "declaration %qD does not declare anything", decl);
12838
12839           type = TREE_TYPE (decl);
12840         }
12841       else
12842         {
12843           /* An elaborated-type-specifier sometimes introduces a new type and
12844              sometimes names an existing type.  Normally, the rule is that it
12845              introduces a new type only if there is not an existing type of
12846              the same name already in scope.  For example, given:
12847
12848                struct S {};
12849                void f() { struct S s; }
12850
12851              the `struct S' in the body of `f' is the same `struct S' as in
12852              the global scope; the existing definition is used.  However, if
12853              there were no global declaration, this would introduce a new
12854              local class named `S'.
12855
12856              An exception to this rule applies to the following code:
12857
12858                namespace N { struct S; }
12859
12860              Here, the elaborated-type-specifier names a new type
12861              unconditionally; even if there is already an `S' in the
12862              containing scope this declaration names a new type.
12863              This exception only applies if the elaborated-type-specifier
12864              forms the complete declaration:
12865
12866                [class.name]
12867
12868                A declaration consisting solely of `class-key identifier ;' is
12869                either a redeclaration of the name in the current scope or a
12870                forward declaration of the identifier as a class name.  It
12871                introduces the name into the current scope.
12872
12873              We are in this situation precisely when the next token is a `;'.
12874
12875              An exception to the exception is that a `friend' declaration does
12876              *not* name a new type; i.e., given:
12877
12878                struct S { friend struct T; };
12879
12880              `T' is not a new type in the scope of `S'.
12881
12882              Also, `new struct S' or `sizeof (struct S)' never results in the
12883              definition of a new type; a new type can only be declared in a
12884              declaration context.  */
12885
12886           tag_scope ts;
12887           bool template_p;
12888
12889           if (is_friend)
12890             /* Friends have special name lookup rules.  */
12891             ts = ts_within_enclosing_non_class;
12892           else if (is_declaration
12893                    && cp_lexer_next_token_is (parser->lexer,
12894                                               CPP_SEMICOLON))
12895             /* This is a `class-key identifier ;' */
12896             ts = ts_current;
12897           else
12898             ts = ts_global;
12899
12900           template_p =
12901             (parser->num_template_parameter_lists
12902              && (cp_parser_next_token_starts_class_definition_p (parser)
12903                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12904           /* An unqualified name was used to reference this type, so
12905              there were no qualifying templates.  */
12906           if (!cp_parser_check_template_parameters (parser,
12907                                                     /*num_templates=*/0,
12908                                                     token->location,
12909                                                     /*declarator=*/NULL))
12910             return error_mark_node;
12911           type = xref_tag (tag_type, identifier, ts, template_p);
12912         }
12913     }
12914
12915   if (type == error_mark_node)
12916     return error_mark_node;
12917
12918   /* Allow attributes on forward declarations of classes.  */
12919   if (attributes)
12920     {
12921       if (TREE_CODE (type) == TYPENAME_TYPE)
12922         warning (OPT_Wattributes,
12923                  "attributes ignored on uninstantiated type");
12924       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12925                && ! processing_explicit_instantiation)
12926         warning (OPT_Wattributes,
12927                  "attributes ignored on template instantiation");
12928       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12929         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12930       else
12931         warning (OPT_Wattributes,
12932                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12933     }
12934
12935   if (tag_type != enum_type)
12936     cp_parser_check_class_key (tag_type, type);
12937
12938   /* A "<" cannot follow an elaborated type specifier.  If that
12939      happens, the user was probably trying to form a template-id.  */
12940   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12941
12942   return type;
12943 }
12944
12945 /* Parse an enum-specifier.
12946
12947    enum-specifier:
12948      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12949
12950    enum-key:
12951      enum
12952      enum class   [C++0x]
12953      enum struct  [C++0x]
12954
12955    enum-base:   [C++0x]
12956      : type-specifier-seq
12957
12958    GNU Extensions:
12959      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12960        { enumerator-list [opt] }attributes[opt]
12961
12962    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12963    if the token stream isn't an enum-specifier after all.  */
12964
12965 static tree
12966 cp_parser_enum_specifier (cp_parser* parser)
12967 {
12968   tree identifier;
12969   tree type;
12970   tree attributes;
12971   bool scoped_enum_p = false;
12972   bool has_underlying_type = false;
12973   tree underlying_type = NULL_TREE;
12974
12975   /* Parse tentatively so that we can back up if we don't find a
12976      enum-specifier.  */
12977   cp_parser_parse_tentatively (parser);
12978
12979   /* Caller guarantees that the current token is 'enum', an identifier
12980      possibly follows, and the token after that is an opening brace.
12981      If we don't have an identifier, fabricate an anonymous name for
12982      the enumeration being defined.  */
12983   cp_lexer_consume_token (parser->lexer);
12984
12985   /* Parse the "class" or "struct", which indicates a scoped
12986      enumeration type in C++0x.  */
12987   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12988       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12989     {
12990       if (cxx_dialect == cxx98)
12991         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12992
12993       /* Consume the `struct' or `class' token.  */
12994       cp_lexer_consume_token (parser->lexer);
12995
12996       scoped_enum_p = true;
12997     }
12998
12999   attributes = cp_parser_attributes_opt (parser);
13000
13001   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13002     identifier = cp_parser_identifier (parser);
13003   else
13004     identifier = make_anon_name ();
13005
13006   /* Check for the `:' that denotes a specified underlying type in C++0x.
13007      Note that a ':' could also indicate a bitfield width, however.  */
13008   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13009     {
13010       cp_decl_specifier_seq type_specifiers;
13011
13012       /* Consume the `:'.  */
13013       cp_lexer_consume_token (parser->lexer);
13014
13015       /* Parse the type-specifier-seq.  */
13016       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13017                                     /*is_trailing_return=*/false,
13018                                     &type_specifiers);
13019
13020       /* At this point this is surely not elaborated type specifier.  */
13021       if (!cp_parser_parse_definitely (parser))
13022         return NULL_TREE;
13023
13024       if (cxx_dialect == cxx98)
13025         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13026
13027       has_underlying_type = true;
13028
13029       /* If that didn't work, stop.  */
13030       if (type_specifiers.type != error_mark_node)
13031         {
13032           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13033                                             /*initialized=*/0, NULL);
13034           if (underlying_type == error_mark_node)
13035             underlying_type = NULL_TREE;
13036         }
13037     }
13038
13039   /* Look for the `{' but don't consume it yet.  */
13040   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13041     {
13042       cp_parser_error (parser, "expected %<{%>");
13043       if (has_underlying_type)
13044         return NULL_TREE;
13045     }
13046
13047   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13048     return NULL_TREE;
13049
13050   /* Issue an error message if type-definitions are forbidden here.  */
13051   if (!cp_parser_check_type_definition (parser))
13052     type = error_mark_node;
13053   else
13054     /* Create the new type.  We do this before consuming the opening
13055        brace so the enum will be recorded as being on the line of its
13056        tag (or the 'enum' keyword, if there is no tag).  */
13057     type = start_enum (identifier, underlying_type, scoped_enum_p);
13058   
13059   /* Consume the opening brace.  */
13060   cp_lexer_consume_token (parser->lexer);
13061
13062   if (type == error_mark_node)
13063     {
13064       cp_parser_skip_to_end_of_block_or_statement (parser);
13065       return error_mark_node;
13066     }
13067
13068   /* If the next token is not '}', then there are some enumerators.  */
13069   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13070     cp_parser_enumerator_list (parser, type);
13071
13072   /* Consume the final '}'.  */
13073   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13074
13075   /* Look for trailing attributes to apply to this enumeration, and
13076      apply them if appropriate.  */
13077   if (cp_parser_allow_gnu_extensions_p (parser))
13078     {
13079       tree trailing_attr = cp_parser_attributes_opt (parser);
13080       trailing_attr = chainon (trailing_attr, attributes);
13081       cplus_decl_attributes (&type,
13082                              trailing_attr,
13083                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13084     }
13085
13086   /* Finish up the enumeration.  */
13087   finish_enum (type);
13088
13089   return type;
13090 }
13091
13092 /* Parse an enumerator-list.  The enumerators all have the indicated
13093    TYPE.
13094
13095    enumerator-list:
13096      enumerator-definition
13097      enumerator-list , enumerator-definition  */
13098
13099 static void
13100 cp_parser_enumerator_list (cp_parser* parser, tree type)
13101 {
13102   while (true)
13103     {
13104       /* Parse an enumerator-definition.  */
13105       cp_parser_enumerator_definition (parser, type);
13106
13107       /* If the next token is not a ',', we've reached the end of
13108          the list.  */
13109       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13110         break;
13111       /* Otherwise, consume the `,' and keep going.  */
13112       cp_lexer_consume_token (parser->lexer);
13113       /* If the next token is a `}', there is a trailing comma.  */
13114       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13115         {
13116           if (!in_system_header)
13117             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13118           break;
13119         }
13120     }
13121 }
13122
13123 /* Parse an enumerator-definition.  The enumerator has the indicated
13124    TYPE.
13125
13126    enumerator-definition:
13127      enumerator
13128      enumerator = constant-expression
13129
13130    enumerator:
13131      identifier  */
13132
13133 static void
13134 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13135 {
13136   tree identifier;
13137   tree value;
13138
13139   /* Look for the identifier.  */
13140   identifier = cp_parser_identifier (parser);
13141   if (identifier == error_mark_node)
13142     return;
13143
13144   /* If the next token is an '=', then there is an explicit value.  */
13145   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13146     {
13147       /* Consume the `=' token.  */
13148       cp_lexer_consume_token (parser->lexer);
13149       /* Parse the value.  */
13150       value = cp_parser_constant_expression (parser,
13151                                              /*allow_non_constant_p=*/false,
13152                                              NULL);
13153     }
13154   else
13155     value = NULL_TREE;
13156
13157   /* If we are processing a template, make sure the initializer of the
13158      enumerator doesn't contain any bare template parameter pack.  */
13159   if (check_for_bare_parameter_packs (value))
13160     value = error_mark_node;
13161
13162   /* Create the enumerator.  */
13163   build_enumerator (identifier, value, type);
13164 }
13165
13166 /* Parse a namespace-name.
13167
13168    namespace-name:
13169      original-namespace-name
13170      namespace-alias
13171
13172    Returns the NAMESPACE_DECL for the namespace.  */
13173
13174 static tree
13175 cp_parser_namespace_name (cp_parser* parser)
13176 {
13177   tree identifier;
13178   tree namespace_decl;
13179
13180   cp_token *token = cp_lexer_peek_token (parser->lexer);
13181
13182   /* Get the name of the namespace.  */
13183   identifier = cp_parser_identifier (parser);
13184   if (identifier == error_mark_node)
13185     return error_mark_node;
13186
13187   /* Look up the identifier in the currently active scope.  Look only
13188      for namespaces, due to:
13189
13190        [basic.lookup.udir]
13191
13192        When looking up a namespace-name in a using-directive or alias
13193        definition, only namespace names are considered.
13194
13195      And:
13196
13197        [basic.lookup.qual]
13198
13199        During the lookup of a name preceding the :: scope resolution
13200        operator, object, function, and enumerator names are ignored.
13201
13202      (Note that cp_parser_qualifying_entity only calls this
13203      function if the token after the name is the scope resolution
13204      operator.)  */
13205   namespace_decl = cp_parser_lookup_name (parser, identifier,
13206                                           none_type,
13207                                           /*is_template=*/false,
13208                                           /*is_namespace=*/true,
13209                                           /*check_dependency=*/true,
13210                                           /*ambiguous_decls=*/NULL,
13211                                           token->location);
13212   /* If it's not a namespace, issue an error.  */
13213   if (namespace_decl == error_mark_node
13214       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13215     {
13216       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13217         error_at (token->location, "%qD is not a namespace-name", identifier);
13218       cp_parser_error (parser, "expected namespace-name");
13219       namespace_decl = error_mark_node;
13220     }
13221
13222   return namespace_decl;
13223 }
13224
13225 /* Parse a namespace-definition.
13226
13227    namespace-definition:
13228      named-namespace-definition
13229      unnamed-namespace-definition
13230
13231    named-namespace-definition:
13232      original-namespace-definition
13233      extension-namespace-definition
13234
13235    original-namespace-definition:
13236      namespace identifier { namespace-body }
13237
13238    extension-namespace-definition:
13239      namespace original-namespace-name { namespace-body }
13240
13241    unnamed-namespace-definition:
13242      namespace { namespace-body } */
13243
13244 static void
13245 cp_parser_namespace_definition (cp_parser* parser)
13246 {
13247   tree identifier, attribs;
13248   bool has_visibility;
13249   bool is_inline;
13250
13251   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13252     {
13253       is_inline = true;
13254       cp_lexer_consume_token (parser->lexer);
13255     }
13256   else
13257     is_inline = false;
13258
13259   /* Look for the `namespace' keyword.  */
13260   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13261
13262   /* Get the name of the namespace.  We do not attempt to distinguish
13263      between an original-namespace-definition and an
13264      extension-namespace-definition at this point.  The semantic
13265      analysis routines are responsible for that.  */
13266   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13267     identifier = cp_parser_identifier (parser);
13268   else
13269     identifier = NULL_TREE;
13270
13271   /* Parse any specified attributes.  */
13272   attribs = cp_parser_attributes_opt (parser);
13273
13274   /* Look for the `{' to start the namespace.  */
13275   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13276   /* Start the namespace.  */
13277   push_namespace (identifier);
13278
13279   /* "inline namespace" is equivalent to a stub namespace definition
13280      followed by a strong using directive.  */
13281   if (is_inline)
13282     {
13283       tree name_space = current_namespace;
13284       /* Set up namespace association.  */
13285       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13286         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13287                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13288       /* Import the contents of the inline namespace.  */
13289       pop_namespace ();
13290       do_using_directive (name_space);
13291       push_namespace (identifier);
13292     }
13293
13294   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13295
13296   /* Parse the body of the namespace.  */
13297   cp_parser_namespace_body (parser);
13298
13299 #ifdef HANDLE_PRAGMA_VISIBILITY
13300   if (has_visibility)
13301     pop_visibility (1);
13302 #endif
13303
13304   /* Finish the namespace.  */
13305   pop_namespace ();
13306   /* Look for the final `}'.  */
13307   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13308 }
13309
13310 /* Parse a namespace-body.
13311
13312    namespace-body:
13313      declaration-seq [opt]  */
13314
13315 static void
13316 cp_parser_namespace_body (cp_parser* parser)
13317 {
13318   cp_parser_declaration_seq_opt (parser);
13319 }
13320
13321 /* Parse a namespace-alias-definition.
13322
13323    namespace-alias-definition:
13324      namespace identifier = qualified-namespace-specifier ;  */
13325
13326 static void
13327 cp_parser_namespace_alias_definition (cp_parser* parser)
13328 {
13329   tree identifier;
13330   tree namespace_specifier;
13331
13332   cp_token *token = cp_lexer_peek_token (parser->lexer);
13333
13334   /* Look for the `namespace' keyword.  */
13335   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13336   /* Look for the identifier.  */
13337   identifier = cp_parser_identifier (parser);
13338   if (identifier == error_mark_node)
13339     return;
13340   /* Look for the `=' token.  */
13341   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13342       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13343     {
13344       error_at (token->location, "%<namespace%> definition is not allowed here");
13345       /* Skip the definition.  */
13346       cp_lexer_consume_token (parser->lexer);
13347       if (cp_parser_skip_to_closing_brace (parser))
13348         cp_lexer_consume_token (parser->lexer);
13349       return;
13350     }
13351   cp_parser_require (parser, CPP_EQ, RT_EQ);
13352   /* Look for the qualified-namespace-specifier.  */
13353   namespace_specifier
13354     = cp_parser_qualified_namespace_specifier (parser);
13355   /* Look for the `;' token.  */
13356   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13357
13358   /* Register the alias in the symbol table.  */
13359   do_namespace_alias (identifier, namespace_specifier);
13360 }
13361
13362 /* Parse a qualified-namespace-specifier.
13363
13364    qualified-namespace-specifier:
13365      :: [opt] nested-name-specifier [opt] namespace-name
13366
13367    Returns a NAMESPACE_DECL corresponding to the specified
13368    namespace.  */
13369
13370 static tree
13371 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13372 {
13373   /* Look for the optional `::'.  */
13374   cp_parser_global_scope_opt (parser,
13375                               /*current_scope_valid_p=*/false);
13376
13377   /* Look for the optional nested-name-specifier.  */
13378   cp_parser_nested_name_specifier_opt (parser,
13379                                        /*typename_keyword_p=*/false,
13380                                        /*check_dependency_p=*/true,
13381                                        /*type_p=*/false,
13382                                        /*is_declaration=*/true);
13383
13384   return cp_parser_namespace_name (parser);
13385 }
13386
13387 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13388    access declaration.
13389
13390    using-declaration:
13391      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13392      using :: unqualified-id ;  
13393
13394    access-declaration:
13395      qualified-id ;  
13396
13397    */
13398
13399 static bool
13400 cp_parser_using_declaration (cp_parser* parser, 
13401                              bool access_declaration_p)
13402 {
13403   cp_token *token;
13404   bool typename_p = false;
13405   bool global_scope_p;
13406   tree decl;
13407   tree identifier;
13408   tree qscope;
13409
13410   if (access_declaration_p)
13411     cp_parser_parse_tentatively (parser);
13412   else
13413     {
13414       /* Look for the `using' keyword.  */
13415       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13416       
13417       /* Peek at the next token.  */
13418       token = cp_lexer_peek_token (parser->lexer);
13419       /* See if it's `typename'.  */
13420       if (token->keyword == RID_TYPENAME)
13421         {
13422           /* Remember that we've seen it.  */
13423           typename_p = true;
13424           /* Consume the `typename' token.  */
13425           cp_lexer_consume_token (parser->lexer);
13426         }
13427     }
13428
13429   /* Look for the optional global scope qualification.  */
13430   global_scope_p
13431     = (cp_parser_global_scope_opt (parser,
13432                                    /*current_scope_valid_p=*/false)
13433        != NULL_TREE);
13434
13435   /* If we saw `typename', or didn't see `::', then there must be a
13436      nested-name-specifier present.  */
13437   if (typename_p || !global_scope_p)
13438     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13439                                               /*check_dependency_p=*/true,
13440                                               /*type_p=*/false,
13441                                               /*is_declaration=*/true);
13442   /* Otherwise, we could be in either of the two productions.  In that
13443      case, treat the nested-name-specifier as optional.  */
13444   else
13445     qscope = cp_parser_nested_name_specifier_opt (parser,
13446                                                   /*typename_keyword_p=*/false,
13447                                                   /*check_dependency_p=*/true,
13448                                                   /*type_p=*/false,
13449                                                   /*is_declaration=*/true);
13450   if (!qscope)
13451     qscope = global_namespace;
13452
13453   if (access_declaration_p && cp_parser_error_occurred (parser))
13454     /* Something has already gone wrong; there's no need to parse
13455        further.  Since an error has occurred, the return value of
13456        cp_parser_parse_definitely will be false, as required.  */
13457     return cp_parser_parse_definitely (parser);
13458
13459   token = cp_lexer_peek_token (parser->lexer);
13460   /* Parse the unqualified-id.  */
13461   identifier = cp_parser_unqualified_id (parser,
13462                                          /*template_keyword_p=*/false,
13463                                          /*check_dependency_p=*/true,
13464                                          /*declarator_p=*/true,
13465                                          /*optional_p=*/false);
13466
13467   if (access_declaration_p)
13468     {
13469       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13470         cp_parser_simulate_error (parser);
13471       if (!cp_parser_parse_definitely (parser))
13472         return false;
13473     }
13474
13475   /* The function we call to handle a using-declaration is different
13476      depending on what scope we are in.  */
13477   if (qscope == error_mark_node || identifier == error_mark_node)
13478     ;
13479   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13480            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13481     /* [namespace.udecl]
13482
13483        A using declaration shall not name a template-id.  */
13484     error_at (token->location,
13485               "a template-id may not appear in a using-declaration");
13486   else
13487     {
13488       if (at_class_scope_p ())
13489         {
13490           /* Create the USING_DECL.  */
13491           decl = do_class_using_decl (parser->scope, identifier);
13492
13493           if (check_for_bare_parameter_packs (decl))
13494             return false;
13495           else
13496             /* Add it to the list of members in this class.  */
13497             finish_member_declaration (decl);
13498         }
13499       else
13500         {
13501           decl = cp_parser_lookup_name_simple (parser,
13502                                                identifier,
13503                                                token->location);
13504           if (decl == error_mark_node)
13505             cp_parser_name_lookup_error (parser, identifier,
13506                                          decl, NLE_NULL,
13507                                          token->location);
13508           else if (check_for_bare_parameter_packs (decl))
13509             return false;
13510           else if (!at_namespace_scope_p ())
13511             do_local_using_decl (decl, qscope, identifier);
13512           else
13513             do_toplevel_using_decl (decl, qscope, identifier);
13514         }
13515     }
13516
13517   /* Look for the final `;'.  */
13518   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13519   
13520   return true;
13521 }
13522
13523 /* Parse a using-directive.
13524
13525    using-directive:
13526      using namespace :: [opt] nested-name-specifier [opt]
13527        namespace-name ;  */
13528
13529 static void
13530 cp_parser_using_directive (cp_parser* parser)
13531 {
13532   tree namespace_decl;
13533   tree attribs;
13534
13535   /* Look for the `using' keyword.  */
13536   cp_parser_require_keyword (parser, RID_USING, RT_USING);
13537   /* And the `namespace' keyword.  */
13538   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13539   /* Look for the optional `::' operator.  */
13540   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13541   /* And the optional nested-name-specifier.  */
13542   cp_parser_nested_name_specifier_opt (parser,
13543                                        /*typename_keyword_p=*/false,
13544                                        /*check_dependency_p=*/true,
13545                                        /*type_p=*/false,
13546                                        /*is_declaration=*/true);
13547   /* Get the namespace being used.  */
13548   namespace_decl = cp_parser_namespace_name (parser);
13549   /* And any specified attributes.  */
13550   attribs = cp_parser_attributes_opt (parser);
13551   /* Update the symbol table.  */
13552   parse_using_directive (namespace_decl, attribs);
13553   /* Look for the final `;'.  */
13554   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13555 }
13556
13557 /* Parse an asm-definition.
13558
13559    asm-definition:
13560      asm ( string-literal ) ;
13561
13562    GNU Extension:
13563
13564    asm-definition:
13565      asm volatile [opt] ( string-literal ) ;
13566      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13567      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13568                           : asm-operand-list [opt] ) ;
13569      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13570                           : asm-operand-list [opt]
13571                           : asm-clobber-list [opt] ) ;
13572      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13573                                : asm-clobber-list [opt]
13574                                : asm-goto-list ) ;  */
13575
13576 static void
13577 cp_parser_asm_definition (cp_parser* parser)
13578 {
13579   tree string;
13580   tree outputs = NULL_TREE;
13581   tree inputs = NULL_TREE;
13582   tree clobbers = NULL_TREE;
13583   tree labels = NULL_TREE;
13584   tree asm_stmt;
13585   bool volatile_p = false;
13586   bool extended_p = false;
13587   bool invalid_inputs_p = false;
13588   bool invalid_outputs_p = false;
13589   bool goto_p = false;
13590   required_token missing = RT_NONE;
13591
13592   /* Look for the `asm' keyword.  */
13593   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
13594   /* See if the next token is `volatile'.  */
13595   if (cp_parser_allow_gnu_extensions_p (parser)
13596       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13597     {
13598       /* Remember that we saw the `volatile' keyword.  */
13599       volatile_p = true;
13600       /* Consume the token.  */
13601       cp_lexer_consume_token (parser->lexer);
13602     }
13603   if (cp_parser_allow_gnu_extensions_p (parser)
13604       && parser->in_function_body
13605       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13606     {
13607       /* Remember that we saw the `goto' keyword.  */
13608       goto_p = true;
13609       /* Consume the token.  */
13610       cp_lexer_consume_token (parser->lexer);
13611     }
13612   /* Look for the opening `('.  */
13613   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13614     return;
13615   /* Look for the string.  */
13616   string = cp_parser_string_literal (parser, false, false);
13617   if (string == error_mark_node)
13618     {
13619       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13620                                              /*consume_paren=*/true);
13621       return;
13622     }
13623
13624   /* If we're allowing GNU extensions, check for the extended assembly
13625      syntax.  Unfortunately, the `:' tokens need not be separated by
13626      a space in C, and so, for compatibility, we tolerate that here
13627      too.  Doing that means that we have to treat the `::' operator as
13628      two `:' tokens.  */
13629   if (cp_parser_allow_gnu_extensions_p (parser)
13630       && parser->in_function_body
13631       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13632           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13633     {
13634       bool inputs_p = false;
13635       bool clobbers_p = false;
13636       bool labels_p = false;
13637
13638       /* The extended syntax was used.  */
13639       extended_p = true;
13640
13641       /* Look for outputs.  */
13642       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13643         {
13644           /* Consume the `:'.  */
13645           cp_lexer_consume_token (parser->lexer);
13646           /* Parse the output-operands.  */
13647           if (cp_lexer_next_token_is_not (parser->lexer,
13648                                           CPP_COLON)
13649               && cp_lexer_next_token_is_not (parser->lexer,
13650                                              CPP_SCOPE)
13651               && cp_lexer_next_token_is_not (parser->lexer,
13652                                              CPP_CLOSE_PAREN)
13653               && !goto_p)
13654             outputs = cp_parser_asm_operand_list (parser);
13655
13656             if (outputs == error_mark_node)
13657               invalid_outputs_p = true;
13658         }
13659       /* If the next token is `::', there are no outputs, and the
13660          next token is the beginning of the inputs.  */
13661       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13662         /* The inputs are coming next.  */
13663         inputs_p = true;
13664
13665       /* Look for inputs.  */
13666       if (inputs_p
13667           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13668         {
13669           /* Consume the `:' or `::'.  */
13670           cp_lexer_consume_token (parser->lexer);
13671           /* Parse the output-operands.  */
13672           if (cp_lexer_next_token_is_not (parser->lexer,
13673                                           CPP_COLON)
13674               && cp_lexer_next_token_is_not (parser->lexer,
13675                                              CPP_SCOPE)
13676               && cp_lexer_next_token_is_not (parser->lexer,
13677                                              CPP_CLOSE_PAREN))
13678             inputs = cp_parser_asm_operand_list (parser);
13679
13680             if (inputs == error_mark_node)
13681               invalid_inputs_p = true;
13682         }
13683       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13684         /* The clobbers are coming next.  */
13685         clobbers_p = true;
13686
13687       /* Look for clobbers.  */
13688       if (clobbers_p
13689           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13690         {
13691           clobbers_p = true;
13692           /* Consume the `:' or `::'.  */
13693           cp_lexer_consume_token (parser->lexer);
13694           /* Parse the clobbers.  */
13695           if (cp_lexer_next_token_is_not (parser->lexer,
13696                                           CPP_COLON)
13697               && cp_lexer_next_token_is_not (parser->lexer,
13698                                              CPP_CLOSE_PAREN))
13699             clobbers = cp_parser_asm_clobber_list (parser);
13700         }
13701       else if (goto_p
13702                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13703         /* The labels are coming next.  */
13704         labels_p = true;
13705
13706       /* Look for labels.  */
13707       if (labels_p
13708           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13709         {
13710           labels_p = true;
13711           /* Consume the `:' or `::'.  */
13712           cp_lexer_consume_token (parser->lexer);
13713           /* Parse the labels.  */
13714           labels = cp_parser_asm_label_list (parser);
13715         }
13716
13717       if (goto_p && !labels_p)
13718         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
13719     }
13720   else if (goto_p)
13721     missing = RT_COLON_SCOPE;
13722
13723   /* Look for the closing `)'.  */
13724   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13725                           missing ? missing : RT_CLOSE_PAREN))
13726     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13727                                            /*consume_paren=*/true);
13728   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13729
13730   if (!invalid_inputs_p && !invalid_outputs_p)
13731     {
13732       /* Create the ASM_EXPR.  */
13733       if (parser->in_function_body)
13734         {
13735           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13736                                       inputs, clobbers, labels);
13737           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13738           if (!extended_p)
13739             {
13740               tree temp = asm_stmt;
13741               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13742                 temp = TREE_OPERAND (temp, 0);
13743
13744               ASM_INPUT_P (temp) = 1;
13745             }
13746         }
13747       else
13748         cgraph_add_asm_node (string);
13749     }
13750 }
13751
13752 /* Declarators [gram.dcl.decl] */
13753
13754 /* Parse an init-declarator.
13755
13756    init-declarator:
13757      declarator initializer [opt]
13758
13759    GNU Extension:
13760
13761    init-declarator:
13762      declarator asm-specification [opt] attributes [opt] initializer [opt]
13763
13764    function-definition:
13765      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13766        function-body
13767      decl-specifier-seq [opt] declarator function-try-block
13768
13769    GNU Extension:
13770
13771    function-definition:
13772      __extension__ function-definition
13773
13774    The DECL_SPECIFIERS apply to this declarator.  Returns a
13775    representation of the entity declared.  If MEMBER_P is TRUE, then
13776    this declarator appears in a class scope.  The new DECL created by
13777    this declarator is returned.
13778
13779    The CHECKS are access checks that should be performed once we know
13780    what entity is being declared (and, therefore, what classes have
13781    befriended it).
13782
13783    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13784    for a function-definition here as well.  If the declarator is a
13785    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13786    be TRUE upon return.  By that point, the function-definition will
13787    have been completely parsed.
13788
13789    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13790    is FALSE.  */
13791
13792 static tree
13793 cp_parser_init_declarator (cp_parser* parser,
13794                            cp_decl_specifier_seq *decl_specifiers,
13795                            VEC (deferred_access_check,gc)* checks,
13796                            bool function_definition_allowed_p,
13797                            bool member_p,
13798                            int declares_class_or_enum,
13799                            bool* function_definition_p)
13800 {
13801   cp_token *token = NULL, *asm_spec_start_token = NULL,
13802            *attributes_start_token = NULL;
13803   cp_declarator *declarator;
13804   tree prefix_attributes;
13805   tree attributes;
13806   tree asm_specification;
13807   tree initializer;
13808   tree decl = NULL_TREE;
13809   tree scope;
13810   int is_initialized;
13811   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13812      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13813      "(...)".  */
13814   enum cpp_ttype initialization_kind;
13815   bool is_direct_init = false;
13816   bool is_non_constant_init;
13817   int ctor_dtor_or_conv_p;
13818   bool friend_p;
13819   tree pushed_scope = NULL;
13820
13821   /* Gather the attributes that were provided with the
13822      decl-specifiers.  */
13823   prefix_attributes = decl_specifiers->attributes;
13824
13825   /* Assume that this is not the declarator for a function
13826      definition.  */
13827   if (function_definition_p)
13828     *function_definition_p = false;
13829
13830   /* Defer access checks while parsing the declarator; we cannot know
13831      what names are accessible until we know what is being
13832      declared.  */
13833   resume_deferring_access_checks ();
13834
13835   /* Parse the declarator.  */
13836   token = cp_lexer_peek_token (parser->lexer);
13837   declarator
13838     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13839                             &ctor_dtor_or_conv_p,
13840                             /*parenthesized_p=*/NULL,
13841                             /*member_p=*/false);
13842   /* Gather up the deferred checks.  */
13843   stop_deferring_access_checks ();
13844
13845   /* If the DECLARATOR was erroneous, there's no need to go
13846      further.  */
13847   if (declarator == cp_error_declarator)
13848     return error_mark_node;
13849
13850   /* Check that the number of template-parameter-lists is OK.  */
13851   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13852                                                        token->location))
13853     return error_mark_node;
13854
13855   if (declares_class_or_enum & 2)
13856     cp_parser_check_for_definition_in_return_type (declarator,
13857                                                    decl_specifiers->type,
13858                                                    decl_specifiers->type_location);
13859
13860   /* Figure out what scope the entity declared by the DECLARATOR is
13861      located in.  `grokdeclarator' sometimes changes the scope, so
13862      we compute it now.  */
13863   scope = get_scope_of_declarator (declarator);
13864
13865   /* Perform any lookups in the declared type which were thought to be
13866      dependent, but are not in the scope of the declarator.  */
13867   decl_specifiers->type
13868     = maybe_update_decl_type (decl_specifiers->type, scope);
13869
13870   /* If we're allowing GNU extensions, look for an asm-specification
13871      and attributes.  */
13872   if (cp_parser_allow_gnu_extensions_p (parser))
13873     {
13874       /* Look for an asm-specification.  */
13875       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13876       asm_specification = cp_parser_asm_specification_opt (parser);
13877       /* And attributes.  */
13878       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13879       attributes = cp_parser_attributes_opt (parser);
13880     }
13881   else
13882     {
13883       asm_specification = NULL_TREE;
13884       attributes = NULL_TREE;
13885     }
13886
13887   /* Peek at the next token.  */
13888   token = cp_lexer_peek_token (parser->lexer);
13889   /* Check to see if the token indicates the start of a
13890      function-definition.  */
13891   if (function_declarator_p (declarator)
13892       && cp_parser_token_starts_function_definition_p (token))
13893     {
13894       if (!function_definition_allowed_p)
13895         {
13896           /* If a function-definition should not appear here, issue an
13897              error message.  */
13898           cp_parser_error (parser,
13899                            "a function-definition is not allowed here");
13900           return error_mark_node;
13901         }
13902       else
13903         {
13904           location_t func_brace_location
13905             = cp_lexer_peek_token (parser->lexer)->location;
13906
13907           /* Neither attributes nor an asm-specification are allowed
13908              on a function-definition.  */
13909           if (asm_specification)
13910             error_at (asm_spec_start_token->location,
13911                       "an asm-specification is not allowed "
13912                       "on a function-definition");
13913           if (attributes)
13914             error_at (attributes_start_token->location,
13915                       "attributes are not allowed on a function-definition");
13916           /* This is a function-definition.  */
13917           *function_definition_p = true;
13918
13919           /* Parse the function definition.  */
13920           if (member_p)
13921             decl = cp_parser_save_member_function_body (parser,
13922                                                         decl_specifiers,
13923                                                         declarator,
13924                                                         prefix_attributes);
13925           else
13926             decl
13927               = (cp_parser_function_definition_from_specifiers_and_declarator
13928                  (parser, decl_specifiers, prefix_attributes, declarator));
13929
13930           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13931             {
13932               /* This is where the prologue starts...  */
13933               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13934                 = func_brace_location;
13935             }
13936
13937           return decl;
13938         }
13939     }
13940
13941   /* [dcl.dcl]
13942
13943      Only in function declarations for constructors, destructors, and
13944      type conversions can the decl-specifier-seq be omitted.
13945
13946      We explicitly postpone this check past the point where we handle
13947      function-definitions because we tolerate function-definitions
13948      that are missing their return types in some modes.  */
13949   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13950     {
13951       cp_parser_error (parser,
13952                        "expected constructor, destructor, or type conversion");
13953       return error_mark_node;
13954     }
13955
13956   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13957   if (token->type == CPP_EQ
13958       || token->type == CPP_OPEN_PAREN
13959       || token->type == CPP_OPEN_BRACE)
13960     {
13961       is_initialized = SD_INITIALIZED;
13962       initialization_kind = token->type;
13963
13964       if (token->type == CPP_EQ
13965           && function_declarator_p (declarator))
13966         {
13967           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13968           if (t2->keyword == RID_DEFAULT)
13969             is_initialized = SD_DEFAULTED;
13970           else if (t2->keyword == RID_DELETE)
13971             is_initialized = SD_DELETED;
13972         }
13973     }
13974   else
13975     {
13976       /* If the init-declarator isn't initialized and isn't followed by a
13977          `,' or `;', it's not a valid init-declarator.  */
13978       if (token->type != CPP_COMMA
13979           && token->type != CPP_SEMICOLON)
13980         {
13981           cp_parser_error (parser, "expected initializer");
13982           return error_mark_node;
13983         }
13984       is_initialized = SD_UNINITIALIZED;
13985       initialization_kind = CPP_EOF;
13986     }
13987
13988   /* Because start_decl has side-effects, we should only call it if we
13989      know we're going ahead.  By this point, we know that we cannot
13990      possibly be looking at any other construct.  */
13991   cp_parser_commit_to_tentative_parse (parser);
13992
13993   /* If the decl specifiers were bad, issue an error now that we're
13994      sure this was intended to be a declarator.  Then continue
13995      declaring the variable(s), as int, to try to cut down on further
13996      errors.  */
13997   if (decl_specifiers->any_specifiers_p
13998       && decl_specifiers->type == error_mark_node)
13999     {
14000       cp_parser_error (parser, "invalid type in declaration");
14001       decl_specifiers->type = integer_type_node;
14002     }
14003
14004   /* Check to see whether or not this declaration is a friend.  */
14005   friend_p = cp_parser_friend_p (decl_specifiers);
14006
14007   /* Enter the newly declared entry in the symbol table.  If we're
14008      processing a declaration in a class-specifier, we wait until
14009      after processing the initializer.  */
14010   if (!member_p)
14011     {
14012       if (parser->in_unbraced_linkage_specification_p)
14013         decl_specifiers->storage_class = sc_extern;
14014       decl = start_decl (declarator, decl_specifiers,
14015                          is_initialized, attributes, prefix_attributes,
14016                          &pushed_scope);
14017     }
14018   else if (scope)
14019     /* Enter the SCOPE.  That way unqualified names appearing in the
14020        initializer will be looked up in SCOPE.  */
14021     pushed_scope = push_scope (scope);
14022
14023   /* Perform deferred access control checks, now that we know in which
14024      SCOPE the declared entity resides.  */
14025   if (!member_p && decl)
14026     {
14027       tree saved_current_function_decl = NULL_TREE;
14028
14029       /* If the entity being declared is a function, pretend that we
14030          are in its scope.  If it is a `friend', it may have access to
14031          things that would not otherwise be accessible.  */
14032       if (TREE_CODE (decl) == FUNCTION_DECL)
14033         {
14034           saved_current_function_decl = current_function_decl;
14035           current_function_decl = decl;
14036         }
14037
14038       /* Perform access checks for template parameters.  */
14039       cp_parser_perform_template_parameter_access_checks (checks);
14040
14041       /* Perform the access control checks for the declarator and the
14042          decl-specifiers.  */
14043       perform_deferred_access_checks ();
14044
14045       /* Restore the saved value.  */
14046       if (TREE_CODE (decl) == FUNCTION_DECL)
14047         current_function_decl = saved_current_function_decl;
14048     }
14049
14050   /* Parse the initializer.  */
14051   initializer = NULL_TREE;
14052   is_direct_init = false;
14053   is_non_constant_init = true;
14054   if (is_initialized)
14055     {
14056       if (function_declarator_p (declarator))
14057         {
14058           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14059            if (initialization_kind == CPP_EQ)
14060              initializer = cp_parser_pure_specifier (parser);
14061            else
14062              {
14063                /* If the declaration was erroneous, we don't really
14064                   know what the user intended, so just silently
14065                   consume the initializer.  */
14066                if (decl != error_mark_node)
14067                  error_at (initializer_start_token->location,
14068                            "initializer provided for function");
14069                cp_parser_skip_to_closing_parenthesis (parser,
14070                                                       /*recovering=*/true,
14071                                                       /*or_comma=*/false,
14072                                                       /*consume_paren=*/true);
14073              }
14074         }
14075       else
14076         {
14077           /* We want to record the extra mangling scope for in-class
14078              initializers of class members and initializers of static data
14079              member templates.  The former is a C++0x feature which isn't
14080              implemented yet, and I expect it will involve deferring
14081              parsing of the initializer until end of class as with default
14082              arguments.  So right here we only handle the latter.  */
14083           if (!member_p && processing_template_decl)
14084             start_lambda_scope (decl);
14085           initializer = cp_parser_initializer (parser,
14086                                                &is_direct_init,
14087                                                &is_non_constant_init);
14088           if (!member_p && processing_template_decl)
14089             finish_lambda_scope ();
14090         }
14091     }
14092
14093   /* The old parser allows attributes to appear after a parenthesized
14094      initializer.  Mark Mitchell proposed removing this functionality
14095      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14096      attributes -- but ignores them.  */
14097   if (cp_parser_allow_gnu_extensions_p (parser)
14098       && initialization_kind == CPP_OPEN_PAREN)
14099     if (cp_parser_attributes_opt (parser))
14100       warning (OPT_Wattributes,
14101                "attributes after parenthesized initializer ignored");
14102
14103   /* For an in-class declaration, use `grokfield' to create the
14104      declaration.  */
14105   if (member_p)
14106     {
14107       if (pushed_scope)
14108         {
14109           pop_scope (pushed_scope);
14110           pushed_scope = false;
14111         }
14112       decl = grokfield (declarator, decl_specifiers,
14113                         initializer, !is_non_constant_init,
14114                         /*asmspec=*/NULL_TREE,
14115                         prefix_attributes);
14116       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14117         cp_parser_save_default_args (parser, decl);
14118     }
14119
14120   /* Finish processing the declaration.  But, skip friend
14121      declarations.  */
14122   if (!friend_p && decl && decl != error_mark_node)
14123     {
14124       cp_finish_decl (decl,
14125                       initializer, !is_non_constant_init,
14126                       asm_specification,
14127                       /* If the initializer is in parentheses, then this is
14128                          a direct-initialization, which means that an
14129                          `explicit' constructor is OK.  Otherwise, an
14130                          `explicit' constructor cannot be used.  */
14131                       ((is_direct_init || !is_initialized)
14132                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14133     }
14134   else if ((cxx_dialect != cxx98) && friend_p
14135            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14136     /* Core issue #226 (C++0x only): A default template-argument
14137        shall not be specified in a friend class template
14138        declaration. */
14139     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14140                              /*is_partial=*/0, /*is_friend_decl=*/1);
14141
14142   if (!friend_p && pushed_scope)
14143     pop_scope (pushed_scope);
14144
14145   return decl;
14146 }
14147
14148 /* Parse a declarator.
14149
14150    declarator:
14151      direct-declarator
14152      ptr-operator declarator
14153
14154    abstract-declarator:
14155      ptr-operator abstract-declarator [opt]
14156      direct-abstract-declarator
14157
14158    GNU Extensions:
14159
14160    declarator:
14161      attributes [opt] direct-declarator
14162      attributes [opt] ptr-operator declarator
14163
14164    abstract-declarator:
14165      attributes [opt] ptr-operator abstract-declarator [opt]
14166      attributes [opt] direct-abstract-declarator
14167
14168    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14169    detect constructor, destructor or conversion operators. It is set
14170    to -1 if the declarator is a name, and +1 if it is a
14171    function. Otherwise it is set to zero. Usually you just want to
14172    test for >0, but internally the negative value is used.
14173
14174    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14175    a decl-specifier-seq unless it declares a constructor, destructor,
14176    or conversion.  It might seem that we could check this condition in
14177    semantic analysis, rather than parsing, but that makes it difficult
14178    to handle something like `f()'.  We want to notice that there are
14179    no decl-specifiers, and therefore realize that this is an
14180    expression, not a declaration.)
14181
14182    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14183    the declarator is a direct-declarator of the form "(...)".
14184
14185    MEMBER_P is true iff this declarator is a member-declarator.  */
14186
14187 static cp_declarator *
14188 cp_parser_declarator (cp_parser* parser,
14189                       cp_parser_declarator_kind dcl_kind,
14190                       int* ctor_dtor_or_conv_p,
14191                       bool* parenthesized_p,
14192                       bool member_p)
14193 {
14194   cp_declarator *declarator;
14195   enum tree_code code;
14196   cp_cv_quals cv_quals;
14197   tree class_type;
14198   tree attributes = NULL_TREE;
14199
14200   /* Assume this is not a constructor, destructor, or type-conversion
14201      operator.  */
14202   if (ctor_dtor_or_conv_p)
14203     *ctor_dtor_or_conv_p = 0;
14204
14205   if (cp_parser_allow_gnu_extensions_p (parser))
14206     attributes = cp_parser_attributes_opt (parser);
14207
14208   /* Check for the ptr-operator production.  */
14209   cp_parser_parse_tentatively (parser);
14210   /* Parse the ptr-operator.  */
14211   code = cp_parser_ptr_operator (parser,
14212                                  &class_type,
14213                                  &cv_quals);
14214   /* If that worked, then we have a ptr-operator.  */
14215   if (cp_parser_parse_definitely (parser))
14216     {
14217       /* If a ptr-operator was found, then this declarator was not
14218          parenthesized.  */
14219       if (parenthesized_p)
14220         *parenthesized_p = true;
14221       /* The dependent declarator is optional if we are parsing an
14222          abstract-declarator.  */
14223       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14224         cp_parser_parse_tentatively (parser);
14225
14226       /* Parse the dependent declarator.  */
14227       declarator = cp_parser_declarator (parser, dcl_kind,
14228                                          /*ctor_dtor_or_conv_p=*/NULL,
14229                                          /*parenthesized_p=*/NULL,
14230                                          /*member_p=*/false);
14231
14232       /* If we are parsing an abstract-declarator, we must handle the
14233          case where the dependent declarator is absent.  */
14234       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14235           && !cp_parser_parse_definitely (parser))
14236         declarator = NULL;
14237
14238       declarator = cp_parser_make_indirect_declarator
14239         (code, class_type, cv_quals, declarator);
14240     }
14241   /* Everything else is a direct-declarator.  */
14242   else
14243     {
14244       if (parenthesized_p)
14245         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14246                                                    CPP_OPEN_PAREN);
14247       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14248                                                 ctor_dtor_or_conv_p,
14249                                                 member_p);
14250     }
14251
14252   if (attributes && declarator && declarator != cp_error_declarator)
14253     declarator->attributes = attributes;
14254
14255   return declarator;
14256 }
14257
14258 /* Parse a direct-declarator or direct-abstract-declarator.
14259
14260    direct-declarator:
14261      declarator-id
14262      direct-declarator ( parameter-declaration-clause )
14263        cv-qualifier-seq [opt]
14264        exception-specification [opt]
14265      direct-declarator [ constant-expression [opt] ]
14266      ( declarator )
14267
14268    direct-abstract-declarator:
14269      direct-abstract-declarator [opt]
14270        ( parameter-declaration-clause )
14271        cv-qualifier-seq [opt]
14272        exception-specification [opt]
14273      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14274      ( abstract-declarator )
14275
14276    Returns a representation of the declarator.  DCL_KIND is
14277    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14278    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14279    we are parsing a direct-declarator.  It is
14280    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14281    of ambiguity we prefer an abstract declarator, as per
14282    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14283    cp_parser_declarator.  */
14284
14285 static cp_declarator *
14286 cp_parser_direct_declarator (cp_parser* parser,
14287                              cp_parser_declarator_kind dcl_kind,
14288                              int* ctor_dtor_or_conv_p,
14289                              bool member_p)
14290 {
14291   cp_token *token;
14292   cp_declarator *declarator = NULL;
14293   tree scope = NULL_TREE;
14294   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14295   bool saved_in_declarator_p = parser->in_declarator_p;
14296   bool first = true;
14297   tree pushed_scope = NULL_TREE;
14298
14299   while (true)
14300     {
14301       /* Peek at the next token.  */
14302       token = cp_lexer_peek_token (parser->lexer);
14303       if (token->type == CPP_OPEN_PAREN)
14304         {
14305           /* This is either a parameter-declaration-clause, or a
14306              parenthesized declarator. When we know we are parsing a
14307              named declarator, it must be a parenthesized declarator
14308              if FIRST is true. For instance, `(int)' is a
14309              parameter-declaration-clause, with an omitted
14310              direct-abstract-declarator. But `((*))', is a
14311              parenthesized abstract declarator. Finally, when T is a
14312              template parameter `(T)' is a
14313              parameter-declaration-clause, and not a parenthesized
14314              named declarator.
14315
14316              We first try and parse a parameter-declaration-clause,
14317              and then try a nested declarator (if FIRST is true).
14318
14319              It is not an error for it not to be a
14320              parameter-declaration-clause, even when FIRST is
14321              false. Consider,
14322
14323                int i (int);
14324                int i (3);
14325
14326              The first is the declaration of a function while the
14327              second is the definition of a variable, including its
14328              initializer.
14329
14330              Having seen only the parenthesis, we cannot know which of
14331              these two alternatives should be selected.  Even more
14332              complex are examples like:
14333
14334                int i (int (a));
14335                int i (int (3));
14336
14337              The former is a function-declaration; the latter is a
14338              variable initialization.
14339
14340              Thus again, we try a parameter-declaration-clause, and if
14341              that fails, we back out and return.  */
14342
14343           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14344             {
14345               tree params;
14346               unsigned saved_num_template_parameter_lists;
14347               bool is_declarator = false;
14348               tree t;
14349
14350               /* In a member-declarator, the only valid interpretation
14351                  of a parenthesis is the start of a
14352                  parameter-declaration-clause.  (It is invalid to
14353                  initialize a static data member with a parenthesized
14354                  initializer; only the "=" form of initialization is
14355                  permitted.)  */
14356               if (!member_p)
14357                 cp_parser_parse_tentatively (parser);
14358
14359               /* Consume the `('.  */
14360               cp_lexer_consume_token (parser->lexer);
14361               if (first)
14362                 {
14363                   /* If this is going to be an abstract declarator, we're
14364                      in a declarator and we can't have default args.  */
14365                   parser->default_arg_ok_p = false;
14366                   parser->in_declarator_p = true;
14367                 }
14368
14369               /* Inside the function parameter list, surrounding
14370                  template-parameter-lists do not apply.  */
14371               saved_num_template_parameter_lists
14372                 = parser->num_template_parameter_lists;
14373               parser->num_template_parameter_lists = 0;
14374
14375               begin_scope (sk_function_parms, NULL_TREE);
14376
14377               /* Parse the parameter-declaration-clause.  */
14378               params = cp_parser_parameter_declaration_clause (parser);
14379
14380               parser->num_template_parameter_lists
14381                 = saved_num_template_parameter_lists;
14382
14383               /* If all went well, parse the cv-qualifier-seq and the
14384                  exception-specification.  */
14385               if (member_p || cp_parser_parse_definitely (parser))
14386                 {
14387                   cp_cv_quals cv_quals;
14388                   tree exception_specification;
14389                   tree late_return;
14390
14391                   is_declarator = true;
14392
14393                   if (ctor_dtor_or_conv_p)
14394                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14395                   first = false;
14396                   /* Consume the `)'.  */
14397                   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14398
14399                   /* Parse the cv-qualifier-seq.  */
14400                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14401                   /* And the exception-specification.  */
14402                   exception_specification
14403                     = cp_parser_exception_specification_opt (parser);
14404
14405                   late_return
14406                     = cp_parser_late_return_type_opt (parser);
14407
14408                   /* Create the function-declarator.  */
14409                   declarator = make_call_declarator (declarator,
14410                                                      params,
14411                                                      cv_quals,
14412                                                      exception_specification,
14413                                                      late_return);
14414                   /* Any subsequent parameter lists are to do with
14415                      return type, so are not those of the declared
14416                      function.  */
14417                   parser->default_arg_ok_p = false;
14418                 }
14419
14420               /* Remove the function parms from scope.  */
14421               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14422                 pop_binding (DECL_NAME (t), t);
14423               leave_scope();
14424
14425               if (is_declarator)
14426                 /* Repeat the main loop.  */
14427                 continue;
14428             }
14429
14430           /* If this is the first, we can try a parenthesized
14431              declarator.  */
14432           if (first)
14433             {
14434               bool saved_in_type_id_in_expr_p;
14435
14436               parser->default_arg_ok_p = saved_default_arg_ok_p;
14437               parser->in_declarator_p = saved_in_declarator_p;
14438
14439               /* Consume the `('.  */
14440               cp_lexer_consume_token (parser->lexer);
14441               /* Parse the nested declarator.  */
14442               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14443               parser->in_type_id_in_expr_p = true;
14444               declarator
14445                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14446                                         /*parenthesized_p=*/NULL,
14447                                         member_p);
14448               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14449               first = false;
14450               /* Expect a `)'.  */
14451               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14452                 declarator = cp_error_declarator;
14453               if (declarator == cp_error_declarator)
14454                 break;
14455
14456               goto handle_declarator;
14457             }
14458           /* Otherwise, we must be done.  */
14459           else
14460             break;
14461         }
14462       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14463                && token->type == CPP_OPEN_SQUARE)
14464         {
14465           /* Parse an array-declarator.  */
14466           tree bounds;
14467
14468           if (ctor_dtor_or_conv_p)
14469             *ctor_dtor_or_conv_p = 0;
14470
14471           first = false;
14472           parser->default_arg_ok_p = false;
14473           parser->in_declarator_p = true;
14474           /* Consume the `['.  */
14475           cp_lexer_consume_token (parser->lexer);
14476           /* Peek at the next token.  */
14477           token = cp_lexer_peek_token (parser->lexer);
14478           /* If the next token is `]', then there is no
14479              constant-expression.  */
14480           if (token->type != CPP_CLOSE_SQUARE)
14481             {
14482               bool non_constant_p;
14483
14484               bounds
14485                 = cp_parser_constant_expression (parser,
14486                                                  /*allow_non_constant=*/true,
14487                                                  &non_constant_p);
14488               if (!non_constant_p)
14489                 bounds = fold_non_dependent_expr (bounds);
14490               /* Normally, the array bound must be an integral constant
14491                  expression.  However, as an extension, we allow VLAs
14492                  in function scopes as long as they aren't part of a
14493                  parameter declaration.  */
14494               else if (!parser->in_function_body
14495                        || current_binding_level->kind == sk_function_parms)
14496                 {
14497                   cp_parser_error (parser,
14498                                    "array bound is not an integer constant");
14499                   bounds = error_mark_node;
14500                 }
14501               else if (processing_template_decl && !error_operand_p (bounds))
14502                 {
14503                   /* Remember this wasn't a constant-expression.  */
14504                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14505                   TREE_SIDE_EFFECTS (bounds) = 1;
14506                 }
14507             }
14508           else
14509             bounds = NULL_TREE;
14510           /* Look for the closing `]'.  */
14511           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14512             {
14513               declarator = cp_error_declarator;
14514               break;
14515             }
14516
14517           declarator = make_array_declarator (declarator, bounds);
14518         }
14519       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14520         {
14521           {
14522             tree qualifying_scope;
14523             tree unqualified_name;
14524             special_function_kind sfk;
14525             bool abstract_ok;
14526             bool pack_expansion_p = false;
14527             cp_token *declarator_id_start_token;
14528
14529             /* Parse a declarator-id */
14530             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14531             if (abstract_ok)
14532               {
14533                 cp_parser_parse_tentatively (parser);
14534
14535                 /* If we see an ellipsis, we should be looking at a
14536                    parameter pack. */
14537                 if (token->type == CPP_ELLIPSIS)
14538                   {
14539                     /* Consume the `...' */
14540                     cp_lexer_consume_token (parser->lexer);
14541
14542                     pack_expansion_p = true;
14543                   }
14544               }
14545
14546             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14547             unqualified_name
14548               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14549             qualifying_scope = parser->scope;
14550             if (abstract_ok)
14551               {
14552                 bool okay = false;
14553
14554                 if (!unqualified_name && pack_expansion_p)
14555                   {
14556                     /* Check whether an error occurred. */
14557                     okay = !cp_parser_error_occurred (parser);
14558
14559                     /* We already consumed the ellipsis to mark a
14560                        parameter pack, but we have no way to report it,
14561                        so abort the tentative parse. We will be exiting
14562                        immediately anyway. */
14563                     cp_parser_abort_tentative_parse (parser);
14564                   }
14565                 else
14566                   okay = cp_parser_parse_definitely (parser);
14567
14568                 if (!okay)
14569                   unqualified_name = error_mark_node;
14570                 else if (unqualified_name
14571                          && (qualifying_scope
14572                              || (TREE_CODE (unqualified_name)
14573                                  != IDENTIFIER_NODE)))
14574                   {
14575                     cp_parser_error (parser, "expected unqualified-id");
14576                     unqualified_name = error_mark_node;
14577                   }
14578               }
14579
14580             if (!unqualified_name)
14581               return NULL;
14582             if (unqualified_name == error_mark_node)
14583               {
14584                 declarator = cp_error_declarator;
14585                 pack_expansion_p = false;
14586                 declarator->parameter_pack_p = false;
14587                 break;
14588               }
14589
14590             if (qualifying_scope && at_namespace_scope_p ()
14591                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14592               {
14593                 /* In the declaration of a member of a template class
14594                    outside of the class itself, the SCOPE will sometimes
14595                    be a TYPENAME_TYPE.  For example, given:
14596
14597                    template <typename T>
14598                    int S<T>::R::i = 3;
14599
14600                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14601                    this context, we must resolve S<T>::R to an ordinary
14602                    type, rather than a typename type.
14603
14604                    The reason we normally avoid resolving TYPENAME_TYPEs
14605                    is that a specialization of `S' might render
14606                    `S<T>::R' not a type.  However, if `S' is
14607                    specialized, then this `i' will not be used, so there
14608                    is no harm in resolving the types here.  */
14609                 tree type;
14610
14611                 /* Resolve the TYPENAME_TYPE.  */
14612                 type = resolve_typename_type (qualifying_scope,
14613                                               /*only_current_p=*/false);
14614                 /* If that failed, the declarator is invalid.  */
14615                 if (TREE_CODE (type) == TYPENAME_TYPE)
14616                   {
14617                     if (typedef_variant_p (type))
14618                       error_at (declarator_id_start_token->location,
14619                                 "cannot define member of dependent typedef "
14620                                 "%qT", type);
14621                     else
14622                       error_at (declarator_id_start_token->location,
14623                                 "%<%T::%E%> is not a type",
14624                                 TYPE_CONTEXT (qualifying_scope),
14625                                 TYPE_IDENTIFIER (qualifying_scope));
14626                   }
14627                 qualifying_scope = type;
14628               }
14629
14630             sfk = sfk_none;
14631
14632             if (unqualified_name)
14633               {
14634                 tree class_type;
14635
14636                 if (qualifying_scope
14637                     && CLASS_TYPE_P (qualifying_scope))
14638                   class_type = qualifying_scope;
14639                 else
14640                   class_type = current_class_type;
14641
14642                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14643                   {
14644                     tree name_type = TREE_TYPE (unqualified_name);
14645                     if (class_type && same_type_p (name_type, class_type))
14646                       {
14647                         if (qualifying_scope
14648                             && CLASSTYPE_USE_TEMPLATE (name_type))
14649                           {
14650                             error_at (declarator_id_start_token->location,
14651                                       "invalid use of constructor as a template");
14652                             inform (declarator_id_start_token->location,
14653                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14654                                     "name the constructor in a qualified name",
14655                                     class_type,
14656                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14657                                     class_type, name_type);
14658                             declarator = cp_error_declarator;
14659                             break;
14660                           }
14661                         else
14662                           unqualified_name = constructor_name (class_type);
14663                       }
14664                     else
14665                       {
14666                         /* We do not attempt to print the declarator
14667                            here because we do not have enough
14668                            information about its original syntactic
14669                            form.  */
14670                         cp_parser_error (parser, "invalid declarator");
14671                         declarator = cp_error_declarator;
14672                         break;
14673                       }
14674                   }
14675
14676                 if (class_type)
14677                   {
14678                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14679                       sfk = sfk_destructor;
14680                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14681                       sfk = sfk_conversion;
14682                     else if (/* There's no way to declare a constructor
14683                                 for an anonymous type, even if the type
14684                                 got a name for linkage purposes.  */
14685                              !TYPE_WAS_ANONYMOUS (class_type)
14686                              && constructor_name_p (unqualified_name,
14687                                                     class_type))
14688                       {
14689                         unqualified_name = constructor_name (class_type);
14690                         sfk = sfk_constructor;
14691                       }
14692                     else if (is_overloaded_fn (unqualified_name)
14693                              && DECL_CONSTRUCTOR_P (get_first_fn
14694                                                     (unqualified_name)))
14695                       sfk = sfk_constructor;
14696
14697                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14698                       *ctor_dtor_or_conv_p = -1;
14699                   }
14700               }
14701             declarator = make_id_declarator (qualifying_scope,
14702                                              unqualified_name,
14703                                              sfk);
14704             declarator->id_loc = token->location;
14705             declarator->parameter_pack_p = pack_expansion_p;
14706
14707             if (pack_expansion_p)
14708               maybe_warn_variadic_templates ();
14709           }
14710
14711         handle_declarator:;
14712           scope = get_scope_of_declarator (declarator);
14713           if (scope)
14714             /* Any names that appear after the declarator-id for a
14715                member are looked up in the containing scope.  */
14716             pushed_scope = push_scope (scope);
14717           parser->in_declarator_p = true;
14718           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14719               || (declarator && declarator->kind == cdk_id))
14720             /* Default args are only allowed on function
14721                declarations.  */
14722             parser->default_arg_ok_p = saved_default_arg_ok_p;
14723           else
14724             parser->default_arg_ok_p = false;
14725
14726           first = false;
14727         }
14728       /* We're done.  */
14729       else
14730         break;
14731     }
14732
14733   /* For an abstract declarator, we might wind up with nothing at this
14734      point.  That's an error; the declarator is not optional.  */
14735   if (!declarator)
14736     cp_parser_error (parser, "expected declarator");
14737
14738   /* If we entered a scope, we must exit it now.  */
14739   if (pushed_scope)
14740     pop_scope (pushed_scope);
14741
14742   parser->default_arg_ok_p = saved_default_arg_ok_p;
14743   parser->in_declarator_p = saved_in_declarator_p;
14744
14745   return declarator;
14746 }
14747
14748 /* Parse a ptr-operator.
14749
14750    ptr-operator:
14751      * cv-qualifier-seq [opt]
14752      &
14753      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14754
14755    GNU Extension:
14756
14757    ptr-operator:
14758      & cv-qualifier-seq [opt]
14759
14760    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14761    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14762    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14763    filled in with the TYPE containing the member.  *CV_QUALS is
14764    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14765    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14766    Note that the tree codes returned by this function have nothing
14767    to do with the types of trees that will be eventually be created
14768    to represent the pointer or reference type being parsed. They are
14769    just constants with suggestive names. */
14770 static enum tree_code
14771 cp_parser_ptr_operator (cp_parser* parser,
14772                         tree* type,
14773                         cp_cv_quals *cv_quals)
14774 {
14775   enum tree_code code = ERROR_MARK;
14776   cp_token *token;
14777
14778   /* Assume that it's not a pointer-to-member.  */
14779   *type = NULL_TREE;
14780   /* And that there are no cv-qualifiers.  */
14781   *cv_quals = TYPE_UNQUALIFIED;
14782
14783   /* Peek at the next token.  */
14784   token = cp_lexer_peek_token (parser->lexer);
14785
14786   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14787   if (token->type == CPP_MULT)
14788     code = INDIRECT_REF;
14789   else if (token->type == CPP_AND)
14790     code = ADDR_EXPR;
14791   else if ((cxx_dialect != cxx98) &&
14792            token->type == CPP_AND_AND) /* C++0x only */
14793     code = NON_LVALUE_EXPR;
14794
14795   if (code != ERROR_MARK)
14796     {
14797       /* Consume the `*', `&' or `&&'.  */
14798       cp_lexer_consume_token (parser->lexer);
14799
14800       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14801          `&', if we are allowing GNU extensions.  (The only qualifier
14802          that can legally appear after `&' is `restrict', but that is
14803          enforced during semantic analysis.  */
14804       if (code == INDIRECT_REF
14805           || cp_parser_allow_gnu_extensions_p (parser))
14806         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14807     }
14808   else
14809     {
14810       /* Try the pointer-to-member case.  */
14811       cp_parser_parse_tentatively (parser);
14812       /* Look for the optional `::' operator.  */
14813       cp_parser_global_scope_opt (parser,
14814                                   /*current_scope_valid_p=*/false);
14815       /* Look for the nested-name specifier.  */
14816       token = cp_lexer_peek_token (parser->lexer);
14817       cp_parser_nested_name_specifier (parser,
14818                                        /*typename_keyword_p=*/false,
14819                                        /*check_dependency_p=*/true,
14820                                        /*type_p=*/false,
14821                                        /*is_declaration=*/false);
14822       /* If we found it, and the next token is a `*', then we are
14823          indeed looking at a pointer-to-member operator.  */
14824       if (!cp_parser_error_occurred (parser)
14825           && cp_parser_require (parser, CPP_MULT, RT_MULT))
14826         {
14827           /* Indicate that the `*' operator was used.  */
14828           code = INDIRECT_REF;
14829
14830           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14831             error_at (token->location, "%qD is a namespace", parser->scope);
14832           else
14833             {
14834               /* The type of which the member is a member is given by the
14835                  current SCOPE.  */
14836               *type = parser->scope;
14837               /* The next name will not be qualified.  */
14838               parser->scope = NULL_TREE;
14839               parser->qualifying_scope = NULL_TREE;
14840               parser->object_scope = NULL_TREE;
14841               /* Look for the optional cv-qualifier-seq.  */
14842               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14843             }
14844         }
14845       /* If that didn't work we don't have a ptr-operator.  */
14846       if (!cp_parser_parse_definitely (parser))
14847         cp_parser_error (parser, "expected ptr-operator");
14848     }
14849
14850   return code;
14851 }
14852
14853 /* Parse an (optional) cv-qualifier-seq.
14854
14855    cv-qualifier-seq:
14856      cv-qualifier cv-qualifier-seq [opt]
14857
14858    cv-qualifier:
14859      const
14860      volatile
14861
14862    GNU Extension:
14863
14864    cv-qualifier:
14865      __restrict__
14866
14867    Returns a bitmask representing the cv-qualifiers.  */
14868
14869 static cp_cv_quals
14870 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14871 {
14872   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14873
14874   while (true)
14875     {
14876       cp_token *token;
14877       cp_cv_quals cv_qualifier;
14878
14879       /* Peek at the next token.  */
14880       token = cp_lexer_peek_token (parser->lexer);
14881       /* See if it's a cv-qualifier.  */
14882       switch (token->keyword)
14883         {
14884         case RID_CONST:
14885           cv_qualifier = TYPE_QUAL_CONST;
14886           break;
14887
14888         case RID_VOLATILE:
14889           cv_qualifier = TYPE_QUAL_VOLATILE;
14890           break;
14891
14892         case RID_RESTRICT:
14893           cv_qualifier = TYPE_QUAL_RESTRICT;
14894           break;
14895
14896         default:
14897           cv_qualifier = TYPE_UNQUALIFIED;
14898           break;
14899         }
14900
14901       if (!cv_qualifier)
14902         break;
14903
14904       if (cv_quals & cv_qualifier)
14905         {
14906           error_at (token->location, "duplicate cv-qualifier");
14907           cp_lexer_purge_token (parser->lexer);
14908         }
14909       else
14910         {
14911           cp_lexer_consume_token (parser->lexer);
14912           cv_quals |= cv_qualifier;
14913         }
14914     }
14915
14916   return cv_quals;
14917 }
14918
14919 /* Parse a late-specified return type, if any.  This is not a separate
14920    non-terminal, but part of a function declarator, which looks like
14921
14922    -> trailing-type-specifier-seq abstract-declarator(opt)
14923
14924    Returns the type indicated by the type-id.  */
14925
14926 static tree
14927 cp_parser_late_return_type_opt (cp_parser* parser)
14928 {
14929   cp_token *token;
14930
14931   /* Peek at the next token.  */
14932   token = cp_lexer_peek_token (parser->lexer);
14933   /* A late-specified return type is indicated by an initial '->'. */
14934   if (token->type != CPP_DEREF)
14935     return NULL_TREE;
14936
14937   /* Consume the ->.  */
14938   cp_lexer_consume_token (parser->lexer);
14939
14940   return cp_parser_trailing_type_id (parser);
14941 }
14942
14943 /* Parse a declarator-id.
14944
14945    declarator-id:
14946      id-expression
14947      :: [opt] nested-name-specifier [opt] type-name
14948
14949    In the `id-expression' case, the value returned is as for
14950    cp_parser_id_expression if the id-expression was an unqualified-id.
14951    If the id-expression was a qualified-id, then a SCOPE_REF is
14952    returned.  The first operand is the scope (either a NAMESPACE_DECL
14953    or TREE_TYPE), but the second is still just a representation of an
14954    unqualified-id.  */
14955
14956 static tree
14957 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14958 {
14959   tree id;
14960   /* The expression must be an id-expression.  Assume that qualified
14961      names are the names of types so that:
14962
14963        template <class T>
14964        int S<T>::R::i = 3;
14965
14966      will work; we must treat `S<T>::R' as the name of a type.
14967      Similarly, assume that qualified names are templates, where
14968      required, so that:
14969
14970        template <class T>
14971        int S<T>::R<T>::i = 3;
14972
14973      will work, too.  */
14974   id = cp_parser_id_expression (parser,
14975                                 /*template_keyword_p=*/false,
14976                                 /*check_dependency_p=*/false,
14977                                 /*template_p=*/NULL,
14978                                 /*declarator_p=*/true,
14979                                 optional_p);
14980   if (id && BASELINK_P (id))
14981     id = BASELINK_FUNCTIONS (id);
14982   return id;
14983 }
14984
14985 /* Parse a type-id.
14986
14987    type-id:
14988      type-specifier-seq abstract-declarator [opt]
14989
14990    Returns the TYPE specified.  */
14991
14992 static tree
14993 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14994                      bool is_trailing_return)
14995 {
14996   cp_decl_specifier_seq type_specifier_seq;
14997   cp_declarator *abstract_declarator;
14998
14999   /* Parse the type-specifier-seq.  */
15000   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15001                                 is_trailing_return,
15002                                 &type_specifier_seq);
15003   if (type_specifier_seq.type == error_mark_node)
15004     return error_mark_node;
15005
15006   /* There might or might not be an abstract declarator.  */
15007   cp_parser_parse_tentatively (parser);
15008   /* Look for the declarator.  */
15009   abstract_declarator
15010     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15011                             /*parenthesized_p=*/NULL,
15012                             /*member_p=*/false);
15013   /* Check to see if there really was a declarator.  */
15014   if (!cp_parser_parse_definitely (parser))
15015     abstract_declarator = NULL;
15016
15017   if (type_specifier_seq.type
15018       && type_uses_auto (type_specifier_seq.type))
15019     {
15020       /* A type-id with type 'auto' is only ok if the abstract declarator
15021          is a function declarator with a late-specified return type.  */
15022       if (abstract_declarator
15023           && abstract_declarator->kind == cdk_function
15024           && abstract_declarator->u.function.late_return_type)
15025         /* OK */;
15026       else
15027         {
15028           error ("invalid use of %<auto%>");
15029           return error_mark_node;
15030         }
15031     }
15032   
15033   return groktypename (&type_specifier_seq, abstract_declarator,
15034                        is_template_arg);
15035 }
15036
15037 static tree cp_parser_type_id (cp_parser *parser)
15038 {
15039   return cp_parser_type_id_1 (parser, false, false);
15040 }
15041
15042 static tree cp_parser_template_type_arg (cp_parser *parser)
15043 {
15044   return cp_parser_type_id_1 (parser, true, false);
15045 }
15046
15047 static tree cp_parser_trailing_type_id (cp_parser *parser)
15048 {
15049   return cp_parser_type_id_1 (parser, false, true);
15050 }
15051
15052 /* Parse a type-specifier-seq.
15053
15054    type-specifier-seq:
15055      type-specifier type-specifier-seq [opt]
15056
15057    GNU extension:
15058
15059    type-specifier-seq:
15060      attributes type-specifier-seq [opt]
15061
15062    If IS_DECLARATION is true, we are at the start of a "condition" or
15063    exception-declaration, so we might be followed by a declarator-id.
15064
15065    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15066    i.e. we've just seen "->".
15067
15068    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15069
15070 static void
15071 cp_parser_type_specifier_seq (cp_parser* parser,
15072                               bool is_declaration,
15073                               bool is_trailing_return,
15074                               cp_decl_specifier_seq *type_specifier_seq)
15075 {
15076   bool seen_type_specifier = false;
15077   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15078   cp_token *start_token = NULL;
15079
15080   /* Clear the TYPE_SPECIFIER_SEQ.  */
15081   clear_decl_specs (type_specifier_seq);
15082
15083   /* In the context of a trailing return type, enum E { } is an
15084      elaborated-type-specifier followed by a function-body, not an
15085      enum-specifier.  */
15086   if (is_trailing_return)
15087     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15088
15089   /* Parse the type-specifiers and attributes.  */
15090   while (true)
15091     {
15092       tree type_specifier;
15093       bool is_cv_qualifier;
15094
15095       /* Check for attributes first.  */
15096       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15097         {
15098           type_specifier_seq->attributes =
15099             chainon (type_specifier_seq->attributes,
15100                      cp_parser_attributes_opt (parser));
15101           continue;
15102         }
15103
15104       /* record the token of the beginning of the type specifier seq,
15105          for error reporting purposes*/
15106      if (!start_token)
15107        start_token = cp_lexer_peek_token (parser->lexer);
15108
15109       /* Look for the type-specifier.  */
15110       type_specifier = cp_parser_type_specifier (parser,
15111                                                  flags,
15112                                                  type_specifier_seq,
15113                                                  /*is_declaration=*/false,
15114                                                  NULL,
15115                                                  &is_cv_qualifier);
15116       if (!type_specifier)
15117         {
15118           /* If the first type-specifier could not be found, this is not a
15119              type-specifier-seq at all.  */
15120           if (!seen_type_specifier)
15121             {
15122               cp_parser_error (parser, "expected type-specifier");
15123               type_specifier_seq->type = error_mark_node;
15124               return;
15125             }
15126           /* If subsequent type-specifiers could not be found, the
15127              type-specifier-seq is complete.  */
15128           break;
15129         }
15130
15131       seen_type_specifier = true;
15132       /* The standard says that a condition can be:
15133
15134             type-specifier-seq declarator = assignment-expression
15135
15136          However, given:
15137
15138            struct S {};
15139            if (int S = ...)
15140
15141          we should treat the "S" as a declarator, not as a
15142          type-specifier.  The standard doesn't say that explicitly for
15143          type-specifier-seq, but it does say that for
15144          decl-specifier-seq in an ordinary declaration.  Perhaps it
15145          would be clearer just to allow a decl-specifier-seq here, and
15146          then add a semantic restriction that if any decl-specifiers
15147          that are not type-specifiers appear, the program is invalid.  */
15148       if (is_declaration && !is_cv_qualifier)
15149         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15150     }
15151
15152   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15153 }
15154
15155 /* Parse a parameter-declaration-clause.
15156
15157    parameter-declaration-clause:
15158      parameter-declaration-list [opt] ... [opt]
15159      parameter-declaration-list , ...
15160
15161    Returns a representation for the parameter declarations.  A return
15162    value of NULL indicates a parameter-declaration-clause consisting
15163    only of an ellipsis.  */
15164
15165 static tree
15166 cp_parser_parameter_declaration_clause (cp_parser* parser)
15167 {
15168   tree parameters;
15169   cp_token *token;
15170   bool ellipsis_p;
15171   bool is_error;
15172
15173   /* Peek at the next token.  */
15174   token = cp_lexer_peek_token (parser->lexer);
15175   /* Check for trivial parameter-declaration-clauses.  */
15176   if (token->type == CPP_ELLIPSIS)
15177     {
15178       /* Consume the `...' token.  */
15179       cp_lexer_consume_token (parser->lexer);
15180       return NULL_TREE;
15181     }
15182   else if (token->type == CPP_CLOSE_PAREN)
15183     /* There are no parameters.  */
15184     {
15185 #ifndef NO_IMPLICIT_EXTERN_C
15186       if (in_system_header && current_class_type == NULL
15187           && current_lang_name == lang_name_c)
15188         return NULL_TREE;
15189       else
15190 #endif
15191         return void_list_node;
15192     }
15193   /* Check for `(void)', too, which is a special case.  */
15194   else if (token->keyword == RID_VOID
15195            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15196                == CPP_CLOSE_PAREN))
15197     {
15198       /* Consume the `void' token.  */
15199       cp_lexer_consume_token (parser->lexer);
15200       /* There are no parameters.  */
15201       return void_list_node;
15202     }
15203
15204   /* Parse the parameter-declaration-list.  */
15205   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15206   /* If a parse error occurred while parsing the
15207      parameter-declaration-list, then the entire
15208      parameter-declaration-clause is erroneous.  */
15209   if (is_error)
15210     return NULL;
15211
15212   /* Peek at the next token.  */
15213   token = cp_lexer_peek_token (parser->lexer);
15214   /* If it's a `,', the clause should terminate with an ellipsis.  */
15215   if (token->type == CPP_COMMA)
15216     {
15217       /* Consume the `,'.  */
15218       cp_lexer_consume_token (parser->lexer);
15219       /* Expect an ellipsis.  */
15220       ellipsis_p
15221         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15222     }
15223   /* It might also be `...' if the optional trailing `,' was
15224      omitted.  */
15225   else if (token->type == CPP_ELLIPSIS)
15226     {
15227       /* Consume the `...' token.  */
15228       cp_lexer_consume_token (parser->lexer);
15229       /* And remember that we saw it.  */
15230       ellipsis_p = true;
15231     }
15232   else
15233     ellipsis_p = false;
15234
15235   /* Finish the parameter list.  */
15236   if (!ellipsis_p)
15237     parameters = chainon (parameters, void_list_node);
15238
15239   return parameters;
15240 }
15241
15242 /* Parse a parameter-declaration-list.
15243
15244    parameter-declaration-list:
15245      parameter-declaration
15246      parameter-declaration-list , parameter-declaration
15247
15248    Returns a representation of the parameter-declaration-list, as for
15249    cp_parser_parameter_declaration_clause.  However, the
15250    `void_list_node' is never appended to the list.  Upon return,
15251    *IS_ERROR will be true iff an error occurred.  */
15252
15253 static tree
15254 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15255 {
15256   tree parameters = NULL_TREE;
15257   tree *tail = &parameters; 
15258   bool saved_in_unbraced_linkage_specification_p;
15259   int index = 0;
15260
15261   /* Assume all will go well.  */
15262   *is_error = false;
15263   /* The special considerations that apply to a function within an
15264      unbraced linkage specifications do not apply to the parameters
15265      to the function.  */
15266   saved_in_unbraced_linkage_specification_p 
15267     = parser->in_unbraced_linkage_specification_p;
15268   parser->in_unbraced_linkage_specification_p = false;
15269
15270   /* Look for more parameters.  */
15271   while (true)
15272     {
15273       cp_parameter_declarator *parameter;
15274       tree decl = error_mark_node;
15275       bool parenthesized_p;
15276       /* Parse the parameter.  */
15277       parameter
15278         = cp_parser_parameter_declaration (parser,
15279                                            /*template_parm_p=*/false,
15280                                            &parenthesized_p);
15281
15282       /* We don't know yet if the enclosing context is deprecated, so wait
15283          and warn in grokparms if appropriate.  */
15284       deprecated_state = DEPRECATED_SUPPRESS;
15285
15286       if (parameter)
15287         decl = grokdeclarator (parameter->declarator,
15288                                &parameter->decl_specifiers,
15289                                PARM,
15290                                parameter->default_argument != NULL_TREE,
15291                                &parameter->decl_specifiers.attributes);
15292
15293       deprecated_state = DEPRECATED_NORMAL;
15294
15295       /* If a parse error occurred parsing the parameter declaration,
15296          then the entire parameter-declaration-list is erroneous.  */
15297       if (decl == error_mark_node)
15298         {
15299           *is_error = true;
15300           parameters = error_mark_node;
15301           break;
15302         }
15303
15304       if (parameter->decl_specifiers.attributes)
15305         cplus_decl_attributes (&decl,
15306                                parameter->decl_specifiers.attributes,
15307                                0);
15308       if (DECL_NAME (decl))
15309         decl = pushdecl (decl);
15310
15311       if (decl != error_mark_node)
15312         {
15313           retrofit_lang_decl (decl);
15314           DECL_PARM_INDEX (decl) = ++index;
15315         }
15316
15317       /* Add the new parameter to the list.  */
15318       *tail = build_tree_list (parameter->default_argument, decl);
15319       tail = &TREE_CHAIN (*tail);
15320
15321       /* Peek at the next token.  */
15322       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15323           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15324           /* These are for Objective-C++ */
15325           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15326           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15327         /* The parameter-declaration-list is complete.  */
15328         break;
15329       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15330         {
15331           cp_token *token;
15332
15333           /* Peek at the next token.  */
15334           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15335           /* If it's an ellipsis, then the list is complete.  */
15336           if (token->type == CPP_ELLIPSIS)
15337             break;
15338           /* Otherwise, there must be more parameters.  Consume the
15339              `,'.  */
15340           cp_lexer_consume_token (parser->lexer);
15341           /* When parsing something like:
15342
15343                 int i(float f, double d)
15344
15345              we can tell after seeing the declaration for "f" that we
15346              are not looking at an initialization of a variable "i",
15347              but rather at the declaration of a function "i".
15348
15349              Due to the fact that the parsing of template arguments
15350              (as specified to a template-id) requires backtracking we
15351              cannot use this technique when inside a template argument
15352              list.  */
15353           if (!parser->in_template_argument_list_p
15354               && !parser->in_type_id_in_expr_p
15355               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15356               /* However, a parameter-declaration of the form
15357                  "foat(f)" (which is a valid declaration of a
15358                  parameter "f") can also be interpreted as an
15359                  expression (the conversion of "f" to "float").  */
15360               && !parenthesized_p)
15361             cp_parser_commit_to_tentative_parse (parser);
15362         }
15363       else
15364         {
15365           cp_parser_error (parser, "expected %<,%> or %<...%>");
15366           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15367             cp_parser_skip_to_closing_parenthesis (parser,
15368                                                    /*recovering=*/true,
15369                                                    /*or_comma=*/false,
15370                                                    /*consume_paren=*/false);
15371           break;
15372         }
15373     }
15374
15375   parser->in_unbraced_linkage_specification_p
15376     = saved_in_unbraced_linkage_specification_p;
15377
15378   return parameters;
15379 }
15380
15381 /* Parse a parameter declaration.
15382
15383    parameter-declaration:
15384      decl-specifier-seq ... [opt] declarator
15385      decl-specifier-seq declarator = assignment-expression
15386      decl-specifier-seq ... [opt] abstract-declarator [opt]
15387      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15388
15389    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15390    declares a template parameter.  (In that case, a non-nested `>'
15391    token encountered during the parsing of the assignment-expression
15392    is not interpreted as a greater-than operator.)
15393
15394    Returns a representation of the parameter, or NULL if an error
15395    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15396    true iff the declarator is of the form "(p)".  */
15397
15398 static cp_parameter_declarator *
15399 cp_parser_parameter_declaration (cp_parser *parser,
15400                                  bool template_parm_p,
15401                                  bool *parenthesized_p)
15402 {
15403   int declares_class_or_enum;
15404   cp_decl_specifier_seq decl_specifiers;
15405   cp_declarator *declarator;
15406   tree default_argument;
15407   cp_token *token = NULL, *declarator_token_start = NULL;
15408   const char *saved_message;
15409
15410   /* In a template parameter, `>' is not an operator.
15411
15412      [temp.param]
15413
15414      When parsing a default template-argument for a non-type
15415      template-parameter, the first non-nested `>' is taken as the end
15416      of the template parameter-list rather than a greater-than
15417      operator.  */
15418
15419   /* Type definitions may not appear in parameter types.  */
15420   saved_message = parser->type_definition_forbidden_message;
15421   parser->type_definition_forbidden_message
15422     = G_("types may not be defined in parameter types");
15423
15424   /* Parse the declaration-specifiers.  */
15425   cp_parser_decl_specifier_seq (parser,
15426                                 CP_PARSER_FLAGS_NONE,
15427                                 &decl_specifiers,
15428                                 &declares_class_or_enum);
15429
15430   /* Complain about missing 'typename' or other invalid type names.  */
15431   if (!decl_specifiers.any_type_specifiers_p)
15432     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15433
15434   /* If an error occurred, there's no reason to attempt to parse the
15435      rest of the declaration.  */
15436   if (cp_parser_error_occurred (parser))
15437     {
15438       parser->type_definition_forbidden_message = saved_message;
15439       return NULL;
15440     }
15441
15442   /* Peek at the next token.  */
15443   token = cp_lexer_peek_token (parser->lexer);
15444
15445   /* If the next token is a `)', `,', `=', `>', or `...', then there
15446      is no declarator. However, when variadic templates are enabled,
15447      there may be a declarator following `...'.  */
15448   if (token->type == CPP_CLOSE_PAREN
15449       || token->type == CPP_COMMA
15450       || token->type == CPP_EQ
15451       || token->type == CPP_GREATER)
15452     {
15453       declarator = NULL;
15454       if (parenthesized_p)
15455         *parenthesized_p = false;
15456     }
15457   /* Otherwise, there should be a declarator.  */
15458   else
15459     {
15460       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15461       parser->default_arg_ok_p = false;
15462
15463       /* After seeing a decl-specifier-seq, if the next token is not a
15464          "(", there is no possibility that the code is a valid
15465          expression.  Therefore, if parsing tentatively, we commit at
15466          this point.  */
15467       if (!parser->in_template_argument_list_p
15468           /* In an expression context, having seen:
15469
15470                (int((char ...
15471
15472              we cannot be sure whether we are looking at a
15473              function-type (taking a "char" as a parameter) or a cast
15474              of some object of type "char" to "int".  */
15475           && !parser->in_type_id_in_expr_p
15476           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15477           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15478         cp_parser_commit_to_tentative_parse (parser);
15479       /* Parse the declarator.  */
15480       declarator_token_start = token;
15481       declarator = cp_parser_declarator (parser,
15482                                          CP_PARSER_DECLARATOR_EITHER,
15483                                          /*ctor_dtor_or_conv_p=*/NULL,
15484                                          parenthesized_p,
15485                                          /*member_p=*/false);
15486       parser->default_arg_ok_p = saved_default_arg_ok_p;
15487       /* After the declarator, allow more attributes.  */
15488       decl_specifiers.attributes
15489         = chainon (decl_specifiers.attributes,
15490                    cp_parser_attributes_opt (parser));
15491     }
15492
15493   /* If the next token is an ellipsis, and we have not seen a
15494      declarator name, and the type of the declarator contains parameter
15495      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15496      a parameter pack expansion expression. Otherwise, leave the
15497      ellipsis for a C-style variadic function. */
15498   token = cp_lexer_peek_token (parser->lexer);
15499   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15500     {
15501       tree type = decl_specifiers.type;
15502
15503       if (type && DECL_P (type))
15504         type = TREE_TYPE (type);
15505
15506       if (type
15507           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15508           && declarator_can_be_parameter_pack (declarator)
15509           && (!declarator || !declarator->parameter_pack_p)
15510           && uses_parameter_packs (type))
15511         {
15512           /* Consume the `...'. */
15513           cp_lexer_consume_token (parser->lexer);
15514           maybe_warn_variadic_templates ();
15515           
15516           /* Build a pack expansion type */
15517           if (declarator)
15518             declarator->parameter_pack_p = true;
15519           else
15520             decl_specifiers.type = make_pack_expansion (type);
15521         }
15522     }
15523
15524   /* The restriction on defining new types applies only to the type
15525      of the parameter, not to the default argument.  */
15526   parser->type_definition_forbidden_message = saved_message;
15527
15528   /* If the next token is `=', then process a default argument.  */
15529   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15530     {
15531       /* Consume the `='.  */
15532       cp_lexer_consume_token (parser->lexer);
15533
15534       /* If we are defining a class, then the tokens that make up the
15535          default argument must be saved and processed later.  */
15536       if (!template_parm_p && at_class_scope_p ()
15537           && TYPE_BEING_DEFINED (current_class_type)
15538           && !LAMBDA_TYPE_P (current_class_type))
15539         {
15540           unsigned depth = 0;
15541           int maybe_template_id = 0;
15542           cp_token *first_token;
15543           cp_token *token;
15544
15545           /* Add tokens until we have processed the entire default
15546              argument.  We add the range [first_token, token).  */
15547           first_token = cp_lexer_peek_token (parser->lexer);
15548           while (true)
15549             {
15550               bool done = false;
15551
15552               /* Peek at the next token.  */
15553               token = cp_lexer_peek_token (parser->lexer);
15554               /* What we do depends on what token we have.  */
15555               switch (token->type)
15556                 {
15557                   /* In valid code, a default argument must be
15558                      immediately followed by a `,' `)', or `...'.  */
15559                 case CPP_COMMA:
15560                   if (depth == 0 && maybe_template_id)
15561                     {
15562                       /* If we've seen a '<', we might be in a
15563                          template-argument-list.  Until Core issue 325 is
15564                          resolved, we don't know how this situation ought
15565                          to be handled, so try to DTRT.  We check whether
15566                          what comes after the comma is a valid parameter
15567                          declaration list.  If it is, then the comma ends
15568                          the default argument; otherwise the default
15569                          argument continues.  */
15570                       bool error = false;
15571
15572                       /* Set ITALP so cp_parser_parameter_declaration_list
15573                          doesn't decide to commit to this parse.  */
15574                       bool saved_italp = parser->in_template_argument_list_p;
15575                       parser->in_template_argument_list_p = true;
15576
15577                       cp_parser_parse_tentatively (parser);
15578                       cp_lexer_consume_token (parser->lexer);
15579                       cp_parser_parameter_declaration_list (parser, &error);
15580                       if (!cp_parser_error_occurred (parser) && !error)
15581                         done = true;
15582                       cp_parser_abort_tentative_parse (parser);
15583
15584                       parser->in_template_argument_list_p = saved_italp;
15585                       break;
15586                     }
15587                 case CPP_CLOSE_PAREN:
15588                 case CPP_ELLIPSIS:
15589                   /* If we run into a non-nested `;', `}', or `]',
15590                      then the code is invalid -- but the default
15591                      argument is certainly over.  */
15592                 case CPP_SEMICOLON:
15593                 case CPP_CLOSE_BRACE:
15594                 case CPP_CLOSE_SQUARE:
15595                   if (depth == 0)
15596                     done = true;
15597                   /* Update DEPTH, if necessary.  */
15598                   else if (token->type == CPP_CLOSE_PAREN
15599                            || token->type == CPP_CLOSE_BRACE
15600                            || token->type == CPP_CLOSE_SQUARE)
15601                     --depth;
15602                   break;
15603
15604                 case CPP_OPEN_PAREN:
15605                 case CPP_OPEN_SQUARE:
15606                 case CPP_OPEN_BRACE:
15607                   ++depth;
15608                   break;
15609
15610                 case CPP_LESS:
15611                   if (depth == 0)
15612                     /* This might be the comparison operator, or it might
15613                        start a template argument list.  */
15614                     ++maybe_template_id;
15615                   break;
15616
15617                 case CPP_RSHIFT:
15618                   if (cxx_dialect == cxx98)
15619                     break;
15620                   /* Fall through for C++0x, which treats the `>>'
15621                      operator like two `>' tokens in certain
15622                      cases.  */
15623
15624                 case CPP_GREATER:
15625                   if (depth == 0)
15626                     {
15627                       /* This might be an operator, or it might close a
15628                          template argument list.  But if a previous '<'
15629                          started a template argument list, this will have
15630                          closed it, so we can't be in one anymore.  */
15631                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15632                       if (maybe_template_id < 0)
15633                         maybe_template_id = 0;
15634                     }
15635                   break;
15636
15637                   /* If we run out of tokens, issue an error message.  */
15638                 case CPP_EOF:
15639                 case CPP_PRAGMA_EOL:
15640                   error_at (token->location, "file ends in default argument");
15641                   done = true;
15642                   break;
15643
15644                 case CPP_NAME:
15645                 case CPP_SCOPE:
15646                   /* In these cases, we should look for template-ids.
15647                      For example, if the default argument is
15648                      `X<int, double>()', we need to do name lookup to
15649                      figure out whether or not `X' is a template; if
15650                      so, the `,' does not end the default argument.
15651
15652                      That is not yet done.  */
15653                   break;
15654
15655                 default:
15656                   break;
15657                 }
15658
15659               /* If we've reached the end, stop.  */
15660               if (done)
15661                 break;
15662
15663               /* Add the token to the token block.  */
15664               token = cp_lexer_consume_token (parser->lexer);
15665             }
15666
15667           /* Create a DEFAULT_ARG to represent the unparsed default
15668              argument.  */
15669           default_argument = make_node (DEFAULT_ARG);
15670           DEFARG_TOKENS (default_argument)
15671             = cp_token_cache_new (first_token, token);
15672           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15673         }
15674       /* Outside of a class definition, we can just parse the
15675          assignment-expression.  */
15676       else
15677         {
15678           token = cp_lexer_peek_token (parser->lexer);
15679           default_argument 
15680             = cp_parser_default_argument (parser, template_parm_p);
15681         }
15682
15683       if (!parser->default_arg_ok_p)
15684         {
15685           if (flag_permissive)
15686             warning (0, "deprecated use of default argument for parameter of non-function");
15687           else
15688             {
15689               error_at (token->location,
15690                         "default arguments are only "
15691                         "permitted for function parameters");
15692               default_argument = NULL_TREE;
15693             }
15694         }
15695       else if ((declarator && declarator->parameter_pack_p)
15696                || (decl_specifiers.type
15697                    && PACK_EXPANSION_P (decl_specifiers.type)))
15698         {
15699           /* Find the name of the parameter pack.  */     
15700           cp_declarator *id_declarator = declarator;
15701           while (id_declarator && id_declarator->kind != cdk_id)
15702             id_declarator = id_declarator->declarator;
15703           
15704           if (id_declarator && id_declarator->kind == cdk_id)
15705             error_at (declarator_token_start->location,
15706                       template_parm_p 
15707                       ? "template parameter pack %qD"
15708                       " cannot have a default argument"
15709                       : "parameter pack %qD cannot have a default argument",
15710                       id_declarator->u.id.unqualified_name);
15711           else
15712             error_at (declarator_token_start->location,
15713                       template_parm_p 
15714                       ? "template parameter pack cannot have a default argument"
15715                       : "parameter pack cannot have a default argument");
15716           
15717           default_argument = NULL_TREE;
15718         }
15719     }
15720   else
15721     default_argument = NULL_TREE;
15722
15723   return make_parameter_declarator (&decl_specifiers,
15724                                     declarator,
15725                                     default_argument);
15726 }
15727
15728 /* Parse a default argument and return it.
15729
15730    TEMPLATE_PARM_P is true if this is a default argument for a
15731    non-type template parameter.  */
15732 static tree
15733 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15734 {
15735   tree default_argument = NULL_TREE;
15736   bool saved_greater_than_is_operator_p;
15737   bool saved_local_variables_forbidden_p;
15738
15739   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15740      set correctly.  */
15741   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15742   parser->greater_than_is_operator_p = !template_parm_p;
15743   /* Local variable names (and the `this' keyword) may not
15744      appear in a default argument.  */
15745   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15746   parser->local_variables_forbidden_p = true;
15747   /* Parse the assignment-expression.  */
15748   if (template_parm_p)
15749     push_deferring_access_checks (dk_no_deferred);
15750   default_argument
15751     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15752   if (template_parm_p)
15753     pop_deferring_access_checks ();
15754   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15755   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15756
15757   return default_argument;
15758 }
15759
15760 /* Parse a function-body.
15761
15762    function-body:
15763      compound_statement  */
15764
15765 static void
15766 cp_parser_function_body (cp_parser *parser)
15767 {
15768   cp_parser_compound_statement (parser, NULL, false);
15769 }
15770
15771 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15772    true if a ctor-initializer was present.  */
15773
15774 static bool
15775 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15776 {
15777   tree body;
15778   bool ctor_initializer_p;
15779
15780   /* Begin the function body.  */
15781   body = begin_function_body ();
15782   /* Parse the optional ctor-initializer.  */
15783   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15784   /* Parse the function-body.  */
15785   cp_parser_function_body (parser);
15786   /* Finish the function body.  */
15787   finish_function_body (body);
15788
15789   return ctor_initializer_p;
15790 }
15791
15792 /* Parse an initializer.
15793
15794    initializer:
15795      = initializer-clause
15796      ( expression-list )
15797
15798    Returns an expression representing the initializer.  If no
15799    initializer is present, NULL_TREE is returned.
15800
15801    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15802    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15803    set to TRUE if there is no initializer present.  If there is an
15804    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15805    is set to true; otherwise it is set to false.  */
15806
15807 static tree
15808 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15809                        bool* non_constant_p)
15810 {
15811   cp_token *token;
15812   tree init;
15813
15814   /* Peek at the next token.  */
15815   token = cp_lexer_peek_token (parser->lexer);
15816
15817   /* Let our caller know whether or not this initializer was
15818      parenthesized.  */
15819   *is_direct_init = (token->type != CPP_EQ);
15820   /* Assume that the initializer is constant.  */
15821   *non_constant_p = false;
15822
15823   if (token->type == CPP_EQ)
15824     {
15825       /* Consume the `='.  */
15826       cp_lexer_consume_token (parser->lexer);
15827       /* Parse the initializer-clause.  */
15828       init = cp_parser_initializer_clause (parser, non_constant_p);
15829     }
15830   else if (token->type == CPP_OPEN_PAREN)
15831     {
15832       VEC(tree,gc) *vec;
15833       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15834                                                      /*cast_p=*/false,
15835                                                      /*allow_expansion_p=*/true,
15836                                                      non_constant_p);
15837       if (vec == NULL)
15838         return error_mark_node;
15839       init = build_tree_list_vec (vec);
15840       release_tree_vector (vec);
15841     }
15842   else if (token->type == CPP_OPEN_BRACE)
15843     {
15844       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15845       init = cp_parser_braced_list (parser, non_constant_p);
15846       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15847     }
15848   else
15849     {
15850       /* Anything else is an error.  */
15851       cp_parser_error (parser, "expected initializer");
15852       init = error_mark_node;
15853     }
15854
15855   return init;
15856 }
15857
15858 /* Parse an initializer-clause.
15859
15860    initializer-clause:
15861      assignment-expression
15862      braced-init-list
15863
15864    Returns an expression representing the initializer.
15865
15866    If the `assignment-expression' production is used the value
15867    returned is simply a representation for the expression.
15868
15869    Otherwise, calls cp_parser_braced_list.  */
15870
15871 static tree
15872 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15873 {
15874   tree initializer;
15875
15876   /* Assume the expression is constant.  */
15877   *non_constant_p = false;
15878
15879   /* If it is not a `{', then we are looking at an
15880      assignment-expression.  */
15881   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15882     {
15883       initializer
15884         = cp_parser_constant_expression (parser,
15885                                         /*allow_non_constant_p=*/true,
15886                                         non_constant_p);
15887       if (!*non_constant_p)
15888         initializer = fold_non_dependent_expr (initializer);
15889     }
15890   else
15891     initializer = cp_parser_braced_list (parser, non_constant_p);
15892
15893   return initializer;
15894 }
15895
15896 /* Parse a brace-enclosed initializer list.
15897
15898    braced-init-list:
15899      { initializer-list , [opt] }
15900      { }
15901
15902    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15903    the elements of the initializer-list (or NULL, if the last
15904    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15905    NULL_TREE.  There is no way to detect whether or not the optional
15906    trailing `,' was provided.  NON_CONSTANT_P is as for
15907    cp_parser_initializer.  */     
15908
15909 static tree
15910 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15911 {
15912   tree initializer;
15913
15914   /* Consume the `{' token.  */
15915   cp_lexer_consume_token (parser->lexer);
15916   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15917   initializer = make_node (CONSTRUCTOR);
15918   /* If it's not a `}', then there is a non-trivial initializer.  */
15919   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15920     {
15921       /* Parse the initializer list.  */
15922       CONSTRUCTOR_ELTS (initializer)
15923         = cp_parser_initializer_list (parser, non_constant_p);
15924       /* A trailing `,' token is allowed.  */
15925       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15926         cp_lexer_consume_token (parser->lexer);
15927     }
15928   /* Now, there should be a trailing `}'.  */
15929   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15930   TREE_TYPE (initializer) = init_list_type_node;
15931   return initializer;
15932 }
15933
15934 /* Parse an initializer-list.
15935
15936    initializer-list:
15937      initializer-clause ... [opt]
15938      initializer-list , initializer-clause ... [opt]
15939
15940    GNU Extension:
15941
15942    initializer-list:
15943      identifier : initializer-clause
15944      initializer-list, identifier : initializer-clause
15945
15946    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15947    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15948    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15949    as for cp_parser_initializer.  */
15950
15951 static VEC(constructor_elt,gc) *
15952 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15953 {
15954   VEC(constructor_elt,gc) *v = NULL;
15955
15956   /* Assume all of the expressions are constant.  */
15957   *non_constant_p = false;
15958
15959   /* Parse the rest of the list.  */
15960   while (true)
15961     {
15962       cp_token *token;
15963       tree identifier;
15964       tree initializer;
15965       bool clause_non_constant_p;
15966
15967       /* If the next token is an identifier and the following one is a
15968          colon, we are looking at the GNU designated-initializer
15969          syntax.  */
15970       if (cp_parser_allow_gnu_extensions_p (parser)
15971           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15972           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15973         {
15974           /* Warn the user that they are using an extension.  */
15975           pedwarn (input_location, OPT_pedantic, 
15976                    "ISO C++ does not allow designated initializers");
15977           /* Consume the identifier.  */
15978           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15979           /* Consume the `:'.  */
15980           cp_lexer_consume_token (parser->lexer);
15981         }
15982       else
15983         identifier = NULL_TREE;
15984
15985       /* Parse the initializer.  */
15986       initializer = cp_parser_initializer_clause (parser,
15987                                                   &clause_non_constant_p);
15988       /* If any clause is non-constant, so is the entire initializer.  */
15989       if (clause_non_constant_p)
15990         *non_constant_p = true;
15991
15992       /* If we have an ellipsis, this is an initializer pack
15993          expansion.  */
15994       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15995         {
15996           /* Consume the `...'.  */
15997           cp_lexer_consume_token (parser->lexer);
15998
15999           /* Turn the initializer into an initializer expansion.  */
16000           initializer = make_pack_expansion (initializer);
16001         }
16002
16003       /* Add it to the vector.  */
16004       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16005
16006       /* If the next token is not a comma, we have reached the end of
16007          the list.  */
16008       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16009         break;
16010
16011       /* Peek at the next token.  */
16012       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16013       /* If the next token is a `}', then we're still done.  An
16014          initializer-clause can have a trailing `,' after the
16015          initializer-list and before the closing `}'.  */
16016       if (token->type == CPP_CLOSE_BRACE)
16017         break;
16018
16019       /* Consume the `,' token.  */
16020       cp_lexer_consume_token (parser->lexer);
16021     }
16022
16023   return v;
16024 }
16025
16026 /* Classes [gram.class] */
16027
16028 /* Parse a class-name.
16029
16030    class-name:
16031      identifier
16032      template-id
16033
16034    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16035    to indicate that names looked up in dependent types should be
16036    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16037    keyword has been used to indicate that the name that appears next
16038    is a template.  TAG_TYPE indicates the explicit tag given before
16039    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16040    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16041    is the class being defined in a class-head.
16042
16043    Returns the TYPE_DECL representing the class.  */
16044
16045 static tree
16046 cp_parser_class_name (cp_parser *parser,
16047                       bool typename_keyword_p,
16048                       bool template_keyword_p,
16049                       enum tag_types tag_type,
16050                       bool check_dependency_p,
16051                       bool class_head_p,
16052                       bool is_declaration)
16053 {
16054   tree decl;
16055   tree scope;
16056   bool typename_p;
16057   cp_token *token;
16058   tree identifier = NULL_TREE;
16059
16060   /* All class-names start with an identifier.  */
16061   token = cp_lexer_peek_token (parser->lexer);
16062   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16063     {
16064       cp_parser_error (parser, "expected class-name");
16065       return error_mark_node;
16066     }
16067
16068   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16069      to a template-id, so we save it here.  */
16070   scope = parser->scope;
16071   if (scope == error_mark_node)
16072     return error_mark_node;
16073
16074   /* Any name names a type if we're following the `typename' keyword
16075      in a qualified name where the enclosing scope is type-dependent.  */
16076   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16077                 && dependent_type_p (scope));
16078   /* Handle the common case (an identifier, but not a template-id)
16079      efficiently.  */
16080   if (token->type == CPP_NAME
16081       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16082     {
16083       cp_token *identifier_token;
16084       bool ambiguous_p;
16085
16086       /* Look for the identifier.  */
16087       identifier_token = cp_lexer_peek_token (parser->lexer);
16088       ambiguous_p = identifier_token->ambiguous_p;
16089       identifier = cp_parser_identifier (parser);
16090       /* If the next token isn't an identifier, we are certainly not
16091          looking at a class-name.  */
16092       if (identifier == error_mark_node)
16093         decl = error_mark_node;
16094       /* If we know this is a type-name, there's no need to look it
16095          up.  */
16096       else if (typename_p)
16097         decl = identifier;
16098       else
16099         {
16100           tree ambiguous_decls;
16101           /* If we already know that this lookup is ambiguous, then
16102              we've already issued an error message; there's no reason
16103              to check again.  */
16104           if (ambiguous_p)
16105             {
16106               cp_parser_simulate_error (parser);
16107               return error_mark_node;
16108             }
16109           /* If the next token is a `::', then the name must be a type
16110              name.
16111
16112              [basic.lookup.qual]
16113
16114              During the lookup for a name preceding the :: scope
16115              resolution operator, object, function, and enumerator
16116              names are ignored.  */
16117           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16118             tag_type = typename_type;
16119           /* Look up the name.  */
16120           decl = cp_parser_lookup_name (parser, identifier,
16121                                         tag_type,
16122                                         /*is_template=*/false,
16123                                         /*is_namespace=*/false,
16124                                         check_dependency_p,
16125                                         &ambiguous_decls,
16126                                         identifier_token->location);
16127           if (ambiguous_decls)
16128             {
16129               if (cp_parser_parsing_tentatively (parser))
16130                 cp_parser_simulate_error (parser);
16131               return error_mark_node;
16132             }
16133         }
16134     }
16135   else
16136     {
16137       /* Try a template-id.  */
16138       decl = cp_parser_template_id (parser, template_keyword_p,
16139                                     check_dependency_p,
16140                                     is_declaration);
16141       if (decl == error_mark_node)
16142         return error_mark_node;
16143     }
16144
16145   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16146
16147   /* If this is a typename, create a TYPENAME_TYPE.  */
16148   if (typename_p && decl != error_mark_node)
16149     {
16150       decl = make_typename_type (scope, decl, typename_type,
16151                                  /*complain=*/tf_error);
16152       if (decl != error_mark_node)
16153         decl = TYPE_NAME (decl);
16154     }
16155
16156   /* Check to see that it is really the name of a class.  */
16157   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16158       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16159       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16160     /* Situations like this:
16161
16162          template <typename T> struct A {
16163            typename T::template X<int>::I i;
16164          };
16165
16166        are problematic.  Is `T::template X<int>' a class-name?  The
16167        standard does not seem to be definitive, but there is no other
16168        valid interpretation of the following `::'.  Therefore, those
16169        names are considered class-names.  */
16170     {
16171       decl = make_typename_type (scope, decl, tag_type, tf_error);
16172       if (decl != error_mark_node)
16173         decl = TYPE_NAME (decl);
16174     }
16175   else if (TREE_CODE (decl) != TYPE_DECL
16176            || TREE_TYPE (decl) == error_mark_node
16177            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16178     decl = error_mark_node;
16179
16180   if (decl == error_mark_node)
16181     cp_parser_error (parser, "expected class-name");
16182   else if (identifier && !parser->scope)
16183     maybe_note_name_used_in_class (identifier, decl);
16184
16185   return decl;
16186 }
16187
16188 /* Parse a class-specifier.
16189
16190    class-specifier:
16191      class-head { member-specification [opt] }
16192
16193    Returns the TREE_TYPE representing the class.  */
16194
16195 static tree
16196 cp_parser_class_specifier (cp_parser* parser)
16197 {
16198   tree type;
16199   tree attributes = NULL_TREE;
16200   bool nested_name_specifier_p;
16201   unsigned saved_num_template_parameter_lists;
16202   bool saved_in_function_body;
16203   bool saved_in_unbraced_linkage_specification_p;
16204   tree old_scope = NULL_TREE;
16205   tree scope = NULL_TREE;
16206   tree bases;
16207
16208   push_deferring_access_checks (dk_no_deferred);
16209
16210   /* Parse the class-head.  */
16211   type = cp_parser_class_head (parser,
16212                                &nested_name_specifier_p,
16213                                &attributes,
16214                                &bases);
16215   /* If the class-head was a semantic disaster, skip the entire body
16216      of the class.  */
16217   if (!type)
16218     {
16219       cp_parser_skip_to_end_of_block_or_statement (parser);
16220       pop_deferring_access_checks ();
16221       return error_mark_node;
16222     }
16223
16224   /* Look for the `{'.  */
16225   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16226     {
16227       pop_deferring_access_checks ();
16228       return error_mark_node;
16229     }
16230
16231   /* Process the base classes. If they're invalid, skip the 
16232      entire class body.  */
16233   if (!xref_basetypes (type, bases))
16234     {
16235       /* Consuming the closing brace yields better error messages
16236          later on.  */
16237       if (cp_parser_skip_to_closing_brace (parser))
16238         cp_lexer_consume_token (parser->lexer);
16239       pop_deferring_access_checks ();
16240       return error_mark_node;
16241     }
16242
16243   /* Issue an error message if type-definitions are forbidden here.  */
16244   cp_parser_check_type_definition (parser);
16245   /* Remember that we are defining one more class.  */
16246   ++parser->num_classes_being_defined;
16247   /* Inside the class, surrounding template-parameter-lists do not
16248      apply.  */
16249   saved_num_template_parameter_lists
16250     = parser->num_template_parameter_lists;
16251   parser->num_template_parameter_lists = 0;
16252   /* We are not in a function body.  */
16253   saved_in_function_body = parser->in_function_body;
16254   parser->in_function_body = false;
16255   /* We are not immediately inside an extern "lang" block.  */
16256   saved_in_unbraced_linkage_specification_p
16257     = parser->in_unbraced_linkage_specification_p;
16258   parser->in_unbraced_linkage_specification_p = false;
16259
16260   /* Start the class.  */
16261   if (nested_name_specifier_p)
16262     {
16263       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16264       old_scope = push_inner_scope (scope);
16265     }
16266   type = begin_class_definition (type, attributes);
16267
16268   if (type == error_mark_node)
16269     /* If the type is erroneous, skip the entire body of the class.  */
16270     cp_parser_skip_to_closing_brace (parser);
16271   else
16272     /* Parse the member-specification.  */
16273     cp_parser_member_specification_opt (parser);
16274
16275   /* Look for the trailing `}'.  */
16276   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16277   /* Look for trailing attributes to apply to this class.  */
16278   if (cp_parser_allow_gnu_extensions_p (parser))
16279     attributes = cp_parser_attributes_opt (parser);
16280   if (type != error_mark_node)
16281     type = finish_struct (type, attributes);
16282   if (nested_name_specifier_p)
16283     pop_inner_scope (old_scope, scope);
16284   /* If this class is not itself within the scope of another class,
16285      then we need to parse the bodies of all of the queued function
16286      definitions.  Note that the queued functions defined in a class
16287      are not always processed immediately following the
16288      class-specifier for that class.  Consider:
16289
16290        struct A {
16291          struct B { void f() { sizeof (A); } };
16292        };
16293
16294      If `f' were processed before the processing of `A' were
16295      completed, there would be no way to compute the size of `A'.
16296      Note that the nesting we are interested in here is lexical --
16297      not the semantic nesting given by TYPE_CONTEXT.  In particular,
16298      for:
16299
16300        struct A { struct B; };
16301        struct A::B { void f() { } };
16302
16303      there is no need to delay the parsing of `A::B::f'.  */
16304   if (--parser->num_classes_being_defined == 0)
16305     {
16306       tree fn;
16307       tree class_type = NULL_TREE;
16308       tree pushed_scope = NULL_TREE;
16309       unsigned ix;
16310       cp_default_arg_entry *e;
16311
16312       /* In a first pass, parse default arguments to the functions.
16313          Then, in a second pass, parse the bodies of the functions.
16314          This two-phased approach handles cases like:
16315
16316             struct S {
16317               void f() { g(); }
16318               void g(int i = 3);
16319             };
16320
16321          */
16322       for (ix = 0;
16323            VEC_iterate (cp_default_arg_entry, unparsed_funs_with_default_args,
16324                         ix, e);
16325            ix++)
16326         {
16327           fn = e->decl;
16328           /* If there are default arguments that have not yet been processed,
16329              take care of them now.  */
16330           if (class_type != e->class_type)
16331             {
16332               if (pushed_scope)
16333                 pop_scope (pushed_scope);
16334               class_type = e->class_type;
16335               pushed_scope = push_scope (class_type);
16336             }
16337           /* Make sure that any template parameters are in scope.  */
16338           maybe_begin_member_template_processing (fn);
16339           /* Parse the default argument expressions.  */
16340           cp_parser_late_parsing_default_args (parser, fn);
16341           /* Remove any template parameters from the symbol table.  */
16342           maybe_end_member_template_processing ();
16343         }
16344       if (pushed_scope)
16345         pop_scope (pushed_scope);
16346       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16347       /* Now parse the body of the functions.  */
16348       for (ix = 0;
16349            VEC_iterate (tree, unparsed_funs_with_definitions, ix, fn);
16350            ix++)
16351         cp_parser_late_parsing_for_member (parser, fn);
16352       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16353     }
16354
16355   /* Put back any saved access checks.  */
16356   pop_deferring_access_checks ();
16357
16358   /* Restore saved state.  */
16359   parser->in_function_body = saved_in_function_body;
16360   parser->num_template_parameter_lists
16361     = saved_num_template_parameter_lists;
16362   parser->in_unbraced_linkage_specification_p
16363     = saved_in_unbraced_linkage_specification_p;
16364
16365   return type;
16366 }
16367
16368 /* Parse a class-head.
16369
16370    class-head:
16371      class-key identifier [opt] base-clause [opt]
16372      class-key nested-name-specifier identifier base-clause [opt]
16373      class-key nested-name-specifier [opt] template-id
16374        base-clause [opt]
16375
16376    GNU Extensions:
16377      class-key attributes identifier [opt] base-clause [opt]
16378      class-key attributes nested-name-specifier identifier base-clause [opt]
16379      class-key attributes nested-name-specifier [opt] template-id
16380        base-clause [opt]
16381
16382    Upon return BASES is initialized to the list of base classes (or
16383    NULL, if there are none) in the same form returned by
16384    cp_parser_base_clause.
16385
16386    Returns the TYPE of the indicated class.  Sets
16387    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16388    involving a nested-name-specifier was used, and FALSE otherwise.
16389
16390    Returns error_mark_node if this is not a class-head.
16391
16392    Returns NULL_TREE if the class-head is syntactically valid, but
16393    semantically invalid in a way that means we should skip the entire
16394    body of the class.  */
16395
16396 static tree
16397 cp_parser_class_head (cp_parser* parser,
16398                       bool* nested_name_specifier_p,
16399                       tree *attributes_p,
16400                       tree *bases)
16401 {
16402   tree nested_name_specifier;
16403   enum tag_types class_key;
16404   tree id = NULL_TREE;
16405   tree type = NULL_TREE;
16406   tree attributes;
16407   bool template_id_p = false;
16408   bool qualified_p = false;
16409   bool invalid_nested_name_p = false;
16410   bool invalid_explicit_specialization_p = false;
16411   tree pushed_scope = NULL_TREE;
16412   unsigned num_templates;
16413   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16414   /* Assume no nested-name-specifier will be present.  */
16415   *nested_name_specifier_p = false;
16416   /* Assume no template parameter lists will be used in defining the
16417      type.  */
16418   num_templates = 0;
16419
16420   *bases = NULL_TREE;
16421
16422   /* Look for the class-key.  */
16423   class_key = cp_parser_class_key (parser);
16424   if (class_key == none_type)
16425     return error_mark_node;
16426
16427   /* Parse the attributes.  */
16428   attributes = cp_parser_attributes_opt (parser);
16429
16430   /* If the next token is `::', that is invalid -- but sometimes
16431      people do try to write:
16432
16433        struct ::S {};
16434
16435      Handle this gracefully by accepting the extra qualifier, and then
16436      issuing an error about it later if this really is a
16437      class-head.  If it turns out just to be an elaborated type
16438      specifier, remain silent.  */
16439   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16440     qualified_p = true;
16441
16442   push_deferring_access_checks (dk_no_check);
16443
16444   /* Determine the name of the class.  Begin by looking for an
16445      optional nested-name-specifier.  */
16446   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16447   nested_name_specifier
16448     = cp_parser_nested_name_specifier_opt (parser,
16449                                            /*typename_keyword_p=*/false,
16450                                            /*check_dependency_p=*/false,
16451                                            /*type_p=*/false,
16452                                            /*is_declaration=*/false);
16453   /* If there was a nested-name-specifier, then there *must* be an
16454      identifier.  */
16455   if (nested_name_specifier)
16456     {
16457       type_start_token = cp_lexer_peek_token (parser->lexer);
16458       /* Although the grammar says `identifier', it really means
16459          `class-name' or `template-name'.  You are only allowed to
16460          define a class that has already been declared with this
16461          syntax.
16462
16463          The proposed resolution for Core Issue 180 says that wherever
16464          you see `class T::X' you should treat `X' as a type-name.
16465
16466          It is OK to define an inaccessible class; for example:
16467
16468            class A { class B; };
16469            class A::B {};
16470
16471          We do not know if we will see a class-name, or a
16472          template-name.  We look for a class-name first, in case the
16473          class-name is a template-id; if we looked for the
16474          template-name first we would stop after the template-name.  */
16475       cp_parser_parse_tentatively (parser);
16476       type = cp_parser_class_name (parser,
16477                                    /*typename_keyword_p=*/false,
16478                                    /*template_keyword_p=*/false,
16479                                    class_type,
16480                                    /*check_dependency_p=*/false,
16481                                    /*class_head_p=*/true,
16482                                    /*is_declaration=*/false);
16483       /* If that didn't work, ignore the nested-name-specifier.  */
16484       if (!cp_parser_parse_definitely (parser))
16485         {
16486           invalid_nested_name_p = true;
16487           type_start_token = cp_lexer_peek_token (parser->lexer);
16488           id = cp_parser_identifier (parser);
16489           if (id == error_mark_node)
16490             id = NULL_TREE;
16491         }
16492       /* If we could not find a corresponding TYPE, treat this
16493          declaration like an unqualified declaration.  */
16494       if (type == error_mark_node)
16495         nested_name_specifier = NULL_TREE;
16496       /* Otherwise, count the number of templates used in TYPE and its
16497          containing scopes.  */
16498       else
16499         {
16500           tree scope;
16501
16502           for (scope = TREE_TYPE (type);
16503                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16504                scope = (TYPE_P (scope)
16505                         ? TYPE_CONTEXT (scope)
16506                         : DECL_CONTEXT (scope)))
16507             if (TYPE_P (scope)
16508                 && CLASS_TYPE_P (scope)
16509                 && CLASSTYPE_TEMPLATE_INFO (scope)
16510                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16511                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16512               ++num_templates;
16513         }
16514     }
16515   /* Otherwise, the identifier is optional.  */
16516   else
16517     {
16518       /* We don't know whether what comes next is a template-id,
16519          an identifier, or nothing at all.  */
16520       cp_parser_parse_tentatively (parser);
16521       /* Check for a template-id.  */
16522       type_start_token = cp_lexer_peek_token (parser->lexer);
16523       id = cp_parser_template_id (parser,
16524                                   /*template_keyword_p=*/false,
16525                                   /*check_dependency_p=*/true,
16526                                   /*is_declaration=*/true);
16527       /* If that didn't work, it could still be an identifier.  */
16528       if (!cp_parser_parse_definitely (parser))
16529         {
16530           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16531             {
16532               type_start_token = cp_lexer_peek_token (parser->lexer);
16533               id = cp_parser_identifier (parser);
16534             }
16535           else
16536             id = NULL_TREE;
16537         }
16538       else
16539         {
16540           template_id_p = true;
16541           ++num_templates;
16542         }
16543     }
16544
16545   pop_deferring_access_checks ();
16546
16547   if (id)
16548     cp_parser_check_for_invalid_template_id (parser, id,
16549                                              type_start_token->location);
16550
16551   /* If it's not a `:' or a `{' then we can't really be looking at a
16552      class-head, since a class-head only appears as part of a
16553      class-specifier.  We have to detect this situation before calling
16554      xref_tag, since that has irreversible side-effects.  */
16555   if (!cp_parser_next_token_starts_class_definition_p (parser))
16556     {
16557       cp_parser_error (parser, "expected %<{%> or %<:%>");
16558       return error_mark_node;
16559     }
16560
16561   /* At this point, we're going ahead with the class-specifier, even
16562      if some other problem occurs.  */
16563   cp_parser_commit_to_tentative_parse (parser);
16564   /* Issue the error about the overly-qualified name now.  */
16565   if (qualified_p)
16566     {
16567       cp_parser_error (parser,
16568                        "global qualification of class name is invalid");
16569       return error_mark_node;
16570     }
16571   else if (invalid_nested_name_p)
16572     {
16573       cp_parser_error (parser,
16574                        "qualified name does not name a class");
16575       return error_mark_node;
16576     }
16577   else if (nested_name_specifier)
16578     {
16579       tree scope;
16580
16581       /* Reject typedef-names in class heads.  */
16582       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16583         {
16584           error_at (type_start_token->location,
16585                     "invalid class name in declaration of %qD",
16586                     type);
16587           type = NULL_TREE;
16588           goto done;
16589         }
16590
16591       /* Figure out in what scope the declaration is being placed.  */
16592       scope = current_scope ();
16593       /* If that scope does not contain the scope in which the
16594          class was originally declared, the program is invalid.  */
16595       if (scope && !is_ancestor (scope, nested_name_specifier))
16596         {
16597           if (at_namespace_scope_p ())
16598             error_at (type_start_token->location,
16599                       "declaration of %qD in namespace %qD which does not "
16600                       "enclose %qD",
16601                       type, scope, nested_name_specifier);
16602           else
16603             error_at (type_start_token->location,
16604                       "declaration of %qD in %qD which does not enclose %qD",
16605                       type, scope, nested_name_specifier);
16606           type = NULL_TREE;
16607           goto done;
16608         }
16609       /* [dcl.meaning]
16610
16611          A declarator-id shall not be qualified except for the
16612          definition of a ... nested class outside of its class
16613          ... [or] the definition or explicit instantiation of a
16614          class member of a namespace outside of its namespace.  */
16615       if (scope == nested_name_specifier)
16616         {
16617           permerror (nested_name_specifier_token_start->location,
16618                      "extra qualification not allowed");
16619           nested_name_specifier = NULL_TREE;
16620           num_templates = 0;
16621         }
16622     }
16623   /* An explicit-specialization must be preceded by "template <>".  If
16624      it is not, try to recover gracefully.  */
16625   if (at_namespace_scope_p ()
16626       && parser->num_template_parameter_lists == 0
16627       && template_id_p)
16628     {
16629       error_at (type_start_token->location,
16630                 "an explicit specialization must be preceded by %<template <>%>");
16631       invalid_explicit_specialization_p = true;
16632       /* Take the same action that would have been taken by
16633          cp_parser_explicit_specialization.  */
16634       ++parser->num_template_parameter_lists;
16635       begin_specialization ();
16636     }
16637   /* There must be no "return" statements between this point and the
16638      end of this function; set "type "to the correct return value and
16639      use "goto done;" to return.  */
16640   /* Make sure that the right number of template parameters were
16641      present.  */
16642   if (!cp_parser_check_template_parameters (parser, num_templates,
16643                                             type_start_token->location,
16644                                             /*declarator=*/NULL))
16645     {
16646       /* If something went wrong, there is no point in even trying to
16647          process the class-definition.  */
16648       type = NULL_TREE;
16649       goto done;
16650     }
16651
16652   /* Look up the type.  */
16653   if (template_id_p)
16654     {
16655       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16656           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16657               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16658         {
16659           error_at (type_start_token->location,
16660                     "function template %qD redeclared as a class template", id);
16661           type = error_mark_node;
16662         }
16663       else
16664         {
16665           type = TREE_TYPE (id);
16666           type = maybe_process_partial_specialization (type);
16667         }
16668       if (nested_name_specifier)
16669         pushed_scope = push_scope (nested_name_specifier);
16670     }
16671   else if (nested_name_specifier)
16672     {
16673       tree class_type;
16674
16675       /* Given:
16676
16677             template <typename T> struct S { struct T };
16678             template <typename T> struct S<T>::T { };
16679
16680          we will get a TYPENAME_TYPE when processing the definition of
16681          `S::T'.  We need to resolve it to the actual type before we
16682          try to define it.  */
16683       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16684         {
16685           class_type = resolve_typename_type (TREE_TYPE (type),
16686                                               /*only_current_p=*/false);
16687           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16688             type = TYPE_NAME (class_type);
16689           else
16690             {
16691               cp_parser_error (parser, "could not resolve typename type");
16692               type = error_mark_node;
16693             }
16694         }
16695
16696       if (maybe_process_partial_specialization (TREE_TYPE (type))
16697           == error_mark_node)
16698         {
16699           type = NULL_TREE;
16700           goto done;
16701         }
16702
16703       class_type = current_class_type;
16704       /* Enter the scope indicated by the nested-name-specifier.  */
16705       pushed_scope = push_scope (nested_name_specifier);
16706       /* Get the canonical version of this type.  */
16707       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16708       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16709           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16710         {
16711           type = push_template_decl (type);
16712           if (type == error_mark_node)
16713             {
16714               type = NULL_TREE;
16715               goto done;
16716             }
16717         }
16718
16719       type = TREE_TYPE (type);
16720       *nested_name_specifier_p = true;
16721     }
16722   else      /* The name is not a nested name.  */
16723     {
16724       /* If the class was unnamed, create a dummy name.  */
16725       if (!id)
16726         id = make_anon_name ();
16727       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16728                        parser->num_template_parameter_lists);
16729     }
16730
16731   /* Indicate whether this class was declared as a `class' or as a
16732      `struct'.  */
16733   if (TREE_CODE (type) == RECORD_TYPE)
16734     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16735   cp_parser_check_class_key (class_key, type);
16736
16737   /* If this type was already complete, and we see another definition,
16738      that's an error.  */
16739   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16740     {
16741       error_at (type_start_token->location, "redefinition of %q#T",
16742                 type);
16743       error_at (type_start_token->location, "previous definition of %q+#T",
16744                 type);
16745       type = NULL_TREE;
16746       goto done;
16747     }
16748   else if (type == error_mark_node)
16749     type = NULL_TREE;
16750
16751   /* We will have entered the scope containing the class; the names of
16752      base classes should be looked up in that context.  For example:
16753
16754        struct A { struct B {}; struct C; };
16755        struct A::C : B {};
16756
16757      is valid.  */
16758
16759   /* Get the list of base-classes, if there is one.  */
16760   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16761     *bases = cp_parser_base_clause (parser);
16762
16763  done:
16764   /* Leave the scope given by the nested-name-specifier.  We will
16765      enter the class scope itself while processing the members.  */
16766   if (pushed_scope)
16767     pop_scope (pushed_scope);
16768
16769   if (invalid_explicit_specialization_p)
16770     {
16771       end_specialization ();
16772       --parser->num_template_parameter_lists;
16773     }
16774
16775   if (type)
16776     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16777   *attributes_p = attributes;
16778   return type;
16779 }
16780
16781 /* Parse a class-key.
16782
16783    class-key:
16784      class
16785      struct
16786      union
16787
16788    Returns the kind of class-key specified, or none_type to indicate
16789    error.  */
16790
16791 static enum tag_types
16792 cp_parser_class_key (cp_parser* parser)
16793 {
16794   cp_token *token;
16795   enum tag_types tag_type;
16796
16797   /* Look for the class-key.  */
16798   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
16799   if (!token)
16800     return none_type;
16801
16802   /* Check to see if the TOKEN is a class-key.  */
16803   tag_type = cp_parser_token_is_class_key (token);
16804   if (!tag_type)
16805     cp_parser_error (parser, "expected class-key");
16806   return tag_type;
16807 }
16808
16809 /* Parse an (optional) member-specification.
16810
16811    member-specification:
16812      member-declaration member-specification [opt]
16813      access-specifier : member-specification [opt]  */
16814
16815 static void
16816 cp_parser_member_specification_opt (cp_parser* parser)
16817 {
16818   while (true)
16819     {
16820       cp_token *token;
16821       enum rid keyword;
16822
16823       /* Peek at the next token.  */
16824       token = cp_lexer_peek_token (parser->lexer);
16825       /* If it's a `}', or EOF then we've seen all the members.  */
16826       if (token->type == CPP_CLOSE_BRACE
16827           || token->type == CPP_EOF
16828           || token->type == CPP_PRAGMA_EOL)
16829         break;
16830
16831       /* See if this token is a keyword.  */
16832       keyword = token->keyword;
16833       switch (keyword)
16834         {
16835         case RID_PUBLIC:
16836         case RID_PROTECTED:
16837         case RID_PRIVATE:
16838           /* Consume the access-specifier.  */
16839           cp_lexer_consume_token (parser->lexer);
16840           /* Remember which access-specifier is active.  */
16841           current_access_specifier = token->u.value;
16842           /* Look for the `:'.  */
16843           cp_parser_require (parser, CPP_COLON, RT_COLON);
16844           break;
16845
16846         default:
16847           /* Accept #pragmas at class scope.  */
16848           if (token->type == CPP_PRAGMA)
16849             {
16850               cp_parser_pragma (parser, pragma_external);
16851               break;
16852             }
16853
16854           /* Otherwise, the next construction must be a
16855              member-declaration.  */
16856           cp_parser_member_declaration (parser);
16857         }
16858     }
16859 }
16860
16861 /* Parse a member-declaration.
16862
16863    member-declaration:
16864      decl-specifier-seq [opt] member-declarator-list [opt] ;
16865      function-definition ; [opt]
16866      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16867      using-declaration
16868      template-declaration
16869
16870    member-declarator-list:
16871      member-declarator
16872      member-declarator-list , member-declarator
16873
16874    member-declarator:
16875      declarator pure-specifier [opt]
16876      declarator constant-initializer [opt]
16877      identifier [opt] : constant-expression
16878
16879    GNU Extensions:
16880
16881    member-declaration:
16882      __extension__ member-declaration
16883
16884    member-declarator:
16885      declarator attributes [opt] pure-specifier [opt]
16886      declarator attributes [opt] constant-initializer [opt]
16887      identifier [opt] attributes [opt] : constant-expression  
16888
16889    C++0x Extensions:
16890
16891    member-declaration:
16892      static_assert-declaration  */
16893
16894 static void
16895 cp_parser_member_declaration (cp_parser* parser)
16896 {
16897   cp_decl_specifier_seq decl_specifiers;
16898   tree prefix_attributes;
16899   tree decl;
16900   int declares_class_or_enum;
16901   bool friend_p;
16902   cp_token *token = NULL;
16903   cp_token *decl_spec_token_start = NULL;
16904   cp_token *initializer_token_start = NULL;
16905   int saved_pedantic;
16906
16907   /* Check for the `__extension__' keyword.  */
16908   if (cp_parser_extension_opt (parser, &saved_pedantic))
16909     {
16910       /* Recurse.  */
16911       cp_parser_member_declaration (parser);
16912       /* Restore the old value of the PEDANTIC flag.  */
16913       pedantic = saved_pedantic;
16914
16915       return;
16916     }
16917
16918   /* Check for a template-declaration.  */
16919   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16920     {
16921       /* An explicit specialization here is an error condition, and we
16922          expect the specialization handler to detect and report this.  */
16923       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16924           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16925         cp_parser_explicit_specialization (parser);
16926       else
16927         cp_parser_template_declaration (parser, /*member_p=*/true);
16928
16929       return;
16930     }
16931
16932   /* Check for a using-declaration.  */
16933   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16934     {
16935       /* Parse the using-declaration.  */
16936       cp_parser_using_declaration (parser,
16937                                    /*access_declaration_p=*/false);
16938       return;
16939     }
16940
16941   /* Check for @defs.  */
16942   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16943     {
16944       tree ivar, member;
16945       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16946       ivar = ivar_chains;
16947       while (ivar)
16948         {
16949           member = ivar;
16950           ivar = TREE_CHAIN (member);
16951           TREE_CHAIN (member) = NULL_TREE;
16952           finish_member_declaration (member);
16953         }
16954       return;
16955     }
16956
16957   /* If the next token is `static_assert' we have a static assertion.  */
16958   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16959     {
16960       cp_parser_static_assert (parser, /*member_p=*/true);
16961       return;
16962     }
16963
16964   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16965     return;
16966
16967   /* Parse the decl-specifier-seq.  */
16968   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16969   cp_parser_decl_specifier_seq (parser,
16970                                 CP_PARSER_FLAGS_OPTIONAL,
16971                                 &decl_specifiers,
16972                                 &declares_class_or_enum);
16973   prefix_attributes = decl_specifiers.attributes;
16974   decl_specifiers.attributes = NULL_TREE;
16975   /* Check for an invalid type-name.  */
16976   if (!decl_specifiers.any_type_specifiers_p
16977       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16978     return;
16979   /* If there is no declarator, then the decl-specifier-seq should
16980      specify a type.  */
16981   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16982     {
16983       /* If there was no decl-specifier-seq, and the next token is a
16984          `;', then we have something like:
16985
16986            struct S { ; };
16987
16988          [class.mem]
16989
16990          Each member-declaration shall declare at least one member
16991          name of the class.  */
16992       if (!decl_specifiers.any_specifiers_p)
16993         {
16994           cp_token *token = cp_lexer_peek_token (parser->lexer);
16995           if (!in_system_header_at (token->location))
16996             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16997         }
16998       else
16999         {
17000           tree type;
17001
17002           /* See if this declaration is a friend.  */
17003           friend_p = cp_parser_friend_p (&decl_specifiers);
17004           /* If there were decl-specifiers, check to see if there was
17005              a class-declaration.  */
17006           type = check_tag_decl (&decl_specifiers);
17007           /* Nested classes have already been added to the class, but
17008              a `friend' needs to be explicitly registered.  */
17009           if (friend_p)
17010             {
17011               /* If the `friend' keyword was present, the friend must
17012                  be introduced with a class-key.  */
17013                if (!declares_class_or_enum)
17014                  error_at (decl_spec_token_start->location,
17015                            "a class-key must be used when declaring a friend");
17016                /* In this case:
17017
17018                     template <typename T> struct A {
17019                       friend struct A<T>::B;
17020                     };
17021
17022                   A<T>::B will be represented by a TYPENAME_TYPE, and
17023                   therefore not recognized by check_tag_decl.  */
17024                if (!type
17025                    && decl_specifiers.type
17026                    && TYPE_P (decl_specifiers.type))
17027                  type = decl_specifiers.type;
17028                if (!type || !TYPE_P (type))
17029                  error_at (decl_spec_token_start->location,
17030                            "friend declaration does not name a class or "
17031                            "function");
17032                else
17033                  make_friend_class (current_class_type, type,
17034                                     /*complain=*/true);
17035             }
17036           /* If there is no TYPE, an error message will already have
17037              been issued.  */
17038           else if (!type || type == error_mark_node)
17039             ;
17040           /* An anonymous aggregate has to be handled specially; such
17041              a declaration really declares a data member (with a
17042              particular type), as opposed to a nested class.  */
17043           else if (ANON_AGGR_TYPE_P (type))
17044             {
17045               /* Remove constructors and such from TYPE, now that we
17046                  know it is an anonymous aggregate.  */
17047               fixup_anonymous_aggr (type);
17048               /* And make the corresponding data member.  */
17049               decl = build_decl (decl_spec_token_start->location,
17050                                  FIELD_DECL, NULL_TREE, type);
17051               /* Add it to the class.  */
17052               finish_member_declaration (decl);
17053             }
17054           else
17055             cp_parser_check_access_in_redeclaration
17056                                               (TYPE_NAME (type),
17057                                                decl_spec_token_start->location);
17058         }
17059     }
17060   else
17061     {
17062       /* See if these declarations will be friends.  */
17063       friend_p = cp_parser_friend_p (&decl_specifiers);
17064
17065       /* Keep going until we hit the `;' at the end of the
17066          declaration.  */
17067       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17068         {
17069           tree attributes = NULL_TREE;
17070           tree first_attribute;
17071
17072           /* Peek at the next token.  */
17073           token = cp_lexer_peek_token (parser->lexer);
17074
17075           /* Check for a bitfield declaration.  */
17076           if (token->type == CPP_COLON
17077               || (token->type == CPP_NAME
17078                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17079                   == CPP_COLON))
17080             {
17081               tree identifier;
17082               tree width;
17083
17084               /* Get the name of the bitfield.  Note that we cannot just
17085                  check TOKEN here because it may have been invalidated by
17086                  the call to cp_lexer_peek_nth_token above.  */
17087               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17088                 identifier = cp_parser_identifier (parser);
17089               else
17090                 identifier = NULL_TREE;
17091
17092               /* Consume the `:' token.  */
17093               cp_lexer_consume_token (parser->lexer);
17094               /* Get the width of the bitfield.  */
17095               width
17096                 = cp_parser_constant_expression (parser,
17097                                                  /*allow_non_constant=*/false,
17098                                                  NULL);
17099
17100               /* Look for attributes that apply to the bitfield.  */
17101               attributes = cp_parser_attributes_opt (parser);
17102               /* Remember which attributes are prefix attributes and
17103                  which are not.  */
17104               first_attribute = attributes;
17105               /* Combine the attributes.  */
17106               attributes = chainon (prefix_attributes, attributes);
17107
17108               /* Create the bitfield declaration.  */
17109               decl = grokbitfield (identifier
17110                                    ? make_id_declarator (NULL_TREE,
17111                                                          identifier,
17112                                                          sfk_none)
17113                                    : NULL,
17114                                    &decl_specifiers,
17115                                    width,
17116                                    attributes);
17117             }
17118           else
17119             {
17120               cp_declarator *declarator;
17121               tree initializer;
17122               tree asm_specification;
17123               int ctor_dtor_or_conv_p;
17124
17125               /* Parse the declarator.  */
17126               declarator
17127                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17128                                         &ctor_dtor_or_conv_p,
17129                                         /*parenthesized_p=*/NULL,
17130                                         /*member_p=*/true);
17131
17132               /* If something went wrong parsing the declarator, make sure
17133                  that we at least consume some tokens.  */
17134               if (declarator == cp_error_declarator)
17135                 {
17136                   /* Skip to the end of the statement.  */
17137                   cp_parser_skip_to_end_of_statement (parser);
17138                   /* If the next token is not a semicolon, that is
17139                      probably because we just skipped over the body of
17140                      a function.  So, we consume a semicolon if
17141                      present, but do not issue an error message if it
17142                      is not present.  */
17143                   if (cp_lexer_next_token_is (parser->lexer,
17144                                               CPP_SEMICOLON))
17145                     cp_lexer_consume_token (parser->lexer);
17146                   return;
17147                 }
17148
17149               if (declares_class_or_enum & 2)
17150                 cp_parser_check_for_definition_in_return_type
17151                                             (declarator, decl_specifiers.type,
17152                                              decl_specifiers.type_location);
17153
17154               /* Look for an asm-specification.  */
17155               asm_specification = cp_parser_asm_specification_opt (parser);
17156               /* Look for attributes that apply to the declaration.  */
17157               attributes = cp_parser_attributes_opt (parser);
17158               /* Remember which attributes are prefix attributes and
17159                  which are not.  */
17160               first_attribute = attributes;
17161               /* Combine the attributes.  */
17162               attributes = chainon (prefix_attributes, attributes);
17163
17164               /* If it's an `=', then we have a constant-initializer or a
17165                  pure-specifier.  It is not correct to parse the
17166                  initializer before registering the member declaration
17167                  since the member declaration should be in scope while
17168                  its initializer is processed.  However, the rest of the
17169                  front end does not yet provide an interface that allows
17170                  us to handle this correctly.  */
17171               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17172                 {
17173                   /* In [class.mem]:
17174
17175                      A pure-specifier shall be used only in the declaration of
17176                      a virtual function.
17177
17178                      A member-declarator can contain a constant-initializer
17179                      only if it declares a static member of integral or
17180                      enumeration type.
17181
17182                      Therefore, if the DECLARATOR is for a function, we look
17183                      for a pure-specifier; otherwise, we look for a
17184                      constant-initializer.  When we call `grokfield', it will
17185                      perform more stringent semantics checks.  */
17186                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17187                   if (function_declarator_p (declarator))
17188                     initializer = cp_parser_pure_specifier (parser);
17189                   else
17190                     /* Parse the initializer.  */
17191                     initializer = cp_parser_constant_initializer (parser);
17192                 }
17193               /* Otherwise, there is no initializer.  */
17194               else
17195                 initializer = NULL_TREE;
17196
17197               /* See if we are probably looking at a function
17198                  definition.  We are certainly not looking at a
17199                  member-declarator.  Calling `grokfield' has
17200                  side-effects, so we must not do it unless we are sure
17201                  that we are looking at a member-declarator.  */
17202               if (cp_parser_token_starts_function_definition_p
17203                   (cp_lexer_peek_token (parser->lexer)))
17204                 {
17205                   /* The grammar does not allow a pure-specifier to be
17206                      used when a member function is defined.  (It is
17207                      possible that this fact is an oversight in the
17208                      standard, since a pure function may be defined
17209                      outside of the class-specifier.  */
17210                   if (initializer)
17211                     error_at (initializer_token_start->location,
17212                               "pure-specifier on function-definition");
17213                   decl = cp_parser_save_member_function_body (parser,
17214                                                               &decl_specifiers,
17215                                                               declarator,
17216                                                               attributes);
17217                   /* If the member was not a friend, declare it here.  */
17218                   if (!friend_p)
17219                     finish_member_declaration (decl);
17220                   /* Peek at the next token.  */
17221                   token = cp_lexer_peek_token (parser->lexer);
17222                   /* If the next token is a semicolon, consume it.  */
17223                   if (token->type == CPP_SEMICOLON)
17224                     cp_lexer_consume_token (parser->lexer);
17225                   return;
17226                 }
17227               else
17228                 if (declarator->kind == cdk_function)
17229                   declarator->id_loc = token->location;
17230                 /* Create the declaration.  */
17231                 decl = grokfield (declarator, &decl_specifiers,
17232                                   initializer, /*init_const_expr_p=*/true,
17233                                   asm_specification,
17234                                   attributes);
17235             }
17236
17237           /* Reset PREFIX_ATTRIBUTES.  */
17238           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17239             attributes = TREE_CHAIN (attributes);
17240           if (attributes)
17241             TREE_CHAIN (attributes) = NULL_TREE;
17242
17243           /* If there is any qualification still in effect, clear it
17244              now; we will be starting fresh with the next declarator.  */
17245           parser->scope = NULL_TREE;
17246           parser->qualifying_scope = NULL_TREE;
17247           parser->object_scope = NULL_TREE;
17248           /* If it's a `,', then there are more declarators.  */
17249           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17250             cp_lexer_consume_token (parser->lexer);
17251           /* If the next token isn't a `;', then we have a parse error.  */
17252           else if (cp_lexer_next_token_is_not (parser->lexer,
17253                                                CPP_SEMICOLON))
17254             {
17255               cp_parser_error (parser, "expected %<;%>");
17256               /* Skip tokens until we find a `;'.  */
17257               cp_parser_skip_to_end_of_statement (parser);
17258
17259               break;
17260             }
17261
17262           if (decl)
17263             {
17264               /* Add DECL to the list of members.  */
17265               if (!friend_p)
17266                 finish_member_declaration (decl);
17267
17268               if (TREE_CODE (decl) == FUNCTION_DECL)
17269                 cp_parser_save_default_args (parser, decl);
17270             }
17271         }
17272     }
17273
17274   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17275 }
17276
17277 /* Parse a pure-specifier.
17278
17279    pure-specifier:
17280      = 0
17281
17282    Returns INTEGER_ZERO_NODE if a pure specifier is found.
17283    Otherwise, ERROR_MARK_NODE is returned.  */
17284
17285 static tree
17286 cp_parser_pure_specifier (cp_parser* parser)
17287 {
17288   cp_token *token;
17289
17290   /* Look for the `=' token.  */
17291   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17292     return error_mark_node;
17293   /* Look for the `0' token.  */
17294   token = cp_lexer_peek_token (parser->lexer);
17295
17296   if (token->type == CPP_EOF
17297       || token->type == CPP_PRAGMA_EOL)
17298     return error_mark_node;
17299
17300   cp_lexer_consume_token (parser->lexer);
17301
17302   /* Accept = default or = delete in c++0x mode.  */
17303   if (token->keyword == RID_DEFAULT
17304       || token->keyword == RID_DELETE)
17305     {
17306       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17307       return token->u.value;
17308     }
17309
17310   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
17311   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17312     {
17313       cp_parser_error (parser,
17314                        "invalid pure specifier (only %<= 0%> is allowed)");
17315       cp_parser_skip_to_end_of_statement (parser);
17316       return error_mark_node;
17317     }
17318   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17319     {
17320       error_at (token->location, "templates may not be %<virtual%>");
17321       return error_mark_node;
17322     }
17323
17324   return integer_zero_node;
17325 }
17326
17327 /* Parse a constant-initializer.
17328
17329    constant-initializer:
17330      = constant-expression
17331
17332    Returns a representation of the constant-expression.  */
17333
17334 static tree
17335 cp_parser_constant_initializer (cp_parser* parser)
17336 {
17337   /* Look for the `=' token.  */
17338   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17339     return error_mark_node;
17340
17341   /* It is invalid to write:
17342
17343        struct S { static const int i = { 7 }; };
17344
17345      */
17346   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17347     {
17348       cp_parser_error (parser,
17349                        "a brace-enclosed initializer is not allowed here");
17350       /* Consume the opening brace.  */
17351       cp_lexer_consume_token (parser->lexer);
17352       /* Skip the initializer.  */
17353       cp_parser_skip_to_closing_brace (parser);
17354       /* Look for the trailing `}'.  */
17355       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17356
17357       return error_mark_node;
17358     }
17359
17360   return cp_parser_constant_expression (parser,
17361                                         /*allow_non_constant=*/false,
17362                                         NULL);
17363 }
17364
17365 /* Derived classes [gram.class.derived] */
17366
17367 /* Parse a base-clause.
17368
17369    base-clause:
17370      : base-specifier-list
17371
17372    base-specifier-list:
17373      base-specifier ... [opt]
17374      base-specifier-list , base-specifier ... [opt]
17375
17376    Returns a TREE_LIST representing the base-classes, in the order in
17377    which they were declared.  The representation of each node is as
17378    described by cp_parser_base_specifier.
17379
17380    In the case that no bases are specified, this function will return
17381    NULL_TREE, not ERROR_MARK_NODE.  */
17382
17383 static tree
17384 cp_parser_base_clause (cp_parser* parser)
17385 {
17386   tree bases = NULL_TREE;
17387
17388   /* Look for the `:' that begins the list.  */
17389   cp_parser_require (parser, CPP_COLON, RT_COLON);
17390
17391   /* Scan the base-specifier-list.  */
17392   while (true)
17393     {
17394       cp_token *token;
17395       tree base;
17396       bool pack_expansion_p = false;
17397
17398       /* Look for the base-specifier.  */
17399       base = cp_parser_base_specifier (parser);
17400       /* Look for the (optional) ellipsis. */
17401       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17402         {
17403           /* Consume the `...'. */
17404           cp_lexer_consume_token (parser->lexer);
17405
17406           pack_expansion_p = true;
17407         }
17408
17409       /* Add BASE to the front of the list.  */
17410       if (base != error_mark_node)
17411         {
17412           if (pack_expansion_p)
17413             /* Make this a pack expansion type. */
17414             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17415           
17416
17417           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17418             {
17419               TREE_CHAIN (base) = bases;
17420               bases = base;
17421             }
17422         }
17423       /* Peek at the next token.  */
17424       token = cp_lexer_peek_token (parser->lexer);
17425       /* If it's not a comma, then the list is complete.  */
17426       if (token->type != CPP_COMMA)
17427         break;
17428       /* Consume the `,'.  */
17429       cp_lexer_consume_token (parser->lexer);
17430     }
17431
17432   /* PARSER->SCOPE may still be non-NULL at this point, if the last
17433      base class had a qualified name.  However, the next name that
17434      appears is certainly not qualified.  */
17435   parser->scope = NULL_TREE;
17436   parser->qualifying_scope = NULL_TREE;
17437   parser->object_scope = NULL_TREE;
17438
17439   return nreverse (bases);
17440 }
17441
17442 /* Parse a base-specifier.
17443
17444    base-specifier:
17445      :: [opt] nested-name-specifier [opt] class-name
17446      virtual access-specifier [opt] :: [opt] nested-name-specifier
17447        [opt] class-name
17448      access-specifier virtual [opt] :: [opt] nested-name-specifier
17449        [opt] class-name
17450
17451    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17452    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17453    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17454    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17455
17456 static tree
17457 cp_parser_base_specifier (cp_parser* parser)
17458 {
17459   cp_token *token;
17460   bool done = false;
17461   bool virtual_p = false;
17462   bool duplicate_virtual_error_issued_p = false;
17463   bool duplicate_access_error_issued_p = false;
17464   bool class_scope_p, template_p;
17465   tree access = access_default_node;
17466   tree type;
17467
17468   /* Process the optional `virtual' and `access-specifier'.  */
17469   while (!done)
17470     {
17471       /* Peek at the next token.  */
17472       token = cp_lexer_peek_token (parser->lexer);
17473       /* Process `virtual'.  */
17474       switch (token->keyword)
17475         {
17476         case RID_VIRTUAL:
17477           /* If `virtual' appears more than once, issue an error.  */
17478           if (virtual_p && !duplicate_virtual_error_issued_p)
17479             {
17480               cp_parser_error (parser,
17481                                "%<virtual%> specified more than once in base-specified");
17482               duplicate_virtual_error_issued_p = true;
17483             }
17484
17485           virtual_p = true;
17486
17487           /* Consume the `virtual' token.  */
17488           cp_lexer_consume_token (parser->lexer);
17489
17490           break;
17491
17492         case RID_PUBLIC:
17493         case RID_PROTECTED:
17494         case RID_PRIVATE:
17495           /* If more than one access specifier appears, issue an
17496              error.  */
17497           if (access != access_default_node
17498               && !duplicate_access_error_issued_p)
17499             {
17500               cp_parser_error (parser,
17501                                "more than one access specifier in base-specified");
17502               duplicate_access_error_issued_p = true;
17503             }
17504
17505           access = ridpointers[(int) token->keyword];
17506
17507           /* Consume the access-specifier.  */
17508           cp_lexer_consume_token (parser->lexer);
17509
17510           break;
17511
17512         default:
17513           done = true;
17514           break;
17515         }
17516     }
17517   /* It is not uncommon to see programs mechanically, erroneously, use
17518      the 'typename' keyword to denote (dependent) qualified types
17519      as base classes.  */
17520   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17521     {
17522       token = cp_lexer_peek_token (parser->lexer);
17523       if (!processing_template_decl)
17524         error_at (token->location,
17525                   "keyword %<typename%> not allowed outside of templates");
17526       else
17527         error_at (token->location,
17528                   "keyword %<typename%> not allowed in this context "
17529                   "(the base class is implicitly a type)");
17530       cp_lexer_consume_token (parser->lexer);
17531     }
17532
17533   /* Look for the optional `::' operator.  */
17534   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17535   /* Look for the nested-name-specifier.  The simplest way to
17536      implement:
17537
17538        [temp.res]
17539
17540        The keyword `typename' is not permitted in a base-specifier or
17541        mem-initializer; in these contexts a qualified name that
17542        depends on a template-parameter is implicitly assumed to be a
17543        type name.
17544
17545      is to pretend that we have seen the `typename' keyword at this
17546      point.  */
17547   cp_parser_nested_name_specifier_opt (parser,
17548                                        /*typename_keyword_p=*/true,
17549                                        /*check_dependency_p=*/true,
17550                                        typename_type,
17551                                        /*is_declaration=*/true);
17552   /* If the base class is given by a qualified name, assume that names
17553      we see are type names or templates, as appropriate.  */
17554   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17555   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17556
17557   /* Finally, look for the class-name.  */
17558   type = cp_parser_class_name (parser,
17559                                class_scope_p,
17560                                template_p,
17561                                typename_type,
17562                                /*check_dependency_p=*/true,
17563                                /*class_head_p=*/false,
17564                                /*is_declaration=*/true);
17565
17566   if (type == error_mark_node)
17567     return error_mark_node;
17568
17569   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17570 }
17571
17572 /* Exception handling [gram.exception] */
17573
17574 /* Parse an (optional) exception-specification.
17575
17576    exception-specification:
17577      throw ( type-id-list [opt] )
17578
17579    Returns a TREE_LIST representing the exception-specification.  The
17580    TREE_VALUE of each node is a type.  */
17581
17582 static tree
17583 cp_parser_exception_specification_opt (cp_parser* parser)
17584 {
17585   cp_token *token;
17586   tree type_id_list;
17587   const char *saved_message;
17588
17589   /* Peek at the next token.  */
17590   token = cp_lexer_peek_token (parser->lexer);
17591
17592   /* Is it a noexcept-specification?  */
17593   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
17594     {
17595       tree expr;
17596       cp_lexer_consume_token (parser->lexer);
17597
17598       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
17599         {
17600           cp_lexer_consume_token (parser->lexer);
17601
17602           /* Types may not be defined in an exception-specification.  */
17603           saved_message = parser->type_definition_forbidden_message;
17604           parser->type_definition_forbidden_message
17605             = G_("types may not be defined in an exception-specification");
17606
17607           expr = cp_parser_constant_expression (parser, false, NULL);
17608
17609           /* Restore the saved message.  */
17610           parser->type_definition_forbidden_message = saved_message;
17611
17612           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17613         }
17614       else
17615         expr = boolean_true_node;
17616
17617       return build_noexcept_spec (expr, tf_warning_or_error);
17618     }
17619
17620   /* If it's not `throw', then there's no exception-specification.  */
17621   if (!cp_parser_is_keyword (token, RID_THROW))
17622     return NULL_TREE;
17623
17624 #if 0
17625   /* Enable this once a lot of code has transitioned to noexcept?  */
17626   if (cxx_dialect == cxx0x && !in_system_header)
17627     warning (OPT_Wdeprecated, "dynamic exception specifications are "
17628              "deprecated in C++0x; use %<noexcept%> instead.");
17629 #endif
17630
17631   /* Consume the `throw'.  */
17632   cp_lexer_consume_token (parser->lexer);
17633
17634   /* Look for the `('.  */
17635   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17636
17637   /* Peek at the next token.  */
17638   token = cp_lexer_peek_token (parser->lexer);
17639   /* If it's not a `)', then there is a type-id-list.  */
17640   if (token->type != CPP_CLOSE_PAREN)
17641     {
17642       /* Types may not be defined in an exception-specification.  */
17643       saved_message = parser->type_definition_forbidden_message;
17644       parser->type_definition_forbidden_message
17645         = G_("types may not be defined in an exception-specification");
17646       /* Parse the type-id-list.  */
17647       type_id_list = cp_parser_type_id_list (parser);
17648       /* Restore the saved message.  */
17649       parser->type_definition_forbidden_message = saved_message;
17650     }
17651   else
17652     type_id_list = empty_except_spec;
17653
17654   /* Look for the `)'.  */
17655   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17656
17657   return type_id_list;
17658 }
17659
17660 /* Parse an (optional) type-id-list.
17661
17662    type-id-list:
17663      type-id ... [opt]
17664      type-id-list , type-id ... [opt]
17665
17666    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17667    in the order that the types were presented.  */
17668
17669 static tree
17670 cp_parser_type_id_list (cp_parser* parser)
17671 {
17672   tree types = NULL_TREE;
17673
17674   while (true)
17675     {
17676       cp_token *token;
17677       tree type;
17678
17679       /* Get the next type-id.  */
17680       type = cp_parser_type_id (parser);
17681       /* Parse the optional ellipsis. */
17682       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17683         {
17684           /* Consume the `...'. */
17685           cp_lexer_consume_token (parser->lexer);
17686
17687           /* Turn the type into a pack expansion expression. */
17688           type = make_pack_expansion (type);
17689         }
17690       /* Add it to the list.  */
17691       types = add_exception_specifier (types, type, /*complain=*/1);
17692       /* Peek at the next token.  */
17693       token = cp_lexer_peek_token (parser->lexer);
17694       /* If it is not a `,', we are done.  */
17695       if (token->type != CPP_COMMA)
17696         break;
17697       /* Consume the `,'.  */
17698       cp_lexer_consume_token (parser->lexer);
17699     }
17700
17701   return nreverse (types);
17702 }
17703
17704 /* Parse a try-block.
17705
17706    try-block:
17707      try compound-statement handler-seq  */
17708
17709 static tree
17710 cp_parser_try_block (cp_parser* parser)
17711 {
17712   tree try_block;
17713
17714   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
17715   try_block = begin_try_block ();
17716   cp_parser_compound_statement (parser, NULL, true);
17717   finish_try_block (try_block);
17718   cp_parser_handler_seq (parser);
17719   finish_handler_sequence (try_block);
17720
17721   return try_block;
17722 }
17723
17724 /* Parse a function-try-block.
17725
17726    function-try-block:
17727      try ctor-initializer [opt] function-body handler-seq  */
17728
17729 static bool
17730 cp_parser_function_try_block (cp_parser* parser)
17731 {
17732   tree compound_stmt;
17733   tree try_block;
17734   bool ctor_initializer_p;
17735
17736   /* Look for the `try' keyword.  */
17737   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
17738     return false;
17739   /* Let the rest of the front end know where we are.  */
17740   try_block = begin_function_try_block (&compound_stmt);
17741   /* Parse the function-body.  */
17742   ctor_initializer_p
17743     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17744   /* We're done with the `try' part.  */
17745   finish_function_try_block (try_block);
17746   /* Parse the handlers.  */
17747   cp_parser_handler_seq (parser);
17748   /* We're done with the handlers.  */
17749   finish_function_handler_sequence (try_block, compound_stmt);
17750
17751   return ctor_initializer_p;
17752 }
17753
17754 /* Parse a handler-seq.
17755
17756    handler-seq:
17757      handler handler-seq [opt]  */
17758
17759 static void
17760 cp_parser_handler_seq (cp_parser* parser)
17761 {
17762   while (true)
17763     {
17764       cp_token *token;
17765
17766       /* Parse the handler.  */
17767       cp_parser_handler (parser);
17768       /* Peek at the next token.  */
17769       token = cp_lexer_peek_token (parser->lexer);
17770       /* If it's not `catch' then there are no more handlers.  */
17771       if (!cp_parser_is_keyword (token, RID_CATCH))
17772         break;
17773     }
17774 }
17775
17776 /* Parse a handler.
17777
17778    handler:
17779      catch ( exception-declaration ) compound-statement  */
17780
17781 static void
17782 cp_parser_handler (cp_parser* parser)
17783 {
17784   tree handler;
17785   tree declaration;
17786
17787   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
17788   handler = begin_handler ();
17789   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17790   declaration = cp_parser_exception_declaration (parser);
17791   finish_handler_parms (declaration, handler);
17792   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17793   cp_parser_compound_statement (parser, NULL, false);
17794   finish_handler (handler);
17795 }
17796
17797 /* Parse an exception-declaration.
17798
17799    exception-declaration:
17800      type-specifier-seq declarator
17801      type-specifier-seq abstract-declarator
17802      type-specifier-seq
17803      ...
17804
17805    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17806    ellipsis variant is used.  */
17807
17808 static tree
17809 cp_parser_exception_declaration (cp_parser* parser)
17810 {
17811   cp_decl_specifier_seq type_specifiers;
17812   cp_declarator *declarator;
17813   const char *saved_message;
17814
17815   /* If it's an ellipsis, it's easy to handle.  */
17816   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17817     {
17818       /* Consume the `...' token.  */
17819       cp_lexer_consume_token (parser->lexer);
17820       return NULL_TREE;
17821     }
17822
17823   /* Types may not be defined in exception-declarations.  */
17824   saved_message = parser->type_definition_forbidden_message;
17825   parser->type_definition_forbidden_message
17826     = G_("types may not be defined in exception-declarations");
17827
17828   /* Parse the type-specifier-seq.  */
17829   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17830                                 /*is_trailing_return=*/false,
17831                                 &type_specifiers);
17832   /* If it's a `)', then there is no declarator.  */
17833   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17834     declarator = NULL;
17835   else
17836     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17837                                        /*ctor_dtor_or_conv_p=*/NULL,
17838                                        /*parenthesized_p=*/NULL,
17839                                        /*member_p=*/false);
17840
17841   /* Restore the saved message.  */
17842   parser->type_definition_forbidden_message = saved_message;
17843
17844   if (!type_specifiers.any_specifiers_p)
17845     return error_mark_node;
17846
17847   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17848 }
17849
17850 /* Parse a throw-expression.
17851
17852    throw-expression:
17853      throw assignment-expression [opt]
17854
17855    Returns a THROW_EXPR representing the throw-expression.  */
17856
17857 static tree
17858 cp_parser_throw_expression (cp_parser* parser)
17859 {
17860   tree expression;
17861   cp_token* token;
17862
17863   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
17864   token = cp_lexer_peek_token (parser->lexer);
17865   /* Figure out whether or not there is an assignment-expression
17866      following the "throw" keyword.  */
17867   if (token->type == CPP_COMMA
17868       || token->type == CPP_SEMICOLON
17869       || token->type == CPP_CLOSE_PAREN
17870       || token->type == CPP_CLOSE_SQUARE
17871       || token->type == CPP_CLOSE_BRACE
17872       || token->type == CPP_COLON)
17873     expression = NULL_TREE;
17874   else
17875     expression = cp_parser_assignment_expression (parser,
17876                                                   /*cast_p=*/false, NULL);
17877
17878   return build_throw (expression);
17879 }
17880
17881 /* GNU Extensions */
17882
17883 /* Parse an (optional) asm-specification.
17884
17885    asm-specification:
17886      asm ( string-literal )
17887
17888    If the asm-specification is present, returns a STRING_CST
17889    corresponding to the string-literal.  Otherwise, returns
17890    NULL_TREE.  */
17891
17892 static tree
17893 cp_parser_asm_specification_opt (cp_parser* parser)
17894 {
17895   cp_token *token;
17896   tree asm_specification;
17897
17898   /* Peek at the next token.  */
17899   token = cp_lexer_peek_token (parser->lexer);
17900   /* If the next token isn't the `asm' keyword, then there's no
17901      asm-specification.  */
17902   if (!cp_parser_is_keyword (token, RID_ASM))
17903     return NULL_TREE;
17904
17905   /* Consume the `asm' token.  */
17906   cp_lexer_consume_token (parser->lexer);
17907   /* Look for the `('.  */
17908   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17909
17910   /* Look for the string-literal.  */
17911   asm_specification = cp_parser_string_literal (parser, false, false);
17912
17913   /* Look for the `)'.  */
17914   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17915
17916   return asm_specification;
17917 }
17918
17919 /* Parse an asm-operand-list.
17920
17921    asm-operand-list:
17922      asm-operand
17923      asm-operand-list , asm-operand
17924
17925    asm-operand:
17926      string-literal ( expression )
17927      [ string-literal ] string-literal ( expression )
17928
17929    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17930    each node is the expression.  The TREE_PURPOSE is itself a
17931    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17932    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17933    is a STRING_CST for the string literal before the parenthesis. Returns
17934    ERROR_MARK_NODE if any of the operands are invalid.  */
17935
17936 static tree
17937 cp_parser_asm_operand_list (cp_parser* parser)
17938 {
17939   tree asm_operands = NULL_TREE;
17940   bool invalid_operands = false;
17941
17942   while (true)
17943     {
17944       tree string_literal;
17945       tree expression;
17946       tree name;
17947
17948       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17949         {
17950           /* Consume the `[' token.  */
17951           cp_lexer_consume_token (parser->lexer);
17952           /* Read the operand name.  */
17953           name = cp_parser_identifier (parser);
17954           if (name != error_mark_node)
17955             name = build_string (IDENTIFIER_LENGTH (name),
17956                                  IDENTIFIER_POINTER (name));
17957           /* Look for the closing `]'.  */
17958           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17959         }
17960       else
17961         name = NULL_TREE;
17962       /* Look for the string-literal.  */
17963       string_literal = cp_parser_string_literal (parser, false, false);
17964
17965       /* Look for the `('.  */
17966       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17967       /* Parse the expression.  */
17968       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17969       /* Look for the `)'.  */
17970       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17971
17972       if (name == error_mark_node 
17973           || string_literal == error_mark_node 
17974           || expression == error_mark_node)
17975         invalid_operands = true;
17976
17977       /* Add this operand to the list.  */
17978       asm_operands = tree_cons (build_tree_list (name, string_literal),
17979                                 expression,
17980                                 asm_operands);
17981       /* If the next token is not a `,', there are no more
17982          operands.  */
17983       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17984         break;
17985       /* Consume the `,'.  */
17986       cp_lexer_consume_token (parser->lexer);
17987     }
17988
17989   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17990 }
17991
17992 /* Parse an asm-clobber-list.
17993
17994    asm-clobber-list:
17995      string-literal
17996      asm-clobber-list , string-literal
17997
17998    Returns a TREE_LIST, indicating the clobbers in the order that they
17999    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18000
18001 static tree
18002 cp_parser_asm_clobber_list (cp_parser* parser)
18003 {
18004   tree clobbers = NULL_TREE;
18005
18006   while (true)
18007     {
18008       tree string_literal;
18009
18010       /* Look for the string literal.  */
18011       string_literal = cp_parser_string_literal (parser, false, false);
18012       /* Add it to the list.  */
18013       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18014       /* If the next token is not a `,', then the list is
18015          complete.  */
18016       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18017         break;
18018       /* Consume the `,' token.  */
18019       cp_lexer_consume_token (parser->lexer);
18020     }
18021
18022   return clobbers;
18023 }
18024
18025 /* Parse an asm-label-list.
18026
18027    asm-label-list:
18028      identifier
18029      asm-label-list , identifier
18030
18031    Returns a TREE_LIST, indicating the labels in the order that they
18032    appeared.  The TREE_VALUE of each node is a label.  */
18033
18034 static tree
18035 cp_parser_asm_label_list (cp_parser* parser)
18036 {
18037   tree labels = NULL_TREE;
18038
18039   while (true)
18040     {
18041       tree identifier, label, name;
18042
18043       /* Look for the identifier.  */
18044       identifier = cp_parser_identifier (parser);
18045       if (!error_operand_p (identifier))
18046         {
18047           label = lookup_label (identifier);
18048           if (TREE_CODE (label) == LABEL_DECL)
18049             {
18050               TREE_USED (label) = 1;
18051               check_goto (label);
18052               name = build_string (IDENTIFIER_LENGTH (identifier),
18053                                    IDENTIFIER_POINTER (identifier));
18054               labels = tree_cons (name, label, labels);
18055             }
18056         }
18057       /* If the next token is not a `,', then the list is
18058          complete.  */
18059       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18060         break;
18061       /* Consume the `,' token.  */
18062       cp_lexer_consume_token (parser->lexer);
18063     }
18064
18065   return nreverse (labels);
18066 }
18067
18068 /* Parse an (optional) series of attributes.
18069
18070    attributes:
18071      attributes attribute
18072
18073    attribute:
18074      __attribute__ (( attribute-list [opt] ))
18075
18076    The return value is as for cp_parser_attribute_list.  */
18077
18078 static tree
18079 cp_parser_attributes_opt (cp_parser* parser)
18080 {
18081   tree attributes = NULL_TREE;
18082
18083   while (true)
18084     {
18085       cp_token *token;
18086       tree attribute_list;
18087
18088       /* Peek at the next token.  */
18089       token = cp_lexer_peek_token (parser->lexer);
18090       /* If it's not `__attribute__', then we're done.  */
18091       if (token->keyword != RID_ATTRIBUTE)
18092         break;
18093
18094       /* Consume the `__attribute__' keyword.  */
18095       cp_lexer_consume_token (parser->lexer);
18096       /* Look for the two `(' tokens.  */
18097       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18098       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18099
18100       /* Peek at the next token.  */
18101       token = cp_lexer_peek_token (parser->lexer);
18102       if (token->type != CPP_CLOSE_PAREN)
18103         /* Parse the attribute-list.  */
18104         attribute_list = cp_parser_attribute_list (parser);
18105       else
18106         /* If the next token is a `)', then there is no attribute
18107            list.  */
18108         attribute_list = NULL;
18109
18110       /* Look for the two `)' tokens.  */
18111       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18112       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18113
18114       /* Add these new attributes to the list.  */
18115       attributes = chainon (attributes, attribute_list);
18116     }
18117
18118   return attributes;
18119 }
18120
18121 /* Parse an attribute-list.
18122
18123    attribute-list:
18124      attribute
18125      attribute-list , attribute
18126
18127    attribute:
18128      identifier
18129      identifier ( identifier )
18130      identifier ( identifier , expression-list )
18131      identifier ( expression-list )
18132
18133    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18134    to an attribute.  The TREE_PURPOSE of each node is the identifier
18135    indicating which attribute is in use.  The TREE_VALUE represents
18136    the arguments, if any.  */
18137
18138 static tree
18139 cp_parser_attribute_list (cp_parser* parser)
18140 {
18141   tree attribute_list = NULL_TREE;
18142   bool save_translate_strings_p = parser->translate_strings_p;
18143
18144   parser->translate_strings_p = false;
18145   while (true)
18146     {
18147       cp_token *token;
18148       tree identifier;
18149       tree attribute;
18150
18151       /* Look for the identifier.  We also allow keywords here; for
18152          example `__attribute__ ((const))' is legal.  */
18153       token = cp_lexer_peek_token (parser->lexer);
18154       if (token->type == CPP_NAME
18155           || token->type == CPP_KEYWORD)
18156         {
18157           tree arguments = NULL_TREE;
18158
18159           /* Consume the token.  */
18160           token = cp_lexer_consume_token (parser->lexer);
18161
18162           /* Save away the identifier that indicates which attribute
18163              this is.  */
18164           identifier = (token->type == CPP_KEYWORD) 
18165             /* For keywords, use the canonical spelling, not the
18166                parsed identifier.  */
18167             ? ridpointers[(int) token->keyword]
18168             : token->u.value;
18169           
18170           attribute = build_tree_list (identifier, NULL_TREE);
18171
18172           /* Peek at the next token.  */
18173           token = cp_lexer_peek_token (parser->lexer);
18174           /* If it's an `(', then parse the attribute arguments.  */
18175           if (token->type == CPP_OPEN_PAREN)
18176             {
18177               VEC(tree,gc) *vec;
18178               int attr_flag = (attribute_takes_identifier_p (identifier)
18179                                ? id_attr : normal_attr);
18180               vec = cp_parser_parenthesized_expression_list
18181                     (parser, attr_flag, /*cast_p=*/false,
18182                      /*allow_expansion_p=*/false,
18183                      /*non_constant_p=*/NULL);
18184               if (vec == NULL)
18185                 arguments = error_mark_node;
18186               else
18187                 {
18188                   arguments = build_tree_list_vec (vec);
18189                   release_tree_vector (vec);
18190                 }
18191               /* Save the arguments away.  */
18192               TREE_VALUE (attribute) = arguments;
18193             }
18194
18195           if (arguments != error_mark_node)
18196             {
18197               /* Add this attribute to the list.  */
18198               TREE_CHAIN (attribute) = attribute_list;
18199               attribute_list = attribute;
18200             }
18201
18202           token = cp_lexer_peek_token (parser->lexer);
18203         }
18204       /* Now, look for more attributes.  If the next token isn't a
18205          `,', we're done.  */
18206       if (token->type != CPP_COMMA)
18207         break;
18208
18209       /* Consume the comma and keep going.  */
18210       cp_lexer_consume_token (parser->lexer);
18211     }
18212   parser->translate_strings_p = save_translate_strings_p;
18213
18214   /* We built up the list in reverse order.  */
18215   return nreverse (attribute_list);
18216 }
18217
18218 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
18219    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
18220    current value of the PEDANTIC flag, regardless of whether or not
18221    the `__extension__' keyword is present.  The caller is responsible
18222    for restoring the value of the PEDANTIC flag.  */
18223
18224 static bool
18225 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18226 {
18227   /* Save the old value of the PEDANTIC flag.  */
18228   *saved_pedantic = pedantic;
18229
18230   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18231     {
18232       /* Consume the `__extension__' token.  */
18233       cp_lexer_consume_token (parser->lexer);
18234       /* We're not being pedantic while the `__extension__' keyword is
18235          in effect.  */
18236       pedantic = 0;
18237
18238       return true;
18239     }
18240
18241   return false;
18242 }
18243
18244 /* Parse a label declaration.
18245
18246    label-declaration:
18247      __label__ label-declarator-seq ;
18248
18249    label-declarator-seq:
18250      identifier , label-declarator-seq
18251      identifier  */
18252
18253 static void
18254 cp_parser_label_declaration (cp_parser* parser)
18255 {
18256   /* Look for the `__label__' keyword.  */
18257   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18258
18259   while (true)
18260     {
18261       tree identifier;
18262
18263       /* Look for an identifier.  */
18264       identifier = cp_parser_identifier (parser);
18265       /* If we failed, stop.  */
18266       if (identifier == error_mark_node)
18267         break;
18268       /* Declare it as a label.  */
18269       finish_label_decl (identifier);
18270       /* If the next token is a `;', stop.  */
18271       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18272         break;
18273       /* Look for the `,' separating the label declarations.  */
18274       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18275     }
18276
18277   /* Look for the final `;'.  */
18278   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18279 }
18280
18281 /* Support Functions */
18282
18283 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18284    NAME should have one of the representations used for an
18285    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18286    is returned.  If PARSER->SCOPE is a dependent type, then a
18287    SCOPE_REF is returned.
18288
18289    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18290    returned; the name was already resolved when the TEMPLATE_ID_EXPR
18291    was formed.  Abstractly, such entities should not be passed to this
18292    function, because they do not need to be looked up, but it is
18293    simpler to check for this special case here, rather than at the
18294    call-sites.
18295
18296    In cases not explicitly covered above, this function returns a
18297    DECL, OVERLOAD, or baselink representing the result of the lookup.
18298    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18299    is returned.
18300
18301    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18302    (e.g., "struct") that was used.  In that case bindings that do not
18303    refer to types are ignored.
18304
18305    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18306    ignored.
18307
18308    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18309    are ignored.
18310
18311    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18312    types.
18313
18314    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18315    TREE_LIST of candidates if name-lookup results in an ambiguity, and
18316    NULL_TREE otherwise.  */
18317
18318 static tree
18319 cp_parser_lookup_name (cp_parser *parser, tree name,
18320                        enum tag_types tag_type,
18321                        bool is_template,
18322                        bool is_namespace,
18323                        bool check_dependency,
18324                        tree *ambiguous_decls,
18325                        location_t name_location)
18326 {
18327   int flags = 0;
18328   tree decl;
18329   tree object_type = parser->context->object_type;
18330
18331   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18332     flags |= LOOKUP_COMPLAIN;
18333
18334   /* Assume that the lookup will be unambiguous.  */
18335   if (ambiguous_decls)
18336     *ambiguous_decls = NULL_TREE;
18337
18338   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18339      no longer valid.  Note that if we are parsing tentatively, and
18340      the parse fails, OBJECT_TYPE will be automatically restored.  */
18341   parser->context->object_type = NULL_TREE;
18342
18343   if (name == error_mark_node)
18344     return error_mark_node;
18345
18346   /* A template-id has already been resolved; there is no lookup to
18347      do.  */
18348   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18349     return name;
18350   if (BASELINK_P (name))
18351     {
18352       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18353                   == TEMPLATE_ID_EXPR);
18354       return name;
18355     }
18356
18357   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
18358      it should already have been checked to make sure that the name
18359      used matches the type being destroyed.  */
18360   if (TREE_CODE (name) == BIT_NOT_EXPR)
18361     {
18362       tree type;
18363
18364       /* Figure out to which type this destructor applies.  */
18365       if (parser->scope)
18366         type = parser->scope;
18367       else if (object_type)
18368         type = object_type;
18369       else
18370         type = current_class_type;
18371       /* If that's not a class type, there is no destructor.  */
18372       if (!type || !CLASS_TYPE_P (type))
18373         return error_mark_node;
18374       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18375         lazily_declare_fn (sfk_destructor, type);
18376       if (!CLASSTYPE_DESTRUCTORS (type))
18377           return error_mark_node;
18378       /* If it was a class type, return the destructor.  */
18379       return CLASSTYPE_DESTRUCTORS (type);
18380     }
18381
18382   /* By this point, the NAME should be an ordinary identifier.  If
18383      the id-expression was a qualified name, the qualifying scope is
18384      stored in PARSER->SCOPE at this point.  */
18385   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18386
18387   /* Perform the lookup.  */
18388   if (parser->scope)
18389     {
18390       bool dependent_p;
18391
18392       if (parser->scope == error_mark_node)
18393         return error_mark_node;
18394
18395       /* If the SCOPE is dependent, the lookup must be deferred until
18396          the template is instantiated -- unless we are explicitly
18397          looking up names in uninstantiated templates.  Even then, we
18398          cannot look up the name if the scope is not a class type; it
18399          might, for example, be a template type parameter.  */
18400       dependent_p = (TYPE_P (parser->scope)
18401                      && dependent_scope_p (parser->scope));
18402       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18403           && dependent_p)
18404         /* Defer lookup.  */
18405         decl = error_mark_node;
18406       else
18407         {
18408           tree pushed_scope = NULL_TREE;
18409
18410           /* If PARSER->SCOPE is a dependent type, then it must be a
18411              class type, and we must not be checking dependencies;
18412              otherwise, we would have processed this lookup above.  So
18413              that PARSER->SCOPE is not considered a dependent base by
18414              lookup_member, we must enter the scope here.  */
18415           if (dependent_p)
18416             pushed_scope = push_scope (parser->scope);
18417
18418           /* If the PARSER->SCOPE is a template specialization, it
18419              may be instantiated during name lookup.  In that case,
18420              errors may be issued.  Even if we rollback the current
18421              tentative parse, those errors are valid.  */
18422           decl = lookup_qualified_name (parser->scope, name,
18423                                         tag_type != none_type,
18424                                         /*complain=*/true);
18425
18426           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18427              lookup result and the nested-name-specifier nominates a class C:
18428                * if the name specified after the nested-name-specifier, when
18429                looked up in C, is the injected-class-name of C (Clause 9), or
18430                * if the name specified after the nested-name-specifier is the
18431                same as the identifier or the simple-template-id's template-
18432                name in the last component of the nested-name-specifier,
18433              the name is instead considered to name the constructor of
18434              class C. [ Note: for example, the constructor is not an
18435              acceptable lookup result in an elaborated-type-specifier so
18436              the constructor would not be used in place of the
18437              injected-class-name. --end note ] Such a constructor name
18438              shall be used only in the declarator-id of a declaration that
18439              names a constructor or in a using-declaration.  */
18440           if (tag_type == none_type
18441               && DECL_SELF_REFERENCE_P (decl)
18442               && same_type_p (DECL_CONTEXT (decl), parser->scope))
18443             decl = lookup_qualified_name (parser->scope, ctor_identifier,
18444                                           tag_type != none_type,
18445                                           /*complain=*/true);
18446
18447           /* If we have a single function from a using decl, pull it out.  */
18448           if (TREE_CODE (decl) == OVERLOAD
18449               && !really_overloaded_fn (decl))
18450             decl = OVL_FUNCTION (decl);
18451
18452           if (pushed_scope)
18453             pop_scope (pushed_scope);
18454         }
18455
18456       /* If the scope is a dependent type and either we deferred lookup or
18457          we did lookup but didn't find the name, rememeber the name.  */
18458       if (decl == error_mark_node && TYPE_P (parser->scope)
18459           && dependent_type_p (parser->scope))
18460         {
18461           if (tag_type)
18462             {
18463               tree type;
18464
18465               /* The resolution to Core Issue 180 says that `struct
18466                  A::B' should be considered a type-name, even if `A'
18467                  is dependent.  */
18468               type = make_typename_type (parser->scope, name, tag_type,
18469                                          /*complain=*/tf_error);
18470               decl = TYPE_NAME (type);
18471             }
18472           else if (is_template
18473                    && (cp_parser_next_token_ends_template_argument_p (parser)
18474                        || cp_lexer_next_token_is (parser->lexer,
18475                                                   CPP_CLOSE_PAREN)))
18476             decl = make_unbound_class_template (parser->scope,
18477                                                 name, NULL_TREE,
18478                                                 /*complain=*/tf_error);
18479           else
18480             decl = build_qualified_name (/*type=*/NULL_TREE,
18481                                          parser->scope, name,
18482                                          is_template);
18483         }
18484       parser->qualifying_scope = parser->scope;
18485       parser->object_scope = NULL_TREE;
18486     }
18487   else if (object_type)
18488     {
18489       tree object_decl = NULL_TREE;
18490       /* Look up the name in the scope of the OBJECT_TYPE, unless the
18491          OBJECT_TYPE is not a class.  */
18492       if (CLASS_TYPE_P (object_type))
18493         /* If the OBJECT_TYPE is a template specialization, it may
18494            be instantiated during name lookup.  In that case, errors
18495            may be issued.  Even if we rollback the current tentative
18496            parse, those errors are valid.  */
18497         object_decl = lookup_member (object_type,
18498                                      name,
18499                                      /*protect=*/0,
18500                                      tag_type != none_type);
18501       /* Look it up in the enclosing context, too.  */
18502       decl = lookup_name_real (name, tag_type != none_type,
18503                                /*nonclass=*/0,
18504                                /*block_p=*/true, is_namespace, flags);
18505       parser->object_scope = object_type;
18506       parser->qualifying_scope = NULL_TREE;
18507       if (object_decl)
18508         decl = object_decl;
18509     }
18510   else
18511     {
18512       decl = lookup_name_real (name, tag_type != none_type,
18513                                /*nonclass=*/0,
18514                                /*block_p=*/true, is_namespace, flags);
18515       parser->qualifying_scope = NULL_TREE;
18516       parser->object_scope = NULL_TREE;
18517     }
18518
18519   /* If the lookup failed, let our caller know.  */
18520   if (!decl || decl == error_mark_node)
18521     return error_mark_node;
18522
18523   /* Pull out the template from an injected-class-name (or multiple).  */
18524   if (is_template)
18525     decl = maybe_get_template_decl_from_type_decl (decl);
18526
18527   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18528   if (TREE_CODE (decl) == TREE_LIST)
18529     {
18530       if (ambiguous_decls)
18531         *ambiguous_decls = decl;
18532       /* The error message we have to print is too complicated for
18533          cp_parser_error, so we incorporate its actions directly.  */
18534       if (!cp_parser_simulate_error (parser))
18535         {
18536           error_at (name_location, "reference to %qD is ambiguous",
18537                     name);
18538           print_candidates (decl);
18539         }
18540       return error_mark_node;
18541     }
18542
18543   gcc_assert (DECL_P (decl)
18544               || TREE_CODE (decl) == OVERLOAD
18545               || TREE_CODE (decl) == SCOPE_REF
18546               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18547               || BASELINK_P (decl));
18548
18549   /* If we have resolved the name of a member declaration, check to
18550      see if the declaration is accessible.  When the name resolves to
18551      set of overloaded functions, accessibility is checked when
18552      overload resolution is done.
18553
18554      During an explicit instantiation, access is not checked at all,
18555      as per [temp.explicit].  */
18556   if (DECL_P (decl))
18557     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18558
18559   return decl;
18560 }
18561
18562 /* Like cp_parser_lookup_name, but for use in the typical case where
18563    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18564    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18565
18566 static tree
18567 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18568 {
18569   return cp_parser_lookup_name (parser, name,
18570                                 none_type,
18571                                 /*is_template=*/false,
18572                                 /*is_namespace=*/false,
18573                                 /*check_dependency=*/true,
18574                                 /*ambiguous_decls=*/NULL,
18575                                 location);
18576 }
18577
18578 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18579    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18580    true, the DECL indicates the class being defined in a class-head,
18581    or declared in an elaborated-type-specifier.
18582
18583    Otherwise, return DECL.  */
18584
18585 static tree
18586 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18587 {
18588   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18589      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18590
18591        struct A {
18592          template <typename T> struct B;
18593        };
18594
18595        template <typename T> struct A::B {};
18596
18597      Similarly, in an elaborated-type-specifier:
18598
18599        namespace N { struct X{}; }
18600
18601        struct A {
18602          template <typename T> friend struct N::X;
18603        };
18604
18605      However, if the DECL refers to a class type, and we are in
18606      the scope of the class, then the name lookup automatically
18607      finds the TYPE_DECL created by build_self_reference rather
18608      than a TEMPLATE_DECL.  For example, in:
18609
18610        template <class T> struct S {
18611          S s;
18612        };
18613
18614      there is no need to handle such case.  */
18615
18616   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18617     return DECL_TEMPLATE_RESULT (decl);
18618
18619   return decl;
18620 }
18621
18622 /* If too many, or too few, template-parameter lists apply to the
18623    declarator, issue an error message.  Returns TRUE if all went well,
18624    and FALSE otherwise.  */
18625
18626 static bool
18627 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18628                                                 cp_declarator *declarator,
18629                                                 location_t declarator_location)
18630 {
18631   unsigned num_templates;
18632
18633   /* We haven't seen any classes that involve template parameters yet.  */
18634   num_templates = 0;
18635
18636   switch (declarator->kind)
18637     {
18638     case cdk_id:
18639       if (declarator->u.id.qualifying_scope)
18640         {
18641           tree scope;
18642
18643           scope = declarator->u.id.qualifying_scope;
18644
18645           while (scope && CLASS_TYPE_P (scope))
18646             {
18647               /* You're supposed to have one `template <...>'
18648                  for every template class, but you don't need one
18649                  for a full specialization.  For example:
18650
18651                  template <class T> struct S{};
18652                  template <> struct S<int> { void f(); };
18653                  void S<int>::f () {}
18654
18655                  is correct; there shouldn't be a `template <>' for
18656                  the definition of `S<int>::f'.  */
18657               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18658                 /* If SCOPE does not have template information of any
18659                    kind, then it is not a template, nor is it nested
18660                    within a template.  */
18661                 break;
18662               if (explicit_class_specialization_p (scope))
18663                 break;
18664               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18665                 ++num_templates;
18666
18667               scope = TYPE_CONTEXT (scope);
18668             }
18669         }
18670       else if (TREE_CODE (declarator->u.id.unqualified_name)
18671                == TEMPLATE_ID_EXPR)
18672         /* If the DECLARATOR has the form `X<y>' then it uses one
18673            additional level of template parameters.  */
18674         ++num_templates;
18675
18676       return cp_parser_check_template_parameters 
18677         (parser, num_templates, declarator_location, declarator);
18678
18679
18680     case cdk_function:
18681     case cdk_array:
18682     case cdk_pointer:
18683     case cdk_reference:
18684     case cdk_ptrmem:
18685       return (cp_parser_check_declarator_template_parameters
18686               (parser, declarator->declarator, declarator_location));
18687
18688     case cdk_error:
18689       return true;
18690
18691     default:
18692       gcc_unreachable ();
18693     }
18694   return false;
18695 }
18696
18697 /* NUM_TEMPLATES were used in the current declaration.  If that is
18698    invalid, return FALSE and issue an error messages.  Otherwise,
18699    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18700    declarator and we can print more accurate diagnostics.  */
18701
18702 static bool
18703 cp_parser_check_template_parameters (cp_parser* parser,
18704                                      unsigned num_templates,
18705                                      location_t location,
18706                                      cp_declarator *declarator)
18707 {
18708   /* If there are the same number of template classes and parameter
18709      lists, that's OK.  */
18710   if (parser->num_template_parameter_lists == num_templates)
18711     return true;
18712   /* If there are more, but only one more, then we are referring to a
18713      member template.  That's OK too.  */
18714   if (parser->num_template_parameter_lists == num_templates + 1)
18715     return true;
18716   /* If there are more template classes than parameter lists, we have
18717      something like:
18718
18719        template <class T> void S<T>::R<T>::f ();  */
18720   if (parser->num_template_parameter_lists < num_templates)
18721     {
18722       if (declarator && !current_function_decl)
18723         error_at (location, "specializing member %<%T::%E%> "
18724                   "requires %<template<>%> syntax", 
18725                   declarator->u.id.qualifying_scope,
18726                   declarator->u.id.unqualified_name);
18727       else if (declarator)
18728         error_at (location, "invalid declaration of %<%T::%E%>",
18729                   declarator->u.id.qualifying_scope,
18730                   declarator->u.id.unqualified_name);
18731       else 
18732         error_at (location, "too few template-parameter-lists");
18733       return false;
18734     }
18735   /* Otherwise, there are too many template parameter lists.  We have
18736      something like:
18737
18738      template <class T> template <class U> void S::f();  */
18739   error_at (location, "too many template-parameter-lists");
18740   return false;
18741 }
18742
18743 /* Parse an optional `::' token indicating that the following name is
18744    from the global namespace.  If so, PARSER->SCOPE is set to the
18745    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18746    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18747    Returns the new value of PARSER->SCOPE, if the `::' token is
18748    present, and NULL_TREE otherwise.  */
18749
18750 static tree
18751 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18752 {
18753   cp_token *token;
18754
18755   /* Peek at the next token.  */
18756   token = cp_lexer_peek_token (parser->lexer);
18757   /* If we're looking at a `::' token then we're starting from the
18758      global namespace, not our current location.  */
18759   if (token->type == CPP_SCOPE)
18760     {
18761       /* Consume the `::' token.  */
18762       cp_lexer_consume_token (parser->lexer);
18763       /* Set the SCOPE so that we know where to start the lookup.  */
18764       parser->scope = global_namespace;
18765       parser->qualifying_scope = global_namespace;
18766       parser->object_scope = NULL_TREE;
18767
18768       return parser->scope;
18769     }
18770   else if (!current_scope_valid_p)
18771     {
18772       parser->scope = NULL_TREE;
18773       parser->qualifying_scope = NULL_TREE;
18774       parser->object_scope = NULL_TREE;
18775     }
18776
18777   return NULL_TREE;
18778 }
18779
18780 /* Returns TRUE if the upcoming token sequence is the start of a
18781    constructor declarator.  If FRIEND_P is true, the declarator is
18782    preceded by the `friend' specifier.  */
18783
18784 static bool
18785 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18786 {
18787   bool constructor_p;
18788   tree nested_name_specifier;
18789   cp_token *next_token;
18790
18791   /* The common case is that this is not a constructor declarator, so
18792      try to avoid doing lots of work if at all possible.  It's not
18793      valid declare a constructor at function scope.  */
18794   if (parser->in_function_body)
18795     return false;
18796   /* And only certain tokens can begin a constructor declarator.  */
18797   next_token = cp_lexer_peek_token (parser->lexer);
18798   if (next_token->type != CPP_NAME
18799       && next_token->type != CPP_SCOPE
18800       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18801       && next_token->type != CPP_TEMPLATE_ID)
18802     return false;
18803
18804   /* Parse tentatively; we are going to roll back all of the tokens
18805      consumed here.  */
18806   cp_parser_parse_tentatively (parser);
18807   /* Assume that we are looking at a constructor declarator.  */
18808   constructor_p = true;
18809
18810   /* Look for the optional `::' operator.  */
18811   cp_parser_global_scope_opt (parser,
18812                               /*current_scope_valid_p=*/false);
18813   /* Look for the nested-name-specifier.  */
18814   nested_name_specifier
18815     = (cp_parser_nested_name_specifier_opt (parser,
18816                                             /*typename_keyword_p=*/false,
18817                                             /*check_dependency_p=*/false,
18818                                             /*type_p=*/false,
18819                                             /*is_declaration=*/false));
18820   /* Outside of a class-specifier, there must be a
18821      nested-name-specifier.  */
18822   if (!nested_name_specifier &&
18823       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18824        || friend_p))
18825     constructor_p = false;
18826   else if (nested_name_specifier == error_mark_node)
18827     constructor_p = false;
18828
18829   /* If we have a class scope, this is easy; DR 147 says that S::S always
18830      names the constructor, and no other qualified name could.  */
18831   if (constructor_p && nested_name_specifier
18832       && TYPE_P (nested_name_specifier))
18833     {
18834       tree id = cp_parser_unqualified_id (parser,
18835                                           /*template_keyword_p=*/false,
18836                                           /*check_dependency_p=*/false,
18837                                           /*declarator_p=*/true,
18838                                           /*optional_p=*/false);
18839       if (is_overloaded_fn (id))
18840         id = DECL_NAME (get_first_fn (id));
18841       if (!constructor_name_p (id, nested_name_specifier))
18842         constructor_p = false;
18843     }
18844   /* If we still think that this might be a constructor-declarator,
18845      look for a class-name.  */
18846   else if (constructor_p)
18847     {
18848       /* If we have:
18849
18850            template <typename T> struct S {
18851              S();
18852            };
18853
18854          we must recognize that the nested `S' names a class.  */
18855       tree type_decl;
18856       type_decl = cp_parser_class_name (parser,
18857                                         /*typename_keyword_p=*/false,
18858                                         /*template_keyword_p=*/false,
18859                                         none_type,
18860                                         /*check_dependency_p=*/false,
18861                                         /*class_head_p=*/false,
18862                                         /*is_declaration=*/false);
18863       /* If there was no class-name, then this is not a constructor.  */
18864       constructor_p = !cp_parser_error_occurred (parser);
18865
18866       /* If we're still considering a constructor, we have to see a `(',
18867          to begin the parameter-declaration-clause, followed by either a
18868          `)', an `...', or a decl-specifier.  We need to check for a
18869          type-specifier to avoid being fooled into thinking that:
18870
18871            S (f) (int);
18872
18873          is a constructor.  (It is actually a function named `f' that
18874          takes one parameter (of type `int') and returns a value of type
18875          `S'.  */
18876       if (constructor_p
18877           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18878         constructor_p = false;
18879
18880       if (constructor_p
18881           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18882           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18883           /* A parameter declaration begins with a decl-specifier,
18884              which is either the "attribute" keyword, a storage class
18885              specifier, or (usually) a type-specifier.  */
18886           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18887         {
18888           tree type;
18889           tree pushed_scope = NULL_TREE;
18890           unsigned saved_num_template_parameter_lists;
18891
18892           /* Names appearing in the type-specifier should be looked up
18893              in the scope of the class.  */
18894           if (current_class_type)
18895             type = NULL_TREE;
18896           else
18897             {
18898               type = TREE_TYPE (type_decl);
18899               if (TREE_CODE (type) == TYPENAME_TYPE)
18900                 {
18901                   type = resolve_typename_type (type,
18902                                                 /*only_current_p=*/false);
18903                   if (TREE_CODE (type) == TYPENAME_TYPE)
18904                     {
18905                       cp_parser_abort_tentative_parse (parser);
18906                       return false;
18907                     }
18908                 }
18909               pushed_scope = push_scope (type);
18910             }
18911
18912           /* Inside the constructor parameter list, surrounding
18913              template-parameter-lists do not apply.  */
18914           saved_num_template_parameter_lists
18915             = parser->num_template_parameter_lists;
18916           parser->num_template_parameter_lists = 0;
18917
18918           /* Look for the type-specifier.  */
18919           cp_parser_type_specifier (parser,
18920                                     CP_PARSER_FLAGS_NONE,
18921                                     /*decl_specs=*/NULL,
18922                                     /*is_declarator=*/true,
18923                                     /*declares_class_or_enum=*/NULL,
18924                                     /*is_cv_qualifier=*/NULL);
18925
18926           parser->num_template_parameter_lists
18927             = saved_num_template_parameter_lists;
18928
18929           /* Leave the scope of the class.  */
18930           if (pushed_scope)
18931             pop_scope (pushed_scope);
18932
18933           constructor_p = !cp_parser_error_occurred (parser);
18934         }
18935     }
18936
18937   /* We did not really want to consume any tokens.  */
18938   cp_parser_abort_tentative_parse (parser);
18939
18940   return constructor_p;
18941 }
18942
18943 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18944    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18945    they must be performed once we are in the scope of the function.
18946
18947    Returns the function defined.  */
18948
18949 static tree
18950 cp_parser_function_definition_from_specifiers_and_declarator
18951   (cp_parser* parser,
18952    cp_decl_specifier_seq *decl_specifiers,
18953    tree attributes,
18954    const cp_declarator *declarator)
18955 {
18956   tree fn;
18957   bool success_p;
18958
18959   /* Begin the function-definition.  */
18960   success_p = start_function (decl_specifiers, declarator, attributes);
18961
18962   /* The things we're about to see are not directly qualified by any
18963      template headers we've seen thus far.  */
18964   reset_specialization ();
18965
18966   /* If there were names looked up in the decl-specifier-seq that we
18967      did not check, check them now.  We must wait until we are in the
18968      scope of the function to perform the checks, since the function
18969      might be a friend.  */
18970   perform_deferred_access_checks ();
18971
18972   if (!success_p)
18973     {
18974       /* Skip the entire function.  */
18975       cp_parser_skip_to_end_of_block_or_statement (parser);
18976       fn = error_mark_node;
18977     }
18978   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18979     {
18980       /* Seen already, skip it.  An error message has already been output.  */
18981       cp_parser_skip_to_end_of_block_or_statement (parser);
18982       fn = current_function_decl;
18983       current_function_decl = NULL_TREE;
18984       /* If this is a function from a class, pop the nested class.  */
18985       if (current_class_name)
18986         pop_nested_class ();
18987     }
18988   else
18989     fn = cp_parser_function_definition_after_declarator (parser,
18990                                                          /*inline_p=*/false);
18991
18992   return fn;
18993 }
18994
18995 /* Parse the part of a function-definition that follows the
18996    declarator.  INLINE_P is TRUE iff this function is an inline
18997    function defined within a class-specifier.
18998
18999    Returns the function defined.  */
19000
19001 static tree
19002 cp_parser_function_definition_after_declarator (cp_parser* parser,
19003                                                 bool inline_p)
19004 {
19005   tree fn;
19006   bool ctor_initializer_p = false;
19007   bool saved_in_unbraced_linkage_specification_p;
19008   bool saved_in_function_body;
19009   unsigned saved_num_template_parameter_lists;
19010   cp_token *token;
19011
19012   saved_in_function_body = parser->in_function_body;
19013   parser->in_function_body = true;
19014   /* If the next token is `return', then the code may be trying to
19015      make use of the "named return value" extension that G++ used to
19016      support.  */
19017   token = cp_lexer_peek_token (parser->lexer);
19018   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19019     {
19020       /* Consume the `return' keyword.  */
19021       cp_lexer_consume_token (parser->lexer);
19022       /* Look for the identifier that indicates what value is to be
19023          returned.  */
19024       cp_parser_identifier (parser);
19025       /* Issue an error message.  */
19026       error_at (token->location,
19027                 "named return values are no longer supported");
19028       /* Skip tokens until we reach the start of the function body.  */
19029       while (true)
19030         {
19031           cp_token *token = cp_lexer_peek_token (parser->lexer);
19032           if (token->type == CPP_OPEN_BRACE
19033               || token->type == CPP_EOF
19034               || token->type == CPP_PRAGMA_EOL)
19035             break;
19036           cp_lexer_consume_token (parser->lexer);
19037         }
19038     }
19039   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19040      anything declared inside `f'.  */
19041   saved_in_unbraced_linkage_specification_p
19042     = parser->in_unbraced_linkage_specification_p;
19043   parser->in_unbraced_linkage_specification_p = false;
19044   /* Inside the function, surrounding template-parameter-lists do not
19045      apply.  */
19046   saved_num_template_parameter_lists
19047     = parser->num_template_parameter_lists;
19048   parser->num_template_parameter_lists = 0;
19049
19050   start_lambda_scope (current_function_decl);
19051
19052   /* If the next token is `try', then we are looking at a
19053      function-try-block.  */
19054   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19055     ctor_initializer_p = cp_parser_function_try_block (parser);
19056   /* A function-try-block includes the function-body, so we only do
19057      this next part if we're not processing a function-try-block.  */
19058   else
19059     ctor_initializer_p
19060       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19061
19062   finish_lambda_scope ();
19063
19064   /* Finish the function.  */
19065   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19066                         (inline_p ? 2 : 0));
19067   /* Generate code for it, if necessary.  */
19068   expand_or_defer_fn (fn);
19069   /* Restore the saved values.  */
19070   parser->in_unbraced_linkage_specification_p
19071     = saved_in_unbraced_linkage_specification_p;
19072   parser->num_template_parameter_lists
19073     = saved_num_template_parameter_lists;
19074   parser->in_function_body = saved_in_function_body;
19075
19076   return fn;
19077 }
19078
19079 /* Parse a template-declaration, assuming that the `export' (and
19080    `extern') keywords, if present, has already been scanned.  MEMBER_P
19081    is as for cp_parser_template_declaration.  */
19082
19083 static void
19084 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19085 {
19086   tree decl = NULL_TREE;
19087   VEC (deferred_access_check,gc) *checks;
19088   tree parameter_list;
19089   bool friend_p = false;
19090   bool need_lang_pop;
19091   cp_token *token;
19092
19093   /* Look for the `template' keyword.  */
19094   token = cp_lexer_peek_token (parser->lexer);
19095   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19096     return;
19097
19098   /* And the `<'.  */
19099   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19100     return;
19101   if (at_class_scope_p () && current_function_decl)
19102     {
19103       /* 14.5.2.2 [temp.mem]
19104
19105          A local class shall not have member templates.  */
19106       error_at (token->location,
19107                 "invalid declaration of member template in local class");
19108       cp_parser_skip_to_end_of_block_or_statement (parser);
19109       return;
19110     }
19111   /* [temp]
19112
19113      A template ... shall not have C linkage.  */
19114   if (current_lang_name == lang_name_c)
19115     {
19116       error_at (token->location, "template with C linkage");
19117       /* Give it C++ linkage to avoid confusing other parts of the
19118          front end.  */
19119       push_lang_context (lang_name_cplusplus);
19120       need_lang_pop = true;
19121     }
19122   else
19123     need_lang_pop = false;
19124
19125   /* We cannot perform access checks on the template parameter
19126      declarations until we know what is being declared, just as we
19127      cannot check the decl-specifier list.  */
19128   push_deferring_access_checks (dk_deferred);
19129
19130   /* If the next token is `>', then we have an invalid
19131      specialization.  Rather than complain about an invalid template
19132      parameter, issue an error message here.  */
19133   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19134     {
19135       cp_parser_error (parser, "invalid explicit specialization");
19136       begin_specialization ();
19137       parameter_list = NULL_TREE;
19138     }
19139   else
19140     /* Parse the template parameters.  */
19141     parameter_list = cp_parser_template_parameter_list (parser);
19142
19143   /* Get the deferred access checks from the parameter list.  These
19144      will be checked once we know what is being declared, as for a
19145      member template the checks must be performed in the scope of the
19146      class containing the member.  */
19147   checks = get_deferred_access_checks ();
19148
19149   /* Look for the `>'.  */
19150   cp_parser_skip_to_end_of_template_parameter_list (parser);
19151   /* We just processed one more parameter list.  */
19152   ++parser->num_template_parameter_lists;
19153   /* If the next token is `template', there are more template
19154      parameters.  */
19155   if (cp_lexer_next_token_is_keyword (parser->lexer,
19156                                       RID_TEMPLATE))
19157     cp_parser_template_declaration_after_export (parser, member_p);
19158   else
19159     {
19160       /* There are no access checks when parsing a template, as we do not
19161          know if a specialization will be a friend.  */
19162       push_deferring_access_checks (dk_no_check);
19163       token = cp_lexer_peek_token (parser->lexer);
19164       decl = cp_parser_single_declaration (parser,
19165                                            checks,
19166                                            member_p,
19167                                            /*explicit_specialization_p=*/false,
19168                                            &friend_p);
19169       pop_deferring_access_checks ();
19170
19171       /* If this is a member template declaration, let the front
19172          end know.  */
19173       if (member_p && !friend_p && decl)
19174         {
19175           if (TREE_CODE (decl) == TYPE_DECL)
19176             cp_parser_check_access_in_redeclaration (decl, token->location);
19177
19178           decl = finish_member_template_decl (decl);
19179         }
19180       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19181         make_friend_class (current_class_type, TREE_TYPE (decl),
19182                            /*complain=*/true);
19183     }
19184   /* We are done with the current parameter list.  */
19185   --parser->num_template_parameter_lists;
19186
19187   pop_deferring_access_checks ();
19188
19189   /* Finish up.  */
19190   finish_template_decl (parameter_list);
19191
19192   /* Register member declarations.  */
19193   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19194     finish_member_declaration (decl);
19195   /* For the erroneous case of a template with C linkage, we pushed an
19196      implicit C++ linkage scope; exit that scope now.  */
19197   if (need_lang_pop)
19198     pop_lang_context ();
19199   /* If DECL is a function template, we must return to parse it later.
19200      (Even though there is no definition, there might be default
19201      arguments that need handling.)  */
19202   if (member_p && decl
19203       && (TREE_CODE (decl) == FUNCTION_DECL
19204           || DECL_FUNCTION_TEMPLATE_P (decl)))
19205     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19206 }
19207
19208 /* Perform the deferred access checks from a template-parameter-list.
19209    CHECKS is a TREE_LIST of access checks, as returned by
19210    get_deferred_access_checks.  */
19211
19212 static void
19213 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19214 {
19215   ++processing_template_parmlist;
19216   perform_access_checks (checks);
19217   --processing_template_parmlist;
19218 }
19219
19220 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19221    `function-definition' sequence.  MEMBER_P is true, this declaration
19222    appears in a class scope.
19223
19224    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
19225    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
19226
19227 static tree
19228 cp_parser_single_declaration (cp_parser* parser,
19229                               VEC (deferred_access_check,gc)* checks,
19230                               bool member_p,
19231                               bool explicit_specialization_p,
19232                               bool* friend_p)
19233 {
19234   int declares_class_or_enum;
19235   tree decl = NULL_TREE;
19236   cp_decl_specifier_seq decl_specifiers;
19237   bool function_definition_p = false;
19238   cp_token *decl_spec_token_start;
19239
19240   /* This function is only used when processing a template
19241      declaration.  */
19242   gcc_assert (innermost_scope_kind () == sk_template_parms
19243               || innermost_scope_kind () == sk_template_spec);
19244
19245   /* Defer access checks until we know what is being declared.  */
19246   push_deferring_access_checks (dk_deferred);
19247
19248   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19249      alternative.  */
19250   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19251   cp_parser_decl_specifier_seq (parser,
19252                                 CP_PARSER_FLAGS_OPTIONAL,
19253                                 &decl_specifiers,
19254                                 &declares_class_or_enum);
19255   if (friend_p)
19256     *friend_p = cp_parser_friend_p (&decl_specifiers);
19257
19258   /* There are no template typedefs.  */
19259   if (decl_specifiers.specs[(int) ds_typedef])
19260     {
19261       error_at (decl_spec_token_start->location,
19262                 "template declaration of %<typedef%>");
19263       decl = error_mark_node;
19264     }
19265
19266   /* Gather up the access checks that occurred the
19267      decl-specifier-seq.  */
19268   stop_deferring_access_checks ();
19269
19270   /* Check for the declaration of a template class.  */
19271   if (declares_class_or_enum)
19272     {
19273       if (cp_parser_declares_only_class_p (parser))
19274         {
19275           decl = shadow_tag (&decl_specifiers);
19276
19277           /* In this case:
19278
19279                struct C {
19280                  friend template <typename T> struct A<T>::B;
19281                };
19282
19283              A<T>::B will be represented by a TYPENAME_TYPE, and
19284              therefore not recognized by shadow_tag.  */
19285           if (friend_p && *friend_p
19286               && !decl
19287               && decl_specifiers.type
19288               && TYPE_P (decl_specifiers.type))
19289             decl = decl_specifiers.type;
19290
19291           if (decl && decl != error_mark_node)
19292             decl = TYPE_NAME (decl);
19293           else
19294             decl = error_mark_node;
19295
19296           /* Perform access checks for template parameters.  */
19297           cp_parser_perform_template_parameter_access_checks (checks);
19298         }
19299     }
19300
19301   /* Complain about missing 'typename' or other invalid type names.  */
19302   if (!decl_specifiers.any_type_specifiers_p)
19303     cp_parser_parse_and_diagnose_invalid_type_name (parser);
19304
19305   /* If it's not a template class, try for a template function.  If
19306      the next token is a `;', then this declaration does not declare
19307      anything.  But, if there were errors in the decl-specifiers, then
19308      the error might well have come from an attempted class-specifier.
19309      In that case, there's no need to warn about a missing declarator.  */
19310   if (!decl
19311       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19312           || decl_specifiers.type != error_mark_node))
19313     {
19314       decl = cp_parser_init_declarator (parser,
19315                                         &decl_specifiers,
19316                                         checks,
19317                                         /*function_definition_allowed_p=*/true,
19318                                         member_p,
19319                                         declares_class_or_enum,
19320                                         &function_definition_p);
19321
19322     /* 7.1.1-1 [dcl.stc]
19323
19324        A storage-class-specifier shall not be specified in an explicit
19325        specialization...  */
19326     if (decl
19327         && explicit_specialization_p
19328         && decl_specifiers.storage_class != sc_none)
19329       {
19330         error_at (decl_spec_token_start->location,
19331                   "explicit template specialization cannot have a storage class");
19332         decl = error_mark_node;
19333       }
19334     }
19335
19336   pop_deferring_access_checks ();
19337
19338   /* Clear any current qualification; whatever comes next is the start
19339      of something new.  */
19340   parser->scope = NULL_TREE;
19341   parser->qualifying_scope = NULL_TREE;
19342   parser->object_scope = NULL_TREE;
19343   /* Look for a trailing `;' after the declaration.  */
19344   if (!function_definition_p
19345       && (decl == error_mark_node
19346           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19347     cp_parser_skip_to_end_of_block_or_statement (parser);
19348
19349   return decl;
19350 }
19351
19352 /* Parse a cast-expression that is not the operand of a unary "&".  */
19353
19354 static tree
19355 cp_parser_simple_cast_expression (cp_parser *parser)
19356 {
19357   return cp_parser_cast_expression (parser, /*address_p=*/false,
19358                                     /*cast_p=*/false, NULL);
19359 }
19360
19361 /* Parse a functional cast to TYPE.  Returns an expression
19362    representing the cast.  */
19363
19364 static tree
19365 cp_parser_functional_cast (cp_parser* parser, tree type)
19366 {
19367   VEC(tree,gc) *vec;
19368   tree expression_list;
19369   tree cast;
19370   bool nonconst_p;
19371
19372   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19373     {
19374       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19375       expression_list = cp_parser_braced_list (parser, &nonconst_p);
19376       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19377       if (TREE_CODE (type) == TYPE_DECL)
19378         type = TREE_TYPE (type);
19379       return finish_compound_literal (type, expression_list);
19380     }
19381
19382
19383   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19384                                                  /*cast_p=*/true,
19385                                                  /*allow_expansion_p=*/true,
19386                                                  /*non_constant_p=*/NULL);
19387   if (vec == NULL)
19388     expression_list = error_mark_node;
19389   else
19390     {
19391       expression_list = build_tree_list_vec (vec);
19392       release_tree_vector (vec);
19393     }
19394
19395   cast = build_functional_cast (type, expression_list,
19396                                 tf_warning_or_error);
19397   /* [expr.const]/1: In an integral constant expression "only type
19398      conversions to integral or enumeration type can be used".  */
19399   if (TREE_CODE (type) == TYPE_DECL)
19400     type = TREE_TYPE (type);
19401   if (cast != error_mark_node
19402       && !cast_valid_in_integral_constant_expression_p (type)
19403       && cp_parser_non_integral_constant_expression (parser,
19404                                                      NIC_CONSTRUCTOR))
19405     return error_mark_node;
19406   return cast;
19407 }
19408
19409 /* Save the tokens that make up the body of a member function defined
19410    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
19411    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
19412    specifiers applied to the declaration.  Returns the FUNCTION_DECL
19413    for the member function.  */
19414
19415 static tree
19416 cp_parser_save_member_function_body (cp_parser* parser,
19417                                      cp_decl_specifier_seq *decl_specifiers,
19418                                      cp_declarator *declarator,
19419                                      tree attributes)
19420 {
19421   cp_token *first;
19422   cp_token *last;
19423   tree fn;
19424
19425   /* Create the FUNCTION_DECL.  */
19426   fn = grokmethod (decl_specifiers, declarator, attributes);
19427   /* If something went badly wrong, bail out now.  */
19428   if (fn == error_mark_node)
19429     {
19430       /* If there's a function-body, skip it.  */
19431       if (cp_parser_token_starts_function_definition_p
19432           (cp_lexer_peek_token (parser->lexer)))
19433         cp_parser_skip_to_end_of_block_or_statement (parser);
19434       return error_mark_node;
19435     }
19436
19437   /* Remember it, if there default args to post process.  */
19438   cp_parser_save_default_args (parser, fn);
19439
19440   /* Save away the tokens that make up the body of the
19441      function.  */
19442   first = parser->lexer->next_token;
19443   /* We can have braced-init-list mem-initializers before the fn body.  */
19444   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19445     {
19446       cp_lexer_consume_token (parser->lexer);
19447       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19448              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19449         {
19450           /* cache_group will stop after an un-nested { } pair, too.  */
19451           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19452             break;
19453
19454           /* variadic mem-inits have ... after the ')'.  */
19455           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19456             cp_lexer_consume_token (parser->lexer);
19457         }
19458     }
19459   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19460   /* Handle function try blocks.  */
19461   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19462     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19463   last = parser->lexer->next_token;
19464
19465   /* Save away the inline definition; we will process it when the
19466      class is complete.  */
19467   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19468   DECL_PENDING_INLINE_P (fn) = 1;
19469
19470   /* We need to know that this was defined in the class, so that
19471      friend templates are handled correctly.  */
19472   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19473
19474   /* Add FN to the queue of functions to be parsed later.  */
19475   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
19476
19477   return fn;
19478 }
19479
19480 /* Parse a template-argument-list, as well as the trailing ">" (but
19481    not the opening ">").  See cp_parser_template_argument_list for the
19482    return value.  */
19483
19484 static tree
19485 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19486 {
19487   tree arguments;
19488   tree saved_scope;
19489   tree saved_qualifying_scope;
19490   tree saved_object_scope;
19491   bool saved_greater_than_is_operator_p;
19492   int saved_unevaluated_operand;
19493   int saved_inhibit_evaluation_warnings;
19494
19495   /* [temp.names]
19496
19497      When parsing a template-id, the first non-nested `>' is taken as
19498      the end of the template-argument-list rather than a greater-than
19499      operator.  */
19500   saved_greater_than_is_operator_p
19501     = parser->greater_than_is_operator_p;
19502   parser->greater_than_is_operator_p = false;
19503   /* Parsing the argument list may modify SCOPE, so we save it
19504      here.  */
19505   saved_scope = parser->scope;
19506   saved_qualifying_scope = parser->qualifying_scope;
19507   saved_object_scope = parser->object_scope;
19508   /* We need to evaluate the template arguments, even though this
19509      template-id may be nested within a "sizeof".  */
19510   saved_unevaluated_operand = cp_unevaluated_operand;
19511   cp_unevaluated_operand = 0;
19512   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19513   c_inhibit_evaluation_warnings = 0;
19514   /* Parse the template-argument-list itself.  */
19515   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19516       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19517     arguments = NULL_TREE;
19518   else
19519     arguments = cp_parser_template_argument_list (parser);
19520   /* Look for the `>' that ends the template-argument-list. If we find
19521      a '>>' instead, it's probably just a typo.  */
19522   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19523     {
19524       if (cxx_dialect != cxx98)
19525         {
19526           /* In C++0x, a `>>' in a template argument list or cast
19527              expression is considered to be two separate `>'
19528              tokens. So, change the current token to a `>', but don't
19529              consume it: it will be consumed later when the outer
19530              template argument list (or cast expression) is parsed.
19531              Note that this replacement of `>' for `>>' is necessary
19532              even if we are parsing tentatively: in the tentative
19533              case, after calling
19534              cp_parser_enclosed_template_argument_list we will always
19535              throw away all of the template arguments and the first
19536              closing `>', either because the template argument list
19537              was erroneous or because we are replacing those tokens
19538              with a CPP_TEMPLATE_ID token.  The second `>' (which will
19539              not have been thrown away) is needed either to close an
19540              outer template argument list or to complete a new-style
19541              cast.  */
19542           cp_token *token = cp_lexer_peek_token (parser->lexer);
19543           token->type = CPP_GREATER;
19544         }
19545       else if (!saved_greater_than_is_operator_p)
19546         {
19547           /* If we're in a nested template argument list, the '>>' has
19548             to be a typo for '> >'. We emit the error message, but we
19549             continue parsing and we push a '>' as next token, so that
19550             the argument list will be parsed correctly.  Note that the
19551             global source location is still on the token before the
19552             '>>', so we need to say explicitly where we want it.  */
19553           cp_token *token = cp_lexer_peek_token (parser->lexer);
19554           error_at (token->location, "%<>>%> should be %<> >%> "
19555                     "within a nested template argument list");
19556
19557           token->type = CPP_GREATER;
19558         }
19559       else
19560         {
19561           /* If this is not a nested template argument list, the '>>'
19562             is a typo for '>'. Emit an error message and continue.
19563             Same deal about the token location, but here we can get it
19564             right by consuming the '>>' before issuing the diagnostic.  */
19565           cp_token *token = cp_lexer_consume_token (parser->lexer);
19566           error_at (token->location,
19567                     "spurious %<>>%>, use %<>%> to terminate "
19568                     "a template argument list");
19569         }
19570     }
19571   else
19572     cp_parser_skip_to_end_of_template_parameter_list (parser);
19573   /* The `>' token might be a greater-than operator again now.  */
19574   parser->greater_than_is_operator_p
19575     = saved_greater_than_is_operator_p;
19576   /* Restore the SAVED_SCOPE.  */
19577   parser->scope = saved_scope;
19578   parser->qualifying_scope = saved_qualifying_scope;
19579   parser->object_scope = saved_object_scope;
19580   cp_unevaluated_operand = saved_unevaluated_operand;
19581   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19582
19583   return arguments;
19584 }
19585
19586 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19587    arguments, or the body of the function have not yet been parsed,
19588    parse them now.  */
19589
19590 static void
19591 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19592 {
19593   /* If this member is a template, get the underlying
19594      FUNCTION_DECL.  */
19595   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19596     member_function = DECL_TEMPLATE_RESULT (member_function);
19597
19598   /* There should not be any class definitions in progress at this
19599      point; the bodies of members are only parsed outside of all class
19600      definitions.  */
19601   gcc_assert (parser->num_classes_being_defined == 0);
19602   /* While we're parsing the member functions we might encounter more
19603      classes.  We want to handle them right away, but we don't want
19604      them getting mixed up with functions that are currently in the
19605      queue.  */
19606   push_unparsed_function_queues (parser);
19607
19608   /* Make sure that any template parameters are in scope.  */
19609   maybe_begin_member_template_processing (member_function);
19610
19611   /* If the body of the function has not yet been parsed, parse it
19612      now.  */
19613   if (DECL_PENDING_INLINE_P (member_function))
19614     {
19615       tree function_scope;
19616       cp_token_cache *tokens;
19617
19618       /* The function is no longer pending; we are processing it.  */
19619       tokens = DECL_PENDING_INLINE_INFO (member_function);
19620       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19621       DECL_PENDING_INLINE_P (member_function) = 0;
19622
19623       /* If this is a local class, enter the scope of the containing
19624          function.  */
19625       function_scope = current_function_decl;
19626       if (function_scope)
19627         push_function_context ();
19628
19629       /* Push the body of the function onto the lexer stack.  */
19630       cp_parser_push_lexer_for_tokens (parser, tokens);
19631
19632       /* Let the front end know that we going to be defining this
19633          function.  */
19634       start_preparsed_function (member_function, NULL_TREE,
19635                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19636
19637       /* Don't do access checking if it is a templated function.  */
19638       if (processing_template_decl)
19639         push_deferring_access_checks (dk_no_check);
19640
19641       /* Now, parse the body of the function.  */
19642       cp_parser_function_definition_after_declarator (parser,
19643                                                       /*inline_p=*/true);
19644
19645       if (processing_template_decl)
19646         pop_deferring_access_checks ();
19647
19648       /* Leave the scope of the containing function.  */
19649       if (function_scope)
19650         pop_function_context ();
19651       cp_parser_pop_lexer (parser);
19652     }
19653
19654   /* Remove any template parameters from the symbol table.  */
19655   maybe_end_member_template_processing ();
19656
19657   /* Restore the queue.  */
19658   pop_unparsed_function_queues (parser);
19659 }
19660
19661 /* If DECL contains any default args, remember it on the unparsed
19662    functions queue.  */
19663
19664 static void
19665 cp_parser_save_default_args (cp_parser* parser, tree decl)
19666 {
19667   tree probe;
19668
19669   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19670        probe;
19671        probe = TREE_CHAIN (probe))
19672     if (TREE_PURPOSE (probe))
19673       {
19674         cp_default_arg_entry *entry
19675           = VEC_safe_push (cp_default_arg_entry, gc,
19676                            unparsed_funs_with_default_args, NULL);
19677         entry->class_type = current_class_type;
19678         entry->decl = decl;
19679         break;
19680       }
19681 }
19682
19683 /* FN is a FUNCTION_DECL which may contains a parameter with an
19684    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19685    assumes that the current scope is the scope in which the default
19686    argument should be processed.  */
19687
19688 static void
19689 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19690 {
19691   bool saved_local_variables_forbidden_p;
19692   tree parm, parmdecl;
19693
19694   /* While we're parsing the default args, we might (due to the
19695      statement expression extension) encounter more classes.  We want
19696      to handle them right away, but we don't want them getting mixed
19697      up with default args that are currently in the queue.  */
19698   push_unparsed_function_queues (parser);
19699
19700   /* Local variable names (and the `this' keyword) may not appear
19701      in a default argument.  */
19702   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19703   parser->local_variables_forbidden_p = true;
19704
19705   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19706          parmdecl = DECL_ARGUMENTS (fn);
19707        parm && parm != void_list_node;
19708        parm = TREE_CHAIN (parm),
19709          parmdecl = DECL_CHAIN (parmdecl))
19710     {
19711       cp_token_cache *tokens;
19712       tree default_arg = TREE_PURPOSE (parm);
19713       tree parsed_arg;
19714       VEC(tree,gc) *insts;
19715       tree copy;
19716       unsigned ix;
19717
19718       if (!default_arg)
19719         continue;
19720
19721       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19722         /* This can happen for a friend declaration for a function
19723            already declared with default arguments.  */
19724         continue;
19725
19726        /* Push the saved tokens for the default argument onto the parser's
19727           lexer stack.  */
19728       tokens = DEFARG_TOKENS (default_arg);
19729       cp_parser_push_lexer_for_tokens (parser, tokens);
19730
19731       start_lambda_scope (parmdecl);
19732
19733       /* Parse the assignment-expression.  */
19734       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19735       if (parsed_arg == error_mark_node)
19736         {
19737           cp_parser_pop_lexer (parser);
19738           continue;
19739         }
19740
19741       if (!processing_template_decl)
19742         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19743
19744       TREE_PURPOSE (parm) = parsed_arg;
19745
19746       /* Update any instantiations we've already created.  */
19747       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19748            VEC_iterate (tree, insts, ix, copy); ix++)
19749         TREE_PURPOSE (copy) = parsed_arg;
19750
19751       finish_lambda_scope ();
19752
19753       /* If the token stream has not been completely used up, then
19754          there was extra junk after the end of the default
19755          argument.  */
19756       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19757         cp_parser_error (parser, "expected %<,%>");
19758
19759       /* Revert to the main lexer.  */
19760       cp_parser_pop_lexer (parser);
19761     }
19762
19763   /* Make sure no default arg is missing.  */
19764   check_default_args (fn);
19765
19766   /* Restore the state of local_variables_forbidden_p.  */
19767   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19768
19769   /* Restore the queue.  */
19770   pop_unparsed_function_queues (parser);
19771 }
19772
19773 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19774    either a TYPE or an expression, depending on the form of the
19775    input.  The KEYWORD indicates which kind of expression we have
19776    encountered.  */
19777
19778 static tree
19779 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19780 {
19781   tree expr = NULL_TREE;
19782   const char *saved_message;
19783   char *tmp;
19784   bool saved_integral_constant_expression_p;
19785   bool saved_non_integral_constant_expression_p;
19786   bool pack_expansion_p = false;
19787
19788   /* Types cannot be defined in a `sizeof' expression.  Save away the
19789      old message.  */
19790   saved_message = parser->type_definition_forbidden_message;
19791   /* And create the new one.  */
19792   tmp = concat ("types may not be defined in %<",
19793                 IDENTIFIER_POINTER (ridpointers[keyword]),
19794                 "%> expressions", NULL);
19795   parser->type_definition_forbidden_message = tmp;
19796
19797   /* The restrictions on constant-expressions do not apply inside
19798      sizeof expressions.  */
19799   saved_integral_constant_expression_p
19800     = parser->integral_constant_expression_p;
19801   saved_non_integral_constant_expression_p
19802     = parser->non_integral_constant_expression_p;
19803   parser->integral_constant_expression_p = false;
19804
19805   /* If it's a `...', then we are computing the length of a parameter
19806      pack.  */
19807   if (keyword == RID_SIZEOF
19808       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19809     {
19810       /* Consume the `...'.  */
19811       cp_lexer_consume_token (parser->lexer);
19812       maybe_warn_variadic_templates ();
19813
19814       /* Note that this is an expansion.  */
19815       pack_expansion_p = true;
19816     }
19817
19818   /* Do not actually evaluate the expression.  */
19819   ++cp_unevaluated_operand;
19820   ++c_inhibit_evaluation_warnings;
19821   /* If it's a `(', then we might be looking at the type-id
19822      construction.  */
19823   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19824     {
19825       tree type;
19826       bool saved_in_type_id_in_expr_p;
19827
19828       /* We can't be sure yet whether we're looking at a type-id or an
19829          expression.  */
19830       cp_parser_parse_tentatively (parser);
19831       /* Consume the `('.  */
19832       cp_lexer_consume_token (parser->lexer);
19833       /* Parse the type-id.  */
19834       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19835       parser->in_type_id_in_expr_p = true;
19836       type = cp_parser_type_id (parser);
19837       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19838       /* Now, look for the trailing `)'.  */
19839       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19840       /* If all went well, then we're done.  */
19841       if (cp_parser_parse_definitely (parser))
19842         {
19843           cp_decl_specifier_seq decl_specs;
19844
19845           /* Build a trivial decl-specifier-seq.  */
19846           clear_decl_specs (&decl_specs);
19847           decl_specs.type = type;
19848
19849           /* Call grokdeclarator to figure out what type this is.  */
19850           expr = grokdeclarator (NULL,
19851                                  &decl_specs,
19852                                  TYPENAME,
19853                                  /*initialized=*/0,
19854                                  /*attrlist=*/NULL);
19855         }
19856     }
19857
19858   /* If the type-id production did not work out, then we must be
19859      looking at the unary-expression production.  */
19860   if (!expr)
19861     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19862                                        /*cast_p=*/false, NULL);
19863
19864   if (pack_expansion_p)
19865     /* Build a pack expansion. */
19866     expr = make_pack_expansion (expr);
19867
19868   /* Go back to evaluating expressions.  */
19869   --cp_unevaluated_operand;
19870   --c_inhibit_evaluation_warnings;
19871
19872   /* Free the message we created.  */
19873   free (tmp);
19874   /* And restore the old one.  */
19875   parser->type_definition_forbidden_message = saved_message;
19876   parser->integral_constant_expression_p
19877     = saved_integral_constant_expression_p;
19878   parser->non_integral_constant_expression_p
19879     = saved_non_integral_constant_expression_p;
19880
19881   return expr;
19882 }
19883
19884 /* If the current declaration has no declarator, return true.  */
19885
19886 static bool
19887 cp_parser_declares_only_class_p (cp_parser *parser)
19888 {
19889   /* If the next token is a `;' or a `,' then there is no
19890      declarator.  */
19891   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19892           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19893 }
19894
19895 /* Update the DECL_SPECS to reflect the storage class indicated by
19896    KEYWORD.  */
19897
19898 static void
19899 cp_parser_set_storage_class (cp_parser *parser,
19900                              cp_decl_specifier_seq *decl_specs,
19901                              enum rid keyword,
19902                              location_t location)
19903 {
19904   cp_storage_class storage_class;
19905
19906   if (parser->in_unbraced_linkage_specification_p)
19907     {
19908       error_at (location, "invalid use of %qD in linkage specification",
19909                 ridpointers[keyword]);
19910       return;
19911     }
19912   else if (decl_specs->storage_class != sc_none)
19913     {
19914       decl_specs->conflicting_specifiers_p = true;
19915       return;
19916     }
19917
19918   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19919       && decl_specs->specs[(int) ds_thread])
19920     {
19921       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19922       decl_specs->specs[(int) ds_thread] = 0;
19923     }
19924
19925   switch (keyword)
19926     {
19927     case RID_AUTO:
19928       storage_class = sc_auto;
19929       break;
19930     case RID_REGISTER:
19931       storage_class = sc_register;
19932       break;
19933     case RID_STATIC:
19934       storage_class = sc_static;
19935       break;
19936     case RID_EXTERN:
19937       storage_class = sc_extern;
19938       break;
19939     case RID_MUTABLE:
19940       storage_class = sc_mutable;
19941       break;
19942     default:
19943       gcc_unreachable ();
19944     }
19945   decl_specs->storage_class = storage_class;
19946
19947   /* A storage class specifier cannot be applied alongside a typedef 
19948      specifier. If there is a typedef specifier present then set 
19949      conflicting_specifiers_p which will trigger an error later
19950      on in grokdeclarator. */
19951   if (decl_specs->specs[(int)ds_typedef])
19952     decl_specs->conflicting_specifiers_p = true;
19953 }
19954
19955 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19956    is true, the type is a user-defined type; otherwise it is a
19957    built-in type specified by a keyword.  */
19958
19959 static void
19960 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19961                               tree type_spec,
19962                               location_t location,
19963                               bool user_defined_p)
19964 {
19965   decl_specs->any_specifiers_p = true;
19966
19967   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19968      (with, for example, in "typedef int wchar_t;") we remember that
19969      this is what happened.  In system headers, we ignore these
19970      declarations so that G++ can work with system headers that are not
19971      C++-safe.  */
19972   if (decl_specs->specs[(int) ds_typedef]
19973       && !user_defined_p
19974       && (type_spec == boolean_type_node
19975           || type_spec == char16_type_node
19976           || type_spec == char32_type_node
19977           || type_spec == wchar_type_node)
19978       && (decl_specs->type
19979           || decl_specs->specs[(int) ds_long]
19980           || decl_specs->specs[(int) ds_short]
19981           || decl_specs->specs[(int) ds_unsigned]
19982           || decl_specs->specs[(int) ds_signed]))
19983     {
19984       decl_specs->redefined_builtin_type = type_spec;
19985       if (!decl_specs->type)
19986         {
19987           decl_specs->type = type_spec;
19988           decl_specs->user_defined_type_p = false;
19989           decl_specs->type_location = location;
19990         }
19991     }
19992   else if (decl_specs->type)
19993     decl_specs->multiple_types_p = true;
19994   else
19995     {
19996       decl_specs->type = type_spec;
19997       decl_specs->user_defined_type_p = user_defined_p;
19998       decl_specs->redefined_builtin_type = NULL_TREE;
19999       decl_specs->type_location = location;
20000     }
20001 }
20002
20003 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20004    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20005
20006 static bool
20007 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20008 {
20009   return decl_specifiers->specs[(int) ds_friend] != 0;
20010 }
20011
20012 /* Issue an error message indicating that TOKEN_DESC was expected.
20013    If KEYWORD is true, it indicated this function is called by
20014    cp_parser_require_keword and the required token can only be
20015    a indicated keyword. */
20016
20017 static void
20018 cp_parser_required_error (cp_parser *parser,
20019                           required_token token_desc,
20020                           bool keyword)
20021 {
20022   switch (token_desc)
20023     {
20024       case RT_NEW:
20025         cp_parser_error (parser, "expected %<new%>");
20026         return;
20027       case RT_DELETE:
20028         cp_parser_error (parser, "expected %<delete%>");
20029         return;
20030       case RT_RETURN:
20031         cp_parser_error (parser, "expected %<return%>");
20032         return;
20033       case RT_WHILE:
20034         cp_parser_error (parser, "expected %<while%>");
20035         return;
20036       case RT_EXTERN:
20037         cp_parser_error (parser, "expected %<extern%>");
20038         return;
20039       case RT_STATIC_ASSERT:
20040         cp_parser_error (parser, "expected %<static_assert%>");
20041         return;
20042       case RT_DECLTYPE:
20043         cp_parser_error (parser, "expected %<decltype%>");
20044         return;
20045       case RT_OPERATOR:
20046         cp_parser_error (parser, "expected %<operator%>");
20047         return;
20048       case RT_CLASS:
20049         cp_parser_error (parser, "expected %<class%>");
20050         return;
20051       case RT_TEMPLATE:
20052         cp_parser_error (parser, "expected %<template%>");
20053         return;
20054       case RT_NAMESPACE:
20055         cp_parser_error (parser, "expected %<namespace%>");
20056         return;
20057       case RT_USING:
20058         cp_parser_error (parser, "expected %<using%>");
20059         return;
20060       case RT_ASM:
20061         cp_parser_error (parser, "expected %<asm%>");
20062         return;
20063       case RT_TRY:
20064         cp_parser_error (parser, "expected %<try%>");
20065         return;
20066       case RT_CATCH:
20067         cp_parser_error (parser, "expected %<catch%>");
20068         return;
20069       case RT_THROW:
20070         cp_parser_error (parser, "expected %<throw%>");
20071         return;
20072       case RT_LABEL:
20073         cp_parser_error (parser, "expected %<__label__%>");
20074         return;
20075       case RT_AT_TRY:
20076         cp_parser_error (parser, "expected %<@try%>");
20077         return;
20078       case RT_AT_SYNCHRONIZED:
20079         cp_parser_error (parser, "expected %<@synchronized%>");
20080         return;
20081       case RT_AT_THROW:
20082         cp_parser_error (parser, "expected %<@throw%>");
20083         return;
20084       default:
20085         break;
20086     }
20087   if (!keyword)
20088     {
20089       switch (token_desc)
20090         {
20091           case RT_SEMICOLON:
20092             cp_parser_error (parser, "expected %<;%>");
20093             return;
20094           case RT_OPEN_PAREN:
20095             cp_parser_error (parser, "expected %<(%>");
20096             return;
20097           case RT_CLOSE_BRACE:
20098             cp_parser_error (parser, "expected %<}%>");
20099             return;
20100           case RT_OPEN_BRACE:
20101             cp_parser_error (parser, "expected %<{%>");
20102             return;
20103           case RT_CLOSE_SQUARE:
20104             cp_parser_error (parser, "expected %<]%>");
20105             return;
20106           case RT_OPEN_SQUARE:
20107             cp_parser_error (parser, "expected %<[%>");
20108             return;
20109           case RT_COMMA:
20110             cp_parser_error (parser, "expected %<,%>");
20111             return;
20112           case RT_SCOPE:
20113             cp_parser_error (parser, "expected %<::%>");
20114             return;
20115           case RT_LESS:
20116             cp_parser_error (parser, "expected %<<%>");
20117             return;
20118           case RT_GREATER:
20119             cp_parser_error (parser, "expected %<>%>");
20120             return;
20121           case RT_EQ:
20122             cp_parser_error (parser, "expected %<=%>");
20123             return;
20124           case RT_ELLIPSIS:
20125             cp_parser_error (parser, "expected %<...%>");
20126             return;
20127           case RT_MULT:
20128             cp_parser_error (parser, "expected %<*%>");
20129             return;
20130           case RT_COMPL:
20131             cp_parser_error (parser, "expected %<~%>");
20132             return;
20133           case RT_COLON:
20134             cp_parser_error (parser, "expected %<:%>");
20135             return;
20136           case RT_COLON_SCOPE:
20137             cp_parser_error (parser, "expected %<:%> or %<::%>");
20138             return;
20139           case RT_CLOSE_PAREN:
20140             cp_parser_error (parser, "expected %<)%>");
20141             return;
20142           case RT_COMMA_CLOSE_PAREN:
20143             cp_parser_error (parser, "expected %<,%> or %<)%>");
20144             return;
20145           case RT_PRAGMA_EOL:
20146             cp_parser_error (parser, "expected end of line");
20147             return;
20148           case RT_NAME:
20149             cp_parser_error (parser, "expected identifier");
20150             return;
20151           case RT_SELECT:
20152             cp_parser_error (parser, "expected selection-statement");
20153             return;
20154           case RT_INTERATION:
20155             cp_parser_error (parser, "expected iteration-statement");
20156             return;
20157           case RT_JUMP:
20158             cp_parser_error (parser, "expected jump-statement");
20159             return;
20160           case RT_CLASS_KEY:
20161             cp_parser_error (parser, "expected class-key");
20162             return;
20163           case RT_CLASS_TYPENAME_TEMPLATE:
20164             cp_parser_error (parser,
20165                  "expected %<class%>, %<typename%>, or %<template%>");
20166             return;
20167           default:
20168             gcc_unreachable ();
20169         }
20170     }
20171   else
20172     gcc_unreachable ();
20173 }
20174
20175
20176
20177 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20178    issue an error message indicating that TOKEN_DESC was expected.
20179
20180    Returns the token consumed, if the token had the appropriate type.
20181    Otherwise, returns NULL.  */
20182
20183 static cp_token *
20184 cp_parser_require (cp_parser* parser,
20185                    enum cpp_ttype type,
20186                    required_token token_desc)
20187 {
20188   if (cp_lexer_next_token_is (parser->lexer, type))
20189     return cp_lexer_consume_token (parser->lexer);
20190   else
20191     {
20192       /* Output the MESSAGE -- unless we're parsing tentatively.  */
20193       if (!cp_parser_simulate_error (parser))
20194         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20195       return NULL;
20196     }
20197 }
20198
20199 /* An error message is produced if the next token is not '>'.
20200    All further tokens are skipped until the desired token is
20201    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
20202
20203 static void
20204 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20205 {
20206   /* Current level of '< ... >'.  */
20207   unsigned level = 0;
20208   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
20209   unsigned nesting_depth = 0;
20210
20211   /* Are we ready, yet?  If not, issue error message.  */
20212   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20213     return;
20214
20215   /* Skip tokens until the desired token is found.  */
20216   while (true)
20217     {
20218       /* Peek at the next token.  */
20219       switch (cp_lexer_peek_token (parser->lexer)->type)
20220         {
20221         case CPP_LESS:
20222           if (!nesting_depth)
20223             ++level;
20224           break;
20225
20226         case CPP_RSHIFT:
20227           if (cxx_dialect == cxx98)
20228             /* C++0x views the `>>' operator as two `>' tokens, but
20229                C++98 does not. */
20230             break;
20231           else if (!nesting_depth && level-- == 0)
20232             {
20233               /* We've hit a `>>' where the first `>' closes the
20234                  template argument list, and the second `>' is
20235                  spurious.  Just consume the `>>' and stop; we've
20236                  already produced at least one error.  */
20237               cp_lexer_consume_token (parser->lexer);
20238               return;
20239             }
20240           /* Fall through for C++0x, so we handle the second `>' in
20241              the `>>'.  */
20242
20243         case CPP_GREATER:
20244           if (!nesting_depth && level-- == 0)
20245             {
20246               /* We've reached the token we want, consume it and stop.  */
20247               cp_lexer_consume_token (parser->lexer);
20248               return;
20249             }
20250           break;
20251
20252         case CPP_OPEN_PAREN:
20253         case CPP_OPEN_SQUARE:
20254           ++nesting_depth;
20255           break;
20256
20257         case CPP_CLOSE_PAREN:
20258         case CPP_CLOSE_SQUARE:
20259           if (nesting_depth-- == 0)
20260             return;
20261           break;
20262
20263         case CPP_EOF:
20264         case CPP_PRAGMA_EOL:
20265         case CPP_SEMICOLON:
20266         case CPP_OPEN_BRACE:
20267         case CPP_CLOSE_BRACE:
20268           /* The '>' was probably forgotten, don't look further.  */
20269           return;
20270
20271         default:
20272           break;
20273         }
20274
20275       /* Consume this token.  */
20276       cp_lexer_consume_token (parser->lexer);
20277     }
20278 }
20279
20280 /* If the next token is the indicated keyword, consume it.  Otherwise,
20281    issue an error message indicating that TOKEN_DESC was expected.
20282
20283    Returns the token consumed, if the token had the appropriate type.
20284    Otherwise, returns NULL.  */
20285
20286 static cp_token *
20287 cp_parser_require_keyword (cp_parser* parser,
20288                            enum rid keyword,
20289                            required_token token_desc)
20290 {
20291   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20292
20293   if (token && token->keyword != keyword)
20294     {
20295       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
20296       return NULL;
20297     }
20298
20299   return token;
20300 }
20301
20302 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20303    function-definition.  */
20304
20305 static bool
20306 cp_parser_token_starts_function_definition_p (cp_token* token)
20307 {
20308   return (/* An ordinary function-body begins with an `{'.  */
20309           token->type == CPP_OPEN_BRACE
20310           /* A ctor-initializer begins with a `:'.  */
20311           || token->type == CPP_COLON
20312           /* A function-try-block begins with `try'.  */
20313           || token->keyword == RID_TRY
20314           /* The named return value extension begins with `return'.  */
20315           || token->keyword == RID_RETURN);
20316 }
20317
20318 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20319    definition.  */
20320
20321 static bool
20322 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20323 {
20324   cp_token *token;
20325
20326   token = cp_lexer_peek_token (parser->lexer);
20327   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20328 }
20329
20330 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20331    C++0x) ending a template-argument.  */
20332
20333 static bool
20334 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20335 {
20336   cp_token *token;
20337
20338   token = cp_lexer_peek_token (parser->lexer);
20339   return (token->type == CPP_COMMA 
20340           || token->type == CPP_GREATER
20341           || token->type == CPP_ELLIPSIS
20342           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20343 }
20344
20345 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20346    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
20347
20348 static bool
20349 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20350                                                      size_t n)
20351 {
20352   cp_token *token;
20353
20354   token = cp_lexer_peek_nth_token (parser->lexer, n);
20355   if (token->type == CPP_LESS)
20356     return true;
20357   /* Check for the sequence `<::' in the original code. It would be lexed as
20358      `[:', where `[' is a digraph, and there is no whitespace before
20359      `:'.  */
20360   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20361     {
20362       cp_token *token2;
20363       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20364       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20365         return true;
20366     }
20367   return false;
20368 }
20369
20370 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20371    or none_type otherwise.  */
20372
20373 static enum tag_types
20374 cp_parser_token_is_class_key (cp_token* token)
20375 {
20376   switch (token->keyword)
20377     {
20378     case RID_CLASS:
20379       return class_type;
20380     case RID_STRUCT:
20381       return record_type;
20382     case RID_UNION:
20383       return union_type;
20384
20385     default:
20386       return none_type;
20387     }
20388 }
20389
20390 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
20391
20392 static void
20393 cp_parser_check_class_key (enum tag_types class_key, tree type)
20394 {
20395   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20396     permerror (input_location, "%qs tag used in naming %q#T",
20397             class_key == union_type ? "union"
20398              : class_key == record_type ? "struct" : "class",
20399              type);
20400 }
20401
20402 /* Issue an error message if DECL is redeclared with different
20403    access than its original declaration [class.access.spec/3].
20404    This applies to nested classes and nested class templates.
20405    [class.mem/1].  */
20406
20407 static void
20408 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20409 {
20410   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20411     return;
20412
20413   if ((TREE_PRIVATE (decl)
20414        != (current_access_specifier == access_private_node))
20415       || (TREE_PROTECTED (decl)
20416           != (current_access_specifier == access_protected_node)))
20417     error_at (location, "%qD redeclared with different access", decl);
20418 }
20419
20420 /* Look for the `template' keyword, as a syntactic disambiguator.
20421    Return TRUE iff it is present, in which case it will be
20422    consumed.  */
20423
20424 static bool
20425 cp_parser_optional_template_keyword (cp_parser *parser)
20426 {
20427   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20428     {
20429       /* The `template' keyword can only be used within templates;
20430          outside templates the parser can always figure out what is a
20431          template and what is not.  */
20432       if (!processing_template_decl)
20433         {
20434           cp_token *token = cp_lexer_peek_token (parser->lexer);
20435           error_at (token->location,
20436                     "%<template%> (as a disambiguator) is only allowed "
20437                     "within templates");
20438           /* If this part of the token stream is rescanned, the same
20439              error message would be generated.  So, we purge the token
20440              from the stream.  */
20441           cp_lexer_purge_token (parser->lexer);
20442           return false;
20443         }
20444       else
20445         {
20446           /* Consume the `template' keyword.  */
20447           cp_lexer_consume_token (parser->lexer);
20448           return true;
20449         }
20450     }
20451
20452   return false;
20453 }
20454
20455 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
20456    set PARSER->SCOPE, and perform other related actions.  */
20457
20458 static void
20459 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20460 {
20461   int i;
20462   struct tree_check *check_value;
20463   deferred_access_check *chk;
20464   VEC (deferred_access_check,gc) *checks;
20465
20466   /* Get the stored value.  */
20467   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
20468   /* Perform any access checks that were deferred.  */
20469   checks = check_value->checks;
20470   if (checks)
20471     {
20472       for (i = 0 ;
20473            VEC_iterate (deferred_access_check, checks, i, chk) ;
20474            ++i)
20475         {
20476           perform_or_defer_access_check (chk->binfo,
20477                                          chk->decl,
20478                                          chk->diag_decl);
20479         }
20480     }
20481   /* Set the scope from the stored value.  */
20482   parser->scope = check_value->value;
20483   parser->qualifying_scope = check_value->qualifying_scope;
20484   parser->object_scope = NULL_TREE;
20485 }
20486
20487 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
20488    encounter the end of a block before what we were looking for.  */
20489
20490 static bool
20491 cp_parser_cache_group (cp_parser *parser,
20492                        enum cpp_ttype end,
20493                        unsigned depth)
20494 {
20495   while (true)
20496     {
20497       cp_token *token = cp_lexer_peek_token (parser->lexer);
20498
20499       /* Abort a parenthesized expression if we encounter a semicolon.  */
20500       if ((end == CPP_CLOSE_PAREN || depth == 0)
20501           && token->type == CPP_SEMICOLON)
20502         return true;
20503       /* If we've reached the end of the file, stop.  */
20504       if (token->type == CPP_EOF
20505           || (end != CPP_PRAGMA_EOL
20506               && token->type == CPP_PRAGMA_EOL))
20507         return true;
20508       if (token->type == CPP_CLOSE_BRACE && depth == 0)
20509         /* We've hit the end of an enclosing block, so there's been some
20510            kind of syntax error.  */
20511         return true;
20512
20513       /* Consume the token.  */
20514       cp_lexer_consume_token (parser->lexer);
20515       /* See if it starts a new group.  */
20516       if (token->type == CPP_OPEN_BRACE)
20517         {
20518           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
20519           /* In theory this should probably check end == '}', but
20520              cp_parser_save_member_function_body needs it to exit
20521              after either '}' or ')' when called with ')'.  */
20522           if (depth == 0)
20523             return false;
20524         }
20525       else if (token->type == CPP_OPEN_PAREN)
20526         {
20527           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
20528           if (depth == 0 && end == CPP_CLOSE_PAREN)
20529             return false;
20530         }
20531       else if (token->type == CPP_PRAGMA)
20532         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
20533       else if (token->type == end)
20534         return false;
20535     }
20536 }
20537
20538 /* Begin parsing tentatively.  We always save tokens while parsing
20539    tentatively so that if the tentative parsing fails we can restore the
20540    tokens.  */
20541
20542 static void
20543 cp_parser_parse_tentatively (cp_parser* parser)
20544 {
20545   /* Enter a new parsing context.  */
20546   parser->context = cp_parser_context_new (parser->context);
20547   /* Begin saving tokens.  */
20548   cp_lexer_save_tokens (parser->lexer);
20549   /* In order to avoid repetitive access control error messages,
20550      access checks are queued up until we are no longer parsing
20551      tentatively.  */
20552   push_deferring_access_checks (dk_deferred);
20553 }
20554
20555 /* Commit to the currently active tentative parse.  */
20556
20557 static void
20558 cp_parser_commit_to_tentative_parse (cp_parser* parser)
20559 {
20560   cp_parser_context *context;
20561   cp_lexer *lexer;
20562
20563   /* Mark all of the levels as committed.  */
20564   lexer = parser->lexer;
20565   for (context = parser->context; context->next; context = context->next)
20566     {
20567       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20568         break;
20569       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20570       while (!cp_lexer_saving_tokens (lexer))
20571         lexer = lexer->next;
20572       cp_lexer_commit_tokens (lexer);
20573     }
20574 }
20575
20576 /* Abort the currently active tentative parse.  All consumed tokens
20577    will be rolled back, and no diagnostics will be issued.  */
20578
20579 static void
20580 cp_parser_abort_tentative_parse (cp_parser* parser)
20581 {
20582   cp_parser_simulate_error (parser);
20583   /* Now, pretend that we want to see if the construct was
20584      successfully parsed.  */
20585   cp_parser_parse_definitely (parser);
20586 }
20587
20588 /* Stop parsing tentatively.  If a parse error has occurred, restore the
20589    token stream.  Otherwise, commit to the tokens we have consumed.
20590    Returns true if no error occurred; false otherwise.  */
20591
20592 static bool
20593 cp_parser_parse_definitely (cp_parser* parser)
20594 {
20595   bool error_occurred;
20596   cp_parser_context *context;
20597
20598   /* Remember whether or not an error occurred, since we are about to
20599      destroy that information.  */
20600   error_occurred = cp_parser_error_occurred (parser);
20601   /* Remove the topmost context from the stack.  */
20602   context = parser->context;
20603   parser->context = context->next;
20604   /* If no parse errors occurred, commit to the tentative parse.  */
20605   if (!error_occurred)
20606     {
20607       /* Commit to the tokens read tentatively, unless that was
20608          already done.  */
20609       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20610         cp_lexer_commit_tokens (parser->lexer);
20611
20612       pop_to_parent_deferring_access_checks ();
20613     }
20614   /* Otherwise, if errors occurred, roll back our state so that things
20615      are just as they were before we began the tentative parse.  */
20616   else
20617     {
20618       cp_lexer_rollback_tokens (parser->lexer);
20619       pop_deferring_access_checks ();
20620     }
20621   /* Add the context to the front of the free list.  */
20622   context->next = cp_parser_context_free_list;
20623   cp_parser_context_free_list = context;
20624
20625   return !error_occurred;
20626 }
20627
20628 /* Returns true if we are parsing tentatively and are not committed to
20629    this tentative parse.  */
20630
20631 static bool
20632 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20633 {
20634   return (cp_parser_parsing_tentatively (parser)
20635           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20636 }
20637
20638 /* Returns nonzero iff an error has occurred during the most recent
20639    tentative parse.  */
20640
20641 static bool
20642 cp_parser_error_occurred (cp_parser* parser)
20643 {
20644   return (cp_parser_parsing_tentatively (parser)
20645           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20646 }
20647
20648 /* Returns nonzero if GNU extensions are allowed.  */
20649
20650 static bool
20651 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20652 {
20653   return parser->allow_gnu_extensions_p;
20654 }
20655 \f
20656 /* Objective-C++ Productions */
20657
20658
20659 /* Parse an Objective-C expression, which feeds into a primary-expression
20660    above.
20661
20662    objc-expression:
20663      objc-message-expression
20664      objc-string-literal
20665      objc-encode-expression
20666      objc-protocol-expression
20667      objc-selector-expression
20668
20669   Returns a tree representation of the expression.  */
20670
20671 static tree
20672 cp_parser_objc_expression (cp_parser* parser)
20673 {
20674   /* Try to figure out what kind of declaration is present.  */
20675   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20676
20677   switch (kwd->type)
20678     {
20679     case CPP_OPEN_SQUARE:
20680       return cp_parser_objc_message_expression (parser);
20681
20682     case CPP_OBJC_STRING:
20683       kwd = cp_lexer_consume_token (parser->lexer);
20684       return objc_build_string_object (kwd->u.value);
20685
20686     case CPP_KEYWORD:
20687       switch (kwd->keyword)
20688         {
20689         case RID_AT_ENCODE:
20690           return cp_parser_objc_encode_expression (parser);
20691
20692         case RID_AT_PROTOCOL:
20693           return cp_parser_objc_protocol_expression (parser);
20694
20695         case RID_AT_SELECTOR:
20696           return cp_parser_objc_selector_expression (parser);
20697
20698         default:
20699           break;
20700         }
20701     default:
20702       error_at (kwd->location,
20703                 "misplaced %<@%D%> Objective-C++ construct",
20704                 kwd->u.value);
20705       cp_parser_skip_to_end_of_block_or_statement (parser);
20706     }
20707
20708   return error_mark_node;
20709 }
20710
20711 /* Parse an Objective-C message expression.
20712
20713    objc-message-expression:
20714      [ objc-message-receiver objc-message-args ]
20715
20716    Returns a representation of an Objective-C message.  */
20717
20718 static tree
20719 cp_parser_objc_message_expression (cp_parser* parser)
20720 {
20721   tree receiver, messageargs;
20722
20723   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20724   receiver = cp_parser_objc_message_receiver (parser);
20725   messageargs = cp_parser_objc_message_args (parser);
20726   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20727
20728   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20729 }
20730
20731 /* Parse an objc-message-receiver.
20732
20733    objc-message-receiver:
20734      expression
20735      simple-type-specifier
20736
20737   Returns a representation of the type or expression.  */
20738
20739 static tree
20740 cp_parser_objc_message_receiver (cp_parser* parser)
20741 {
20742   tree rcv;
20743
20744   /* An Objective-C message receiver may be either (1) a type
20745      or (2) an expression.  */
20746   cp_parser_parse_tentatively (parser);
20747   rcv = cp_parser_expression (parser, false, NULL);
20748
20749   if (cp_parser_parse_definitely (parser))
20750     return rcv;
20751
20752   rcv = cp_parser_simple_type_specifier (parser,
20753                                          /*decl_specs=*/NULL,
20754                                          CP_PARSER_FLAGS_NONE);
20755
20756   return objc_get_class_reference (rcv);
20757 }
20758
20759 /* Parse the arguments and selectors comprising an Objective-C message.
20760
20761    objc-message-args:
20762      objc-selector
20763      objc-selector-args
20764      objc-selector-args , objc-comma-args
20765
20766    objc-selector-args:
20767      objc-selector [opt] : assignment-expression
20768      objc-selector-args objc-selector [opt] : assignment-expression
20769
20770    objc-comma-args:
20771      assignment-expression
20772      objc-comma-args , assignment-expression
20773
20774    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20775    selector arguments and TREE_VALUE containing a list of comma
20776    arguments.  */
20777
20778 static tree
20779 cp_parser_objc_message_args (cp_parser* parser)
20780 {
20781   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20782   bool maybe_unary_selector_p = true;
20783   cp_token *token = cp_lexer_peek_token (parser->lexer);
20784
20785   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20786     {
20787       tree selector = NULL_TREE, arg;
20788
20789       if (token->type != CPP_COLON)
20790         selector = cp_parser_objc_selector (parser);
20791
20792       /* Detect if we have a unary selector.  */
20793       if (maybe_unary_selector_p
20794           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20795         return build_tree_list (selector, NULL_TREE);
20796
20797       maybe_unary_selector_p = false;
20798       cp_parser_require (parser, CPP_COLON, RT_COLON);
20799       arg = cp_parser_assignment_expression (parser, false, NULL);
20800
20801       sel_args
20802         = chainon (sel_args,
20803                    build_tree_list (selector, arg));
20804
20805       token = cp_lexer_peek_token (parser->lexer);
20806     }
20807
20808   /* Handle non-selector arguments, if any. */
20809   while (token->type == CPP_COMMA)
20810     {
20811       tree arg;
20812
20813       cp_lexer_consume_token (parser->lexer);
20814       arg = cp_parser_assignment_expression (parser, false, NULL);
20815
20816       addl_args
20817         = chainon (addl_args,
20818                    build_tree_list (NULL_TREE, arg));
20819
20820       token = cp_lexer_peek_token (parser->lexer);
20821     }
20822
20823   return build_tree_list (sel_args, addl_args);
20824 }
20825
20826 /* Parse an Objective-C encode expression.
20827
20828    objc-encode-expression:
20829      @encode objc-typename
20830
20831    Returns an encoded representation of the type argument.  */
20832
20833 static tree
20834 cp_parser_objc_encode_expression (cp_parser* parser)
20835 {
20836   tree type;
20837   cp_token *token;
20838
20839   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20840   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20841   token = cp_lexer_peek_token (parser->lexer);
20842   type = complete_type (cp_parser_type_id (parser));
20843   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20844
20845   if (!type)
20846     {
20847       error_at (token->location, 
20848                 "%<@encode%> must specify a type as an argument");
20849       return error_mark_node;
20850     }
20851
20852   return objc_build_encode_expr (type);
20853 }
20854
20855 /* Parse an Objective-C @defs expression.  */
20856
20857 static tree
20858 cp_parser_objc_defs_expression (cp_parser *parser)
20859 {
20860   tree name;
20861
20862   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20863   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20864   name = cp_parser_identifier (parser);
20865   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20866
20867   return objc_get_class_ivars (name);
20868 }
20869
20870 /* Parse an Objective-C protocol expression.
20871
20872   objc-protocol-expression:
20873     @protocol ( identifier )
20874
20875   Returns a representation of the protocol expression.  */
20876
20877 static tree
20878 cp_parser_objc_protocol_expression (cp_parser* parser)
20879 {
20880   tree proto;
20881
20882   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20883   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20884   proto = cp_parser_identifier (parser);
20885   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20886
20887   return objc_build_protocol_expr (proto);
20888 }
20889
20890 /* Parse an Objective-C selector expression.
20891
20892    objc-selector-expression:
20893      @selector ( objc-method-signature )
20894
20895    objc-method-signature:
20896      objc-selector
20897      objc-selector-seq
20898
20899    objc-selector-seq:
20900      objc-selector :
20901      objc-selector-seq objc-selector :
20902
20903   Returns a representation of the method selector.  */
20904
20905 static tree
20906 cp_parser_objc_selector_expression (cp_parser* parser)
20907 {
20908   tree sel_seq = NULL_TREE;
20909   bool maybe_unary_selector_p = true;
20910   cp_token *token;
20911   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20912
20913   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20914   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20915   token = cp_lexer_peek_token (parser->lexer);
20916
20917   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20918          || token->type == CPP_SCOPE)
20919     {
20920       tree selector = NULL_TREE;
20921
20922       if (token->type != CPP_COLON
20923           || token->type == CPP_SCOPE)
20924         selector = cp_parser_objc_selector (parser);
20925
20926       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20927           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20928         {
20929           /* Detect if we have a unary selector.  */
20930           if (maybe_unary_selector_p)
20931             {
20932               sel_seq = selector;
20933               goto finish_selector;
20934             }
20935           else
20936             {
20937               cp_parser_error (parser, "expected %<:%>");
20938             }
20939         }
20940       maybe_unary_selector_p = false;
20941       token = cp_lexer_consume_token (parser->lexer);
20942
20943       if (token->type == CPP_SCOPE)
20944         {
20945           sel_seq
20946             = chainon (sel_seq,
20947                        build_tree_list (selector, NULL_TREE));
20948           sel_seq
20949             = chainon (sel_seq,
20950                        build_tree_list (NULL_TREE, NULL_TREE));
20951         }
20952       else
20953         sel_seq
20954           = chainon (sel_seq,
20955                      build_tree_list (selector, NULL_TREE));
20956
20957       token = cp_lexer_peek_token (parser->lexer);
20958     }
20959
20960  finish_selector:
20961   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20962
20963   return objc_build_selector_expr (loc, sel_seq);
20964 }
20965
20966 /* Parse a list of identifiers.
20967
20968    objc-identifier-list:
20969      identifier
20970      objc-identifier-list , identifier
20971
20972    Returns a TREE_LIST of identifier nodes.  */
20973
20974 static tree
20975 cp_parser_objc_identifier_list (cp_parser* parser)
20976 {
20977   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20978   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20979
20980   while (sep->type == CPP_COMMA)
20981     {
20982       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20983       list = chainon (list,
20984                       build_tree_list (NULL_TREE,
20985                                        cp_parser_identifier (parser)));
20986       sep = cp_lexer_peek_token (parser->lexer);
20987     }
20988
20989   return list;
20990 }
20991
20992 /* Parse an Objective-C alias declaration.
20993
20994    objc-alias-declaration:
20995      @compatibility_alias identifier identifier ;
20996
20997    This function registers the alias mapping with the Objective-C front end.
20998    It returns nothing.  */
20999
21000 static void
21001 cp_parser_objc_alias_declaration (cp_parser* parser)
21002 {
21003   tree alias, orig;
21004
21005   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21006   alias = cp_parser_identifier (parser);
21007   orig = cp_parser_identifier (parser);
21008   objc_declare_alias (alias, orig);
21009   cp_parser_consume_semicolon_at_end_of_statement (parser);
21010 }
21011
21012 /* Parse an Objective-C class forward-declaration.
21013
21014    objc-class-declaration:
21015      @class objc-identifier-list ;
21016
21017    The function registers the forward declarations with the Objective-C
21018    front end.  It returns nothing.  */
21019
21020 static void
21021 cp_parser_objc_class_declaration (cp_parser* parser)
21022 {
21023   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21024   objc_declare_class (cp_parser_objc_identifier_list (parser));
21025   cp_parser_consume_semicolon_at_end_of_statement (parser);
21026 }
21027
21028 /* Parse a list of Objective-C protocol references.
21029
21030    objc-protocol-refs-opt:
21031      objc-protocol-refs [opt]
21032
21033    objc-protocol-refs:
21034      < objc-identifier-list >
21035
21036    Returns a TREE_LIST of identifiers, if any.  */
21037
21038 static tree
21039 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21040 {
21041   tree protorefs = NULL_TREE;
21042
21043   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21044     {
21045       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21046       protorefs = cp_parser_objc_identifier_list (parser);
21047       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21048     }
21049
21050   return protorefs;
21051 }
21052
21053 /* Parse a Objective-C visibility specification.  */
21054
21055 static void
21056 cp_parser_objc_visibility_spec (cp_parser* parser)
21057 {
21058   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21059
21060   switch (vis->keyword)
21061     {
21062     case RID_AT_PRIVATE:
21063       objc_set_visibility (2);
21064       break;
21065     case RID_AT_PROTECTED:
21066       objc_set_visibility (0);
21067       break;
21068     case RID_AT_PUBLIC:
21069       objc_set_visibility (1);
21070       break;
21071     default:
21072       return;
21073     }
21074
21075   /* Eat '@private'/'@protected'/'@public'.  */
21076   cp_lexer_consume_token (parser->lexer);
21077 }
21078
21079 /* Parse an Objective-C method type.  */
21080
21081 static void
21082 cp_parser_objc_method_type (cp_parser* parser)
21083 {
21084   objc_set_method_type
21085    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
21086     ? PLUS_EXPR
21087     : MINUS_EXPR);
21088 }
21089
21090 /* Parse an Objective-C protocol qualifier.  */
21091
21092 static tree
21093 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21094 {
21095   tree quals = NULL_TREE, node;
21096   cp_token *token = cp_lexer_peek_token (parser->lexer);
21097
21098   node = token->u.value;
21099
21100   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21101          && (node == ridpointers [(int) RID_IN]
21102              || node == ridpointers [(int) RID_OUT]
21103              || node == ridpointers [(int) RID_INOUT]
21104              || node == ridpointers [(int) RID_BYCOPY]
21105              || node == ridpointers [(int) RID_BYREF]
21106              || node == ridpointers [(int) RID_ONEWAY]))
21107     {
21108       quals = tree_cons (NULL_TREE, node, quals);
21109       cp_lexer_consume_token (parser->lexer);
21110       token = cp_lexer_peek_token (parser->lexer);
21111       node = token->u.value;
21112     }
21113
21114   return quals;
21115 }
21116
21117 /* Parse an Objective-C typename.  */
21118
21119 static tree
21120 cp_parser_objc_typename (cp_parser* parser)
21121 {
21122   tree type_name = NULL_TREE;
21123
21124   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21125     {
21126       tree proto_quals, cp_type = NULL_TREE;
21127
21128       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21129       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21130
21131       /* An ObjC type name may consist of just protocol qualifiers, in which
21132          case the type shall default to 'id'.  */
21133       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21134         cp_type = cp_parser_type_id (parser);
21135
21136       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21137       type_name = build_tree_list (proto_quals, cp_type);
21138     }
21139
21140   return type_name;
21141 }
21142
21143 /* Check to see if TYPE refers to an Objective-C selector name.  */
21144
21145 static bool
21146 cp_parser_objc_selector_p (enum cpp_ttype type)
21147 {
21148   return (type == CPP_NAME || type == CPP_KEYWORD
21149           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21150           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21151           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21152           || type == CPP_XOR || type == CPP_XOR_EQ);
21153 }
21154
21155 /* Parse an Objective-C selector.  */
21156
21157 static tree
21158 cp_parser_objc_selector (cp_parser* parser)
21159 {
21160   cp_token *token = cp_lexer_consume_token (parser->lexer);
21161
21162   if (!cp_parser_objc_selector_p (token->type))
21163     {
21164       error_at (token->location, "invalid Objective-C++ selector name");
21165       return error_mark_node;
21166     }
21167
21168   /* C++ operator names are allowed to appear in ObjC selectors.  */
21169   switch (token->type)
21170     {
21171     case CPP_AND_AND: return get_identifier ("and");
21172     case CPP_AND_EQ: return get_identifier ("and_eq");
21173     case CPP_AND: return get_identifier ("bitand");
21174     case CPP_OR: return get_identifier ("bitor");
21175     case CPP_COMPL: return get_identifier ("compl");
21176     case CPP_NOT: return get_identifier ("not");
21177     case CPP_NOT_EQ: return get_identifier ("not_eq");
21178     case CPP_OR_OR: return get_identifier ("or");
21179     case CPP_OR_EQ: return get_identifier ("or_eq");
21180     case CPP_XOR: return get_identifier ("xor");
21181     case CPP_XOR_EQ: return get_identifier ("xor_eq");
21182     default: return token->u.value;
21183     }
21184 }
21185
21186 /* Parse an Objective-C params list.  */
21187
21188 static tree
21189 cp_parser_objc_method_keyword_params (cp_parser* parser)
21190 {
21191   tree params = NULL_TREE;
21192   bool maybe_unary_selector_p = true;
21193   cp_token *token = cp_lexer_peek_token (parser->lexer);
21194
21195   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21196     {
21197       tree selector = NULL_TREE, type_name, identifier;
21198
21199       if (token->type != CPP_COLON)
21200         selector = cp_parser_objc_selector (parser);
21201
21202       /* Detect if we have a unary selector.  */
21203       if (maybe_unary_selector_p
21204           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21205         return selector;
21206
21207       maybe_unary_selector_p = false;
21208       cp_parser_require (parser, CPP_COLON, RT_COLON);
21209       type_name = cp_parser_objc_typename (parser);
21210       identifier = cp_parser_identifier (parser);
21211
21212       params
21213         = chainon (params,
21214                    objc_build_keyword_decl (selector,
21215                                             type_name,
21216                                             identifier));
21217
21218       token = cp_lexer_peek_token (parser->lexer);
21219     }
21220
21221   return params;
21222 }
21223
21224 /* Parse the non-keyword Objective-C params.  */
21225
21226 static tree
21227 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
21228 {
21229   tree params = make_node (TREE_LIST);
21230   cp_token *token = cp_lexer_peek_token (parser->lexer);
21231   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
21232
21233   while (token->type == CPP_COMMA)
21234     {
21235       cp_parameter_declarator *parmdecl;
21236       tree parm;
21237
21238       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21239       token = cp_lexer_peek_token (parser->lexer);
21240
21241       if (token->type == CPP_ELLIPSIS)
21242         {
21243           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
21244           *ellipsisp = true;
21245           break;
21246         }
21247
21248       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21249       parm = grokdeclarator (parmdecl->declarator,
21250                              &parmdecl->decl_specifiers,
21251                              PARM, /*initialized=*/0,
21252                              /*attrlist=*/NULL);
21253
21254       chainon (params, build_tree_list (NULL_TREE, parm));
21255       token = cp_lexer_peek_token (parser->lexer);
21256     }
21257
21258   return params;
21259 }
21260
21261 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
21262
21263 static void
21264 cp_parser_objc_interstitial_code (cp_parser* parser)
21265 {
21266   cp_token *token = cp_lexer_peek_token (parser->lexer);
21267
21268   /* If the next token is `extern' and the following token is a string
21269      literal, then we have a linkage specification.  */
21270   if (token->keyword == RID_EXTERN
21271       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21272     cp_parser_linkage_specification (parser);
21273   /* Handle #pragma, if any.  */
21274   else if (token->type == CPP_PRAGMA)
21275     cp_parser_pragma (parser, pragma_external);
21276   /* Allow stray semicolons.  */
21277   else if (token->type == CPP_SEMICOLON)
21278     cp_lexer_consume_token (parser->lexer);
21279   /* Finally, try to parse a block-declaration, or a function-definition.  */
21280   else
21281     cp_parser_block_declaration (parser, /*statement_p=*/false);
21282 }
21283
21284 /* Parse a method signature.  */
21285
21286 static tree
21287 cp_parser_objc_method_signature (cp_parser* parser)
21288 {
21289   tree rettype, kwdparms, optparms;
21290   bool ellipsis = false;
21291
21292   cp_parser_objc_method_type (parser);
21293   rettype = cp_parser_objc_typename (parser);
21294   kwdparms = cp_parser_objc_method_keyword_params (parser);
21295   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
21296
21297   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
21298 }
21299
21300 /* Pars an Objective-C method prototype list.  */
21301
21302 static void
21303 cp_parser_objc_method_prototype_list (cp_parser* parser)
21304 {
21305   cp_token *token = cp_lexer_peek_token (parser->lexer);
21306
21307   while (token->keyword != RID_AT_END)
21308     {
21309       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21310         {
21311           objc_add_method_declaration
21312            (cp_parser_objc_method_signature (parser));
21313           cp_parser_consume_semicolon_at_end_of_statement (parser);
21314         }
21315       else
21316         /* Allow for interspersed non-ObjC++ code.  */
21317         cp_parser_objc_interstitial_code (parser);
21318
21319       token = cp_lexer_peek_token (parser->lexer);
21320     }
21321
21322   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21323   objc_finish_interface ();
21324 }
21325
21326 /* Parse an Objective-C method definition list.  */
21327
21328 static void
21329 cp_parser_objc_method_definition_list (cp_parser* parser)
21330 {
21331   cp_token *token = cp_lexer_peek_token (parser->lexer);
21332
21333   while (token->keyword != RID_AT_END)
21334     {
21335       tree meth;
21336
21337       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21338         {
21339           push_deferring_access_checks (dk_deferred);
21340           objc_start_method_definition
21341            (cp_parser_objc_method_signature (parser));
21342
21343           /* For historical reasons, we accept an optional semicolon.  */
21344           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21345             cp_lexer_consume_token (parser->lexer);
21346
21347           perform_deferred_access_checks ();
21348           stop_deferring_access_checks ();
21349           meth = cp_parser_function_definition_after_declarator (parser,
21350                                                                  false);
21351           pop_deferring_access_checks ();
21352           objc_finish_method_definition (meth);
21353         }
21354       else
21355         /* Allow for interspersed non-ObjC++ code.  */
21356         cp_parser_objc_interstitial_code (parser);
21357
21358       token = cp_lexer_peek_token (parser->lexer);
21359     }
21360
21361   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21362   objc_finish_implementation ();
21363 }
21364
21365 /* Parse Objective-C ivars.  */
21366
21367 static void
21368 cp_parser_objc_class_ivars (cp_parser* parser)
21369 {
21370   cp_token *token = cp_lexer_peek_token (parser->lexer);
21371
21372   if (token->type != CPP_OPEN_BRACE)
21373     return;     /* No ivars specified.  */
21374
21375   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
21376   token = cp_lexer_peek_token (parser->lexer);
21377
21378   while (token->type != CPP_CLOSE_BRACE)
21379     {
21380       cp_decl_specifier_seq declspecs;
21381       int decl_class_or_enum_p;
21382       tree prefix_attributes;
21383
21384       cp_parser_objc_visibility_spec (parser);
21385
21386       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21387         break;
21388
21389       cp_parser_decl_specifier_seq (parser,
21390                                     CP_PARSER_FLAGS_OPTIONAL,
21391                                     &declspecs,
21392                                     &decl_class_or_enum_p);
21393       prefix_attributes = declspecs.attributes;
21394       declspecs.attributes = NULL_TREE;
21395
21396       /* Keep going until we hit the `;' at the end of the
21397          declaration.  */
21398       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21399         {
21400           tree width = NULL_TREE, attributes, first_attribute, decl;
21401           cp_declarator *declarator = NULL;
21402           int ctor_dtor_or_conv_p;
21403
21404           /* Check for a (possibly unnamed) bitfield declaration.  */
21405           token = cp_lexer_peek_token (parser->lexer);
21406           if (token->type == CPP_COLON)
21407             goto eat_colon;
21408
21409           if (token->type == CPP_NAME
21410               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21411                   == CPP_COLON))
21412             {
21413               /* Get the name of the bitfield.  */
21414               declarator = make_id_declarator (NULL_TREE,
21415                                                cp_parser_identifier (parser),
21416                                                sfk_none);
21417
21418              eat_colon:
21419               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
21420               /* Get the width of the bitfield.  */
21421               width
21422                 = cp_parser_constant_expression (parser,
21423                                                  /*allow_non_constant=*/false,
21424                                                  NULL);
21425             }
21426           else
21427             {
21428               /* Parse the declarator.  */
21429               declarator
21430                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21431                                         &ctor_dtor_or_conv_p,
21432                                         /*parenthesized_p=*/NULL,
21433                                         /*member_p=*/false);
21434             }
21435
21436           /* Look for attributes that apply to the ivar.  */
21437           attributes = cp_parser_attributes_opt (parser);
21438           /* Remember which attributes are prefix attributes and
21439              which are not.  */
21440           first_attribute = attributes;
21441           /* Combine the attributes.  */
21442           attributes = chainon (prefix_attributes, attributes);
21443
21444           if (width)
21445               /* Create the bitfield declaration.  */
21446               decl = grokbitfield (declarator, &declspecs,
21447                                    width,
21448                                    attributes);
21449           else
21450             decl = grokfield (declarator, &declspecs,
21451                               NULL_TREE, /*init_const_expr_p=*/false,
21452                               NULL_TREE, attributes);
21453
21454           /* Add the instance variable.  */
21455           objc_add_instance_variable (decl);
21456
21457           /* Reset PREFIX_ATTRIBUTES.  */
21458           while (attributes && TREE_CHAIN (attributes) != first_attribute)
21459             attributes = TREE_CHAIN (attributes);
21460           if (attributes)
21461             TREE_CHAIN (attributes) = NULL_TREE;
21462
21463           token = cp_lexer_peek_token (parser->lexer);
21464
21465           if (token->type == CPP_COMMA)
21466             {
21467               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21468               continue;
21469             }
21470           break;
21471         }
21472
21473       cp_parser_consume_semicolon_at_end_of_statement (parser);
21474       token = cp_lexer_peek_token (parser->lexer);
21475     }
21476
21477   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
21478   /* For historical reasons, we accept an optional semicolon.  */
21479   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21480     cp_lexer_consume_token (parser->lexer);
21481 }
21482
21483 /* Parse an Objective-C protocol declaration.  */
21484
21485 static void
21486 cp_parser_objc_protocol_declaration (cp_parser* parser)
21487 {
21488   tree proto, protorefs;
21489   cp_token *tok;
21490
21491   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21492   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
21493     {
21494       tok = cp_lexer_peek_token (parser->lexer);
21495       error_at (tok->location, "identifier expected after %<@protocol%>");
21496       goto finish;
21497     }
21498
21499   /* See if we have a forward declaration or a definition.  */
21500   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
21501
21502   /* Try a forward declaration first.  */
21503   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
21504     {
21505       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
21506      finish:
21507       cp_parser_consume_semicolon_at_end_of_statement (parser);
21508     }
21509
21510   /* Ok, we got a full-fledged definition (or at least should).  */
21511   else
21512     {
21513       proto = cp_parser_identifier (parser);
21514       protorefs = cp_parser_objc_protocol_refs_opt (parser);
21515       objc_start_protocol (proto, protorefs);
21516       cp_parser_objc_method_prototype_list (parser);
21517     }
21518 }
21519
21520 /* Parse an Objective-C superclass or category.  */
21521
21522 static void
21523 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
21524                                                           tree *categ)
21525 {
21526   cp_token *next = cp_lexer_peek_token (parser->lexer);
21527
21528   *super = *categ = NULL_TREE;
21529   if (next->type == CPP_COLON)
21530     {
21531       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
21532       *super = cp_parser_identifier (parser);
21533     }
21534   else if (next->type == CPP_OPEN_PAREN)
21535     {
21536       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21537       *categ = cp_parser_identifier (parser);
21538       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21539     }
21540 }
21541
21542 /* Parse an Objective-C class interface.  */
21543
21544 static void
21545 cp_parser_objc_class_interface (cp_parser* parser)
21546 {
21547   tree name, super, categ, protos;
21548
21549   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
21550   name = cp_parser_identifier (parser);
21551   cp_parser_objc_superclass_or_category (parser, &super, &categ);
21552   protos = cp_parser_objc_protocol_refs_opt (parser);
21553
21554   /* We have either a class or a category on our hands.  */
21555   if (categ)
21556     objc_start_category_interface (name, categ, protos);
21557   else
21558     {
21559       objc_start_class_interface (name, super, protos);
21560       /* Handle instance variable declarations, if any.  */
21561       cp_parser_objc_class_ivars (parser);
21562       objc_continue_interface ();
21563     }
21564
21565   cp_parser_objc_method_prototype_list (parser);
21566 }
21567
21568 /* Parse an Objective-C class implementation.  */
21569
21570 static void
21571 cp_parser_objc_class_implementation (cp_parser* parser)
21572 {
21573   tree name, super, categ;
21574
21575   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
21576   name = cp_parser_identifier (parser);
21577   cp_parser_objc_superclass_or_category (parser, &super, &categ);
21578
21579   /* We have either a class or a category on our hands.  */
21580   if (categ)
21581     objc_start_category_implementation (name, categ);
21582   else
21583     {
21584       objc_start_class_implementation (name, super);
21585       /* Handle instance variable declarations, if any.  */
21586       cp_parser_objc_class_ivars (parser);
21587       objc_continue_implementation ();
21588     }
21589
21590   cp_parser_objc_method_definition_list (parser);
21591 }
21592
21593 /* Consume the @end token and finish off the implementation.  */
21594
21595 static void
21596 cp_parser_objc_end_implementation (cp_parser* parser)
21597 {
21598   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21599   objc_finish_implementation ();
21600 }
21601
21602 /* Parse an Objective-C declaration.  */
21603
21604 static void
21605 cp_parser_objc_declaration (cp_parser* parser)
21606 {
21607   /* Try to figure out what kind of declaration is present.  */
21608   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21609
21610   switch (kwd->keyword)
21611     {
21612     case RID_AT_ALIAS:
21613       cp_parser_objc_alias_declaration (parser);
21614       break;
21615     case RID_AT_CLASS:
21616       cp_parser_objc_class_declaration (parser);
21617       break;
21618     case RID_AT_PROTOCOL:
21619       cp_parser_objc_protocol_declaration (parser);
21620       break;
21621     case RID_AT_INTERFACE:
21622       cp_parser_objc_class_interface (parser);
21623       break;
21624     case RID_AT_IMPLEMENTATION:
21625       cp_parser_objc_class_implementation (parser);
21626       break;
21627     case RID_AT_END:
21628       cp_parser_objc_end_implementation (parser);
21629       break;
21630     default:
21631       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21632                 kwd->u.value);
21633       cp_parser_skip_to_end_of_block_or_statement (parser);
21634     }
21635 }
21636
21637 /* Parse an Objective-C try-catch-finally statement.
21638
21639    objc-try-catch-finally-stmt:
21640      @try compound-statement objc-catch-clause-seq [opt]
21641        objc-finally-clause [opt]
21642
21643    objc-catch-clause-seq:
21644      objc-catch-clause objc-catch-clause-seq [opt]
21645
21646    objc-catch-clause:
21647      @catch ( exception-declaration ) compound-statement
21648
21649    objc-finally-clause
21650      @finally compound-statement
21651
21652    Returns NULL_TREE.  */
21653
21654 static tree
21655 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21656   location_t location;
21657   tree stmt;
21658
21659   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
21660   location = cp_lexer_peek_token (parser->lexer)->location;
21661   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21662      node, lest it get absorbed into the surrounding block.  */
21663   stmt = push_stmt_list ();
21664   cp_parser_compound_statement (parser, NULL, false);
21665   objc_begin_try_stmt (location, pop_stmt_list (stmt));
21666
21667   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21668     {
21669       cp_parameter_declarator *parmdecl;
21670       tree parm;
21671
21672       cp_lexer_consume_token (parser->lexer);
21673       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21674       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21675       parm = grokdeclarator (parmdecl->declarator,
21676                              &parmdecl->decl_specifiers,
21677                              PARM, /*initialized=*/0,
21678                              /*attrlist=*/NULL);
21679       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21680       objc_begin_catch_clause (parm);
21681       cp_parser_compound_statement (parser, NULL, false);
21682       objc_finish_catch_clause ();
21683     }
21684
21685   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21686     {
21687       cp_lexer_consume_token (parser->lexer);
21688       location = cp_lexer_peek_token (parser->lexer)->location;
21689       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21690          node, lest it get absorbed into the surrounding block.  */
21691       stmt = push_stmt_list ();
21692       cp_parser_compound_statement (parser, NULL, false);
21693       objc_build_finally_clause (location, pop_stmt_list (stmt));
21694     }
21695
21696   return objc_finish_try_stmt ();
21697 }
21698
21699 /* Parse an Objective-C synchronized statement.
21700
21701    objc-synchronized-stmt:
21702      @synchronized ( expression ) compound-statement
21703
21704    Returns NULL_TREE.  */
21705
21706 static tree
21707 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21708   location_t location;
21709   tree lock, stmt;
21710
21711   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
21712
21713   location = cp_lexer_peek_token (parser->lexer)->location;
21714   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21715   lock = cp_parser_expression (parser, false, NULL);
21716   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21717
21718   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21719      node, lest it get absorbed into the surrounding block.  */
21720   stmt = push_stmt_list ();
21721   cp_parser_compound_statement (parser, NULL, false);
21722
21723   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21724 }
21725
21726 /* Parse an Objective-C throw statement.
21727
21728    objc-throw-stmt:
21729      @throw assignment-expression [opt] ;
21730
21731    Returns a constructed '@throw' statement.  */
21732
21733 static tree
21734 cp_parser_objc_throw_statement (cp_parser *parser) {
21735   tree expr = NULL_TREE;
21736   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21737
21738   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
21739
21740   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21741     expr = cp_parser_assignment_expression (parser, false, NULL);
21742
21743   cp_parser_consume_semicolon_at_end_of_statement (parser);
21744
21745   return objc_build_throw_stmt (loc, expr);
21746 }
21747
21748 /* Parse an Objective-C statement.  */
21749
21750 static tree
21751 cp_parser_objc_statement (cp_parser * parser) {
21752   /* Try to figure out what kind of declaration is present.  */
21753   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21754
21755   switch (kwd->keyword)
21756     {
21757     case RID_AT_TRY:
21758       return cp_parser_objc_try_catch_finally_statement (parser);
21759     case RID_AT_SYNCHRONIZED:
21760       return cp_parser_objc_synchronized_statement (parser);
21761     case RID_AT_THROW:
21762       return cp_parser_objc_throw_statement (parser);
21763     default:
21764       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21765                kwd->u.value);
21766       cp_parser_skip_to_end_of_block_or_statement (parser);
21767     }
21768
21769   return error_mark_node;
21770 }
21771 \f
21772 /* OpenMP 2.5 parsing routines.  */
21773
21774 /* Returns name of the next clause.
21775    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21776    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21777    returned and the token is consumed.  */
21778
21779 static pragma_omp_clause
21780 cp_parser_omp_clause_name (cp_parser *parser)
21781 {
21782   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21783
21784   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21785     result = PRAGMA_OMP_CLAUSE_IF;
21786   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21787     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21788   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21789     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21790   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21791     {
21792       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21793       const char *p = IDENTIFIER_POINTER (id);
21794
21795       switch (p[0])
21796         {
21797         case 'c':
21798           if (!strcmp ("collapse", p))
21799             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21800           else if (!strcmp ("copyin", p))
21801             result = PRAGMA_OMP_CLAUSE_COPYIN;
21802           else if (!strcmp ("copyprivate", p))
21803             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21804           break;
21805         case 'f':
21806           if (!strcmp ("firstprivate", p))
21807             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21808           break;
21809         case 'l':
21810           if (!strcmp ("lastprivate", p))
21811             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21812           break;
21813         case 'n':
21814           if (!strcmp ("nowait", p))
21815             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21816           else if (!strcmp ("num_threads", p))
21817             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21818           break;
21819         case 'o':
21820           if (!strcmp ("ordered", p))
21821             result = PRAGMA_OMP_CLAUSE_ORDERED;
21822           break;
21823         case 'r':
21824           if (!strcmp ("reduction", p))
21825             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21826           break;
21827         case 's':
21828           if (!strcmp ("schedule", p))
21829             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21830           else if (!strcmp ("shared", p))
21831             result = PRAGMA_OMP_CLAUSE_SHARED;
21832           break;
21833         case 'u':
21834           if (!strcmp ("untied", p))
21835             result = PRAGMA_OMP_CLAUSE_UNTIED;
21836           break;
21837         }
21838     }
21839
21840   if (result != PRAGMA_OMP_CLAUSE_NONE)
21841     cp_lexer_consume_token (parser->lexer);
21842
21843   return result;
21844 }
21845
21846 /* Validate that a clause of the given type does not already exist.  */
21847
21848 static void
21849 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21850                            const char *name, location_t location)
21851 {
21852   tree c;
21853
21854   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21855     if (OMP_CLAUSE_CODE (c) == code)
21856       {
21857         error_at (location, "too many %qs clauses", name);
21858         break;
21859       }
21860 }
21861
21862 /* OpenMP 2.5:
21863    variable-list:
21864      identifier
21865      variable-list , identifier
21866
21867    In addition, we match a closing parenthesis.  An opening parenthesis
21868    will have been consumed by the caller.
21869
21870    If KIND is nonzero, create the appropriate node and install the decl
21871    in OMP_CLAUSE_DECL and add the node to the head of the list.
21872
21873    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21874    return the list created.  */
21875
21876 static tree
21877 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21878                                 tree list)
21879 {
21880   cp_token *token;
21881   while (1)
21882     {
21883       tree name, decl;
21884
21885       token = cp_lexer_peek_token (parser->lexer);
21886       name = cp_parser_id_expression (parser, /*template_p=*/false,
21887                                       /*check_dependency_p=*/true,
21888                                       /*template_p=*/NULL,
21889                                       /*declarator_p=*/false,
21890                                       /*optional_p=*/false);
21891       if (name == error_mark_node)
21892         goto skip_comma;
21893
21894       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21895       if (decl == error_mark_node)
21896         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
21897                                      token->location);
21898       else if (kind != 0)
21899         {
21900           tree u = build_omp_clause (token->location, kind);
21901           OMP_CLAUSE_DECL (u) = decl;
21902           OMP_CLAUSE_CHAIN (u) = list;
21903           list = u;
21904         }
21905       else
21906         list = tree_cons (decl, NULL_TREE, list);
21907
21908     get_comma:
21909       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21910         break;
21911       cp_lexer_consume_token (parser->lexer);
21912     }
21913
21914   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21915     {
21916       int ending;
21917
21918       /* Try to resync to an unnested comma.  Copied from
21919          cp_parser_parenthesized_expression_list.  */
21920     skip_comma:
21921       ending = cp_parser_skip_to_closing_parenthesis (parser,
21922                                                       /*recovering=*/true,
21923                                                       /*or_comma=*/true,
21924                                                       /*consume_paren=*/true);
21925       if (ending < 0)
21926         goto get_comma;
21927     }
21928
21929   return list;
21930 }
21931
21932 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21933    common case for omp clauses.  */
21934
21935 static tree
21936 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21937 {
21938   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21939     return cp_parser_omp_var_list_no_open (parser, kind, list);
21940   return list;
21941 }
21942
21943 /* OpenMP 3.0:
21944    collapse ( constant-expression ) */
21945
21946 static tree
21947 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21948 {
21949   tree c, num;
21950   location_t loc;
21951   HOST_WIDE_INT n;
21952
21953   loc = cp_lexer_peek_token (parser->lexer)->location;
21954   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21955     return list;
21956
21957   num = cp_parser_constant_expression (parser, false, NULL);
21958
21959   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21960     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21961                                            /*or_comma=*/false,
21962                                            /*consume_paren=*/true);
21963
21964   if (num == error_mark_node)
21965     return list;
21966   num = fold_non_dependent_expr (num);
21967   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21968       || !host_integerp (num, 0)
21969       || (n = tree_low_cst (num, 0)) <= 0
21970       || (int) n != n)
21971     {
21972       error_at (loc, "collapse argument needs positive constant integer expression");
21973       return list;
21974     }
21975
21976   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21977   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21978   OMP_CLAUSE_CHAIN (c) = list;
21979   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21980
21981   return c;
21982 }
21983
21984 /* OpenMP 2.5:
21985    default ( shared | none ) */
21986
21987 static tree
21988 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21989 {
21990   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21991   tree c;
21992
21993   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21994     return list;
21995   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21996     {
21997       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21998       const char *p = IDENTIFIER_POINTER (id);
21999
22000       switch (p[0])
22001         {
22002         case 'n':
22003           if (strcmp ("none", p) != 0)
22004             goto invalid_kind;
22005           kind = OMP_CLAUSE_DEFAULT_NONE;
22006           break;
22007
22008         case 's':
22009           if (strcmp ("shared", p) != 0)
22010             goto invalid_kind;
22011           kind = OMP_CLAUSE_DEFAULT_SHARED;
22012           break;
22013
22014         default:
22015           goto invalid_kind;
22016         }
22017
22018       cp_lexer_consume_token (parser->lexer);
22019     }
22020   else
22021     {
22022     invalid_kind:
22023       cp_parser_error (parser, "expected %<none%> or %<shared%>");
22024     }
22025
22026   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22027     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22028                                            /*or_comma=*/false,
22029                                            /*consume_paren=*/true);
22030
22031   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
22032     return list;
22033
22034   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
22035   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
22036   OMP_CLAUSE_CHAIN (c) = list;
22037   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
22038
22039   return c;
22040 }
22041
22042 /* OpenMP 2.5:
22043    if ( expression ) */
22044
22045 static tree
22046 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
22047 {
22048   tree t, c;
22049
22050   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22051     return list;
22052
22053   t = cp_parser_condition (parser);
22054
22055   if (t == error_mark_node
22056       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22057     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22058                                            /*or_comma=*/false,
22059                                            /*consume_paren=*/true);
22060
22061   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
22062
22063   c = build_omp_clause (location, OMP_CLAUSE_IF);
22064   OMP_CLAUSE_IF_EXPR (c) = t;
22065   OMP_CLAUSE_CHAIN (c) = list;
22066
22067   return c;
22068 }
22069
22070 /* OpenMP 2.5:
22071    nowait */
22072
22073 static tree
22074 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
22075                              tree list, location_t location)
22076 {
22077   tree c;
22078
22079   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
22080
22081   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
22082   OMP_CLAUSE_CHAIN (c) = list;
22083   return c;
22084 }
22085
22086 /* OpenMP 2.5:
22087    num_threads ( expression ) */
22088
22089 static tree
22090 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
22091                                   location_t location)
22092 {
22093   tree t, c;
22094
22095   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22096     return list;
22097
22098   t = cp_parser_expression (parser, false, NULL);
22099
22100   if (t == error_mark_node
22101       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22102     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22103                                            /*or_comma=*/false,
22104                                            /*consume_paren=*/true);
22105
22106   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
22107                              "num_threads", location);
22108
22109   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
22110   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
22111   OMP_CLAUSE_CHAIN (c) = list;
22112
22113   return c;
22114 }
22115
22116 /* OpenMP 2.5:
22117    ordered */
22118
22119 static tree
22120 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
22121                               tree list, location_t location)
22122 {
22123   tree c;
22124
22125   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
22126                              "ordered", location);
22127
22128   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
22129   OMP_CLAUSE_CHAIN (c) = list;
22130   return c;
22131 }
22132
22133 /* OpenMP 2.5:
22134    reduction ( reduction-operator : variable-list )
22135
22136    reduction-operator:
22137      One of: + * - & ^ | && || */
22138
22139 static tree
22140 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
22141 {
22142   enum tree_code code;
22143   tree nlist, c;
22144
22145   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22146     return list;
22147
22148   switch (cp_lexer_peek_token (parser->lexer)->type)
22149     {
22150     case CPP_PLUS:
22151       code = PLUS_EXPR;
22152       break;
22153     case CPP_MULT:
22154       code = MULT_EXPR;
22155       break;
22156     case CPP_MINUS:
22157       code = MINUS_EXPR;
22158       break;
22159     case CPP_AND:
22160       code = BIT_AND_EXPR;
22161       break;
22162     case CPP_XOR:
22163       code = BIT_XOR_EXPR;
22164       break;
22165     case CPP_OR:
22166       code = BIT_IOR_EXPR;
22167       break;
22168     case CPP_AND_AND:
22169       code = TRUTH_ANDIF_EXPR;
22170       break;
22171     case CPP_OR_OR:
22172       code = TRUTH_ORIF_EXPR;
22173       break;
22174     default:
22175       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
22176                                "%<|%>, %<&&%>, or %<||%>");
22177     resync_fail:
22178       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22179                                              /*or_comma=*/false,
22180                                              /*consume_paren=*/true);
22181       return list;
22182     }
22183   cp_lexer_consume_token (parser->lexer);
22184
22185   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22186     goto resync_fail;
22187
22188   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
22189   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
22190     OMP_CLAUSE_REDUCTION_CODE (c) = code;
22191
22192   return nlist;
22193 }
22194
22195 /* OpenMP 2.5:
22196    schedule ( schedule-kind )
22197    schedule ( schedule-kind , expression )
22198
22199    schedule-kind:
22200      static | dynamic | guided | runtime | auto  */
22201
22202 static tree
22203 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
22204 {
22205   tree c, t;
22206
22207   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22208     return list;
22209
22210   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
22211
22212   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22213     {
22214       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22215       const char *p = IDENTIFIER_POINTER (id);
22216
22217       switch (p[0])
22218         {
22219         case 'd':
22220           if (strcmp ("dynamic", p) != 0)
22221             goto invalid_kind;
22222           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
22223           break;
22224
22225         case 'g':
22226           if (strcmp ("guided", p) != 0)
22227             goto invalid_kind;
22228           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
22229           break;
22230
22231         case 'r':
22232           if (strcmp ("runtime", p) != 0)
22233             goto invalid_kind;
22234           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
22235           break;
22236
22237         default:
22238           goto invalid_kind;
22239         }
22240     }
22241   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
22242     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
22243   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
22244     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
22245   else
22246     goto invalid_kind;
22247   cp_lexer_consume_token (parser->lexer);
22248
22249   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22250     {
22251       cp_token *token;
22252       cp_lexer_consume_token (parser->lexer);
22253
22254       token = cp_lexer_peek_token (parser->lexer);
22255       t = cp_parser_assignment_expression (parser, false, NULL);
22256
22257       if (t == error_mark_node)
22258         goto resync_fail;
22259       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
22260         error_at (token->location, "schedule %<runtime%> does not take "
22261                   "a %<chunk_size%> parameter");
22262       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
22263         error_at (token->location, "schedule %<auto%> does not take "
22264                   "a %<chunk_size%> parameter");
22265       else
22266         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
22267
22268       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22269         goto resync_fail;
22270     }
22271   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
22272     goto resync_fail;
22273
22274   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
22275   OMP_CLAUSE_CHAIN (c) = list;
22276   return c;
22277
22278  invalid_kind:
22279   cp_parser_error (parser, "invalid schedule kind");
22280  resync_fail:
22281   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22282                                          /*or_comma=*/false,
22283                                          /*consume_paren=*/true);
22284   return list;
22285 }
22286
22287 /* OpenMP 3.0:
22288    untied */
22289
22290 static tree
22291 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
22292                              tree list, location_t location)
22293 {
22294   tree c;
22295
22296   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
22297
22298   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
22299   OMP_CLAUSE_CHAIN (c) = list;
22300   return c;
22301 }
22302
22303 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
22304    is a bitmask in MASK.  Return the list of clauses found; the result
22305    of clause default goes in *pdefault.  */
22306
22307 static tree
22308 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
22309                            const char *where, cp_token *pragma_tok)
22310 {
22311   tree clauses = NULL;
22312   bool first = true;
22313   cp_token *token = NULL;
22314
22315   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
22316     {
22317       pragma_omp_clause c_kind;
22318       const char *c_name;
22319       tree prev = clauses;
22320
22321       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22322         cp_lexer_consume_token (parser->lexer);
22323
22324       token = cp_lexer_peek_token (parser->lexer);
22325       c_kind = cp_parser_omp_clause_name (parser);
22326       first = false;
22327
22328       switch (c_kind)
22329         {
22330         case PRAGMA_OMP_CLAUSE_COLLAPSE:
22331           clauses = cp_parser_omp_clause_collapse (parser, clauses,
22332                                                    token->location);
22333           c_name = "collapse";
22334           break;
22335         case PRAGMA_OMP_CLAUSE_COPYIN:
22336           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
22337           c_name = "copyin";
22338           break;
22339         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
22340           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
22341                                             clauses);
22342           c_name = "copyprivate";
22343           break;
22344         case PRAGMA_OMP_CLAUSE_DEFAULT:
22345           clauses = cp_parser_omp_clause_default (parser, clauses,
22346                                                   token->location);
22347           c_name = "default";
22348           break;
22349         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
22350           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
22351                                             clauses);
22352           c_name = "firstprivate";
22353           break;
22354         case PRAGMA_OMP_CLAUSE_IF:
22355           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
22356           c_name = "if";
22357           break;
22358         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
22359           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
22360                                             clauses);
22361           c_name = "lastprivate";
22362           break;
22363         case PRAGMA_OMP_CLAUSE_NOWAIT:
22364           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
22365           c_name = "nowait";
22366           break;
22367         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
22368           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
22369                                                       token->location);
22370           c_name = "num_threads";
22371           break;
22372         case PRAGMA_OMP_CLAUSE_ORDERED:
22373           clauses = cp_parser_omp_clause_ordered (parser, clauses,
22374                                                   token->location);
22375           c_name = "ordered";
22376           break;
22377         case PRAGMA_OMP_CLAUSE_PRIVATE:
22378           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
22379                                             clauses);
22380           c_name = "private";
22381           break;
22382         case PRAGMA_OMP_CLAUSE_REDUCTION:
22383           clauses = cp_parser_omp_clause_reduction (parser, clauses);
22384           c_name = "reduction";
22385           break;
22386         case PRAGMA_OMP_CLAUSE_SCHEDULE:
22387           clauses = cp_parser_omp_clause_schedule (parser, clauses,
22388                                                    token->location);
22389           c_name = "schedule";
22390           break;
22391         case PRAGMA_OMP_CLAUSE_SHARED:
22392           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
22393                                             clauses);
22394           c_name = "shared";
22395           break;
22396         case PRAGMA_OMP_CLAUSE_UNTIED:
22397           clauses = cp_parser_omp_clause_untied (parser, clauses,
22398                                                  token->location);
22399           c_name = "nowait";
22400           break;
22401         default:
22402           cp_parser_error (parser, "expected %<#pragma omp%> clause");
22403           goto saw_error;
22404         }
22405
22406       if (((mask >> c_kind) & 1) == 0)
22407         {
22408           /* Remove the invalid clause(s) from the list to avoid
22409              confusing the rest of the compiler.  */
22410           clauses = prev;
22411           error_at (token->location, "%qs is not valid for %qs", c_name, where);
22412         }
22413     }
22414  saw_error:
22415   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22416   return finish_omp_clauses (clauses);
22417 }
22418
22419 /* OpenMP 2.5:
22420    structured-block:
22421      statement
22422
22423    In practice, we're also interested in adding the statement to an
22424    outer node.  So it is convenient if we work around the fact that
22425    cp_parser_statement calls add_stmt.  */
22426
22427 static unsigned
22428 cp_parser_begin_omp_structured_block (cp_parser *parser)
22429 {
22430   unsigned save = parser->in_statement;
22431
22432   /* Only move the values to IN_OMP_BLOCK if they weren't false.
22433      This preserves the "not within loop or switch" style error messages
22434      for nonsense cases like
22435         void foo() {
22436         #pragma omp single
22437           break;
22438         }
22439   */
22440   if (parser->in_statement)
22441     parser->in_statement = IN_OMP_BLOCK;
22442
22443   return save;
22444 }
22445
22446 static void
22447 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
22448 {
22449   parser->in_statement = save;
22450 }
22451
22452 static tree
22453 cp_parser_omp_structured_block (cp_parser *parser)
22454 {
22455   tree stmt = begin_omp_structured_block ();
22456   unsigned int save = cp_parser_begin_omp_structured_block (parser);
22457
22458   cp_parser_statement (parser, NULL_TREE, false, NULL);
22459
22460   cp_parser_end_omp_structured_block (parser, save);
22461   return finish_omp_structured_block (stmt);
22462 }
22463
22464 /* OpenMP 2.5:
22465    # pragma omp atomic new-line
22466      expression-stmt
22467
22468    expression-stmt:
22469      x binop= expr | x++ | ++x | x-- | --x
22470    binop:
22471      +, *, -, /, &, ^, |, <<, >>
22472
22473   where x is an lvalue expression with scalar type.  */
22474
22475 static void
22476 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
22477 {
22478   tree lhs, rhs;
22479   enum tree_code code;
22480
22481   cp_parser_require_pragma_eol (parser, pragma_tok);
22482
22483   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
22484                                     /*cast_p=*/false, NULL);
22485   switch (TREE_CODE (lhs))
22486     {
22487     case ERROR_MARK:
22488       goto saw_error;
22489
22490     case PREINCREMENT_EXPR:
22491     case POSTINCREMENT_EXPR:
22492       lhs = TREE_OPERAND (lhs, 0);
22493       code = PLUS_EXPR;
22494       rhs = integer_one_node;
22495       break;
22496
22497     case PREDECREMENT_EXPR:
22498     case POSTDECREMENT_EXPR:
22499       lhs = TREE_OPERAND (lhs, 0);
22500       code = MINUS_EXPR;
22501       rhs = integer_one_node;
22502       break;
22503
22504     default:
22505       switch (cp_lexer_peek_token (parser->lexer)->type)
22506         {
22507         case CPP_MULT_EQ:
22508           code = MULT_EXPR;
22509           break;
22510         case CPP_DIV_EQ:
22511           code = TRUNC_DIV_EXPR;
22512           break;
22513         case CPP_PLUS_EQ:
22514           code = PLUS_EXPR;
22515           break;
22516         case CPP_MINUS_EQ:
22517           code = MINUS_EXPR;
22518           break;
22519         case CPP_LSHIFT_EQ:
22520           code = LSHIFT_EXPR;
22521           break;
22522         case CPP_RSHIFT_EQ:
22523           code = RSHIFT_EXPR;
22524           break;
22525         case CPP_AND_EQ:
22526           code = BIT_AND_EXPR;
22527           break;
22528         case CPP_OR_EQ:
22529           code = BIT_IOR_EXPR;
22530           break;
22531         case CPP_XOR_EQ:
22532           code = BIT_XOR_EXPR;
22533           break;
22534         default:
22535           cp_parser_error (parser,
22536                            "invalid operator for %<#pragma omp atomic%>");
22537           goto saw_error;
22538         }
22539       cp_lexer_consume_token (parser->lexer);
22540
22541       rhs = cp_parser_expression (parser, false, NULL);
22542       if (rhs == error_mark_node)
22543         goto saw_error;
22544       break;
22545     }
22546   finish_omp_atomic (code, lhs, rhs);
22547   cp_parser_consume_semicolon_at_end_of_statement (parser);
22548   return;
22549
22550  saw_error:
22551   cp_parser_skip_to_end_of_block_or_statement (parser);
22552 }
22553
22554
22555 /* OpenMP 2.5:
22556    # pragma omp barrier new-line  */
22557
22558 static void
22559 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
22560 {
22561   cp_parser_require_pragma_eol (parser, pragma_tok);
22562   finish_omp_barrier ();
22563 }
22564
22565 /* OpenMP 2.5:
22566    # pragma omp critical [(name)] new-line
22567      structured-block  */
22568
22569 static tree
22570 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22571 {
22572   tree stmt, name = NULL;
22573
22574   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22575     {
22576       cp_lexer_consume_token (parser->lexer);
22577
22578       name = cp_parser_identifier (parser);
22579
22580       if (name == error_mark_node
22581           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22582         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22583                                                /*or_comma=*/false,
22584                                                /*consume_paren=*/true);
22585       if (name == error_mark_node)
22586         name = NULL;
22587     }
22588   cp_parser_require_pragma_eol (parser, pragma_tok);
22589
22590   stmt = cp_parser_omp_structured_block (parser);
22591   return c_finish_omp_critical (input_location, stmt, name);
22592 }
22593
22594 /* OpenMP 2.5:
22595    # pragma omp flush flush-vars[opt] new-line
22596
22597    flush-vars:
22598      ( variable-list ) */
22599
22600 static void
22601 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22602 {
22603   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22604     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22605   cp_parser_require_pragma_eol (parser, pragma_tok);
22606
22607   finish_omp_flush ();
22608 }
22609
22610 /* Helper function, to parse omp for increment expression.  */
22611
22612 static tree
22613 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22614 {
22615   tree cond = cp_parser_binary_expression (parser, false, true,
22616                                            PREC_NOT_OPERATOR, NULL);
22617   bool overloaded_p;
22618
22619   if (cond == error_mark_node
22620       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22621     {
22622       cp_parser_skip_to_end_of_statement (parser);
22623       return error_mark_node;
22624     }
22625
22626   switch (TREE_CODE (cond))
22627     {
22628     case GT_EXPR:
22629     case GE_EXPR:
22630     case LT_EXPR:
22631     case LE_EXPR:
22632       break;
22633     default:
22634       return error_mark_node;
22635     }
22636
22637   /* If decl is an iterator, preserve LHS and RHS of the relational
22638      expr until finish_omp_for.  */
22639   if (decl
22640       && (type_dependent_expression_p (decl)
22641           || CLASS_TYPE_P (TREE_TYPE (decl))))
22642     return cond;
22643
22644   return build_x_binary_op (TREE_CODE (cond),
22645                             TREE_OPERAND (cond, 0), ERROR_MARK,
22646                             TREE_OPERAND (cond, 1), ERROR_MARK,
22647                             &overloaded_p, tf_warning_or_error);
22648 }
22649
22650 /* Helper function, to parse omp for increment expression.  */
22651
22652 static tree
22653 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22654 {
22655   cp_token *token = cp_lexer_peek_token (parser->lexer);
22656   enum tree_code op;
22657   tree lhs, rhs;
22658   cp_id_kind idk;
22659   bool decl_first;
22660
22661   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22662     {
22663       op = (token->type == CPP_PLUS_PLUS
22664             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22665       cp_lexer_consume_token (parser->lexer);
22666       lhs = cp_parser_cast_expression (parser, false, false, NULL);
22667       if (lhs != decl)
22668         return error_mark_node;
22669       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22670     }
22671
22672   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22673   if (lhs != decl)
22674     return error_mark_node;
22675
22676   token = cp_lexer_peek_token (parser->lexer);
22677   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22678     {
22679       op = (token->type == CPP_PLUS_PLUS
22680             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22681       cp_lexer_consume_token (parser->lexer);
22682       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22683     }
22684
22685   op = cp_parser_assignment_operator_opt (parser);
22686   if (op == ERROR_MARK)
22687     return error_mark_node;
22688
22689   if (op != NOP_EXPR)
22690     {
22691       rhs = cp_parser_assignment_expression (parser, false, NULL);
22692       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22693       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22694     }
22695
22696   lhs = cp_parser_binary_expression (parser, false, false,
22697                                      PREC_ADDITIVE_EXPRESSION, NULL);
22698   token = cp_lexer_peek_token (parser->lexer);
22699   decl_first = lhs == decl;
22700   if (decl_first)
22701     lhs = NULL_TREE;
22702   if (token->type != CPP_PLUS
22703       && token->type != CPP_MINUS)
22704     return error_mark_node;
22705
22706   do
22707     {
22708       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22709       cp_lexer_consume_token (parser->lexer);
22710       rhs = cp_parser_binary_expression (parser, false, false,
22711                                          PREC_ADDITIVE_EXPRESSION, NULL);
22712       token = cp_lexer_peek_token (parser->lexer);
22713       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22714         {
22715           if (lhs == NULL_TREE)
22716             {
22717               if (op == PLUS_EXPR)
22718                 lhs = rhs;
22719               else
22720                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22721             }
22722           else
22723             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22724                                      NULL, tf_warning_or_error);
22725         }
22726     }
22727   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22728
22729   if (!decl_first)
22730     {
22731       if (rhs != decl || op == MINUS_EXPR)
22732         return error_mark_node;
22733       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22734     }
22735   else
22736     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22737
22738   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22739 }
22740
22741 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22742
22743 static tree
22744 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22745 {
22746   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22747   tree real_decl, initv, condv, incrv, declv;
22748   tree this_pre_body, cl;
22749   location_t loc_first;
22750   bool collapse_err = false;
22751   int i, collapse = 1, nbraces = 0;
22752   VEC(tree,gc) *for_block = make_tree_vector ();
22753
22754   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22755     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22756       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22757
22758   gcc_assert (collapse >= 1);
22759
22760   declv = make_tree_vec (collapse);
22761   initv = make_tree_vec (collapse);
22762   condv = make_tree_vec (collapse);
22763   incrv = make_tree_vec (collapse);
22764
22765   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22766
22767   for (i = 0; i < collapse; i++)
22768     {
22769       int bracecount = 0;
22770       bool add_private_clause = false;
22771       location_t loc;
22772
22773       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22774         {
22775           cp_parser_error (parser, "for statement expected");
22776           return NULL;
22777         }
22778       loc = cp_lexer_consume_token (parser->lexer)->location;
22779
22780       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22781         return NULL;
22782
22783       init = decl = real_decl = NULL;
22784       this_pre_body = push_stmt_list ();
22785       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22786         {
22787           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22788
22789              init-expr:
22790                        var = lb
22791                        integer-type var = lb
22792                        random-access-iterator-type var = lb
22793                        pointer-type var = lb
22794           */
22795           cp_decl_specifier_seq type_specifiers;
22796
22797           /* First, try to parse as an initialized declaration.  See
22798              cp_parser_condition, from whence the bulk of this is copied.  */
22799
22800           cp_parser_parse_tentatively (parser);
22801           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22802                                         /*is_trailing_return=*/false,
22803                                         &type_specifiers);
22804           if (cp_parser_parse_definitely (parser))
22805             {
22806               /* If parsing a type specifier seq succeeded, then this
22807                  MUST be a initialized declaration.  */
22808               tree asm_specification, attributes;
22809               cp_declarator *declarator;
22810
22811               declarator = cp_parser_declarator (parser,
22812                                                  CP_PARSER_DECLARATOR_NAMED,
22813                                                  /*ctor_dtor_or_conv_p=*/NULL,
22814                                                  /*parenthesized_p=*/NULL,
22815                                                  /*member_p=*/false);
22816               attributes = cp_parser_attributes_opt (parser);
22817               asm_specification = cp_parser_asm_specification_opt (parser);
22818
22819               if (declarator == cp_error_declarator) 
22820                 cp_parser_skip_to_end_of_statement (parser);
22821
22822               else 
22823                 {
22824                   tree pushed_scope, auto_node;
22825
22826                   decl = start_decl (declarator, &type_specifiers,
22827                                      SD_INITIALIZED, attributes,
22828                                      /*prefix_attributes=*/NULL_TREE,
22829                                      &pushed_scope);
22830
22831                   auto_node = type_uses_auto (TREE_TYPE (decl));
22832                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22833                     {
22834                       if (cp_lexer_next_token_is (parser->lexer, 
22835                                                   CPP_OPEN_PAREN))
22836                         error ("parenthesized initialization is not allowed in "
22837                                "OpenMP %<for%> loop");
22838                       else
22839                         /* Trigger an error.  */
22840                         cp_parser_require (parser, CPP_EQ, RT_EQ);
22841
22842                       init = error_mark_node;
22843                       cp_parser_skip_to_end_of_statement (parser);
22844                     }
22845                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22846                            || type_dependent_expression_p (decl)
22847                            || auto_node)
22848                     {
22849                       bool is_direct_init, is_non_constant_init;
22850
22851                       init = cp_parser_initializer (parser,
22852                                                     &is_direct_init,
22853                                                     &is_non_constant_init);
22854
22855                       if (auto_node && describable_type (init))
22856                         {
22857                           TREE_TYPE (decl)
22858                             = do_auto_deduction (TREE_TYPE (decl), init,
22859                                                  auto_node);
22860
22861                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22862                               && !type_dependent_expression_p (decl))
22863                             goto non_class;
22864                         }
22865                       
22866                       cp_finish_decl (decl, init, !is_non_constant_init,
22867                                       asm_specification,
22868                                       LOOKUP_ONLYCONVERTING);
22869                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22870                         {
22871                           VEC_safe_push (tree, gc, for_block, this_pre_body);
22872                           init = NULL_TREE;
22873                         }
22874                       else
22875                         init = pop_stmt_list (this_pre_body);
22876                       this_pre_body = NULL_TREE;
22877                     }
22878                   else
22879                     {
22880                       /* Consume '='.  */
22881                       cp_lexer_consume_token (parser->lexer);
22882                       init = cp_parser_assignment_expression (parser, false, NULL);
22883
22884                     non_class:
22885                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22886                         init = error_mark_node;
22887                       else
22888                         cp_finish_decl (decl, NULL_TREE,
22889                                         /*init_const_expr_p=*/false,
22890                                         asm_specification,
22891                                         LOOKUP_ONLYCONVERTING);
22892                     }
22893
22894                   if (pushed_scope)
22895                     pop_scope (pushed_scope);
22896                 }
22897             }
22898           else 
22899             {
22900               cp_id_kind idk;
22901               /* If parsing a type specifier sequence failed, then
22902                  this MUST be a simple expression.  */
22903               cp_parser_parse_tentatively (parser);
22904               decl = cp_parser_primary_expression (parser, false, false,
22905                                                    false, &idk);
22906               if (!cp_parser_error_occurred (parser)
22907                   && decl
22908                   && DECL_P (decl)
22909                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22910                 {
22911                   tree rhs;
22912
22913                   cp_parser_parse_definitely (parser);
22914                   cp_parser_require (parser, CPP_EQ, RT_EQ);
22915                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22916                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22917                                                          rhs,
22918                                                          tf_warning_or_error));
22919                   add_private_clause = true;
22920                 }
22921               else
22922                 {
22923                   decl = NULL;
22924                   cp_parser_abort_tentative_parse (parser);
22925                   init = cp_parser_expression (parser, false, NULL);
22926                   if (init)
22927                     {
22928                       if (TREE_CODE (init) == MODIFY_EXPR
22929                           || TREE_CODE (init) == MODOP_EXPR)
22930                         real_decl = TREE_OPERAND (init, 0);
22931                     }
22932                 }
22933             }
22934         }
22935       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22936       if (this_pre_body)
22937         {
22938           this_pre_body = pop_stmt_list (this_pre_body);
22939           if (pre_body)
22940             {
22941               tree t = pre_body;
22942               pre_body = push_stmt_list ();
22943               add_stmt (t);
22944               add_stmt (this_pre_body);
22945               pre_body = pop_stmt_list (pre_body);
22946             }
22947           else
22948             pre_body = this_pre_body;
22949         }
22950
22951       if (decl)
22952         real_decl = decl;
22953       if (par_clauses != NULL && real_decl != NULL_TREE)
22954         {
22955           tree *c;
22956           for (c = par_clauses; *c ; )
22957             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22958                 && OMP_CLAUSE_DECL (*c) == real_decl)
22959               {
22960                 error_at (loc, "iteration variable %qD"
22961                           " should not be firstprivate", real_decl);
22962                 *c = OMP_CLAUSE_CHAIN (*c);
22963               }
22964             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22965                      && OMP_CLAUSE_DECL (*c) == real_decl)
22966               {
22967                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22968                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22969                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22970                 OMP_CLAUSE_DECL (l) = real_decl;
22971                 OMP_CLAUSE_CHAIN (l) = clauses;
22972                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22973                 clauses = l;
22974                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22975                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22976                 add_private_clause = false;
22977               }
22978             else
22979               {
22980                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22981                     && OMP_CLAUSE_DECL (*c) == real_decl)
22982                   add_private_clause = false;
22983                 c = &OMP_CLAUSE_CHAIN (*c);
22984               }
22985         }
22986
22987       if (add_private_clause)
22988         {
22989           tree c;
22990           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22991             {
22992               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22993                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22994                   && OMP_CLAUSE_DECL (c) == decl)
22995                 break;
22996               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22997                        && OMP_CLAUSE_DECL (c) == decl)
22998                 error_at (loc, "iteration variable %qD "
22999                           "should not be firstprivate",
23000                           decl);
23001               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
23002                        && OMP_CLAUSE_DECL (c) == decl)
23003                 error_at (loc, "iteration variable %qD should not be reduction",
23004                           decl);
23005             }
23006           if (c == NULL)
23007             {
23008               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
23009               OMP_CLAUSE_DECL (c) = decl;
23010               c = finish_omp_clauses (c);
23011               if (c)
23012                 {
23013                   OMP_CLAUSE_CHAIN (c) = clauses;
23014                   clauses = c;
23015                 }
23016             }
23017         }
23018
23019       cond = NULL;
23020       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23021         cond = cp_parser_omp_for_cond (parser, decl);
23022       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23023
23024       incr = NULL;
23025       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23026         {
23027           /* If decl is an iterator, preserve the operator on decl
23028              until finish_omp_for.  */
23029           if (decl
23030               && (type_dependent_expression_p (decl)
23031                   || CLASS_TYPE_P (TREE_TYPE (decl))))
23032             incr = cp_parser_omp_for_incr (parser, decl);
23033           else
23034             incr = cp_parser_expression (parser, false, NULL);
23035         }
23036
23037       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23038         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23039                                                /*or_comma=*/false,
23040                                                /*consume_paren=*/true);
23041
23042       TREE_VEC_ELT (declv, i) = decl;
23043       TREE_VEC_ELT (initv, i) = init;
23044       TREE_VEC_ELT (condv, i) = cond;
23045       TREE_VEC_ELT (incrv, i) = incr;
23046
23047       if (i == collapse - 1)
23048         break;
23049
23050       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
23051          in between the collapsed for loops to be still considered perfectly
23052          nested.  Hopefully the final version clarifies this.
23053          For now handle (multiple) {'s and empty statements.  */
23054       cp_parser_parse_tentatively (parser);
23055       do
23056         {
23057           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23058             break;
23059           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23060             {
23061               cp_lexer_consume_token (parser->lexer);
23062               bracecount++;
23063             }
23064           else if (bracecount
23065                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23066             cp_lexer_consume_token (parser->lexer);
23067           else
23068             {
23069               loc = cp_lexer_peek_token (parser->lexer)->location;
23070               error_at (loc, "not enough collapsed for loops");
23071               collapse_err = true;
23072               cp_parser_abort_tentative_parse (parser);
23073               declv = NULL_TREE;
23074               break;
23075             }
23076         }
23077       while (1);
23078
23079       if (declv)
23080         {
23081           cp_parser_parse_definitely (parser);
23082           nbraces += bracecount;
23083         }
23084     }
23085
23086   /* Note that we saved the original contents of this flag when we entered
23087      the structured block, and so we don't need to re-save it here.  */
23088   parser->in_statement = IN_OMP_FOR;
23089
23090   /* Note that the grammar doesn't call for a structured block here,
23091      though the loop as a whole is a structured block.  */
23092   body = push_stmt_list ();
23093   cp_parser_statement (parser, NULL_TREE, false, NULL);
23094   body = pop_stmt_list (body);
23095
23096   if (declv == NULL_TREE)
23097     ret = NULL_TREE;
23098   else
23099     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
23100                           pre_body, clauses);
23101
23102   while (nbraces)
23103     {
23104       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23105         {
23106           cp_lexer_consume_token (parser->lexer);
23107           nbraces--;
23108         }
23109       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23110         cp_lexer_consume_token (parser->lexer);
23111       else
23112         {
23113           if (!collapse_err)
23114             {
23115               error_at (cp_lexer_peek_token (parser->lexer)->location,
23116                         "collapsed loops not perfectly nested");
23117             }
23118           collapse_err = true;
23119           cp_parser_statement_seq_opt (parser, NULL);
23120           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23121             break;
23122         }
23123     }
23124
23125   while (!VEC_empty (tree, for_block))
23126     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
23127   release_tree_vector (for_block);
23128
23129   return ret;
23130 }
23131
23132 /* OpenMP 2.5:
23133    #pragma omp for for-clause[optseq] new-line
23134      for-loop  */
23135
23136 #define OMP_FOR_CLAUSE_MASK                             \
23137         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23138         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23139         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
23140         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
23141         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
23142         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
23143         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
23144         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
23145
23146 static tree
23147 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
23148 {
23149   tree clauses, sb, ret;
23150   unsigned int save;
23151
23152   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
23153                                        "#pragma omp for", pragma_tok);
23154
23155   sb = begin_omp_structured_block ();
23156   save = cp_parser_begin_omp_structured_block (parser);
23157
23158   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
23159
23160   cp_parser_end_omp_structured_block (parser, save);
23161   add_stmt (finish_omp_structured_block (sb));
23162
23163   return ret;
23164 }
23165
23166 /* OpenMP 2.5:
23167    # pragma omp master new-line
23168      structured-block  */
23169
23170 static tree
23171 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
23172 {
23173   cp_parser_require_pragma_eol (parser, pragma_tok);
23174   return c_finish_omp_master (input_location,
23175                               cp_parser_omp_structured_block (parser));
23176 }
23177
23178 /* OpenMP 2.5:
23179    # pragma omp ordered new-line
23180      structured-block  */
23181
23182 static tree
23183 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
23184 {
23185   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23186   cp_parser_require_pragma_eol (parser, pragma_tok);
23187   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
23188 }
23189
23190 /* OpenMP 2.5:
23191
23192    section-scope:
23193      { section-sequence }
23194
23195    section-sequence:
23196      section-directive[opt] structured-block
23197      section-sequence section-directive structured-block  */
23198
23199 static tree
23200 cp_parser_omp_sections_scope (cp_parser *parser)
23201 {
23202   tree stmt, substmt;
23203   bool error_suppress = false;
23204   cp_token *tok;
23205
23206   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23207     return NULL_TREE;
23208
23209   stmt = push_stmt_list ();
23210
23211   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
23212     {
23213       unsigned save;
23214
23215       substmt = begin_omp_structured_block ();
23216       save = cp_parser_begin_omp_structured_block (parser);
23217
23218       while (1)
23219         {
23220           cp_parser_statement (parser, NULL_TREE, false, NULL);
23221
23222           tok = cp_lexer_peek_token (parser->lexer);
23223           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23224             break;
23225           if (tok->type == CPP_CLOSE_BRACE)
23226             break;
23227           if (tok->type == CPP_EOF)
23228             break;
23229         }
23230
23231       cp_parser_end_omp_structured_block (parser, save);
23232       substmt = finish_omp_structured_block (substmt);
23233       substmt = build1 (OMP_SECTION, void_type_node, substmt);
23234       add_stmt (substmt);
23235     }
23236
23237   while (1)
23238     {
23239       tok = cp_lexer_peek_token (parser->lexer);
23240       if (tok->type == CPP_CLOSE_BRACE)
23241         break;
23242       if (tok->type == CPP_EOF)
23243         break;
23244
23245       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23246         {
23247           cp_lexer_consume_token (parser->lexer);
23248           cp_parser_require_pragma_eol (parser, tok);
23249           error_suppress = false;
23250         }
23251       else if (!error_suppress)
23252         {
23253           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
23254           error_suppress = true;
23255         }
23256
23257       substmt = cp_parser_omp_structured_block (parser);
23258       substmt = build1 (OMP_SECTION, void_type_node, substmt);
23259       add_stmt (substmt);
23260     }
23261   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23262
23263   substmt = pop_stmt_list (stmt);
23264
23265   stmt = make_node (OMP_SECTIONS);
23266   TREE_TYPE (stmt) = void_type_node;
23267   OMP_SECTIONS_BODY (stmt) = substmt;
23268
23269   add_stmt (stmt);
23270   return stmt;
23271 }
23272
23273 /* OpenMP 2.5:
23274    # pragma omp sections sections-clause[optseq] newline
23275      sections-scope  */
23276
23277 #define OMP_SECTIONS_CLAUSE_MASK                        \
23278         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23279         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23280         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
23281         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
23282         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23283
23284 static tree
23285 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
23286 {
23287   tree clauses, ret;
23288
23289   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
23290                                        "#pragma omp sections", pragma_tok);
23291
23292   ret = cp_parser_omp_sections_scope (parser);
23293   if (ret)
23294     OMP_SECTIONS_CLAUSES (ret) = clauses;
23295
23296   return ret;
23297 }
23298
23299 /* OpenMP 2.5:
23300    # pragma parallel parallel-clause new-line
23301    # pragma parallel for parallel-for-clause new-line
23302    # pragma parallel sections parallel-sections-clause new-line  */
23303
23304 #define OMP_PARALLEL_CLAUSE_MASK                        \
23305         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
23306         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23307         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23308         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
23309         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
23310         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
23311         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
23312         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
23313
23314 static tree
23315 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
23316 {
23317   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
23318   const char *p_name = "#pragma omp parallel";
23319   tree stmt, clauses, par_clause, ws_clause, block;
23320   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
23321   unsigned int save;
23322   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23323
23324   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23325     {
23326       cp_lexer_consume_token (parser->lexer);
23327       p_kind = PRAGMA_OMP_PARALLEL_FOR;
23328       p_name = "#pragma omp parallel for";
23329       mask |= OMP_FOR_CLAUSE_MASK;
23330       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23331     }
23332   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23333     {
23334       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23335       const char *p = IDENTIFIER_POINTER (id);
23336       if (strcmp (p, "sections") == 0)
23337         {
23338           cp_lexer_consume_token (parser->lexer);
23339           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
23340           p_name = "#pragma omp parallel sections";
23341           mask |= OMP_SECTIONS_CLAUSE_MASK;
23342           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23343         }
23344     }
23345
23346   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
23347   block = begin_omp_parallel ();
23348   save = cp_parser_begin_omp_structured_block (parser);
23349
23350   switch (p_kind)
23351     {
23352     case PRAGMA_OMP_PARALLEL:
23353       cp_parser_statement (parser, NULL_TREE, false, NULL);
23354       par_clause = clauses;
23355       break;
23356
23357     case PRAGMA_OMP_PARALLEL_FOR:
23358       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23359       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
23360       break;
23361
23362     case PRAGMA_OMP_PARALLEL_SECTIONS:
23363       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23364       stmt = cp_parser_omp_sections_scope (parser);
23365       if (stmt)
23366         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
23367       break;
23368
23369     default:
23370       gcc_unreachable ();
23371     }
23372
23373   cp_parser_end_omp_structured_block (parser, save);
23374   stmt = finish_omp_parallel (par_clause, block);
23375   if (p_kind != PRAGMA_OMP_PARALLEL)
23376     OMP_PARALLEL_COMBINED (stmt) = 1;
23377   return stmt;
23378 }
23379
23380 /* OpenMP 2.5:
23381    # pragma omp single single-clause[optseq] new-line
23382      structured-block  */
23383
23384 #define OMP_SINGLE_CLAUSE_MASK                          \
23385         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23386         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23387         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
23388         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23389
23390 static tree
23391 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
23392 {
23393   tree stmt = make_node (OMP_SINGLE);
23394   TREE_TYPE (stmt) = void_type_node;
23395
23396   OMP_SINGLE_CLAUSES (stmt)
23397     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
23398                                  "#pragma omp single", pragma_tok);
23399   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
23400
23401   return add_stmt (stmt);
23402 }
23403
23404 /* OpenMP 3.0:
23405    # pragma omp task task-clause[optseq] new-line
23406      structured-block  */
23407
23408 #define OMP_TASK_CLAUSE_MASK                            \
23409         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
23410         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
23411         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
23412         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23413         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23414         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
23415
23416 static tree
23417 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
23418 {
23419   tree clauses, block;
23420   unsigned int save;
23421
23422   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
23423                                        "#pragma omp task", pragma_tok);
23424   block = begin_omp_task ();
23425   save = cp_parser_begin_omp_structured_block (parser);
23426   cp_parser_statement (parser, NULL_TREE, false, NULL);
23427   cp_parser_end_omp_structured_block (parser, save);
23428   return finish_omp_task (clauses, block);
23429 }
23430
23431 /* OpenMP 3.0:
23432    # pragma omp taskwait new-line  */
23433
23434 static void
23435 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
23436 {
23437   cp_parser_require_pragma_eol (parser, pragma_tok);
23438   finish_omp_taskwait ();
23439 }
23440
23441 /* OpenMP 2.5:
23442    # pragma omp threadprivate (variable-list) */
23443
23444 static void
23445 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
23446 {
23447   tree vars;
23448
23449   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23450   cp_parser_require_pragma_eol (parser, pragma_tok);
23451
23452   finish_omp_threadprivate (vars);
23453 }
23454
23455 /* Main entry point to OpenMP statement pragmas.  */
23456
23457 static void
23458 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
23459 {
23460   tree stmt;
23461
23462   switch (pragma_tok->pragma_kind)
23463     {
23464     case PRAGMA_OMP_ATOMIC:
23465       cp_parser_omp_atomic (parser, pragma_tok);
23466       return;
23467     case PRAGMA_OMP_CRITICAL:
23468       stmt = cp_parser_omp_critical (parser, pragma_tok);
23469       break;
23470     case PRAGMA_OMP_FOR:
23471       stmt = cp_parser_omp_for (parser, pragma_tok);
23472       break;
23473     case PRAGMA_OMP_MASTER:
23474       stmt = cp_parser_omp_master (parser, pragma_tok);
23475       break;
23476     case PRAGMA_OMP_ORDERED:
23477       stmt = cp_parser_omp_ordered (parser, pragma_tok);
23478       break;
23479     case PRAGMA_OMP_PARALLEL:
23480       stmt = cp_parser_omp_parallel (parser, pragma_tok);
23481       break;
23482     case PRAGMA_OMP_SECTIONS:
23483       stmt = cp_parser_omp_sections (parser, pragma_tok);
23484       break;
23485     case PRAGMA_OMP_SINGLE:
23486       stmt = cp_parser_omp_single (parser, pragma_tok);
23487       break;
23488     case PRAGMA_OMP_TASK:
23489       stmt = cp_parser_omp_task (parser, pragma_tok);
23490       break;
23491     default:
23492       gcc_unreachable ();
23493     }
23494
23495   if (stmt)
23496     SET_EXPR_LOCATION (stmt, pragma_tok->location);
23497 }
23498 \f
23499 /* The parser.  */
23500
23501 static GTY (()) cp_parser *the_parser;
23502
23503 \f
23504 /* Special handling for the first token or line in the file.  The first
23505    thing in the file might be #pragma GCC pch_preprocess, which loads a
23506    PCH file, which is a GC collection point.  So we need to handle this
23507    first pragma without benefit of an existing lexer structure.
23508
23509    Always returns one token to the caller in *FIRST_TOKEN.  This is
23510    either the true first token of the file, or the first token after
23511    the initial pragma.  */
23512
23513 static void
23514 cp_parser_initial_pragma (cp_token *first_token)
23515 {
23516   tree name = NULL;
23517
23518   cp_lexer_get_preprocessor_token (NULL, first_token);
23519   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
23520     return;
23521
23522   cp_lexer_get_preprocessor_token (NULL, first_token);
23523   if (first_token->type == CPP_STRING)
23524     {
23525       name = first_token->u.value;
23526
23527       cp_lexer_get_preprocessor_token (NULL, first_token);
23528       if (first_token->type != CPP_PRAGMA_EOL)
23529         error_at (first_token->location,
23530                   "junk at end of %<#pragma GCC pch_preprocess%>");
23531     }
23532   else
23533     error_at (first_token->location, "expected string literal");
23534
23535   /* Skip to the end of the pragma.  */
23536   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
23537     cp_lexer_get_preprocessor_token (NULL, first_token);
23538
23539   /* Now actually load the PCH file.  */
23540   if (name)
23541     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
23542
23543   /* Read one more token to return to our caller.  We have to do this
23544      after reading the PCH file in, since its pointers have to be
23545      live.  */
23546   cp_lexer_get_preprocessor_token (NULL, first_token);
23547 }
23548
23549 /* Normal parsing of a pragma token.  Here we can (and must) use the
23550    regular lexer.  */
23551
23552 static bool
23553 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
23554 {
23555   cp_token *pragma_tok;
23556   unsigned int id;
23557
23558   pragma_tok = cp_lexer_consume_token (parser->lexer);
23559   gcc_assert (pragma_tok->type == CPP_PRAGMA);
23560   parser->lexer->in_pragma = true;
23561
23562   id = pragma_tok->pragma_kind;
23563   switch (id)
23564     {
23565     case PRAGMA_GCC_PCH_PREPROCESS:
23566       error_at (pragma_tok->location,
23567                 "%<#pragma GCC pch_preprocess%> must be first");
23568       break;
23569
23570     case PRAGMA_OMP_BARRIER:
23571       switch (context)
23572         {
23573         case pragma_compound:
23574           cp_parser_omp_barrier (parser, pragma_tok);
23575           return false;
23576         case pragma_stmt:
23577           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23578                     "used in compound statements");
23579           break;
23580         default:
23581           goto bad_stmt;
23582         }
23583       break;
23584
23585     case PRAGMA_OMP_FLUSH:
23586       switch (context)
23587         {
23588         case pragma_compound:
23589           cp_parser_omp_flush (parser, pragma_tok);
23590           return false;
23591         case pragma_stmt:
23592           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23593                     "used in compound statements");
23594           break;
23595         default:
23596           goto bad_stmt;
23597         }
23598       break;
23599
23600     case PRAGMA_OMP_TASKWAIT:
23601       switch (context)
23602         {
23603         case pragma_compound:
23604           cp_parser_omp_taskwait (parser, pragma_tok);
23605           return false;
23606         case pragma_stmt:
23607           error_at (pragma_tok->location,
23608                     "%<#pragma omp taskwait%> may only be "
23609                     "used in compound statements");
23610           break;
23611         default:
23612           goto bad_stmt;
23613         }
23614       break;
23615
23616     case PRAGMA_OMP_THREADPRIVATE:
23617       cp_parser_omp_threadprivate (parser, pragma_tok);
23618       return false;
23619
23620     case PRAGMA_OMP_ATOMIC:
23621     case PRAGMA_OMP_CRITICAL:
23622     case PRAGMA_OMP_FOR:
23623     case PRAGMA_OMP_MASTER:
23624     case PRAGMA_OMP_ORDERED:
23625     case PRAGMA_OMP_PARALLEL:
23626     case PRAGMA_OMP_SECTIONS:
23627     case PRAGMA_OMP_SINGLE:
23628     case PRAGMA_OMP_TASK:
23629       if (context == pragma_external)
23630         goto bad_stmt;
23631       cp_parser_omp_construct (parser, pragma_tok);
23632       return true;
23633
23634     case PRAGMA_OMP_SECTION:
23635       error_at (pragma_tok->location, 
23636                 "%<#pragma omp section%> may only be used in "
23637                 "%<#pragma omp sections%> construct");
23638       break;
23639
23640     default:
23641       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23642       c_invoke_pragma_handler (id);
23643       break;
23644
23645     bad_stmt:
23646       cp_parser_error (parser, "expected declaration specifiers");
23647       break;
23648     }
23649
23650   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23651   return false;
23652 }
23653
23654 /* The interface the pragma parsers have to the lexer.  */
23655
23656 enum cpp_ttype
23657 pragma_lex (tree *value)
23658 {
23659   cp_token *tok;
23660   enum cpp_ttype ret;
23661
23662   tok = cp_lexer_peek_token (the_parser->lexer);
23663
23664   ret = tok->type;
23665   *value = tok->u.value;
23666
23667   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23668     ret = CPP_EOF;
23669   else if (ret == CPP_STRING)
23670     *value = cp_parser_string_literal (the_parser, false, false);
23671   else
23672     {
23673       cp_lexer_consume_token (the_parser->lexer);
23674       if (ret == CPP_KEYWORD)
23675         ret = CPP_NAME;
23676     }
23677
23678   return ret;
23679 }
23680
23681 \f
23682 /* External interface.  */
23683
23684 /* Parse one entire translation unit.  */
23685
23686 void
23687 c_parse_file (void)
23688 {
23689   static bool already_called = false;
23690
23691   if (already_called)
23692     {
23693       sorry ("inter-module optimizations not implemented for C++");
23694       return;
23695     }
23696   already_called = true;
23697
23698   the_parser = cp_parser_new ();
23699   push_deferring_access_checks (flag_access_control
23700                                 ? dk_no_deferred : dk_no_check);
23701   cp_parser_translation_unit (the_parser);
23702   the_parser = NULL;
23703 }
23704
23705 #include "gt-cp-parser.h"