OSDN Git Service

In gcc/c-family/:
[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   else if (token->type == CPP_AT_NAME)
569     {
570       /* This only happens in Objective-C++; it must be a keyword.  */
571       token->type = CPP_KEYWORD;
572       switch (C_RID_CODE (token->u.value))
573         {
574           /* Replace 'class' with '@class', 'private' with '@private',
575              etc.  This prevents confusion with the C++ keyword
576              'class', and makes the tokens consistent with other
577              Objective-C 'AT' keywords.  For example '@class' is
578              reported as RID_AT_CLASS which is consistent with
579              '@synchronized', which is reported as
580              RID_AT_SYNCHRONIZED.
581           */
582         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
583         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
584         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
585         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
586         case RID_THROW:     token->keyword = RID_AT_THROW; break;
587         case RID_TRY:       token->keyword = RID_AT_TRY; break;
588         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
589         default:            token->keyword = C_RID_CODE (token->u.value);
590         }
591     }
592   else if (token->type == CPP_PRAGMA)
593     {
594       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
595       token->pragma_kind = ((enum pragma_kind)
596                             TREE_INT_CST_LOW (token->u.value));
597       token->u.value = NULL_TREE;
598     }
599 }
600
601 /* Update the globals input_location and the input file stack from TOKEN.  */
602 static inline void
603 cp_lexer_set_source_position_from_token (cp_token *token)
604 {
605   if (token->type != CPP_EOF)
606     {
607       input_location = token->location;
608     }
609 }
610
611 /* Return a pointer to the next token in the token stream, but do not
612    consume it.  */
613
614 static inline cp_token *
615 cp_lexer_peek_token (cp_lexer *lexer)
616 {
617   if (cp_lexer_debugging_p (lexer))
618     {
619       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
620       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
621       putc ('\n', cp_lexer_debug_stream);
622     }
623   return lexer->next_token;
624 }
625
626 /* Return true if the next token has the indicated TYPE.  */
627
628 static inline bool
629 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
630 {
631   return cp_lexer_peek_token (lexer)->type == type;
632 }
633
634 /* Return true if the next token does not have the indicated TYPE.  */
635
636 static inline bool
637 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
638 {
639   return !cp_lexer_next_token_is (lexer, type);
640 }
641
642 /* Return true if the next token is the indicated KEYWORD.  */
643
644 static inline bool
645 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
646 {
647   return cp_lexer_peek_token (lexer)->keyword == keyword;
648 }
649
650 /* Return true if the next token is not the indicated KEYWORD.  */
651
652 static inline bool
653 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
654 {
655   return cp_lexer_peek_token (lexer)->keyword != keyword;
656 }
657
658 /* Return true if the next token is a keyword for a decl-specifier.  */
659
660 static bool
661 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
662 {
663   cp_token *token;
664
665   token = cp_lexer_peek_token (lexer);
666   switch (token->keyword) 
667     {
668       /* auto specifier: storage-class-specifier in C++,
669          simple-type-specifier in C++0x.  */
670     case RID_AUTO:
671       /* Storage classes.  */
672     case RID_REGISTER:
673     case RID_STATIC:
674     case RID_EXTERN:
675     case RID_MUTABLE:
676     case RID_THREAD:
677       /* Elaborated type specifiers.  */
678     case RID_ENUM:
679     case RID_CLASS:
680     case RID_STRUCT:
681     case RID_UNION:
682     case RID_TYPENAME:
683       /* Simple type specifiers.  */
684     case RID_CHAR:
685     case RID_CHAR16:
686     case RID_CHAR32:
687     case RID_WCHAR:
688     case RID_BOOL:
689     case RID_SHORT:
690     case RID_INT:
691     case RID_LONG:
692     case RID_INT128:
693     case RID_SIGNED:
694     case RID_UNSIGNED:
695     case RID_FLOAT:
696     case RID_DOUBLE:
697     case RID_VOID:
698       /* GNU extensions.  */ 
699     case RID_ATTRIBUTE:
700     case RID_TYPEOF:
701       /* C++0x extensions.  */
702     case RID_DECLTYPE:
703       return true;
704
705     default:
706       return false;
707     }
708 }
709
710 /* Return a pointer to the Nth token in the token stream.  If N is 1,
711    then this is precisely equivalent to cp_lexer_peek_token (except
712    that it is not inline).  One would like to disallow that case, but
713    there is one case (cp_parser_nth_token_starts_template_id) where
714    the caller passes a variable for N and it might be 1.  */
715
716 static cp_token *
717 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
718 {
719   cp_token *token;
720
721   /* N is 1-based, not zero-based.  */
722   gcc_assert (n > 0);
723
724   if (cp_lexer_debugging_p (lexer))
725     fprintf (cp_lexer_debug_stream,
726              "cp_lexer: peeking ahead %ld at token: ", (long)n);
727
728   --n;
729   token = lexer->next_token;
730   gcc_assert (!n || token != &eof_token);
731   while (n != 0)
732     {
733       ++token;
734       if (token == lexer->last_token)
735         {
736           token = &eof_token;
737           break;
738         }
739
740       if (token->type != CPP_PURGED)
741         --n;
742     }
743
744   if (cp_lexer_debugging_p (lexer))
745     {
746       cp_lexer_print_token (cp_lexer_debug_stream, token);
747       putc ('\n', cp_lexer_debug_stream);
748     }
749
750   return token;
751 }
752
753 /* Return the next token, and advance the lexer's next_token pointer
754    to point to the next non-purged token.  */
755
756 static cp_token *
757 cp_lexer_consume_token (cp_lexer* lexer)
758 {
759   cp_token *token = lexer->next_token;
760
761   gcc_assert (token != &eof_token);
762   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
763
764   do
765     {
766       lexer->next_token++;
767       if (lexer->next_token == lexer->last_token)
768         {
769           lexer->next_token = &eof_token;
770           break;
771         }
772
773     }
774   while (lexer->next_token->type == CPP_PURGED);
775
776   cp_lexer_set_source_position_from_token (token);
777
778   /* Provide debugging output.  */
779   if (cp_lexer_debugging_p (lexer))
780     {
781       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
782       cp_lexer_print_token (cp_lexer_debug_stream, token);
783       putc ('\n', cp_lexer_debug_stream);
784     }
785
786   return token;
787 }
788
789 /* Permanently remove the next token from the token stream, and
790    advance the next_token pointer to refer to the next non-purged
791    token.  */
792
793 static void
794 cp_lexer_purge_token (cp_lexer *lexer)
795 {
796   cp_token *tok = lexer->next_token;
797
798   gcc_assert (tok != &eof_token);
799   tok->type = CPP_PURGED;
800   tok->location = UNKNOWN_LOCATION;
801   tok->u.value = NULL_TREE;
802   tok->keyword = RID_MAX;
803
804   do
805     {
806       tok++;
807       if (tok == lexer->last_token)
808         {
809           tok = &eof_token;
810           break;
811         }
812     }
813   while (tok->type == CPP_PURGED);
814   lexer->next_token = tok;
815 }
816
817 /* Permanently remove all tokens after TOK, up to, but not
818    including, the token that will be returned next by
819    cp_lexer_peek_token.  */
820
821 static void
822 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
823 {
824   cp_token *peek = lexer->next_token;
825
826   if (peek == &eof_token)
827     peek = lexer->last_token;
828
829   gcc_assert (tok < peek);
830
831   for ( tok += 1; tok != peek; tok += 1)
832     {
833       tok->type = CPP_PURGED;
834       tok->location = UNKNOWN_LOCATION;
835       tok->u.value = NULL_TREE;
836       tok->keyword = RID_MAX;
837     }
838 }
839
840 /* Begin saving tokens.  All tokens consumed after this point will be
841    preserved.  */
842
843 static void
844 cp_lexer_save_tokens (cp_lexer* lexer)
845 {
846   /* Provide debugging output.  */
847   if (cp_lexer_debugging_p (lexer))
848     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
849
850   VEC_safe_push (cp_token_position, heap,
851                  lexer->saved_tokens, lexer->next_token);
852 }
853
854 /* Commit to the portion of the token stream most recently saved.  */
855
856 static void
857 cp_lexer_commit_tokens (cp_lexer* lexer)
858 {
859   /* Provide debugging output.  */
860   if (cp_lexer_debugging_p (lexer))
861     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
862
863   VEC_pop (cp_token_position, lexer->saved_tokens);
864 }
865
866 /* Return all tokens saved since the last call to cp_lexer_save_tokens
867    to the token stream.  Stop saving tokens.  */
868
869 static void
870 cp_lexer_rollback_tokens (cp_lexer* lexer)
871 {
872   /* Provide debugging output.  */
873   if (cp_lexer_debugging_p (lexer))
874     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
875
876   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
877 }
878
879 /* Print a representation of the TOKEN on the STREAM.  */
880
881 #ifdef ENABLE_CHECKING
882
883 static void
884 cp_lexer_print_token (FILE * stream, cp_token *token)
885 {
886   /* We don't use cpp_type2name here because the parser defines
887      a few tokens of its own.  */
888   static const char *const token_names[] = {
889     /* cpplib-defined token types */
890 #define OP(e, s) #e,
891 #define TK(e, s) #e,
892     TTYPE_TABLE
893 #undef OP
894 #undef TK
895     /* C++ parser token types - see "Manifest constants", above.  */
896     "KEYWORD",
897     "TEMPLATE_ID",
898     "NESTED_NAME_SPECIFIER",
899     "PURGED"
900   };
901
902   /* If we have a name for the token, print it out.  Otherwise, we
903      simply give the numeric code.  */
904   gcc_assert (token->type < ARRAY_SIZE(token_names));
905   fputs (token_names[token->type], stream);
906
907   /* For some tokens, print the associated data.  */
908   switch (token->type)
909     {
910     case CPP_KEYWORD:
911       /* Some keywords have a value that is not an IDENTIFIER_NODE.
912          For example, `struct' is mapped to an INTEGER_CST.  */
913       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
914         break;
915       /* else fall through */
916     case CPP_NAME:
917       fputs (IDENTIFIER_POINTER (token->u.value), stream);
918       break;
919
920     case CPP_STRING:
921     case CPP_STRING16:
922     case CPP_STRING32:
923     case CPP_WSTRING:
924     case CPP_UTF8STRING:
925       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
926       break;
927
928     default:
929       break;
930     }
931 }
932
933 /* Start emitting debugging information.  */
934
935 static void
936 cp_lexer_start_debugging (cp_lexer* lexer)
937 {
938   lexer->debugging_p = true;
939 }
940
941 /* Stop emitting debugging information.  */
942
943 static void
944 cp_lexer_stop_debugging (cp_lexer* lexer)
945 {
946   lexer->debugging_p = false;
947 }
948
949 #endif /* ENABLE_CHECKING */
950
951 /* Create a new cp_token_cache, representing a range of tokens.  */
952
953 static cp_token_cache *
954 cp_token_cache_new (cp_token *first, cp_token *last)
955 {
956   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
957   cache->first = first;
958   cache->last = last;
959   return cache;
960 }
961
962 \f
963 /* Decl-specifiers.  */
964
965 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
966
967 static void
968 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
969 {
970   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
971 }
972
973 /* Declarators.  */
974
975 /* Nothing other than the parser should be creating declarators;
976    declarators are a semi-syntactic representation of C++ entities.
977    Other parts of the front end that need to create entities (like
978    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
979
980 static cp_declarator *make_call_declarator
981   (cp_declarator *, tree, cp_cv_quals, tree, tree);
982 static cp_declarator *make_array_declarator
983   (cp_declarator *, tree);
984 static cp_declarator *make_pointer_declarator
985   (cp_cv_quals, cp_declarator *);
986 static cp_declarator *make_reference_declarator
987   (cp_cv_quals, cp_declarator *, bool);
988 static cp_parameter_declarator *make_parameter_declarator
989   (cp_decl_specifier_seq *, cp_declarator *, tree);
990 static cp_declarator *make_ptrmem_declarator
991   (cp_cv_quals, tree, cp_declarator *);
992
993 /* An erroneous declarator.  */
994 static cp_declarator *cp_error_declarator;
995
996 /* The obstack on which declarators and related data structures are
997    allocated.  */
998 static struct obstack declarator_obstack;
999
1000 /* Alloc BYTES from the declarator memory pool.  */
1001
1002 static inline void *
1003 alloc_declarator (size_t bytes)
1004 {
1005   return obstack_alloc (&declarator_obstack, bytes);
1006 }
1007
1008 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1009    common to all declarators.  */
1010
1011 static cp_declarator *
1012 make_declarator (cp_declarator_kind kind)
1013 {
1014   cp_declarator *declarator;
1015
1016   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1017   declarator->kind = kind;
1018   declarator->attributes = NULL_TREE;
1019   declarator->declarator = NULL;
1020   declarator->parameter_pack_p = false;
1021   declarator->id_loc = UNKNOWN_LOCATION;
1022
1023   return declarator;
1024 }
1025
1026 /* Make a declarator for a generalized identifier.  If
1027    QUALIFYING_SCOPE is non-NULL, the identifier is
1028    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1029    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1030    is, if any.   */
1031
1032 static cp_declarator *
1033 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1034                     special_function_kind sfk)
1035 {
1036   cp_declarator *declarator;
1037
1038   /* It is valid to write:
1039
1040        class C { void f(); };
1041        typedef C D;
1042        void D::f();
1043
1044      The standard is not clear about whether `typedef const C D' is
1045      legal; as of 2002-09-15 the committee is considering that
1046      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1047      well.  */
1048   if (qualifying_scope && TYPE_P (qualifying_scope))
1049     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1050
1051   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1052               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1053               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1054
1055   declarator = make_declarator (cdk_id);
1056   declarator->u.id.qualifying_scope = qualifying_scope;
1057   declarator->u.id.unqualified_name = unqualified_name;
1058   declarator->u.id.sfk = sfk;
1059   
1060   return declarator;
1061 }
1062
1063 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1064    of modifiers such as const or volatile to apply to the pointer
1065    type, represented as identifiers.  */
1066
1067 cp_declarator *
1068 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1069 {
1070   cp_declarator *declarator;
1071
1072   declarator = make_declarator (cdk_pointer);
1073   declarator->declarator = target;
1074   declarator->u.pointer.qualifiers = cv_qualifiers;
1075   declarator->u.pointer.class_type = NULL_TREE;
1076   if (target)
1077     {
1078       declarator->id_loc = target->id_loc;
1079       declarator->parameter_pack_p = target->parameter_pack_p;
1080       target->parameter_pack_p = false;
1081     }
1082   else
1083     declarator->parameter_pack_p = false;
1084
1085   return declarator;
1086 }
1087
1088 /* Like make_pointer_declarator -- but for references.  */
1089
1090 cp_declarator *
1091 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1092                            bool rvalue_ref)
1093 {
1094   cp_declarator *declarator;
1095
1096   declarator = make_declarator (cdk_reference);
1097   declarator->declarator = target;
1098   declarator->u.reference.qualifiers = cv_qualifiers;
1099   declarator->u.reference.rvalue_ref = rvalue_ref;
1100   if (target)
1101     {
1102       declarator->id_loc = target->id_loc;
1103       declarator->parameter_pack_p = target->parameter_pack_p;
1104       target->parameter_pack_p = false;
1105     }
1106   else
1107     declarator->parameter_pack_p = false;
1108
1109   return declarator;
1110 }
1111
1112 /* Like make_pointer_declarator -- but for a pointer to a non-static
1113    member of CLASS_TYPE.  */
1114
1115 cp_declarator *
1116 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1117                         cp_declarator *pointee)
1118 {
1119   cp_declarator *declarator;
1120
1121   declarator = make_declarator (cdk_ptrmem);
1122   declarator->declarator = pointee;
1123   declarator->u.pointer.qualifiers = cv_qualifiers;
1124   declarator->u.pointer.class_type = class_type;
1125
1126   if (pointee)
1127     {
1128       declarator->parameter_pack_p = pointee->parameter_pack_p;
1129       pointee->parameter_pack_p = false;
1130     }
1131   else
1132     declarator->parameter_pack_p = false;
1133
1134   return declarator;
1135 }
1136
1137 /* Make a declarator for the function given by TARGET, with the
1138    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1139    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1140    indicates what exceptions can be thrown.  */
1141
1142 cp_declarator *
1143 make_call_declarator (cp_declarator *target,
1144                       tree parms,
1145                       cp_cv_quals cv_qualifiers,
1146                       tree exception_specification,
1147                       tree late_return_type)
1148 {
1149   cp_declarator *declarator;
1150
1151   declarator = make_declarator (cdk_function);
1152   declarator->declarator = target;
1153   declarator->u.function.parameters = parms;
1154   declarator->u.function.qualifiers = cv_qualifiers;
1155   declarator->u.function.exception_specification = exception_specification;
1156   declarator->u.function.late_return_type = late_return_type;
1157   if (target)
1158     {
1159       declarator->id_loc = target->id_loc;
1160       declarator->parameter_pack_p = target->parameter_pack_p;
1161       target->parameter_pack_p = false;
1162     }
1163   else
1164     declarator->parameter_pack_p = false;
1165
1166   return declarator;
1167 }
1168
1169 /* Make a declarator for an array of BOUNDS elements, each of which is
1170    defined by ELEMENT.  */
1171
1172 cp_declarator *
1173 make_array_declarator (cp_declarator *element, tree bounds)
1174 {
1175   cp_declarator *declarator;
1176
1177   declarator = make_declarator (cdk_array);
1178   declarator->declarator = element;
1179   declarator->u.array.bounds = bounds;
1180   if (element)
1181     {
1182       declarator->id_loc = element->id_loc;
1183       declarator->parameter_pack_p = element->parameter_pack_p;
1184       element->parameter_pack_p = false;
1185     }
1186   else
1187     declarator->parameter_pack_p = false;
1188
1189   return declarator;
1190 }
1191
1192 /* Determine whether the declarator we've seen so far can be a
1193    parameter pack, when followed by an ellipsis.  */
1194 static bool 
1195 declarator_can_be_parameter_pack (cp_declarator *declarator)
1196 {
1197   /* Search for a declarator name, or any other declarator that goes
1198      after the point where the ellipsis could appear in a parameter
1199      pack. If we find any of these, then this declarator can not be
1200      made into a parameter pack.  */
1201   bool found = false;
1202   while (declarator && !found)
1203     {
1204       switch ((int)declarator->kind)
1205         {
1206         case cdk_id:
1207         case cdk_array:
1208           found = true;
1209           break;
1210
1211         case cdk_error:
1212           return true;
1213
1214         default:
1215           declarator = declarator->declarator;
1216           break;
1217         }
1218     }
1219
1220   return !found;
1221 }
1222
1223 cp_parameter_declarator *no_parameters;
1224
1225 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1226    DECLARATOR and DEFAULT_ARGUMENT.  */
1227
1228 cp_parameter_declarator *
1229 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1230                            cp_declarator *declarator,
1231                            tree default_argument)
1232 {
1233   cp_parameter_declarator *parameter;
1234
1235   parameter = ((cp_parameter_declarator *)
1236                alloc_declarator (sizeof (cp_parameter_declarator)));
1237   parameter->next = NULL;
1238   if (decl_specifiers)
1239     parameter->decl_specifiers = *decl_specifiers;
1240   else
1241     clear_decl_specs (&parameter->decl_specifiers);
1242   parameter->declarator = declarator;
1243   parameter->default_argument = default_argument;
1244   parameter->ellipsis_p = false;
1245
1246   return parameter;
1247 }
1248
1249 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1250
1251 static bool
1252 function_declarator_p (const cp_declarator *declarator)
1253 {
1254   while (declarator)
1255     {
1256       if (declarator->kind == cdk_function
1257           && declarator->declarator->kind == cdk_id)
1258         return true;
1259       if (declarator->kind == cdk_id
1260           || declarator->kind == cdk_error)
1261         return false;
1262       declarator = declarator->declarator;
1263     }
1264   return false;
1265 }
1266  
1267 /* The parser.  */
1268
1269 /* Overview
1270    --------
1271
1272    A cp_parser parses the token stream as specified by the C++
1273    grammar.  Its job is purely parsing, not semantic analysis.  For
1274    example, the parser breaks the token stream into declarators,
1275    expressions, statements, and other similar syntactic constructs.
1276    It does not check that the types of the expressions on either side
1277    of an assignment-statement are compatible, or that a function is
1278    not declared with a parameter of type `void'.
1279
1280    The parser invokes routines elsewhere in the compiler to perform
1281    semantic analysis and to build up the abstract syntax tree for the
1282    code processed.
1283
1284    The parser (and the template instantiation code, which is, in a
1285    way, a close relative of parsing) are the only parts of the
1286    compiler that should be calling push_scope and pop_scope, or
1287    related functions.  The parser (and template instantiation code)
1288    keeps track of what scope is presently active; everything else
1289    should simply honor that.  (The code that generates static
1290    initializers may also need to set the scope, in order to check
1291    access control correctly when emitting the initializers.)
1292
1293    Methodology
1294    -----------
1295
1296    The parser is of the standard recursive-descent variety.  Upcoming
1297    tokens in the token stream are examined in order to determine which
1298    production to use when parsing a non-terminal.  Some C++ constructs
1299    require arbitrary look ahead to disambiguate.  For example, it is
1300    impossible, in the general case, to tell whether a statement is an
1301    expression or declaration without scanning the entire statement.
1302    Therefore, the parser is capable of "parsing tentatively."  When the
1303    parser is not sure what construct comes next, it enters this mode.
1304    Then, while we attempt to parse the construct, the parser queues up
1305    error messages, rather than issuing them immediately, and saves the
1306    tokens it consumes.  If the construct is parsed successfully, the
1307    parser "commits", i.e., it issues any queued error messages and
1308    the tokens that were being preserved are permanently discarded.
1309    If, however, the construct is not parsed successfully, the parser
1310    rolls back its state completely so that it can resume parsing using
1311    a different alternative.
1312
1313    Future Improvements
1314    -------------------
1315
1316    The performance of the parser could probably be improved substantially.
1317    We could often eliminate the need to parse tentatively by looking ahead
1318    a little bit.  In some places, this approach might not entirely eliminate
1319    the need to parse tentatively, but it might still speed up the average
1320    case.  */
1321
1322 /* Flags that are passed to some parsing functions.  These values can
1323    be bitwise-ored together.  */
1324
1325 enum
1326 {
1327   /* No flags.  */
1328   CP_PARSER_FLAGS_NONE = 0x0,
1329   /* The construct is optional.  If it is not present, then no error
1330      should be issued.  */
1331   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1332   /* When parsing a type-specifier, treat user-defined type-names
1333      as non-type identifiers.  */
1334   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1335   /* When parsing a type-specifier, do not try to parse a class-specifier
1336      or enum-specifier.  */
1337   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1338 };
1339
1340 /* This type is used for parameters and variables which hold
1341    combinations of the above flags.  */
1342 typedef int cp_parser_flags;
1343
1344 /* The different kinds of declarators we want to parse.  */
1345
1346 typedef enum cp_parser_declarator_kind
1347 {
1348   /* We want an abstract declarator.  */
1349   CP_PARSER_DECLARATOR_ABSTRACT,
1350   /* We want a named declarator.  */
1351   CP_PARSER_DECLARATOR_NAMED,
1352   /* We don't mind, but the name must be an unqualified-id.  */
1353   CP_PARSER_DECLARATOR_EITHER
1354 } cp_parser_declarator_kind;
1355
1356 /* The precedence values used to parse binary expressions.  The minimum value
1357    of PREC must be 1, because zero is reserved to quickly discriminate
1358    binary operators from other tokens.  */
1359
1360 enum cp_parser_prec
1361 {
1362   PREC_NOT_OPERATOR,
1363   PREC_LOGICAL_OR_EXPRESSION,
1364   PREC_LOGICAL_AND_EXPRESSION,
1365   PREC_INCLUSIVE_OR_EXPRESSION,
1366   PREC_EXCLUSIVE_OR_EXPRESSION,
1367   PREC_AND_EXPRESSION,
1368   PREC_EQUALITY_EXPRESSION,
1369   PREC_RELATIONAL_EXPRESSION,
1370   PREC_SHIFT_EXPRESSION,
1371   PREC_ADDITIVE_EXPRESSION,
1372   PREC_MULTIPLICATIVE_EXPRESSION,
1373   PREC_PM_EXPRESSION,
1374   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1375 };
1376
1377 /* A mapping from a token type to a corresponding tree node type, with a
1378    precedence value.  */
1379
1380 typedef struct cp_parser_binary_operations_map_node
1381 {
1382   /* The token type.  */
1383   enum cpp_ttype token_type;
1384   /* The corresponding tree code.  */
1385   enum tree_code tree_type;
1386   /* The precedence of this operator.  */
1387   enum cp_parser_prec prec;
1388 } cp_parser_binary_operations_map_node;
1389
1390 /* The status of a tentative parse.  */
1391
1392 typedef enum cp_parser_status_kind
1393 {
1394   /* No errors have occurred.  */
1395   CP_PARSER_STATUS_KIND_NO_ERROR,
1396   /* An error has occurred.  */
1397   CP_PARSER_STATUS_KIND_ERROR,
1398   /* We are committed to this tentative parse, whether or not an error
1399      has occurred.  */
1400   CP_PARSER_STATUS_KIND_COMMITTED
1401 } cp_parser_status_kind;
1402
1403 typedef struct cp_parser_expression_stack_entry
1404 {
1405   /* Left hand side of the binary operation we are currently
1406      parsing.  */
1407   tree lhs;
1408   /* Original tree code for left hand side, if it was a binary
1409      expression itself (used for -Wparentheses).  */
1410   enum tree_code lhs_type;
1411   /* Tree code for the binary operation we are parsing.  */
1412   enum tree_code tree_type;
1413   /* Precedence of the binary operation we are parsing.  */
1414   enum cp_parser_prec prec;
1415 } cp_parser_expression_stack_entry;
1416
1417 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1418    entries because precedence levels on the stack are monotonically
1419    increasing.  */
1420 typedef struct cp_parser_expression_stack_entry
1421   cp_parser_expression_stack[NUM_PREC_VALUES];
1422
1423 /* Context that is saved and restored when parsing tentatively.  */
1424 typedef struct GTY (()) cp_parser_context {
1425   /* If this is a tentative parsing context, the status of the
1426      tentative parse.  */
1427   enum cp_parser_status_kind status;
1428   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1429      that are looked up in this context must be looked up both in the
1430      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1431      the context of the containing expression.  */
1432   tree object_type;
1433
1434   /* The next parsing context in the stack.  */
1435   struct cp_parser_context *next;
1436 } cp_parser_context;
1437
1438 /* Prototypes.  */
1439
1440 /* Constructors and destructors.  */
1441
1442 static cp_parser_context *cp_parser_context_new
1443   (cp_parser_context *);
1444
1445 /* Class variables.  */
1446
1447 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1448
1449 /* The operator-precedence table used by cp_parser_binary_expression.
1450    Transformed into an associative array (binops_by_token) by
1451    cp_parser_new.  */
1452
1453 static const cp_parser_binary_operations_map_node binops[] = {
1454   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1455   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1456
1457   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1458   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1459   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1460
1461   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1462   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1463
1464   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1465   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1466
1467   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1468   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1469   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1470   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1471
1472   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1473   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1474
1475   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1476
1477   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1478
1479   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1480
1481   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1482
1483   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1484 };
1485
1486 /* The same as binops, but initialized by cp_parser_new so that
1487    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1488    for speed.  */
1489 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1490
1491 /* Constructors and destructors.  */
1492
1493 /* Construct a new context.  The context below this one on the stack
1494    is given by NEXT.  */
1495
1496 static cp_parser_context *
1497 cp_parser_context_new (cp_parser_context* next)
1498 {
1499   cp_parser_context *context;
1500
1501   /* Allocate the storage.  */
1502   if (cp_parser_context_free_list != NULL)
1503     {
1504       /* Pull the first entry from the free list.  */
1505       context = cp_parser_context_free_list;
1506       cp_parser_context_free_list = context->next;
1507       memset (context, 0, sizeof (*context));
1508     }
1509   else
1510     context = ggc_alloc_cleared_cp_parser_context ();
1511
1512   /* No errors have occurred yet in this context.  */
1513   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1514   /* If this is not the bottommost context, copy information that we
1515      need from the previous context.  */
1516   if (next)
1517     {
1518       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1519          expression, then we are parsing one in this context, too.  */
1520       context->object_type = next->object_type;
1521       /* Thread the stack.  */
1522       context->next = next;
1523     }
1524
1525   return context;
1526 }
1527
1528 /* An entry in a queue of function arguments that require post-processing.  */
1529
1530 typedef struct GTY(()) cp_default_arg_entry_d {
1531   /* The current_class_type when we parsed this arg.  */
1532   tree class_type;
1533
1534   /* The function decl itself.  */
1535   tree decl;
1536 } cp_default_arg_entry;
1537
1538 DEF_VEC_O(cp_default_arg_entry);
1539 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1540
1541 /* An entry in a stack for member functions of local classes.  */
1542
1543 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1544   /* Functions with default arguments that require post-processing.
1545      Functions appear in this list in declaration order.  */
1546   VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1547
1548   /* Functions with defintions that require post-processing.  Functions
1549      appear in this list in declaration order.  */
1550   VEC(tree,gc) *funs_with_definitions;
1551 } cp_unparsed_functions_entry;
1552
1553 DEF_VEC_O(cp_unparsed_functions_entry);
1554 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1555
1556 /* The cp_parser structure represents the C++ parser.  */
1557
1558 typedef struct GTY(()) cp_parser {
1559   /* The lexer from which we are obtaining tokens.  */
1560   cp_lexer *lexer;
1561
1562   /* The scope in which names should be looked up.  If NULL_TREE, then
1563      we look up names in the scope that is currently open in the
1564      source program.  If non-NULL, this is either a TYPE or
1565      NAMESPACE_DECL for the scope in which we should look.  It can
1566      also be ERROR_MARK, when we've parsed a bogus scope.
1567
1568      This value is not cleared automatically after a name is looked
1569      up, so we must be careful to clear it before starting a new look
1570      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1571      will look up `Z' in the scope of `X', rather than the current
1572      scope.)  Unfortunately, it is difficult to tell when name lookup
1573      is complete, because we sometimes peek at a token, look it up,
1574      and then decide not to consume it.   */
1575   tree scope;
1576
1577   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1578      last lookup took place.  OBJECT_SCOPE is used if an expression
1579      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1580      respectively.  QUALIFYING_SCOPE is used for an expression of the
1581      form "X::Y"; it refers to X.  */
1582   tree object_scope;
1583   tree qualifying_scope;
1584
1585   /* A stack of parsing contexts.  All but the bottom entry on the
1586      stack will be tentative contexts.
1587
1588      We parse tentatively in order to determine which construct is in
1589      use in some situations.  For example, in order to determine
1590      whether a statement is an expression-statement or a
1591      declaration-statement we parse it tentatively as a
1592      declaration-statement.  If that fails, we then reparse the same
1593      token stream as an expression-statement.  */
1594   cp_parser_context *context;
1595
1596   /* True if we are parsing GNU C++.  If this flag is not set, then
1597      GNU extensions are not recognized.  */
1598   bool allow_gnu_extensions_p;
1599
1600   /* TRUE if the `>' token should be interpreted as the greater-than
1601      operator.  FALSE if it is the end of a template-id or
1602      template-parameter-list. In C++0x mode, this flag also applies to
1603      `>>' tokens, which are viewed as two consecutive `>' tokens when
1604      this flag is FALSE.  */
1605   bool greater_than_is_operator_p;
1606
1607   /* TRUE if default arguments are allowed within a parameter list
1608      that starts at this point. FALSE if only a gnu extension makes
1609      them permissible.  */
1610   bool default_arg_ok_p;
1611
1612   /* TRUE if we are parsing an integral constant-expression.  See
1613      [expr.const] for a precise definition.  */
1614   bool integral_constant_expression_p;
1615
1616   /* TRUE if we are parsing an integral constant-expression -- but a
1617      non-constant expression should be permitted as well.  This flag
1618      is used when parsing an array bound so that GNU variable-length
1619      arrays are tolerated.  */
1620   bool allow_non_integral_constant_expression_p;
1621
1622   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1623      been seen that makes the expression non-constant.  */
1624   bool non_integral_constant_expression_p;
1625
1626   /* TRUE if local variable names and `this' are forbidden in the
1627      current context.  */
1628   bool local_variables_forbidden_p;
1629
1630   /* TRUE if the declaration we are parsing is part of a
1631      linkage-specification of the form `extern string-literal
1632      declaration'.  */
1633   bool in_unbraced_linkage_specification_p;
1634
1635   /* TRUE if we are presently parsing a declarator, after the
1636      direct-declarator.  */
1637   bool in_declarator_p;
1638
1639   /* TRUE if we are presently parsing a template-argument-list.  */
1640   bool in_template_argument_list_p;
1641
1642   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1643      to IN_OMP_BLOCK if parsing OpenMP structured block and
1644      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1645      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1646      iteration-statement, OpenMP block or loop within that switch.  */
1647 #define IN_SWITCH_STMT          1
1648 #define IN_ITERATION_STMT       2
1649 #define IN_OMP_BLOCK            4
1650 #define IN_OMP_FOR              8
1651 #define IN_IF_STMT             16
1652   unsigned char in_statement;
1653
1654   /* TRUE if we are presently parsing the body of a switch statement.
1655      Note that this doesn't quite overlap with in_statement above.
1656      The difference relates to giving the right sets of error messages:
1657      "case not in switch" vs "break statement used with OpenMP...".  */
1658   bool in_switch_statement_p;
1659
1660   /* TRUE if we are parsing a type-id in an expression context.  In
1661      such a situation, both "type (expr)" and "type (type)" are valid
1662      alternatives.  */
1663   bool in_type_id_in_expr_p;
1664
1665   /* TRUE if we are currently in a header file where declarations are
1666      implicitly extern "C".  */
1667   bool implicit_extern_c;
1668
1669   /* TRUE if strings in expressions should be translated to the execution
1670      character set.  */
1671   bool translate_strings_p;
1672
1673   /* TRUE if we are presently parsing the body of a function, but not
1674      a local class.  */
1675   bool in_function_body;
1676
1677   /* If non-NULL, then we are parsing a construct where new type
1678      definitions are not permitted.  The string stored here will be
1679      issued as an error message if a type is defined.  */
1680   const char *type_definition_forbidden_message;
1681
1682   /* A stack used for member functions of local classes.  The lists
1683      contained in an individual entry can only be processed once the
1684      outermost class being defined is complete.  */
1685   VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1686
1687   /* The number of classes whose definitions are currently in
1688      progress.  */
1689   unsigned num_classes_being_defined;
1690
1691   /* The number of template parameter lists that apply directly to the
1692      current declaration.  */
1693   unsigned num_template_parameter_lists;
1694 } cp_parser;
1695
1696 /* Managing the unparsed function queues.  */
1697
1698 #define unparsed_funs_with_default_args \
1699   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1700 #define unparsed_funs_with_definitions \
1701   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1702
1703 static void
1704 push_unparsed_function_queues (cp_parser *parser)
1705 {
1706   VEC_safe_push (cp_unparsed_functions_entry, gc,
1707                  parser->unparsed_queues, NULL);
1708   unparsed_funs_with_default_args = NULL;
1709   unparsed_funs_with_definitions = make_tree_vector ();
1710 }
1711
1712 static void
1713 pop_unparsed_function_queues (cp_parser *parser)
1714 {
1715   release_tree_vector (unparsed_funs_with_definitions);
1716   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1717 }
1718
1719 /* Prototypes.  */
1720
1721 /* Constructors and destructors.  */
1722
1723 static cp_parser *cp_parser_new
1724   (void);
1725
1726 /* Routines to parse various constructs.
1727
1728    Those that return `tree' will return the error_mark_node (rather
1729    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1730    Sometimes, they will return an ordinary node if error-recovery was
1731    attempted, even though a parse error occurred.  So, to check
1732    whether or not a parse error occurred, you should always use
1733    cp_parser_error_occurred.  If the construct is optional (indicated
1734    either by an `_opt' in the name of the function that does the
1735    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1736    the construct is not present.  */
1737
1738 /* Lexical conventions [gram.lex]  */
1739
1740 static tree cp_parser_identifier
1741   (cp_parser *);
1742 static tree cp_parser_string_literal
1743   (cp_parser *, bool, bool);
1744
1745 /* Basic concepts [gram.basic]  */
1746
1747 static bool cp_parser_translation_unit
1748   (cp_parser *);
1749
1750 /* Expressions [gram.expr]  */
1751
1752 static tree cp_parser_primary_expression
1753   (cp_parser *, bool, bool, bool, cp_id_kind *);
1754 static tree cp_parser_id_expression
1755   (cp_parser *, bool, bool, bool *, bool, bool);
1756 static tree cp_parser_unqualified_id
1757   (cp_parser *, bool, bool, bool, bool);
1758 static tree cp_parser_nested_name_specifier_opt
1759   (cp_parser *, bool, bool, bool, bool);
1760 static tree cp_parser_nested_name_specifier
1761   (cp_parser *, bool, bool, bool, bool);
1762 static tree cp_parser_qualifying_entity
1763   (cp_parser *, bool, bool, bool, bool, bool);
1764 static tree cp_parser_postfix_expression
1765   (cp_parser *, bool, bool, bool, cp_id_kind *);
1766 static tree cp_parser_postfix_open_square_expression
1767   (cp_parser *, tree, bool);
1768 static tree cp_parser_postfix_dot_deref_expression
1769   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1770 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1771   (cp_parser *, int, bool, bool, bool *);
1772 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1773 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1774 static void cp_parser_pseudo_destructor_name
1775   (cp_parser *, tree *, tree *);
1776 static tree cp_parser_unary_expression
1777   (cp_parser *, bool, bool, cp_id_kind *);
1778 static enum tree_code cp_parser_unary_operator
1779   (cp_token *);
1780 static tree cp_parser_new_expression
1781   (cp_parser *);
1782 static VEC(tree,gc) *cp_parser_new_placement
1783   (cp_parser *);
1784 static tree cp_parser_new_type_id
1785   (cp_parser *, tree *);
1786 static cp_declarator *cp_parser_new_declarator_opt
1787   (cp_parser *);
1788 static cp_declarator *cp_parser_direct_new_declarator
1789   (cp_parser *);
1790 static VEC(tree,gc) *cp_parser_new_initializer
1791   (cp_parser *);
1792 static tree cp_parser_delete_expression
1793   (cp_parser *);
1794 static tree cp_parser_cast_expression
1795   (cp_parser *, bool, bool, cp_id_kind *);
1796 static tree cp_parser_binary_expression
1797   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1798 static tree cp_parser_question_colon_clause
1799   (cp_parser *, tree);
1800 static tree cp_parser_assignment_expression
1801   (cp_parser *, bool, cp_id_kind *);
1802 static enum tree_code cp_parser_assignment_operator_opt
1803   (cp_parser *);
1804 static tree cp_parser_expression
1805   (cp_parser *, bool, cp_id_kind *);
1806 static tree cp_parser_constant_expression
1807   (cp_parser *, bool, bool *);
1808 static tree cp_parser_builtin_offsetof
1809   (cp_parser *);
1810 static tree cp_parser_lambda_expression
1811   (cp_parser *);
1812 static void cp_parser_lambda_introducer
1813   (cp_parser *, tree);
1814 static void cp_parser_lambda_declarator_opt
1815   (cp_parser *, tree);
1816 static void cp_parser_lambda_body
1817   (cp_parser *, tree);
1818
1819 /* Statements [gram.stmt.stmt]  */
1820
1821 static void cp_parser_statement
1822   (cp_parser *, tree, bool, bool *);
1823 static void cp_parser_label_for_labeled_statement
1824   (cp_parser *);
1825 static tree cp_parser_expression_statement
1826   (cp_parser *, tree);
1827 static tree cp_parser_compound_statement
1828   (cp_parser *, tree, bool);
1829 static void cp_parser_statement_seq_opt
1830   (cp_parser *, tree);
1831 static tree cp_parser_selection_statement
1832   (cp_parser *, bool *);
1833 static tree cp_parser_condition
1834   (cp_parser *);
1835 static tree cp_parser_iteration_statement
1836   (cp_parser *);
1837 static void cp_parser_for_init_statement
1838   (cp_parser *);
1839 static tree  cp_parser_c_for
1840   (cp_parser *);
1841 static tree  cp_parser_range_for
1842   (cp_parser *);
1843 static tree cp_parser_jump_statement
1844   (cp_parser *);
1845 static void cp_parser_declaration_statement
1846   (cp_parser *);
1847
1848 static tree cp_parser_implicitly_scoped_statement
1849   (cp_parser *, bool *);
1850 static void cp_parser_already_scoped_statement
1851   (cp_parser *);
1852
1853 /* Declarations [gram.dcl.dcl] */
1854
1855 static void cp_parser_declaration_seq_opt
1856   (cp_parser *);
1857 static void cp_parser_declaration
1858   (cp_parser *);
1859 static void cp_parser_block_declaration
1860   (cp_parser *, bool);
1861 static void cp_parser_simple_declaration
1862   (cp_parser *, bool);
1863 static void cp_parser_decl_specifier_seq
1864   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1865 static tree cp_parser_storage_class_specifier_opt
1866   (cp_parser *);
1867 static tree cp_parser_function_specifier_opt
1868   (cp_parser *, cp_decl_specifier_seq *);
1869 static tree cp_parser_type_specifier
1870   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1871    int *, bool *);
1872 static tree cp_parser_simple_type_specifier
1873   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1874 static tree cp_parser_type_name
1875   (cp_parser *);
1876 static tree cp_parser_nonclass_name 
1877   (cp_parser* parser);
1878 static tree cp_parser_elaborated_type_specifier
1879   (cp_parser *, bool, bool);
1880 static tree cp_parser_enum_specifier
1881   (cp_parser *);
1882 static void cp_parser_enumerator_list
1883   (cp_parser *, tree);
1884 static void cp_parser_enumerator_definition
1885   (cp_parser *, tree);
1886 static tree cp_parser_namespace_name
1887   (cp_parser *);
1888 static void cp_parser_namespace_definition
1889   (cp_parser *);
1890 static void cp_parser_namespace_body
1891   (cp_parser *);
1892 static tree cp_parser_qualified_namespace_specifier
1893   (cp_parser *);
1894 static void cp_parser_namespace_alias_definition
1895   (cp_parser *);
1896 static bool cp_parser_using_declaration
1897   (cp_parser *, bool);
1898 static void cp_parser_using_directive
1899   (cp_parser *);
1900 static void cp_parser_asm_definition
1901   (cp_parser *);
1902 static void cp_parser_linkage_specification
1903   (cp_parser *);
1904 static void cp_parser_static_assert
1905   (cp_parser *, bool);
1906 static tree cp_parser_decltype
1907   (cp_parser *);
1908
1909 /* Declarators [gram.dcl.decl] */
1910
1911 static tree cp_parser_init_declarator
1912   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1913 static cp_declarator *cp_parser_declarator
1914   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1915 static cp_declarator *cp_parser_direct_declarator
1916   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1917 static enum tree_code cp_parser_ptr_operator
1918   (cp_parser *, tree *, cp_cv_quals *);
1919 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1920   (cp_parser *);
1921 static tree cp_parser_late_return_type_opt
1922   (cp_parser *);
1923 static tree cp_parser_declarator_id
1924   (cp_parser *, bool);
1925 static tree cp_parser_type_id
1926   (cp_parser *);
1927 static tree cp_parser_template_type_arg
1928   (cp_parser *);
1929 static tree cp_parser_trailing_type_id (cp_parser *);
1930 static tree cp_parser_type_id_1
1931   (cp_parser *, bool, bool);
1932 static void cp_parser_type_specifier_seq
1933   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1934 static tree cp_parser_parameter_declaration_clause
1935   (cp_parser *);
1936 static tree cp_parser_parameter_declaration_list
1937   (cp_parser *, bool *);
1938 static cp_parameter_declarator *cp_parser_parameter_declaration
1939   (cp_parser *, bool, bool *);
1940 static tree cp_parser_default_argument 
1941   (cp_parser *, bool);
1942 static void cp_parser_function_body
1943   (cp_parser *);
1944 static tree cp_parser_initializer
1945   (cp_parser *, bool *, bool *);
1946 static tree cp_parser_initializer_clause
1947   (cp_parser *, bool *);
1948 static tree cp_parser_braced_list
1949   (cp_parser*, bool*);
1950 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1951   (cp_parser *, bool *);
1952
1953 static bool cp_parser_ctor_initializer_opt_and_function_body
1954   (cp_parser *);
1955
1956 /* Classes [gram.class] */
1957
1958 static tree cp_parser_class_name
1959   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1960 static tree cp_parser_class_specifier
1961   (cp_parser *);
1962 static tree cp_parser_class_head
1963   (cp_parser *, bool *, tree *, tree *);
1964 static enum tag_types cp_parser_class_key
1965   (cp_parser *);
1966 static void cp_parser_member_specification_opt
1967   (cp_parser *);
1968 static void cp_parser_member_declaration
1969   (cp_parser *);
1970 static tree cp_parser_pure_specifier
1971   (cp_parser *);
1972 static tree cp_parser_constant_initializer
1973   (cp_parser *);
1974
1975 /* Derived classes [gram.class.derived] */
1976
1977 static tree cp_parser_base_clause
1978   (cp_parser *);
1979 static tree cp_parser_base_specifier
1980   (cp_parser *);
1981
1982 /* Special member functions [gram.special] */
1983
1984 static tree cp_parser_conversion_function_id
1985   (cp_parser *);
1986 static tree cp_parser_conversion_type_id
1987   (cp_parser *);
1988 static cp_declarator *cp_parser_conversion_declarator_opt
1989   (cp_parser *);
1990 static bool cp_parser_ctor_initializer_opt
1991   (cp_parser *);
1992 static void cp_parser_mem_initializer_list
1993   (cp_parser *);
1994 static tree cp_parser_mem_initializer
1995   (cp_parser *);
1996 static tree cp_parser_mem_initializer_id
1997   (cp_parser *);
1998
1999 /* Overloading [gram.over] */
2000
2001 static tree cp_parser_operator_function_id
2002   (cp_parser *);
2003 static tree cp_parser_operator
2004   (cp_parser *);
2005
2006 /* Templates [gram.temp] */
2007
2008 static void cp_parser_template_declaration
2009   (cp_parser *, bool);
2010 static tree cp_parser_template_parameter_list
2011   (cp_parser *);
2012 static tree cp_parser_template_parameter
2013   (cp_parser *, bool *, bool *);
2014 static tree cp_parser_type_parameter
2015   (cp_parser *, bool *);
2016 static tree cp_parser_template_id
2017   (cp_parser *, bool, bool, bool);
2018 static tree cp_parser_template_name
2019   (cp_parser *, bool, bool, bool, bool *);
2020 static tree cp_parser_template_argument_list
2021   (cp_parser *);
2022 static tree cp_parser_template_argument
2023   (cp_parser *);
2024 static void cp_parser_explicit_instantiation
2025   (cp_parser *);
2026 static void cp_parser_explicit_specialization
2027   (cp_parser *);
2028
2029 /* Exception handling [gram.exception] */
2030
2031 static tree cp_parser_try_block
2032   (cp_parser *);
2033 static bool cp_parser_function_try_block
2034   (cp_parser *);
2035 static void cp_parser_handler_seq
2036   (cp_parser *);
2037 static void cp_parser_handler
2038   (cp_parser *);
2039 static tree cp_parser_exception_declaration
2040   (cp_parser *);
2041 static tree cp_parser_throw_expression
2042   (cp_parser *);
2043 static tree cp_parser_exception_specification_opt
2044   (cp_parser *);
2045 static tree cp_parser_type_id_list
2046   (cp_parser *);
2047
2048 /* GNU Extensions */
2049
2050 static tree cp_parser_asm_specification_opt
2051   (cp_parser *);
2052 static tree cp_parser_asm_operand_list
2053   (cp_parser *);
2054 static tree cp_parser_asm_clobber_list
2055   (cp_parser *);
2056 static tree cp_parser_asm_label_list
2057   (cp_parser *);
2058 static tree cp_parser_attributes_opt
2059   (cp_parser *);
2060 static tree cp_parser_attribute_list
2061   (cp_parser *);
2062 static bool cp_parser_extension_opt
2063   (cp_parser *, int *);
2064 static void cp_parser_label_declaration
2065   (cp_parser *);
2066
2067 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2068 static bool cp_parser_pragma
2069   (cp_parser *, enum pragma_context);
2070
2071 /* Objective-C++ Productions */
2072
2073 static tree cp_parser_objc_message_receiver
2074   (cp_parser *);
2075 static tree cp_parser_objc_message_args
2076   (cp_parser *);
2077 static tree cp_parser_objc_message_expression
2078   (cp_parser *);
2079 static tree cp_parser_objc_encode_expression
2080   (cp_parser *);
2081 static tree cp_parser_objc_defs_expression
2082   (cp_parser *);
2083 static tree cp_parser_objc_protocol_expression
2084   (cp_parser *);
2085 static tree cp_parser_objc_selector_expression
2086   (cp_parser *);
2087 static tree cp_parser_objc_expression
2088   (cp_parser *);
2089 static bool cp_parser_objc_selector_p
2090   (enum cpp_ttype);
2091 static tree cp_parser_objc_selector
2092   (cp_parser *);
2093 static tree cp_parser_objc_protocol_refs_opt
2094   (cp_parser *);
2095 static void cp_parser_objc_declaration
2096   (cp_parser *, tree);
2097 static tree cp_parser_objc_statement
2098   (cp_parser *);
2099 static bool cp_parser_objc_valid_prefix_attributes
2100   (cp_parser* parser, tree *attrib);
2101
2102 /* Utility Routines */
2103
2104 static tree cp_parser_lookup_name
2105   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2106 static tree cp_parser_lookup_name_simple
2107   (cp_parser *, tree, location_t);
2108 static tree cp_parser_maybe_treat_template_as_class
2109   (tree, bool);
2110 static bool cp_parser_check_declarator_template_parameters
2111   (cp_parser *, cp_declarator *, location_t);
2112 static bool cp_parser_check_template_parameters
2113   (cp_parser *, unsigned, location_t, cp_declarator *);
2114 static tree cp_parser_simple_cast_expression
2115   (cp_parser *);
2116 static tree cp_parser_global_scope_opt
2117   (cp_parser *, bool);
2118 static bool cp_parser_constructor_declarator_p
2119   (cp_parser *, bool);
2120 static tree cp_parser_function_definition_from_specifiers_and_declarator
2121   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2122 static tree cp_parser_function_definition_after_declarator
2123   (cp_parser *, bool);
2124 static void cp_parser_template_declaration_after_export
2125   (cp_parser *, bool);
2126 static void cp_parser_perform_template_parameter_access_checks
2127   (VEC (deferred_access_check,gc)*);
2128 static tree cp_parser_single_declaration
2129   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2130 static tree cp_parser_functional_cast
2131   (cp_parser *, tree);
2132 static tree cp_parser_save_member_function_body
2133   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2134 static tree cp_parser_enclosed_template_argument_list
2135   (cp_parser *);
2136 static void cp_parser_save_default_args
2137   (cp_parser *, tree);
2138 static void cp_parser_late_parsing_for_member
2139   (cp_parser *, tree);
2140 static void cp_parser_late_parsing_default_args
2141   (cp_parser *, tree);
2142 static tree cp_parser_sizeof_operand
2143   (cp_parser *, enum rid);
2144 static tree cp_parser_trait_expr
2145   (cp_parser *, enum rid);
2146 static bool cp_parser_declares_only_class_p
2147   (cp_parser *);
2148 static void cp_parser_set_storage_class
2149   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2150 static void cp_parser_set_decl_spec_type
2151   (cp_decl_specifier_seq *, tree, location_t, bool);
2152 static bool cp_parser_friend_p
2153   (const cp_decl_specifier_seq *);
2154 static void cp_parser_required_error
2155   (cp_parser *, required_token, bool);
2156 static cp_token *cp_parser_require
2157   (cp_parser *, enum cpp_ttype, required_token);
2158 static cp_token *cp_parser_require_keyword
2159   (cp_parser *, enum rid, required_token);
2160 static bool cp_parser_token_starts_function_definition_p
2161   (cp_token *);
2162 static bool cp_parser_next_token_starts_class_definition_p
2163   (cp_parser *);
2164 static bool cp_parser_next_token_ends_template_argument_p
2165   (cp_parser *);
2166 static bool cp_parser_nth_token_starts_template_argument_list_p
2167   (cp_parser *, size_t);
2168 static enum tag_types cp_parser_token_is_class_key
2169   (cp_token *);
2170 static void cp_parser_check_class_key
2171   (enum tag_types, tree type);
2172 static void cp_parser_check_access_in_redeclaration
2173   (tree type, location_t location);
2174 static bool cp_parser_optional_template_keyword
2175   (cp_parser *);
2176 static void cp_parser_pre_parsed_nested_name_specifier
2177   (cp_parser *);
2178 static bool cp_parser_cache_group
2179   (cp_parser *, enum cpp_ttype, unsigned);
2180 static void cp_parser_parse_tentatively
2181   (cp_parser *);
2182 static void cp_parser_commit_to_tentative_parse
2183   (cp_parser *);
2184 static void cp_parser_abort_tentative_parse
2185   (cp_parser *);
2186 static bool cp_parser_parse_definitely
2187   (cp_parser *);
2188 static inline bool cp_parser_parsing_tentatively
2189   (cp_parser *);
2190 static bool cp_parser_uncommitted_to_tentative_parse_p
2191   (cp_parser *);
2192 static void cp_parser_error
2193   (cp_parser *, const char *);
2194 static void cp_parser_name_lookup_error
2195   (cp_parser *, tree, tree, name_lookup_error, location_t);
2196 static bool cp_parser_simulate_error
2197   (cp_parser *);
2198 static bool cp_parser_check_type_definition
2199   (cp_parser *);
2200 static void cp_parser_check_for_definition_in_return_type
2201   (cp_declarator *, tree, location_t type_location);
2202 static void cp_parser_check_for_invalid_template_id
2203   (cp_parser *, tree, location_t location);
2204 static bool cp_parser_non_integral_constant_expression
2205   (cp_parser *, non_integral_constant);
2206 static void cp_parser_diagnose_invalid_type_name
2207   (cp_parser *, tree, tree, location_t);
2208 static bool cp_parser_parse_and_diagnose_invalid_type_name
2209   (cp_parser *);
2210 static int cp_parser_skip_to_closing_parenthesis
2211   (cp_parser *, bool, bool, bool);
2212 static void cp_parser_skip_to_end_of_statement
2213   (cp_parser *);
2214 static void cp_parser_consume_semicolon_at_end_of_statement
2215   (cp_parser *);
2216 static void cp_parser_skip_to_end_of_block_or_statement
2217   (cp_parser *);
2218 static bool cp_parser_skip_to_closing_brace
2219   (cp_parser *);
2220 static void cp_parser_skip_to_end_of_template_parameter_list
2221   (cp_parser *);
2222 static void cp_parser_skip_to_pragma_eol
2223   (cp_parser*, cp_token *);
2224 static bool cp_parser_error_occurred
2225   (cp_parser *);
2226 static bool cp_parser_allow_gnu_extensions_p
2227   (cp_parser *);
2228 static bool cp_parser_is_string_literal
2229   (cp_token *);
2230 static bool cp_parser_is_keyword
2231   (cp_token *, enum rid);
2232 static tree cp_parser_make_typename_type
2233   (cp_parser *, tree, tree, location_t location);
2234 static cp_declarator * cp_parser_make_indirect_declarator
2235   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2236
2237 /* Returns nonzero if we are parsing tentatively.  */
2238
2239 static inline bool
2240 cp_parser_parsing_tentatively (cp_parser* parser)
2241 {
2242   return parser->context->next != NULL;
2243 }
2244
2245 /* Returns nonzero if TOKEN is a string literal.  */
2246
2247 static bool
2248 cp_parser_is_string_literal (cp_token* token)
2249 {
2250   return (token->type == CPP_STRING ||
2251           token->type == CPP_STRING16 ||
2252           token->type == CPP_STRING32 ||
2253           token->type == CPP_WSTRING ||
2254           token->type == CPP_UTF8STRING);
2255 }
2256
2257 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2258
2259 static bool
2260 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2261 {
2262   return token->keyword == keyword;
2263 }
2264
2265 /* If not parsing tentatively, issue a diagnostic of the form
2266       FILE:LINE: MESSAGE before TOKEN
2267    where TOKEN is the next token in the input stream.  MESSAGE
2268    (specified by the caller) is usually of the form "expected
2269    OTHER-TOKEN".  */
2270
2271 static void
2272 cp_parser_error (cp_parser* parser, const char* gmsgid)
2273 {
2274   if (!cp_parser_simulate_error (parser))
2275     {
2276       cp_token *token = cp_lexer_peek_token (parser->lexer);
2277       /* This diagnostic makes more sense if it is tagged to the line
2278          of the token we just peeked at.  */
2279       cp_lexer_set_source_position_from_token (token);
2280
2281       if (token->type == CPP_PRAGMA)
2282         {
2283           error_at (token->location,
2284                     "%<#pragma%> is not allowed here");
2285           cp_parser_skip_to_pragma_eol (parser, token);
2286           return;
2287         }
2288
2289       c_parse_error (gmsgid,
2290                      /* Because c_parser_error does not understand
2291                         CPP_KEYWORD, keywords are treated like
2292                         identifiers.  */
2293                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2294                      token->u.value, token->flags);
2295     }
2296 }
2297
2298 /* Issue an error about name-lookup failing.  NAME is the
2299    IDENTIFIER_NODE DECL is the result of
2300    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2301    the thing that we hoped to find.  */
2302
2303 static void
2304 cp_parser_name_lookup_error (cp_parser* parser,
2305                              tree name,
2306                              tree decl,
2307                              name_lookup_error desired,
2308                              location_t location)
2309 {
2310   /* If name lookup completely failed, tell the user that NAME was not
2311      declared.  */
2312   if (decl == error_mark_node)
2313     {
2314       if (parser->scope && parser->scope != global_namespace)
2315         error_at (location, "%<%E::%E%> has not been declared",
2316                   parser->scope, name);
2317       else if (parser->scope == global_namespace)
2318         error_at (location, "%<::%E%> has not been declared", name);
2319       else if (parser->object_scope
2320                && !CLASS_TYPE_P (parser->object_scope))
2321         error_at (location, "request for member %qE in non-class type %qT",
2322                   name, parser->object_scope);
2323       else if (parser->object_scope)
2324         error_at (location, "%<%T::%E%> has not been declared",
2325                   parser->object_scope, name);
2326       else
2327         error_at (location, "%qE has not been declared", name);
2328     }
2329   else if (parser->scope && parser->scope != global_namespace)
2330     {
2331       switch (desired)
2332         {
2333           case NLE_TYPE:
2334             error_at (location, "%<%E::%E%> is not a type",
2335                                 parser->scope, name);
2336             break;
2337           case NLE_CXX98:
2338             error_at (location, "%<%E::%E%> is not a class or namespace",
2339                                 parser->scope, name);
2340             break;
2341           case NLE_NOT_CXX98:
2342             error_at (location,
2343                       "%<%E::%E%> is not a class, namespace, or enumeration",
2344                       parser->scope, name);
2345             break;
2346           default:
2347             gcc_unreachable ();
2348             
2349         }
2350     }
2351   else if (parser->scope == global_namespace)
2352     {
2353       switch (desired)
2354         {
2355           case NLE_TYPE:
2356             error_at (location, "%<::%E%> is not a type", name);
2357             break;
2358           case NLE_CXX98:
2359             error_at (location, "%<::%E%> is not a class or namespace", name);
2360             break;
2361           case NLE_NOT_CXX98:
2362             error_at (location,
2363                       "%<::%E%> is not a class, namespace, or enumeration",
2364                       name);
2365             break;
2366           default:
2367             gcc_unreachable ();
2368         }
2369     }
2370   else
2371     {
2372       switch (desired)
2373         {
2374           case NLE_TYPE:
2375             error_at (location, "%qE is not a type", name);
2376             break;
2377           case NLE_CXX98:
2378             error_at (location, "%qE is not a class or namespace", name);
2379             break;
2380           case NLE_NOT_CXX98:
2381             error_at (location,
2382                       "%qE is not a class, namespace, or enumeration", name);
2383             break;
2384           default:
2385             gcc_unreachable ();
2386         }
2387     }
2388 }
2389
2390 /* If we are parsing tentatively, remember that an error has occurred
2391    during this tentative parse.  Returns true if the error was
2392    simulated; false if a message should be issued by the caller.  */
2393
2394 static bool
2395 cp_parser_simulate_error (cp_parser* parser)
2396 {
2397   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2398     {
2399       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2400       return true;
2401     }
2402   return false;
2403 }
2404
2405 /* Check for repeated decl-specifiers.  */
2406
2407 static void
2408 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2409                            location_t location)
2410 {
2411   int ds;
2412
2413   for (ds = ds_first; ds != ds_last; ++ds)
2414     {
2415       unsigned count = decl_specs->specs[ds];
2416       if (count < 2)
2417         continue;
2418       /* The "long" specifier is a special case because of "long long".  */
2419       if (ds == ds_long)
2420         {
2421           if (count > 2)
2422             error_at (location, "%<long long long%> is too long for GCC");
2423           else 
2424             pedwarn_cxx98 (location, OPT_Wlong_long, 
2425                            "ISO C++ 1998 does not support %<long long%>");
2426         }
2427       else if (count > 1)
2428         {
2429           static const char *const decl_spec_names[] = {
2430             "signed",
2431             "unsigned",
2432             "short",
2433             "long",
2434             "const",
2435             "volatile",
2436             "restrict",
2437             "inline",
2438             "virtual",
2439             "explicit",
2440             "friend",
2441             "typedef",
2442             "constexpr",
2443             "__complex",
2444             "__thread"
2445           };
2446           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2447         }
2448     }
2449 }
2450
2451 /* This function is called when a type is defined.  If type
2452    definitions are forbidden at this point, an error message is
2453    issued.  */
2454
2455 static bool
2456 cp_parser_check_type_definition (cp_parser* parser)
2457 {
2458   /* If types are forbidden here, issue a message.  */
2459   if (parser->type_definition_forbidden_message)
2460     {
2461       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2462          in the message need to be interpreted.  */
2463       error (parser->type_definition_forbidden_message);
2464       return false;
2465     }
2466   return true;
2467 }
2468
2469 /* This function is called when the DECLARATOR is processed.  The TYPE
2470    was a type defined in the decl-specifiers.  If it is invalid to
2471    define a type in the decl-specifiers for DECLARATOR, an error is
2472    issued. TYPE_LOCATION is the location of TYPE and is used
2473    for error reporting.  */
2474
2475 static void
2476 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2477                                                tree type, location_t type_location)
2478 {
2479   /* [dcl.fct] forbids type definitions in return types.
2480      Unfortunately, it's not easy to know whether or not we are
2481      processing a return type until after the fact.  */
2482   while (declarator
2483          && (declarator->kind == cdk_pointer
2484              || declarator->kind == cdk_reference
2485              || declarator->kind == cdk_ptrmem))
2486     declarator = declarator->declarator;
2487   if (declarator
2488       && declarator->kind == cdk_function)
2489     {
2490       error_at (type_location,
2491                 "new types may not be defined in a return type");
2492       inform (type_location, 
2493               "(perhaps a semicolon is missing after the definition of %qT)",
2494               type);
2495     }
2496 }
2497
2498 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2499    "<" in any valid C++ program.  If the next token is indeed "<",
2500    issue a message warning the user about what appears to be an
2501    invalid attempt to form a template-id. LOCATION is the location
2502    of the type-specifier (TYPE) */
2503
2504 static void
2505 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2506                                          tree type, location_t location)
2507 {
2508   cp_token_position start = 0;
2509
2510   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2511     {
2512       if (TYPE_P (type))
2513         error_at (location, "%qT is not a template", type);
2514       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2515         error_at (location, "%qE is not a template", type);
2516       else
2517         error_at (location, "invalid template-id");
2518       /* Remember the location of the invalid "<".  */
2519       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2520         start = cp_lexer_token_position (parser->lexer, true);
2521       /* Consume the "<".  */
2522       cp_lexer_consume_token (parser->lexer);
2523       /* Parse the template arguments.  */
2524       cp_parser_enclosed_template_argument_list (parser);
2525       /* Permanently remove the invalid template arguments so that
2526          this error message is not issued again.  */
2527       if (start)
2528         cp_lexer_purge_tokens_after (parser->lexer, start);
2529     }
2530 }
2531
2532 /* If parsing an integral constant-expression, issue an error message
2533    about the fact that THING appeared and return true.  Otherwise,
2534    return false.  In either case, set
2535    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2536
2537 static bool
2538 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2539                                             non_integral_constant thing)
2540 {
2541   parser->non_integral_constant_expression_p = true;
2542   if (parser->integral_constant_expression_p)
2543     {
2544       if (!parser->allow_non_integral_constant_expression_p)
2545         {
2546           const char *msg = NULL;
2547           switch (thing)
2548             {
2549               case NIC_FLOAT:
2550                 error ("floating-point literal "
2551                        "cannot appear in a constant-expression");
2552                 return true;
2553               case NIC_CAST:
2554                 error ("a cast to a type other than an integral or "
2555                        "enumeration type cannot appear in a "
2556                        "constant-expression");
2557                 return true;
2558               case NIC_TYPEID:
2559                 error ("%<typeid%> operator "
2560                        "cannot appear in a constant-expression");
2561                 return true;
2562               case NIC_NCC:
2563                 error ("non-constant compound literals "
2564                        "cannot appear in a constant-expression");
2565                 return true;
2566               case NIC_FUNC_CALL:
2567                 error ("a function call "
2568                        "cannot appear in a constant-expression");
2569                 return true;
2570               case NIC_INC:
2571                 error ("an increment "
2572                        "cannot appear in a constant-expression");
2573                 return true;
2574               case NIC_DEC:
2575                 error ("an decrement "
2576                        "cannot appear in a constant-expression");
2577                 return true;
2578               case NIC_ARRAY_REF:
2579                 error ("an array reference "
2580                        "cannot appear in a constant-expression");
2581                 return true;
2582               case NIC_ADDR_LABEL:
2583                 error ("the address of a label "
2584                        "cannot appear in a constant-expression");
2585                 return true;
2586               case NIC_OVERLOADED:
2587                 error ("calls to overloaded operators "
2588                        "cannot appear in a constant-expression");
2589                 return true;
2590               case NIC_ASSIGNMENT:
2591                 error ("an assignment cannot appear in a constant-expression");
2592                 return true;
2593               case NIC_COMMA:
2594                 error ("a comma operator "
2595                        "cannot appear in a constant-expression");
2596                 return true;
2597               case NIC_CONSTRUCTOR:
2598                 error ("a call to a constructor "
2599                        "cannot appear in a constant-expression");
2600                 return true;
2601               case NIC_THIS:
2602                 msg = "this";
2603                 break;
2604               case NIC_FUNC_NAME:
2605                 msg = "__FUNCTION__";
2606                 break;
2607               case NIC_PRETTY_FUNC:
2608                 msg = "__PRETTY_FUNCTION__";
2609                 break;
2610               case NIC_C99_FUNC:
2611                 msg = "__func__";
2612                 break;
2613               case NIC_VA_ARG:
2614                 msg = "va_arg";
2615                 break;
2616               case NIC_ARROW:
2617                 msg = "->";
2618                 break;
2619               case NIC_POINT:
2620                 msg = ".";
2621                 break;
2622               case NIC_STAR:
2623                 msg = "*";
2624                 break;
2625               case NIC_ADDR:
2626                 msg = "&";
2627                 break;
2628               case NIC_PREINCREMENT:
2629                 msg = "++";
2630                 break;
2631               case NIC_PREDECREMENT:
2632                 msg = "--";
2633                 break;
2634               case NIC_NEW:
2635                 msg = "new";
2636                 break;
2637               case NIC_DEL:
2638                 msg = "delete";
2639                 break;
2640               default:
2641                 gcc_unreachable ();
2642             }
2643           if (msg)
2644             error ("%qs cannot appear in a constant-expression", msg);
2645           return true;
2646         }
2647     }
2648   return false;
2649 }
2650
2651 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2652    qualifying scope (or NULL, if none) for ID.  This function commits
2653    to the current active tentative parse, if any.  (Otherwise, the
2654    problematic construct might be encountered again later, resulting
2655    in duplicate error messages.) LOCATION is the location of ID.  */
2656
2657 static void
2658 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2659                                       tree scope, tree id,
2660                                       location_t location)
2661 {
2662   tree decl, old_scope;
2663   /* Try to lookup the identifier.  */
2664   old_scope = parser->scope;
2665   parser->scope = scope;
2666   decl = cp_parser_lookup_name_simple (parser, id, location);
2667   parser->scope = old_scope;
2668   /* If the lookup found a template-name, it means that the user forgot
2669   to specify an argument list. Emit a useful error message.  */
2670   if (TREE_CODE (decl) == TEMPLATE_DECL)
2671     error_at (location,
2672               "invalid use of template-name %qE without an argument list",
2673               decl);
2674   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2675     error_at (location, "invalid use of destructor %qD as a type", id);
2676   else if (TREE_CODE (decl) == TYPE_DECL)
2677     /* Something like 'unsigned A a;'  */
2678     error_at (location, "invalid combination of multiple type-specifiers");
2679   else if (!parser->scope)
2680     {
2681       /* Issue an error message.  */
2682       error_at (location, "%qE does not name a type", id);
2683       /* If we're in a template class, it's possible that the user was
2684          referring to a type from a base class.  For example:
2685
2686            template <typename T> struct A { typedef T X; };
2687            template <typename T> struct B : public A<T> { X x; };
2688
2689          The user should have said "typename A<T>::X".  */
2690       if (processing_template_decl && current_class_type
2691           && TYPE_BINFO (current_class_type))
2692         {
2693           tree b;
2694
2695           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2696                b;
2697                b = TREE_CHAIN (b))
2698             {
2699               tree base_type = BINFO_TYPE (b);
2700               if (CLASS_TYPE_P (base_type)
2701                   && dependent_type_p (base_type))
2702                 {
2703                   tree field;
2704                   /* Go from a particular instantiation of the
2705                      template (which will have an empty TYPE_FIELDs),
2706                      to the main version.  */
2707                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2708                   for (field = TYPE_FIELDS (base_type);
2709                        field;
2710                        field = DECL_CHAIN (field))
2711                     if (TREE_CODE (field) == TYPE_DECL
2712                         && DECL_NAME (field) == id)
2713                       {
2714                         inform (location, 
2715                                 "(perhaps %<typename %T::%E%> was intended)",
2716                                 BINFO_TYPE (b), id);
2717                         break;
2718                       }
2719                   if (field)
2720                     break;
2721                 }
2722             }
2723         }
2724     }
2725   /* Here we diagnose qualified-ids where the scope is actually correct,
2726      but the identifier does not resolve to a valid type name.  */
2727   else if (parser->scope != error_mark_node)
2728     {
2729       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2730         error_at (location, "%qE in namespace %qE does not name a type",
2731                   id, parser->scope);
2732       else if (CLASS_TYPE_P (parser->scope)
2733                && constructor_name_p (id, parser->scope))
2734         {
2735           /* A<T>::A<T>() */
2736           error_at (location, "%<%T::%E%> names the constructor, not"
2737                     " the type", parser->scope, id);
2738           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2739             error_at (location, "and %qT has no template constructors",
2740                       parser->scope);
2741         }
2742       else if (TYPE_P (parser->scope)
2743                && dependent_scope_p (parser->scope))
2744         error_at (location, "need %<typename%> before %<%T::%E%> because "
2745                   "%qT is a dependent scope",
2746                   parser->scope, id, parser->scope);
2747       else if (TYPE_P (parser->scope))
2748         error_at (location, "%qE in class %qT does not name a type",
2749                   id, parser->scope);
2750       else
2751         gcc_unreachable ();
2752     }
2753   cp_parser_commit_to_tentative_parse (parser);
2754 }
2755
2756 /* Check for a common situation where a type-name should be present,
2757    but is not, and issue a sensible error message.  Returns true if an
2758    invalid type-name was detected.
2759
2760    The situation handled by this function are variable declarations of the
2761    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2762    Usually, `ID' should name a type, but if we got here it means that it
2763    does not. We try to emit the best possible error message depending on
2764    how exactly the id-expression looks like.  */
2765
2766 static bool
2767 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2768 {
2769   tree id;
2770   cp_token *token = cp_lexer_peek_token (parser->lexer);
2771
2772   /* Avoid duplicate error about ambiguous lookup.  */
2773   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2774     {
2775       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2776       if (next->type == CPP_NAME && next->ambiguous_p)
2777         goto out;
2778     }
2779
2780   cp_parser_parse_tentatively (parser);
2781   id = cp_parser_id_expression (parser,
2782                                 /*template_keyword_p=*/false,
2783                                 /*check_dependency_p=*/true,
2784                                 /*template_p=*/NULL,
2785                                 /*declarator_p=*/true,
2786                                 /*optional_p=*/false);
2787   /* If the next token is a (, this is a function with no explicit return
2788      type, i.e. constructor, destructor or conversion op.  */
2789   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2790       || TREE_CODE (id) == TYPE_DECL)
2791     {
2792       cp_parser_abort_tentative_parse (parser);
2793       return false;
2794     }
2795   if (!cp_parser_parse_definitely (parser))
2796     return false;
2797
2798   /* Emit a diagnostic for the invalid type.  */
2799   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2800                                         id, token->location);
2801  out:
2802   /* If we aren't in the middle of a declarator (i.e. in a
2803      parameter-declaration-clause), skip to the end of the declaration;
2804      there's no point in trying to process it.  */
2805   if (!parser->in_declarator_p)
2806     cp_parser_skip_to_end_of_block_or_statement (parser);
2807   return true;
2808 }
2809
2810 /* Consume tokens up to, and including, the next non-nested closing `)'.
2811    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2812    are doing error recovery. Returns -1 if OR_COMMA is true and we
2813    found an unnested comma.  */
2814
2815 static int
2816 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2817                                        bool recovering,
2818                                        bool or_comma,
2819                                        bool consume_paren)
2820 {
2821   unsigned paren_depth = 0;
2822   unsigned brace_depth = 0;
2823   unsigned square_depth = 0;
2824
2825   if (recovering && !or_comma
2826       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2827     return 0;
2828
2829   while (true)
2830     {
2831       cp_token * token = cp_lexer_peek_token (parser->lexer);
2832
2833       switch (token->type)
2834         {
2835         case CPP_EOF:
2836         case CPP_PRAGMA_EOL:
2837           /* If we've run out of tokens, then there is no closing `)'.  */
2838           return 0;
2839
2840         /* This is good for lambda expression capture-lists.  */
2841         case CPP_OPEN_SQUARE:
2842           ++square_depth;
2843           break;
2844         case CPP_CLOSE_SQUARE:
2845           if (!square_depth--)
2846             return 0;
2847           break;
2848
2849         case CPP_SEMICOLON:
2850           /* This matches the processing in skip_to_end_of_statement.  */
2851           if (!brace_depth)
2852             return 0;
2853           break;
2854
2855         case CPP_OPEN_BRACE:
2856           ++brace_depth;
2857           break;
2858         case CPP_CLOSE_BRACE:
2859           if (!brace_depth--)
2860             return 0;
2861           break;
2862
2863         case CPP_COMMA:
2864           if (recovering && or_comma && !brace_depth && !paren_depth
2865               && !square_depth)
2866             return -1;
2867           break;
2868
2869         case CPP_OPEN_PAREN:
2870           if (!brace_depth)
2871             ++paren_depth;
2872           break;
2873
2874         case CPP_CLOSE_PAREN:
2875           if (!brace_depth && !paren_depth--)
2876             {
2877               if (consume_paren)
2878                 cp_lexer_consume_token (parser->lexer);
2879               return 1;
2880             }
2881           break;
2882
2883         default:
2884           break;
2885         }
2886
2887       /* Consume the token.  */
2888       cp_lexer_consume_token (parser->lexer);
2889     }
2890 }
2891
2892 /* Consume tokens until we reach the end of the current statement.
2893    Normally, that will be just before consuming a `;'.  However, if a
2894    non-nested `}' comes first, then we stop before consuming that.  */
2895
2896 static void
2897 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2898 {
2899   unsigned nesting_depth = 0;
2900
2901   while (true)
2902     {
2903       cp_token *token = cp_lexer_peek_token (parser->lexer);
2904
2905       switch (token->type)
2906         {
2907         case CPP_EOF:
2908         case CPP_PRAGMA_EOL:
2909           /* If we've run out of tokens, stop.  */
2910           return;
2911
2912         case CPP_SEMICOLON:
2913           /* If the next token is a `;', we have reached the end of the
2914              statement.  */
2915           if (!nesting_depth)
2916             return;
2917           break;
2918
2919         case CPP_CLOSE_BRACE:
2920           /* If this is a non-nested '}', stop before consuming it.
2921              That way, when confronted with something like:
2922
2923                { 3 + }
2924
2925              we stop before consuming the closing '}', even though we
2926              have not yet reached a `;'.  */
2927           if (nesting_depth == 0)
2928             return;
2929
2930           /* If it is the closing '}' for a block that we have
2931              scanned, stop -- but only after consuming the token.
2932              That way given:
2933
2934                 void f g () { ... }
2935                 typedef int I;
2936
2937              we will stop after the body of the erroneously declared
2938              function, but before consuming the following `typedef'
2939              declaration.  */
2940           if (--nesting_depth == 0)
2941             {
2942               cp_lexer_consume_token (parser->lexer);
2943               return;
2944             }
2945
2946         case CPP_OPEN_BRACE:
2947           ++nesting_depth;
2948           break;
2949
2950         default:
2951           break;
2952         }
2953
2954       /* Consume the token.  */
2955       cp_lexer_consume_token (parser->lexer);
2956     }
2957 }
2958
2959 /* This function is called at the end of a statement or declaration.
2960    If the next token is a semicolon, it is consumed; otherwise, error
2961    recovery is attempted.  */
2962
2963 static void
2964 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2965 {
2966   /* Look for the trailing `;'.  */
2967   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2968     {
2969       /* If there is additional (erroneous) input, skip to the end of
2970          the statement.  */
2971       cp_parser_skip_to_end_of_statement (parser);
2972       /* If the next token is now a `;', consume it.  */
2973       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2974         cp_lexer_consume_token (parser->lexer);
2975     }
2976 }
2977
2978 /* Skip tokens until we have consumed an entire block, or until we
2979    have consumed a non-nested `;'.  */
2980
2981 static void
2982 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2983 {
2984   int nesting_depth = 0;
2985
2986   while (nesting_depth >= 0)
2987     {
2988       cp_token *token = cp_lexer_peek_token (parser->lexer);
2989
2990       switch (token->type)
2991         {
2992         case CPP_EOF:
2993         case CPP_PRAGMA_EOL:
2994           /* If we've run out of tokens, stop.  */
2995           return;
2996
2997         case CPP_SEMICOLON:
2998           /* Stop if this is an unnested ';'. */
2999           if (!nesting_depth)
3000             nesting_depth = -1;
3001           break;
3002
3003         case CPP_CLOSE_BRACE:
3004           /* Stop if this is an unnested '}', or closes the outermost
3005              nesting level.  */
3006           nesting_depth--;
3007           if (nesting_depth < 0)
3008             return;
3009           if (!nesting_depth)
3010             nesting_depth = -1;
3011           break;
3012
3013         case CPP_OPEN_BRACE:
3014           /* Nest. */
3015           nesting_depth++;
3016           break;
3017
3018         default:
3019           break;
3020         }
3021
3022       /* Consume the token.  */
3023       cp_lexer_consume_token (parser->lexer);
3024     }
3025 }
3026
3027 /* Skip tokens until a non-nested closing curly brace is the next
3028    token, or there are no more tokens. Return true in the first case,
3029    false otherwise.  */
3030
3031 static bool
3032 cp_parser_skip_to_closing_brace (cp_parser *parser)
3033 {
3034   unsigned nesting_depth = 0;
3035
3036   while (true)
3037     {
3038       cp_token *token = cp_lexer_peek_token (parser->lexer);
3039
3040       switch (token->type)
3041         {
3042         case CPP_EOF:
3043         case CPP_PRAGMA_EOL:
3044           /* If we've run out of tokens, stop.  */
3045           return false;
3046
3047         case CPP_CLOSE_BRACE:
3048           /* If the next token is a non-nested `}', then we have reached
3049              the end of the current block.  */
3050           if (nesting_depth-- == 0)
3051             return true;
3052           break;
3053
3054         case CPP_OPEN_BRACE:
3055           /* If it the next token is a `{', then we are entering a new
3056              block.  Consume the entire block.  */
3057           ++nesting_depth;
3058           break;
3059
3060         default:
3061           break;
3062         }
3063
3064       /* Consume the token.  */
3065       cp_lexer_consume_token (parser->lexer);
3066     }
3067 }
3068
3069 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3070    parameter is the PRAGMA token, allowing us to purge the entire pragma
3071    sequence.  */
3072
3073 static void
3074 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3075 {
3076   cp_token *token;
3077
3078   parser->lexer->in_pragma = false;
3079
3080   do
3081     token = cp_lexer_consume_token (parser->lexer);
3082   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3083
3084   /* Ensure that the pragma is not parsed again.  */
3085   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3086 }
3087
3088 /* Require pragma end of line, resyncing with it as necessary.  The
3089    arguments are as for cp_parser_skip_to_pragma_eol.  */
3090
3091 static void
3092 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3093 {
3094   parser->lexer->in_pragma = false;
3095   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3096     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3097 }
3098
3099 /* This is a simple wrapper around make_typename_type. When the id is
3100    an unresolved identifier node, we can provide a superior diagnostic
3101    using cp_parser_diagnose_invalid_type_name.  */
3102
3103 static tree
3104 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3105                               tree id, location_t id_location)
3106 {
3107   tree result;
3108   if (TREE_CODE (id) == IDENTIFIER_NODE)
3109     {
3110       result = make_typename_type (scope, id, typename_type,
3111                                    /*complain=*/tf_none);
3112       if (result == error_mark_node)
3113         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3114       return result;
3115     }
3116   return make_typename_type (scope, id, typename_type, tf_error);
3117 }
3118
3119 /* This is a wrapper around the
3120    make_{pointer,ptrmem,reference}_declarator functions that decides
3121    which one to call based on the CODE and CLASS_TYPE arguments. The
3122    CODE argument should be one of the values returned by
3123    cp_parser_ptr_operator. */
3124 static cp_declarator *
3125 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3126                                     cp_cv_quals cv_qualifiers,
3127                                     cp_declarator *target)
3128 {
3129   if (code == ERROR_MARK)
3130     return cp_error_declarator;
3131
3132   if (code == INDIRECT_REF)
3133     if (class_type == NULL_TREE)
3134       return make_pointer_declarator (cv_qualifiers, target);
3135     else
3136       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3137   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3138     return make_reference_declarator (cv_qualifiers, target, false);
3139   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3140     return make_reference_declarator (cv_qualifiers, target, true);
3141   gcc_unreachable ();
3142 }
3143
3144 /* Create a new C++ parser.  */
3145
3146 static cp_parser *
3147 cp_parser_new (void)
3148 {
3149   cp_parser *parser;
3150   cp_lexer *lexer;
3151   unsigned i;
3152
3153   /* cp_lexer_new_main is called before doing GC allocation because
3154      cp_lexer_new_main might load a PCH file.  */
3155   lexer = cp_lexer_new_main ();
3156
3157   /* Initialize the binops_by_token so that we can get the tree
3158      directly from the token.  */
3159   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3160     binops_by_token[binops[i].token_type] = binops[i];
3161
3162   parser = ggc_alloc_cleared_cp_parser ();
3163   parser->lexer = lexer;
3164   parser->context = cp_parser_context_new (NULL);
3165
3166   /* For now, we always accept GNU extensions.  */
3167   parser->allow_gnu_extensions_p = 1;
3168
3169   /* The `>' token is a greater-than operator, not the end of a
3170      template-id.  */
3171   parser->greater_than_is_operator_p = true;
3172
3173   parser->default_arg_ok_p = true;
3174
3175   /* We are not parsing a constant-expression.  */
3176   parser->integral_constant_expression_p = false;
3177   parser->allow_non_integral_constant_expression_p = false;
3178   parser->non_integral_constant_expression_p = false;
3179
3180   /* Local variable names are not forbidden.  */
3181   parser->local_variables_forbidden_p = false;
3182
3183   /* We are not processing an `extern "C"' declaration.  */
3184   parser->in_unbraced_linkage_specification_p = false;
3185
3186   /* We are not processing a declarator.  */
3187   parser->in_declarator_p = false;
3188
3189   /* We are not processing a template-argument-list.  */
3190   parser->in_template_argument_list_p = false;
3191
3192   /* We are not in an iteration statement.  */
3193   parser->in_statement = 0;
3194
3195   /* We are not in a switch statement.  */
3196   parser->in_switch_statement_p = false;
3197
3198   /* We are not parsing a type-id inside an expression.  */
3199   parser->in_type_id_in_expr_p = false;
3200
3201   /* Declarations aren't implicitly extern "C".  */
3202   parser->implicit_extern_c = false;
3203
3204   /* String literals should be translated to the execution character set.  */
3205   parser->translate_strings_p = true;
3206
3207   /* We are not parsing a function body.  */
3208   parser->in_function_body = false;
3209
3210   /* The unparsed function queue is empty.  */
3211   push_unparsed_function_queues (parser);
3212
3213   /* There are no classes being defined.  */
3214   parser->num_classes_being_defined = 0;
3215
3216   /* No template parameters apply.  */
3217   parser->num_template_parameter_lists = 0;
3218
3219   return parser;
3220 }
3221
3222 /* Create a cp_lexer structure which will emit the tokens in CACHE
3223    and push it onto the parser's lexer stack.  This is used for delayed
3224    parsing of in-class method bodies and default arguments, and should
3225    not be confused with tentative parsing.  */
3226 static void
3227 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3228 {
3229   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3230   lexer->next = parser->lexer;
3231   parser->lexer = lexer;
3232
3233   /* Move the current source position to that of the first token in the
3234      new lexer.  */
3235   cp_lexer_set_source_position_from_token (lexer->next_token);
3236 }
3237
3238 /* Pop the top lexer off the parser stack.  This is never used for the
3239    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3240 static void
3241 cp_parser_pop_lexer (cp_parser *parser)
3242 {
3243   cp_lexer *lexer = parser->lexer;
3244   parser->lexer = lexer->next;
3245   cp_lexer_destroy (lexer);
3246
3247   /* Put the current source position back where it was before this
3248      lexer was pushed.  */
3249   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3250 }
3251
3252 /* Lexical conventions [gram.lex]  */
3253
3254 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3255    identifier.  */
3256
3257 static tree
3258 cp_parser_identifier (cp_parser* parser)
3259 {
3260   cp_token *token;
3261
3262   /* Look for the identifier.  */
3263   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3264   /* Return the value.  */
3265   return token ? token->u.value : error_mark_node;
3266 }
3267
3268 /* Parse a sequence of adjacent string constants.  Returns a
3269    TREE_STRING representing the combined, nul-terminated string
3270    constant.  If TRANSLATE is true, translate the string to the
3271    execution character set.  If WIDE_OK is true, a wide string is
3272    invalid here.
3273
3274    C++98 [lex.string] says that if a narrow string literal token is
3275    adjacent to a wide string literal token, the behavior is undefined.
3276    However, C99 6.4.5p4 says that this results in a wide string literal.
3277    We follow C99 here, for consistency with the C front end.
3278
3279    This code is largely lifted from lex_string() in c-lex.c.
3280
3281    FUTURE: ObjC++ will need to handle @-strings here.  */
3282 static tree
3283 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3284 {
3285   tree value;
3286   size_t count;
3287   struct obstack str_ob;
3288   cpp_string str, istr, *strs;
3289   cp_token *tok;
3290   enum cpp_ttype type;
3291
3292   tok = cp_lexer_peek_token (parser->lexer);
3293   if (!cp_parser_is_string_literal (tok))
3294     {
3295       cp_parser_error (parser, "expected string-literal");
3296       return error_mark_node;
3297     }
3298
3299   type = tok->type;
3300
3301   /* Try to avoid the overhead of creating and destroying an obstack
3302      for the common case of just one string.  */
3303   if (!cp_parser_is_string_literal
3304       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3305     {
3306       cp_lexer_consume_token (parser->lexer);
3307
3308       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3309       str.len = TREE_STRING_LENGTH (tok->u.value);
3310       count = 1;
3311
3312       strs = &str;
3313     }
3314   else
3315     {
3316       gcc_obstack_init (&str_ob);
3317       count = 0;
3318
3319       do
3320         {
3321           cp_lexer_consume_token (parser->lexer);
3322           count++;
3323           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3324           str.len = TREE_STRING_LENGTH (tok->u.value);
3325
3326           if (type != tok->type)
3327             {
3328               if (type == CPP_STRING)
3329                 type = tok->type;
3330               else if (tok->type != CPP_STRING)
3331                 error_at (tok->location,
3332                           "unsupported non-standard concatenation "
3333                           "of string literals");
3334             }
3335
3336           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3337
3338           tok = cp_lexer_peek_token (parser->lexer);
3339         }
3340       while (cp_parser_is_string_literal (tok));
3341
3342       strs = (cpp_string *) obstack_finish (&str_ob);
3343     }
3344
3345   if (type != CPP_STRING && !wide_ok)
3346     {
3347       cp_parser_error (parser, "a wide string is invalid in this context");
3348       type = CPP_STRING;
3349     }
3350
3351   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3352       (parse_in, strs, count, &istr, type))
3353     {
3354       value = build_string (istr.len, (const char *)istr.text);
3355       free (CONST_CAST (unsigned char *, istr.text));
3356
3357       switch (type)
3358         {
3359         default:
3360         case CPP_STRING:
3361         case CPP_UTF8STRING:
3362           TREE_TYPE (value) = char_array_type_node;
3363           break;
3364         case CPP_STRING16:
3365           TREE_TYPE (value) = char16_array_type_node;
3366           break;
3367         case CPP_STRING32:
3368           TREE_TYPE (value) = char32_array_type_node;
3369           break;
3370         case CPP_WSTRING:
3371           TREE_TYPE (value) = wchar_array_type_node;
3372           break;
3373         }
3374
3375       value = fix_string_type (value);
3376     }
3377   else
3378     /* cpp_interpret_string has issued an error.  */
3379     value = error_mark_node;
3380
3381   if (count > 1)
3382     obstack_free (&str_ob, 0);
3383
3384   return value;
3385 }
3386
3387
3388 /* Basic concepts [gram.basic]  */
3389
3390 /* Parse a translation-unit.
3391
3392    translation-unit:
3393      declaration-seq [opt]
3394
3395    Returns TRUE if all went well.  */
3396
3397 static bool
3398 cp_parser_translation_unit (cp_parser* parser)
3399 {
3400   /* The address of the first non-permanent object on the declarator
3401      obstack.  */
3402   static void *declarator_obstack_base;
3403
3404   bool success;
3405
3406   /* Create the declarator obstack, if necessary.  */
3407   if (!cp_error_declarator)
3408     {
3409       gcc_obstack_init (&declarator_obstack);
3410       /* Create the error declarator.  */
3411       cp_error_declarator = make_declarator (cdk_error);
3412       /* Create the empty parameter list.  */
3413       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3414       /* Remember where the base of the declarator obstack lies.  */
3415       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3416     }
3417
3418   cp_parser_declaration_seq_opt (parser);
3419
3420   /* If there are no tokens left then all went well.  */
3421   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3422     {
3423       /* Get rid of the token array; we don't need it any more.  */
3424       cp_lexer_destroy (parser->lexer);
3425       parser->lexer = NULL;
3426
3427       /* This file might have been a context that's implicitly extern
3428          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3429       if (parser->implicit_extern_c)
3430         {
3431           pop_lang_context ();
3432           parser->implicit_extern_c = false;
3433         }
3434
3435       /* Finish up.  */
3436       finish_translation_unit ();
3437
3438       success = true;
3439     }
3440   else
3441     {
3442       cp_parser_error (parser, "expected declaration");
3443       success = false;
3444     }
3445
3446   /* Make sure the declarator obstack was fully cleaned up.  */
3447   gcc_assert (obstack_next_free (&declarator_obstack)
3448               == declarator_obstack_base);
3449
3450   /* All went well.  */
3451   return success;
3452 }
3453
3454 /* Expressions [gram.expr] */
3455
3456 /* Parse a primary-expression.
3457
3458    primary-expression:
3459      literal
3460      this
3461      ( expression )
3462      id-expression
3463
3464    GNU Extensions:
3465
3466    primary-expression:
3467      ( compound-statement )
3468      __builtin_va_arg ( assignment-expression , type-id )
3469      __builtin_offsetof ( type-id , offsetof-expression )
3470
3471    C++ Extensions:
3472      __has_nothrow_assign ( type-id )   
3473      __has_nothrow_constructor ( type-id )
3474      __has_nothrow_copy ( type-id )
3475      __has_trivial_assign ( type-id )   
3476      __has_trivial_constructor ( type-id )
3477      __has_trivial_copy ( type-id )
3478      __has_trivial_destructor ( type-id )
3479      __has_virtual_destructor ( type-id )     
3480      __is_abstract ( type-id )
3481      __is_base_of ( type-id , type-id )
3482      __is_class ( type-id )
3483      __is_convertible_to ( type-id , type-id )     
3484      __is_empty ( type-id )
3485      __is_enum ( type-id )
3486      __is_pod ( type-id )
3487      __is_polymorphic ( type-id )
3488      __is_union ( type-id )
3489
3490    Objective-C++ Extension:
3491
3492    primary-expression:
3493      objc-expression
3494
3495    literal:
3496      __null
3497
3498    ADDRESS_P is true iff this expression was immediately preceded by
3499    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3500    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3501    true iff this expression is a template argument.
3502
3503    Returns a representation of the expression.  Upon return, *IDK
3504    indicates what kind of id-expression (if any) was present.  */
3505
3506 static tree
3507 cp_parser_primary_expression (cp_parser *parser,
3508                               bool address_p,
3509                               bool cast_p,
3510                               bool template_arg_p,
3511                               cp_id_kind *idk)
3512 {
3513   cp_token *token = NULL;
3514
3515   /* Assume the primary expression is not an id-expression.  */
3516   *idk = CP_ID_KIND_NONE;
3517
3518   /* Peek at the next token.  */
3519   token = cp_lexer_peek_token (parser->lexer);
3520   switch (token->type)
3521     {
3522       /* literal:
3523            integer-literal
3524            character-literal
3525            floating-literal
3526            string-literal
3527            boolean-literal  */
3528     case CPP_CHAR:
3529     case CPP_CHAR16:
3530     case CPP_CHAR32:
3531     case CPP_WCHAR:
3532     case CPP_NUMBER:
3533       token = cp_lexer_consume_token (parser->lexer);
3534       if (TREE_CODE (token->u.value) == FIXED_CST)
3535         {
3536           error_at (token->location,
3537                     "fixed-point types not supported in C++");
3538           return error_mark_node;
3539         }
3540       /* Floating-point literals are only allowed in an integral
3541          constant expression if they are cast to an integral or
3542          enumeration type.  */
3543       if (TREE_CODE (token->u.value) == REAL_CST
3544           && parser->integral_constant_expression_p
3545           && pedantic)
3546         {
3547           /* CAST_P will be set even in invalid code like "int(2.7 +
3548              ...)".   Therefore, we have to check that the next token
3549              is sure to end the cast.  */
3550           if (cast_p)
3551             {
3552               cp_token *next_token;
3553
3554               next_token = cp_lexer_peek_token (parser->lexer);
3555               if (/* The comma at the end of an
3556                      enumerator-definition.  */
3557                   next_token->type != CPP_COMMA
3558                   /* The curly brace at the end of an enum-specifier.  */
3559                   && next_token->type != CPP_CLOSE_BRACE
3560                   /* The end of a statement.  */
3561                   && next_token->type != CPP_SEMICOLON
3562                   /* The end of the cast-expression.  */
3563                   && next_token->type != CPP_CLOSE_PAREN
3564                   /* The end of an array bound.  */
3565                   && next_token->type != CPP_CLOSE_SQUARE
3566                   /* The closing ">" in a template-argument-list.  */
3567                   && (next_token->type != CPP_GREATER
3568                       || parser->greater_than_is_operator_p)
3569                   /* C++0x only: A ">>" treated like two ">" tokens,
3570                      in a template-argument-list.  */
3571                   && (next_token->type != CPP_RSHIFT
3572                       || (cxx_dialect == cxx98)
3573                       || parser->greater_than_is_operator_p))
3574                 cast_p = false;
3575             }
3576
3577           /* If we are within a cast, then the constraint that the
3578              cast is to an integral or enumeration type will be
3579              checked at that point.  If we are not within a cast, then
3580              this code is invalid.  */
3581           if (!cast_p)
3582             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3583         }
3584       return token->u.value;
3585
3586     case CPP_STRING:
3587     case CPP_STRING16:
3588     case CPP_STRING32:
3589     case CPP_WSTRING:
3590     case CPP_UTF8STRING:
3591       /* ??? Should wide strings be allowed when parser->translate_strings_p
3592          is false (i.e. in attributes)?  If not, we can kill the third
3593          argument to cp_parser_string_literal.  */
3594       return cp_parser_string_literal (parser,
3595                                        parser->translate_strings_p,
3596                                        true);
3597
3598     case CPP_OPEN_PAREN:
3599       {
3600         tree expr;
3601         bool saved_greater_than_is_operator_p;
3602
3603         /* Consume the `('.  */
3604         cp_lexer_consume_token (parser->lexer);
3605         /* Within a parenthesized expression, a `>' token is always
3606            the greater-than operator.  */
3607         saved_greater_than_is_operator_p
3608           = parser->greater_than_is_operator_p;
3609         parser->greater_than_is_operator_p = true;
3610         /* If we see `( { ' then we are looking at the beginning of
3611            a GNU statement-expression.  */
3612         if (cp_parser_allow_gnu_extensions_p (parser)
3613             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3614           {
3615             /* Statement-expressions are not allowed by the standard.  */
3616             pedwarn (token->location, OPT_pedantic, 
3617                      "ISO C++ forbids braced-groups within expressions");
3618
3619             /* And they're not allowed outside of a function-body; you
3620                cannot, for example, write:
3621
3622                  int i = ({ int j = 3; j + 1; });
3623
3624                at class or namespace scope.  */
3625             if (!parser->in_function_body
3626                 || parser->in_template_argument_list_p)
3627               {
3628                 error_at (token->location,
3629                           "statement-expressions are not allowed outside "
3630                           "functions nor in template-argument lists");
3631                 cp_parser_skip_to_end_of_block_or_statement (parser);
3632                 expr = error_mark_node;
3633               }
3634             else
3635               {
3636                 /* Start the statement-expression.  */
3637                 expr = begin_stmt_expr ();
3638                 /* Parse the compound-statement.  */
3639                 cp_parser_compound_statement (parser, expr, false);
3640                 /* Finish up.  */
3641                 expr = finish_stmt_expr (expr, false);
3642               }
3643           }
3644         else
3645           {
3646             /* Parse the parenthesized expression.  */
3647             expr = cp_parser_expression (parser, cast_p, idk);
3648             /* Let the front end know that this expression was
3649                enclosed in parentheses. This matters in case, for
3650                example, the expression is of the form `A::B', since
3651                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3652                not.  */
3653             finish_parenthesized_expr (expr);
3654           }
3655         /* The `>' token might be the end of a template-id or
3656            template-parameter-list now.  */
3657         parser->greater_than_is_operator_p
3658           = saved_greater_than_is_operator_p;
3659         /* Consume the `)'.  */
3660         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3661           cp_parser_skip_to_end_of_statement (parser);
3662
3663         return expr;
3664       }
3665
3666     case CPP_OPEN_SQUARE:
3667       if (c_dialect_objc ())
3668         /* We have an Objective-C++ message. */
3669         return cp_parser_objc_expression (parser);
3670       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3671       return cp_parser_lambda_expression (parser);
3672
3673     case CPP_OBJC_STRING:
3674       if (c_dialect_objc ())
3675         /* We have an Objective-C++ string literal. */
3676         return cp_parser_objc_expression (parser);
3677       cp_parser_error (parser, "expected primary-expression");
3678       return error_mark_node;
3679
3680     case CPP_KEYWORD:
3681       switch (token->keyword)
3682         {
3683           /* These two are the boolean literals.  */
3684         case RID_TRUE:
3685           cp_lexer_consume_token (parser->lexer);
3686           return boolean_true_node;
3687         case RID_FALSE:
3688           cp_lexer_consume_token (parser->lexer);
3689           return boolean_false_node;
3690
3691           /* The `__null' literal.  */
3692         case RID_NULL:
3693           cp_lexer_consume_token (parser->lexer);
3694           return null_node;
3695
3696           /* The `nullptr' literal.  */
3697         case RID_NULLPTR:
3698           cp_lexer_consume_token (parser->lexer);
3699           return nullptr_node;
3700
3701           /* Recognize the `this' keyword.  */
3702         case RID_THIS:
3703           cp_lexer_consume_token (parser->lexer);
3704           if (parser->local_variables_forbidden_p)
3705             {
3706               error_at (token->location,
3707                         "%<this%> may not be used in this context");
3708               return error_mark_node;
3709             }
3710           /* Pointers cannot appear in constant-expressions.  */
3711           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3712             return error_mark_node;
3713           return finish_this_expr ();
3714
3715           /* The `operator' keyword can be the beginning of an
3716              id-expression.  */
3717         case RID_OPERATOR:
3718           goto id_expression;
3719
3720         case RID_FUNCTION_NAME:
3721         case RID_PRETTY_FUNCTION_NAME:
3722         case RID_C99_FUNCTION_NAME:
3723           {
3724             non_integral_constant name;
3725
3726             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3727                __func__ are the names of variables -- but they are
3728                treated specially.  Therefore, they are handled here,
3729                rather than relying on the generic id-expression logic
3730                below.  Grammatically, these names are id-expressions.
3731
3732                Consume the token.  */
3733             token = cp_lexer_consume_token (parser->lexer);
3734
3735             switch (token->keyword)
3736               {
3737               case RID_FUNCTION_NAME:
3738                 name = NIC_FUNC_NAME;
3739                 break;
3740               case RID_PRETTY_FUNCTION_NAME:
3741                 name = NIC_PRETTY_FUNC;
3742                 break;
3743               case RID_C99_FUNCTION_NAME:
3744                 name = NIC_C99_FUNC;
3745                 break;
3746               default:
3747                 gcc_unreachable ();
3748               }
3749
3750             if (cp_parser_non_integral_constant_expression (parser, name))
3751               return error_mark_node;
3752
3753             /* Look up the name.  */
3754             return finish_fname (token->u.value);
3755           }
3756
3757         case RID_VA_ARG:
3758           {
3759             tree expression;
3760             tree type;
3761
3762             /* The `__builtin_va_arg' construct is used to handle
3763                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3764             cp_lexer_consume_token (parser->lexer);
3765             /* Look for the opening `('.  */
3766             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3767             /* Now, parse the assignment-expression.  */
3768             expression = cp_parser_assignment_expression (parser,
3769                                                           /*cast_p=*/false, NULL);
3770             /* Look for the `,'.  */
3771             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3772             /* Parse the type-id.  */
3773             type = cp_parser_type_id (parser);
3774             /* Look for the closing `)'.  */
3775             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3776             /* Using `va_arg' in a constant-expression is not
3777                allowed.  */
3778             if (cp_parser_non_integral_constant_expression (parser,
3779                                                             NIC_VA_ARG))
3780               return error_mark_node;
3781             return build_x_va_arg (expression, type);
3782           }
3783
3784         case RID_OFFSETOF:
3785           return cp_parser_builtin_offsetof (parser);
3786
3787         case RID_HAS_NOTHROW_ASSIGN:
3788         case RID_HAS_NOTHROW_CONSTRUCTOR:
3789         case RID_HAS_NOTHROW_COPY:        
3790         case RID_HAS_TRIVIAL_ASSIGN:
3791         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3792         case RID_HAS_TRIVIAL_COPY:        
3793         case RID_HAS_TRIVIAL_DESTRUCTOR:
3794         case RID_HAS_VIRTUAL_DESTRUCTOR:
3795         case RID_IS_ABSTRACT:
3796         case RID_IS_BASE_OF:
3797         case RID_IS_CLASS:
3798         case RID_IS_CONVERTIBLE_TO:
3799         case RID_IS_EMPTY:
3800         case RID_IS_ENUM:
3801         case RID_IS_POD:
3802         case RID_IS_POLYMORPHIC:
3803         case RID_IS_STD_LAYOUT:
3804         case RID_IS_TRIVIAL:
3805         case RID_IS_UNION:
3806           return cp_parser_trait_expr (parser, token->keyword);
3807
3808         /* Objective-C++ expressions.  */
3809         case RID_AT_ENCODE:
3810         case RID_AT_PROTOCOL:
3811         case RID_AT_SELECTOR:
3812           return cp_parser_objc_expression (parser);
3813
3814         case RID_TEMPLATE:
3815           if (parser->in_function_body
3816               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3817                   == CPP_LESS))
3818             {
3819               error_at (token->location,
3820                         "a template declaration cannot appear at block scope");
3821               cp_parser_skip_to_end_of_block_or_statement (parser);
3822               return error_mark_node;
3823             }
3824         default:
3825           cp_parser_error (parser, "expected primary-expression");
3826           return error_mark_node;
3827         }
3828
3829       /* An id-expression can start with either an identifier, a
3830          `::' as the beginning of a qualified-id, or the "operator"
3831          keyword.  */
3832     case CPP_NAME:
3833     case CPP_SCOPE:
3834     case CPP_TEMPLATE_ID:
3835     case CPP_NESTED_NAME_SPECIFIER:
3836       {
3837         tree id_expression;
3838         tree decl;
3839         const char *error_msg;
3840         bool template_p;
3841         bool done;
3842         cp_token *id_expr_token;
3843
3844       id_expression:
3845         /* Parse the id-expression.  */
3846         id_expression
3847           = cp_parser_id_expression (parser,
3848                                      /*template_keyword_p=*/false,
3849                                      /*check_dependency_p=*/true,
3850                                      &template_p,
3851                                      /*declarator_p=*/false,
3852                                      /*optional_p=*/false);
3853         if (id_expression == error_mark_node)
3854           return error_mark_node;
3855         id_expr_token = token;
3856         token = cp_lexer_peek_token (parser->lexer);
3857         done = (token->type != CPP_OPEN_SQUARE
3858                 && token->type != CPP_OPEN_PAREN
3859                 && token->type != CPP_DOT
3860                 && token->type != CPP_DEREF
3861                 && token->type != CPP_PLUS_PLUS
3862                 && token->type != CPP_MINUS_MINUS);
3863         /* If we have a template-id, then no further lookup is
3864            required.  If the template-id was for a template-class, we
3865            will sometimes have a TYPE_DECL at this point.  */
3866         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3867                  || TREE_CODE (id_expression) == TYPE_DECL)
3868           decl = id_expression;
3869         /* Look up the name.  */
3870         else
3871           {
3872             tree ambiguous_decls;
3873
3874             /* If we already know that this lookup is ambiguous, then
3875                we've already issued an error message; there's no reason
3876                to check again.  */
3877             if (id_expr_token->type == CPP_NAME
3878                 && id_expr_token->ambiguous_p)
3879               {
3880                 cp_parser_simulate_error (parser);
3881                 return error_mark_node;
3882               }
3883
3884             decl = cp_parser_lookup_name (parser, id_expression,
3885                                           none_type,
3886                                           template_p,
3887                                           /*is_namespace=*/false,
3888                                           /*check_dependency=*/true,
3889                                           &ambiguous_decls,
3890                                           id_expr_token->location);
3891             /* If the lookup was ambiguous, an error will already have
3892                been issued.  */
3893             if (ambiguous_decls)
3894               return error_mark_node;
3895
3896             /* In Objective-C++, an instance variable (ivar) may be preferred
3897                to whatever cp_parser_lookup_name() found.  */
3898             decl = objc_lookup_ivar (decl, id_expression);
3899
3900             /* If name lookup gives us a SCOPE_REF, then the
3901                qualifying scope was dependent.  */
3902             if (TREE_CODE (decl) == SCOPE_REF)
3903               {
3904                 /* At this point, we do not know if DECL is a valid
3905                    integral constant expression.  We assume that it is
3906                    in fact such an expression, so that code like:
3907
3908                       template <int N> struct A {
3909                         int a[B<N>::i];
3910                       };
3911                      
3912                    is accepted.  At template-instantiation time, we
3913                    will check that B<N>::i is actually a constant.  */
3914                 return decl;
3915               }
3916             /* Check to see if DECL is a local variable in a context
3917                where that is forbidden.  */
3918             if (parser->local_variables_forbidden_p
3919                 && local_variable_p (decl))
3920               {
3921                 /* It might be that we only found DECL because we are
3922                    trying to be generous with pre-ISO scoping rules.
3923                    For example, consider:
3924
3925                      int i;
3926                      void g() {
3927                        for (int i = 0; i < 10; ++i) {}
3928                        extern void f(int j = i);
3929                      }
3930
3931                    Here, name look up will originally find the out
3932                    of scope `i'.  We need to issue a warning message,
3933                    but then use the global `i'.  */
3934                 decl = check_for_out_of_scope_variable (decl);
3935                 if (local_variable_p (decl))
3936                   {
3937                     error_at (id_expr_token->location,
3938                               "local variable %qD may not appear in this context",
3939                               decl);
3940                     return error_mark_node;
3941                   }
3942               }
3943           }
3944
3945         decl = (finish_id_expression
3946                 (id_expression, decl, parser->scope,
3947                  idk,
3948                  parser->integral_constant_expression_p,
3949                  parser->allow_non_integral_constant_expression_p,
3950                  &parser->non_integral_constant_expression_p,
3951                  template_p, done, address_p,
3952                  template_arg_p,
3953                  &error_msg,
3954                  id_expr_token->location));
3955         if (error_msg)
3956           cp_parser_error (parser, error_msg);
3957         return decl;
3958       }
3959
3960       /* Anything else is an error.  */
3961     default:
3962       cp_parser_error (parser, "expected primary-expression");
3963       return error_mark_node;
3964     }
3965 }
3966
3967 /* Parse an id-expression.
3968
3969    id-expression:
3970      unqualified-id
3971      qualified-id
3972
3973    qualified-id:
3974      :: [opt] nested-name-specifier template [opt] unqualified-id
3975      :: identifier
3976      :: operator-function-id
3977      :: template-id
3978
3979    Return a representation of the unqualified portion of the
3980    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3981    a `::' or nested-name-specifier.
3982
3983    Often, if the id-expression was a qualified-id, the caller will
3984    want to make a SCOPE_REF to represent the qualified-id.  This
3985    function does not do this in order to avoid wastefully creating
3986    SCOPE_REFs when they are not required.
3987
3988    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3989    `template' keyword.
3990
3991    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3992    uninstantiated templates.
3993
3994    If *TEMPLATE_P is non-NULL, it is set to true iff the
3995    `template' keyword is used to explicitly indicate that the entity
3996    named is a template.
3997
3998    If DECLARATOR_P is true, the id-expression is appearing as part of
3999    a declarator, rather than as part of an expression.  */
4000
4001 static tree
4002 cp_parser_id_expression (cp_parser *parser,
4003                          bool template_keyword_p,
4004                          bool check_dependency_p,
4005                          bool *template_p,
4006                          bool declarator_p,
4007                          bool optional_p)
4008 {
4009   bool global_scope_p;
4010   bool nested_name_specifier_p;
4011
4012   /* Assume the `template' keyword was not used.  */
4013   if (template_p)
4014     *template_p = template_keyword_p;
4015
4016   /* Look for the optional `::' operator.  */
4017   global_scope_p
4018     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4019        != NULL_TREE);
4020   /* Look for the optional nested-name-specifier.  */
4021   nested_name_specifier_p
4022     = (cp_parser_nested_name_specifier_opt (parser,
4023                                             /*typename_keyword_p=*/false,
4024                                             check_dependency_p,
4025                                             /*type_p=*/false,
4026                                             declarator_p)
4027        != NULL_TREE);
4028   /* If there is a nested-name-specifier, then we are looking at
4029      the first qualified-id production.  */
4030   if (nested_name_specifier_p)
4031     {
4032       tree saved_scope;
4033       tree saved_object_scope;
4034       tree saved_qualifying_scope;
4035       tree unqualified_id;
4036       bool is_template;
4037
4038       /* See if the next token is the `template' keyword.  */
4039       if (!template_p)
4040         template_p = &is_template;
4041       *template_p = cp_parser_optional_template_keyword (parser);
4042       /* Name lookup we do during the processing of the
4043          unqualified-id might obliterate SCOPE.  */
4044       saved_scope = parser->scope;
4045       saved_object_scope = parser->object_scope;
4046       saved_qualifying_scope = parser->qualifying_scope;
4047       /* Process the final unqualified-id.  */
4048       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4049                                                  check_dependency_p,
4050                                                  declarator_p,
4051                                                  /*optional_p=*/false);
4052       /* Restore the SAVED_SCOPE for our caller.  */
4053       parser->scope = saved_scope;
4054       parser->object_scope = saved_object_scope;
4055       parser->qualifying_scope = saved_qualifying_scope;
4056
4057       return unqualified_id;
4058     }
4059   /* Otherwise, if we are in global scope, then we are looking at one
4060      of the other qualified-id productions.  */
4061   else if (global_scope_p)
4062     {
4063       cp_token *token;
4064       tree id;
4065
4066       /* Peek at the next token.  */
4067       token = cp_lexer_peek_token (parser->lexer);
4068
4069       /* If it's an identifier, and the next token is not a "<", then
4070          we can avoid the template-id case.  This is an optimization
4071          for this common case.  */
4072       if (token->type == CPP_NAME
4073           && !cp_parser_nth_token_starts_template_argument_list_p
4074                (parser, 2))
4075         return cp_parser_identifier (parser);
4076
4077       cp_parser_parse_tentatively (parser);
4078       /* Try a template-id.  */
4079       id = cp_parser_template_id (parser,
4080                                   /*template_keyword_p=*/false,
4081                                   /*check_dependency_p=*/true,
4082                                   declarator_p);
4083       /* If that worked, we're done.  */
4084       if (cp_parser_parse_definitely (parser))
4085         return id;
4086
4087       /* Peek at the next token.  (Changes in the token buffer may
4088          have invalidated the pointer obtained above.)  */
4089       token = cp_lexer_peek_token (parser->lexer);
4090
4091       switch (token->type)
4092         {
4093         case CPP_NAME:
4094           return cp_parser_identifier (parser);
4095
4096         case CPP_KEYWORD:
4097           if (token->keyword == RID_OPERATOR)
4098             return cp_parser_operator_function_id (parser);
4099           /* Fall through.  */
4100
4101         default:
4102           cp_parser_error (parser, "expected id-expression");
4103           return error_mark_node;
4104         }
4105     }
4106   else
4107     return cp_parser_unqualified_id (parser, template_keyword_p,
4108                                      /*check_dependency_p=*/true,
4109                                      declarator_p,
4110                                      optional_p);
4111 }
4112
4113 /* Parse an unqualified-id.
4114
4115    unqualified-id:
4116      identifier
4117      operator-function-id
4118      conversion-function-id
4119      ~ class-name
4120      template-id
4121
4122    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4123    keyword, in a construct like `A::template ...'.
4124
4125    Returns a representation of unqualified-id.  For the `identifier'
4126    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4127    production a BIT_NOT_EXPR is returned; the operand of the
4128    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4129    other productions, see the documentation accompanying the
4130    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4131    names are looked up in uninstantiated templates.  If DECLARATOR_P
4132    is true, the unqualified-id is appearing as part of a declarator,
4133    rather than as part of an expression.  */
4134
4135 static tree
4136 cp_parser_unqualified_id (cp_parser* parser,
4137                           bool template_keyword_p,
4138                           bool check_dependency_p,
4139                           bool declarator_p,
4140                           bool optional_p)
4141 {
4142   cp_token *token;
4143
4144   /* Peek at the next token.  */
4145   token = cp_lexer_peek_token (parser->lexer);
4146
4147   switch (token->type)
4148     {
4149     case CPP_NAME:
4150       {
4151         tree id;
4152
4153         /* We don't know yet whether or not this will be a
4154            template-id.  */
4155         cp_parser_parse_tentatively (parser);
4156         /* Try a template-id.  */
4157         id = cp_parser_template_id (parser, template_keyword_p,
4158                                     check_dependency_p,
4159                                     declarator_p);
4160         /* If it worked, we're done.  */
4161         if (cp_parser_parse_definitely (parser))
4162           return id;
4163         /* Otherwise, it's an ordinary identifier.  */
4164         return cp_parser_identifier (parser);
4165       }
4166
4167     case CPP_TEMPLATE_ID:
4168       return cp_parser_template_id (parser, template_keyword_p,
4169                                     check_dependency_p,
4170                                     declarator_p);
4171
4172     case CPP_COMPL:
4173       {
4174         tree type_decl;
4175         tree qualifying_scope;
4176         tree object_scope;
4177         tree scope;
4178         bool done;
4179
4180         /* Consume the `~' token.  */
4181         cp_lexer_consume_token (parser->lexer);
4182         /* Parse the class-name.  The standard, as written, seems to
4183            say that:
4184
4185              template <typename T> struct S { ~S (); };
4186              template <typename T> S<T>::~S() {}
4187
4188            is invalid, since `~' must be followed by a class-name, but
4189            `S<T>' is dependent, and so not known to be a class.
4190            That's not right; we need to look in uninstantiated
4191            templates.  A further complication arises from:
4192
4193              template <typename T> void f(T t) {
4194                t.T::~T();
4195              }
4196
4197            Here, it is not possible to look up `T' in the scope of `T'
4198            itself.  We must look in both the current scope, and the
4199            scope of the containing complete expression.
4200
4201            Yet another issue is:
4202
4203              struct S {
4204                int S;
4205                ~S();
4206              };
4207
4208              S::~S() {}
4209
4210            The standard does not seem to say that the `S' in `~S'
4211            should refer to the type `S' and not the data member
4212            `S::S'.  */
4213
4214         /* DR 244 says that we look up the name after the "~" in the
4215            same scope as we looked up the qualifying name.  That idea
4216            isn't fully worked out; it's more complicated than that.  */
4217         scope = parser->scope;
4218         object_scope = parser->object_scope;
4219         qualifying_scope = parser->qualifying_scope;
4220
4221         /* Check for invalid scopes.  */
4222         if (scope == error_mark_node)
4223           {
4224             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4225               cp_lexer_consume_token (parser->lexer);
4226             return error_mark_node;
4227           }
4228         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4229           {
4230             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4231               error_at (token->location,
4232                         "scope %qT before %<~%> is not a class-name",
4233                         scope);
4234             cp_parser_simulate_error (parser);
4235             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4236               cp_lexer_consume_token (parser->lexer);
4237             return error_mark_node;
4238           }
4239         gcc_assert (!scope || TYPE_P (scope));
4240
4241         /* If the name is of the form "X::~X" it's OK even if X is a
4242            typedef.  */
4243         token = cp_lexer_peek_token (parser->lexer);
4244         if (scope
4245             && token->type == CPP_NAME
4246             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4247                 != CPP_LESS)
4248             && (token->u.value == TYPE_IDENTIFIER (scope)
4249                 || constructor_name_p (token->u.value, scope)))
4250           {
4251             cp_lexer_consume_token (parser->lexer);
4252             return build_nt (BIT_NOT_EXPR, scope);
4253           }
4254
4255         /* If there was an explicit qualification (S::~T), first look
4256            in the scope given by the qualification (i.e., S).
4257
4258            Note: in the calls to cp_parser_class_name below we pass
4259            typename_type so that lookup finds the injected-class-name
4260            rather than the constructor.  */
4261         done = false;
4262         type_decl = NULL_TREE;
4263         if (scope)
4264           {
4265             cp_parser_parse_tentatively (parser);
4266             type_decl = cp_parser_class_name (parser,
4267                                               /*typename_keyword_p=*/false,
4268                                               /*template_keyword_p=*/false,
4269                                               typename_type,
4270                                               /*check_dependency=*/false,
4271                                               /*class_head_p=*/false,
4272                                               declarator_p);
4273             if (cp_parser_parse_definitely (parser))
4274               done = true;
4275           }
4276         /* In "N::S::~S", look in "N" as well.  */
4277         if (!done && scope && qualifying_scope)
4278           {
4279             cp_parser_parse_tentatively (parser);
4280             parser->scope = qualifying_scope;
4281             parser->object_scope = NULL_TREE;
4282             parser->qualifying_scope = NULL_TREE;
4283             type_decl
4284               = cp_parser_class_name (parser,
4285                                       /*typename_keyword_p=*/false,
4286                                       /*template_keyword_p=*/false,
4287                                       typename_type,
4288                                       /*check_dependency=*/false,
4289                                       /*class_head_p=*/false,
4290                                       declarator_p);
4291             if (cp_parser_parse_definitely (parser))
4292               done = true;
4293           }
4294         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4295         else if (!done && object_scope)
4296           {
4297             cp_parser_parse_tentatively (parser);
4298             parser->scope = object_scope;
4299             parser->object_scope = NULL_TREE;
4300             parser->qualifying_scope = NULL_TREE;
4301             type_decl
4302               = cp_parser_class_name (parser,
4303                                       /*typename_keyword_p=*/false,
4304                                       /*template_keyword_p=*/false,
4305                                       typename_type,
4306                                       /*check_dependency=*/false,
4307                                       /*class_head_p=*/false,
4308                                       declarator_p);
4309             if (cp_parser_parse_definitely (parser))
4310               done = true;
4311           }
4312         /* Look in the surrounding context.  */
4313         if (!done)
4314           {
4315             parser->scope = NULL_TREE;
4316             parser->object_scope = NULL_TREE;
4317             parser->qualifying_scope = NULL_TREE;
4318             if (processing_template_decl)
4319               cp_parser_parse_tentatively (parser);
4320             type_decl
4321               = cp_parser_class_name (parser,
4322                                       /*typename_keyword_p=*/false,
4323                                       /*template_keyword_p=*/false,
4324                                       typename_type,
4325                                       /*check_dependency=*/false,
4326                                       /*class_head_p=*/false,
4327                                       declarator_p);
4328             if (processing_template_decl
4329                 && ! cp_parser_parse_definitely (parser))
4330               {
4331                 /* We couldn't find a type with this name, so just accept
4332                    it and check for a match at instantiation time.  */
4333                 type_decl = cp_parser_identifier (parser);
4334                 if (type_decl != error_mark_node)
4335                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4336                 return type_decl;
4337               }
4338           }
4339         /* If an error occurred, assume that the name of the
4340            destructor is the same as the name of the qualifying
4341            class.  That allows us to keep parsing after running
4342            into ill-formed destructor names.  */
4343         if (type_decl == error_mark_node && scope)
4344           return build_nt (BIT_NOT_EXPR, scope);
4345         else if (type_decl == error_mark_node)
4346           return error_mark_node;
4347
4348         /* Check that destructor name and scope match.  */
4349         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4350           {
4351             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4352               error_at (token->location,
4353                         "declaration of %<~%T%> as member of %qT",
4354                         type_decl, scope);
4355             cp_parser_simulate_error (parser);
4356             return error_mark_node;
4357           }
4358
4359         /* [class.dtor]
4360
4361            A typedef-name that names a class shall not be used as the
4362            identifier in the declarator for a destructor declaration.  */
4363         if (declarator_p
4364             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4365             && !DECL_SELF_REFERENCE_P (type_decl)
4366             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4367           error_at (token->location,
4368                     "typedef-name %qD used as destructor declarator",
4369                     type_decl);
4370
4371         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4372       }
4373
4374     case CPP_KEYWORD:
4375       if (token->keyword == RID_OPERATOR)
4376         {
4377           tree id;
4378
4379           /* This could be a template-id, so we try that first.  */
4380           cp_parser_parse_tentatively (parser);
4381           /* Try a template-id.  */
4382           id = cp_parser_template_id (parser, template_keyword_p,
4383                                       /*check_dependency_p=*/true,
4384                                       declarator_p);
4385           /* If that worked, we're done.  */
4386           if (cp_parser_parse_definitely (parser))
4387             return id;
4388           /* We still don't know whether we're looking at an
4389              operator-function-id or a conversion-function-id.  */
4390           cp_parser_parse_tentatively (parser);
4391           /* Try an operator-function-id.  */
4392           id = cp_parser_operator_function_id (parser);
4393           /* If that didn't work, try a conversion-function-id.  */
4394           if (!cp_parser_parse_definitely (parser))
4395             id = cp_parser_conversion_function_id (parser);
4396
4397           return id;
4398         }
4399       /* Fall through.  */
4400
4401     default:
4402       if (optional_p)
4403         return NULL_TREE;
4404       cp_parser_error (parser, "expected unqualified-id");
4405       return error_mark_node;
4406     }
4407 }
4408
4409 /* Parse an (optional) nested-name-specifier.
4410
4411    nested-name-specifier: [C++98]
4412      class-or-namespace-name :: nested-name-specifier [opt]
4413      class-or-namespace-name :: template nested-name-specifier [opt]
4414
4415    nested-name-specifier: [C++0x]
4416      type-name ::
4417      namespace-name ::
4418      nested-name-specifier identifier ::
4419      nested-name-specifier template [opt] simple-template-id ::
4420
4421    PARSER->SCOPE should be set appropriately before this function is
4422    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4423    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4424    in name lookups.
4425
4426    Sets PARSER->SCOPE to the class (TYPE) or namespace
4427    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4428    it unchanged if there is no nested-name-specifier.  Returns the new
4429    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4430
4431    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4432    part of a declaration and/or decl-specifier.  */
4433
4434 static tree
4435 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4436                                      bool typename_keyword_p,
4437                                      bool check_dependency_p,
4438                                      bool type_p,
4439                                      bool is_declaration)
4440 {
4441   bool success = false;
4442   cp_token_position start = 0;
4443   cp_token *token;
4444
4445   /* Remember where the nested-name-specifier starts.  */
4446   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4447     {
4448       start = cp_lexer_token_position (parser->lexer, false);
4449       push_deferring_access_checks (dk_deferred);
4450     }
4451
4452   while (true)
4453     {
4454       tree new_scope;
4455       tree old_scope;
4456       tree saved_qualifying_scope;
4457       bool template_keyword_p;
4458
4459       /* Spot cases that cannot be the beginning of a
4460          nested-name-specifier.  */
4461       token = cp_lexer_peek_token (parser->lexer);
4462
4463       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4464          the already parsed nested-name-specifier.  */
4465       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4466         {
4467           /* Grab the nested-name-specifier and continue the loop.  */
4468           cp_parser_pre_parsed_nested_name_specifier (parser);
4469           /* If we originally encountered this nested-name-specifier
4470              with IS_DECLARATION set to false, we will not have
4471              resolved TYPENAME_TYPEs, so we must do so here.  */
4472           if (is_declaration
4473               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4474             {
4475               new_scope = resolve_typename_type (parser->scope,
4476                                                  /*only_current_p=*/false);
4477               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4478                 parser->scope = new_scope;
4479             }
4480           success = true;
4481           continue;
4482         }
4483
4484       /* Spot cases that cannot be the beginning of a
4485          nested-name-specifier.  On the second and subsequent times
4486          through the loop, we look for the `template' keyword.  */
4487       if (success && token->keyword == RID_TEMPLATE)
4488         ;
4489       /* A template-id can start a nested-name-specifier.  */
4490       else if (token->type == CPP_TEMPLATE_ID)
4491         ;
4492       else
4493         {
4494           /* If the next token is not an identifier, then it is
4495              definitely not a type-name or namespace-name.  */
4496           if (token->type != CPP_NAME)
4497             break;
4498           /* If the following token is neither a `<' (to begin a
4499              template-id), nor a `::', then we are not looking at a
4500              nested-name-specifier.  */
4501           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4502           if (token->type != CPP_SCOPE
4503               && !cp_parser_nth_token_starts_template_argument_list_p
4504                   (parser, 2))
4505             break;
4506         }
4507
4508       /* The nested-name-specifier is optional, so we parse
4509          tentatively.  */
4510       cp_parser_parse_tentatively (parser);
4511
4512       /* Look for the optional `template' keyword, if this isn't the
4513          first time through the loop.  */
4514       if (success)
4515         template_keyword_p = cp_parser_optional_template_keyword (parser);
4516       else
4517         template_keyword_p = false;
4518
4519       /* Save the old scope since the name lookup we are about to do
4520          might destroy it.  */
4521       old_scope = parser->scope;
4522       saved_qualifying_scope = parser->qualifying_scope;
4523       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4524          look up names in "X<T>::I" in order to determine that "Y" is
4525          a template.  So, if we have a typename at this point, we make
4526          an effort to look through it.  */
4527       if (is_declaration
4528           && !typename_keyword_p
4529           && parser->scope
4530           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4531         parser->scope = resolve_typename_type (parser->scope,
4532                                                /*only_current_p=*/false);
4533       /* Parse the qualifying entity.  */
4534       new_scope
4535         = cp_parser_qualifying_entity (parser,
4536                                        typename_keyword_p,
4537                                        template_keyword_p,
4538                                        check_dependency_p,
4539                                        type_p,
4540                                        is_declaration);
4541       /* Look for the `::' token.  */
4542       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4543
4544       /* If we found what we wanted, we keep going; otherwise, we're
4545          done.  */
4546       if (!cp_parser_parse_definitely (parser))
4547         {
4548           bool error_p = false;
4549
4550           /* Restore the OLD_SCOPE since it was valid before the
4551              failed attempt at finding the last
4552              class-or-namespace-name.  */
4553           parser->scope = old_scope;
4554           parser->qualifying_scope = saved_qualifying_scope;
4555           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4556             break;
4557           /* If the next token is an identifier, and the one after
4558              that is a `::', then any valid interpretation would have
4559              found a class-or-namespace-name.  */
4560           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4561                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4562                      == CPP_SCOPE)
4563                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4564                      != CPP_COMPL))
4565             {
4566               token = cp_lexer_consume_token (parser->lexer);
4567               if (!error_p)
4568                 {
4569                   if (!token->ambiguous_p)
4570                     {
4571                       tree decl;
4572                       tree ambiguous_decls;
4573
4574                       decl = cp_parser_lookup_name (parser, token->u.value,
4575                                                     none_type,
4576                                                     /*is_template=*/false,
4577                                                     /*is_namespace=*/false,
4578                                                     /*check_dependency=*/true,
4579                                                     &ambiguous_decls,
4580                                                     token->location);
4581                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4582                         error_at (token->location,
4583                                   "%qD used without template parameters",
4584                                   decl);
4585                       else if (ambiguous_decls)
4586                         {
4587                           error_at (token->location,
4588                                     "reference to %qD is ambiguous",
4589                                     token->u.value);
4590                           print_candidates (ambiguous_decls);
4591                           decl = error_mark_node;
4592                         }
4593                       else
4594                         {
4595                           if (cxx_dialect != cxx98)
4596                             cp_parser_name_lookup_error
4597                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4598                              token->location);
4599                           else
4600                             cp_parser_name_lookup_error
4601                             (parser, token->u.value, decl, NLE_CXX98,
4602                              token->location);
4603                         }
4604                     }
4605                   parser->scope = error_mark_node;
4606                   error_p = true;
4607                   /* Treat this as a successful nested-name-specifier
4608                      due to:
4609
4610                      [basic.lookup.qual]
4611
4612                      If the name found is not a class-name (clause
4613                      _class_) or namespace-name (_namespace.def_), the
4614                      program is ill-formed.  */
4615                   success = true;
4616                 }
4617               cp_lexer_consume_token (parser->lexer);
4618             }
4619           break;
4620         }
4621       /* We've found one valid nested-name-specifier.  */
4622       success = true;
4623       /* Name lookup always gives us a DECL.  */
4624       if (TREE_CODE (new_scope) == TYPE_DECL)
4625         new_scope = TREE_TYPE (new_scope);
4626       /* Uses of "template" must be followed by actual templates.  */
4627       if (template_keyword_p
4628           && !(CLASS_TYPE_P (new_scope)
4629                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4630                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4631                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4632           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4633                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4634                    == TEMPLATE_ID_EXPR)))
4635         permerror (input_location, TYPE_P (new_scope)
4636                    ? "%qT is not a template"
4637                    : "%qD is not a template",
4638                    new_scope);
4639       /* If it is a class scope, try to complete it; we are about to
4640          be looking up names inside the class.  */
4641       if (TYPE_P (new_scope)
4642           /* Since checking types for dependency can be expensive,
4643              avoid doing it if the type is already complete.  */
4644           && !COMPLETE_TYPE_P (new_scope)
4645           /* Do not try to complete dependent types.  */
4646           && !dependent_type_p (new_scope))
4647         {
4648           new_scope = complete_type (new_scope);
4649           /* If it is a typedef to current class, use the current
4650              class instead, as the typedef won't have any names inside
4651              it yet.  */
4652           if (!COMPLETE_TYPE_P (new_scope)
4653               && currently_open_class (new_scope))
4654             new_scope = TYPE_MAIN_VARIANT (new_scope);
4655         }
4656       /* Make sure we look in the right scope the next time through
4657          the loop.  */
4658       parser->scope = new_scope;
4659     }
4660
4661   /* If parsing tentatively, replace the sequence of tokens that makes
4662      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4663      token.  That way, should we re-parse the token stream, we will
4664      not have to repeat the effort required to do the parse, nor will
4665      we issue duplicate error messages.  */
4666   if (success && start)
4667     {
4668       cp_token *token;
4669
4670       token = cp_lexer_token_at (parser->lexer, start);
4671       /* Reset the contents of the START token.  */
4672       token->type = CPP_NESTED_NAME_SPECIFIER;
4673       /* Retrieve any deferred checks.  Do not pop this access checks yet
4674          so the memory will not be reclaimed during token replacing below.  */
4675       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4676       token->u.tree_check_value->value = parser->scope;
4677       token->u.tree_check_value->checks = get_deferred_access_checks ();
4678       token->u.tree_check_value->qualifying_scope =
4679         parser->qualifying_scope;
4680       token->keyword = RID_MAX;
4681
4682       /* Purge all subsequent tokens.  */
4683       cp_lexer_purge_tokens_after (parser->lexer, start);
4684     }
4685
4686   if (start)
4687     pop_to_parent_deferring_access_checks ();
4688
4689   return success ? parser->scope : NULL_TREE;
4690 }
4691
4692 /* Parse a nested-name-specifier.  See
4693    cp_parser_nested_name_specifier_opt for details.  This function
4694    behaves identically, except that it will an issue an error if no
4695    nested-name-specifier is present.  */
4696
4697 static tree
4698 cp_parser_nested_name_specifier (cp_parser *parser,
4699                                  bool typename_keyword_p,
4700                                  bool check_dependency_p,
4701                                  bool type_p,
4702                                  bool is_declaration)
4703 {
4704   tree scope;
4705
4706   /* Look for the nested-name-specifier.  */
4707   scope = cp_parser_nested_name_specifier_opt (parser,
4708                                                typename_keyword_p,
4709                                                check_dependency_p,
4710                                                type_p,
4711                                                is_declaration);
4712   /* If it was not present, issue an error message.  */
4713   if (!scope)
4714     {
4715       cp_parser_error (parser, "expected nested-name-specifier");
4716       parser->scope = NULL_TREE;
4717     }
4718
4719   return scope;
4720 }
4721
4722 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4723    this is either a class-name or a namespace-name (which corresponds
4724    to the class-or-namespace-name production in the grammar). For
4725    C++0x, it can also be a type-name that refers to an enumeration
4726    type.
4727
4728    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4729    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4730    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4731    TYPE_P is TRUE iff the next name should be taken as a class-name,
4732    even the same name is declared to be another entity in the same
4733    scope.
4734
4735    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4736    specified by the class-or-namespace-name.  If neither is found the
4737    ERROR_MARK_NODE is returned.  */
4738
4739 static tree
4740 cp_parser_qualifying_entity (cp_parser *parser,
4741                              bool typename_keyword_p,
4742                              bool template_keyword_p,
4743                              bool check_dependency_p,
4744                              bool type_p,
4745                              bool is_declaration)
4746 {
4747   tree saved_scope;
4748   tree saved_qualifying_scope;
4749   tree saved_object_scope;
4750   tree scope;
4751   bool only_class_p;
4752   bool successful_parse_p;
4753
4754   /* Before we try to parse the class-name, we must save away the
4755      current PARSER->SCOPE since cp_parser_class_name will destroy
4756      it.  */
4757   saved_scope = parser->scope;
4758   saved_qualifying_scope = parser->qualifying_scope;
4759   saved_object_scope = parser->object_scope;
4760   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4761      there is no need to look for a namespace-name.  */
4762   only_class_p = template_keyword_p 
4763     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4764   if (!only_class_p)
4765     cp_parser_parse_tentatively (parser);
4766   scope = cp_parser_class_name (parser,
4767                                 typename_keyword_p,
4768                                 template_keyword_p,
4769                                 type_p ? class_type : none_type,
4770                                 check_dependency_p,
4771                                 /*class_head_p=*/false,
4772                                 is_declaration);
4773   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4774   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4775   if (!only_class_p 
4776       && cxx_dialect != cxx98
4777       && !successful_parse_p)
4778     {
4779       /* Restore the saved scope.  */
4780       parser->scope = saved_scope;
4781       parser->qualifying_scope = saved_qualifying_scope;
4782       parser->object_scope = saved_object_scope;
4783
4784       /* Parse tentatively.  */
4785       cp_parser_parse_tentatively (parser);
4786      
4787       /* Parse a typedef-name or enum-name.  */
4788       scope = cp_parser_nonclass_name (parser);
4789
4790       /* "If the name found does not designate a namespace or a class,
4791          enumeration, or dependent type, the program is ill-formed."
4792
4793          We cover classes and dependent types above and namespaces below,
4794          so this code is only looking for enums.  */
4795       if (!scope || TREE_CODE (scope) != TYPE_DECL
4796           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4797         cp_parser_simulate_error (parser);
4798
4799       successful_parse_p = cp_parser_parse_definitely (parser);
4800     }
4801   /* If that didn't work, try for a namespace-name.  */
4802   if (!only_class_p && !successful_parse_p)
4803     {
4804       /* Restore the saved scope.  */
4805       parser->scope = saved_scope;
4806       parser->qualifying_scope = saved_qualifying_scope;
4807       parser->object_scope = saved_object_scope;
4808       /* If we are not looking at an identifier followed by the scope
4809          resolution operator, then this is not part of a
4810          nested-name-specifier.  (Note that this function is only used
4811          to parse the components of a nested-name-specifier.)  */
4812       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4813           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4814         return error_mark_node;
4815       scope = cp_parser_namespace_name (parser);
4816     }
4817
4818   return scope;
4819 }
4820
4821 /* Parse a postfix-expression.
4822
4823    postfix-expression:
4824      primary-expression
4825      postfix-expression [ expression ]
4826      postfix-expression ( expression-list [opt] )
4827      simple-type-specifier ( expression-list [opt] )
4828      typename :: [opt] nested-name-specifier identifier
4829        ( expression-list [opt] )
4830      typename :: [opt] nested-name-specifier template [opt] template-id
4831        ( expression-list [opt] )
4832      postfix-expression . template [opt] id-expression
4833      postfix-expression -> template [opt] id-expression
4834      postfix-expression . pseudo-destructor-name
4835      postfix-expression -> pseudo-destructor-name
4836      postfix-expression ++
4837      postfix-expression --
4838      dynamic_cast < type-id > ( expression )
4839      static_cast < type-id > ( expression )
4840      reinterpret_cast < type-id > ( expression )
4841      const_cast < type-id > ( expression )
4842      typeid ( expression )
4843      typeid ( type-id )
4844
4845    GNU Extension:
4846
4847    postfix-expression:
4848      ( type-id ) { initializer-list , [opt] }
4849
4850    This extension is a GNU version of the C99 compound-literal
4851    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4852    but they are essentially the same concept.)
4853
4854    If ADDRESS_P is true, the postfix expression is the operand of the
4855    `&' operator.  CAST_P is true if this expression is the target of a
4856    cast.
4857
4858    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4859    class member access expressions [expr.ref].
4860
4861    Returns a representation of the expression.  */
4862
4863 static tree
4864 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4865                               bool member_access_only_p,
4866                               cp_id_kind * pidk_return)
4867 {
4868   cp_token *token;
4869   enum rid keyword;
4870   cp_id_kind idk = CP_ID_KIND_NONE;
4871   tree postfix_expression = NULL_TREE;
4872   bool is_member_access = false;
4873
4874   /* Peek at the next token.  */
4875   token = cp_lexer_peek_token (parser->lexer);
4876   /* Some of the productions are determined by keywords.  */
4877   keyword = token->keyword;
4878   switch (keyword)
4879     {
4880     case RID_DYNCAST:
4881     case RID_STATCAST:
4882     case RID_REINTCAST:
4883     case RID_CONSTCAST:
4884       {
4885         tree type;
4886         tree expression;
4887         const char *saved_message;
4888
4889         /* All of these can be handled in the same way from the point
4890            of view of parsing.  Begin by consuming the token
4891            identifying the cast.  */
4892         cp_lexer_consume_token (parser->lexer);
4893
4894         /* New types cannot be defined in the cast.  */
4895         saved_message = parser->type_definition_forbidden_message;
4896         parser->type_definition_forbidden_message
4897           = G_("types may not be defined in casts");
4898
4899         /* Look for the opening `<'.  */
4900         cp_parser_require (parser, CPP_LESS, RT_LESS);
4901         /* Parse the type to which we are casting.  */
4902         type = cp_parser_type_id (parser);
4903         /* Look for the closing `>'.  */
4904         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4905         /* Restore the old message.  */
4906         parser->type_definition_forbidden_message = saved_message;
4907
4908         /* And the expression which is being cast.  */
4909         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4910         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4911         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4912
4913         /* Only type conversions to integral or enumeration types
4914            can be used in constant-expressions.  */
4915         if (!cast_valid_in_integral_constant_expression_p (type)
4916             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4917           return error_mark_node;
4918
4919         switch (keyword)
4920           {
4921           case RID_DYNCAST:
4922             postfix_expression
4923               = build_dynamic_cast (type, expression, tf_warning_or_error);
4924             break;
4925           case RID_STATCAST:
4926             postfix_expression
4927               = build_static_cast (type, expression, tf_warning_or_error);
4928             break;
4929           case RID_REINTCAST:
4930             postfix_expression
4931               = build_reinterpret_cast (type, expression, 
4932                                         tf_warning_or_error);
4933             break;
4934           case RID_CONSTCAST:
4935             postfix_expression
4936               = build_const_cast (type, expression, tf_warning_or_error);
4937             break;
4938           default:
4939             gcc_unreachable ();
4940           }
4941       }
4942       break;
4943
4944     case RID_TYPEID:
4945       {
4946         tree type;
4947         const char *saved_message;
4948         bool saved_in_type_id_in_expr_p;
4949
4950         /* Consume the `typeid' token.  */
4951         cp_lexer_consume_token (parser->lexer);
4952         /* Look for the `(' token.  */
4953         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4954         /* Types cannot be defined in a `typeid' expression.  */
4955         saved_message = parser->type_definition_forbidden_message;
4956         parser->type_definition_forbidden_message
4957           = G_("types may not be defined in a %<typeid%> expression");
4958         /* We can't be sure yet whether we're looking at a type-id or an
4959            expression.  */
4960         cp_parser_parse_tentatively (parser);
4961         /* Try a type-id first.  */
4962         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4963         parser->in_type_id_in_expr_p = true;
4964         type = cp_parser_type_id (parser);
4965         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4966         /* Look for the `)' token.  Otherwise, we can't be sure that
4967            we're not looking at an expression: consider `typeid (int
4968            (3))', for example.  */
4969         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4970         /* If all went well, simply lookup the type-id.  */
4971         if (cp_parser_parse_definitely (parser))
4972           postfix_expression = get_typeid (type);
4973         /* Otherwise, fall back to the expression variant.  */
4974         else
4975           {
4976             tree expression;
4977
4978             /* Look for an expression.  */
4979             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4980             /* Compute its typeid.  */
4981             postfix_expression = build_typeid (expression);
4982             /* Look for the `)' token.  */
4983             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4984           }
4985         /* Restore the saved message.  */
4986         parser->type_definition_forbidden_message = saved_message;
4987         /* `typeid' may not appear in an integral constant expression.  */
4988         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4989           return error_mark_node;
4990       }
4991       break;
4992
4993     case RID_TYPENAME:
4994       {
4995         tree type;
4996         /* The syntax permitted here is the same permitted for an
4997            elaborated-type-specifier.  */
4998         type = cp_parser_elaborated_type_specifier (parser,
4999                                                     /*is_friend=*/false,
5000                                                     /*is_declaration=*/false);
5001         postfix_expression = cp_parser_functional_cast (parser, type);
5002       }
5003       break;
5004
5005     default:
5006       {
5007         tree type;
5008
5009         /* If the next thing is a simple-type-specifier, we may be
5010            looking at a functional cast.  We could also be looking at
5011            an id-expression.  So, we try the functional cast, and if
5012            that doesn't work we fall back to the primary-expression.  */
5013         cp_parser_parse_tentatively (parser);
5014         /* Look for the simple-type-specifier.  */
5015         type = cp_parser_simple_type_specifier (parser,
5016                                                 /*decl_specs=*/NULL,
5017                                                 CP_PARSER_FLAGS_NONE);
5018         /* Parse the cast itself.  */
5019         if (!cp_parser_error_occurred (parser))
5020           postfix_expression
5021             = cp_parser_functional_cast (parser, type);
5022         /* If that worked, we're done.  */
5023         if (cp_parser_parse_definitely (parser))
5024           break;
5025
5026         /* If the functional-cast didn't work out, try a
5027            compound-literal.  */
5028         if (cp_parser_allow_gnu_extensions_p (parser)
5029             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5030           {
5031             VEC(constructor_elt,gc) *initializer_list = NULL;
5032             bool saved_in_type_id_in_expr_p;
5033
5034             cp_parser_parse_tentatively (parser);
5035             /* Consume the `('.  */
5036             cp_lexer_consume_token (parser->lexer);
5037             /* Parse the type.  */
5038             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5039             parser->in_type_id_in_expr_p = true;
5040             type = cp_parser_type_id (parser);
5041             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5042             /* Look for the `)'.  */
5043             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5044             /* Look for the `{'.  */
5045             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5046             /* If things aren't going well, there's no need to
5047                keep going.  */
5048             if (!cp_parser_error_occurred (parser))
5049               {
5050                 bool non_constant_p;
5051                 /* Parse the initializer-list.  */
5052                 initializer_list
5053                   = cp_parser_initializer_list (parser, &non_constant_p);
5054                 /* Allow a trailing `,'.  */
5055                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5056                   cp_lexer_consume_token (parser->lexer);
5057                 /* Look for the final `}'.  */
5058                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5059               }
5060             /* If that worked, we're definitely looking at a
5061                compound-literal expression.  */
5062             if (cp_parser_parse_definitely (parser))
5063               {
5064                 /* Warn the user that a compound literal is not
5065                    allowed in standard C++.  */
5066                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5067                 /* For simplicity, we disallow compound literals in
5068                    constant-expressions.  We could
5069                    allow compound literals of integer type, whose
5070                    initializer was a constant, in constant
5071                    expressions.  Permitting that usage, as a further
5072                    extension, would not change the meaning of any
5073                    currently accepted programs.  (Of course, as
5074                    compound literals are not part of ISO C++, the
5075                    standard has nothing to say.)  */
5076                 if (cp_parser_non_integral_constant_expression (parser,
5077                                                                 NIC_NCC))
5078                   {
5079                     postfix_expression = error_mark_node;
5080                     break;
5081                   }
5082                 /* Form the representation of the compound-literal.  */
5083                 postfix_expression
5084                   = (finish_compound_literal
5085                      (type, build_constructor (init_list_type_node,
5086                                                initializer_list)));
5087                 break;
5088               }
5089           }
5090
5091         /* It must be a primary-expression.  */
5092         postfix_expression
5093           = cp_parser_primary_expression (parser, address_p, cast_p,
5094                                           /*template_arg_p=*/false,
5095                                           &idk);
5096       }
5097       break;
5098     }
5099
5100   /* Keep looping until the postfix-expression is complete.  */
5101   while (true)
5102     {
5103       if (idk == CP_ID_KIND_UNQUALIFIED
5104           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5105           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5106         /* It is not a Koenig lookup function call.  */
5107         postfix_expression
5108           = unqualified_name_lookup_error (postfix_expression);
5109
5110       /* Peek at the next token.  */
5111       token = cp_lexer_peek_token (parser->lexer);
5112
5113       switch (token->type)
5114         {
5115         case CPP_OPEN_SQUARE:
5116           postfix_expression
5117             = cp_parser_postfix_open_square_expression (parser,
5118                                                         postfix_expression,
5119                                                         false);
5120           idk = CP_ID_KIND_NONE;
5121           is_member_access = false;
5122           break;
5123
5124         case CPP_OPEN_PAREN:
5125           /* postfix-expression ( expression-list [opt] ) */
5126           {
5127             bool koenig_p;
5128             bool is_builtin_constant_p;
5129             bool saved_integral_constant_expression_p = false;
5130             bool saved_non_integral_constant_expression_p = false;
5131             VEC(tree,gc) *args;
5132
5133             is_member_access = false;
5134
5135             is_builtin_constant_p
5136               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5137             if (is_builtin_constant_p)
5138               {
5139                 /* The whole point of __builtin_constant_p is to allow
5140                    non-constant expressions to appear as arguments.  */
5141                 saved_integral_constant_expression_p
5142                   = parser->integral_constant_expression_p;
5143                 saved_non_integral_constant_expression_p
5144                   = parser->non_integral_constant_expression_p;
5145                 parser->integral_constant_expression_p = false;
5146               }
5147             args = (cp_parser_parenthesized_expression_list
5148                     (parser, non_attr,
5149                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5150                      /*non_constant_p=*/NULL));
5151             if (is_builtin_constant_p)
5152               {
5153                 parser->integral_constant_expression_p
5154                   = saved_integral_constant_expression_p;
5155                 parser->non_integral_constant_expression_p
5156                   = saved_non_integral_constant_expression_p;
5157               }
5158
5159             if (args == NULL)
5160               {
5161                 postfix_expression = error_mark_node;
5162                 break;
5163               }
5164
5165             /* Function calls are not permitted in
5166                constant-expressions.  */
5167             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5168                 && cp_parser_non_integral_constant_expression (parser,
5169                                                                NIC_FUNC_CALL))
5170               {
5171                 postfix_expression = error_mark_node;
5172                 release_tree_vector (args);
5173                 break;
5174               }
5175
5176             koenig_p = false;
5177             if (idk == CP_ID_KIND_UNQUALIFIED
5178                 || idk == CP_ID_KIND_TEMPLATE_ID)
5179               {
5180                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5181                   {
5182                     if (!VEC_empty (tree, args))
5183                       {
5184                         koenig_p = true;
5185                         if (!any_type_dependent_arguments_p (args))
5186                           postfix_expression
5187                             = perform_koenig_lookup (postfix_expression, args,
5188                                                      /*include_std=*/false);
5189                       }
5190                     else
5191                       postfix_expression
5192                         = unqualified_fn_lookup_error (postfix_expression);
5193                   }
5194                 /* We do not perform argument-dependent lookup if
5195                    normal lookup finds a non-function, in accordance
5196                    with the expected resolution of DR 218.  */
5197                 else if (!VEC_empty (tree, args)
5198                          && is_overloaded_fn (postfix_expression))
5199                   {
5200                     tree fn = get_first_fn (postfix_expression);
5201                     fn = STRIP_TEMPLATE (fn);
5202
5203                     /* Do not do argument dependent lookup if regular
5204                        lookup finds a member function or a block-scope
5205                        function declaration.  [basic.lookup.argdep]/3  */
5206                     if (!DECL_FUNCTION_MEMBER_P (fn)
5207                         && !DECL_LOCAL_FUNCTION_P (fn))
5208                       {
5209                         koenig_p = true;
5210                         if (!any_type_dependent_arguments_p (args))
5211                           postfix_expression
5212                             = perform_koenig_lookup (postfix_expression, args,
5213                                                      /*include_std=*/false);
5214                       }
5215                   }
5216               }
5217
5218             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5219               {
5220                 tree instance = TREE_OPERAND (postfix_expression, 0);
5221                 tree fn = TREE_OPERAND (postfix_expression, 1);
5222
5223                 if (processing_template_decl
5224                     && (type_dependent_expression_p (instance)
5225                         || (!BASELINK_P (fn)
5226                             && TREE_CODE (fn) != FIELD_DECL)
5227                         || type_dependent_expression_p (fn)
5228                         || any_type_dependent_arguments_p (args)))
5229                   {
5230                     postfix_expression
5231                       = build_nt_call_vec (postfix_expression, args);
5232                     release_tree_vector (args);
5233                     break;
5234                   }
5235
5236                 if (BASELINK_P (fn))
5237                   {
5238                   postfix_expression
5239                     = (build_new_method_call
5240                        (instance, fn, &args, NULL_TREE,
5241                         (idk == CP_ID_KIND_QUALIFIED
5242                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5243                         /*fn_p=*/NULL,
5244                         tf_warning_or_error));
5245                   }
5246                 else
5247                   postfix_expression
5248                     = finish_call_expr (postfix_expression, &args,
5249                                         /*disallow_virtual=*/false,
5250                                         /*koenig_p=*/false,
5251                                         tf_warning_or_error);
5252               }
5253             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5254                      || TREE_CODE (postfix_expression) == MEMBER_REF
5255                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5256               postfix_expression = (build_offset_ref_call_from_tree
5257                                     (postfix_expression, &args));
5258             else if (idk == CP_ID_KIND_QUALIFIED)
5259               /* A call to a static class member, or a namespace-scope
5260                  function.  */
5261               postfix_expression
5262                 = finish_call_expr (postfix_expression, &args,
5263                                     /*disallow_virtual=*/true,
5264                                     koenig_p,
5265                                     tf_warning_or_error);
5266             else
5267               /* All other function calls.  */
5268               postfix_expression
5269                 = finish_call_expr (postfix_expression, &args,
5270                                     /*disallow_virtual=*/false,
5271                                     koenig_p,
5272                                     tf_warning_or_error);
5273
5274             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5275             idk = CP_ID_KIND_NONE;
5276
5277             release_tree_vector (args);
5278           }
5279           break;
5280
5281         case CPP_DOT:
5282         case CPP_DEREF:
5283           /* postfix-expression . template [opt] id-expression
5284              postfix-expression . pseudo-destructor-name
5285              postfix-expression -> template [opt] id-expression
5286              postfix-expression -> pseudo-destructor-name */
5287
5288           /* Consume the `.' or `->' operator.  */
5289           cp_lexer_consume_token (parser->lexer);
5290
5291           postfix_expression
5292             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5293                                                       postfix_expression,
5294                                                       false, &idk,
5295                                                       token->location);
5296
5297           is_member_access = true;
5298           break;
5299
5300         case CPP_PLUS_PLUS:
5301           /* postfix-expression ++  */
5302           /* Consume the `++' token.  */
5303           cp_lexer_consume_token (parser->lexer);
5304           /* Generate a representation for the complete expression.  */
5305           postfix_expression
5306             = finish_increment_expr (postfix_expression,
5307                                      POSTINCREMENT_EXPR);
5308           /* Increments may not appear in constant-expressions.  */
5309           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5310             postfix_expression = error_mark_node;
5311           idk = CP_ID_KIND_NONE;
5312           is_member_access = false;
5313           break;
5314
5315         case CPP_MINUS_MINUS:
5316           /* postfix-expression -- */
5317           /* Consume the `--' token.  */
5318           cp_lexer_consume_token (parser->lexer);
5319           /* Generate a representation for the complete expression.  */
5320           postfix_expression
5321             = finish_increment_expr (postfix_expression,
5322                                      POSTDECREMENT_EXPR);
5323           /* Decrements may not appear in constant-expressions.  */
5324           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5325             postfix_expression = error_mark_node;
5326           idk = CP_ID_KIND_NONE;
5327           is_member_access = false;
5328           break;
5329
5330         default:
5331           if (pidk_return != NULL)
5332             * pidk_return = idk;
5333           if (member_access_only_p)
5334             return is_member_access? postfix_expression : error_mark_node;
5335           else
5336             return postfix_expression;
5337         }
5338     }
5339
5340   /* We should never get here.  */
5341   gcc_unreachable ();
5342   return error_mark_node;
5343 }
5344
5345 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5346    by cp_parser_builtin_offsetof.  We're looking for
5347
5348      postfix-expression [ expression ]
5349
5350    FOR_OFFSETOF is set if we're being called in that context, which
5351    changes how we deal with integer constant expressions.  */
5352
5353 static tree
5354 cp_parser_postfix_open_square_expression (cp_parser *parser,
5355                                           tree postfix_expression,
5356                                           bool for_offsetof)
5357 {
5358   tree index;
5359
5360   /* Consume the `[' token.  */
5361   cp_lexer_consume_token (parser->lexer);
5362
5363   /* Parse the index expression.  */
5364   /* ??? For offsetof, there is a question of what to allow here.  If
5365      offsetof is not being used in an integral constant expression context,
5366      then we *could* get the right answer by computing the value at runtime.
5367      If we are in an integral constant expression context, then we might
5368      could accept any constant expression; hard to say without analysis.
5369      Rather than open the barn door too wide right away, allow only integer
5370      constant expressions here.  */
5371   if (for_offsetof)
5372     index = cp_parser_constant_expression (parser, false, NULL);
5373   else
5374     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5375
5376   /* Look for the closing `]'.  */
5377   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5378
5379   /* Build the ARRAY_REF.  */
5380   postfix_expression = grok_array_decl (postfix_expression, index);
5381
5382   /* When not doing offsetof, array references are not permitted in
5383      constant-expressions.  */
5384   if (!for_offsetof
5385       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5386     postfix_expression = error_mark_node;
5387
5388   return postfix_expression;
5389 }
5390
5391 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5392    by cp_parser_builtin_offsetof.  We're looking for
5393
5394      postfix-expression . template [opt] id-expression
5395      postfix-expression . pseudo-destructor-name
5396      postfix-expression -> template [opt] id-expression
5397      postfix-expression -> pseudo-destructor-name
5398
5399    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5400    limits what of the above we'll actually accept, but nevermind.
5401    TOKEN_TYPE is the "." or "->" token, which will already have been
5402    removed from the stream.  */
5403
5404 static tree
5405 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5406                                         enum cpp_ttype token_type,
5407                                         tree postfix_expression,
5408                                         bool for_offsetof, cp_id_kind *idk,
5409                                         location_t location)
5410 {
5411   tree name;
5412   bool dependent_p;
5413   bool pseudo_destructor_p;
5414   tree scope = NULL_TREE;
5415
5416   /* If this is a `->' operator, dereference the pointer.  */
5417   if (token_type == CPP_DEREF)
5418     postfix_expression = build_x_arrow (postfix_expression);
5419   /* Check to see whether or not the expression is type-dependent.  */
5420   dependent_p = type_dependent_expression_p (postfix_expression);
5421   /* The identifier following the `->' or `.' is not qualified.  */
5422   parser->scope = NULL_TREE;
5423   parser->qualifying_scope = NULL_TREE;
5424   parser->object_scope = NULL_TREE;
5425   *idk = CP_ID_KIND_NONE;
5426
5427   /* Enter the scope corresponding to the type of the object
5428      given by the POSTFIX_EXPRESSION.  */
5429   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5430     {
5431       scope = TREE_TYPE (postfix_expression);
5432       /* According to the standard, no expression should ever have
5433          reference type.  Unfortunately, we do not currently match
5434          the standard in this respect in that our internal representation
5435          of an expression may have reference type even when the standard
5436          says it does not.  Therefore, we have to manually obtain the
5437          underlying type here.  */
5438       scope = non_reference (scope);
5439       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5440       if (scope == unknown_type_node)
5441         {
5442           error_at (location, "%qE does not have class type",
5443                     postfix_expression);
5444           scope = NULL_TREE;
5445         }
5446       else
5447         scope = complete_type_or_else (scope, NULL_TREE);
5448       /* Let the name lookup machinery know that we are processing a
5449          class member access expression.  */
5450       parser->context->object_type = scope;
5451       /* If something went wrong, we want to be able to discern that case,
5452          as opposed to the case where there was no SCOPE due to the type
5453          of expression being dependent.  */
5454       if (!scope)
5455         scope = error_mark_node;
5456       /* If the SCOPE was erroneous, make the various semantic analysis
5457          functions exit quickly -- and without issuing additional error
5458          messages.  */
5459       if (scope == error_mark_node)
5460         postfix_expression = error_mark_node;
5461     }
5462
5463   /* Assume this expression is not a pseudo-destructor access.  */
5464   pseudo_destructor_p = false;
5465
5466   /* If the SCOPE is a scalar type, then, if this is a valid program,
5467      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5468      is type dependent, it can be pseudo-destructor-name or something else.
5469      Try to parse it as pseudo-destructor-name first.  */
5470   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5471     {
5472       tree s;
5473       tree type;
5474
5475       cp_parser_parse_tentatively (parser);
5476       /* Parse the pseudo-destructor-name.  */
5477       s = NULL_TREE;
5478       cp_parser_pseudo_destructor_name (parser, &s, &type);
5479       if (dependent_p
5480           && (cp_parser_error_occurred (parser)
5481               || TREE_CODE (type) != TYPE_DECL
5482               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5483         cp_parser_abort_tentative_parse (parser);
5484       else if (cp_parser_parse_definitely (parser))
5485         {
5486           pseudo_destructor_p = true;
5487           postfix_expression
5488             = finish_pseudo_destructor_expr (postfix_expression,
5489                                              s, TREE_TYPE (type));
5490         }
5491     }
5492
5493   if (!pseudo_destructor_p)
5494     {
5495       /* If the SCOPE is not a scalar type, we are looking at an
5496          ordinary class member access expression, rather than a
5497          pseudo-destructor-name.  */
5498       bool template_p;
5499       cp_token *token = cp_lexer_peek_token (parser->lexer);
5500       /* Parse the id-expression.  */
5501       name = (cp_parser_id_expression
5502               (parser,
5503                cp_parser_optional_template_keyword (parser),
5504                /*check_dependency_p=*/true,
5505                &template_p,
5506                /*declarator_p=*/false,
5507                /*optional_p=*/false));
5508       /* In general, build a SCOPE_REF if the member name is qualified.
5509          However, if the name was not dependent and has already been
5510          resolved; there is no need to build the SCOPE_REF.  For example;
5511
5512              struct X { void f(); };
5513              template <typename T> void f(T* t) { t->X::f(); }
5514
5515          Even though "t" is dependent, "X::f" is not and has been resolved
5516          to a BASELINK; there is no need to include scope information.  */
5517
5518       /* But we do need to remember that there was an explicit scope for
5519          virtual function calls.  */
5520       if (parser->scope)
5521         *idk = CP_ID_KIND_QUALIFIED;
5522
5523       /* If the name is a template-id that names a type, we will get a
5524          TYPE_DECL here.  That is invalid code.  */
5525       if (TREE_CODE (name) == TYPE_DECL)
5526         {
5527           error_at (token->location, "invalid use of %qD", name);
5528           postfix_expression = error_mark_node;
5529         }
5530       else
5531         {
5532           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5533             {
5534               name = build_qualified_name (/*type=*/NULL_TREE,
5535                                            parser->scope,
5536                                            name,
5537                                            template_p);
5538               parser->scope = NULL_TREE;
5539               parser->qualifying_scope = NULL_TREE;
5540               parser->object_scope = NULL_TREE;
5541             }
5542           if (scope && name && BASELINK_P (name))
5543             adjust_result_of_qualified_name_lookup
5544               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5545           postfix_expression
5546             = finish_class_member_access_expr (postfix_expression, name,
5547                                                template_p, 
5548                                                tf_warning_or_error);
5549         }
5550     }
5551
5552   /* We no longer need to look up names in the scope of the object on
5553      the left-hand side of the `.' or `->' operator.  */
5554   parser->context->object_type = NULL_TREE;
5555
5556   /* Outside of offsetof, these operators may not appear in
5557      constant-expressions.  */
5558   if (!for_offsetof
5559       && (cp_parser_non_integral_constant_expression
5560           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5561     postfix_expression = error_mark_node;
5562
5563   return postfix_expression;
5564 }
5565
5566 /* Parse a parenthesized expression-list.
5567
5568    expression-list:
5569      assignment-expression
5570      expression-list, assignment-expression
5571
5572    attribute-list:
5573      expression-list
5574      identifier
5575      identifier, expression-list
5576
5577    CAST_P is true if this expression is the target of a cast.
5578
5579    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5580    argument pack.
5581
5582    Returns a vector of trees.  Each element is a representation of an
5583    assignment-expression.  NULL is returned if the ( and or ) are
5584    missing.  An empty, but allocated, vector is returned on no
5585    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5586    if we are parsing an attribute list for an attribute that wants a
5587    plain identifier argument, normal_attr for an attribute that wants
5588    an expression, or non_attr if we aren't parsing an attribute list.  If
5589    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5590    not all of the expressions in the list were constant.  */
5591
5592 static VEC(tree,gc) *
5593 cp_parser_parenthesized_expression_list (cp_parser* parser,
5594                                          int is_attribute_list,
5595                                          bool cast_p,
5596                                          bool allow_expansion_p,
5597                                          bool *non_constant_p)
5598 {
5599   VEC(tree,gc) *expression_list;
5600   bool fold_expr_p = is_attribute_list != non_attr;
5601   tree identifier = NULL_TREE;
5602   bool saved_greater_than_is_operator_p;
5603
5604   /* Assume all the expressions will be constant.  */
5605   if (non_constant_p)
5606     *non_constant_p = false;
5607
5608   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5609     return NULL;
5610
5611   expression_list = make_tree_vector ();
5612
5613   /* Within a parenthesized expression, a `>' token is always
5614      the greater-than operator.  */
5615   saved_greater_than_is_operator_p
5616     = parser->greater_than_is_operator_p;
5617   parser->greater_than_is_operator_p = true;
5618
5619   /* Consume expressions until there are no more.  */
5620   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5621     while (true)
5622       {
5623         tree expr;
5624
5625         /* At the beginning of attribute lists, check to see if the
5626            next token is an identifier.  */
5627         if (is_attribute_list == id_attr
5628             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5629           {
5630             cp_token *token;
5631
5632             /* Consume the identifier.  */
5633             token = cp_lexer_consume_token (parser->lexer);
5634             /* Save the identifier.  */
5635             identifier = token->u.value;
5636           }
5637         else
5638           {
5639             bool expr_non_constant_p;
5640
5641             /* Parse the next assignment-expression.  */
5642             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5643               {
5644                 /* A braced-init-list.  */
5645                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5646                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5647                 if (non_constant_p && expr_non_constant_p)
5648                   *non_constant_p = true;
5649               }
5650             else if (non_constant_p)
5651               {
5652                 expr = (cp_parser_constant_expression
5653                         (parser, /*allow_non_constant_p=*/true,
5654                          &expr_non_constant_p));
5655                 if (expr_non_constant_p)
5656                   *non_constant_p = true;
5657               }
5658             else
5659               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5660
5661             if (fold_expr_p)
5662               expr = fold_non_dependent_expr (expr);
5663
5664             /* If we have an ellipsis, then this is an expression
5665                expansion.  */
5666             if (allow_expansion_p
5667                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5668               {
5669                 /* Consume the `...'.  */
5670                 cp_lexer_consume_token (parser->lexer);
5671
5672                 /* Build the argument pack.  */
5673                 expr = make_pack_expansion (expr);
5674               }
5675
5676              /* Add it to the list.  We add error_mark_node
5677                 expressions to the list, so that we can still tell if
5678                 the correct form for a parenthesized expression-list
5679                 is found. That gives better errors.  */
5680             VEC_safe_push (tree, gc, expression_list, expr);
5681
5682             if (expr == error_mark_node)
5683               goto skip_comma;
5684           }
5685
5686         /* After the first item, attribute lists look the same as
5687            expression lists.  */
5688         is_attribute_list = non_attr;
5689
5690       get_comma:;
5691         /* If the next token isn't a `,', then we are done.  */
5692         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5693           break;
5694
5695         /* Otherwise, consume the `,' and keep going.  */
5696         cp_lexer_consume_token (parser->lexer);
5697       }
5698
5699   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5700     {
5701       int ending;
5702
5703     skip_comma:;
5704       /* We try and resync to an unnested comma, as that will give the
5705          user better diagnostics.  */
5706       ending = cp_parser_skip_to_closing_parenthesis (parser,
5707                                                       /*recovering=*/true,
5708                                                       /*or_comma=*/true,
5709                                                       /*consume_paren=*/true);
5710       if (ending < 0)
5711         goto get_comma;
5712       if (!ending)
5713         {
5714           parser->greater_than_is_operator_p
5715             = saved_greater_than_is_operator_p;
5716           return NULL;
5717         }
5718     }
5719
5720   parser->greater_than_is_operator_p
5721     = saved_greater_than_is_operator_p;
5722
5723   if (identifier)
5724     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5725
5726   return expression_list;
5727 }
5728
5729 /* Parse a pseudo-destructor-name.
5730
5731    pseudo-destructor-name:
5732      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5733      :: [opt] nested-name-specifier template template-id :: ~ type-name
5734      :: [opt] nested-name-specifier [opt] ~ type-name
5735
5736    If either of the first two productions is used, sets *SCOPE to the
5737    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5738    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5739    or ERROR_MARK_NODE if the parse fails.  */
5740
5741 static void
5742 cp_parser_pseudo_destructor_name (cp_parser* parser,
5743                                   tree* scope,
5744                                   tree* type)
5745 {
5746   bool nested_name_specifier_p;
5747
5748   /* Assume that things will not work out.  */
5749   *type = error_mark_node;
5750
5751   /* Look for the optional `::' operator.  */
5752   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5753   /* Look for the optional nested-name-specifier.  */
5754   nested_name_specifier_p
5755     = (cp_parser_nested_name_specifier_opt (parser,
5756                                             /*typename_keyword_p=*/false,
5757                                             /*check_dependency_p=*/true,
5758                                             /*type_p=*/false,
5759                                             /*is_declaration=*/false)
5760        != NULL_TREE);
5761   /* Now, if we saw a nested-name-specifier, we might be doing the
5762      second production.  */
5763   if (nested_name_specifier_p
5764       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5765     {
5766       /* Consume the `template' keyword.  */
5767       cp_lexer_consume_token (parser->lexer);
5768       /* Parse the template-id.  */
5769       cp_parser_template_id (parser,
5770                              /*template_keyword_p=*/true,
5771                              /*check_dependency_p=*/false,
5772                              /*is_declaration=*/true);
5773       /* Look for the `::' token.  */
5774       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5775     }
5776   /* If the next token is not a `~', then there might be some
5777      additional qualification.  */
5778   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5779     {
5780       /* At this point, we're looking for "type-name :: ~".  The type-name
5781          must not be a class-name, since this is a pseudo-destructor.  So,
5782          it must be either an enum-name, or a typedef-name -- both of which
5783          are just identifiers.  So, we peek ahead to check that the "::"
5784          and "~" tokens are present; if they are not, then we can avoid
5785          calling type_name.  */
5786       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5787           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5788           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5789         {
5790           cp_parser_error (parser, "non-scalar type");
5791           return;
5792         }
5793
5794       /* Look for the type-name.  */
5795       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5796       if (*scope == error_mark_node)
5797         return;
5798
5799       /* Look for the `::' token.  */
5800       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5801     }
5802   else
5803     *scope = NULL_TREE;
5804
5805   /* Look for the `~'.  */
5806   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5807   /* Look for the type-name again.  We are not responsible for
5808      checking that it matches the first type-name.  */
5809   *type = cp_parser_nonclass_name (parser);
5810 }
5811
5812 /* Parse a unary-expression.
5813
5814    unary-expression:
5815      postfix-expression
5816      ++ cast-expression
5817      -- cast-expression
5818      unary-operator cast-expression
5819      sizeof unary-expression
5820      sizeof ( type-id )
5821      new-expression
5822      delete-expression
5823
5824    GNU Extensions:
5825
5826    unary-expression:
5827      __extension__ cast-expression
5828      __alignof__ unary-expression
5829      __alignof__ ( type-id )
5830      __real__ cast-expression
5831      __imag__ cast-expression
5832      && identifier
5833
5834    ADDRESS_P is true iff the unary-expression is appearing as the
5835    operand of the `&' operator.   CAST_P is true if this expression is
5836    the target of a cast.
5837
5838    Returns a representation of the expression.  */
5839
5840 static tree
5841 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5842                             cp_id_kind * pidk)
5843 {
5844   cp_token *token;
5845   enum tree_code unary_operator;
5846
5847   /* Peek at the next token.  */
5848   token = cp_lexer_peek_token (parser->lexer);
5849   /* Some keywords give away the kind of expression.  */
5850   if (token->type == CPP_KEYWORD)
5851     {
5852       enum rid keyword = token->keyword;
5853
5854       switch (keyword)
5855         {
5856         case RID_ALIGNOF:
5857         case RID_SIZEOF:
5858           {
5859             tree operand;
5860             enum tree_code op;
5861
5862             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5863             /* Consume the token.  */
5864             cp_lexer_consume_token (parser->lexer);
5865             /* Parse the operand.  */
5866             operand = cp_parser_sizeof_operand (parser, keyword);
5867
5868             if (TYPE_P (operand))
5869               return cxx_sizeof_or_alignof_type (operand, op, true);
5870             else
5871               return cxx_sizeof_or_alignof_expr (operand, op, true);
5872           }
5873
5874         case RID_NEW:
5875           return cp_parser_new_expression (parser);
5876
5877         case RID_DELETE:
5878           return cp_parser_delete_expression (parser);
5879
5880         case RID_EXTENSION:
5881           {
5882             /* The saved value of the PEDANTIC flag.  */
5883             int saved_pedantic;
5884             tree expr;
5885
5886             /* Save away the PEDANTIC flag.  */
5887             cp_parser_extension_opt (parser, &saved_pedantic);
5888             /* Parse the cast-expression.  */
5889             expr = cp_parser_simple_cast_expression (parser);
5890             /* Restore the PEDANTIC flag.  */
5891             pedantic = saved_pedantic;
5892
5893             return expr;
5894           }
5895
5896         case RID_REALPART:
5897         case RID_IMAGPART:
5898           {
5899             tree expression;
5900
5901             /* Consume the `__real__' or `__imag__' token.  */
5902             cp_lexer_consume_token (parser->lexer);
5903             /* Parse the cast-expression.  */
5904             expression = cp_parser_simple_cast_expression (parser);
5905             /* Create the complete representation.  */
5906             return build_x_unary_op ((keyword == RID_REALPART
5907                                       ? REALPART_EXPR : IMAGPART_EXPR),
5908                                      expression,
5909                                      tf_warning_or_error);
5910           }
5911           break;
5912
5913         case RID_NOEXCEPT:
5914           {
5915             tree expr;
5916             const char *saved_message;
5917             bool saved_integral_constant_expression_p;
5918             bool saved_non_integral_constant_expression_p;
5919             bool saved_greater_than_is_operator_p;
5920
5921             cp_lexer_consume_token (parser->lexer);
5922             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5923
5924             saved_message = parser->type_definition_forbidden_message;
5925             parser->type_definition_forbidden_message
5926               = G_("types may not be defined in %<noexcept%> expressions");
5927
5928             saved_integral_constant_expression_p
5929               = parser->integral_constant_expression_p;
5930             saved_non_integral_constant_expression_p
5931               = parser->non_integral_constant_expression_p;
5932             parser->integral_constant_expression_p = false;
5933
5934             saved_greater_than_is_operator_p
5935               = parser->greater_than_is_operator_p;
5936             parser->greater_than_is_operator_p = true;
5937
5938             ++cp_unevaluated_operand;
5939             ++c_inhibit_evaluation_warnings;
5940             expr = cp_parser_expression (parser, false, NULL);
5941             --c_inhibit_evaluation_warnings;
5942             --cp_unevaluated_operand;
5943
5944             parser->greater_than_is_operator_p
5945               = saved_greater_than_is_operator_p;
5946
5947             parser->integral_constant_expression_p
5948               = saved_integral_constant_expression_p;
5949             parser->non_integral_constant_expression_p
5950               = saved_non_integral_constant_expression_p;
5951
5952             parser->type_definition_forbidden_message = saved_message;
5953
5954             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5955             return finish_noexcept_expr (expr, tf_warning_or_error);
5956           }
5957
5958         default:
5959           break;
5960         }
5961     }
5962
5963   /* Look for the `:: new' and `:: delete', which also signal the
5964      beginning of a new-expression, or delete-expression,
5965      respectively.  If the next token is `::', then it might be one of
5966      these.  */
5967   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5968     {
5969       enum rid keyword;
5970
5971       /* See if the token after the `::' is one of the keywords in
5972          which we're interested.  */
5973       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5974       /* If it's `new', we have a new-expression.  */
5975       if (keyword == RID_NEW)
5976         return cp_parser_new_expression (parser);
5977       /* Similarly, for `delete'.  */
5978       else if (keyword == RID_DELETE)
5979         return cp_parser_delete_expression (parser);
5980     }
5981
5982   /* Look for a unary operator.  */
5983   unary_operator = cp_parser_unary_operator (token);
5984   /* The `++' and `--' operators can be handled similarly, even though
5985      they are not technically unary-operators in the grammar.  */
5986   if (unary_operator == ERROR_MARK)
5987     {
5988       if (token->type == CPP_PLUS_PLUS)
5989         unary_operator = PREINCREMENT_EXPR;
5990       else if (token->type == CPP_MINUS_MINUS)
5991         unary_operator = PREDECREMENT_EXPR;
5992       /* Handle the GNU address-of-label extension.  */
5993       else if (cp_parser_allow_gnu_extensions_p (parser)
5994                && token->type == CPP_AND_AND)
5995         {
5996           tree identifier;
5997           tree expression;
5998           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5999
6000           /* Consume the '&&' token.  */
6001           cp_lexer_consume_token (parser->lexer);
6002           /* Look for the identifier.  */
6003           identifier = cp_parser_identifier (parser);
6004           /* Create an expression representing the address.  */
6005           expression = finish_label_address_expr (identifier, loc);
6006           if (cp_parser_non_integral_constant_expression (parser,
6007                                                           NIC_ADDR_LABEL))
6008             expression = error_mark_node;
6009           return expression;
6010         }
6011     }
6012   if (unary_operator != ERROR_MARK)
6013     {
6014       tree cast_expression;
6015       tree expression = error_mark_node;
6016       non_integral_constant non_constant_p = NIC_NONE;
6017
6018       /* Consume the operator token.  */
6019       token = cp_lexer_consume_token (parser->lexer);
6020       /* Parse the cast-expression.  */
6021       cast_expression
6022         = cp_parser_cast_expression (parser,
6023                                      unary_operator == ADDR_EXPR,
6024                                      /*cast_p=*/false, pidk);
6025       /* Now, build an appropriate representation.  */
6026       switch (unary_operator)
6027         {
6028         case INDIRECT_REF:
6029           non_constant_p = NIC_STAR;
6030           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6031                                              tf_warning_or_error);
6032           break;
6033
6034         case ADDR_EXPR:
6035            non_constant_p = NIC_ADDR;
6036           /* Fall through.  */
6037         case BIT_NOT_EXPR:
6038           expression = build_x_unary_op (unary_operator, cast_expression,
6039                                          tf_warning_or_error);
6040           break;
6041
6042         case PREINCREMENT_EXPR:
6043         case PREDECREMENT_EXPR:
6044           non_constant_p = unary_operator == PREINCREMENT_EXPR
6045                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6046           /* Fall through.  */
6047         case UNARY_PLUS_EXPR:
6048         case NEGATE_EXPR:
6049         case TRUTH_NOT_EXPR:
6050           expression = finish_unary_op_expr (unary_operator, cast_expression);
6051           break;
6052
6053         default:
6054           gcc_unreachable ();
6055         }
6056
6057       if (non_constant_p != NIC_NONE
6058           && cp_parser_non_integral_constant_expression (parser,
6059                                                          non_constant_p))
6060         expression = error_mark_node;
6061
6062       return expression;
6063     }
6064
6065   return cp_parser_postfix_expression (parser, address_p, cast_p,
6066                                        /*member_access_only_p=*/false,
6067                                        pidk);
6068 }
6069
6070 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6071    unary-operator, the corresponding tree code is returned.  */
6072
6073 static enum tree_code
6074 cp_parser_unary_operator (cp_token* token)
6075 {
6076   switch (token->type)
6077     {
6078     case CPP_MULT:
6079       return INDIRECT_REF;
6080
6081     case CPP_AND:
6082       return ADDR_EXPR;
6083
6084     case CPP_PLUS:
6085       return UNARY_PLUS_EXPR;
6086
6087     case CPP_MINUS:
6088       return NEGATE_EXPR;
6089
6090     case CPP_NOT:
6091       return TRUTH_NOT_EXPR;
6092
6093     case CPP_COMPL:
6094       return BIT_NOT_EXPR;
6095
6096     default:
6097       return ERROR_MARK;
6098     }
6099 }
6100
6101 /* Parse a new-expression.
6102
6103    new-expression:
6104      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6105      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6106
6107    Returns a representation of the expression.  */
6108
6109 static tree
6110 cp_parser_new_expression (cp_parser* parser)
6111 {
6112   bool global_scope_p;
6113   VEC(tree,gc) *placement;
6114   tree type;
6115   VEC(tree,gc) *initializer;
6116   tree nelts;
6117   tree ret;
6118
6119   /* Look for the optional `::' operator.  */
6120   global_scope_p
6121     = (cp_parser_global_scope_opt (parser,
6122                                    /*current_scope_valid_p=*/false)
6123        != NULL_TREE);
6124   /* Look for the `new' operator.  */
6125   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6126   /* There's no easy way to tell a new-placement from the
6127      `( type-id )' construct.  */
6128   cp_parser_parse_tentatively (parser);
6129   /* Look for a new-placement.  */
6130   placement = cp_parser_new_placement (parser);
6131   /* If that didn't work out, there's no new-placement.  */
6132   if (!cp_parser_parse_definitely (parser))
6133     {
6134       if (placement != NULL)
6135         release_tree_vector (placement);
6136       placement = NULL;
6137     }
6138
6139   /* If the next token is a `(', then we have a parenthesized
6140      type-id.  */
6141   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6142     {
6143       cp_token *token;
6144       /* Consume the `('.  */
6145       cp_lexer_consume_token (parser->lexer);
6146       /* Parse the type-id.  */
6147       type = cp_parser_type_id (parser);
6148       /* Look for the closing `)'.  */
6149       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6150       token = cp_lexer_peek_token (parser->lexer);
6151       /* There should not be a direct-new-declarator in this production,
6152          but GCC used to allowed this, so we check and emit a sensible error
6153          message for this case.  */
6154       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6155         {
6156           error_at (token->location,
6157                     "array bound forbidden after parenthesized type-id");
6158           inform (token->location, 
6159                   "try removing the parentheses around the type-id");
6160           cp_parser_direct_new_declarator (parser);
6161         }
6162       nelts = NULL_TREE;
6163     }
6164   /* Otherwise, there must be a new-type-id.  */
6165   else
6166     type = cp_parser_new_type_id (parser, &nelts);
6167
6168   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6169   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6170       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6171     initializer = cp_parser_new_initializer (parser);
6172   else
6173     initializer = NULL;
6174
6175   /* A new-expression may not appear in an integral constant
6176      expression.  */
6177   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6178     ret = error_mark_node;
6179   else
6180     {
6181       /* Create a representation of the new-expression.  */
6182       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6183                        tf_warning_or_error);
6184     }
6185
6186   if (placement != NULL)
6187     release_tree_vector (placement);
6188   if (initializer != NULL)
6189     release_tree_vector (initializer);
6190
6191   return ret;
6192 }
6193
6194 /* Parse a new-placement.
6195
6196    new-placement:
6197      ( expression-list )
6198
6199    Returns the same representation as for an expression-list.  */
6200
6201 static VEC(tree,gc) *
6202 cp_parser_new_placement (cp_parser* parser)
6203 {
6204   VEC(tree,gc) *expression_list;
6205
6206   /* Parse the expression-list.  */
6207   expression_list = (cp_parser_parenthesized_expression_list
6208                      (parser, non_attr, /*cast_p=*/false,
6209                       /*allow_expansion_p=*/true,
6210                       /*non_constant_p=*/NULL));
6211
6212   return expression_list;
6213 }
6214
6215 /* Parse a new-type-id.
6216
6217    new-type-id:
6218      type-specifier-seq new-declarator [opt]
6219
6220    Returns the TYPE allocated.  If the new-type-id indicates an array
6221    type, *NELTS is set to the number of elements in the last array
6222    bound; the TYPE will not include the last array bound.  */
6223
6224 static tree
6225 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6226 {
6227   cp_decl_specifier_seq type_specifier_seq;
6228   cp_declarator *new_declarator;
6229   cp_declarator *declarator;
6230   cp_declarator *outer_declarator;
6231   const char *saved_message;
6232   tree type;
6233
6234   /* The type-specifier sequence must not contain type definitions.
6235      (It cannot contain declarations of new types either, but if they
6236      are not definitions we will catch that because they are not
6237      complete.)  */
6238   saved_message = parser->type_definition_forbidden_message;
6239   parser->type_definition_forbidden_message
6240     = G_("types may not be defined in a new-type-id");
6241   /* Parse the type-specifier-seq.  */
6242   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6243                                 /*is_trailing_return=*/false,
6244                                 &type_specifier_seq);
6245   /* Restore the old message.  */
6246   parser->type_definition_forbidden_message = saved_message;
6247   /* Parse the new-declarator.  */
6248   new_declarator = cp_parser_new_declarator_opt (parser);
6249
6250   /* Determine the number of elements in the last array dimension, if
6251      any.  */
6252   *nelts = NULL_TREE;
6253   /* Skip down to the last array dimension.  */
6254   declarator = new_declarator;
6255   outer_declarator = NULL;
6256   while (declarator && (declarator->kind == cdk_pointer
6257                         || declarator->kind == cdk_ptrmem))
6258     {
6259       outer_declarator = declarator;
6260       declarator = declarator->declarator;
6261     }
6262   while (declarator
6263          && declarator->kind == cdk_array
6264          && declarator->declarator
6265          && declarator->declarator->kind == cdk_array)
6266     {
6267       outer_declarator = declarator;
6268       declarator = declarator->declarator;
6269     }
6270
6271   if (declarator && declarator->kind == cdk_array)
6272     {
6273       *nelts = declarator->u.array.bounds;
6274       if (*nelts == error_mark_node)
6275         *nelts = integer_one_node;
6276
6277       if (outer_declarator)
6278         outer_declarator->declarator = declarator->declarator;
6279       else
6280         new_declarator = NULL;
6281     }
6282
6283   type = groktypename (&type_specifier_seq, new_declarator, false);
6284   return type;
6285 }
6286
6287 /* Parse an (optional) new-declarator.
6288
6289    new-declarator:
6290      ptr-operator new-declarator [opt]
6291      direct-new-declarator
6292
6293    Returns the declarator.  */
6294
6295 static cp_declarator *
6296 cp_parser_new_declarator_opt (cp_parser* parser)
6297 {
6298   enum tree_code code;
6299   tree type;
6300   cp_cv_quals cv_quals;
6301
6302   /* We don't know if there's a ptr-operator next, or not.  */
6303   cp_parser_parse_tentatively (parser);
6304   /* Look for a ptr-operator.  */
6305   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6306   /* If that worked, look for more new-declarators.  */
6307   if (cp_parser_parse_definitely (parser))
6308     {
6309       cp_declarator *declarator;
6310
6311       /* Parse another optional declarator.  */
6312       declarator = cp_parser_new_declarator_opt (parser);
6313
6314       return cp_parser_make_indirect_declarator
6315         (code, type, cv_quals, declarator);
6316     }
6317
6318   /* If the next token is a `[', there is a direct-new-declarator.  */
6319   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6320     return cp_parser_direct_new_declarator (parser);
6321
6322   return NULL;
6323 }
6324
6325 /* Parse a direct-new-declarator.
6326
6327    direct-new-declarator:
6328      [ expression ]
6329      direct-new-declarator [constant-expression]
6330
6331    */
6332
6333 static cp_declarator *
6334 cp_parser_direct_new_declarator (cp_parser* parser)
6335 {
6336   cp_declarator *declarator = NULL;
6337
6338   while (true)
6339     {
6340       tree expression;
6341
6342       /* Look for the opening `['.  */
6343       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6344       /* The first expression is not required to be constant.  */
6345       if (!declarator)
6346         {
6347           cp_token *token = cp_lexer_peek_token (parser->lexer);
6348           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6349           /* The standard requires that the expression have integral
6350              type.  DR 74 adds enumeration types.  We believe that the
6351              real intent is that these expressions be handled like the
6352              expression in a `switch' condition, which also allows
6353              classes with a single conversion to integral or
6354              enumeration type.  */
6355           if (!processing_template_decl)
6356             {
6357               expression
6358                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6359                                               expression,
6360                                               /*complain=*/true);
6361               if (!expression)
6362                 {
6363                   error_at (token->location,
6364                             "expression in new-declarator must have integral "
6365                             "or enumeration type");
6366                   expression = error_mark_node;
6367                 }
6368             }
6369         }
6370       /* But all the other expressions must be.  */
6371       else
6372         expression
6373           = cp_parser_constant_expression (parser,
6374                                            /*allow_non_constant=*/false,
6375                                            NULL);
6376       /* Look for the closing `]'.  */
6377       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6378
6379       /* Add this bound to the declarator.  */
6380       declarator = make_array_declarator (declarator, expression);
6381
6382       /* If the next token is not a `[', then there are no more
6383          bounds.  */
6384       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6385         break;
6386     }
6387
6388   return declarator;
6389 }
6390
6391 /* Parse a new-initializer.
6392
6393    new-initializer:
6394      ( expression-list [opt] )
6395      braced-init-list
6396
6397    Returns a representation of the expression-list.  */
6398
6399 static VEC(tree,gc) *
6400 cp_parser_new_initializer (cp_parser* parser)
6401 {
6402   VEC(tree,gc) *expression_list;
6403
6404   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6405     {
6406       tree t;
6407       bool expr_non_constant_p;
6408       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6409       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6410       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6411       expression_list = make_tree_vector_single (t);
6412     }
6413   else
6414     expression_list = (cp_parser_parenthesized_expression_list
6415                        (parser, non_attr, /*cast_p=*/false,
6416                         /*allow_expansion_p=*/true,
6417                         /*non_constant_p=*/NULL));
6418
6419   return expression_list;
6420 }
6421
6422 /* Parse a delete-expression.
6423
6424    delete-expression:
6425      :: [opt] delete cast-expression
6426      :: [opt] delete [ ] cast-expression
6427
6428    Returns a representation of the expression.  */
6429
6430 static tree
6431 cp_parser_delete_expression (cp_parser* parser)
6432 {
6433   bool global_scope_p;
6434   bool array_p;
6435   tree expression;
6436
6437   /* Look for the optional `::' operator.  */
6438   global_scope_p
6439     = (cp_parser_global_scope_opt (parser,
6440                                    /*current_scope_valid_p=*/false)
6441        != NULL_TREE);
6442   /* Look for the `delete' keyword.  */
6443   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6444   /* See if the array syntax is in use.  */
6445   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6446     {
6447       /* Consume the `[' token.  */
6448       cp_lexer_consume_token (parser->lexer);
6449       /* Look for the `]' token.  */
6450       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6451       /* Remember that this is the `[]' construct.  */
6452       array_p = true;
6453     }
6454   else
6455     array_p = false;
6456
6457   /* Parse the cast-expression.  */
6458   expression = cp_parser_simple_cast_expression (parser);
6459
6460   /* A delete-expression may not appear in an integral constant
6461      expression.  */
6462   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6463     return error_mark_node;
6464
6465   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6466 }
6467
6468 /* Returns true if TOKEN may start a cast-expression and false
6469    otherwise.  */
6470
6471 static bool
6472 cp_parser_token_starts_cast_expression (cp_token *token)
6473 {
6474   switch (token->type)
6475     {
6476     case CPP_COMMA:
6477     case CPP_SEMICOLON:
6478     case CPP_QUERY:
6479     case CPP_COLON:
6480     case CPP_CLOSE_SQUARE:
6481     case CPP_CLOSE_PAREN:
6482     case CPP_CLOSE_BRACE:
6483     case CPP_DOT:
6484     case CPP_DOT_STAR:
6485     case CPP_DEREF:
6486     case CPP_DEREF_STAR:
6487     case CPP_DIV:
6488     case CPP_MOD:
6489     case CPP_LSHIFT:
6490     case CPP_RSHIFT:
6491     case CPP_LESS:
6492     case CPP_GREATER:
6493     case CPP_LESS_EQ:
6494     case CPP_GREATER_EQ:
6495     case CPP_EQ_EQ:
6496     case CPP_NOT_EQ:
6497     case CPP_EQ:
6498     case CPP_MULT_EQ:
6499     case CPP_DIV_EQ:
6500     case CPP_MOD_EQ:
6501     case CPP_PLUS_EQ:
6502     case CPP_MINUS_EQ:
6503     case CPP_RSHIFT_EQ:
6504     case CPP_LSHIFT_EQ:
6505     case CPP_AND_EQ:
6506     case CPP_XOR_EQ:
6507     case CPP_OR_EQ:
6508     case CPP_XOR:
6509     case CPP_OR:
6510     case CPP_OR_OR:
6511     case CPP_EOF:
6512       return false;
6513
6514       /* '[' may start a primary-expression in obj-c++.  */
6515     case CPP_OPEN_SQUARE:
6516       return c_dialect_objc ();
6517
6518     default:
6519       return true;
6520     }
6521 }
6522
6523 /* Parse a cast-expression.
6524
6525    cast-expression:
6526      unary-expression
6527      ( type-id ) cast-expression
6528
6529    ADDRESS_P is true iff the unary-expression is appearing as the
6530    operand of the `&' operator.   CAST_P is true if this expression is
6531    the target of a cast.
6532
6533    Returns a representation of the expression.  */
6534
6535 static tree
6536 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6537                            cp_id_kind * pidk)
6538 {
6539   /* If it's a `(', then we might be looking at a cast.  */
6540   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6541     {
6542       tree type = NULL_TREE;
6543       tree expr = NULL_TREE;
6544       bool compound_literal_p;
6545       const char *saved_message;
6546
6547       /* There's no way to know yet whether or not this is a cast.
6548          For example, `(int (3))' is a unary-expression, while `(int)
6549          3' is a cast.  So, we resort to parsing tentatively.  */
6550       cp_parser_parse_tentatively (parser);
6551       /* Types may not be defined in a cast.  */
6552       saved_message = parser->type_definition_forbidden_message;
6553       parser->type_definition_forbidden_message
6554         = G_("types may not be defined in casts");
6555       /* Consume the `('.  */
6556       cp_lexer_consume_token (parser->lexer);
6557       /* A very tricky bit is that `(struct S) { 3 }' is a
6558          compound-literal (which we permit in C++ as an extension).
6559          But, that construct is not a cast-expression -- it is a
6560          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6561          is legal; if the compound-literal were a cast-expression,
6562          you'd need an extra set of parentheses.)  But, if we parse
6563          the type-id, and it happens to be a class-specifier, then we
6564          will commit to the parse at that point, because we cannot
6565          undo the action that is done when creating a new class.  So,
6566          then we cannot back up and do a postfix-expression.
6567
6568          Therefore, we scan ahead to the closing `)', and check to see
6569          if the token after the `)' is a `{'.  If so, we are not
6570          looking at a cast-expression.
6571
6572          Save tokens so that we can put them back.  */
6573       cp_lexer_save_tokens (parser->lexer);
6574       /* Skip tokens until the next token is a closing parenthesis.
6575          If we find the closing `)', and the next token is a `{', then
6576          we are looking at a compound-literal.  */
6577       compound_literal_p
6578         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6579                                                   /*consume_paren=*/true)
6580            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6581       /* Roll back the tokens we skipped.  */
6582       cp_lexer_rollback_tokens (parser->lexer);
6583       /* If we were looking at a compound-literal, simulate an error
6584          so that the call to cp_parser_parse_definitely below will
6585          fail.  */
6586       if (compound_literal_p)
6587         cp_parser_simulate_error (parser);
6588       else
6589         {
6590           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6591           parser->in_type_id_in_expr_p = true;
6592           /* Look for the type-id.  */
6593           type = cp_parser_type_id (parser);
6594           /* Look for the closing `)'.  */
6595           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6596           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6597         }
6598
6599       /* Restore the saved message.  */
6600       parser->type_definition_forbidden_message = saved_message;
6601
6602       /* At this point this can only be either a cast or a
6603          parenthesized ctor such as `(T ())' that looks like a cast to
6604          function returning T.  */
6605       if (!cp_parser_error_occurred (parser)
6606           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6607                                                      (parser->lexer)))
6608         {
6609           cp_parser_parse_definitely (parser);
6610           expr = cp_parser_cast_expression (parser,
6611                                             /*address_p=*/false,
6612                                             /*cast_p=*/true, pidk);
6613
6614           /* Warn about old-style casts, if so requested.  */
6615           if (warn_old_style_cast
6616               && !in_system_header
6617               && !VOID_TYPE_P (type)
6618               && current_lang_name != lang_name_c)
6619             warning (OPT_Wold_style_cast, "use of old-style cast");
6620
6621           /* Only type conversions to integral or enumeration types
6622              can be used in constant-expressions.  */
6623           if (!cast_valid_in_integral_constant_expression_p (type)
6624               && cp_parser_non_integral_constant_expression (parser,
6625                                                              NIC_CAST))
6626             return error_mark_node;
6627
6628           /* Perform the cast.  */
6629           expr = build_c_cast (input_location, type, expr);
6630           return expr;
6631         }
6632       else 
6633         cp_parser_abort_tentative_parse (parser);
6634     }
6635
6636   /* If we get here, then it's not a cast, so it must be a
6637      unary-expression.  */
6638   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6639 }
6640
6641 /* Parse a binary expression of the general form:
6642
6643    pm-expression:
6644      cast-expression
6645      pm-expression .* cast-expression
6646      pm-expression ->* cast-expression
6647
6648    multiplicative-expression:
6649      pm-expression
6650      multiplicative-expression * pm-expression
6651      multiplicative-expression / pm-expression
6652      multiplicative-expression % pm-expression
6653
6654    additive-expression:
6655      multiplicative-expression
6656      additive-expression + multiplicative-expression
6657      additive-expression - multiplicative-expression
6658
6659    shift-expression:
6660      additive-expression
6661      shift-expression << additive-expression
6662      shift-expression >> additive-expression
6663
6664    relational-expression:
6665      shift-expression
6666      relational-expression < shift-expression
6667      relational-expression > shift-expression
6668      relational-expression <= shift-expression
6669      relational-expression >= shift-expression
6670
6671   GNU Extension:
6672
6673    relational-expression:
6674      relational-expression <? shift-expression
6675      relational-expression >? shift-expression
6676
6677    equality-expression:
6678      relational-expression
6679      equality-expression == relational-expression
6680      equality-expression != relational-expression
6681
6682    and-expression:
6683      equality-expression
6684      and-expression & equality-expression
6685
6686    exclusive-or-expression:
6687      and-expression
6688      exclusive-or-expression ^ and-expression
6689
6690    inclusive-or-expression:
6691      exclusive-or-expression
6692      inclusive-or-expression | exclusive-or-expression
6693
6694    logical-and-expression:
6695      inclusive-or-expression
6696      logical-and-expression && inclusive-or-expression
6697
6698    logical-or-expression:
6699      logical-and-expression
6700      logical-or-expression || logical-and-expression
6701
6702    All these are implemented with a single function like:
6703
6704    binary-expression:
6705      simple-cast-expression
6706      binary-expression <token> binary-expression
6707
6708    CAST_P is true if this expression is the target of a cast.
6709
6710    The binops_by_token map is used to get the tree codes for each <token> type.
6711    binary-expressions are associated according to a precedence table.  */
6712
6713 #define TOKEN_PRECEDENCE(token)                              \
6714 (((token->type == CPP_GREATER                                \
6715    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6716   && !parser->greater_than_is_operator_p)                    \
6717  ? PREC_NOT_OPERATOR                                         \
6718  : binops_by_token[token->type].prec)
6719
6720 static tree
6721 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6722                              bool no_toplevel_fold_p,
6723                              enum cp_parser_prec prec,
6724                              cp_id_kind * pidk)
6725 {
6726   cp_parser_expression_stack stack;
6727   cp_parser_expression_stack_entry *sp = &stack[0];
6728   tree lhs, rhs;
6729   cp_token *token;
6730   enum tree_code tree_type, lhs_type, rhs_type;
6731   enum cp_parser_prec new_prec, lookahead_prec;
6732   bool overloaded_p;
6733
6734   /* Parse the first expression.  */
6735   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6736   lhs_type = ERROR_MARK;
6737
6738   for (;;)
6739     {
6740       /* Get an operator token.  */
6741       token = cp_lexer_peek_token (parser->lexer);
6742
6743       if (warn_cxx0x_compat
6744           && token->type == CPP_RSHIFT
6745           && !parser->greater_than_is_operator_p)
6746         {
6747           if (warning_at (token->location, OPT_Wc__0x_compat, 
6748                           "%<>>%> operator will be treated as"
6749                           " two right angle brackets in C++0x"))
6750             inform (token->location,
6751                     "suggest parentheses around %<>>%> expression");
6752         }
6753
6754       new_prec = TOKEN_PRECEDENCE (token);
6755
6756       /* Popping an entry off the stack means we completed a subexpression:
6757          - either we found a token which is not an operator (`>' where it is not
6758            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6759            will happen repeatedly;
6760          - or, we found an operator which has lower priority.  This is the case
6761            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6762            parsing `3 * 4'.  */
6763       if (new_prec <= prec)
6764         {
6765           if (sp == stack)
6766             break;
6767           else
6768             goto pop;
6769         }
6770
6771      get_rhs:
6772       tree_type = binops_by_token[token->type].tree_type;
6773
6774       /* We used the operator token.  */
6775       cp_lexer_consume_token (parser->lexer);
6776
6777       /* For "false && x" or "true || x", x will never be executed;
6778          disable warnings while evaluating it.  */
6779       if (tree_type == TRUTH_ANDIF_EXPR)
6780         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6781       else if (tree_type == TRUTH_ORIF_EXPR)
6782         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6783
6784       /* Extract another operand.  It may be the RHS of this expression
6785          or the LHS of a new, higher priority expression.  */
6786       rhs = cp_parser_simple_cast_expression (parser);
6787       rhs_type = ERROR_MARK;
6788
6789       /* Get another operator token.  Look up its precedence to avoid
6790          building a useless (immediately popped) stack entry for common
6791          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6792       token = cp_lexer_peek_token (parser->lexer);
6793       lookahead_prec = TOKEN_PRECEDENCE (token);
6794       if (lookahead_prec > new_prec)
6795         {
6796           /* ... and prepare to parse the RHS of the new, higher priority
6797              expression.  Since precedence levels on the stack are
6798              monotonically increasing, we do not have to care about
6799              stack overflows.  */
6800           sp->prec = prec;
6801           sp->tree_type = tree_type;
6802           sp->lhs = lhs;
6803           sp->lhs_type = lhs_type;
6804           sp++;
6805           lhs = rhs;
6806           lhs_type = rhs_type;
6807           prec = new_prec;
6808           new_prec = lookahead_prec;
6809           goto get_rhs;
6810
6811          pop:
6812           lookahead_prec = new_prec;
6813           /* If the stack is not empty, we have parsed into LHS the right side
6814              (`4' in the example above) of an expression we had suspended.
6815              We can use the information on the stack to recover the LHS (`3')
6816              from the stack together with the tree code (`MULT_EXPR'), and
6817              the precedence of the higher level subexpression
6818              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6819              which will be used to actually build the additive expression.  */
6820           --sp;
6821           prec = sp->prec;
6822           tree_type = sp->tree_type;
6823           rhs = lhs;
6824           rhs_type = lhs_type;
6825           lhs = sp->lhs;
6826           lhs_type = sp->lhs_type;
6827         }
6828
6829       /* Undo the disabling of warnings done above.  */
6830       if (tree_type == TRUTH_ANDIF_EXPR)
6831         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6832       else if (tree_type == TRUTH_ORIF_EXPR)
6833         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6834
6835       overloaded_p = false;
6836       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6837          ERROR_MARK for everything that is not a binary expression.
6838          This makes warn_about_parentheses miss some warnings that
6839          involve unary operators.  For unary expressions we should
6840          pass the correct tree_code unless the unary expression was
6841          surrounded by parentheses.
6842       */
6843       if (no_toplevel_fold_p
6844           && lookahead_prec <= prec
6845           && sp == stack
6846           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6847         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6848       else
6849         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6850                                  &overloaded_p, tf_warning_or_error);
6851       lhs_type = tree_type;
6852
6853       /* If the binary operator required the use of an overloaded operator,
6854          then this expression cannot be an integral constant-expression.
6855          An overloaded operator can be used even if both operands are
6856          otherwise permissible in an integral constant-expression if at
6857          least one of the operands is of enumeration type.  */
6858
6859       if (overloaded_p
6860           && cp_parser_non_integral_constant_expression (parser,
6861                                                          NIC_OVERLOADED))
6862         return error_mark_node;
6863     }
6864
6865   return lhs;
6866 }
6867
6868
6869 /* Parse the `? expression : assignment-expression' part of a
6870    conditional-expression.  The LOGICAL_OR_EXPR is the
6871    logical-or-expression that started the conditional-expression.
6872    Returns a representation of the entire conditional-expression.
6873
6874    This routine is used by cp_parser_assignment_expression.
6875
6876      ? expression : assignment-expression
6877
6878    GNU Extensions:
6879
6880      ? : assignment-expression */
6881
6882 static tree
6883 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6884 {
6885   tree expr;
6886   tree assignment_expr;
6887   struct cp_token *token;
6888
6889   /* Consume the `?' token.  */
6890   cp_lexer_consume_token (parser->lexer);
6891   token = cp_lexer_peek_token (parser->lexer);
6892   if (cp_parser_allow_gnu_extensions_p (parser)
6893       && token->type == CPP_COLON)
6894     {
6895       pedwarn (token->location, OPT_pedantic, 
6896                "ISO C++ does not allow ?: with omitted middle operand");
6897       /* Implicit true clause.  */
6898       expr = NULL_TREE;
6899       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6900       warn_for_omitted_condop (token->location, logical_or_expr);
6901     }
6902   else
6903     {
6904       /* Parse the expression.  */
6905       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6906       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6907       c_inhibit_evaluation_warnings +=
6908         ((logical_or_expr == truthvalue_true_node)
6909          - (logical_or_expr == truthvalue_false_node));
6910     }
6911
6912   /* The next token should be a `:'.  */
6913   cp_parser_require (parser, CPP_COLON, RT_COLON);
6914   /* Parse the assignment-expression.  */
6915   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6916   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6917
6918   /* Build the conditional-expression.  */
6919   return build_x_conditional_expr (logical_or_expr,
6920                                    expr,
6921                                    assignment_expr,
6922                                    tf_warning_or_error);
6923 }
6924
6925 /* Parse an assignment-expression.
6926
6927    assignment-expression:
6928      conditional-expression
6929      logical-or-expression assignment-operator assignment_expression
6930      throw-expression
6931
6932    CAST_P is true if this expression is the target of a cast.
6933
6934    Returns a representation for the expression.  */
6935
6936 static tree
6937 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6938                                  cp_id_kind * pidk)
6939 {
6940   tree expr;
6941
6942   /* If the next token is the `throw' keyword, then we're looking at
6943      a throw-expression.  */
6944   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6945     expr = cp_parser_throw_expression (parser);
6946   /* Otherwise, it must be that we are looking at a
6947      logical-or-expression.  */
6948   else
6949     {
6950       /* Parse the binary expressions (logical-or-expression).  */
6951       expr = cp_parser_binary_expression (parser, cast_p, false,
6952                                           PREC_NOT_OPERATOR, pidk);
6953       /* If the next token is a `?' then we're actually looking at a
6954          conditional-expression.  */
6955       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6956         return cp_parser_question_colon_clause (parser, expr);
6957       else
6958         {
6959           enum tree_code assignment_operator;
6960
6961           /* If it's an assignment-operator, we're using the second
6962              production.  */
6963           assignment_operator
6964             = cp_parser_assignment_operator_opt (parser);
6965           if (assignment_operator != ERROR_MARK)
6966             {
6967               bool non_constant_p;
6968
6969               /* Parse the right-hand side of the assignment.  */
6970               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6971
6972               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6973                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6974
6975               /* An assignment may not appear in a
6976                  constant-expression.  */
6977               if (cp_parser_non_integral_constant_expression (parser,
6978                                                               NIC_ASSIGNMENT))
6979                 return error_mark_node;
6980               /* Build the assignment expression.  */
6981               expr = build_x_modify_expr (expr,
6982                                           assignment_operator,
6983                                           rhs,
6984                                           tf_warning_or_error);
6985             }
6986         }
6987     }
6988
6989   return expr;
6990 }
6991
6992 /* Parse an (optional) assignment-operator.
6993
6994    assignment-operator: one of
6995      = *= /= %= += -= >>= <<= &= ^= |=
6996
6997    GNU Extension:
6998
6999    assignment-operator: one of
7000      <?= >?=
7001
7002    If the next token is an assignment operator, the corresponding tree
7003    code is returned, and the token is consumed.  For example, for
7004    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7005    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7006    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7007    operator, ERROR_MARK is returned.  */
7008
7009 static enum tree_code
7010 cp_parser_assignment_operator_opt (cp_parser* parser)
7011 {
7012   enum tree_code op;
7013   cp_token *token;
7014
7015   /* Peek at the next token.  */
7016   token = cp_lexer_peek_token (parser->lexer);
7017
7018   switch (token->type)
7019     {
7020     case CPP_EQ:
7021       op = NOP_EXPR;
7022       break;
7023
7024     case CPP_MULT_EQ:
7025       op = MULT_EXPR;
7026       break;
7027
7028     case CPP_DIV_EQ:
7029       op = TRUNC_DIV_EXPR;
7030       break;
7031
7032     case CPP_MOD_EQ:
7033       op = TRUNC_MOD_EXPR;
7034       break;
7035
7036     case CPP_PLUS_EQ:
7037       op = PLUS_EXPR;
7038       break;
7039
7040     case CPP_MINUS_EQ:
7041       op = MINUS_EXPR;
7042       break;
7043
7044     case CPP_RSHIFT_EQ:
7045       op = RSHIFT_EXPR;
7046       break;
7047
7048     case CPP_LSHIFT_EQ:
7049       op = LSHIFT_EXPR;
7050       break;
7051
7052     case CPP_AND_EQ:
7053       op = BIT_AND_EXPR;
7054       break;
7055
7056     case CPP_XOR_EQ:
7057       op = BIT_XOR_EXPR;
7058       break;
7059
7060     case CPP_OR_EQ:
7061       op = BIT_IOR_EXPR;
7062       break;
7063
7064     default:
7065       /* Nothing else is an assignment operator.  */
7066       op = ERROR_MARK;
7067     }
7068
7069   /* If it was an assignment operator, consume it.  */
7070   if (op != ERROR_MARK)
7071     cp_lexer_consume_token (parser->lexer);
7072
7073   return op;
7074 }
7075
7076 /* Parse an expression.
7077
7078    expression:
7079      assignment-expression
7080      expression , assignment-expression
7081
7082    CAST_P is true if this expression is the target of a cast.
7083
7084    Returns a representation of the expression.  */
7085
7086 static tree
7087 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7088 {
7089   tree expression = NULL_TREE;
7090
7091   while (true)
7092     {
7093       tree assignment_expression;
7094
7095       /* Parse the next assignment-expression.  */
7096       assignment_expression
7097         = cp_parser_assignment_expression (parser, cast_p, pidk);
7098       /* If this is the first assignment-expression, we can just
7099          save it away.  */
7100       if (!expression)
7101         expression = assignment_expression;
7102       else
7103         expression = build_x_compound_expr (expression,
7104                                             assignment_expression,
7105                                             tf_warning_or_error);
7106       /* If the next token is not a comma, then we are done with the
7107          expression.  */
7108       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7109         break;
7110       /* Consume the `,'.  */
7111       cp_lexer_consume_token (parser->lexer);
7112       /* A comma operator cannot appear in a constant-expression.  */
7113       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7114         expression = error_mark_node;
7115     }
7116
7117   return expression;
7118 }
7119
7120 /* Parse a constant-expression.
7121
7122    constant-expression:
7123      conditional-expression
7124
7125   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7126   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7127   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7128   is false, NON_CONSTANT_P should be NULL.  */
7129
7130 static tree
7131 cp_parser_constant_expression (cp_parser* parser,
7132                                bool allow_non_constant_p,
7133                                bool *non_constant_p)
7134 {
7135   bool saved_integral_constant_expression_p;
7136   bool saved_allow_non_integral_constant_expression_p;
7137   bool saved_non_integral_constant_expression_p;
7138   tree expression;
7139
7140   /* It might seem that we could simply parse the
7141      conditional-expression, and then check to see if it were
7142      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7143      one that the compiler can figure out is constant, possibly after
7144      doing some simplifications or optimizations.  The standard has a
7145      precise definition of constant-expression, and we must honor
7146      that, even though it is somewhat more restrictive.
7147
7148      For example:
7149
7150        int i[(2, 3)];
7151
7152      is not a legal declaration, because `(2, 3)' is not a
7153      constant-expression.  The `,' operator is forbidden in a
7154      constant-expression.  However, GCC's constant-folding machinery
7155      will fold this operation to an INTEGER_CST for `3'.  */
7156
7157   /* Save the old settings.  */
7158   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7159   saved_allow_non_integral_constant_expression_p
7160     = parser->allow_non_integral_constant_expression_p;
7161   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7162   /* We are now parsing a constant-expression.  */
7163   parser->integral_constant_expression_p = true;
7164   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
7165   parser->non_integral_constant_expression_p = false;
7166   /* Although the grammar says "conditional-expression", we parse an
7167      "assignment-expression", which also permits "throw-expression"
7168      and the use of assignment operators.  In the case that
7169      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7170      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7171      actually essential that we look for an assignment-expression.
7172      For example, cp_parser_initializer_clauses uses this function to
7173      determine whether a particular assignment-expression is in fact
7174      constant.  */
7175   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7176   /* Restore the old settings.  */
7177   parser->integral_constant_expression_p
7178     = saved_integral_constant_expression_p;
7179   parser->allow_non_integral_constant_expression_p
7180     = saved_allow_non_integral_constant_expression_p;
7181   if (allow_non_constant_p)
7182     *non_constant_p = parser->non_integral_constant_expression_p;
7183   else if (parser->non_integral_constant_expression_p)
7184     expression = error_mark_node;
7185   parser->non_integral_constant_expression_p
7186     = saved_non_integral_constant_expression_p;
7187
7188   return expression;
7189 }
7190
7191 /* Parse __builtin_offsetof.
7192
7193    offsetof-expression:
7194      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7195
7196    offsetof-member-designator:
7197      id-expression
7198      | offsetof-member-designator "." id-expression
7199      | offsetof-member-designator "[" expression "]"
7200      | offsetof-member-designator "->" id-expression  */
7201
7202 static tree
7203 cp_parser_builtin_offsetof (cp_parser *parser)
7204 {
7205   int save_ice_p, save_non_ice_p;
7206   tree type, expr;
7207   cp_id_kind dummy;
7208   cp_token *token;
7209
7210   /* We're about to accept non-integral-constant things, but will
7211      definitely yield an integral constant expression.  Save and
7212      restore these values around our local parsing.  */
7213   save_ice_p = parser->integral_constant_expression_p;
7214   save_non_ice_p = parser->non_integral_constant_expression_p;
7215
7216   /* Consume the "__builtin_offsetof" token.  */
7217   cp_lexer_consume_token (parser->lexer);
7218   /* Consume the opening `('.  */
7219   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7220   /* Parse the type-id.  */
7221   type = cp_parser_type_id (parser);
7222   /* Look for the `,'.  */
7223   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7224   token = cp_lexer_peek_token (parser->lexer);
7225
7226   /* Build the (type *)null that begins the traditional offsetof macro.  */
7227   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7228                             tf_warning_or_error);
7229
7230   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7231   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7232                                                  true, &dummy, token->location);
7233   while (true)
7234     {
7235       token = cp_lexer_peek_token (parser->lexer);
7236       switch (token->type)
7237         {
7238         case CPP_OPEN_SQUARE:
7239           /* offsetof-member-designator "[" expression "]" */
7240           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7241           break;
7242
7243         case CPP_DEREF:
7244           /* offsetof-member-designator "->" identifier */
7245           expr = grok_array_decl (expr, integer_zero_node);
7246           /* FALLTHRU */
7247
7248         case CPP_DOT:
7249           /* offsetof-member-designator "." identifier */
7250           cp_lexer_consume_token (parser->lexer);
7251           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7252                                                          expr, true, &dummy,
7253                                                          token->location);
7254           break;
7255
7256         case CPP_CLOSE_PAREN:
7257           /* Consume the ")" token.  */
7258           cp_lexer_consume_token (parser->lexer);
7259           goto success;
7260
7261         default:
7262           /* Error.  We know the following require will fail, but
7263              that gives the proper error message.  */
7264           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7265           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7266           expr = error_mark_node;
7267           goto failure;
7268         }
7269     }
7270
7271  success:
7272   /* If we're processing a template, we can't finish the semantics yet.
7273      Otherwise we can fold the entire expression now.  */
7274   if (processing_template_decl)
7275     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7276   else
7277     expr = finish_offsetof (expr);
7278
7279  failure:
7280   parser->integral_constant_expression_p = save_ice_p;
7281   parser->non_integral_constant_expression_p = save_non_ice_p;
7282
7283   return expr;
7284 }
7285
7286 /* Parse a trait expression.  */
7287
7288 static tree
7289 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7290 {
7291   cp_trait_kind kind;
7292   tree type1, type2 = NULL_TREE;
7293   bool binary = false;
7294   cp_decl_specifier_seq decl_specs;
7295
7296   switch (keyword)
7297     {
7298     case RID_HAS_NOTHROW_ASSIGN:
7299       kind = CPTK_HAS_NOTHROW_ASSIGN;
7300       break;
7301     case RID_HAS_NOTHROW_CONSTRUCTOR:
7302       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7303       break;
7304     case RID_HAS_NOTHROW_COPY:
7305       kind = CPTK_HAS_NOTHROW_COPY;
7306       break;
7307     case RID_HAS_TRIVIAL_ASSIGN:
7308       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7309       break;
7310     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7311       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7312       break;
7313     case RID_HAS_TRIVIAL_COPY:
7314       kind = CPTK_HAS_TRIVIAL_COPY;
7315       break;
7316     case RID_HAS_TRIVIAL_DESTRUCTOR:
7317       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7318       break;
7319     case RID_HAS_VIRTUAL_DESTRUCTOR:
7320       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7321       break;
7322     case RID_IS_ABSTRACT:
7323       kind = CPTK_IS_ABSTRACT;
7324       break;
7325     case RID_IS_BASE_OF:
7326       kind = CPTK_IS_BASE_OF;
7327       binary = true;
7328       break;
7329     case RID_IS_CLASS:
7330       kind = CPTK_IS_CLASS;
7331       break;
7332     case RID_IS_CONVERTIBLE_TO:
7333       kind = CPTK_IS_CONVERTIBLE_TO;
7334       binary = true;
7335       break;
7336     case RID_IS_EMPTY:
7337       kind = CPTK_IS_EMPTY;
7338       break;
7339     case RID_IS_ENUM:
7340       kind = CPTK_IS_ENUM;
7341       break;
7342     case RID_IS_POD:
7343       kind = CPTK_IS_POD;
7344       break;
7345     case RID_IS_POLYMORPHIC:
7346       kind = CPTK_IS_POLYMORPHIC;
7347       break;
7348     case RID_IS_STD_LAYOUT:
7349       kind = CPTK_IS_STD_LAYOUT;
7350       break;
7351     case RID_IS_TRIVIAL:
7352       kind = CPTK_IS_TRIVIAL;
7353       break;
7354     case RID_IS_UNION:
7355       kind = CPTK_IS_UNION;
7356       break;
7357     default:
7358       gcc_unreachable ();
7359     }
7360
7361   /* Consume the token.  */
7362   cp_lexer_consume_token (parser->lexer);
7363
7364   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7365
7366   type1 = cp_parser_type_id (parser);
7367
7368   if (type1 == error_mark_node)
7369     return error_mark_node;
7370
7371   /* Build a trivial decl-specifier-seq.  */
7372   clear_decl_specs (&decl_specs);
7373   decl_specs.type = type1;
7374
7375   /* Call grokdeclarator to figure out what type this is.  */
7376   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7377                           /*initialized=*/0, /*attrlist=*/NULL);
7378
7379   if (binary)
7380     {
7381       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7382  
7383       type2 = cp_parser_type_id (parser);
7384
7385       if (type2 == error_mark_node)
7386         return error_mark_node;
7387
7388       /* Build a trivial decl-specifier-seq.  */
7389       clear_decl_specs (&decl_specs);
7390       decl_specs.type = type2;
7391
7392       /* Call grokdeclarator to figure out what type this is.  */
7393       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7394                               /*initialized=*/0, /*attrlist=*/NULL);
7395     }
7396
7397   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7398
7399   /* Complete the trait expression, which may mean either processing
7400      the trait expr now or saving it for template instantiation.  */
7401   return finish_trait_expr (kind, type1, type2);
7402 }
7403
7404 /* Lambdas that appear in variable initializer or default argument scope
7405    get that in their mangling, so we need to record it.  We might as well
7406    use the count for function and namespace scopes as well.  */
7407 static GTY(()) tree lambda_scope;
7408 static GTY(()) int lambda_count;
7409 typedef struct GTY(()) tree_int
7410 {
7411   tree t;
7412   int i;
7413 } tree_int;
7414 DEF_VEC_O(tree_int);
7415 DEF_VEC_ALLOC_O(tree_int,gc);
7416 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7417
7418 static void
7419 start_lambda_scope (tree decl)
7420 {
7421   tree_int ti;
7422   gcc_assert (decl);
7423   /* Once we're inside a function, we ignore other scopes and just push
7424      the function again so that popping works properly.  */
7425   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7426     decl = current_function_decl;
7427   ti.t = lambda_scope;
7428   ti.i = lambda_count;
7429   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7430   if (lambda_scope != decl)
7431     {
7432       /* Don't reset the count if we're still in the same function.  */
7433       lambda_scope = decl;
7434       lambda_count = 0;
7435     }
7436 }
7437
7438 static void
7439 record_lambda_scope (tree lambda)
7440 {
7441   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7442   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7443 }
7444
7445 static void
7446 finish_lambda_scope (void)
7447 {
7448   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7449   if (lambda_scope != p->t)
7450     {
7451       lambda_scope = p->t;
7452       lambda_count = p->i;
7453     }
7454   VEC_pop (tree_int, lambda_scope_stack);
7455 }
7456
7457 /* Parse a lambda expression.
7458
7459    lambda-expression:
7460      lambda-introducer lambda-declarator [opt] compound-statement
7461
7462    Returns a representation of the expression.  */
7463
7464 static tree
7465 cp_parser_lambda_expression (cp_parser* parser)
7466 {
7467   tree lambda_expr = build_lambda_expr ();
7468   tree type;
7469
7470   LAMBDA_EXPR_LOCATION (lambda_expr)
7471     = cp_lexer_peek_token (parser->lexer)->location;
7472
7473   if (cp_unevaluated_operand)
7474     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7475               "lambda-expression in unevaluated context");
7476
7477   /* We may be in the middle of deferred access check.  Disable
7478      it now.  */
7479   push_deferring_access_checks (dk_no_deferred);
7480
7481   cp_parser_lambda_introducer (parser, lambda_expr);
7482
7483   type = begin_lambda_type (lambda_expr);
7484
7485   record_lambda_scope (lambda_expr);
7486
7487   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7488   determine_visibility (TYPE_NAME (type));
7489
7490   /* Now that we've started the type, add the capture fields for any
7491      explicit captures.  */
7492   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7493
7494   {
7495     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7496     unsigned int saved_num_template_parameter_lists
7497         = parser->num_template_parameter_lists;
7498
7499     parser->num_template_parameter_lists = 0;
7500
7501     /* By virtue of defining a local class, a lambda expression has access to
7502        the private variables of enclosing classes.  */
7503
7504     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7505
7506     cp_parser_lambda_body (parser, lambda_expr);
7507
7508     /* The capture list was built up in reverse order; fix that now.  */
7509     {
7510       tree newlist = NULL_TREE;
7511       tree elt, next;
7512
7513       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7514            elt; elt = next)
7515         {
7516           tree field = TREE_PURPOSE (elt);
7517           char *buf;
7518
7519           next = TREE_CHAIN (elt);
7520           TREE_CHAIN (elt) = newlist;
7521           newlist = elt;
7522
7523           /* Also add __ to the beginning of the field name so that code
7524              outside the lambda body can't see the captured name.  We could
7525              just remove the name entirely, but this is more useful for
7526              debugging.  */
7527           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7528             /* The 'this' capture already starts with __.  */
7529             continue;
7530
7531           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7532           buf[1] = buf[0] = '_';
7533           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7534                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7535           DECL_NAME (field) = get_identifier (buf);
7536         }
7537       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7538     }
7539
7540     maybe_add_lambda_conv_op (type);
7541
7542     type = finish_struct (type, /*attributes=*/NULL_TREE);
7543
7544     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7545   }
7546
7547   pop_deferring_access_checks ();
7548
7549   return build_lambda_object (lambda_expr);
7550 }
7551
7552 /* Parse the beginning of a lambda expression.
7553
7554    lambda-introducer:
7555      [ lambda-capture [opt] ]
7556
7557    LAMBDA_EXPR is the current representation of the lambda expression.  */
7558
7559 static void
7560 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7561 {
7562   /* Need commas after the first capture.  */
7563   bool first = true;
7564
7565   /* Eat the leading `['.  */
7566   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7567
7568   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7569   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7570       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7571     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7572   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7573     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7574
7575   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7576     {
7577       cp_lexer_consume_token (parser->lexer);
7578       first = false;
7579     }
7580
7581   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7582     {
7583       cp_token* capture_token;
7584       tree capture_id;
7585       tree capture_init_expr;
7586       cp_id_kind idk = CP_ID_KIND_NONE;
7587       bool explicit_init_p = false;
7588
7589       enum capture_kind_type
7590       {
7591         BY_COPY,
7592         BY_REFERENCE
7593       };
7594       enum capture_kind_type capture_kind = BY_COPY;
7595
7596       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7597         {
7598           error ("expected end of capture-list");
7599           return;
7600         }
7601
7602       if (first)
7603         first = false;
7604       else
7605         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7606
7607       /* Possibly capture `this'.  */
7608       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7609         {
7610           cp_lexer_consume_token (parser->lexer);
7611           add_capture (lambda_expr,
7612                        /*id=*/get_identifier ("__this"),
7613                        /*initializer=*/finish_this_expr(),
7614                        /*by_reference_p=*/false,
7615                        explicit_init_p);
7616           continue;
7617         }
7618
7619       /* Remember whether we want to capture as a reference or not.  */
7620       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7621         {
7622           capture_kind = BY_REFERENCE;
7623           cp_lexer_consume_token (parser->lexer);
7624         }
7625
7626       /* Get the identifier.  */
7627       capture_token = cp_lexer_peek_token (parser->lexer);
7628       capture_id = cp_parser_identifier (parser);
7629
7630       if (capture_id == error_mark_node)
7631         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7632            delimiters, but I modified this to stop on unnested ']' as well.  It
7633            was already changed to stop on unnested '}', so the
7634            "closing_parenthesis" name is no more misleading with my change.  */
7635         {
7636           cp_parser_skip_to_closing_parenthesis (parser,
7637                                                  /*recovering=*/true,
7638                                                  /*or_comma=*/true,
7639                                                  /*consume_paren=*/true);
7640           break;
7641         }
7642
7643       /* Find the initializer for this capture.  */
7644       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7645         {
7646           /* An explicit expression exists.  */
7647           cp_lexer_consume_token (parser->lexer);
7648           pedwarn (input_location, OPT_pedantic,
7649                    "ISO C++ does not allow initializers "
7650                    "in lambda expression capture lists");
7651           capture_init_expr = cp_parser_assignment_expression (parser,
7652                                                                /*cast_p=*/true,
7653                                                                &idk);
7654           explicit_init_p = true;
7655         }
7656       else
7657         {
7658           const char* error_msg;
7659
7660           /* Turn the identifier into an id-expression.  */
7661           capture_init_expr
7662             = cp_parser_lookup_name
7663                 (parser,
7664                  capture_id,
7665                  none_type,
7666                  /*is_template=*/false,
7667                  /*is_namespace=*/false,
7668                  /*check_dependency=*/true,
7669                  /*ambiguous_decls=*/NULL,
7670                  capture_token->location);
7671
7672           capture_init_expr
7673             = finish_id_expression
7674                 (capture_id,
7675                  capture_init_expr,
7676                  parser->scope,
7677                  &idk,
7678                  /*integral_constant_expression_p=*/false,
7679                  /*allow_non_integral_constant_expression_p=*/false,
7680                  /*non_integral_constant_expression_p=*/NULL,
7681                  /*template_p=*/false,
7682                  /*done=*/true,
7683                  /*address_p=*/false,
7684                  /*template_arg_p=*/false,
7685                  &error_msg,
7686                  capture_token->location);
7687         }
7688
7689       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7690         capture_init_expr
7691           = unqualified_name_lookup_error (capture_init_expr);
7692
7693       add_capture (lambda_expr,
7694                    capture_id,
7695                    capture_init_expr,
7696                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7697                    explicit_init_p);
7698     }
7699
7700   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7701 }
7702
7703 /* Parse the (optional) middle of a lambda expression.
7704
7705    lambda-declarator:
7706      ( parameter-declaration-clause [opt] )
7707        attribute-specifier [opt]
7708        mutable [opt]
7709        exception-specification [opt]
7710        lambda-return-type-clause [opt]
7711
7712    LAMBDA_EXPR is the current representation of the lambda expression.  */
7713
7714 static void
7715 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7716 {
7717   /* 5.1.1.4 of the standard says:
7718        If a lambda-expression does not include a lambda-declarator, it is as if
7719        the lambda-declarator were ().
7720      This means an empty parameter list, no attributes, and no exception
7721      specification.  */
7722   tree param_list = void_list_node;
7723   tree attributes = NULL_TREE;
7724   tree exception_spec = NULL_TREE;
7725   tree t;
7726
7727   /* The lambda-declarator is optional, but must begin with an opening
7728      parenthesis if present.  */
7729   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7730     {
7731       cp_lexer_consume_token (parser->lexer);
7732
7733       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7734
7735       /* Parse parameters.  */
7736       param_list = cp_parser_parameter_declaration_clause (parser);
7737
7738       /* Default arguments shall not be specified in the
7739          parameter-declaration-clause of a lambda-declarator.  */
7740       for (t = param_list; t; t = TREE_CHAIN (t))
7741         if (TREE_PURPOSE (t))
7742           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7743                    "default argument specified for lambda parameter");
7744
7745       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7746
7747       attributes = cp_parser_attributes_opt (parser);
7748
7749       /* Parse optional `mutable' keyword.  */
7750       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7751         {
7752           cp_lexer_consume_token (parser->lexer);
7753           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7754         }
7755
7756       /* Parse optional exception specification.  */
7757       exception_spec = cp_parser_exception_specification_opt (parser);
7758
7759       /* Parse optional trailing return type.  */
7760       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7761         {
7762           cp_lexer_consume_token (parser->lexer);
7763           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7764         }
7765
7766       /* The function parameters must be in scope all the way until after the
7767          trailing-return-type in case of decltype.  */
7768       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7769         pop_binding (DECL_NAME (t), t);
7770
7771       leave_scope ();
7772     }
7773
7774   /* Create the function call operator.
7775
7776      Messing with declarators like this is no uglier than building up the
7777      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7778      other code.  */
7779   {
7780     cp_decl_specifier_seq return_type_specs;
7781     cp_declarator* declarator;
7782     tree fco;
7783     int quals;
7784     void *p;
7785
7786     clear_decl_specs (&return_type_specs);
7787     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7788       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7789     else
7790       /* Maybe we will deduce the return type later, but we can use void
7791          as a placeholder return type anyways.  */
7792       return_type_specs.type = void_type_node;
7793
7794     p = obstack_alloc (&declarator_obstack, 0);
7795
7796     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7797                                      sfk_none);
7798
7799     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7800              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7801     declarator = make_call_declarator (declarator, param_list, quals,
7802                                        exception_spec,
7803                                        /*late_return_type=*/NULL_TREE);
7804     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7805
7806     fco = grokmethod (&return_type_specs,
7807                       declarator,
7808                       attributes);
7809     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7810     DECL_ARTIFICIAL (fco) = 1;
7811
7812     finish_member_declaration (fco);
7813
7814     obstack_free (&declarator_obstack, p);
7815   }
7816 }
7817
7818 /* Parse the body of a lambda expression, which is simply
7819
7820    compound-statement
7821
7822    but which requires special handling.
7823    LAMBDA_EXPR is the current representation of the lambda expression.  */
7824
7825 static void
7826 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7827 {
7828   bool nested = (current_function_decl != NULL_TREE);
7829   if (nested)
7830     push_function_context ();
7831
7832   /* Finish the function call operator
7833      - class_specifier
7834      + late_parsing_for_member
7835      + function_definition_after_declarator
7836      + ctor_initializer_opt_and_function_body  */
7837   {
7838     tree fco = lambda_function (lambda_expr);
7839     tree body;
7840     bool done = false;
7841
7842     /* Let the front end know that we are going to be defining this
7843        function.  */
7844     start_preparsed_function (fco,
7845                               NULL_TREE,
7846                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7847
7848     start_lambda_scope (fco);
7849     body = begin_function_body ();
7850
7851     /* 5.1.1.4 of the standard says:
7852          If a lambda-expression does not include a trailing-return-type, it
7853          is as if the trailing-return-type denotes the following type:
7854           * if the compound-statement is of the form
7855                { return attribute-specifier [opt] expression ; }
7856              the type of the returned expression after lvalue-to-rvalue
7857              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7858              (_conv.array_ 4.2), and function-to-pointer conversion
7859              (_conv.func_ 4.3);
7860           * otherwise, void.  */
7861
7862     /* In a lambda that has neither a lambda-return-type-clause
7863        nor a deducible form, errors should be reported for return statements
7864        in the body.  Since we used void as the placeholder return type, parsing
7865        the body as usual will give such desired behavior.  */
7866     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7867         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7868         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7869         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7870       {
7871         tree compound_stmt;
7872         tree expr = NULL_TREE;
7873         cp_id_kind idk = CP_ID_KIND_NONE;
7874
7875         /* Parse tentatively in case there's more after the initial return
7876            statement.  */
7877         cp_parser_parse_tentatively (parser);
7878
7879         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7880         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7881
7882         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7883
7884         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7885         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7886
7887         if (cp_parser_parse_definitely (parser))
7888           {
7889             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7890
7891             compound_stmt = begin_compound_stmt (0);
7892             /* Will get error here if type not deduced yet.  */
7893             finish_return_stmt (expr);
7894             finish_compound_stmt (compound_stmt);
7895
7896             done = true;
7897           }
7898       }
7899
7900     if (!done)
7901       {
7902         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7903           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7904         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7905            cp_parser_compound_stmt does not pass it.  */
7906         cp_parser_function_body (parser);
7907         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7908       }
7909
7910     finish_function_body (body);
7911     finish_lambda_scope ();
7912
7913     /* Finish the function and generate code for it if necessary.  */
7914     expand_or_defer_fn (finish_function (/*inline*/2));
7915   }
7916
7917   if (nested)
7918     pop_function_context();
7919 }
7920
7921 /* Statements [gram.stmt.stmt]  */
7922
7923 /* Parse a statement.
7924
7925    statement:
7926      labeled-statement
7927      expression-statement
7928      compound-statement
7929      selection-statement
7930      iteration-statement
7931      jump-statement
7932      declaration-statement
7933      try-block
7934
7935   IN_COMPOUND is true when the statement is nested inside a
7936   cp_parser_compound_statement; this matters for certain pragmas.
7937
7938   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7939   is a (possibly labeled) if statement which is not enclosed in braces
7940   and has an else clause.  This is used to implement -Wparentheses.  */
7941
7942 static void
7943 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7944                      bool in_compound, bool *if_p)
7945 {
7946   tree statement;
7947   cp_token *token;
7948   location_t statement_location;
7949
7950  restart:
7951   if (if_p != NULL)
7952     *if_p = false;
7953   /* There is no statement yet.  */
7954   statement = NULL_TREE;
7955   /* Peek at the next token.  */
7956   token = cp_lexer_peek_token (parser->lexer);
7957   /* Remember the location of the first token in the statement.  */
7958   statement_location = token->location;
7959   /* If this is a keyword, then that will often determine what kind of
7960      statement we have.  */
7961   if (token->type == CPP_KEYWORD)
7962     {
7963       enum rid keyword = token->keyword;
7964
7965       switch (keyword)
7966         {
7967         case RID_CASE:
7968         case RID_DEFAULT:
7969           /* Looks like a labeled-statement with a case label.
7970              Parse the label, and then use tail recursion to parse
7971              the statement.  */
7972           cp_parser_label_for_labeled_statement (parser);
7973           goto restart;
7974
7975         case RID_IF:
7976         case RID_SWITCH:
7977           statement = cp_parser_selection_statement (parser, if_p);
7978           break;
7979
7980         case RID_WHILE:
7981         case RID_DO:
7982         case RID_FOR:
7983           statement = cp_parser_iteration_statement (parser);
7984           break;
7985
7986         case RID_BREAK:
7987         case RID_CONTINUE:
7988         case RID_RETURN:
7989         case RID_GOTO:
7990           statement = cp_parser_jump_statement (parser);
7991           break;
7992
7993           /* Objective-C++ exception-handling constructs.  */
7994         case RID_AT_TRY:
7995         case RID_AT_CATCH:
7996         case RID_AT_FINALLY:
7997         case RID_AT_SYNCHRONIZED:
7998         case RID_AT_THROW:
7999           statement = cp_parser_objc_statement (parser);
8000           break;
8001
8002         case RID_TRY:
8003           statement = cp_parser_try_block (parser);
8004           break;
8005
8006         case RID_NAMESPACE:
8007           /* This must be a namespace alias definition.  */
8008           cp_parser_declaration_statement (parser);
8009           return;
8010           
8011         default:
8012           /* It might be a keyword like `int' that can start a
8013              declaration-statement.  */
8014           break;
8015         }
8016     }
8017   else if (token->type == CPP_NAME)
8018     {
8019       /* If the next token is a `:', then we are looking at a
8020          labeled-statement.  */
8021       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8022       if (token->type == CPP_COLON)
8023         {
8024           /* Looks like a labeled-statement with an ordinary label.
8025              Parse the label, and then use tail recursion to parse
8026              the statement.  */
8027           cp_parser_label_for_labeled_statement (parser);
8028           goto restart;
8029         }
8030     }
8031   /* Anything that starts with a `{' must be a compound-statement.  */
8032   else if (token->type == CPP_OPEN_BRACE)
8033     statement = cp_parser_compound_statement (parser, NULL, false);
8034   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8035      a statement all its own.  */
8036   else if (token->type == CPP_PRAGMA)
8037     {
8038       /* Only certain OpenMP pragmas are attached to statements, and thus
8039          are considered statements themselves.  All others are not.  In
8040          the context of a compound, accept the pragma as a "statement" and
8041          return so that we can check for a close brace.  Otherwise we
8042          require a real statement and must go back and read one.  */
8043       if (in_compound)
8044         cp_parser_pragma (parser, pragma_compound);
8045       else if (!cp_parser_pragma (parser, pragma_stmt))
8046         goto restart;
8047       return;
8048     }
8049   else if (token->type == CPP_EOF)
8050     {
8051       cp_parser_error (parser, "expected statement");
8052       return;
8053     }
8054
8055   /* Everything else must be a declaration-statement or an
8056      expression-statement.  Try for the declaration-statement
8057      first, unless we are looking at a `;', in which case we know that
8058      we have an expression-statement.  */
8059   if (!statement)
8060     {
8061       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8062         {
8063           cp_parser_parse_tentatively (parser);
8064           /* Try to parse the declaration-statement.  */
8065           cp_parser_declaration_statement (parser);
8066           /* If that worked, we're done.  */
8067           if (cp_parser_parse_definitely (parser))
8068             return;
8069         }
8070       /* Look for an expression-statement instead.  */
8071       statement = cp_parser_expression_statement (parser, in_statement_expr);
8072     }
8073
8074   /* Set the line number for the statement.  */
8075   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8076     SET_EXPR_LOCATION (statement, statement_location);
8077 }
8078
8079 /* Parse the label for a labeled-statement, i.e.
8080
8081    identifier :
8082    case constant-expression :
8083    default :
8084
8085    GNU Extension:
8086    case constant-expression ... constant-expression : statement
8087
8088    When a label is parsed without errors, the label is added to the
8089    parse tree by the finish_* functions, so this function doesn't
8090    have to return the label.  */
8091
8092 static void
8093 cp_parser_label_for_labeled_statement (cp_parser* parser)
8094 {
8095   cp_token *token;
8096   tree label = NULL_TREE;
8097
8098   /* The next token should be an identifier.  */
8099   token = cp_lexer_peek_token (parser->lexer);
8100   if (token->type != CPP_NAME
8101       && token->type != CPP_KEYWORD)
8102     {
8103       cp_parser_error (parser, "expected labeled-statement");
8104       return;
8105     }
8106
8107   switch (token->keyword)
8108     {
8109     case RID_CASE:
8110       {
8111         tree expr, expr_hi;
8112         cp_token *ellipsis;
8113
8114         /* Consume the `case' token.  */
8115         cp_lexer_consume_token (parser->lexer);
8116         /* Parse the constant-expression.  */
8117         expr = cp_parser_constant_expression (parser,
8118                                               /*allow_non_constant_p=*/false,
8119                                               NULL);
8120
8121         ellipsis = cp_lexer_peek_token (parser->lexer);
8122         if (ellipsis->type == CPP_ELLIPSIS)
8123           {
8124             /* Consume the `...' token.  */
8125             cp_lexer_consume_token (parser->lexer);
8126             expr_hi =
8127               cp_parser_constant_expression (parser,
8128                                              /*allow_non_constant_p=*/false,
8129                                              NULL);
8130             /* We don't need to emit warnings here, as the common code
8131                will do this for us.  */
8132           }
8133         else
8134           expr_hi = NULL_TREE;
8135
8136         if (parser->in_switch_statement_p)
8137           finish_case_label (token->location, expr, expr_hi);
8138         else
8139           error_at (token->location,
8140                     "case label %qE not within a switch statement",
8141                     expr);
8142       }
8143       break;
8144
8145     case RID_DEFAULT:
8146       /* Consume the `default' token.  */
8147       cp_lexer_consume_token (parser->lexer);
8148
8149       if (parser->in_switch_statement_p)
8150         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8151       else
8152         error_at (token->location, "case label not within a switch statement");
8153       break;
8154
8155     default:
8156       /* Anything else must be an ordinary label.  */
8157       label = finish_label_stmt (cp_parser_identifier (parser));
8158       break;
8159     }
8160
8161   /* Require the `:' token.  */
8162   cp_parser_require (parser, CPP_COLON, RT_COLON);
8163
8164   /* An ordinary label may optionally be followed by attributes.
8165      However, this is only permitted if the attributes are then
8166      followed by a semicolon.  This is because, for backward
8167      compatibility, when parsing
8168        lab: __attribute__ ((unused)) int i;
8169      we want the attribute to attach to "i", not "lab".  */
8170   if (label != NULL_TREE
8171       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8172     {
8173       tree attrs;
8174
8175       cp_parser_parse_tentatively (parser);
8176       attrs = cp_parser_attributes_opt (parser);
8177       if (attrs == NULL_TREE
8178           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8179         cp_parser_abort_tentative_parse (parser);
8180       else if (!cp_parser_parse_definitely (parser))
8181         ;
8182       else
8183         cplus_decl_attributes (&label, attrs, 0);
8184     }
8185 }
8186
8187 /* Parse an expression-statement.
8188
8189    expression-statement:
8190      expression [opt] ;
8191
8192    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8193    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8194    indicates whether this expression-statement is part of an
8195    expression statement.  */
8196
8197 static tree
8198 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8199 {
8200   tree statement = NULL_TREE;
8201   cp_token *token = cp_lexer_peek_token (parser->lexer);
8202
8203   /* If the next token is a ';', then there is no expression
8204      statement.  */
8205   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8206     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8207
8208   /* Give a helpful message for "A<T>::type t;" and the like.  */
8209   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8210       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8211     {
8212       if (TREE_CODE (statement) == SCOPE_REF)
8213         error_at (token->location, "need %<typename%> before %qE because "
8214                   "%qT is a dependent scope",
8215                   statement, TREE_OPERAND (statement, 0));
8216       else if (is_overloaded_fn (statement)
8217                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8218         {
8219           /* A::A a; */
8220           tree fn = get_first_fn (statement);
8221           error_at (token->location,
8222                     "%<%T::%D%> names the constructor, not the type",
8223                     DECL_CONTEXT (fn), DECL_NAME (fn));
8224         }
8225     }
8226
8227   /* Consume the final `;'.  */
8228   cp_parser_consume_semicolon_at_end_of_statement (parser);
8229
8230   if (in_statement_expr
8231       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8232     /* This is the final expression statement of a statement
8233        expression.  */
8234     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8235   else if (statement)
8236     statement = finish_expr_stmt (statement);
8237   else
8238     finish_stmt ();
8239
8240   return statement;
8241 }
8242
8243 /* Parse a compound-statement.
8244
8245    compound-statement:
8246      { statement-seq [opt] }
8247
8248    GNU extension:
8249
8250    compound-statement:
8251      { label-declaration-seq [opt] statement-seq [opt] }
8252
8253    label-declaration-seq:
8254      label-declaration
8255      label-declaration-seq label-declaration
8256
8257    Returns a tree representing the statement.  */
8258
8259 static tree
8260 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8261                               bool in_try)
8262 {
8263   tree compound_stmt;
8264
8265   /* Consume the `{'.  */
8266   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8267     return error_mark_node;
8268   /* Begin the compound-statement.  */
8269   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8270   /* If the next keyword is `__label__' we have a label declaration.  */
8271   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8272     cp_parser_label_declaration (parser);
8273   /* Parse an (optional) statement-seq.  */
8274   cp_parser_statement_seq_opt (parser, in_statement_expr);
8275   /* Finish the compound-statement.  */
8276   finish_compound_stmt (compound_stmt);
8277   /* Consume the `}'.  */
8278   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8279
8280   return compound_stmt;
8281 }
8282
8283 /* Parse an (optional) statement-seq.
8284
8285    statement-seq:
8286      statement
8287      statement-seq [opt] statement  */
8288
8289 static void
8290 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8291 {
8292   /* Scan statements until there aren't any more.  */
8293   while (true)
8294     {
8295       cp_token *token = cp_lexer_peek_token (parser->lexer);
8296
8297       /* If we're looking at a `}', then we've run out of statements.  */
8298       if (token->type == CPP_CLOSE_BRACE
8299           || token->type == CPP_EOF
8300           || token->type == CPP_PRAGMA_EOL)
8301         break;
8302       
8303       /* If we are in a compound statement and find 'else' then
8304          something went wrong.  */
8305       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8306         {
8307           if (parser->in_statement & IN_IF_STMT) 
8308             break;
8309           else
8310             {
8311               token = cp_lexer_consume_token (parser->lexer);
8312               error_at (token->location, "%<else%> without a previous %<if%>");
8313             }
8314         }
8315
8316       /* Parse the statement.  */
8317       cp_parser_statement (parser, in_statement_expr, true, NULL);
8318     }
8319 }
8320
8321 /* Parse a selection-statement.
8322
8323    selection-statement:
8324      if ( condition ) statement
8325      if ( condition ) statement else statement
8326      switch ( condition ) statement
8327
8328    Returns the new IF_STMT or SWITCH_STMT.
8329
8330    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8331    is a (possibly labeled) if statement which is not enclosed in
8332    braces and has an else clause.  This is used to implement
8333    -Wparentheses.  */
8334
8335 static tree
8336 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8337 {
8338   cp_token *token;
8339   enum rid keyword;
8340
8341   if (if_p != NULL)
8342     *if_p = false;
8343
8344   /* Peek at the next token.  */
8345   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8346
8347   /* See what kind of keyword it is.  */
8348   keyword = token->keyword;
8349   switch (keyword)
8350     {
8351     case RID_IF:
8352     case RID_SWITCH:
8353       {
8354         tree statement;
8355         tree condition;
8356
8357         /* Look for the `('.  */
8358         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8359           {
8360             cp_parser_skip_to_end_of_statement (parser);
8361             return error_mark_node;
8362           }
8363
8364         /* Begin the selection-statement.  */
8365         if (keyword == RID_IF)
8366           statement = begin_if_stmt ();
8367         else
8368           statement = begin_switch_stmt ();
8369
8370         /* Parse the condition.  */
8371         condition = cp_parser_condition (parser);
8372         /* Look for the `)'.  */
8373         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8374           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8375                                                  /*consume_paren=*/true);
8376
8377         if (keyword == RID_IF)
8378           {
8379             bool nested_if;
8380             unsigned char in_statement;
8381
8382             /* Add the condition.  */
8383             finish_if_stmt_cond (condition, statement);
8384
8385             /* Parse the then-clause.  */
8386             in_statement = parser->in_statement;
8387             parser->in_statement |= IN_IF_STMT;
8388             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8389               {
8390                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8391                 add_stmt (build_empty_stmt (loc));
8392                 cp_lexer_consume_token (parser->lexer);
8393                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8394                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8395                               "empty body in an %<if%> statement");
8396                 nested_if = false;
8397               }
8398             else
8399               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8400             parser->in_statement = in_statement;
8401
8402             finish_then_clause (statement);
8403
8404             /* If the next token is `else', parse the else-clause.  */
8405             if (cp_lexer_next_token_is_keyword (parser->lexer,
8406                                                 RID_ELSE))
8407               {
8408                 /* Consume the `else' keyword.  */
8409                 cp_lexer_consume_token (parser->lexer);
8410                 begin_else_clause (statement);
8411                 /* Parse the else-clause.  */
8412                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8413                   {
8414                     location_t loc;
8415                     loc = cp_lexer_peek_token (parser->lexer)->location;
8416                     warning_at (loc,
8417                                 OPT_Wempty_body, "suggest braces around "
8418                                 "empty body in an %<else%> statement");
8419                     add_stmt (build_empty_stmt (loc));
8420                     cp_lexer_consume_token (parser->lexer);
8421                   }
8422                 else
8423                   cp_parser_implicitly_scoped_statement (parser, NULL);
8424
8425                 finish_else_clause (statement);
8426
8427                 /* If we are currently parsing a then-clause, then
8428                    IF_P will not be NULL.  We set it to true to
8429                    indicate that this if statement has an else clause.
8430                    This may trigger the Wparentheses warning below
8431                    when we get back up to the parent if statement.  */
8432                 if (if_p != NULL)
8433                   *if_p = true;
8434               }
8435             else
8436               {
8437                 /* This if statement does not have an else clause.  If
8438                    NESTED_IF is true, then the then-clause is an if
8439                    statement which does have an else clause.  We warn
8440                    about the potential ambiguity.  */
8441                 if (nested_if)
8442                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8443                               "suggest explicit braces to avoid ambiguous"
8444                               " %<else%>");
8445               }
8446
8447             /* Now we're all done with the if-statement.  */
8448             finish_if_stmt (statement);
8449           }
8450         else
8451           {
8452             bool in_switch_statement_p;
8453             unsigned char in_statement;
8454
8455             /* Add the condition.  */
8456             finish_switch_cond (condition, statement);
8457
8458             /* Parse the body of the switch-statement.  */
8459             in_switch_statement_p = parser->in_switch_statement_p;
8460             in_statement = parser->in_statement;
8461             parser->in_switch_statement_p = true;
8462             parser->in_statement |= IN_SWITCH_STMT;
8463             cp_parser_implicitly_scoped_statement (parser, NULL);
8464             parser->in_switch_statement_p = in_switch_statement_p;
8465             parser->in_statement = in_statement;
8466
8467             /* Now we're all done with the switch-statement.  */
8468             finish_switch_stmt (statement);
8469           }
8470
8471         return statement;
8472       }
8473       break;
8474
8475     default:
8476       cp_parser_error (parser, "expected selection-statement");
8477       return error_mark_node;
8478     }
8479 }
8480
8481 /* Parse a condition.
8482
8483    condition:
8484      expression
8485      type-specifier-seq declarator = initializer-clause
8486      type-specifier-seq declarator braced-init-list
8487
8488    GNU Extension:
8489
8490    condition:
8491      type-specifier-seq declarator asm-specification [opt]
8492        attributes [opt] = assignment-expression
8493
8494    Returns the expression that should be tested.  */
8495
8496 static tree
8497 cp_parser_condition (cp_parser* parser)
8498 {
8499   cp_decl_specifier_seq type_specifiers;
8500   const char *saved_message;
8501
8502   /* Try the declaration first.  */
8503   cp_parser_parse_tentatively (parser);
8504   /* New types are not allowed in the type-specifier-seq for a
8505      condition.  */
8506   saved_message = parser->type_definition_forbidden_message;
8507   parser->type_definition_forbidden_message
8508     = G_("types may not be defined in conditions");
8509   /* Parse the type-specifier-seq.  */
8510   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8511                                 /*is_trailing_return=*/false,
8512                                 &type_specifiers);
8513   /* Restore the saved message.  */
8514   parser->type_definition_forbidden_message = saved_message;
8515   /* If all is well, we might be looking at a declaration.  */
8516   if (!cp_parser_error_occurred (parser))
8517     {
8518       tree decl;
8519       tree asm_specification;
8520       tree attributes;
8521       cp_declarator *declarator;
8522       tree initializer = NULL_TREE;
8523
8524       /* Parse the declarator.  */
8525       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8526                                          /*ctor_dtor_or_conv_p=*/NULL,
8527                                          /*parenthesized_p=*/NULL,
8528                                          /*member_p=*/false);
8529       /* Parse the attributes.  */
8530       attributes = cp_parser_attributes_opt (parser);
8531       /* Parse the asm-specification.  */
8532       asm_specification = cp_parser_asm_specification_opt (parser);
8533       /* If the next token is not an `=' or '{', then we might still be
8534          looking at an expression.  For example:
8535
8536            if (A(a).x)
8537
8538          looks like a decl-specifier-seq and a declarator -- but then
8539          there is no `=', so this is an expression.  */
8540       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8541           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8542         cp_parser_simulate_error (parser);
8543         
8544       /* If we did see an `=' or '{', then we are looking at a declaration
8545          for sure.  */
8546       if (cp_parser_parse_definitely (parser))
8547         {
8548           tree pushed_scope;
8549           bool non_constant_p;
8550           bool flags = LOOKUP_ONLYCONVERTING;
8551
8552           /* Create the declaration.  */
8553           decl = start_decl (declarator, &type_specifiers,
8554                              /*initialized_p=*/true,
8555                              attributes, /*prefix_attributes=*/NULL_TREE,
8556                              &pushed_scope);
8557
8558           /* Parse the initializer.  */
8559           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8560             {
8561               initializer = cp_parser_braced_list (parser, &non_constant_p);
8562               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8563               flags = 0;
8564             }
8565           else
8566             {
8567               /* Consume the `='.  */
8568               cp_parser_require (parser, CPP_EQ, RT_EQ);
8569               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8570             }
8571           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8572             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8573
8574           if (!non_constant_p)
8575             initializer = fold_non_dependent_expr (initializer);
8576
8577           /* Process the initializer.  */
8578           cp_finish_decl (decl,
8579                           initializer, !non_constant_p,
8580                           asm_specification,
8581                           flags);
8582
8583           if (pushed_scope)
8584             pop_scope (pushed_scope);
8585
8586           return convert_from_reference (decl);
8587         }
8588     }
8589   /* If we didn't even get past the declarator successfully, we are
8590      definitely not looking at a declaration.  */
8591   else
8592     cp_parser_abort_tentative_parse (parser);
8593
8594   /* Otherwise, we are looking at an expression.  */
8595   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8596 }
8597
8598 /* Parses a traditional for-statement until the closing ')', not included. */
8599
8600 static tree
8601 cp_parser_c_for (cp_parser *parser)
8602 {
8603   /* Normal for loop */
8604   tree stmt;
8605   tree condition = NULL_TREE;
8606   tree expression = NULL_TREE;
8607
8608   /* Begin the for-statement.  */
8609   stmt = begin_for_stmt ();
8610
8611   /* Parse the initialization.  */
8612   cp_parser_for_init_statement (parser);
8613   finish_for_init_stmt (stmt);
8614
8615   /* If there's a condition, process it.  */
8616   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8617     condition = cp_parser_condition (parser);
8618   finish_for_cond (condition, stmt);
8619   /* Look for the `;'.  */
8620   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8621
8622   /* If there's an expression, process it.  */
8623   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8624     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8625   finish_for_expr (expression, stmt);
8626
8627   return stmt;
8628 }
8629
8630 /* Tries to parse a range-based for-statement:
8631
8632   range-based-for:
8633     type-specifier-seq declarator : expression
8634
8635   If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8636   expression. Note that the *DECL is returned unfinished, so
8637   later you should call cp_finish_decl().
8638
8639   Returns TRUE iff a range-based for is parsed. */
8640
8641 static tree
8642 cp_parser_range_for (cp_parser *parser)
8643 {
8644   tree stmt, range_decl, range_expr;
8645   cp_decl_specifier_seq type_specifiers;
8646   cp_declarator *declarator;
8647   const char *saved_message;
8648   tree attributes, pushed_scope;
8649
8650   cp_parser_parse_tentatively (parser);
8651   /* New types are not allowed in the type-specifier-seq for a
8652      range-based for loop.  */
8653   saved_message = parser->type_definition_forbidden_message;
8654   parser->type_definition_forbidden_message
8655     = G_("types may not be defined in range-based for loops");
8656   /* Parse the type-specifier-seq.  */
8657   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8658                                 /*is_trailing_return=*/false,
8659                                 &type_specifiers);
8660   /* Restore the saved message.  */
8661   parser->type_definition_forbidden_message = saved_message;
8662   /* If all is well, we might be looking at a declaration.  */
8663   if (cp_parser_error_occurred (parser))
8664     {
8665       cp_parser_abort_tentative_parse (parser);
8666       return NULL_TREE;
8667     }
8668   /* Parse the declarator.  */
8669   declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8670                                      /*ctor_dtor_or_conv_p=*/NULL,
8671                                      /*parenthesized_p=*/NULL,
8672                                      /*member_p=*/false);
8673   /* Parse the attributes.  */
8674   attributes = cp_parser_attributes_opt (parser);
8675   /* The next token should be `:'. */
8676   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8677     cp_parser_simulate_error (parser);
8678
8679   /* Check if it is a range-based for */
8680   if (!cp_parser_parse_definitely (parser))
8681     return NULL_TREE;
8682
8683   cp_parser_require (parser, CPP_COLON, RT_COLON);
8684   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8685     {
8686       bool expr_non_constant_p;
8687       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8688     }
8689   else
8690     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8691
8692   /* If in template, STMT is converted to a normal for-statements
8693      at instantiation. If not, it is done just ahead. */
8694   if (processing_template_decl)
8695     stmt = begin_range_for_stmt ();
8696   else
8697     stmt = begin_for_stmt ();
8698
8699   /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8700   range_decl = start_decl (declarator, &type_specifiers,
8701                            /*initialized_p=*/SD_INITIALIZED,
8702                            attributes, /*prefix_attributes=*/NULL_TREE,
8703                            &pushed_scope);
8704   /* No scope allowed here */
8705   pop_scope (pushed_scope);
8706
8707   if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8708     finish_range_for_decl (stmt, range_decl, range_expr);
8709   else
8710     /* Convert the range-based for loop into a normal for-statement. */
8711     stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8712
8713   return stmt;
8714 }
8715
8716 /* Converts a range-based for-statement into a normal
8717    for-statement, as per the definition.
8718
8719       for (RANGE_DECL : RANGE_EXPR)
8720         BLOCK
8721
8722    should be equivalent to:
8723
8724       {
8725         auto &&__range = RANGE_EXPR;
8726         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8727               __begin != __end;
8728               ++__begin)
8729           {
8730               RANGE_DECL = *__begin;
8731               BLOCK
8732           }
8733       }
8734
8735    If RANGE_EXPR is an array:
8736        BEGIN_EXPR = __range
8737        END_EXPR = __range + ARRAY_SIZE(__range)
8738    Else:
8739         BEGIN_EXPR = begin(__range)
8740         END_EXPR = end(__range);
8741
8742    When calling begin()/end() we must use argument dependent
8743    lookup, but always considering 'std' as an associated namespace.  */
8744
8745 tree
8746 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8747 {
8748   tree range_type, range_temp;
8749   tree begin, end;
8750   tree iter_type, begin_expr, end_expr;
8751   tree condition, expression;
8752
8753   /* Find out the type deduced by the declaration
8754    * `auto &&__range = range_expr' */
8755   range_type = cp_build_reference_type (make_auto (), true);
8756   range_type = do_auto_deduction (range_type, range_expr,
8757                                   type_uses_auto (range_type));
8758
8759   /* Create the __range variable */
8760   range_temp = build_decl (input_location, VAR_DECL,
8761                            get_identifier ("__for_range"), range_type);
8762   TREE_USED (range_temp) = 1;
8763   DECL_ARTIFICIAL (range_temp) = 1;
8764   pushdecl (range_temp);
8765   finish_expr_stmt (cp_build_modify_expr (range_temp, INIT_EXPR, range_expr,
8766                                           tf_warning_or_error));
8767   range_temp = convert_from_reference (range_temp);
8768
8769   if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8770     {
8771       /* If RANGE_TEMP is an array we will use pointer arithmetic */
8772       iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8773       begin_expr = range_temp;
8774       end_expr
8775         = build_binary_op (input_location, PLUS_EXPR,
8776                            range_temp,
8777                            array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8778     }
8779   else
8780     {
8781       /* If it is not an array, we must call begin(__range)/end__range() */
8782       VEC(tree,gc) *vec;
8783
8784       begin_expr = get_identifier ("begin");
8785       vec = make_tree_vector ();
8786       VEC_safe_push (tree, gc, vec, range_temp);
8787       begin_expr = perform_koenig_lookup (begin_expr, vec,
8788                                           /*include_std=*/true);
8789       begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8790                                      tf_warning_or_error);
8791       release_tree_vector (vec);
8792
8793       end_expr = get_identifier ("end");
8794       vec = make_tree_vector ();
8795       VEC_safe_push (tree, gc, vec, range_temp);
8796       end_expr = perform_koenig_lookup (end_expr, vec,
8797                                         /*include_std=*/true);
8798       end_expr = finish_call_expr (end_expr, &vec, false, true,
8799                                    tf_warning_or_error);
8800       release_tree_vector (vec);
8801
8802       /* The unqualified type of the __begin and __end temporaries should
8803       * be the same as required by the multiple auto declaration */
8804       iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8805       if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8806         error ("inconsistent begin/end types in range-based for: %qT and %qT",
8807                TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8808     }
8809
8810   /* The new for initialization statement */
8811   begin = build_decl (input_location, VAR_DECL,
8812                       get_identifier ("__for_begin"), iter_type);
8813   TREE_USED (begin) = 1;
8814   DECL_ARTIFICIAL (begin) = 1;
8815   pushdecl (begin);
8816   finish_expr_stmt (cp_build_modify_expr (begin, INIT_EXPR, begin_expr,
8817                                           tf_warning_or_error));
8818   end = build_decl (input_location, VAR_DECL,
8819                     get_identifier ("__for_end"), iter_type);
8820   TREE_USED (end) = 1;
8821   DECL_ARTIFICIAL (end) = 1;
8822   pushdecl (end);
8823
8824   finish_expr_stmt (cp_build_modify_expr (end, INIT_EXPR, end_expr,
8825                                           tf_warning_or_error));
8826
8827   finish_for_init_stmt (statement);
8828
8829 /* The new for condition */
8830   condition = build_x_binary_op (NE_EXPR,
8831                                  begin, ERROR_MARK,
8832                                  end, ERROR_MARK,
8833                                  NULL, tf_warning_or_error);
8834   finish_for_cond (condition, statement);
8835
8836   /* The new increment expression */
8837   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8838   finish_for_expr (expression, statement);
8839
8840   /* The declaration is initialized with *__begin inside the loop body */
8841   cp_finish_decl (range_decl,
8842                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8843                   /*is_constant_init*/false, NULL_TREE,
8844                   LOOKUP_ONLYCONVERTING);
8845
8846   return statement;
8847 }
8848
8849
8850 /* Parse an iteration-statement.
8851
8852    iteration-statement:
8853      while ( condition ) statement
8854      do statement while ( expression ) ;
8855      for ( for-init-statement condition [opt] ; expression [opt] )
8856        statement
8857
8858    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8859
8860 static tree
8861 cp_parser_iteration_statement (cp_parser* parser)
8862 {
8863   cp_token *token;
8864   enum rid keyword;
8865   tree statement;
8866   unsigned char in_statement;
8867
8868   /* Peek at the next token.  */
8869   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8870   if (!token)
8871     return error_mark_node;
8872
8873   /* Remember whether or not we are already within an iteration
8874      statement.  */
8875   in_statement = parser->in_statement;
8876
8877   /* See what kind of keyword it is.  */
8878   keyword = token->keyword;
8879   switch (keyword)
8880     {
8881     case RID_WHILE:
8882       {
8883         tree condition;
8884
8885         /* Begin the while-statement.  */
8886         statement = begin_while_stmt ();
8887         /* Look for the `('.  */
8888         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8889         /* Parse the condition.  */
8890         condition = cp_parser_condition (parser);
8891         finish_while_stmt_cond (condition, statement);
8892         /* Look for the `)'.  */
8893         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8894         /* Parse the dependent statement.  */
8895         parser->in_statement = IN_ITERATION_STMT;
8896         cp_parser_already_scoped_statement (parser);
8897         parser->in_statement = in_statement;
8898         /* We're done with the while-statement.  */
8899         finish_while_stmt (statement);
8900       }
8901       break;
8902
8903     case RID_DO:
8904       {
8905         tree expression;
8906
8907         /* Begin the do-statement.  */
8908         statement = begin_do_stmt ();
8909         /* Parse the body of the do-statement.  */
8910         parser->in_statement = IN_ITERATION_STMT;
8911         cp_parser_implicitly_scoped_statement (parser, NULL);
8912         parser->in_statement = in_statement;
8913         finish_do_body (statement);
8914         /* Look for the `while' keyword.  */
8915         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8916         /* Look for the `('.  */
8917         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8918         /* Parse the expression.  */
8919         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8920         /* We're done with the do-statement.  */
8921         finish_do_stmt (expression, statement);
8922         /* Look for the `)'.  */
8923         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8924         /* Look for the `;'.  */
8925         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8926       }
8927       break;
8928
8929     case RID_FOR:
8930       {
8931         /* Look for the `('.  */
8932         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8933
8934         if (cxx_dialect == cxx0x)
8935           statement = cp_parser_range_for (parser);
8936         else
8937           statement = NULL_TREE;
8938         if (statement == NULL_TREE)
8939           statement = cp_parser_c_for (parser);
8940
8941         /* Look for the `)'.  */
8942         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8943
8944         /* Parse the body of the for-statement.  */
8945         parser->in_statement = IN_ITERATION_STMT;
8946         cp_parser_already_scoped_statement (parser);
8947         parser->in_statement = in_statement;
8948
8949         /* We're done with the for-statement.  */
8950         finish_for_stmt (statement);
8951       }
8952       break;
8953
8954     default:
8955       cp_parser_error (parser, "expected iteration-statement");
8956       statement = error_mark_node;
8957       break;
8958     }
8959
8960   return statement;
8961 }
8962
8963 /* Parse a for-init-statement.
8964
8965    for-init-statement:
8966      expression-statement
8967      simple-declaration  */
8968
8969 static void
8970 cp_parser_for_init_statement (cp_parser* parser)
8971 {
8972   /* If the next token is a `;', then we have an empty
8973      expression-statement.  Grammatically, this is also a
8974      simple-declaration, but an invalid one, because it does not
8975      declare anything.  Therefore, if we did not handle this case
8976      specially, we would issue an error message about an invalid
8977      declaration.  */
8978   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8979     {
8980       /* We're going to speculatively look for a declaration, falling back
8981          to an expression, if necessary.  */
8982       cp_parser_parse_tentatively (parser);
8983       /* Parse the declaration.  */
8984       cp_parser_simple_declaration (parser,
8985                                     /*function_definition_allowed_p=*/false);
8986       /* If the tentative parse failed, then we shall need to look for an
8987          expression-statement.  */
8988       if (cp_parser_parse_definitely (parser))
8989         return;
8990     }
8991
8992   cp_parser_expression_statement (parser, NULL_TREE);
8993 }
8994
8995 /* Parse a jump-statement.
8996
8997    jump-statement:
8998      break ;
8999      continue ;
9000      return expression [opt] ;
9001      return braced-init-list ;
9002      goto identifier ;
9003
9004    GNU extension:
9005
9006    jump-statement:
9007      goto * expression ;
9008
9009    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9010
9011 static tree
9012 cp_parser_jump_statement (cp_parser* parser)
9013 {
9014   tree statement = error_mark_node;
9015   cp_token *token;
9016   enum rid keyword;
9017   unsigned char in_statement;
9018
9019   /* Peek at the next token.  */
9020   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9021   if (!token)
9022     return error_mark_node;
9023
9024   /* See what kind of keyword it is.  */
9025   keyword = token->keyword;
9026   switch (keyword)
9027     {
9028     case RID_BREAK:
9029       in_statement = parser->in_statement & ~IN_IF_STMT;      
9030       switch (in_statement)
9031         {
9032         case 0:
9033           error_at (token->location, "break statement not within loop or switch");
9034           break;
9035         default:
9036           gcc_assert ((in_statement & IN_SWITCH_STMT)
9037                       || in_statement == IN_ITERATION_STMT);
9038           statement = finish_break_stmt ();
9039           break;
9040         case IN_OMP_BLOCK:
9041           error_at (token->location, "invalid exit from OpenMP structured block");
9042           break;
9043         case IN_OMP_FOR:
9044           error_at (token->location, "break statement used with OpenMP for loop");
9045           break;
9046         }
9047       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9048       break;
9049
9050     case RID_CONTINUE:
9051       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9052         {
9053         case 0:
9054           error_at (token->location, "continue statement not within a loop");
9055           break;
9056         case IN_ITERATION_STMT:
9057         case IN_OMP_FOR:
9058           statement = finish_continue_stmt ();
9059           break;
9060         case IN_OMP_BLOCK:
9061           error_at (token->location, "invalid exit from OpenMP structured block");
9062           break;
9063         default:
9064           gcc_unreachable ();
9065         }
9066       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9067       break;
9068
9069     case RID_RETURN:
9070       {
9071         tree expr;
9072         bool expr_non_constant_p;
9073
9074         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9075           {
9076             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9077             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9078           }
9079         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9080           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9081         else
9082           /* If the next token is a `;', then there is no
9083              expression.  */
9084           expr = NULL_TREE;
9085         /* Build the return-statement.  */
9086         statement = finish_return_stmt (expr);
9087         /* Look for the final `;'.  */
9088         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9089       }
9090       break;
9091
9092     case RID_GOTO:
9093       /* Create the goto-statement.  */
9094       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9095         {
9096           /* Issue a warning about this use of a GNU extension.  */
9097           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9098           /* Consume the '*' token.  */
9099           cp_lexer_consume_token (parser->lexer);
9100           /* Parse the dependent expression.  */
9101           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9102         }
9103       else
9104         finish_goto_stmt (cp_parser_identifier (parser));
9105       /* Look for the final `;'.  */
9106       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9107       break;
9108
9109     default:
9110       cp_parser_error (parser, "expected jump-statement");
9111       break;
9112     }
9113
9114   return statement;
9115 }
9116
9117 /* Parse a declaration-statement.
9118
9119    declaration-statement:
9120      block-declaration  */
9121
9122 static void
9123 cp_parser_declaration_statement (cp_parser* parser)
9124 {
9125   void *p;
9126
9127   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9128   p = obstack_alloc (&declarator_obstack, 0);
9129
9130  /* Parse the block-declaration.  */
9131   cp_parser_block_declaration (parser, /*statement_p=*/true);
9132
9133   /* Free any declarators allocated.  */
9134   obstack_free (&declarator_obstack, p);
9135
9136   /* Finish off the statement.  */
9137   finish_stmt ();
9138 }
9139
9140 /* Some dependent statements (like `if (cond) statement'), are
9141    implicitly in their own scope.  In other words, if the statement is
9142    a single statement (as opposed to a compound-statement), it is
9143    none-the-less treated as if it were enclosed in braces.  Any
9144    declarations appearing in the dependent statement are out of scope
9145    after control passes that point.  This function parses a statement,
9146    but ensures that is in its own scope, even if it is not a
9147    compound-statement.
9148
9149    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9150    is a (possibly labeled) if statement which is not enclosed in
9151    braces and has an else clause.  This is used to implement
9152    -Wparentheses.
9153
9154    Returns the new statement.  */
9155
9156 static tree
9157 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9158 {
9159   tree statement;
9160
9161   if (if_p != NULL)
9162     *if_p = false;
9163
9164   /* Mark if () ; with a special NOP_EXPR.  */
9165   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9166     {
9167       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9168       cp_lexer_consume_token (parser->lexer);
9169       statement = add_stmt (build_empty_stmt (loc));
9170     }
9171   /* if a compound is opened, we simply parse the statement directly.  */
9172   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9173     statement = cp_parser_compound_statement (parser, NULL, false);
9174   /* If the token is not a `{', then we must take special action.  */
9175   else
9176     {
9177       /* Create a compound-statement.  */
9178       statement = begin_compound_stmt (0);
9179       /* Parse the dependent-statement.  */
9180       cp_parser_statement (parser, NULL_TREE, false, if_p);
9181       /* Finish the dummy compound-statement.  */
9182       finish_compound_stmt (statement);
9183     }
9184
9185   /* Return the statement.  */
9186   return statement;
9187 }
9188
9189 /* For some dependent statements (like `while (cond) statement'), we
9190    have already created a scope.  Therefore, even if the dependent
9191    statement is a compound-statement, we do not want to create another
9192    scope.  */
9193
9194 static void
9195 cp_parser_already_scoped_statement (cp_parser* parser)
9196 {
9197   /* If the token is a `{', then we must take special action.  */
9198   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9199     cp_parser_statement (parser, NULL_TREE, false, NULL);
9200   else
9201     {
9202       /* Avoid calling cp_parser_compound_statement, so that we
9203          don't create a new scope.  Do everything else by hand.  */
9204       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9205       /* If the next keyword is `__label__' we have a label declaration.  */
9206       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9207         cp_parser_label_declaration (parser);
9208       /* Parse an (optional) statement-seq.  */
9209       cp_parser_statement_seq_opt (parser, NULL_TREE);
9210       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9211     }
9212 }
9213
9214 /* Declarations [gram.dcl.dcl] */
9215
9216 /* Parse an optional declaration-sequence.
9217
9218    declaration-seq:
9219      declaration
9220      declaration-seq declaration  */
9221
9222 static void
9223 cp_parser_declaration_seq_opt (cp_parser* parser)
9224 {
9225   while (true)
9226     {
9227       cp_token *token;
9228
9229       token = cp_lexer_peek_token (parser->lexer);
9230
9231       if (token->type == CPP_CLOSE_BRACE
9232           || token->type == CPP_EOF
9233           || token->type == CPP_PRAGMA_EOL)
9234         break;
9235
9236       if (token->type == CPP_SEMICOLON)
9237         {
9238           /* A declaration consisting of a single semicolon is
9239              invalid.  Allow it unless we're being pedantic.  */
9240           cp_lexer_consume_token (parser->lexer);
9241           if (!in_system_header)
9242             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9243           continue;
9244         }
9245
9246       /* If we're entering or exiting a region that's implicitly
9247          extern "C", modify the lang context appropriately.  */
9248       if (!parser->implicit_extern_c && token->implicit_extern_c)
9249         {
9250           push_lang_context (lang_name_c);
9251           parser->implicit_extern_c = true;
9252         }
9253       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9254         {
9255           pop_lang_context ();
9256           parser->implicit_extern_c = false;
9257         }
9258
9259       if (token->type == CPP_PRAGMA)
9260         {
9261           /* A top-level declaration can consist solely of a #pragma.
9262              A nested declaration cannot, so this is done here and not
9263              in cp_parser_declaration.  (A #pragma at block scope is
9264              handled in cp_parser_statement.)  */
9265           cp_parser_pragma (parser, pragma_external);
9266           continue;
9267         }
9268
9269       /* Parse the declaration itself.  */
9270       cp_parser_declaration (parser);
9271     }
9272 }
9273
9274 /* Parse a declaration.
9275
9276    declaration:
9277      block-declaration
9278      function-definition
9279      template-declaration
9280      explicit-instantiation
9281      explicit-specialization
9282      linkage-specification
9283      namespace-definition
9284
9285    GNU extension:
9286
9287    declaration:
9288       __extension__ declaration */
9289
9290 static void
9291 cp_parser_declaration (cp_parser* parser)
9292 {
9293   cp_token token1;
9294   cp_token token2;
9295   int saved_pedantic;
9296   void *p;
9297   tree attributes = NULL_TREE;
9298
9299   /* Check for the `__extension__' keyword.  */
9300   if (cp_parser_extension_opt (parser, &saved_pedantic))
9301     {
9302       /* Parse the qualified declaration.  */
9303       cp_parser_declaration (parser);
9304       /* Restore the PEDANTIC flag.  */
9305       pedantic = saved_pedantic;
9306
9307       return;
9308     }
9309
9310   /* Try to figure out what kind of declaration is present.  */
9311   token1 = *cp_lexer_peek_token (parser->lexer);
9312
9313   if (token1.type != CPP_EOF)
9314     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9315   else
9316     {
9317       token2.type = CPP_EOF;
9318       token2.keyword = RID_MAX;
9319     }
9320
9321   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9322   p = obstack_alloc (&declarator_obstack, 0);
9323
9324   /* If the next token is `extern' and the following token is a string
9325      literal, then we have a linkage specification.  */
9326   if (token1.keyword == RID_EXTERN
9327       && cp_parser_is_string_literal (&token2))
9328     cp_parser_linkage_specification (parser);
9329   /* If the next token is `template', then we have either a template
9330      declaration, an explicit instantiation, or an explicit
9331      specialization.  */
9332   else if (token1.keyword == RID_TEMPLATE)
9333     {
9334       /* `template <>' indicates a template specialization.  */
9335       if (token2.type == CPP_LESS
9336           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9337         cp_parser_explicit_specialization (parser);
9338       /* `template <' indicates a template declaration.  */
9339       else if (token2.type == CPP_LESS)
9340         cp_parser_template_declaration (parser, /*member_p=*/false);
9341       /* Anything else must be an explicit instantiation.  */
9342       else
9343         cp_parser_explicit_instantiation (parser);
9344     }
9345   /* If the next token is `export', then we have a template
9346      declaration.  */
9347   else if (token1.keyword == RID_EXPORT)
9348     cp_parser_template_declaration (parser, /*member_p=*/false);
9349   /* If the next token is `extern', 'static' or 'inline' and the one
9350      after that is `template', we have a GNU extended explicit
9351      instantiation directive.  */
9352   else if (cp_parser_allow_gnu_extensions_p (parser)
9353            && (token1.keyword == RID_EXTERN
9354                || token1.keyword == RID_STATIC
9355                || token1.keyword == RID_INLINE)
9356            && token2.keyword == RID_TEMPLATE)
9357     cp_parser_explicit_instantiation (parser);
9358   /* If the next token is `namespace', check for a named or unnamed
9359      namespace definition.  */
9360   else if (token1.keyword == RID_NAMESPACE
9361            && (/* A named namespace definition.  */
9362                (token2.type == CPP_NAME
9363                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9364                     != CPP_EQ))
9365                /* An unnamed namespace definition.  */
9366                || token2.type == CPP_OPEN_BRACE
9367                || token2.keyword == RID_ATTRIBUTE))
9368     cp_parser_namespace_definition (parser);
9369   /* An inline (associated) namespace definition.  */
9370   else if (token1.keyword == RID_INLINE
9371            && token2.keyword == RID_NAMESPACE)
9372     cp_parser_namespace_definition (parser);
9373   /* Objective-C++ declaration/definition.  */
9374   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9375     cp_parser_objc_declaration (parser, NULL_TREE);
9376   else if (c_dialect_objc ()
9377            && token1.keyword == RID_ATTRIBUTE
9378            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9379     cp_parser_objc_declaration (parser, attributes);
9380   /* We must have either a block declaration or a function
9381      definition.  */
9382   else
9383     /* Try to parse a block-declaration, or a function-definition.  */
9384     cp_parser_block_declaration (parser, /*statement_p=*/false);
9385
9386   /* Free any declarators allocated.  */
9387   obstack_free (&declarator_obstack, p);
9388 }
9389
9390 /* Parse a block-declaration.
9391
9392    block-declaration:
9393      simple-declaration
9394      asm-definition
9395      namespace-alias-definition
9396      using-declaration
9397      using-directive
9398
9399    GNU Extension:
9400
9401    block-declaration:
9402      __extension__ block-declaration
9403
9404    C++0x Extension:
9405
9406    block-declaration:
9407      static_assert-declaration
9408
9409    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9410    part of a declaration-statement.  */
9411
9412 static void
9413 cp_parser_block_declaration (cp_parser *parser,
9414                              bool      statement_p)
9415 {
9416   cp_token *token1;
9417   int saved_pedantic;
9418
9419   /* Check for the `__extension__' keyword.  */
9420   if (cp_parser_extension_opt (parser, &saved_pedantic))
9421     {
9422       /* Parse the qualified declaration.  */
9423       cp_parser_block_declaration (parser, statement_p);
9424       /* Restore the PEDANTIC flag.  */
9425       pedantic = saved_pedantic;
9426
9427       return;
9428     }
9429
9430   /* Peek at the next token to figure out which kind of declaration is
9431      present.  */
9432   token1 = cp_lexer_peek_token (parser->lexer);
9433
9434   /* If the next keyword is `asm', we have an asm-definition.  */
9435   if (token1->keyword == RID_ASM)
9436     {
9437       if (statement_p)
9438         cp_parser_commit_to_tentative_parse (parser);
9439       cp_parser_asm_definition (parser);
9440     }
9441   /* If the next keyword is `namespace', we have a
9442      namespace-alias-definition.  */
9443   else if (token1->keyword == RID_NAMESPACE)
9444     cp_parser_namespace_alias_definition (parser);
9445   /* If the next keyword is `using', we have either a
9446      using-declaration or a using-directive.  */
9447   else if (token1->keyword == RID_USING)
9448     {
9449       cp_token *token2;
9450
9451       if (statement_p)
9452         cp_parser_commit_to_tentative_parse (parser);
9453       /* If the token after `using' is `namespace', then we have a
9454          using-directive.  */
9455       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9456       if (token2->keyword == RID_NAMESPACE)
9457         cp_parser_using_directive (parser);
9458       /* Otherwise, it's a using-declaration.  */
9459       else
9460         cp_parser_using_declaration (parser,
9461                                      /*access_declaration_p=*/false);
9462     }
9463   /* If the next keyword is `__label__' we have a misplaced label
9464      declaration.  */
9465   else if (token1->keyword == RID_LABEL)
9466     {
9467       cp_lexer_consume_token (parser->lexer);
9468       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9469       cp_parser_skip_to_end_of_statement (parser);
9470       /* If the next token is now a `;', consume it.  */
9471       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9472         cp_lexer_consume_token (parser->lexer);
9473     }
9474   /* If the next token is `static_assert' we have a static assertion.  */
9475   else if (token1->keyword == RID_STATIC_ASSERT)
9476     cp_parser_static_assert (parser, /*member_p=*/false);
9477   /* Anything else must be a simple-declaration.  */
9478   else
9479     cp_parser_simple_declaration (parser, !statement_p);
9480 }
9481
9482 /* Parse a simple-declaration.
9483
9484    simple-declaration:
9485      decl-specifier-seq [opt] init-declarator-list [opt] ;
9486
9487    init-declarator-list:
9488      init-declarator
9489      init-declarator-list , init-declarator
9490
9491    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9492    function-definition as a simple-declaration.  */
9493
9494 static void
9495 cp_parser_simple_declaration (cp_parser* parser,
9496                               bool function_definition_allowed_p)
9497 {
9498   cp_decl_specifier_seq decl_specifiers;
9499   int declares_class_or_enum;
9500   bool saw_declarator;
9501
9502   /* Defer access checks until we know what is being declared; the
9503      checks for names appearing in the decl-specifier-seq should be
9504      done as if we were in the scope of the thing being declared.  */
9505   push_deferring_access_checks (dk_deferred);
9506
9507   /* Parse the decl-specifier-seq.  We have to keep track of whether
9508      or not the decl-specifier-seq declares a named class or
9509      enumeration type, since that is the only case in which the
9510      init-declarator-list is allowed to be empty.
9511
9512      [dcl.dcl]
9513
9514      In a simple-declaration, the optional init-declarator-list can be
9515      omitted only when declaring a class or enumeration, that is when
9516      the decl-specifier-seq contains either a class-specifier, an
9517      elaborated-type-specifier, or an enum-specifier.  */
9518   cp_parser_decl_specifier_seq (parser,
9519                                 CP_PARSER_FLAGS_OPTIONAL,
9520                                 &decl_specifiers,
9521                                 &declares_class_or_enum);
9522   /* We no longer need to defer access checks.  */
9523   stop_deferring_access_checks ();
9524
9525   /* In a block scope, a valid declaration must always have a
9526      decl-specifier-seq.  By not trying to parse declarators, we can
9527      resolve the declaration/expression ambiguity more quickly.  */
9528   if (!function_definition_allowed_p
9529       && !decl_specifiers.any_specifiers_p)
9530     {
9531       cp_parser_error (parser, "expected declaration");
9532       goto done;
9533     }
9534
9535   /* If the next two tokens are both identifiers, the code is
9536      erroneous. The usual cause of this situation is code like:
9537
9538        T t;
9539
9540      where "T" should name a type -- but does not.  */
9541   if (!decl_specifiers.any_type_specifiers_p
9542       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9543     {
9544       /* If parsing tentatively, we should commit; we really are
9545          looking at a declaration.  */
9546       cp_parser_commit_to_tentative_parse (parser);
9547       /* Give up.  */
9548       goto done;
9549     }
9550
9551   /* If we have seen at least one decl-specifier, and the next token
9552      is not a parenthesis, then we must be looking at a declaration.
9553      (After "int (" we might be looking at a functional cast.)  */
9554   if (decl_specifiers.any_specifiers_p
9555       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9556       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9557       && !cp_parser_error_occurred (parser))
9558     cp_parser_commit_to_tentative_parse (parser);
9559
9560   /* Keep going until we hit the `;' at the end of the simple
9561      declaration.  */
9562   saw_declarator = false;
9563   while (cp_lexer_next_token_is_not (parser->lexer,
9564                                      CPP_SEMICOLON))
9565     {
9566       cp_token *token;
9567       bool function_definition_p;
9568       tree decl;
9569
9570       if (saw_declarator)
9571         {
9572           /* If we are processing next declarator, coma is expected */
9573           token = cp_lexer_peek_token (parser->lexer);
9574           gcc_assert (token->type == CPP_COMMA);
9575           cp_lexer_consume_token (parser->lexer);
9576         }
9577       else
9578         saw_declarator = true;
9579
9580       /* Parse the init-declarator.  */
9581       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9582                                         /*checks=*/NULL,
9583                                         function_definition_allowed_p,
9584                                         /*member_p=*/false,
9585                                         declares_class_or_enum,
9586                                         &function_definition_p);
9587       /* If an error occurred while parsing tentatively, exit quickly.
9588          (That usually happens when in the body of a function; each
9589          statement is treated as a declaration-statement until proven
9590          otherwise.)  */
9591       if (cp_parser_error_occurred (parser))
9592         goto done;
9593       /* Handle function definitions specially.  */
9594       if (function_definition_p)
9595         {
9596           /* If the next token is a `,', then we are probably
9597              processing something like:
9598
9599                void f() {}, *p;
9600
9601              which is erroneous.  */
9602           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9603             {
9604               cp_token *token = cp_lexer_peek_token (parser->lexer);
9605               error_at (token->location,
9606                         "mixing"
9607                         " declarations and function-definitions is forbidden");
9608             }
9609           /* Otherwise, we're done with the list of declarators.  */
9610           else
9611             {
9612               pop_deferring_access_checks ();
9613               return;
9614             }
9615         }
9616       /* The next token should be either a `,' or a `;'.  */
9617       token = cp_lexer_peek_token (parser->lexer);
9618       /* If it's a `,', there are more declarators to come.  */
9619       if (token->type == CPP_COMMA)
9620         /* will be consumed next time around */;
9621       /* If it's a `;', we are done.  */
9622       else if (token->type == CPP_SEMICOLON)
9623         break;
9624       /* Anything else is an error.  */
9625       else
9626         {
9627           /* If we have already issued an error message we don't need
9628              to issue another one.  */
9629           if (decl != error_mark_node
9630               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9631             cp_parser_error (parser, "expected %<,%> or %<;%>");
9632           /* Skip tokens until we reach the end of the statement.  */
9633           cp_parser_skip_to_end_of_statement (parser);
9634           /* If the next token is now a `;', consume it.  */
9635           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9636             cp_lexer_consume_token (parser->lexer);
9637           goto done;
9638         }
9639       /* After the first time around, a function-definition is not
9640          allowed -- even if it was OK at first.  For example:
9641
9642            int i, f() {}
9643
9644          is not valid.  */
9645       function_definition_allowed_p = false;
9646     }
9647
9648   /* Issue an error message if no declarators are present, and the
9649      decl-specifier-seq does not itself declare a class or
9650      enumeration.  */
9651   if (!saw_declarator)
9652     {
9653       if (cp_parser_declares_only_class_p (parser))
9654         shadow_tag (&decl_specifiers);
9655       /* Perform any deferred access checks.  */
9656       perform_deferred_access_checks ();
9657     }
9658
9659   /* Consume the `;'.  */
9660   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9661
9662  done:
9663   pop_deferring_access_checks ();
9664 }
9665
9666 /* Parse a decl-specifier-seq.
9667
9668    decl-specifier-seq:
9669      decl-specifier-seq [opt] decl-specifier
9670
9671    decl-specifier:
9672      storage-class-specifier
9673      type-specifier
9674      function-specifier
9675      friend
9676      typedef
9677
9678    GNU Extension:
9679
9680    decl-specifier:
9681      attributes
9682
9683    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9684
9685    The parser flags FLAGS is used to control type-specifier parsing.
9686
9687    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9688    flags:
9689
9690      1: one of the decl-specifiers is an elaborated-type-specifier
9691         (i.e., a type declaration)
9692      2: one of the decl-specifiers is an enum-specifier or a
9693         class-specifier (i.e., a type definition)
9694
9695    */
9696
9697 static void
9698 cp_parser_decl_specifier_seq (cp_parser* parser,
9699                               cp_parser_flags flags,
9700                               cp_decl_specifier_seq *decl_specs,
9701                               int* declares_class_or_enum)
9702 {
9703   bool constructor_possible_p = !parser->in_declarator_p;
9704   cp_token *start_token = NULL;
9705
9706   /* Clear DECL_SPECS.  */
9707   clear_decl_specs (decl_specs);
9708
9709   /* Assume no class or enumeration type is declared.  */
9710   *declares_class_or_enum = 0;
9711
9712   /* Keep reading specifiers until there are no more to read.  */
9713   while (true)
9714     {
9715       bool constructor_p;
9716       bool found_decl_spec;
9717       cp_token *token;
9718
9719       /* Peek at the next token.  */
9720       token = cp_lexer_peek_token (parser->lexer);
9721
9722       /* Save the first token of the decl spec list for error
9723          reporting.  */
9724       if (!start_token)
9725         start_token = token;
9726       /* Handle attributes.  */
9727       if (token->keyword == RID_ATTRIBUTE)
9728         {
9729           /* Parse the attributes.  */
9730           decl_specs->attributes
9731             = chainon (decl_specs->attributes,
9732                        cp_parser_attributes_opt (parser));
9733           continue;
9734         }
9735       /* Assume we will find a decl-specifier keyword.  */
9736       found_decl_spec = true;
9737       /* If the next token is an appropriate keyword, we can simply
9738          add it to the list.  */
9739       switch (token->keyword)
9740         {
9741           /* decl-specifier:
9742                friend
9743                constexpr */
9744         case RID_FRIEND:
9745           if (!at_class_scope_p ())
9746             {
9747               error_at (token->location, "%<friend%> used outside of class");
9748               cp_lexer_purge_token (parser->lexer);
9749             }
9750           else
9751             {
9752               ++decl_specs->specs[(int) ds_friend];
9753               /* Consume the token.  */
9754               cp_lexer_consume_token (parser->lexer);
9755             }
9756           break;
9757
9758         case RID_CONSTEXPR:
9759           ++decl_specs->specs[(int) ds_constexpr];
9760           cp_lexer_consume_token (parser->lexer);
9761           break;
9762
9763           /* function-specifier:
9764                inline
9765                virtual
9766                explicit  */
9767         case RID_INLINE:
9768         case RID_VIRTUAL:
9769         case RID_EXPLICIT:
9770           cp_parser_function_specifier_opt (parser, decl_specs);
9771           break;
9772
9773           /* decl-specifier:
9774                typedef  */
9775         case RID_TYPEDEF:
9776           ++decl_specs->specs[(int) ds_typedef];
9777           /* Consume the token.  */
9778           cp_lexer_consume_token (parser->lexer);
9779           /* A constructor declarator cannot appear in a typedef.  */
9780           constructor_possible_p = false;
9781           /* The "typedef" keyword can only occur in a declaration; we
9782              may as well commit at this point.  */
9783           cp_parser_commit_to_tentative_parse (parser);
9784
9785           if (decl_specs->storage_class != sc_none)
9786             decl_specs->conflicting_specifiers_p = true;
9787           break;
9788
9789           /* storage-class-specifier:
9790                auto
9791                register
9792                static
9793                extern
9794                mutable
9795
9796              GNU Extension:
9797                thread  */
9798         case RID_AUTO:
9799           if (cxx_dialect == cxx98) 
9800             {
9801               /* Consume the token.  */
9802               cp_lexer_consume_token (parser->lexer);
9803
9804               /* Complain about `auto' as a storage specifier, if
9805                  we're complaining about C++0x compatibility.  */
9806               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9807                           " will change meaning in C++0x; please remove it");
9808
9809               /* Set the storage class anyway.  */
9810               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9811                                            token->location);
9812             }
9813           else
9814             /* C++0x auto type-specifier.  */
9815             found_decl_spec = false;
9816           break;
9817
9818         case RID_REGISTER:
9819         case RID_STATIC:
9820         case RID_EXTERN:
9821         case RID_MUTABLE:
9822           /* Consume the token.  */
9823           cp_lexer_consume_token (parser->lexer);
9824           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9825                                        token->location);
9826           break;
9827         case RID_THREAD:
9828           /* Consume the token.  */
9829           cp_lexer_consume_token (parser->lexer);
9830           ++decl_specs->specs[(int) ds_thread];
9831           break;
9832
9833         default:
9834           /* We did not yet find a decl-specifier yet.  */
9835           found_decl_spec = false;
9836           break;
9837         }
9838
9839       /* Constructors are a special case.  The `S' in `S()' is not a
9840          decl-specifier; it is the beginning of the declarator.  */
9841       constructor_p
9842         = (!found_decl_spec
9843            && constructor_possible_p
9844            && (cp_parser_constructor_declarator_p
9845                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9846
9847       /* If we don't have a DECL_SPEC yet, then we must be looking at
9848          a type-specifier.  */
9849       if (!found_decl_spec && !constructor_p)
9850         {
9851           int decl_spec_declares_class_or_enum;
9852           bool is_cv_qualifier;
9853           tree type_spec;
9854
9855           type_spec
9856             = cp_parser_type_specifier (parser, flags,
9857                                         decl_specs,
9858                                         /*is_declaration=*/true,
9859                                         &decl_spec_declares_class_or_enum,
9860                                         &is_cv_qualifier);
9861           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9862
9863           /* If this type-specifier referenced a user-defined type
9864              (a typedef, class-name, etc.), then we can't allow any
9865              more such type-specifiers henceforth.
9866
9867              [dcl.spec]
9868
9869              The longest sequence of decl-specifiers that could
9870              possibly be a type name is taken as the
9871              decl-specifier-seq of a declaration.  The sequence shall
9872              be self-consistent as described below.
9873
9874              [dcl.type]
9875
9876              As a general rule, at most one type-specifier is allowed
9877              in the complete decl-specifier-seq of a declaration.  The
9878              only exceptions are the following:
9879
9880              -- const or volatile can be combined with any other
9881                 type-specifier.
9882
9883              -- signed or unsigned can be combined with char, long,
9884                 short, or int.
9885
9886              -- ..
9887
9888              Example:
9889
9890                typedef char* Pc;
9891                void g (const int Pc);
9892
9893              Here, Pc is *not* part of the decl-specifier seq; it's
9894              the declarator.  Therefore, once we see a type-specifier
9895              (other than a cv-qualifier), we forbid any additional
9896              user-defined types.  We *do* still allow things like `int
9897              int' to be considered a decl-specifier-seq, and issue the
9898              error message later.  */
9899           if (type_spec && !is_cv_qualifier)
9900             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9901           /* A constructor declarator cannot follow a type-specifier.  */
9902           if (type_spec)
9903             {
9904               constructor_possible_p = false;
9905               found_decl_spec = true;
9906               if (!is_cv_qualifier)
9907                 decl_specs->any_type_specifiers_p = true;
9908             }
9909         }
9910
9911       /* If we still do not have a DECL_SPEC, then there are no more
9912          decl-specifiers.  */
9913       if (!found_decl_spec)
9914         break;
9915
9916       decl_specs->any_specifiers_p = true;
9917       /* After we see one decl-specifier, further decl-specifiers are
9918          always optional.  */
9919       flags |= CP_PARSER_FLAGS_OPTIONAL;
9920     }
9921
9922   cp_parser_check_decl_spec (decl_specs, start_token->location);
9923
9924   /* Don't allow a friend specifier with a class definition.  */
9925   if (decl_specs->specs[(int) ds_friend] != 0
9926       && (*declares_class_or_enum & 2))
9927     error_at (start_token->location,
9928               "class definition may not be declared a friend");
9929 }
9930
9931 /* Parse an (optional) storage-class-specifier.
9932
9933    storage-class-specifier:
9934      auto
9935      register
9936      static
9937      extern
9938      mutable
9939
9940    GNU Extension:
9941
9942    storage-class-specifier:
9943      thread
9944
9945    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9946
9947 static tree
9948 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9949 {
9950   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9951     {
9952     case RID_AUTO:
9953       if (cxx_dialect != cxx98)
9954         return NULL_TREE;
9955       /* Fall through for C++98.  */
9956
9957     case RID_REGISTER:
9958     case RID_STATIC:
9959     case RID_EXTERN:
9960     case RID_MUTABLE:
9961     case RID_THREAD:
9962       /* Consume the token.  */
9963       return cp_lexer_consume_token (parser->lexer)->u.value;
9964
9965     default:
9966       return NULL_TREE;
9967     }
9968 }
9969
9970 /* Parse an (optional) function-specifier.
9971
9972    function-specifier:
9973      inline
9974      virtual
9975      explicit
9976
9977    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9978    Updates DECL_SPECS, if it is non-NULL.  */
9979
9980 static tree
9981 cp_parser_function_specifier_opt (cp_parser* parser,
9982                                   cp_decl_specifier_seq *decl_specs)
9983 {
9984   cp_token *token = cp_lexer_peek_token (parser->lexer);
9985   switch (token->keyword)
9986     {
9987     case RID_INLINE:
9988       if (decl_specs)
9989         ++decl_specs->specs[(int) ds_inline];
9990       break;
9991
9992     case RID_VIRTUAL:
9993       /* 14.5.2.3 [temp.mem]
9994
9995          A member function template shall not be virtual.  */
9996       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9997         error_at (token->location, "templates may not be %<virtual%>");
9998       else if (decl_specs)
9999         ++decl_specs->specs[(int) ds_virtual];
10000       break;
10001
10002     case RID_EXPLICIT:
10003       if (decl_specs)
10004         ++decl_specs->specs[(int) ds_explicit];
10005       break;
10006
10007     default:
10008       return NULL_TREE;
10009     }
10010
10011   /* Consume the token.  */
10012   return cp_lexer_consume_token (parser->lexer)->u.value;
10013 }
10014
10015 /* Parse a linkage-specification.
10016
10017    linkage-specification:
10018      extern string-literal { declaration-seq [opt] }
10019      extern string-literal declaration  */
10020
10021 static void
10022 cp_parser_linkage_specification (cp_parser* parser)
10023 {
10024   tree linkage;
10025
10026   /* Look for the `extern' keyword.  */
10027   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10028
10029   /* Look for the string-literal.  */
10030   linkage = cp_parser_string_literal (parser, false, false);
10031
10032   /* Transform the literal into an identifier.  If the literal is a
10033      wide-character string, or contains embedded NULs, then we can't
10034      handle it as the user wants.  */
10035   if (strlen (TREE_STRING_POINTER (linkage))
10036       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10037     {
10038       cp_parser_error (parser, "invalid linkage-specification");
10039       /* Assume C++ linkage.  */
10040       linkage = lang_name_cplusplus;
10041     }
10042   else
10043     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10044
10045   /* We're now using the new linkage.  */
10046   push_lang_context (linkage);
10047
10048   /* If the next token is a `{', then we're using the first
10049      production.  */
10050   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10051     {
10052       /* Consume the `{' token.  */
10053       cp_lexer_consume_token (parser->lexer);
10054       /* Parse the declarations.  */
10055       cp_parser_declaration_seq_opt (parser);
10056       /* Look for the closing `}'.  */
10057       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10058     }
10059   /* Otherwise, there's just one declaration.  */
10060   else
10061     {
10062       bool saved_in_unbraced_linkage_specification_p;
10063
10064       saved_in_unbraced_linkage_specification_p
10065         = parser->in_unbraced_linkage_specification_p;
10066       parser->in_unbraced_linkage_specification_p = true;
10067       cp_parser_declaration (parser);
10068       parser->in_unbraced_linkage_specification_p
10069         = saved_in_unbraced_linkage_specification_p;
10070     }
10071
10072   /* We're done with the linkage-specification.  */
10073   pop_lang_context ();
10074 }
10075
10076 /* Parse a static_assert-declaration.
10077
10078    static_assert-declaration:
10079      static_assert ( constant-expression , string-literal ) ; 
10080
10081    If MEMBER_P, this static_assert is a class member.  */
10082
10083 static void 
10084 cp_parser_static_assert(cp_parser *parser, bool member_p)
10085 {
10086   tree condition;
10087   tree message;
10088   cp_token *token;
10089   location_t saved_loc;
10090
10091   /* Peek at the `static_assert' token so we can keep track of exactly
10092      where the static assertion started.  */
10093   token = cp_lexer_peek_token (parser->lexer);
10094   saved_loc = token->location;
10095
10096   /* Look for the `static_assert' keyword.  */
10097   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10098                                   RT_STATIC_ASSERT))
10099     return;
10100
10101   /*  We know we are in a static assertion; commit to any tentative
10102       parse.  */
10103   if (cp_parser_parsing_tentatively (parser))
10104     cp_parser_commit_to_tentative_parse (parser);
10105
10106   /* Parse the `(' starting the static assertion condition.  */
10107   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10108
10109   /* Parse the constant-expression.  */
10110   condition = 
10111     cp_parser_constant_expression (parser,
10112                                    /*allow_non_constant_p=*/false,
10113                                    /*non_constant_p=*/NULL);
10114
10115   /* Parse the separating `,'.  */
10116   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10117
10118   /* Parse the string-literal message.  */
10119   message = cp_parser_string_literal (parser, 
10120                                       /*translate=*/false,
10121                                       /*wide_ok=*/true);
10122
10123   /* A `)' completes the static assertion.  */
10124   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10125     cp_parser_skip_to_closing_parenthesis (parser, 
10126                                            /*recovering=*/true, 
10127                                            /*or_comma=*/false,
10128                                            /*consume_paren=*/true);
10129
10130   /* A semicolon terminates the declaration.  */
10131   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10132
10133   /* Complete the static assertion, which may mean either processing 
10134      the static assert now or saving it for template instantiation.  */
10135   finish_static_assert (condition, message, saved_loc, member_p);
10136 }
10137
10138 /* Parse a `decltype' type. Returns the type. 
10139
10140    simple-type-specifier:
10141      decltype ( expression )  */
10142
10143 static tree
10144 cp_parser_decltype (cp_parser *parser)
10145 {
10146   tree expr;
10147   bool id_expression_or_member_access_p = false;
10148   const char *saved_message;
10149   bool saved_integral_constant_expression_p;
10150   bool saved_non_integral_constant_expression_p;
10151   cp_token *id_expr_start_token;
10152
10153   /* Look for the `decltype' token.  */
10154   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10155     return error_mark_node;
10156
10157   /* Types cannot be defined in a `decltype' expression.  Save away the
10158      old message.  */
10159   saved_message = parser->type_definition_forbidden_message;
10160
10161   /* And create the new one.  */
10162   parser->type_definition_forbidden_message
10163     = G_("types may not be defined in %<decltype%> expressions");
10164
10165   /* The restrictions on constant-expressions do not apply inside
10166      decltype expressions.  */
10167   saved_integral_constant_expression_p
10168     = parser->integral_constant_expression_p;
10169   saved_non_integral_constant_expression_p
10170     = parser->non_integral_constant_expression_p;
10171   parser->integral_constant_expression_p = false;
10172
10173   /* Do not actually evaluate the expression.  */
10174   ++cp_unevaluated_operand;
10175
10176   /* Do not warn about problems with the expression.  */
10177   ++c_inhibit_evaluation_warnings;
10178
10179   /* Parse the opening `('.  */
10180   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10181     return error_mark_node;
10182   
10183   /* First, try parsing an id-expression.  */
10184   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10185   cp_parser_parse_tentatively (parser);
10186   expr = cp_parser_id_expression (parser,
10187                                   /*template_keyword_p=*/false,
10188                                   /*check_dependency_p=*/true,
10189                                   /*template_p=*/NULL,
10190                                   /*declarator_p=*/false,
10191                                   /*optional_p=*/false);
10192
10193   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10194     {
10195       bool non_integral_constant_expression_p = false;
10196       tree id_expression = expr;
10197       cp_id_kind idk;
10198       const char *error_msg;
10199
10200       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10201         /* Lookup the name we got back from the id-expression.  */
10202         expr = cp_parser_lookup_name (parser, expr,
10203                                       none_type,
10204                                       /*is_template=*/false,
10205                                       /*is_namespace=*/false,
10206                                       /*check_dependency=*/true,
10207                                       /*ambiguous_decls=*/NULL,
10208                                       id_expr_start_token->location);
10209
10210       if (expr
10211           && expr != error_mark_node
10212           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10213           && TREE_CODE (expr) != TYPE_DECL
10214           && (TREE_CODE (expr) != BIT_NOT_EXPR
10215               || !TYPE_P (TREE_OPERAND (expr, 0)))
10216           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10217         {
10218           /* Complete lookup of the id-expression.  */
10219           expr = (finish_id_expression
10220                   (id_expression, expr, parser->scope, &idk,
10221                    /*integral_constant_expression_p=*/false,
10222                    /*allow_non_integral_constant_expression_p=*/true,
10223                    &non_integral_constant_expression_p,
10224                    /*template_p=*/false,
10225                    /*done=*/true,
10226                    /*address_p=*/false,
10227                    /*template_arg_p=*/false,
10228                    &error_msg,
10229                    id_expr_start_token->location));
10230
10231           if (expr == error_mark_node)
10232             /* We found an id-expression, but it was something that we
10233                should not have found. This is an error, not something
10234                we can recover from, so note that we found an
10235                id-expression and we'll recover as gracefully as
10236                possible.  */
10237             id_expression_or_member_access_p = true;
10238         }
10239
10240       if (expr 
10241           && expr != error_mark_node
10242           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10243         /* We have an id-expression.  */
10244         id_expression_or_member_access_p = true;
10245     }
10246
10247   if (!id_expression_or_member_access_p)
10248     {
10249       /* Abort the id-expression parse.  */
10250       cp_parser_abort_tentative_parse (parser);
10251
10252       /* Parsing tentatively, again.  */
10253       cp_parser_parse_tentatively (parser);
10254
10255       /* Parse a class member access.  */
10256       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10257                                            /*cast_p=*/false,
10258                                            /*member_access_only_p=*/true, NULL);
10259
10260       if (expr 
10261           && expr != error_mark_node
10262           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10263         /* We have an id-expression.  */
10264         id_expression_or_member_access_p = true;
10265     }
10266
10267   if (id_expression_or_member_access_p)
10268     /* We have parsed the complete id-expression or member access.  */
10269     cp_parser_parse_definitely (parser);
10270   else
10271     {
10272       bool saved_greater_than_is_operator_p;
10273
10274       /* Abort our attempt to parse an id-expression or member access
10275          expression.  */
10276       cp_parser_abort_tentative_parse (parser);
10277
10278       /* Within a parenthesized expression, a `>' token is always
10279          the greater-than operator.  */
10280       saved_greater_than_is_operator_p
10281         = parser->greater_than_is_operator_p;
10282       parser->greater_than_is_operator_p = true;
10283
10284       /* Parse a full expression.  */
10285       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10286
10287       /* The `>' token might be the end of a template-id or
10288          template-parameter-list now.  */
10289       parser->greater_than_is_operator_p
10290         = saved_greater_than_is_operator_p;
10291     }
10292
10293   /* Go back to evaluating expressions.  */
10294   --cp_unevaluated_operand;
10295   --c_inhibit_evaluation_warnings;
10296
10297   /* Restore the old message and the integral constant expression
10298      flags.  */
10299   parser->type_definition_forbidden_message = saved_message;
10300   parser->integral_constant_expression_p
10301     = saved_integral_constant_expression_p;
10302   parser->non_integral_constant_expression_p
10303     = saved_non_integral_constant_expression_p;
10304
10305   if (expr == error_mark_node)
10306     {
10307       /* Skip everything up to the closing `)'.  */
10308       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10309                                              /*consume_paren=*/true);
10310       return error_mark_node;
10311     }
10312   
10313   /* Parse to the closing `)'.  */
10314   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10315     {
10316       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10317                                              /*consume_paren=*/true);
10318       return error_mark_node;
10319     }
10320
10321   return finish_decltype_type (expr, id_expression_or_member_access_p);
10322 }
10323
10324 /* Special member functions [gram.special] */
10325
10326 /* Parse a conversion-function-id.
10327
10328    conversion-function-id:
10329      operator conversion-type-id
10330
10331    Returns an IDENTIFIER_NODE representing the operator.  */
10332
10333 static tree
10334 cp_parser_conversion_function_id (cp_parser* parser)
10335 {
10336   tree type;
10337   tree saved_scope;
10338   tree saved_qualifying_scope;
10339   tree saved_object_scope;
10340   tree pushed_scope = NULL_TREE;
10341
10342   /* Look for the `operator' token.  */
10343   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10344     return error_mark_node;
10345   /* When we parse the conversion-type-id, the current scope will be
10346      reset.  However, we need that information in able to look up the
10347      conversion function later, so we save it here.  */
10348   saved_scope = parser->scope;
10349   saved_qualifying_scope = parser->qualifying_scope;
10350   saved_object_scope = parser->object_scope;
10351   /* We must enter the scope of the class so that the names of
10352      entities declared within the class are available in the
10353      conversion-type-id.  For example, consider:
10354
10355        struct S {
10356          typedef int I;
10357          operator I();
10358        };
10359
10360        S::operator I() { ... }
10361
10362      In order to see that `I' is a type-name in the definition, we
10363      must be in the scope of `S'.  */
10364   if (saved_scope)
10365     pushed_scope = push_scope (saved_scope);
10366   /* Parse the conversion-type-id.  */
10367   type = cp_parser_conversion_type_id (parser);
10368   /* Leave the scope of the class, if any.  */
10369   if (pushed_scope)
10370     pop_scope (pushed_scope);
10371   /* Restore the saved scope.  */
10372   parser->scope = saved_scope;
10373   parser->qualifying_scope = saved_qualifying_scope;
10374   parser->object_scope = saved_object_scope;
10375   /* If the TYPE is invalid, indicate failure.  */
10376   if (type == error_mark_node)
10377     return error_mark_node;
10378   return mangle_conv_op_name_for_type (type);
10379 }
10380
10381 /* Parse a conversion-type-id:
10382
10383    conversion-type-id:
10384      type-specifier-seq conversion-declarator [opt]
10385
10386    Returns the TYPE specified.  */
10387
10388 static tree
10389 cp_parser_conversion_type_id (cp_parser* parser)
10390 {
10391   tree attributes;
10392   cp_decl_specifier_seq type_specifiers;
10393   cp_declarator *declarator;
10394   tree type_specified;
10395
10396   /* Parse the attributes.  */
10397   attributes = cp_parser_attributes_opt (parser);
10398   /* Parse the type-specifiers.  */
10399   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10400                                 /*is_trailing_return=*/false,
10401                                 &type_specifiers);
10402   /* If that didn't work, stop.  */
10403   if (type_specifiers.type == error_mark_node)
10404     return error_mark_node;
10405   /* Parse the conversion-declarator.  */
10406   declarator = cp_parser_conversion_declarator_opt (parser);
10407
10408   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10409                                     /*initialized=*/0, &attributes);
10410   if (attributes)
10411     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10412
10413   /* Don't give this error when parsing tentatively.  This happens to
10414      work because we always parse this definitively once.  */
10415   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10416       && type_uses_auto (type_specified))
10417     {
10418       error ("invalid use of %<auto%> in conversion operator");
10419       return error_mark_node;
10420     }
10421
10422   return type_specified;
10423 }
10424
10425 /* Parse an (optional) conversion-declarator.
10426
10427    conversion-declarator:
10428      ptr-operator conversion-declarator [opt]
10429
10430    */
10431
10432 static cp_declarator *
10433 cp_parser_conversion_declarator_opt (cp_parser* parser)
10434 {
10435   enum tree_code code;
10436   tree class_type;
10437   cp_cv_quals cv_quals;
10438
10439   /* We don't know if there's a ptr-operator next, or not.  */
10440   cp_parser_parse_tentatively (parser);
10441   /* Try the ptr-operator.  */
10442   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10443   /* If it worked, look for more conversion-declarators.  */
10444   if (cp_parser_parse_definitely (parser))
10445     {
10446       cp_declarator *declarator;
10447
10448       /* Parse another optional declarator.  */
10449       declarator = cp_parser_conversion_declarator_opt (parser);
10450
10451       return cp_parser_make_indirect_declarator
10452         (code, class_type, cv_quals, declarator);
10453    }
10454
10455   return NULL;
10456 }
10457
10458 /* Parse an (optional) ctor-initializer.
10459
10460    ctor-initializer:
10461      : mem-initializer-list
10462
10463    Returns TRUE iff the ctor-initializer was actually present.  */
10464
10465 static bool
10466 cp_parser_ctor_initializer_opt (cp_parser* parser)
10467 {
10468   /* If the next token is not a `:', then there is no
10469      ctor-initializer.  */
10470   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10471     {
10472       /* Do default initialization of any bases and members.  */
10473       if (DECL_CONSTRUCTOR_P (current_function_decl))
10474         finish_mem_initializers (NULL_TREE);
10475
10476       return false;
10477     }
10478
10479   /* Consume the `:' token.  */
10480   cp_lexer_consume_token (parser->lexer);
10481   /* And the mem-initializer-list.  */
10482   cp_parser_mem_initializer_list (parser);
10483
10484   return true;
10485 }
10486
10487 /* Parse a mem-initializer-list.
10488
10489    mem-initializer-list:
10490      mem-initializer ... [opt]
10491      mem-initializer ... [opt] , mem-initializer-list  */
10492
10493 static void
10494 cp_parser_mem_initializer_list (cp_parser* parser)
10495 {
10496   tree mem_initializer_list = NULL_TREE;
10497   cp_token *token = cp_lexer_peek_token (parser->lexer);
10498
10499   /* Let the semantic analysis code know that we are starting the
10500      mem-initializer-list.  */
10501   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10502     error_at (token->location,
10503               "only constructors take member initializers");
10504
10505   /* Loop through the list.  */
10506   while (true)
10507     {
10508       tree mem_initializer;
10509
10510       token = cp_lexer_peek_token (parser->lexer);
10511       /* Parse the mem-initializer.  */
10512       mem_initializer = cp_parser_mem_initializer (parser);
10513       /* If the next token is a `...', we're expanding member initializers. */
10514       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10515         {
10516           /* Consume the `...'. */
10517           cp_lexer_consume_token (parser->lexer);
10518
10519           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10520              can be expanded but members cannot. */
10521           if (mem_initializer != error_mark_node
10522               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10523             {
10524               error_at (token->location,
10525                         "cannot expand initializer for member %<%D%>",
10526                         TREE_PURPOSE (mem_initializer));
10527               mem_initializer = error_mark_node;
10528             }
10529
10530           /* Construct the pack expansion type. */
10531           if (mem_initializer != error_mark_node)
10532             mem_initializer = make_pack_expansion (mem_initializer);
10533         }
10534       /* Add it to the list, unless it was erroneous.  */
10535       if (mem_initializer != error_mark_node)
10536         {
10537           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10538           mem_initializer_list = mem_initializer;
10539         }
10540       /* If the next token is not a `,', we're done.  */
10541       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10542         break;
10543       /* Consume the `,' token.  */
10544       cp_lexer_consume_token (parser->lexer);
10545     }
10546
10547   /* Perform semantic analysis.  */
10548   if (DECL_CONSTRUCTOR_P (current_function_decl))
10549     finish_mem_initializers (mem_initializer_list);
10550 }
10551
10552 /* Parse a mem-initializer.
10553
10554    mem-initializer:
10555      mem-initializer-id ( expression-list [opt] )
10556      mem-initializer-id braced-init-list
10557
10558    GNU extension:
10559
10560    mem-initializer:
10561      ( expression-list [opt] )
10562
10563    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10564    class) or FIELD_DECL (for a non-static data member) to initialize;
10565    the TREE_VALUE is the expression-list.  An empty initialization
10566    list is represented by void_list_node.  */
10567
10568 static tree
10569 cp_parser_mem_initializer (cp_parser* parser)
10570 {
10571   tree mem_initializer_id;
10572   tree expression_list;
10573   tree member;
10574   cp_token *token = cp_lexer_peek_token (parser->lexer);
10575
10576   /* Find out what is being initialized.  */
10577   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10578     {
10579       permerror (token->location,
10580                  "anachronistic old-style base class initializer");
10581       mem_initializer_id = NULL_TREE;
10582     }
10583   else
10584     {
10585       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10586       if (mem_initializer_id == error_mark_node)
10587         return mem_initializer_id;
10588     }
10589   member = expand_member_init (mem_initializer_id);
10590   if (member && !DECL_P (member))
10591     in_base_initializer = 1;
10592
10593   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10594     {
10595       bool expr_non_constant_p;
10596       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10597       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10598       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10599       expression_list = build_tree_list (NULL_TREE, expression_list);
10600     }
10601   else
10602     {
10603       VEC(tree,gc)* vec;
10604       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10605                                                      /*cast_p=*/false,
10606                                                      /*allow_expansion_p=*/true,
10607                                                      /*non_constant_p=*/NULL);
10608       if (vec == NULL)
10609         return error_mark_node;
10610       expression_list = build_tree_list_vec (vec);
10611       release_tree_vector (vec);
10612     }
10613
10614   if (expression_list == error_mark_node)
10615     return error_mark_node;
10616   if (!expression_list)
10617     expression_list = void_type_node;
10618
10619   in_base_initializer = 0;
10620
10621   return member ? build_tree_list (member, expression_list) : error_mark_node;
10622 }
10623
10624 /* Parse a mem-initializer-id.
10625
10626    mem-initializer-id:
10627      :: [opt] nested-name-specifier [opt] class-name
10628      identifier
10629
10630    Returns a TYPE indicating the class to be initializer for the first
10631    production.  Returns an IDENTIFIER_NODE indicating the data member
10632    to be initialized for the second production.  */
10633
10634 static tree
10635 cp_parser_mem_initializer_id (cp_parser* parser)
10636 {
10637   bool global_scope_p;
10638   bool nested_name_specifier_p;
10639   bool template_p = false;
10640   tree id;
10641
10642   cp_token *token = cp_lexer_peek_token (parser->lexer);
10643
10644   /* `typename' is not allowed in this context ([temp.res]).  */
10645   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10646     {
10647       error_at (token->location, 
10648                 "keyword %<typename%> not allowed in this context (a qualified "
10649                 "member initializer is implicitly a type)");
10650       cp_lexer_consume_token (parser->lexer);
10651     }
10652   /* Look for the optional `::' operator.  */
10653   global_scope_p
10654     = (cp_parser_global_scope_opt (parser,
10655                                    /*current_scope_valid_p=*/false)
10656        != NULL_TREE);
10657   /* Look for the optional nested-name-specifier.  The simplest way to
10658      implement:
10659
10660        [temp.res]
10661
10662        The keyword `typename' is not permitted in a base-specifier or
10663        mem-initializer; in these contexts a qualified name that
10664        depends on a template-parameter is implicitly assumed to be a
10665        type name.
10666
10667      is to assume that we have seen the `typename' keyword at this
10668      point.  */
10669   nested_name_specifier_p
10670     = (cp_parser_nested_name_specifier_opt (parser,
10671                                             /*typename_keyword_p=*/true,
10672                                             /*check_dependency_p=*/true,
10673                                             /*type_p=*/true,
10674                                             /*is_declaration=*/true)
10675        != NULL_TREE);
10676   if (nested_name_specifier_p)
10677     template_p = cp_parser_optional_template_keyword (parser);
10678   /* If there is a `::' operator or a nested-name-specifier, then we
10679      are definitely looking for a class-name.  */
10680   if (global_scope_p || nested_name_specifier_p)
10681     return cp_parser_class_name (parser,
10682                                  /*typename_keyword_p=*/true,
10683                                  /*template_keyword_p=*/template_p,
10684                                  typename_type,
10685                                  /*check_dependency_p=*/true,
10686                                  /*class_head_p=*/false,
10687                                  /*is_declaration=*/true);
10688   /* Otherwise, we could also be looking for an ordinary identifier.  */
10689   cp_parser_parse_tentatively (parser);
10690   /* Try a class-name.  */
10691   id = cp_parser_class_name (parser,
10692                              /*typename_keyword_p=*/true,
10693                              /*template_keyword_p=*/false,
10694                              none_type,
10695                              /*check_dependency_p=*/true,
10696                              /*class_head_p=*/false,
10697                              /*is_declaration=*/true);
10698   /* If we found one, we're done.  */
10699   if (cp_parser_parse_definitely (parser))
10700     return id;
10701   /* Otherwise, look for an ordinary identifier.  */
10702   return cp_parser_identifier (parser);
10703 }
10704
10705 /* Overloading [gram.over] */
10706
10707 /* Parse an operator-function-id.
10708
10709    operator-function-id:
10710      operator operator
10711
10712    Returns an IDENTIFIER_NODE for the operator which is a
10713    human-readable spelling of the identifier, e.g., `operator +'.  */
10714
10715 static tree
10716 cp_parser_operator_function_id (cp_parser* parser)
10717 {
10718   /* Look for the `operator' keyword.  */
10719   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10720     return error_mark_node;
10721   /* And then the name of the operator itself.  */
10722   return cp_parser_operator (parser);
10723 }
10724
10725 /* Parse an operator.
10726
10727    operator:
10728      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10729      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10730      || ++ -- , ->* -> () []
10731
10732    GNU Extensions:
10733
10734    operator:
10735      <? >? <?= >?=
10736
10737    Returns an IDENTIFIER_NODE for the operator which is a
10738    human-readable spelling of the identifier, e.g., `operator +'.  */
10739
10740 static tree
10741 cp_parser_operator (cp_parser* parser)
10742 {
10743   tree id = NULL_TREE;
10744   cp_token *token;
10745
10746   /* Peek at the next token.  */
10747   token = cp_lexer_peek_token (parser->lexer);
10748   /* Figure out which operator we have.  */
10749   switch (token->type)
10750     {
10751     case CPP_KEYWORD:
10752       {
10753         enum tree_code op;
10754
10755         /* The keyword should be either `new' or `delete'.  */
10756         if (token->keyword == RID_NEW)
10757           op = NEW_EXPR;
10758         else if (token->keyword == RID_DELETE)
10759           op = DELETE_EXPR;
10760         else
10761           break;
10762
10763         /* Consume the `new' or `delete' token.  */
10764         cp_lexer_consume_token (parser->lexer);
10765
10766         /* Peek at the next token.  */
10767         token = cp_lexer_peek_token (parser->lexer);
10768         /* If it's a `[' token then this is the array variant of the
10769            operator.  */
10770         if (token->type == CPP_OPEN_SQUARE)
10771           {
10772             /* Consume the `[' token.  */
10773             cp_lexer_consume_token (parser->lexer);
10774             /* Look for the `]' token.  */
10775             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10776             id = ansi_opname (op == NEW_EXPR
10777                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10778           }
10779         /* Otherwise, we have the non-array variant.  */
10780         else
10781           id = ansi_opname (op);
10782
10783         return id;
10784       }
10785
10786     case CPP_PLUS:
10787       id = ansi_opname (PLUS_EXPR);
10788       break;
10789
10790     case CPP_MINUS:
10791       id = ansi_opname (MINUS_EXPR);
10792       break;
10793
10794     case CPP_MULT:
10795       id = ansi_opname (MULT_EXPR);
10796       break;
10797
10798     case CPP_DIV:
10799       id = ansi_opname (TRUNC_DIV_EXPR);
10800       break;
10801
10802     case CPP_MOD:
10803       id = ansi_opname (TRUNC_MOD_EXPR);
10804       break;
10805
10806     case CPP_XOR:
10807       id = ansi_opname (BIT_XOR_EXPR);
10808       break;
10809
10810     case CPP_AND:
10811       id = ansi_opname (BIT_AND_EXPR);
10812       break;
10813
10814     case CPP_OR:
10815       id = ansi_opname (BIT_IOR_EXPR);
10816       break;
10817
10818     case CPP_COMPL:
10819       id = ansi_opname (BIT_NOT_EXPR);
10820       break;
10821
10822     case CPP_NOT:
10823       id = ansi_opname (TRUTH_NOT_EXPR);
10824       break;
10825
10826     case CPP_EQ:
10827       id = ansi_assopname (NOP_EXPR);
10828       break;
10829
10830     case CPP_LESS:
10831       id = ansi_opname (LT_EXPR);
10832       break;
10833
10834     case CPP_GREATER:
10835       id = ansi_opname (GT_EXPR);
10836       break;
10837
10838     case CPP_PLUS_EQ:
10839       id = ansi_assopname (PLUS_EXPR);
10840       break;
10841
10842     case CPP_MINUS_EQ:
10843       id = ansi_assopname (MINUS_EXPR);
10844       break;
10845
10846     case CPP_MULT_EQ:
10847       id = ansi_assopname (MULT_EXPR);
10848       break;
10849
10850     case CPP_DIV_EQ:
10851       id = ansi_assopname (TRUNC_DIV_EXPR);
10852       break;
10853
10854     case CPP_MOD_EQ:
10855       id = ansi_assopname (TRUNC_MOD_EXPR);
10856       break;
10857
10858     case CPP_XOR_EQ:
10859       id = ansi_assopname (BIT_XOR_EXPR);
10860       break;
10861
10862     case CPP_AND_EQ:
10863       id = ansi_assopname (BIT_AND_EXPR);
10864       break;
10865
10866     case CPP_OR_EQ:
10867       id = ansi_assopname (BIT_IOR_EXPR);
10868       break;
10869
10870     case CPP_LSHIFT:
10871       id = ansi_opname (LSHIFT_EXPR);
10872       break;
10873
10874     case CPP_RSHIFT:
10875       id = ansi_opname (RSHIFT_EXPR);
10876       break;
10877
10878     case CPP_LSHIFT_EQ:
10879       id = ansi_assopname (LSHIFT_EXPR);
10880       break;
10881
10882     case CPP_RSHIFT_EQ:
10883       id = ansi_assopname (RSHIFT_EXPR);
10884       break;
10885
10886     case CPP_EQ_EQ:
10887       id = ansi_opname (EQ_EXPR);
10888       break;
10889
10890     case CPP_NOT_EQ:
10891       id = ansi_opname (NE_EXPR);
10892       break;
10893
10894     case CPP_LESS_EQ:
10895       id = ansi_opname (LE_EXPR);
10896       break;
10897
10898     case CPP_GREATER_EQ:
10899       id = ansi_opname (GE_EXPR);
10900       break;
10901
10902     case CPP_AND_AND:
10903       id = ansi_opname (TRUTH_ANDIF_EXPR);
10904       break;
10905
10906     case CPP_OR_OR:
10907       id = ansi_opname (TRUTH_ORIF_EXPR);
10908       break;
10909
10910     case CPP_PLUS_PLUS:
10911       id = ansi_opname (POSTINCREMENT_EXPR);
10912       break;
10913
10914     case CPP_MINUS_MINUS:
10915       id = ansi_opname (PREDECREMENT_EXPR);
10916       break;
10917
10918     case CPP_COMMA:
10919       id = ansi_opname (COMPOUND_EXPR);
10920       break;
10921
10922     case CPP_DEREF_STAR:
10923       id = ansi_opname (MEMBER_REF);
10924       break;
10925
10926     case CPP_DEREF:
10927       id = ansi_opname (COMPONENT_REF);
10928       break;
10929
10930     case CPP_OPEN_PAREN:
10931       /* Consume the `('.  */
10932       cp_lexer_consume_token (parser->lexer);
10933       /* Look for the matching `)'.  */
10934       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10935       return ansi_opname (CALL_EXPR);
10936
10937     case CPP_OPEN_SQUARE:
10938       /* Consume the `['.  */
10939       cp_lexer_consume_token (parser->lexer);
10940       /* Look for the matching `]'.  */
10941       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10942       return ansi_opname (ARRAY_REF);
10943
10944     default:
10945       /* Anything else is an error.  */
10946       break;
10947     }
10948
10949   /* If we have selected an identifier, we need to consume the
10950      operator token.  */
10951   if (id)
10952     cp_lexer_consume_token (parser->lexer);
10953   /* Otherwise, no valid operator name was present.  */
10954   else
10955     {
10956       cp_parser_error (parser, "expected operator");
10957       id = error_mark_node;
10958     }
10959
10960   return id;
10961 }
10962
10963 /* Parse a template-declaration.
10964
10965    template-declaration:
10966      export [opt] template < template-parameter-list > declaration
10967
10968    If MEMBER_P is TRUE, this template-declaration occurs within a
10969    class-specifier.
10970
10971    The grammar rule given by the standard isn't correct.  What
10972    is really meant is:
10973
10974    template-declaration:
10975      export [opt] template-parameter-list-seq
10976        decl-specifier-seq [opt] init-declarator [opt] ;
10977      export [opt] template-parameter-list-seq
10978        function-definition
10979
10980    template-parameter-list-seq:
10981      template-parameter-list-seq [opt]
10982      template < template-parameter-list >  */
10983
10984 static void
10985 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10986 {
10987   /* Check for `export'.  */
10988   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10989     {
10990       /* Consume the `export' token.  */
10991       cp_lexer_consume_token (parser->lexer);
10992       /* Warn that we do not support `export'.  */
10993       warning (0, "keyword %<export%> not implemented, and will be ignored");
10994     }
10995
10996   cp_parser_template_declaration_after_export (parser, member_p);
10997 }
10998
10999 /* Parse a template-parameter-list.
11000
11001    template-parameter-list:
11002      template-parameter
11003      template-parameter-list , template-parameter
11004
11005    Returns a TREE_LIST.  Each node represents a template parameter.
11006    The nodes are connected via their TREE_CHAINs.  */
11007
11008 static tree
11009 cp_parser_template_parameter_list (cp_parser* parser)
11010 {
11011   tree parameter_list = NULL_TREE;
11012
11013   begin_template_parm_list ();
11014   while (true)
11015     {
11016       tree parameter;
11017       bool is_non_type;
11018       bool is_parameter_pack;
11019       location_t parm_loc;
11020
11021       /* Parse the template-parameter.  */
11022       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11023       parameter = cp_parser_template_parameter (parser, 
11024                                                 &is_non_type,
11025                                                 &is_parameter_pack);
11026       /* Add it to the list.  */
11027       if (parameter != error_mark_node)
11028         parameter_list = process_template_parm (parameter_list,
11029                                                 parm_loc,
11030                                                 parameter,
11031                                                 is_non_type,
11032                                                 is_parameter_pack);
11033       else
11034        {
11035          tree err_parm = build_tree_list (parameter, parameter);
11036          TREE_VALUE (err_parm) = error_mark_node;
11037          parameter_list = chainon (parameter_list, err_parm);
11038        }
11039
11040       /* If the next token is not a `,', we're done.  */
11041       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11042         break;
11043       /* Otherwise, consume the `,' token.  */
11044       cp_lexer_consume_token (parser->lexer);
11045     }
11046
11047   return end_template_parm_list (parameter_list);
11048 }
11049
11050 /* Parse a template-parameter.
11051
11052    template-parameter:
11053      type-parameter
11054      parameter-declaration
11055
11056    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11057    the parameter.  The TREE_PURPOSE is the default value, if any.
11058    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11059    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11060    set to true iff this parameter is a parameter pack. */
11061
11062 static tree
11063 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11064                               bool *is_parameter_pack)
11065 {
11066   cp_token *token;
11067   cp_parameter_declarator *parameter_declarator;
11068   cp_declarator *id_declarator;
11069   tree parm;
11070
11071   /* Assume it is a type parameter or a template parameter.  */
11072   *is_non_type = false;
11073   /* Assume it not a parameter pack. */
11074   *is_parameter_pack = false;
11075   /* Peek at the next token.  */
11076   token = cp_lexer_peek_token (parser->lexer);
11077   /* If it is `class' or `template', we have a type-parameter.  */
11078   if (token->keyword == RID_TEMPLATE)
11079     return cp_parser_type_parameter (parser, is_parameter_pack);
11080   /* If it is `class' or `typename' we do not know yet whether it is a
11081      type parameter or a non-type parameter.  Consider:
11082
11083        template <typename T, typename T::X X> ...
11084
11085      or:
11086
11087        template <class C, class D*> ...
11088
11089      Here, the first parameter is a type parameter, and the second is
11090      a non-type parameter.  We can tell by looking at the token after
11091      the identifier -- if it is a `,', `=', or `>' then we have a type
11092      parameter.  */
11093   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11094     {
11095       /* Peek at the token after `class' or `typename'.  */
11096       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11097       /* If it's an ellipsis, we have a template type parameter
11098          pack. */
11099       if (token->type == CPP_ELLIPSIS)
11100         return cp_parser_type_parameter (parser, is_parameter_pack);
11101       /* If it's an identifier, skip it.  */
11102       if (token->type == CPP_NAME)
11103         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11104       /* Now, see if the token looks like the end of a template
11105          parameter.  */
11106       if (token->type == CPP_COMMA
11107           || token->type == CPP_EQ
11108           || token->type == CPP_GREATER)
11109         return cp_parser_type_parameter (parser, is_parameter_pack);
11110     }
11111
11112   /* Otherwise, it is a non-type parameter.
11113
11114      [temp.param]
11115
11116      When parsing a default template-argument for a non-type
11117      template-parameter, the first non-nested `>' is taken as the end
11118      of the template parameter-list rather than a greater-than
11119      operator.  */
11120   *is_non_type = true;
11121   parameter_declarator
11122      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11123                                         /*parenthesized_p=*/NULL);
11124
11125   /* If the parameter declaration is marked as a parameter pack, set
11126      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11127      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11128      grokdeclarator. */
11129   if (parameter_declarator
11130       && parameter_declarator->declarator
11131       && parameter_declarator->declarator->parameter_pack_p)
11132     {
11133       *is_parameter_pack = true;
11134       parameter_declarator->declarator->parameter_pack_p = false;
11135     }
11136
11137   /* If the next token is an ellipsis, and we don't already have it
11138      marked as a parameter pack, then we have a parameter pack (that
11139      has no declarator).  */
11140   if (!*is_parameter_pack
11141       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11142       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11143     {
11144       /* Consume the `...'.  */
11145       cp_lexer_consume_token (parser->lexer);
11146       maybe_warn_variadic_templates ();
11147       
11148       *is_parameter_pack = true;
11149     }
11150   /* We might end up with a pack expansion as the type of the non-type
11151      template parameter, in which case this is a non-type template
11152      parameter pack.  */
11153   else if (parameter_declarator
11154            && parameter_declarator->decl_specifiers.type
11155            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11156     {
11157       *is_parameter_pack = true;
11158       parameter_declarator->decl_specifiers.type = 
11159         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11160     }
11161
11162   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11163     {
11164       /* Parameter packs cannot have default arguments.  However, a
11165          user may try to do so, so we'll parse them and give an
11166          appropriate diagnostic here.  */
11167
11168       /* Consume the `='.  */
11169       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11170       cp_lexer_consume_token (parser->lexer);
11171       
11172       /* Find the name of the parameter pack.  */     
11173       id_declarator = parameter_declarator->declarator;
11174       while (id_declarator && id_declarator->kind != cdk_id)
11175         id_declarator = id_declarator->declarator;
11176       
11177       if (id_declarator && id_declarator->kind == cdk_id)
11178         error_at (start_token->location,
11179                   "template parameter pack %qD cannot have a default argument",
11180                   id_declarator->u.id.unqualified_name);
11181       else
11182         error_at (start_token->location,
11183                   "template parameter pack cannot have a default argument");
11184       
11185       /* Parse the default argument, but throw away the result.  */
11186       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11187     }
11188
11189   parm = grokdeclarator (parameter_declarator->declarator,
11190                          &parameter_declarator->decl_specifiers,
11191                          TPARM, /*initialized=*/0,
11192                          /*attrlist=*/NULL);
11193   if (parm == error_mark_node)
11194     return error_mark_node;
11195
11196   return build_tree_list (parameter_declarator->default_argument, parm);
11197 }
11198
11199 /* Parse a type-parameter.
11200
11201    type-parameter:
11202      class identifier [opt]
11203      class identifier [opt] = type-id
11204      typename identifier [opt]
11205      typename identifier [opt] = type-id
11206      template < template-parameter-list > class identifier [opt]
11207      template < template-parameter-list > class identifier [opt]
11208        = id-expression
11209
11210    GNU Extension (variadic templates):
11211
11212    type-parameter:
11213      class ... identifier [opt]
11214      typename ... identifier [opt]
11215
11216    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11217    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11218    the declaration of the parameter.
11219
11220    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11221
11222 static tree
11223 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11224 {
11225   cp_token *token;
11226   tree parameter;
11227
11228   /* Look for a keyword to tell us what kind of parameter this is.  */
11229   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11230   if (!token)
11231     return error_mark_node;
11232
11233   switch (token->keyword)
11234     {
11235     case RID_CLASS:
11236     case RID_TYPENAME:
11237       {
11238         tree identifier;
11239         tree default_argument;
11240
11241         /* If the next token is an ellipsis, we have a template
11242            argument pack. */
11243         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11244           {
11245             /* Consume the `...' token. */
11246             cp_lexer_consume_token (parser->lexer);
11247             maybe_warn_variadic_templates ();
11248
11249             *is_parameter_pack = true;
11250           }
11251
11252         /* If the next token is an identifier, then it names the
11253            parameter.  */
11254         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11255           identifier = cp_parser_identifier (parser);
11256         else
11257           identifier = NULL_TREE;
11258
11259         /* Create the parameter.  */
11260         parameter = finish_template_type_parm (class_type_node, identifier);
11261
11262         /* If the next token is an `=', we have a default argument.  */
11263         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11264           {
11265             /* Consume the `=' token.  */
11266             cp_lexer_consume_token (parser->lexer);
11267             /* Parse the default-argument.  */
11268             push_deferring_access_checks (dk_no_deferred);
11269             default_argument = cp_parser_type_id (parser);
11270
11271             /* Template parameter packs cannot have default
11272                arguments. */
11273             if (*is_parameter_pack)
11274               {
11275                 if (identifier)
11276                   error_at (token->location,
11277                             "template parameter pack %qD cannot have a "
11278                             "default argument", identifier);
11279                 else
11280                   error_at (token->location,
11281                             "template parameter packs cannot have "
11282                             "default arguments");
11283                 default_argument = NULL_TREE;
11284               }
11285             pop_deferring_access_checks ();
11286           }
11287         else
11288           default_argument = NULL_TREE;
11289
11290         /* Create the combined representation of the parameter and the
11291            default argument.  */
11292         parameter = build_tree_list (default_argument, parameter);
11293       }
11294       break;
11295
11296     case RID_TEMPLATE:
11297       {
11298         tree identifier;
11299         tree default_argument;
11300
11301         /* Look for the `<'.  */
11302         cp_parser_require (parser, CPP_LESS, RT_LESS);
11303         /* Parse the template-parameter-list.  */
11304         cp_parser_template_parameter_list (parser);
11305         /* Look for the `>'.  */
11306         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11307         /* Look for the `class' keyword.  */
11308         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11309         /* If the next token is an ellipsis, we have a template
11310            argument pack. */
11311         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11312           {
11313             /* Consume the `...' token. */
11314             cp_lexer_consume_token (parser->lexer);
11315             maybe_warn_variadic_templates ();
11316
11317             *is_parameter_pack = true;
11318           }
11319         /* If the next token is an `=', then there is a
11320            default-argument.  If the next token is a `>', we are at
11321            the end of the parameter-list.  If the next token is a `,',
11322            then we are at the end of this parameter.  */
11323         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11324             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11325             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11326           {
11327             identifier = cp_parser_identifier (parser);
11328             /* Treat invalid names as if the parameter were nameless.  */
11329             if (identifier == error_mark_node)
11330               identifier = NULL_TREE;
11331           }
11332         else
11333           identifier = NULL_TREE;
11334
11335         /* Create the template parameter.  */
11336         parameter = finish_template_template_parm (class_type_node,
11337                                                    identifier);
11338
11339         /* If the next token is an `=', then there is a
11340            default-argument.  */
11341         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11342           {
11343             bool is_template;
11344
11345             /* Consume the `='.  */
11346             cp_lexer_consume_token (parser->lexer);
11347             /* Parse the id-expression.  */
11348             push_deferring_access_checks (dk_no_deferred);
11349             /* save token before parsing the id-expression, for error
11350                reporting */
11351             token = cp_lexer_peek_token (parser->lexer);
11352             default_argument
11353               = cp_parser_id_expression (parser,
11354                                          /*template_keyword_p=*/false,
11355                                          /*check_dependency_p=*/true,
11356                                          /*template_p=*/&is_template,
11357                                          /*declarator_p=*/false,
11358                                          /*optional_p=*/false);
11359             if (TREE_CODE (default_argument) == TYPE_DECL)
11360               /* If the id-expression was a template-id that refers to
11361                  a template-class, we already have the declaration here,
11362                  so no further lookup is needed.  */
11363                  ;
11364             else
11365               /* Look up the name.  */
11366               default_argument
11367                 = cp_parser_lookup_name (parser, default_argument,
11368                                          none_type,
11369                                          /*is_template=*/is_template,
11370                                          /*is_namespace=*/false,
11371                                          /*check_dependency=*/true,
11372                                          /*ambiguous_decls=*/NULL,
11373                                          token->location);
11374             /* See if the default argument is valid.  */
11375             default_argument
11376               = check_template_template_default_arg (default_argument);
11377
11378             /* Template parameter packs cannot have default
11379                arguments. */
11380             if (*is_parameter_pack)
11381               {
11382                 if (identifier)
11383                   error_at (token->location,
11384                             "template parameter pack %qD cannot "
11385                             "have a default argument",
11386                             identifier);
11387                 else
11388                   error_at (token->location, "template parameter packs cannot "
11389                             "have default arguments");
11390                 default_argument = NULL_TREE;
11391               }
11392             pop_deferring_access_checks ();
11393           }
11394         else
11395           default_argument = NULL_TREE;
11396
11397         /* Create the combined representation of the parameter and the
11398            default argument.  */
11399         parameter = build_tree_list (default_argument, parameter);
11400       }
11401       break;
11402
11403     default:
11404       gcc_unreachable ();
11405       break;
11406     }
11407
11408   return parameter;
11409 }
11410
11411 /* Parse a template-id.
11412
11413    template-id:
11414      template-name < template-argument-list [opt] >
11415
11416    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11417    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11418    returned.  Otherwise, if the template-name names a function, or set
11419    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11420    names a class, returns a TYPE_DECL for the specialization.
11421
11422    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11423    uninstantiated templates.  */
11424
11425 static tree
11426 cp_parser_template_id (cp_parser *parser,
11427                        bool template_keyword_p,
11428                        bool check_dependency_p,
11429                        bool is_declaration)
11430 {
11431   int i;
11432   tree templ;
11433   tree arguments;
11434   tree template_id;
11435   cp_token_position start_of_id = 0;
11436   deferred_access_check *chk;
11437   VEC (deferred_access_check,gc) *access_check;
11438   cp_token *next_token = NULL, *next_token_2 = NULL;
11439   bool is_identifier;
11440
11441   /* If the next token corresponds to a template-id, there is no need
11442      to reparse it.  */
11443   next_token = cp_lexer_peek_token (parser->lexer);
11444   if (next_token->type == CPP_TEMPLATE_ID)
11445     {
11446       struct tree_check *check_value;
11447
11448       /* Get the stored value.  */
11449       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11450       /* Perform any access checks that were deferred.  */
11451       access_check = check_value->checks;
11452       if (access_check)
11453         {
11454           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11455             perform_or_defer_access_check (chk->binfo,
11456                                            chk->decl,
11457                                            chk->diag_decl);
11458         }
11459       /* Return the stored value.  */
11460       return check_value->value;
11461     }
11462
11463   /* Avoid performing name lookup if there is no possibility of
11464      finding a template-id.  */
11465   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11466       || (next_token->type == CPP_NAME
11467           && !cp_parser_nth_token_starts_template_argument_list_p
11468                (parser, 2)))
11469     {
11470       cp_parser_error (parser, "expected template-id");
11471       return error_mark_node;
11472     }
11473
11474   /* Remember where the template-id starts.  */
11475   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11476     start_of_id = cp_lexer_token_position (parser->lexer, false);
11477
11478   push_deferring_access_checks (dk_deferred);
11479
11480   /* Parse the template-name.  */
11481   is_identifier = false;
11482   templ = cp_parser_template_name (parser, template_keyword_p,
11483                                    check_dependency_p,
11484                                    is_declaration,
11485                                    &is_identifier);
11486   if (templ == error_mark_node || is_identifier)
11487     {
11488       pop_deferring_access_checks ();
11489       return templ;
11490     }
11491
11492   /* If we find the sequence `[:' after a template-name, it's probably
11493      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11494      parse correctly the argument list.  */
11495   next_token = cp_lexer_peek_token (parser->lexer);
11496   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11497   if (next_token->type == CPP_OPEN_SQUARE
11498       && next_token->flags & DIGRAPH
11499       && next_token_2->type == CPP_COLON
11500       && !(next_token_2->flags & PREV_WHITE))
11501     {
11502       cp_parser_parse_tentatively (parser);
11503       /* Change `:' into `::'.  */
11504       next_token_2->type = CPP_SCOPE;
11505       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11506          CPP_LESS.  */
11507       cp_lexer_consume_token (parser->lexer);
11508
11509       /* Parse the arguments.  */
11510       arguments = cp_parser_enclosed_template_argument_list (parser);
11511       if (!cp_parser_parse_definitely (parser))
11512         {
11513           /* If we couldn't parse an argument list, then we revert our changes
11514              and return simply an error. Maybe this is not a template-id
11515              after all.  */
11516           next_token_2->type = CPP_COLON;
11517           cp_parser_error (parser, "expected %<<%>");
11518           pop_deferring_access_checks ();
11519           return error_mark_node;
11520         }
11521       /* Otherwise, emit an error about the invalid digraph, but continue
11522          parsing because we got our argument list.  */
11523       if (permerror (next_token->location,
11524                      "%<<::%> cannot begin a template-argument list"))
11525         {
11526           static bool hint = false;
11527           inform (next_token->location,
11528                   "%<<:%> is an alternate spelling for %<[%>."
11529                   " Insert whitespace between %<<%> and %<::%>");
11530           if (!hint && !flag_permissive)
11531             {
11532               inform (next_token->location, "(if you use %<-fpermissive%>"
11533                       " G++ will accept your code)");
11534               hint = true;
11535             }
11536         }
11537     }
11538   else
11539     {
11540       /* Look for the `<' that starts the template-argument-list.  */
11541       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11542         {
11543           pop_deferring_access_checks ();
11544           return error_mark_node;
11545         }
11546       /* Parse the arguments.  */
11547       arguments = cp_parser_enclosed_template_argument_list (parser);
11548     }
11549
11550   /* Build a representation of the specialization.  */
11551   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11552     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11553   else if (DECL_CLASS_TEMPLATE_P (templ)
11554            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11555     {
11556       bool entering_scope;
11557       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11558          template (rather than some instantiation thereof) only if
11559          is not nested within some other construct.  For example, in
11560          "template <typename T> void f(T) { A<T>::", A<T> is just an
11561          instantiation of A.  */
11562       entering_scope = (template_parm_scope_p ()
11563                         && cp_lexer_next_token_is (parser->lexer,
11564                                                    CPP_SCOPE));
11565       template_id
11566         = finish_template_type (templ, arguments, entering_scope);
11567     }
11568   else
11569     {
11570       /* If it's not a class-template or a template-template, it should be
11571          a function-template.  */
11572       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11573                    || TREE_CODE (templ) == OVERLOAD
11574                    || BASELINK_P (templ)));
11575
11576       template_id = lookup_template_function (templ, arguments);
11577     }
11578
11579   /* If parsing tentatively, replace the sequence of tokens that makes
11580      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11581      should we re-parse the token stream, we will not have to repeat
11582      the effort required to do the parse, nor will we issue duplicate
11583      error messages about problems during instantiation of the
11584      template.  */
11585   if (start_of_id)
11586     {
11587       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11588
11589       /* Reset the contents of the START_OF_ID token.  */
11590       token->type = CPP_TEMPLATE_ID;
11591       /* Retrieve any deferred checks.  Do not pop this access checks yet
11592          so the memory will not be reclaimed during token replacing below.  */
11593       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11594       token->u.tree_check_value->value = template_id;
11595       token->u.tree_check_value->checks = get_deferred_access_checks ();
11596       token->keyword = RID_MAX;
11597
11598       /* Purge all subsequent tokens.  */
11599       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11600
11601       /* ??? Can we actually assume that, if template_id ==
11602          error_mark_node, we will have issued a diagnostic to the
11603          user, as opposed to simply marking the tentative parse as
11604          failed?  */
11605       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11606         error_at (token->location, "parse error in template argument list");
11607     }
11608
11609   pop_deferring_access_checks ();
11610   return template_id;
11611 }
11612
11613 /* Parse a template-name.
11614
11615    template-name:
11616      identifier
11617
11618    The standard should actually say:
11619
11620    template-name:
11621      identifier
11622      operator-function-id
11623
11624    A defect report has been filed about this issue.
11625
11626    A conversion-function-id cannot be a template name because they cannot
11627    be part of a template-id. In fact, looking at this code:
11628
11629    a.operator K<int>()
11630
11631    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11632    It is impossible to call a templated conversion-function-id with an
11633    explicit argument list, since the only allowed template parameter is
11634    the type to which it is converting.
11635
11636    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11637    `template' keyword, in a construction like:
11638
11639      T::template f<3>()
11640
11641    In that case `f' is taken to be a template-name, even though there
11642    is no way of knowing for sure.
11643
11644    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11645    name refers to a set of overloaded functions, at least one of which
11646    is a template, or an IDENTIFIER_NODE with the name of the template,
11647    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11648    names are looked up inside uninstantiated templates.  */
11649
11650 static tree
11651 cp_parser_template_name (cp_parser* parser,
11652                          bool template_keyword_p,
11653                          bool check_dependency_p,
11654                          bool is_declaration,
11655                          bool *is_identifier)
11656 {
11657   tree identifier;
11658   tree decl;
11659   tree fns;
11660   cp_token *token = cp_lexer_peek_token (parser->lexer);
11661
11662   /* If the next token is `operator', then we have either an
11663      operator-function-id or a conversion-function-id.  */
11664   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11665     {
11666       /* We don't know whether we're looking at an
11667          operator-function-id or a conversion-function-id.  */
11668       cp_parser_parse_tentatively (parser);
11669       /* Try an operator-function-id.  */
11670       identifier = cp_parser_operator_function_id (parser);
11671       /* If that didn't work, try a conversion-function-id.  */
11672       if (!cp_parser_parse_definitely (parser))
11673         {
11674           cp_parser_error (parser, "expected template-name");
11675           return error_mark_node;
11676         }
11677     }
11678   /* Look for the identifier.  */
11679   else
11680     identifier = cp_parser_identifier (parser);
11681
11682   /* If we didn't find an identifier, we don't have a template-id.  */
11683   if (identifier == error_mark_node)
11684     return error_mark_node;
11685
11686   /* If the name immediately followed the `template' keyword, then it
11687      is a template-name.  However, if the next token is not `<', then
11688      we do not treat it as a template-name, since it is not being used
11689      as part of a template-id.  This enables us to handle constructs
11690      like:
11691
11692        template <typename T> struct S { S(); };
11693        template <typename T> S<T>::S();
11694
11695      correctly.  We would treat `S' as a template -- if it were `S<T>'
11696      -- but we do not if there is no `<'.  */
11697
11698   if (processing_template_decl
11699       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11700     {
11701       /* In a declaration, in a dependent context, we pretend that the
11702          "template" keyword was present in order to improve error
11703          recovery.  For example, given:
11704
11705            template <typename T> void f(T::X<int>);
11706
11707          we want to treat "X<int>" as a template-id.  */
11708       if (is_declaration
11709           && !template_keyword_p
11710           && parser->scope && TYPE_P (parser->scope)
11711           && check_dependency_p
11712           && dependent_scope_p (parser->scope)
11713           /* Do not do this for dtors (or ctors), since they never
11714              need the template keyword before their name.  */
11715           && !constructor_name_p (identifier, parser->scope))
11716         {
11717           cp_token_position start = 0;
11718
11719           /* Explain what went wrong.  */
11720           error_at (token->location, "non-template %qD used as template",
11721                     identifier);
11722           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11723                   parser->scope, identifier);
11724           /* If parsing tentatively, find the location of the "<" token.  */
11725           if (cp_parser_simulate_error (parser))
11726             start = cp_lexer_token_position (parser->lexer, true);
11727           /* Parse the template arguments so that we can issue error
11728              messages about them.  */
11729           cp_lexer_consume_token (parser->lexer);
11730           cp_parser_enclosed_template_argument_list (parser);
11731           /* Skip tokens until we find a good place from which to
11732              continue parsing.  */
11733           cp_parser_skip_to_closing_parenthesis (parser,
11734                                                  /*recovering=*/true,
11735                                                  /*or_comma=*/true,
11736                                                  /*consume_paren=*/false);
11737           /* If parsing tentatively, permanently remove the
11738              template argument list.  That will prevent duplicate
11739              error messages from being issued about the missing
11740              "template" keyword.  */
11741           if (start)
11742             cp_lexer_purge_tokens_after (parser->lexer, start);
11743           if (is_identifier)
11744             *is_identifier = true;
11745           return identifier;
11746         }
11747
11748       /* If the "template" keyword is present, then there is generally
11749          no point in doing name-lookup, so we just return IDENTIFIER.
11750          But, if the qualifying scope is non-dependent then we can
11751          (and must) do name-lookup normally.  */
11752       if (template_keyword_p
11753           && (!parser->scope
11754               || (TYPE_P (parser->scope)
11755                   && dependent_type_p (parser->scope))))
11756         return identifier;
11757     }
11758
11759   /* Look up the name.  */
11760   decl = cp_parser_lookup_name (parser, identifier,
11761                                 none_type,
11762                                 /*is_template=*/true,
11763                                 /*is_namespace=*/false,
11764                                 check_dependency_p,
11765                                 /*ambiguous_decls=*/NULL,
11766                                 token->location);
11767
11768   /* If DECL is a template, then the name was a template-name.  */
11769   if (TREE_CODE (decl) == TEMPLATE_DECL)
11770     ;
11771   else
11772     {
11773       tree fn = NULL_TREE;
11774
11775       /* The standard does not explicitly indicate whether a name that
11776          names a set of overloaded declarations, some of which are
11777          templates, is a template-name.  However, such a name should
11778          be a template-name; otherwise, there is no way to form a
11779          template-id for the overloaded templates.  */
11780       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11781       if (TREE_CODE (fns) == OVERLOAD)
11782         for (fn = fns; fn; fn = OVL_NEXT (fn))
11783           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11784             break;
11785
11786       if (!fn)
11787         {
11788           /* The name does not name a template.  */
11789           cp_parser_error (parser, "expected template-name");
11790           return error_mark_node;
11791         }
11792     }
11793
11794   /* If DECL is dependent, and refers to a function, then just return
11795      its name; we will look it up again during template instantiation.  */
11796   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11797     {
11798       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11799       if (TYPE_P (scope) && dependent_type_p (scope))
11800         return identifier;
11801     }
11802
11803   return decl;
11804 }
11805
11806 /* Parse a template-argument-list.
11807
11808    template-argument-list:
11809      template-argument ... [opt]
11810      template-argument-list , template-argument ... [opt]
11811
11812    Returns a TREE_VEC containing the arguments.  */
11813
11814 static tree
11815 cp_parser_template_argument_list (cp_parser* parser)
11816 {
11817   tree fixed_args[10];
11818   unsigned n_args = 0;
11819   unsigned alloced = 10;
11820   tree *arg_ary = fixed_args;
11821   tree vec;
11822   bool saved_in_template_argument_list_p;
11823   bool saved_ice_p;
11824   bool saved_non_ice_p;
11825
11826   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11827   parser->in_template_argument_list_p = true;
11828   /* Even if the template-id appears in an integral
11829      constant-expression, the contents of the argument list do
11830      not.  */
11831   saved_ice_p = parser->integral_constant_expression_p;
11832   parser->integral_constant_expression_p = false;
11833   saved_non_ice_p = parser->non_integral_constant_expression_p;
11834   parser->non_integral_constant_expression_p = false;
11835   /* Parse the arguments.  */
11836   do
11837     {
11838       tree argument;
11839
11840       if (n_args)
11841         /* Consume the comma.  */
11842         cp_lexer_consume_token (parser->lexer);
11843
11844       /* Parse the template-argument.  */
11845       argument = cp_parser_template_argument (parser);
11846
11847       /* If the next token is an ellipsis, we're expanding a template
11848          argument pack. */
11849       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11850         {
11851           if (argument == error_mark_node)
11852             {
11853               cp_token *token = cp_lexer_peek_token (parser->lexer);
11854               error_at (token->location,
11855                         "expected parameter pack before %<...%>");
11856             }
11857           /* Consume the `...' token. */
11858           cp_lexer_consume_token (parser->lexer);
11859
11860           /* Make the argument into a TYPE_PACK_EXPANSION or
11861              EXPR_PACK_EXPANSION. */
11862           argument = make_pack_expansion (argument);
11863         }
11864
11865       if (n_args == alloced)
11866         {
11867           alloced *= 2;
11868
11869           if (arg_ary == fixed_args)
11870             {
11871               arg_ary = XNEWVEC (tree, alloced);
11872               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11873             }
11874           else
11875             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11876         }
11877       arg_ary[n_args++] = argument;
11878     }
11879   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11880
11881   vec = make_tree_vec (n_args);
11882
11883   while (n_args--)
11884     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11885
11886   if (arg_ary != fixed_args)
11887     free (arg_ary);
11888   parser->non_integral_constant_expression_p = saved_non_ice_p;
11889   parser->integral_constant_expression_p = saved_ice_p;
11890   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11891 #ifdef ENABLE_CHECKING
11892   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11893 #endif
11894   return vec;
11895 }
11896
11897 /* Parse a template-argument.
11898
11899    template-argument:
11900      assignment-expression
11901      type-id
11902      id-expression
11903
11904    The representation is that of an assignment-expression, type-id, or
11905    id-expression -- except that the qualified id-expression is
11906    evaluated, so that the value returned is either a DECL or an
11907    OVERLOAD.
11908
11909    Although the standard says "assignment-expression", it forbids
11910    throw-expressions or assignments in the template argument.
11911    Therefore, we use "conditional-expression" instead.  */
11912
11913 static tree
11914 cp_parser_template_argument (cp_parser* parser)
11915 {
11916   tree argument;
11917   bool template_p;
11918   bool address_p;
11919   bool maybe_type_id = false;
11920   cp_token *token = NULL, *argument_start_token = NULL;
11921   cp_id_kind idk;
11922
11923   /* There's really no way to know what we're looking at, so we just
11924      try each alternative in order.
11925
11926        [temp.arg]
11927
11928        In a template-argument, an ambiguity between a type-id and an
11929        expression is resolved to a type-id, regardless of the form of
11930        the corresponding template-parameter.
11931
11932      Therefore, we try a type-id first.  */
11933   cp_parser_parse_tentatively (parser);
11934   argument = cp_parser_template_type_arg (parser);
11935   /* If there was no error parsing the type-id but the next token is a
11936      '>>', our behavior depends on which dialect of C++ we're
11937      parsing. In C++98, we probably found a typo for '> >'. But there
11938      are type-id which are also valid expressions. For instance:
11939
11940      struct X { int operator >> (int); };
11941      template <int V> struct Foo {};
11942      Foo<X () >> 5> r;
11943
11944      Here 'X()' is a valid type-id of a function type, but the user just
11945      wanted to write the expression "X() >> 5". Thus, we remember that we
11946      found a valid type-id, but we still try to parse the argument as an
11947      expression to see what happens. 
11948
11949      In C++0x, the '>>' will be considered two separate '>'
11950      tokens.  */
11951   if (!cp_parser_error_occurred (parser)
11952       && cxx_dialect == cxx98
11953       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11954     {
11955       maybe_type_id = true;
11956       cp_parser_abort_tentative_parse (parser);
11957     }
11958   else
11959     {
11960       /* If the next token isn't a `,' or a `>', then this argument wasn't
11961       really finished. This means that the argument is not a valid
11962       type-id.  */
11963       if (!cp_parser_next_token_ends_template_argument_p (parser))
11964         cp_parser_error (parser, "expected template-argument");
11965       /* If that worked, we're done.  */
11966       if (cp_parser_parse_definitely (parser))
11967         return argument;
11968     }
11969   /* We're still not sure what the argument will be.  */
11970   cp_parser_parse_tentatively (parser);
11971   /* Try a template.  */
11972   argument_start_token = cp_lexer_peek_token (parser->lexer);
11973   argument = cp_parser_id_expression (parser,
11974                                       /*template_keyword_p=*/false,
11975                                       /*check_dependency_p=*/true,
11976                                       &template_p,
11977                                       /*declarator_p=*/false,
11978                                       /*optional_p=*/false);
11979   /* If the next token isn't a `,' or a `>', then this argument wasn't
11980      really finished.  */
11981   if (!cp_parser_next_token_ends_template_argument_p (parser))
11982     cp_parser_error (parser, "expected template-argument");
11983   if (!cp_parser_error_occurred (parser))
11984     {
11985       /* Figure out what is being referred to.  If the id-expression
11986          was for a class template specialization, then we will have a
11987          TYPE_DECL at this point.  There is no need to do name lookup
11988          at this point in that case.  */
11989       if (TREE_CODE (argument) != TYPE_DECL)
11990         argument = cp_parser_lookup_name (parser, argument,
11991                                           none_type,
11992                                           /*is_template=*/template_p,
11993                                           /*is_namespace=*/false,
11994                                           /*check_dependency=*/true,
11995                                           /*ambiguous_decls=*/NULL,
11996                                           argument_start_token->location);
11997       if (TREE_CODE (argument) != TEMPLATE_DECL
11998           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11999         cp_parser_error (parser, "expected template-name");
12000     }
12001   if (cp_parser_parse_definitely (parser))
12002     return argument;
12003   /* It must be a non-type argument.  There permitted cases are given
12004      in [temp.arg.nontype]:
12005
12006      -- an integral constant-expression of integral or enumeration
12007         type; or
12008
12009      -- the name of a non-type template-parameter; or
12010
12011      -- the name of an object or function with external linkage...
12012
12013      -- the address of an object or function with external linkage...
12014
12015      -- a pointer to member...  */
12016   /* Look for a non-type template parameter.  */
12017   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12018     {
12019       cp_parser_parse_tentatively (parser);
12020       argument = cp_parser_primary_expression (parser,
12021                                                /*address_p=*/false,
12022                                                /*cast_p=*/false,
12023                                                /*template_arg_p=*/true,
12024                                                &idk);
12025       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12026           || !cp_parser_next_token_ends_template_argument_p (parser))
12027         cp_parser_simulate_error (parser);
12028       if (cp_parser_parse_definitely (parser))
12029         return argument;
12030     }
12031
12032   /* If the next token is "&", the argument must be the address of an
12033      object or function with external linkage.  */
12034   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12035   if (address_p)
12036     cp_lexer_consume_token (parser->lexer);
12037   /* See if we might have an id-expression.  */
12038   token = cp_lexer_peek_token (parser->lexer);
12039   if (token->type == CPP_NAME
12040       || token->keyword == RID_OPERATOR
12041       || token->type == CPP_SCOPE
12042       || token->type == CPP_TEMPLATE_ID
12043       || token->type == CPP_NESTED_NAME_SPECIFIER)
12044     {
12045       cp_parser_parse_tentatively (parser);
12046       argument = cp_parser_primary_expression (parser,
12047                                                address_p,
12048                                                /*cast_p=*/false,
12049                                                /*template_arg_p=*/true,
12050                                                &idk);
12051       if (cp_parser_error_occurred (parser)
12052           || !cp_parser_next_token_ends_template_argument_p (parser))
12053         cp_parser_abort_tentative_parse (parser);
12054       else
12055         {
12056           tree probe;
12057
12058           if (TREE_CODE (argument) == INDIRECT_REF)
12059             {
12060               gcc_assert (REFERENCE_REF_P (argument));
12061               argument = TREE_OPERAND (argument, 0);
12062             }
12063
12064           /* If we're in a template, we represent a qualified-id referring
12065              to a static data member as a SCOPE_REF even if the scope isn't
12066              dependent so that we can check access control later.  */
12067           probe = argument;
12068           if (TREE_CODE (probe) == SCOPE_REF)
12069             probe = TREE_OPERAND (probe, 1);
12070           if (TREE_CODE (probe) == VAR_DECL)
12071             {
12072               /* A variable without external linkage might still be a
12073                  valid constant-expression, so no error is issued here
12074                  if the external-linkage check fails.  */
12075               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12076                 cp_parser_simulate_error (parser);
12077             }
12078           else if (is_overloaded_fn (argument))
12079             /* All overloaded functions are allowed; if the external
12080                linkage test does not pass, an error will be issued
12081                later.  */
12082             ;
12083           else if (address_p
12084                    && (TREE_CODE (argument) == OFFSET_REF
12085                        || TREE_CODE (argument) == SCOPE_REF))
12086             /* A pointer-to-member.  */
12087             ;
12088           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12089             ;
12090           else
12091             cp_parser_simulate_error (parser);
12092
12093           if (cp_parser_parse_definitely (parser))
12094             {
12095               if (address_p)
12096                 argument = build_x_unary_op (ADDR_EXPR, argument,
12097                                              tf_warning_or_error);
12098               return argument;
12099             }
12100         }
12101     }
12102   /* If the argument started with "&", there are no other valid
12103      alternatives at this point.  */
12104   if (address_p)
12105     {
12106       cp_parser_error (parser, "invalid non-type template argument");
12107       return error_mark_node;
12108     }
12109
12110   /* If the argument wasn't successfully parsed as a type-id followed
12111      by '>>', the argument can only be a constant expression now.
12112      Otherwise, we try parsing the constant-expression tentatively,
12113      because the argument could really be a type-id.  */
12114   if (maybe_type_id)
12115     cp_parser_parse_tentatively (parser);
12116   argument = cp_parser_constant_expression (parser,
12117                                             /*allow_non_constant_p=*/false,
12118                                             /*non_constant_p=*/NULL);
12119   argument = fold_non_dependent_expr (argument);
12120   if (!maybe_type_id)
12121     return argument;
12122   if (!cp_parser_next_token_ends_template_argument_p (parser))
12123     cp_parser_error (parser, "expected template-argument");
12124   if (cp_parser_parse_definitely (parser))
12125     return argument;
12126   /* We did our best to parse the argument as a non type-id, but that
12127      was the only alternative that matched (albeit with a '>' after
12128      it). We can assume it's just a typo from the user, and a
12129      diagnostic will then be issued.  */
12130   return cp_parser_template_type_arg (parser);
12131 }
12132
12133 /* Parse an explicit-instantiation.
12134
12135    explicit-instantiation:
12136      template declaration
12137
12138    Although the standard says `declaration', what it really means is:
12139
12140    explicit-instantiation:
12141      template decl-specifier-seq [opt] declarator [opt] ;
12142
12143    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12144    supposed to be allowed.  A defect report has been filed about this
12145    issue.
12146
12147    GNU Extension:
12148
12149    explicit-instantiation:
12150      storage-class-specifier template
12151        decl-specifier-seq [opt] declarator [opt] ;
12152      function-specifier template
12153        decl-specifier-seq [opt] declarator [opt] ;  */
12154
12155 static void
12156 cp_parser_explicit_instantiation (cp_parser* parser)
12157 {
12158   int declares_class_or_enum;
12159   cp_decl_specifier_seq decl_specifiers;
12160   tree extension_specifier = NULL_TREE;
12161
12162   /* Look for an (optional) storage-class-specifier or
12163      function-specifier.  */
12164   if (cp_parser_allow_gnu_extensions_p (parser))
12165     {
12166       extension_specifier
12167         = cp_parser_storage_class_specifier_opt (parser);
12168       if (!extension_specifier)
12169         extension_specifier
12170           = cp_parser_function_specifier_opt (parser,
12171                                               /*decl_specs=*/NULL);
12172     }
12173
12174   /* Look for the `template' keyword.  */
12175   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12176   /* Let the front end know that we are processing an explicit
12177      instantiation.  */
12178   begin_explicit_instantiation ();
12179   /* [temp.explicit] says that we are supposed to ignore access
12180      control while processing explicit instantiation directives.  */
12181   push_deferring_access_checks (dk_no_check);
12182   /* Parse a decl-specifier-seq.  */
12183   cp_parser_decl_specifier_seq (parser,
12184                                 CP_PARSER_FLAGS_OPTIONAL,
12185                                 &decl_specifiers,
12186                                 &declares_class_or_enum);
12187   /* If there was exactly one decl-specifier, and it declared a class,
12188      and there's no declarator, then we have an explicit type
12189      instantiation.  */
12190   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12191     {
12192       tree type;
12193
12194       type = check_tag_decl (&decl_specifiers);
12195       /* Turn access control back on for names used during
12196          template instantiation.  */
12197       pop_deferring_access_checks ();
12198       if (type)
12199         do_type_instantiation (type, extension_specifier,
12200                                /*complain=*/tf_error);
12201     }
12202   else
12203     {
12204       cp_declarator *declarator;
12205       tree decl;
12206
12207       /* Parse the declarator.  */
12208       declarator
12209         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12210                                 /*ctor_dtor_or_conv_p=*/NULL,
12211                                 /*parenthesized_p=*/NULL,
12212                                 /*member_p=*/false);
12213       if (declares_class_or_enum & 2)
12214         cp_parser_check_for_definition_in_return_type (declarator,
12215                                                        decl_specifiers.type,
12216                                                        decl_specifiers.type_location);
12217       if (declarator != cp_error_declarator)
12218         {
12219           decl = grokdeclarator (declarator, &decl_specifiers,
12220                                  NORMAL, 0, &decl_specifiers.attributes);
12221           /* Turn access control back on for names used during
12222              template instantiation.  */
12223           pop_deferring_access_checks ();
12224           /* Do the explicit instantiation.  */
12225           do_decl_instantiation (decl, extension_specifier);
12226         }
12227       else
12228         {
12229           pop_deferring_access_checks ();
12230           /* Skip the body of the explicit instantiation.  */
12231           cp_parser_skip_to_end_of_statement (parser);
12232         }
12233     }
12234   /* We're done with the instantiation.  */
12235   end_explicit_instantiation ();
12236
12237   cp_parser_consume_semicolon_at_end_of_statement (parser);
12238 }
12239
12240 /* Parse an explicit-specialization.
12241
12242    explicit-specialization:
12243      template < > declaration
12244
12245    Although the standard says `declaration', what it really means is:
12246
12247    explicit-specialization:
12248      template <> decl-specifier [opt] init-declarator [opt] ;
12249      template <> function-definition
12250      template <> explicit-specialization
12251      template <> template-declaration  */
12252
12253 static void
12254 cp_parser_explicit_specialization (cp_parser* parser)
12255 {
12256   bool need_lang_pop;
12257   cp_token *token = cp_lexer_peek_token (parser->lexer);
12258
12259   /* Look for the `template' keyword.  */
12260   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12261   /* Look for the `<'.  */
12262   cp_parser_require (parser, CPP_LESS, RT_LESS);
12263   /* Look for the `>'.  */
12264   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12265   /* We have processed another parameter list.  */
12266   ++parser->num_template_parameter_lists;
12267   /* [temp]
12268
12269      A template ... explicit specialization ... shall not have C
12270      linkage.  */
12271   if (current_lang_name == lang_name_c)
12272     {
12273       error_at (token->location, "template specialization with C linkage");
12274       /* Give it C++ linkage to avoid confusing other parts of the
12275          front end.  */
12276       push_lang_context (lang_name_cplusplus);
12277       need_lang_pop = true;
12278     }
12279   else
12280     need_lang_pop = false;
12281   /* Let the front end know that we are beginning a specialization.  */
12282   if (!begin_specialization ())
12283     {
12284       end_specialization ();
12285       return;
12286     }
12287
12288   /* If the next keyword is `template', we need to figure out whether
12289      or not we're looking a template-declaration.  */
12290   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12291     {
12292       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12293           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12294         cp_parser_template_declaration_after_export (parser,
12295                                                      /*member_p=*/false);
12296       else
12297         cp_parser_explicit_specialization (parser);
12298     }
12299   else
12300     /* Parse the dependent declaration.  */
12301     cp_parser_single_declaration (parser,
12302                                   /*checks=*/NULL,
12303                                   /*member_p=*/false,
12304                                   /*explicit_specialization_p=*/true,
12305                                   /*friend_p=*/NULL);
12306   /* We're done with the specialization.  */
12307   end_specialization ();
12308   /* For the erroneous case of a template with C linkage, we pushed an
12309      implicit C++ linkage scope; exit that scope now.  */
12310   if (need_lang_pop)
12311     pop_lang_context ();
12312   /* We're done with this parameter list.  */
12313   --parser->num_template_parameter_lists;
12314 }
12315
12316 /* Parse a type-specifier.
12317
12318    type-specifier:
12319      simple-type-specifier
12320      class-specifier
12321      enum-specifier
12322      elaborated-type-specifier
12323      cv-qualifier
12324
12325    GNU Extension:
12326
12327    type-specifier:
12328      __complex__
12329
12330    Returns a representation of the type-specifier.  For a
12331    class-specifier, enum-specifier, or elaborated-type-specifier, a
12332    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12333
12334    The parser flags FLAGS is used to control type-specifier parsing.
12335
12336    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12337    in a decl-specifier-seq.
12338
12339    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12340    class-specifier, enum-specifier, or elaborated-type-specifier, then
12341    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12342    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12343    zero.
12344
12345    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12346    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12347    is set to FALSE.  */
12348
12349 static tree
12350 cp_parser_type_specifier (cp_parser* parser,
12351                           cp_parser_flags flags,
12352                           cp_decl_specifier_seq *decl_specs,
12353                           bool is_declaration,
12354                           int* declares_class_or_enum,
12355                           bool* is_cv_qualifier)
12356 {
12357   tree type_spec = NULL_TREE;
12358   cp_token *token;
12359   enum rid keyword;
12360   cp_decl_spec ds = ds_last;
12361
12362   /* Assume this type-specifier does not declare a new type.  */
12363   if (declares_class_or_enum)
12364     *declares_class_or_enum = 0;
12365   /* And that it does not specify a cv-qualifier.  */
12366   if (is_cv_qualifier)
12367     *is_cv_qualifier = false;
12368   /* Peek at the next token.  */
12369   token = cp_lexer_peek_token (parser->lexer);
12370
12371   /* If we're looking at a keyword, we can use that to guide the
12372      production we choose.  */
12373   keyword = token->keyword;
12374   switch (keyword)
12375     {
12376     case RID_ENUM:
12377       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12378         goto elaborated_type_specifier;
12379
12380       /* Look for the enum-specifier.  */
12381       type_spec = cp_parser_enum_specifier (parser);
12382       /* If that worked, we're done.  */
12383       if (type_spec)
12384         {
12385           if (declares_class_or_enum)
12386             *declares_class_or_enum = 2;
12387           if (decl_specs)
12388             cp_parser_set_decl_spec_type (decl_specs,
12389                                           type_spec,
12390                                           token->location,
12391                                           /*user_defined_p=*/true);
12392           return type_spec;
12393         }
12394       else
12395         goto elaborated_type_specifier;
12396
12397       /* Any of these indicate either a class-specifier, or an
12398          elaborated-type-specifier.  */
12399     case RID_CLASS:
12400     case RID_STRUCT:
12401     case RID_UNION:
12402       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12403         goto elaborated_type_specifier;
12404
12405       /* Parse tentatively so that we can back up if we don't find a
12406          class-specifier.  */
12407       cp_parser_parse_tentatively (parser);
12408       /* Look for the class-specifier.  */
12409       type_spec = cp_parser_class_specifier (parser);
12410       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12411       /* If that worked, we're done.  */
12412       if (cp_parser_parse_definitely (parser))
12413         {
12414           if (declares_class_or_enum)
12415             *declares_class_or_enum = 2;
12416           if (decl_specs)
12417             cp_parser_set_decl_spec_type (decl_specs,
12418                                           type_spec,
12419                                           token->location,
12420                                           /*user_defined_p=*/true);
12421           return type_spec;
12422         }
12423
12424       /* Fall through.  */
12425     elaborated_type_specifier:
12426       /* We're declaring (not defining) a class or enum.  */
12427       if (declares_class_or_enum)
12428         *declares_class_or_enum = 1;
12429
12430       /* Fall through.  */
12431     case RID_TYPENAME:
12432       /* Look for an elaborated-type-specifier.  */
12433       type_spec
12434         = (cp_parser_elaborated_type_specifier
12435            (parser,
12436             decl_specs && decl_specs->specs[(int) ds_friend],
12437             is_declaration));
12438       if (decl_specs)
12439         cp_parser_set_decl_spec_type (decl_specs,
12440                                       type_spec,
12441                                       token->location,
12442                                       /*user_defined_p=*/true);
12443       return type_spec;
12444
12445     case RID_CONST:
12446       ds = ds_const;
12447       if (is_cv_qualifier)
12448         *is_cv_qualifier = true;
12449       break;
12450
12451     case RID_VOLATILE:
12452       ds = ds_volatile;
12453       if (is_cv_qualifier)
12454         *is_cv_qualifier = true;
12455       break;
12456
12457     case RID_RESTRICT:
12458       ds = ds_restrict;
12459       if (is_cv_qualifier)
12460         *is_cv_qualifier = true;
12461       break;
12462
12463     case RID_COMPLEX:
12464       /* The `__complex__' keyword is a GNU extension.  */
12465       ds = ds_complex;
12466       break;
12467
12468     default:
12469       break;
12470     }
12471
12472   /* Handle simple keywords.  */
12473   if (ds != ds_last)
12474     {
12475       if (decl_specs)
12476         {
12477           ++decl_specs->specs[(int)ds];
12478           decl_specs->any_specifiers_p = true;
12479         }
12480       return cp_lexer_consume_token (parser->lexer)->u.value;
12481     }
12482
12483   /* If we do not already have a type-specifier, assume we are looking
12484      at a simple-type-specifier.  */
12485   type_spec = cp_parser_simple_type_specifier (parser,
12486                                                decl_specs,
12487                                                flags);
12488
12489   /* If we didn't find a type-specifier, and a type-specifier was not
12490      optional in this context, issue an error message.  */
12491   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12492     {
12493       cp_parser_error (parser, "expected type specifier");
12494       return error_mark_node;
12495     }
12496
12497   return type_spec;
12498 }
12499
12500 /* Parse a simple-type-specifier.
12501
12502    simple-type-specifier:
12503      :: [opt] nested-name-specifier [opt] type-name
12504      :: [opt] nested-name-specifier template template-id
12505      char
12506      wchar_t
12507      bool
12508      short
12509      int
12510      long
12511      signed
12512      unsigned
12513      float
12514      double
12515      void
12516
12517    C++0x Extension:
12518
12519    simple-type-specifier:
12520      auto
12521      decltype ( expression )   
12522      char16_t
12523      char32_t
12524
12525    GNU Extension:
12526
12527    simple-type-specifier:
12528      __int128
12529      __typeof__ unary-expression
12530      __typeof__ ( type-id )
12531
12532    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12533    appropriately updated.  */
12534
12535 static tree
12536 cp_parser_simple_type_specifier (cp_parser* parser,
12537                                  cp_decl_specifier_seq *decl_specs,
12538                                  cp_parser_flags flags)
12539 {
12540   tree type = NULL_TREE;
12541   cp_token *token;
12542
12543   /* Peek at the next token.  */
12544   token = cp_lexer_peek_token (parser->lexer);
12545
12546   /* If we're looking at a keyword, things are easy.  */
12547   switch (token->keyword)
12548     {
12549     case RID_CHAR:
12550       if (decl_specs)
12551         decl_specs->explicit_char_p = true;
12552       type = char_type_node;
12553       break;
12554     case RID_CHAR16:
12555       type = char16_type_node;
12556       break;
12557     case RID_CHAR32:
12558       type = char32_type_node;
12559       break;
12560     case RID_WCHAR:
12561       type = wchar_type_node;
12562       break;
12563     case RID_BOOL:
12564       type = boolean_type_node;
12565       break;
12566     case RID_SHORT:
12567       if (decl_specs)
12568         ++decl_specs->specs[(int) ds_short];
12569       type = short_integer_type_node;
12570       break;
12571     case RID_INT:
12572       if (decl_specs)
12573         decl_specs->explicit_int_p = true;
12574       type = integer_type_node;
12575       break;
12576     case RID_INT128:
12577       if (!int128_integer_type_node)
12578         break;
12579       if (decl_specs)
12580         decl_specs->explicit_int128_p = true;
12581       type = int128_integer_type_node;
12582       break;
12583     case RID_LONG:
12584       if (decl_specs)
12585         ++decl_specs->specs[(int) ds_long];
12586       type = long_integer_type_node;
12587       break;
12588     case RID_SIGNED:
12589       if (decl_specs)
12590         ++decl_specs->specs[(int) ds_signed];
12591       type = integer_type_node;
12592       break;
12593     case RID_UNSIGNED:
12594       if (decl_specs)
12595         ++decl_specs->specs[(int) ds_unsigned];
12596       type = unsigned_type_node;
12597       break;
12598     case RID_FLOAT:
12599       type = float_type_node;
12600       break;
12601     case RID_DOUBLE:
12602       type = double_type_node;
12603       break;
12604     case RID_VOID:
12605       type = void_type_node;
12606       break;
12607       
12608     case RID_AUTO:
12609       maybe_warn_cpp0x (CPP0X_AUTO);
12610       type = make_auto ();
12611       break;
12612
12613     case RID_DECLTYPE:
12614       /* Parse the `decltype' type.  */
12615       type = cp_parser_decltype (parser);
12616
12617       if (decl_specs)
12618         cp_parser_set_decl_spec_type (decl_specs, type,
12619                                       token->location,
12620                                       /*user_defined_p=*/true);
12621
12622       return type;
12623
12624     case RID_TYPEOF:
12625       /* Consume the `typeof' token.  */
12626       cp_lexer_consume_token (parser->lexer);
12627       /* Parse the operand to `typeof'.  */
12628       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12629       /* If it is not already a TYPE, take its type.  */
12630       if (!TYPE_P (type))
12631         type = finish_typeof (type);
12632
12633       if (decl_specs)
12634         cp_parser_set_decl_spec_type (decl_specs, type,
12635                                       token->location,
12636                                       /*user_defined_p=*/true);
12637
12638       return type;
12639
12640     default:
12641       break;
12642     }
12643
12644   /* If the type-specifier was for a built-in type, we're done.  */
12645   if (type)
12646     {
12647       /* Record the type.  */
12648       if (decl_specs
12649           && (token->keyword != RID_SIGNED
12650               && token->keyword != RID_UNSIGNED
12651               && token->keyword != RID_SHORT
12652               && token->keyword != RID_LONG))
12653         cp_parser_set_decl_spec_type (decl_specs,
12654                                       type,
12655                                       token->location,
12656                                       /*user_defined=*/false);
12657       if (decl_specs)
12658         decl_specs->any_specifiers_p = true;
12659
12660       /* Consume the token.  */
12661       cp_lexer_consume_token (parser->lexer);
12662
12663       /* There is no valid C++ program where a non-template type is
12664          followed by a "<".  That usually indicates that the user thought
12665          that the type was a template.  */
12666       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12667
12668       return TYPE_NAME (type);
12669     }
12670
12671   /* The type-specifier must be a user-defined type.  */
12672   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12673     {
12674       bool qualified_p;
12675       bool global_p;
12676
12677       /* Don't gobble tokens or issue error messages if this is an
12678          optional type-specifier.  */
12679       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12680         cp_parser_parse_tentatively (parser);
12681
12682       /* Look for the optional `::' operator.  */
12683       global_p
12684         = (cp_parser_global_scope_opt (parser,
12685                                        /*current_scope_valid_p=*/false)
12686            != NULL_TREE);
12687       /* Look for the nested-name specifier.  */
12688       qualified_p
12689         = (cp_parser_nested_name_specifier_opt (parser,
12690                                                 /*typename_keyword_p=*/false,
12691                                                 /*check_dependency_p=*/true,
12692                                                 /*type_p=*/false,
12693                                                 /*is_declaration=*/false)
12694            != NULL_TREE);
12695       token = cp_lexer_peek_token (parser->lexer);
12696       /* If we have seen a nested-name-specifier, and the next token
12697          is `template', then we are using the template-id production.  */
12698       if (parser->scope
12699           && cp_parser_optional_template_keyword (parser))
12700         {
12701           /* Look for the template-id.  */
12702           type = cp_parser_template_id (parser,
12703                                         /*template_keyword_p=*/true,
12704                                         /*check_dependency_p=*/true,
12705                                         /*is_declaration=*/false);
12706           /* If the template-id did not name a type, we are out of
12707              luck.  */
12708           if (TREE_CODE (type) != TYPE_DECL)
12709             {
12710               cp_parser_error (parser, "expected template-id for type");
12711               type = NULL_TREE;
12712             }
12713         }
12714       /* Otherwise, look for a type-name.  */
12715       else
12716         type = cp_parser_type_name (parser);
12717       /* Keep track of all name-lookups performed in class scopes.  */
12718       if (type
12719           && !global_p
12720           && !qualified_p
12721           && TREE_CODE (type) == TYPE_DECL
12722           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12723         maybe_note_name_used_in_class (DECL_NAME (type), type);
12724       /* If it didn't work out, we don't have a TYPE.  */
12725       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12726           && !cp_parser_parse_definitely (parser))
12727         type = NULL_TREE;
12728       if (type && decl_specs)
12729         cp_parser_set_decl_spec_type (decl_specs, type,
12730                                       token->location,
12731                                       /*user_defined=*/true);
12732     }
12733
12734   /* If we didn't get a type-name, issue an error message.  */
12735   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12736     {
12737       cp_parser_error (parser, "expected type-name");
12738       return error_mark_node;
12739     }
12740
12741   /* There is no valid C++ program where a non-template type is
12742      followed by a "<".  That usually indicates that the user thought
12743      that the type was a template.  */
12744   if (type && type != error_mark_node)
12745     {
12746       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12747          If it is, then the '<'...'>' enclose protocol names rather than
12748          template arguments, and so everything is fine.  */
12749       if (c_dialect_objc ()
12750           && (objc_is_id (type) || objc_is_class_name (type)))
12751         {
12752           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12753           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12754
12755           /* Clobber the "unqualified" type previously entered into
12756              DECL_SPECS with the new, improved protocol-qualified version.  */
12757           if (decl_specs)
12758             decl_specs->type = qual_type;
12759
12760           return qual_type;
12761         }
12762
12763       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12764                                                token->location);
12765     }
12766
12767   return type;
12768 }
12769
12770 /* Parse a type-name.
12771
12772    type-name:
12773      class-name
12774      enum-name
12775      typedef-name
12776
12777    enum-name:
12778      identifier
12779
12780    typedef-name:
12781      identifier
12782
12783    Returns a TYPE_DECL for the type.  */
12784
12785 static tree
12786 cp_parser_type_name (cp_parser* parser)
12787 {
12788   tree type_decl;
12789
12790   /* We can't know yet whether it is a class-name or not.  */
12791   cp_parser_parse_tentatively (parser);
12792   /* Try a class-name.  */
12793   type_decl = cp_parser_class_name (parser,
12794                                     /*typename_keyword_p=*/false,
12795                                     /*template_keyword_p=*/false,
12796                                     none_type,
12797                                     /*check_dependency_p=*/true,
12798                                     /*class_head_p=*/false,
12799                                     /*is_declaration=*/false);
12800   /* If it's not a class-name, keep looking.  */
12801   if (!cp_parser_parse_definitely (parser))
12802     {
12803       /* It must be a typedef-name or an enum-name.  */
12804       return cp_parser_nonclass_name (parser);
12805     }
12806
12807   return type_decl;
12808 }
12809
12810 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12811
12812    enum-name:
12813      identifier
12814
12815    typedef-name:
12816      identifier
12817
12818    Returns a TYPE_DECL for the type.  */
12819
12820 static tree
12821 cp_parser_nonclass_name (cp_parser* parser)
12822 {
12823   tree type_decl;
12824   tree identifier;
12825
12826   cp_token *token = cp_lexer_peek_token (parser->lexer);
12827   identifier = cp_parser_identifier (parser);
12828   if (identifier == error_mark_node)
12829     return error_mark_node;
12830
12831   /* Look up the type-name.  */
12832   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12833
12834   if (TREE_CODE (type_decl) != TYPE_DECL
12835       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12836     {
12837       /* See if this is an Objective-C type.  */
12838       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12839       tree type = objc_get_protocol_qualified_type (identifier, protos);
12840       if (type)
12841         type_decl = TYPE_NAME (type);
12842     }
12843   
12844   /* Issue an error if we did not find a type-name.  */
12845   if (TREE_CODE (type_decl) != TYPE_DECL)
12846     {
12847       if (!cp_parser_simulate_error (parser))
12848         cp_parser_name_lookup_error (parser, identifier, type_decl,
12849                                      NLE_TYPE, token->location);
12850       return error_mark_node;
12851     }
12852   /* Remember that the name was used in the definition of the
12853      current class so that we can check later to see if the
12854      meaning would have been different after the class was
12855      entirely defined.  */
12856   else if (type_decl != error_mark_node
12857            && !parser->scope)
12858     maybe_note_name_used_in_class (identifier, type_decl);
12859   
12860   return type_decl;
12861 }
12862
12863 /* Parse an elaborated-type-specifier.  Note that the grammar given
12864    here incorporates the resolution to DR68.
12865
12866    elaborated-type-specifier:
12867      class-key :: [opt] nested-name-specifier [opt] identifier
12868      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12869      enum-key :: [opt] nested-name-specifier [opt] identifier
12870      typename :: [opt] nested-name-specifier identifier
12871      typename :: [opt] nested-name-specifier template [opt]
12872        template-id
12873
12874    GNU extension:
12875
12876    elaborated-type-specifier:
12877      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12878      class-key attributes :: [opt] nested-name-specifier [opt]
12879                template [opt] template-id
12880      enum attributes :: [opt] nested-name-specifier [opt] identifier
12881
12882    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12883    declared `friend'.  If IS_DECLARATION is TRUE, then this
12884    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12885    something is being declared.
12886
12887    Returns the TYPE specified.  */
12888
12889 static tree
12890 cp_parser_elaborated_type_specifier (cp_parser* parser,
12891                                      bool is_friend,
12892                                      bool is_declaration)
12893 {
12894   enum tag_types tag_type;
12895   tree identifier;
12896   tree type = NULL_TREE;
12897   tree attributes = NULL_TREE;
12898   tree globalscope;
12899   cp_token *token = NULL;
12900
12901   /* See if we're looking at the `enum' keyword.  */
12902   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12903     {
12904       /* Consume the `enum' token.  */
12905       cp_lexer_consume_token (parser->lexer);
12906       /* Remember that it's an enumeration type.  */
12907       tag_type = enum_type;
12908       /* Parse the optional `struct' or `class' key (for C++0x scoped
12909          enums).  */
12910       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12911           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12912         {
12913           if (cxx_dialect == cxx98)
12914             maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12915
12916           /* Consume the `struct' or `class'.  */
12917           cp_lexer_consume_token (parser->lexer);
12918         }
12919       /* Parse the attributes.  */
12920       attributes = cp_parser_attributes_opt (parser);
12921     }
12922   /* Or, it might be `typename'.  */
12923   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12924                                            RID_TYPENAME))
12925     {
12926       /* Consume the `typename' token.  */
12927       cp_lexer_consume_token (parser->lexer);
12928       /* Remember that it's a `typename' type.  */
12929       tag_type = typename_type;
12930     }
12931   /* Otherwise it must be a class-key.  */
12932   else
12933     {
12934       tag_type = cp_parser_class_key (parser);
12935       if (tag_type == none_type)
12936         return error_mark_node;
12937       /* Parse the attributes.  */
12938       attributes = cp_parser_attributes_opt (parser);
12939     }
12940
12941   /* Look for the `::' operator.  */
12942   globalscope =  cp_parser_global_scope_opt (parser,
12943                                              /*current_scope_valid_p=*/false);
12944   /* Look for the nested-name-specifier.  */
12945   if (tag_type == typename_type && !globalscope)
12946     {
12947       if (!cp_parser_nested_name_specifier (parser,
12948                                            /*typename_keyword_p=*/true,
12949                                            /*check_dependency_p=*/true,
12950                                            /*type_p=*/true,
12951                                             is_declaration))
12952         return error_mark_node;
12953     }
12954   else
12955     /* Even though `typename' is not present, the proposed resolution
12956        to Core Issue 180 says that in `class A<T>::B', `B' should be
12957        considered a type-name, even if `A<T>' is dependent.  */
12958     cp_parser_nested_name_specifier_opt (parser,
12959                                          /*typename_keyword_p=*/true,
12960                                          /*check_dependency_p=*/true,
12961                                          /*type_p=*/true,
12962                                          is_declaration);
12963  /* For everything but enumeration types, consider a template-id.
12964     For an enumeration type, consider only a plain identifier.  */
12965   if (tag_type != enum_type)
12966     {
12967       bool template_p = false;
12968       tree decl;
12969
12970       /* Allow the `template' keyword.  */
12971       template_p = cp_parser_optional_template_keyword (parser);
12972       /* If we didn't see `template', we don't know if there's a
12973          template-id or not.  */
12974       if (!template_p)
12975         cp_parser_parse_tentatively (parser);
12976       /* Parse the template-id.  */
12977       token = cp_lexer_peek_token (parser->lexer);
12978       decl = cp_parser_template_id (parser, template_p,
12979                                     /*check_dependency_p=*/true,
12980                                     is_declaration);
12981       /* If we didn't find a template-id, look for an ordinary
12982          identifier.  */
12983       if (!template_p && !cp_parser_parse_definitely (parser))
12984         ;
12985       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12986          in effect, then we must assume that, upon instantiation, the
12987          template will correspond to a class.  */
12988       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12989                && tag_type == typename_type)
12990         type = make_typename_type (parser->scope, decl,
12991                                    typename_type,
12992                                    /*complain=*/tf_error);
12993       /* If the `typename' keyword is in effect and DECL is not a type
12994          decl. Then type is non existant.   */
12995       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12996         type = NULL_TREE; 
12997       else 
12998         type = TREE_TYPE (decl);
12999     }
13000
13001   if (!type)
13002     {
13003       token = cp_lexer_peek_token (parser->lexer);
13004       identifier = cp_parser_identifier (parser);
13005
13006       if (identifier == error_mark_node)
13007         {
13008           parser->scope = NULL_TREE;
13009           return error_mark_node;
13010         }
13011
13012       /* For a `typename', we needn't call xref_tag.  */
13013       if (tag_type == typename_type
13014           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13015         return cp_parser_make_typename_type (parser, parser->scope,
13016                                              identifier,
13017                                              token->location);
13018       /* Look up a qualified name in the usual way.  */
13019       if (parser->scope)
13020         {
13021           tree decl;
13022           tree ambiguous_decls;
13023
13024           decl = cp_parser_lookup_name (parser, identifier,
13025                                         tag_type,
13026                                         /*is_template=*/false,
13027                                         /*is_namespace=*/false,
13028                                         /*check_dependency=*/true,
13029                                         &ambiguous_decls,
13030                                         token->location);
13031
13032           /* If the lookup was ambiguous, an error will already have been
13033              issued.  */
13034           if (ambiguous_decls)
13035             return error_mark_node;
13036
13037           /* If we are parsing friend declaration, DECL may be a
13038              TEMPLATE_DECL tree node here.  However, we need to check
13039              whether this TEMPLATE_DECL results in valid code.  Consider
13040              the following example:
13041
13042                namespace N {
13043                  template <class T> class C {};
13044                }
13045                class X {
13046                  template <class T> friend class N::C; // #1, valid code
13047                };
13048                template <class T> class Y {
13049                  friend class N::C;                    // #2, invalid code
13050                };
13051
13052              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13053              name lookup of `N::C'.  We see that friend declaration must
13054              be template for the code to be valid.  Note that
13055              processing_template_decl does not work here since it is
13056              always 1 for the above two cases.  */
13057
13058           decl = (cp_parser_maybe_treat_template_as_class
13059                   (decl, /*tag_name_p=*/is_friend
13060                          && parser->num_template_parameter_lists));
13061
13062           if (TREE_CODE (decl) != TYPE_DECL)
13063             {
13064               cp_parser_diagnose_invalid_type_name (parser,
13065                                                     parser->scope,
13066                                                     identifier,
13067                                                     token->location);
13068               return error_mark_node;
13069             }
13070
13071           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13072             {
13073               bool allow_template = (parser->num_template_parameter_lists
13074                                       || DECL_SELF_REFERENCE_P (decl));
13075               type = check_elaborated_type_specifier (tag_type, decl, 
13076                                                       allow_template);
13077
13078               if (type == error_mark_node)
13079                 return error_mark_node;
13080             }
13081
13082           /* Forward declarations of nested types, such as
13083
13084                class C1::C2;
13085                class C1::C2::C3;
13086
13087              are invalid unless all components preceding the final '::'
13088              are complete.  If all enclosing types are complete, these
13089              declarations become merely pointless.
13090
13091              Invalid forward declarations of nested types are errors
13092              caught elsewhere in parsing.  Those that are pointless arrive
13093              here.  */
13094
13095           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13096               && !is_friend && !processing_explicit_instantiation)
13097             warning (0, "declaration %qD does not declare anything", decl);
13098
13099           type = TREE_TYPE (decl);
13100         }
13101       else
13102         {
13103           /* An elaborated-type-specifier sometimes introduces a new type and
13104              sometimes names an existing type.  Normally, the rule is that it
13105              introduces a new type only if there is not an existing type of
13106              the same name already in scope.  For example, given:
13107
13108                struct S {};
13109                void f() { struct S s; }
13110
13111              the `struct S' in the body of `f' is the same `struct S' as in
13112              the global scope; the existing definition is used.  However, if
13113              there were no global declaration, this would introduce a new
13114              local class named `S'.
13115
13116              An exception to this rule applies to the following code:
13117
13118                namespace N { struct S; }
13119
13120              Here, the elaborated-type-specifier names a new type
13121              unconditionally; even if there is already an `S' in the
13122              containing scope this declaration names a new type.
13123              This exception only applies if the elaborated-type-specifier
13124              forms the complete declaration:
13125
13126                [class.name]
13127
13128                A declaration consisting solely of `class-key identifier ;' is
13129                either a redeclaration of the name in the current scope or a
13130                forward declaration of the identifier as a class name.  It
13131                introduces the name into the current scope.
13132
13133              We are in this situation precisely when the next token is a `;'.
13134
13135              An exception to the exception is that a `friend' declaration does
13136              *not* name a new type; i.e., given:
13137
13138                struct S { friend struct T; };
13139
13140              `T' is not a new type in the scope of `S'.
13141
13142              Also, `new struct S' or `sizeof (struct S)' never results in the
13143              definition of a new type; a new type can only be declared in a
13144              declaration context.  */
13145
13146           tag_scope ts;
13147           bool template_p;
13148
13149           if (is_friend)
13150             /* Friends have special name lookup rules.  */
13151             ts = ts_within_enclosing_non_class;
13152           else if (is_declaration
13153                    && cp_lexer_next_token_is (parser->lexer,
13154                                               CPP_SEMICOLON))
13155             /* This is a `class-key identifier ;' */
13156             ts = ts_current;
13157           else
13158             ts = ts_global;
13159
13160           template_p =
13161             (parser->num_template_parameter_lists
13162              && (cp_parser_next_token_starts_class_definition_p (parser)
13163                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13164           /* An unqualified name was used to reference this type, so
13165              there were no qualifying templates.  */
13166           if (!cp_parser_check_template_parameters (parser,
13167                                                     /*num_templates=*/0,
13168                                                     token->location,
13169                                                     /*declarator=*/NULL))
13170             return error_mark_node;
13171           type = xref_tag (tag_type, identifier, ts, template_p);
13172         }
13173     }
13174
13175   if (type == error_mark_node)
13176     return error_mark_node;
13177
13178   /* Allow attributes on forward declarations of classes.  */
13179   if (attributes)
13180     {
13181       if (TREE_CODE (type) == TYPENAME_TYPE)
13182         warning (OPT_Wattributes,
13183                  "attributes ignored on uninstantiated type");
13184       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13185                && ! processing_explicit_instantiation)
13186         warning (OPT_Wattributes,
13187                  "attributes ignored on template instantiation");
13188       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13189         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13190       else
13191         warning (OPT_Wattributes,
13192                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13193     }
13194
13195   if (tag_type != enum_type)
13196     cp_parser_check_class_key (tag_type, type);
13197
13198   /* A "<" cannot follow an elaborated type specifier.  If that
13199      happens, the user was probably trying to form a template-id.  */
13200   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13201
13202   return type;
13203 }
13204
13205 /* Parse an enum-specifier.
13206
13207    enum-specifier:
13208      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
13209
13210    enum-key:
13211      enum
13212      enum class   [C++0x]
13213      enum struct  [C++0x]
13214
13215    enum-base:   [C++0x]
13216      : type-specifier-seq
13217
13218    GNU Extensions:
13219      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13220        { enumerator-list [opt] }attributes[opt]
13221
13222    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13223    if the token stream isn't an enum-specifier after all.  */
13224
13225 static tree
13226 cp_parser_enum_specifier (cp_parser* parser)
13227 {
13228   tree identifier;
13229   tree type;
13230   tree attributes;
13231   bool scoped_enum_p = false;
13232   bool has_underlying_type = false;
13233   tree underlying_type = NULL_TREE;
13234
13235   /* Parse tentatively so that we can back up if we don't find a
13236      enum-specifier.  */
13237   cp_parser_parse_tentatively (parser);
13238
13239   /* Caller guarantees that the current token is 'enum', an identifier
13240      possibly follows, and the token after that is an opening brace.
13241      If we don't have an identifier, fabricate an anonymous name for
13242      the enumeration being defined.  */
13243   cp_lexer_consume_token (parser->lexer);
13244
13245   /* Parse the "class" or "struct", which indicates a scoped
13246      enumeration type in C++0x.  */
13247   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13248       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13249     {
13250       if (cxx_dialect == cxx98)
13251         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13252
13253       /* Consume the `struct' or `class' token.  */
13254       cp_lexer_consume_token (parser->lexer);
13255
13256       scoped_enum_p = true;
13257     }
13258
13259   attributes = cp_parser_attributes_opt (parser);
13260
13261   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13262     identifier = cp_parser_identifier (parser);
13263   else
13264     identifier = make_anon_name ();
13265
13266   /* Check for the `:' that denotes a specified underlying type in C++0x.
13267      Note that a ':' could also indicate a bitfield width, however.  */
13268   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13269     {
13270       cp_decl_specifier_seq type_specifiers;
13271
13272       /* Consume the `:'.  */
13273       cp_lexer_consume_token (parser->lexer);
13274
13275       /* Parse the type-specifier-seq.  */
13276       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13277                                     /*is_trailing_return=*/false,
13278                                     &type_specifiers);
13279
13280       /* At this point this is surely not elaborated type specifier.  */
13281       if (!cp_parser_parse_definitely (parser))
13282         return NULL_TREE;
13283
13284       if (cxx_dialect == cxx98)
13285         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13286
13287       has_underlying_type = true;
13288
13289       /* If that didn't work, stop.  */
13290       if (type_specifiers.type != error_mark_node)
13291         {
13292           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13293                                             /*initialized=*/0, NULL);
13294           if (underlying_type == error_mark_node)
13295             underlying_type = NULL_TREE;
13296         }
13297     }
13298
13299   /* Look for the `{' but don't consume it yet.  */
13300   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13301     {
13302       cp_parser_error (parser, "expected %<{%>");
13303       if (has_underlying_type)
13304         return NULL_TREE;
13305     }
13306
13307   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13308     return NULL_TREE;
13309
13310   /* Issue an error message if type-definitions are forbidden here.  */
13311   if (!cp_parser_check_type_definition (parser))
13312     type = error_mark_node;
13313   else
13314     /* Create the new type.  We do this before consuming the opening
13315        brace so the enum will be recorded as being on the line of its
13316        tag (or the 'enum' keyword, if there is no tag).  */
13317     type = start_enum (identifier, underlying_type, scoped_enum_p);
13318   
13319   /* Consume the opening brace.  */
13320   cp_lexer_consume_token (parser->lexer);
13321
13322   if (type == error_mark_node)
13323     {
13324       cp_parser_skip_to_end_of_block_or_statement (parser);
13325       return error_mark_node;
13326     }
13327
13328   /* If the next token is not '}', then there are some enumerators.  */
13329   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13330     cp_parser_enumerator_list (parser, type);
13331
13332   /* Consume the final '}'.  */
13333   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13334
13335   /* Look for trailing attributes to apply to this enumeration, and
13336      apply them if appropriate.  */
13337   if (cp_parser_allow_gnu_extensions_p (parser))
13338     {
13339       tree trailing_attr = cp_parser_attributes_opt (parser);
13340       trailing_attr = chainon (trailing_attr, attributes);
13341       cplus_decl_attributes (&type,
13342                              trailing_attr,
13343                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13344     }
13345
13346   /* Finish up the enumeration.  */
13347   finish_enum (type);
13348
13349   return type;
13350 }
13351
13352 /* Parse an enumerator-list.  The enumerators all have the indicated
13353    TYPE.
13354
13355    enumerator-list:
13356      enumerator-definition
13357      enumerator-list , enumerator-definition  */
13358
13359 static void
13360 cp_parser_enumerator_list (cp_parser* parser, tree type)
13361 {
13362   while (true)
13363     {
13364       /* Parse an enumerator-definition.  */
13365       cp_parser_enumerator_definition (parser, type);
13366
13367       /* If the next token is not a ',', we've reached the end of
13368          the list.  */
13369       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13370         break;
13371       /* Otherwise, consume the `,' and keep going.  */
13372       cp_lexer_consume_token (parser->lexer);
13373       /* If the next token is a `}', there is a trailing comma.  */
13374       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13375         {
13376           if (!in_system_header)
13377             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13378           break;
13379         }
13380     }
13381 }
13382
13383 /* Parse an enumerator-definition.  The enumerator has the indicated
13384    TYPE.
13385
13386    enumerator-definition:
13387      enumerator
13388      enumerator = constant-expression
13389
13390    enumerator:
13391      identifier  */
13392
13393 static void
13394 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13395 {
13396   tree identifier;
13397   tree value;
13398   location_t loc;
13399
13400   /* Save the input location because we are interested in the location
13401      of the identifier and not the location of the explicit value.  */
13402   loc = cp_lexer_peek_token (parser->lexer)->location;
13403
13404   /* Look for the identifier.  */
13405   identifier = cp_parser_identifier (parser);
13406   if (identifier == error_mark_node)
13407     return;
13408
13409   /* If the next token is an '=', then there is an explicit value.  */
13410   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13411     {
13412       /* Consume the `=' token.  */
13413       cp_lexer_consume_token (parser->lexer);
13414       /* Parse the value.  */
13415       value = cp_parser_constant_expression (parser,
13416                                              /*allow_non_constant_p=*/false,
13417                                              NULL);
13418     }
13419   else
13420     value = NULL_TREE;
13421
13422   /* If we are processing a template, make sure the initializer of the
13423      enumerator doesn't contain any bare template parameter pack.  */
13424   if (check_for_bare_parameter_packs (value))
13425     value = error_mark_node;
13426
13427   /* Create the enumerator.  */
13428   build_enumerator (identifier, value, type, loc);
13429 }
13430
13431 /* Parse a namespace-name.
13432
13433    namespace-name:
13434      original-namespace-name
13435      namespace-alias
13436
13437    Returns the NAMESPACE_DECL for the namespace.  */
13438
13439 static tree
13440 cp_parser_namespace_name (cp_parser* parser)
13441 {
13442   tree identifier;
13443   tree namespace_decl;
13444
13445   cp_token *token = cp_lexer_peek_token (parser->lexer);
13446
13447   /* Get the name of the namespace.  */
13448   identifier = cp_parser_identifier (parser);
13449   if (identifier == error_mark_node)
13450     return error_mark_node;
13451
13452   /* Look up the identifier in the currently active scope.  Look only
13453      for namespaces, due to:
13454
13455        [basic.lookup.udir]
13456
13457        When looking up a namespace-name in a using-directive or alias
13458        definition, only namespace names are considered.
13459
13460      And:
13461
13462        [basic.lookup.qual]
13463
13464        During the lookup of a name preceding the :: scope resolution
13465        operator, object, function, and enumerator names are ignored.
13466
13467      (Note that cp_parser_qualifying_entity only calls this
13468      function if the token after the name is the scope resolution
13469      operator.)  */
13470   namespace_decl = cp_parser_lookup_name (parser, identifier,
13471                                           none_type,
13472                                           /*is_template=*/false,
13473                                           /*is_namespace=*/true,
13474                                           /*check_dependency=*/true,
13475                                           /*ambiguous_decls=*/NULL,
13476                                           token->location);
13477   /* If it's not a namespace, issue an error.  */
13478   if (namespace_decl == error_mark_node
13479       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13480     {
13481       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13482         error_at (token->location, "%qD is not a namespace-name", identifier);
13483       cp_parser_error (parser, "expected namespace-name");
13484       namespace_decl = error_mark_node;
13485     }
13486
13487   return namespace_decl;
13488 }
13489
13490 /* Parse a namespace-definition.
13491
13492    namespace-definition:
13493      named-namespace-definition
13494      unnamed-namespace-definition
13495
13496    named-namespace-definition:
13497      original-namespace-definition
13498      extension-namespace-definition
13499
13500    original-namespace-definition:
13501      namespace identifier { namespace-body }
13502
13503    extension-namespace-definition:
13504      namespace original-namespace-name { namespace-body }
13505
13506    unnamed-namespace-definition:
13507      namespace { namespace-body } */
13508
13509 static void
13510 cp_parser_namespace_definition (cp_parser* parser)
13511 {
13512   tree identifier, attribs;
13513   bool has_visibility;
13514   bool is_inline;
13515
13516   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13517     {
13518       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13519       is_inline = true;
13520       cp_lexer_consume_token (parser->lexer);
13521     }
13522   else
13523     is_inline = false;
13524
13525   /* Look for the `namespace' keyword.  */
13526   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13527
13528   /* Get the name of the namespace.  We do not attempt to distinguish
13529      between an original-namespace-definition and an
13530      extension-namespace-definition at this point.  The semantic
13531      analysis routines are responsible for that.  */
13532   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13533     identifier = cp_parser_identifier (parser);
13534   else
13535     identifier = NULL_TREE;
13536
13537   /* Parse any specified attributes.  */
13538   attribs = cp_parser_attributes_opt (parser);
13539
13540   /* Look for the `{' to start the namespace.  */
13541   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13542   /* Start the namespace.  */
13543   push_namespace (identifier);
13544
13545   /* "inline namespace" is equivalent to a stub namespace definition
13546      followed by a strong using directive.  */
13547   if (is_inline)
13548     {
13549       tree name_space = current_namespace;
13550       /* Set up namespace association.  */
13551       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13552         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13553                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13554       /* Import the contents of the inline namespace.  */
13555       pop_namespace ();
13556       do_using_directive (name_space);
13557       push_namespace (identifier);
13558     }
13559
13560   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13561
13562   /* Parse the body of the namespace.  */
13563   cp_parser_namespace_body (parser);
13564
13565 #ifdef HANDLE_PRAGMA_VISIBILITY
13566   if (has_visibility)
13567     pop_visibility (1);
13568 #endif
13569
13570   /* Finish the namespace.  */
13571   pop_namespace ();
13572   /* Look for the final `}'.  */
13573   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13574 }
13575
13576 /* Parse a namespace-body.
13577
13578    namespace-body:
13579      declaration-seq [opt]  */
13580
13581 static void
13582 cp_parser_namespace_body (cp_parser* parser)
13583 {
13584   cp_parser_declaration_seq_opt (parser);
13585 }
13586
13587 /* Parse a namespace-alias-definition.
13588
13589    namespace-alias-definition:
13590      namespace identifier = qualified-namespace-specifier ;  */
13591
13592 static void
13593 cp_parser_namespace_alias_definition (cp_parser* parser)
13594 {
13595   tree identifier;
13596   tree namespace_specifier;
13597
13598   cp_token *token = cp_lexer_peek_token (parser->lexer);
13599
13600   /* Look for the `namespace' keyword.  */
13601   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13602   /* Look for the identifier.  */
13603   identifier = cp_parser_identifier (parser);
13604   if (identifier == error_mark_node)
13605     return;
13606   /* Look for the `=' token.  */
13607   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13608       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13609     {
13610       error_at (token->location, "%<namespace%> definition is not allowed here");
13611       /* Skip the definition.  */
13612       cp_lexer_consume_token (parser->lexer);
13613       if (cp_parser_skip_to_closing_brace (parser))
13614         cp_lexer_consume_token (parser->lexer);
13615       return;
13616     }
13617   cp_parser_require (parser, CPP_EQ, RT_EQ);
13618   /* Look for the qualified-namespace-specifier.  */
13619   namespace_specifier
13620     = cp_parser_qualified_namespace_specifier (parser);
13621   /* Look for the `;' token.  */
13622   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13623
13624   /* Register the alias in the symbol table.  */
13625   do_namespace_alias (identifier, namespace_specifier);
13626 }
13627
13628 /* Parse a qualified-namespace-specifier.
13629
13630    qualified-namespace-specifier:
13631      :: [opt] nested-name-specifier [opt] namespace-name
13632
13633    Returns a NAMESPACE_DECL corresponding to the specified
13634    namespace.  */
13635
13636 static tree
13637 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13638 {
13639   /* Look for the optional `::'.  */
13640   cp_parser_global_scope_opt (parser,
13641                               /*current_scope_valid_p=*/false);
13642
13643   /* Look for the optional nested-name-specifier.  */
13644   cp_parser_nested_name_specifier_opt (parser,
13645                                        /*typename_keyword_p=*/false,
13646                                        /*check_dependency_p=*/true,
13647                                        /*type_p=*/false,
13648                                        /*is_declaration=*/true);
13649
13650   return cp_parser_namespace_name (parser);
13651 }
13652
13653 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13654    access declaration.
13655
13656    using-declaration:
13657      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13658      using :: unqualified-id ;  
13659
13660    access-declaration:
13661      qualified-id ;  
13662
13663    */
13664
13665 static bool
13666 cp_parser_using_declaration (cp_parser* parser, 
13667                              bool access_declaration_p)
13668 {
13669   cp_token *token;
13670   bool typename_p = false;
13671   bool global_scope_p;
13672   tree decl;
13673   tree identifier;
13674   tree qscope;
13675
13676   if (access_declaration_p)
13677     cp_parser_parse_tentatively (parser);
13678   else
13679     {
13680       /* Look for the `using' keyword.  */
13681       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13682       
13683       /* Peek at the next token.  */
13684       token = cp_lexer_peek_token (parser->lexer);
13685       /* See if it's `typename'.  */
13686       if (token->keyword == RID_TYPENAME)
13687         {
13688           /* Remember that we've seen it.  */
13689           typename_p = true;
13690           /* Consume the `typename' token.  */
13691           cp_lexer_consume_token (parser->lexer);
13692         }
13693     }
13694
13695   /* Look for the optional global scope qualification.  */
13696   global_scope_p
13697     = (cp_parser_global_scope_opt (parser,
13698                                    /*current_scope_valid_p=*/false)
13699        != NULL_TREE);
13700
13701   /* If we saw `typename', or didn't see `::', then there must be a
13702      nested-name-specifier present.  */
13703   if (typename_p || !global_scope_p)
13704     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13705                                               /*check_dependency_p=*/true,
13706                                               /*type_p=*/false,
13707                                               /*is_declaration=*/true);
13708   /* Otherwise, we could be in either of the two productions.  In that
13709      case, treat the nested-name-specifier as optional.  */
13710   else
13711     qscope = cp_parser_nested_name_specifier_opt (parser,
13712                                                   /*typename_keyword_p=*/false,
13713                                                   /*check_dependency_p=*/true,
13714                                                   /*type_p=*/false,
13715                                                   /*is_declaration=*/true);
13716   if (!qscope)
13717     qscope = global_namespace;
13718
13719   if (access_declaration_p && cp_parser_error_occurred (parser))
13720     /* Something has already gone wrong; there's no need to parse
13721        further.  Since an error has occurred, the return value of
13722        cp_parser_parse_definitely will be false, as required.  */
13723     return cp_parser_parse_definitely (parser);
13724
13725   token = cp_lexer_peek_token (parser->lexer);
13726   /* Parse the unqualified-id.  */
13727   identifier = cp_parser_unqualified_id (parser,
13728                                          /*template_keyword_p=*/false,
13729                                          /*check_dependency_p=*/true,
13730                                          /*declarator_p=*/true,
13731                                          /*optional_p=*/false);
13732
13733   if (access_declaration_p)
13734     {
13735       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13736         cp_parser_simulate_error (parser);
13737       if (!cp_parser_parse_definitely (parser))
13738         return false;
13739     }
13740
13741   /* The function we call to handle a using-declaration is different
13742      depending on what scope we are in.  */
13743   if (qscope == error_mark_node || identifier == error_mark_node)
13744     ;
13745   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13746            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13747     /* [namespace.udecl]
13748
13749        A using declaration shall not name a template-id.  */
13750     error_at (token->location,
13751               "a template-id may not appear in a using-declaration");
13752   else
13753     {
13754       if (at_class_scope_p ())
13755         {
13756           /* Create the USING_DECL.  */
13757           decl = do_class_using_decl (parser->scope, identifier);
13758
13759           if (check_for_bare_parameter_packs (decl))
13760             return false;
13761           else
13762             /* Add it to the list of members in this class.  */
13763             finish_member_declaration (decl);
13764         }
13765       else
13766         {
13767           decl = cp_parser_lookup_name_simple (parser,
13768                                                identifier,
13769                                                token->location);
13770           if (decl == error_mark_node)
13771             cp_parser_name_lookup_error (parser, identifier,
13772                                          decl, NLE_NULL,
13773                                          token->location);
13774           else if (check_for_bare_parameter_packs (decl))
13775             return false;
13776           else if (!at_namespace_scope_p ())
13777             do_local_using_decl (decl, qscope, identifier);
13778           else
13779             do_toplevel_using_decl (decl, qscope, identifier);
13780         }
13781     }
13782
13783   /* Look for the final `;'.  */
13784   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13785   
13786   return true;
13787 }
13788
13789 /* Parse a using-directive.
13790
13791    using-directive:
13792      using namespace :: [opt] nested-name-specifier [opt]
13793        namespace-name ;  */
13794
13795 static void
13796 cp_parser_using_directive (cp_parser* parser)
13797 {
13798   tree namespace_decl;
13799   tree attribs;
13800
13801   /* Look for the `using' keyword.  */
13802   cp_parser_require_keyword (parser, RID_USING, RT_USING);
13803   /* And the `namespace' keyword.  */
13804   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13805   /* Look for the optional `::' operator.  */
13806   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13807   /* And the optional nested-name-specifier.  */
13808   cp_parser_nested_name_specifier_opt (parser,
13809                                        /*typename_keyword_p=*/false,
13810                                        /*check_dependency_p=*/true,
13811                                        /*type_p=*/false,
13812                                        /*is_declaration=*/true);
13813   /* Get the namespace being used.  */
13814   namespace_decl = cp_parser_namespace_name (parser);
13815   /* And any specified attributes.  */
13816   attribs = cp_parser_attributes_opt (parser);
13817   /* Update the symbol table.  */
13818   parse_using_directive (namespace_decl, attribs);
13819   /* Look for the final `;'.  */
13820   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13821 }
13822
13823 /* Parse an asm-definition.
13824
13825    asm-definition:
13826      asm ( string-literal ) ;
13827
13828    GNU Extension:
13829
13830    asm-definition:
13831      asm volatile [opt] ( string-literal ) ;
13832      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13833      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13834                           : asm-operand-list [opt] ) ;
13835      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13836                           : asm-operand-list [opt]
13837                           : asm-clobber-list [opt] ) ;
13838      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13839                                : asm-clobber-list [opt]
13840                                : asm-goto-list ) ;  */
13841
13842 static void
13843 cp_parser_asm_definition (cp_parser* parser)
13844 {
13845   tree string;
13846   tree outputs = NULL_TREE;
13847   tree inputs = NULL_TREE;
13848   tree clobbers = NULL_TREE;
13849   tree labels = NULL_TREE;
13850   tree asm_stmt;
13851   bool volatile_p = false;
13852   bool extended_p = false;
13853   bool invalid_inputs_p = false;
13854   bool invalid_outputs_p = false;
13855   bool goto_p = false;
13856   required_token missing = RT_NONE;
13857
13858   /* Look for the `asm' keyword.  */
13859   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
13860   /* See if the next token is `volatile'.  */
13861   if (cp_parser_allow_gnu_extensions_p (parser)
13862       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13863     {
13864       /* Remember that we saw the `volatile' keyword.  */
13865       volatile_p = true;
13866       /* Consume the token.  */
13867       cp_lexer_consume_token (parser->lexer);
13868     }
13869   if (cp_parser_allow_gnu_extensions_p (parser)
13870       && parser->in_function_body
13871       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13872     {
13873       /* Remember that we saw the `goto' keyword.  */
13874       goto_p = true;
13875       /* Consume the token.  */
13876       cp_lexer_consume_token (parser->lexer);
13877     }
13878   /* Look for the opening `('.  */
13879   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13880     return;
13881   /* Look for the string.  */
13882   string = cp_parser_string_literal (parser, false, false);
13883   if (string == error_mark_node)
13884     {
13885       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13886                                              /*consume_paren=*/true);
13887       return;
13888     }
13889
13890   /* If we're allowing GNU extensions, check for the extended assembly
13891      syntax.  Unfortunately, the `:' tokens need not be separated by
13892      a space in C, and so, for compatibility, we tolerate that here
13893      too.  Doing that means that we have to treat the `::' operator as
13894      two `:' tokens.  */
13895   if (cp_parser_allow_gnu_extensions_p (parser)
13896       && parser->in_function_body
13897       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13898           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13899     {
13900       bool inputs_p = false;
13901       bool clobbers_p = false;
13902       bool labels_p = false;
13903
13904       /* The extended syntax was used.  */
13905       extended_p = true;
13906
13907       /* Look for outputs.  */
13908       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13909         {
13910           /* Consume the `:'.  */
13911           cp_lexer_consume_token (parser->lexer);
13912           /* Parse the output-operands.  */
13913           if (cp_lexer_next_token_is_not (parser->lexer,
13914                                           CPP_COLON)
13915               && cp_lexer_next_token_is_not (parser->lexer,
13916                                              CPP_SCOPE)
13917               && cp_lexer_next_token_is_not (parser->lexer,
13918                                              CPP_CLOSE_PAREN)
13919               && !goto_p)
13920             outputs = cp_parser_asm_operand_list (parser);
13921
13922             if (outputs == error_mark_node)
13923               invalid_outputs_p = true;
13924         }
13925       /* If the next token is `::', there are no outputs, and the
13926          next token is the beginning of the inputs.  */
13927       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13928         /* The inputs are coming next.  */
13929         inputs_p = true;
13930
13931       /* Look for inputs.  */
13932       if (inputs_p
13933           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13934         {
13935           /* Consume the `:' or `::'.  */
13936           cp_lexer_consume_token (parser->lexer);
13937           /* Parse the output-operands.  */
13938           if (cp_lexer_next_token_is_not (parser->lexer,
13939                                           CPP_COLON)
13940               && cp_lexer_next_token_is_not (parser->lexer,
13941                                              CPP_SCOPE)
13942               && cp_lexer_next_token_is_not (parser->lexer,
13943                                              CPP_CLOSE_PAREN))
13944             inputs = cp_parser_asm_operand_list (parser);
13945
13946             if (inputs == error_mark_node)
13947               invalid_inputs_p = true;
13948         }
13949       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13950         /* The clobbers are coming next.  */
13951         clobbers_p = true;
13952
13953       /* Look for clobbers.  */
13954       if (clobbers_p
13955           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13956         {
13957           clobbers_p = true;
13958           /* Consume the `:' or `::'.  */
13959           cp_lexer_consume_token (parser->lexer);
13960           /* Parse the clobbers.  */
13961           if (cp_lexer_next_token_is_not (parser->lexer,
13962                                           CPP_COLON)
13963               && cp_lexer_next_token_is_not (parser->lexer,
13964                                              CPP_CLOSE_PAREN))
13965             clobbers = cp_parser_asm_clobber_list (parser);
13966         }
13967       else if (goto_p
13968                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13969         /* The labels are coming next.  */
13970         labels_p = true;
13971
13972       /* Look for labels.  */
13973       if (labels_p
13974           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13975         {
13976           labels_p = true;
13977           /* Consume the `:' or `::'.  */
13978           cp_lexer_consume_token (parser->lexer);
13979           /* Parse the labels.  */
13980           labels = cp_parser_asm_label_list (parser);
13981         }
13982
13983       if (goto_p && !labels_p)
13984         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
13985     }
13986   else if (goto_p)
13987     missing = RT_COLON_SCOPE;
13988
13989   /* Look for the closing `)'.  */
13990   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13991                           missing ? missing : RT_CLOSE_PAREN))
13992     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13993                                            /*consume_paren=*/true);
13994   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13995
13996   if (!invalid_inputs_p && !invalid_outputs_p)
13997     {
13998       /* Create the ASM_EXPR.  */
13999       if (parser->in_function_body)
14000         {
14001           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14002                                       inputs, clobbers, labels);
14003           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14004           if (!extended_p)
14005             {
14006               tree temp = asm_stmt;
14007               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14008                 temp = TREE_OPERAND (temp, 0);
14009
14010               ASM_INPUT_P (temp) = 1;
14011             }
14012         }
14013       else
14014         cgraph_add_asm_node (string);
14015     }
14016 }
14017
14018 /* Declarators [gram.dcl.decl] */
14019
14020 /* Parse an init-declarator.
14021
14022    init-declarator:
14023      declarator initializer [opt]
14024
14025    GNU Extension:
14026
14027    init-declarator:
14028      declarator asm-specification [opt] attributes [opt] initializer [opt]
14029
14030    function-definition:
14031      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14032        function-body
14033      decl-specifier-seq [opt] declarator function-try-block
14034
14035    GNU Extension:
14036
14037    function-definition:
14038      __extension__ function-definition
14039
14040    The DECL_SPECIFIERS apply to this declarator.  Returns a
14041    representation of the entity declared.  If MEMBER_P is TRUE, then
14042    this declarator appears in a class scope.  The new DECL created by
14043    this declarator is returned.
14044
14045    The CHECKS are access checks that should be performed once we know
14046    what entity is being declared (and, therefore, what classes have
14047    befriended it).
14048
14049    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14050    for a function-definition here as well.  If the declarator is a
14051    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14052    be TRUE upon return.  By that point, the function-definition will
14053    have been completely parsed.
14054
14055    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14056    is FALSE.  */
14057
14058 static tree
14059 cp_parser_init_declarator (cp_parser* parser,
14060                            cp_decl_specifier_seq *decl_specifiers,
14061                            VEC (deferred_access_check,gc)* checks,
14062                            bool function_definition_allowed_p,
14063                            bool member_p,
14064                            int declares_class_or_enum,
14065                            bool* function_definition_p)
14066 {
14067   cp_token *token = NULL, *asm_spec_start_token = NULL,
14068            *attributes_start_token = NULL;
14069   cp_declarator *declarator;
14070   tree prefix_attributes;
14071   tree attributes;
14072   tree asm_specification;
14073   tree initializer;
14074   tree decl = NULL_TREE;
14075   tree scope;
14076   int is_initialized;
14077   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14078      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14079      "(...)".  */
14080   enum cpp_ttype initialization_kind;
14081   bool is_direct_init = false;
14082   bool is_non_constant_init;
14083   int ctor_dtor_or_conv_p;
14084   bool friend_p;
14085   tree pushed_scope = NULL;
14086
14087   /* Gather the attributes that were provided with the
14088      decl-specifiers.  */
14089   prefix_attributes = decl_specifiers->attributes;
14090
14091   /* Assume that this is not the declarator for a function
14092      definition.  */
14093   if (function_definition_p)
14094     *function_definition_p = false;
14095
14096   /* Defer access checks while parsing the declarator; we cannot know
14097      what names are accessible until we know what is being
14098      declared.  */
14099   resume_deferring_access_checks ();
14100
14101   /* Parse the declarator.  */
14102   token = cp_lexer_peek_token (parser->lexer);
14103   declarator
14104     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14105                             &ctor_dtor_or_conv_p,
14106                             /*parenthesized_p=*/NULL,
14107                             /*member_p=*/false);
14108   /* Gather up the deferred checks.  */
14109   stop_deferring_access_checks ();
14110
14111   /* If the DECLARATOR was erroneous, there's no need to go
14112      further.  */
14113   if (declarator == cp_error_declarator)
14114     return error_mark_node;
14115
14116   /* Check that the number of template-parameter-lists is OK.  */
14117   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14118                                                        token->location))
14119     return error_mark_node;
14120
14121   if (declares_class_or_enum & 2)
14122     cp_parser_check_for_definition_in_return_type (declarator,
14123                                                    decl_specifiers->type,
14124                                                    decl_specifiers->type_location);
14125
14126   /* Figure out what scope the entity declared by the DECLARATOR is
14127      located in.  `grokdeclarator' sometimes changes the scope, so
14128      we compute it now.  */
14129   scope = get_scope_of_declarator (declarator);
14130
14131   /* Perform any lookups in the declared type which were thought to be
14132      dependent, but are not in the scope of the declarator.  */
14133   decl_specifiers->type
14134     = maybe_update_decl_type (decl_specifiers->type, scope);
14135
14136   /* If we're allowing GNU extensions, look for an asm-specification
14137      and attributes.  */
14138   if (cp_parser_allow_gnu_extensions_p (parser))
14139     {
14140       /* Look for an asm-specification.  */
14141       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14142       asm_specification = cp_parser_asm_specification_opt (parser);
14143       /* And attributes.  */
14144       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14145       attributes = cp_parser_attributes_opt (parser);
14146     }
14147   else
14148     {
14149       asm_specification = NULL_TREE;
14150       attributes = NULL_TREE;
14151     }
14152
14153   /* Peek at the next token.  */
14154   token = cp_lexer_peek_token (parser->lexer);
14155   /* Check to see if the token indicates the start of a
14156      function-definition.  */
14157   if (function_declarator_p (declarator)
14158       && cp_parser_token_starts_function_definition_p (token))
14159     {
14160       if (!function_definition_allowed_p)
14161         {
14162           /* If a function-definition should not appear here, issue an
14163              error message.  */
14164           cp_parser_error (parser,
14165                            "a function-definition is not allowed here");
14166           return error_mark_node;
14167         }
14168       else
14169         {
14170           location_t func_brace_location
14171             = cp_lexer_peek_token (parser->lexer)->location;
14172
14173           /* Neither attributes nor an asm-specification are allowed
14174              on a function-definition.  */
14175           if (asm_specification)
14176             error_at (asm_spec_start_token->location,
14177                       "an asm-specification is not allowed "
14178                       "on a function-definition");
14179           if (attributes)
14180             error_at (attributes_start_token->location,
14181                       "attributes are not allowed on a function-definition");
14182           /* This is a function-definition.  */
14183           *function_definition_p = true;
14184
14185           /* Parse the function definition.  */
14186           if (member_p)
14187             decl = cp_parser_save_member_function_body (parser,
14188                                                         decl_specifiers,
14189                                                         declarator,
14190                                                         prefix_attributes);
14191           else
14192             decl
14193               = (cp_parser_function_definition_from_specifiers_and_declarator
14194                  (parser, decl_specifiers, prefix_attributes, declarator));
14195
14196           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14197             {
14198               /* This is where the prologue starts...  */
14199               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14200                 = func_brace_location;
14201             }
14202
14203           return decl;
14204         }
14205     }
14206
14207   /* [dcl.dcl]
14208
14209      Only in function declarations for constructors, destructors, and
14210      type conversions can the decl-specifier-seq be omitted.
14211
14212      We explicitly postpone this check past the point where we handle
14213      function-definitions because we tolerate function-definitions
14214      that are missing their return types in some modes.  */
14215   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14216     {
14217       cp_parser_error (parser,
14218                        "expected constructor, destructor, or type conversion");
14219       return error_mark_node;
14220     }
14221
14222   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14223   if (token->type == CPP_EQ
14224       || token->type == CPP_OPEN_PAREN
14225       || token->type == CPP_OPEN_BRACE)
14226     {
14227       is_initialized = SD_INITIALIZED;
14228       initialization_kind = token->type;
14229
14230       if (token->type == CPP_EQ
14231           && function_declarator_p (declarator))
14232         {
14233           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14234           if (t2->keyword == RID_DEFAULT)
14235             is_initialized = SD_DEFAULTED;
14236           else if (t2->keyword == RID_DELETE)
14237             is_initialized = SD_DELETED;
14238         }
14239     }
14240   else
14241     {
14242       /* If the init-declarator isn't initialized and isn't followed by a
14243          `,' or `;', it's not a valid init-declarator.  */
14244       if (token->type != CPP_COMMA
14245           && token->type != CPP_SEMICOLON)
14246         {
14247           cp_parser_error (parser, "expected initializer");
14248           return error_mark_node;
14249         }
14250       is_initialized = SD_UNINITIALIZED;
14251       initialization_kind = CPP_EOF;
14252     }
14253
14254   /* Because start_decl has side-effects, we should only call it if we
14255      know we're going ahead.  By this point, we know that we cannot
14256      possibly be looking at any other construct.  */
14257   cp_parser_commit_to_tentative_parse (parser);
14258
14259   /* If the decl specifiers were bad, issue an error now that we're
14260      sure this was intended to be a declarator.  Then continue
14261      declaring the variable(s), as int, to try to cut down on further
14262      errors.  */
14263   if (decl_specifiers->any_specifiers_p
14264       && decl_specifiers->type == error_mark_node)
14265     {
14266       cp_parser_error (parser, "invalid type in declaration");
14267       decl_specifiers->type = integer_type_node;
14268     }
14269
14270   /* Check to see whether or not this declaration is a friend.  */
14271   friend_p = cp_parser_friend_p (decl_specifiers);
14272
14273   /* Enter the newly declared entry in the symbol table.  If we're
14274      processing a declaration in a class-specifier, we wait until
14275      after processing the initializer.  */
14276   if (!member_p)
14277     {
14278       if (parser->in_unbraced_linkage_specification_p)
14279         decl_specifiers->storage_class = sc_extern;
14280       decl = start_decl (declarator, decl_specifiers,
14281                          is_initialized, attributes, prefix_attributes,
14282                          &pushed_scope);
14283       /* Adjust location of decl if declarator->id_loc is more appropriate:
14284          set, and decl wasn't merged with another decl, in which case its
14285          location would be different from input_location, and more accurate.  */
14286       if (DECL_P (decl)
14287           && declarator->id_loc != UNKNOWN_LOCATION
14288           && DECL_SOURCE_LOCATION (decl) == input_location)
14289         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14290     }
14291   else if (scope)
14292     /* Enter the SCOPE.  That way unqualified names appearing in the
14293        initializer will be looked up in SCOPE.  */
14294     pushed_scope = push_scope (scope);
14295
14296   /* Perform deferred access control checks, now that we know in which
14297      SCOPE the declared entity resides.  */
14298   if (!member_p && decl)
14299     {
14300       tree saved_current_function_decl = NULL_TREE;
14301
14302       /* If the entity being declared is a function, pretend that we
14303          are in its scope.  If it is a `friend', it may have access to
14304          things that would not otherwise be accessible.  */
14305       if (TREE_CODE (decl) == FUNCTION_DECL)
14306         {
14307           saved_current_function_decl = current_function_decl;
14308           current_function_decl = decl;
14309         }
14310
14311       /* Perform access checks for template parameters.  */
14312       cp_parser_perform_template_parameter_access_checks (checks);
14313
14314       /* Perform the access control checks for the declarator and the
14315          decl-specifiers.  */
14316       perform_deferred_access_checks ();
14317
14318       /* Restore the saved value.  */
14319       if (TREE_CODE (decl) == FUNCTION_DECL)
14320         current_function_decl = saved_current_function_decl;
14321     }
14322
14323   /* Parse the initializer.  */
14324   initializer = NULL_TREE;
14325   is_direct_init = false;
14326   is_non_constant_init = true;
14327   if (is_initialized)
14328     {
14329       if (function_declarator_p (declarator))
14330         {
14331           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14332            if (initialization_kind == CPP_EQ)
14333              initializer = cp_parser_pure_specifier (parser);
14334            else
14335              {
14336                /* If the declaration was erroneous, we don't really
14337                   know what the user intended, so just silently
14338                   consume the initializer.  */
14339                if (decl != error_mark_node)
14340                  error_at (initializer_start_token->location,
14341                            "initializer provided for function");
14342                cp_parser_skip_to_closing_parenthesis (parser,
14343                                                       /*recovering=*/true,
14344                                                       /*or_comma=*/false,
14345                                                       /*consume_paren=*/true);
14346              }
14347         }
14348       else
14349         {
14350           /* We want to record the extra mangling scope for in-class
14351              initializers of class members and initializers of static data
14352              member templates.  The former is a C++0x feature which isn't
14353              implemented yet, and I expect it will involve deferring
14354              parsing of the initializer until end of class as with default
14355              arguments.  So right here we only handle the latter.  */
14356           if (!member_p && processing_template_decl)
14357             start_lambda_scope (decl);
14358           initializer = cp_parser_initializer (parser,
14359                                                &is_direct_init,
14360                                                &is_non_constant_init);
14361           if (!member_p && processing_template_decl)
14362             finish_lambda_scope ();
14363         }
14364     }
14365
14366   /* The old parser allows attributes to appear after a parenthesized
14367      initializer.  Mark Mitchell proposed removing this functionality
14368      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14369      attributes -- but ignores them.  */
14370   if (cp_parser_allow_gnu_extensions_p (parser)
14371       && initialization_kind == CPP_OPEN_PAREN)
14372     if (cp_parser_attributes_opt (parser))
14373       warning (OPT_Wattributes,
14374                "attributes after parenthesized initializer ignored");
14375
14376   /* For an in-class declaration, use `grokfield' to create the
14377      declaration.  */
14378   if (member_p)
14379     {
14380       if (pushed_scope)
14381         {
14382           pop_scope (pushed_scope);
14383           pushed_scope = false;
14384         }
14385       decl = grokfield (declarator, decl_specifiers,
14386                         initializer, !is_non_constant_init,
14387                         /*asmspec=*/NULL_TREE,
14388                         prefix_attributes);
14389       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14390         cp_parser_save_default_args (parser, decl);
14391     }
14392
14393   /* Finish processing the declaration.  But, skip friend
14394      declarations.  */
14395   if (!friend_p && decl && decl != error_mark_node)
14396     {
14397       cp_finish_decl (decl,
14398                       initializer, !is_non_constant_init,
14399                       asm_specification,
14400                       /* If the initializer is in parentheses, then this is
14401                          a direct-initialization, which means that an
14402                          `explicit' constructor is OK.  Otherwise, an
14403                          `explicit' constructor cannot be used.  */
14404                       ((is_direct_init || !is_initialized)
14405                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14406     }
14407   else if ((cxx_dialect != cxx98) && friend_p
14408            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14409     /* Core issue #226 (C++0x only): A default template-argument
14410        shall not be specified in a friend class template
14411        declaration. */
14412     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14413                              /*is_partial=*/0, /*is_friend_decl=*/1);
14414
14415   if (!friend_p && pushed_scope)
14416     pop_scope (pushed_scope);
14417
14418   return decl;
14419 }
14420
14421 /* Parse a declarator.
14422
14423    declarator:
14424      direct-declarator
14425      ptr-operator declarator
14426
14427    abstract-declarator:
14428      ptr-operator abstract-declarator [opt]
14429      direct-abstract-declarator
14430
14431    GNU Extensions:
14432
14433    declarator:
14434      attributes [opt] direct-declarator
14435      attributes [opt] ptr-operator declarator
14436
14437    abstract-declarator:
14438      attributes [opt] ptr-operator abstract-declarator [opt]
14439      attributes [opt] direct-abstract-declarator
14440
14441    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14442    detect constructor, destructor or conversion operators. It is set
14443    to -1 if the declarator is a name, and +1 if it is a
14444    function. Otherwise it is set to zero. Usually you just want to
14445    test for >0, but internally the negative value is used.
14446
14447    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14448    a decl-specifier-seq unless it declares a constructor, destructor,
14449    or conversion.  It might seem that we could check this condition in
14450    semantic analysis, rather than parsing, but that makes it difficult
14451    to handle something like `f()'.  We want to notice that there are
14452    no decl-specifiers, and therefore realize that this is an
14453    expression, not a declaration.)
14454
14455    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14456    the declarator is a direct-declarator of the form "(...)".
14457
14458    MEMBER_P is true iff this declarator is a member-declarator.  */
14459
14460 static cp_declarator *
14461 cp_parser_declarator (cp_parser* parser,
14462                       cp_parser_declarator_kind dcl_kind,
14463                       int* ctor_dtor_or_conv_p,
14464                       bool* parenthesized_p,
14465                       bool member_p)
14466 {
14467   cp_declarator *declarator;
14468   enum tree_code code;
14469   cp_cv_quals cv_quals;
14470   tree class_type;
14471   tree attributes = NULL_TREE;
14472
14473   /* Assume this is not a constructor, destructor, or type-conversion
14474      operator.  */
14475   if (ctor_dtor_or_conv_p)
14476     *ctor_dtor_or_conv_p = 0;
14477
14478   if (cp_parser_allow_gnu_extensions_p (parser))
14479     attributes = cp_parser_attributes_opt (parser);
14480
14481   /* Check for the ptr-operator production.  */
14482   cp_parser_parse_tentatively (parser);
14483   /* Parse the ptr-operator.  */
14484   code = cp_parser_ptr_operator (parser,
14485                                  &class_type,
14486                                  &cv_quals);
14487   /* If that worked, then we have a ptr-operator.  */
14488   if (cp_parser_parse_definitely (parser))
14489     {
14490       /* If a ptr-operator was found, then this declarator was not
14491          parenthesized.  */
14492       if (parenthesized_p)
14493         *parenthesized_p = true;
14494       /* The dependent declarator is optional if we are parsing an
14495          abstract-declarator.  */
14496       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14497         cp_parser_parse_tentatively (parser);
14498
14499       /* Parse the dependent declarator.  */
14500       declarator = cp_parser_declarator (parser, dcl_kind,
14501                                          /*ctor_dtor_or_conv_p=*/NULL,
14502                                          /*parenthesized_p=*/NULL,
14503                                          /*member_p=*/false);
14504
14505       /* If we are parsing an abstract-declarator, we must handle the
14506          case where the dependent declarator is absent.  */
14507       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14508           && !cp_parser_parse_definitely (parser))
14509         declarator = NULL;
14510
14511       declarator = cp_parser_make_indirect_declarator
14512         (code, class_type, cv_quals, declarator);
14513     }
14514   /* Everything else is a direct-declarator.  */
14515   else
14516     {
14517       if (parenthesized_p)
14518         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14519                                                    CPP_OPEN_PAREN);
14520       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14521                                                 ctor_dtor_or_conv_p,
14522                                                 member_p);
14523     }
14524
14525   if (attributes && declarator && declarator != cp_error_declarator)
14526     declarator->attributes = attributes;
14527
14528   return declarator;
14529 }
14530
14531 /* Parse a direct-declarator or direct-abstract-declarator.
14532
14533    direct-declarator:
14534      declarator-id
14535      direct-declarator ( parameter-declaration-clause )
14536        cv-qualifier-seq [opt]
14537        exception-specification [opt]
14538      direct-declarator [ constant-expression [opt] ]
14539      ( declarator )
14540
14541    direct-abstract-declarator:
14542      direct-abstract-declarator [opt]
14543        ( parameter-declaration-clause )
14544        cv-qualifier-seq [opt]
14545        exception-specification [opt]
14546      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14547      ( abstract-declarator )
14548
14549    Returns a representation of the declarator.  DCL_KIND is
14550    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14551    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14552    we are parsing a direct-declarator.  It is
14553    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14554    of ambiguity we prefer an abstract declarator, as per
14555    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14556    cp_parser_declarator.  */
14557
14558 static cp_declarator *
14559 cp_parser_direct_declarator (cp_parser* parser,
14560                              cp_parser_declarator_kind dcl_kind,
14561                              int* ctor_dtor_or_conv_p,
14562                              bool member_p)
14563 {
14564   cp_token *token;
14565   cp_declarator *declarator = NULL;
14566   tree scope = NULL_TREE;
14567   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14568   bool saved_in_declarator_p = parser->in_declarator_p;
14569   bool first = true;
14570   tree pushed_scope = NULL_TREE;
14571
14572   while (true)
14573     {
14574       /* Peek at the next token.  */
14575       token = cp_lexer_peek_token (parser->lexer);
14576       if (token->type == CPP_OPEN_PAREN)
14577         {
14578           /* This is either a parameter-declaration-clause, or a
14579              parenthesized declarator. When we know we are parsing a
14580              named declarator, it must be a parenthesized declarator
14581              if FIRST is true. For instance, `(int)' is a
14582              parameter-declaration-clause, with an omitted
14583              direct-abstract-declarator. But `((*))', is a
14584              parenthesized abstract declarator. Finally, when T is a
14585              template parameter `(T)' is a
14586              parameter-declaration-clause, and not a parenthesized
14587              named declarator.
14588
14589              We first try and parse a parameter-declaration-clause,
14590              and then try a nested declarator (if FIRST is true).
14591
14592              It is not an error for it not to be a
14593              parameter-declaration-clause, even when FIRST is
14594              false. Consider,
14595
14596                int i (int);
14597                int i (3);
14598
14599              The first is the declaration of a function while the
14600              second is the definition of a variable, including its
14601              initializer.
14602
14603              Having seen only the parenthesis, we cannot know which of
14604              these two alternatives should be selected.  Even more
14605              complex are examples like:
14606
14607                int i (int (a));
14608                int i (int (3));
14609
14610              The former is a function-declaration; the latter is a
14611              variable initialization.
14612
14613              Thus again, we try a parameter-declaration-clause, and if
14614              that fails, we back out and return.  */
14615
14616           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14617             {
14618               tree params;
14619               unsigned saved_num_template_parameter_lists;
14620               bool is_declarator = false;
14621               tree t;
14622
14623               /* In a member-declarator, the only valid interpretation
14624                  of a parenthesis is the start of a
14625                  parameter-declaration-clause.  (It is invalid to
14626                  initialize a static data member with a parenthesized
14627                  initializer; only the "=" form of initialization is
14628                  permitted.)  */
14629               if (!member_p)
14630                 cp_parser_parse_tentatively (parser);
14631
14632               /* Consume the `('.  */
14633               cp_lexer_consume_token (parser->lexer);
14634               if (first)
14635                 {
14636                   /* If this is going to be an abstract declarator, we're
14637                      in a declarator and we can't have default args.  */
14638                   parser->default_arg_ok_p = false;
14639                   parser->in_declarator_p = true;
14640                 }
14641
14642               /* Inside the function parameter list, surrounding
14643                  template-parameter-lists do not apply.  */
14644               saved_num_template_parameter_lists
14645                 = parser->num_template_parameter_lists;
14646               parser->num_template_parameter_lists = 0;
14647
14648               begin_scope (sk_function_parms, NULL_TREE);
14649
14650               /* Parse the parameter-declaration-clause.  */
14651               params = cp_parser_parameter_declaration_clause (parser);
14652
14653               parser->num_template_parameter_lists
14654                 = saved_num_template_parameter_lists;
14655
14656               /* If all went well, parse the cv-qualifier-seq and the
14657                  exception-specification.  */
14658               if (member_p || cp_parser_parse_definitely (parser))
14659                 {
14660                   cp_cv_quals cv_quals;
14661                   tree exception_specification;
14662                   tree late_return;
14663
14664                   is_declarator = true;
14665
14666                   if (ctor_dtor_or_conv_p)
14667                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14668                   first = false;
14669                   /* Consume the `)'.  */
14670                   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14671
14672                   /* Parse the cv-qualifier-seq.  */
14673                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14674                   /* And the exception-specification.  */
14675                   exception_specification
14676                     = cp_parser_exception_specification_opt (parser);
14677
14678                   late_return
14679                     = cp_parser_late_return_type_opt (parser);
14680
14681                   /* Create the function-declarator.  */
14682                   declarator = make_call_declarator (declarator,
14683                                                      params,
14684                                                      cv_quals,
14685                                                      exception_specification,
14686                                                      late_return);
14687                   /* Any subsequent parameter lists are to do with
14688                      return type, so are not those of the declared
14689                      function.  */
14690                   parser->default_arg_ok_p = false;
14691                 }
14692
14693               /* Remove the function parms from scope.  */
14694               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14695                 pop_binding (DECL_NAME (t), t);
14696               leave_scope();
14697
14698               if (is_declarator)
14699                 /* Repeat the main loop.  */
14700                 continue;
14701             }
14702
14703           /* If this is the first, we can try a parenthesized
14704              declarator.  */
14705           if (first)
14706             {
14707               bool saved_in_type_id_in_expr_p;
14708
14709               parser->default_arg_ok_p = saved_default_arg_ok_p;
14710               parser->in_declarator_p = saved_in_declarator_p;
14711
14712               /* Consume the `('.  */
14713               cp_lexer_consume_token (parser->lexer);
14714               /* Parse the nested declarator.  */
14715               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14716               parser->in_type_id_in_expr_p = true;
14717               declarator
14718                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14719                                         /*parenthesized_p=*/NULL,
14720                                         member_p);
14721               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14722               first = false;
14723               /* Expect a `)'.  */
14724               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14725                 declarator = cp_error_declarator;
14726               if (declarator == cp_error_declarator)
14727                 break;
14728
14729               goto handle_declarator;
14730             }
14731           /* Otherwise, we must be done.  */
14732           else
14733             break;
14734         }
14735       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14736                && token->type == CPP_OPEN_SQUARE)
14737         {
14738           /* Parse an array-declarator.  */
14739           tree bounds;
14740
14741           if (ctor_dtor_or_conv_p)
14742             *ctor_dtor_or_conv_p = 0;
14743
14744           first = false;
14745           parser->default_arg_ok_p = false;
14746           parser->in_declarator_p = true;
14747           /* Consume the `['.  */
14748           cp_lexer_consume_token (parser->lexer);
14749           /* Peek at the next token.  */
14750           token = cp_lexer_peek_token (parser->lexer);
14751           /* If the next token is `]', then there is no
14752              constant-expression.  */
14753           if (token->type != CPP_CLOSE_SQUARE)
14754             {
14755               bool non_constant_p;
14756
14757               bounds
14758                 = cp_parser_constant_expression (parser,
14759                                                  /*allow_non_constant=*/true,
14760                                                  &non_constant_p);
14761               if (!non_constant_p)
14762                 bounds = fold_non_dependent_expr (bounds);
14763               /* Normally, the array bound must be an integral constant
14764                  expression.  However, as an extension, we allow VLAs
14765                  in function scopes as long as they aren't part of a
14766                  parameter declaration.  */
14767               else if (!parser->in_function_body
14768                        || current_binding_level->kind == sk_function_parms)
14769                 {
14770                   cp_parser_error (parser,
14771                                    "array bound is not an integer constant");
14772                   bounds = error_mark_node;
14773                 }
14774               else if (processing_template_decl && !error_operand_p (bounds))
14775                 {
14776                   /* Remember this wasn't a constant-expression.  */
14777                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14778                   TREE_SIDE_EFFECTS (bounds) = 1;
14779                 }
14780             }
14781           else
14782             bounds = NULL_TREE;
14783           /* Look for the closing `]'.  */
14784           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14785             {
14786               declarator = cp_error_declarator;
14787               break;
14788             }
14789
14790           declarator = make_array_declarator (declarator, bounds);
14791         }
14792       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14793         {
14794           {
14795             tree qualifying_scope;
14796             tree unqualified_name;
14797             special_function_kind sfk;
14798             bool abstract_ok;
14799             bool pack_expansion_p = false;
14800             cp_token *declarator_id_start_token;
14801
14802             /* Parse a declarator-id */
14803             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14804             if (abstract_ok)
14805               {
14806                 cp_parser_parse_tentatively (parser);
14807
14808                 /* If we see an ellipsis, we should be looking at a
14809                    parameter pack. */
14810                 if (token->type == CPP_ELLIPSIS)
14811                   {
14812                     /* Consume the `...' */
14813                     cp_lexer_consume_token (parser->lexer);
14814
14815                     pack_expansion_p = true;
14816                   }
14817               }
14818
14819             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14820             unqualified_name
14821               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14822             qualifying_scope = parser->scope;
14823             if (abstract_ok)
14824               {
14825                 bool okay = false;
14826
14827                 if (!unqualified_name && pack_expansion_p)
14828                   {
14829                     /* Check whether an error occurred. */
14830                     okay = !cp_parser_error_occurred (parser);
14831
14832                     /* We already consumed the ellipsis to mark a
14833                        parameter pack, but we have no way to report it,
14834                        so abort the tentative parse. We will be exiting
14835                        immediately anyway. */
14836                     cp_parser_abort_tentative_parse (parser);
14837                   }
14838                 else
14839                   okay = cp_parser_parse_definitely (parser);
14840
14841                 if (!okay)
14842                   unqualified_name = error_mark_node;
14843                 else if (unqualified_name
14844                          && (qualifying_scope
14845                              || (TREE_CODE (unqualified_name)
14846                                  != IDENTIFIER_NODE)))
14847                   {
14848                     cp_parser_error (parser, "expected unqualified-id");
14849                     unqualified_name = error_mark_node;
14850                   }
14851               }
14852
14853             if (!unqualified_name)
14854               return NULL;
14855             if (unqualified_name == error_mark_node)
14856               {
14857                 declarator = cp_error_declarator;
14858                 pack_expansion_p = false;
14859                 declarator->parameter_pack_p = false;
14860                 break;
14861               }
14862
14863             if (qualifying_scope && at_namespace_scope_p ()
14864                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14865               {
14866                 /* In the declaration of a member of a template class
14867                    outside of the class itself, the SCOPE will sometimes
14868                    be a TYPENAME_TYPE.  For example, given:
14869
14870                    template <typename T>
14871                    int S<T>::R::i = 3;
14872
14873                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14874                    this context, we must resolve S<T>::R to an ordinary
14875                    type, rather than a typename type.
14876
14877                    The reason we normally avoid resolving TYPENAME_TYPEs
14878                    is that a specialization of `S' might render
14879                    `S<T>::R' not a type.  However, if `S' is
14880                    specialized, then this `i' will not be used, so there
14881                    is no harm in resolving the types here.  */
14882                 tree type;
14883
14884                 /* Resolve the TYPENAME_TYPE.  */
14885                 type = resolve_typename_type (qualifying_scope,
14886                                               /*only_current_p=*/false);
14887                 /* If that failed, the declarator is invalid.  */
14888                 if (TREE_CODE (type) == TYPENAME_TYPE)
14889                   {
14890                     if (typedef_variant_p (type))
14891                       error_at (declarator_id_start_token->location,
14892                                 "cannot define member of dependent typedef "
14893                                 "%qT", type);
14894                     else
14895                       error_at (declarator_id_start_token->location,
14896                                 "%<%T::%E%> is not a type",
14897                                 TYPE_CONTEXT (qualifying_scope),
14898                                 TYPE_IDENTIFIER (qualifying_scope));
14899                   }
14900                 qualifying_scope = type;
14901               }
14902
14903             sfk = sfk_none;
14904
14905             if (unqualified_name)
14906               {
14907                 tree class_type;
14908
14909                 if (qualifying_scope
14910                     && CLASS_TYPE_P (qualifying_scope))
14911                   class_type = qualifying_scope;
14912                 else
14913                   class_type = current_class_type;
14914
14915                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14916                   {
14917                     tree name_type = TREE_TYPE (unqualified_name);
14918                     if (class_type && same_type_p (name_type, class_type))
14919                       {
14920                         if (qualifying_scope
14921                             && CLASSTYPE_USE_TEMPLATE (name_type))
14922                           {
14923                             error_at (declarator_id_start_token->location,
14924                                       "invalid use of constructor as a template");
14925                             inform (declarator_id_start_token->location,
14926                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14927                                     "name the constructor in a qualified name",
14928                                     class_type,
14929                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14930                                     class_type, name_type);
14931                             declarator = cp_error_declarator;
14932                             break;
14933                           }
14934                         else
14935                           unqualified_name = constructor_name (class_type);
14936                       }
14937                     else
14938                       {
14939                         /* We do not attempt to print the declarator
14940                            here because we do not have enough
14941                            information about its original syntactic
14942                            form.  */
14943                         cp_parser_error (parser, "invalid declarator");
14944                         declarator = cp_error_declarator;
14945                         break;
14946                       }
14947                   }
14948
14949                 if (class_type)
14950                   {
14951                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14952                       sfk = sfk_destructor;
14953                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14954                       sfk = sfk_conversion;
14955                     else if (/* There's no way to declare a constructor
14956                                 for an anonymous type, even if the type
14957                                 got a name for linkage purposes.  */
14958                              !TYPE_WAS_ANONYMOUS (class_type)
14959                              && constructor_name_p (unqualified_name,
14960                                                     class_type))
14961                       {
14962                         unqualified_name = constructor_name (class_type);
14963                         sfk = sfk_constructor;
14964                       }
14965                     else if (is_overloaded_fn (unqualified_name)
14966                              && DECL_CONSTRUCTOR_P (get_first_fn
14967                                                     (unqualified_name)))
14968                       sfk = sfk_constructor;
14969
14970                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14971                       *ctor_dtor_or_conv_p = -1;
14972                   }
14973               }
14974             declarator = make_id_declarator (qualifying_scope,
14975                                              unqualified_name,
14976                                              sfk);
14977             declarator->id_loc = token->location;
14978             declarator->parameter_pack_p = pack_expansion_p;
14979
14980             if (pack_expansion_p)
14981               maybe_warn_variadic_templates ();
14982           }
14983
14984         handle_declarator:;
14985           scope = get_scope_of_declarator (declarator);
14986           if (scope)
14987             /* Any names that appear after the declarator-id for a
14988                member are looked up in the containing scope.  */
14989             pushed_scope = push_scope (scope);
14990           parser->in_declarator_p = true;
14991           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14992               || (declarator && declarator->kind == cdk_id))
14993             /* Default args are only allowed on function
14994                declarations.  */
14995             parser->default_arg_ok_p = saved_default_arg_ok_p;
14996           else
14997             parser->default_arg_ok_p = false;
14998
14999           first = false;
15000         }
15001       /* We're done.  */
15002       else
15003         break;
15004     }
15005
15006   /* For an abstract declarator, we might wind up with nothing at this
15007      point.  That's an error; the declarator is not optional.  */
15008   if (!declarator)
15009     cp_parser_error (parser, "expected declarator");
15010
15011   /* If we entered a scope, we must exit it now.  */
15012   if (pushed_scope)
15013     pop_scope (pushed_scope);
15014
15015   parser->default_arg_ok_p = saved_default_arg_ok_p;
15016   parser->in_declarator_p = saved_in_declarator_p;
15017
15018   return declarator;
15019 }
15020
15021 /* Parse a ptr-operator.
15022
15023    ptr-operator:
15024      * cv-qualifier-seq [opt]
15025      &
15026      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15027
15028    GNU Extension:
15029
15030    ptr-operator:
15031      & cv-qualifier-seq [opt]
15032
15033    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15034    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15035    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15036    filled in with the TYPE containing the member.  *CV_QUALS is
15037    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15038    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15039    Note that the tree codes returned by this function have nothing
15040    to do with the types of trees that will be eventually be created
15041    to represent the pointer or reference type being parsed. They are
15042    just constants with suggestive names. */
15043 static enum tree_code
15044 cp_parser_ptr_operator (cp_parser* parser,
15045                         tree* type,
15046                         cp_cv_quals *cv_quals)
15047 {
15048   enum tree_code code = ERROR_MARK;
15049   cp_token *token;
15050
15051   /* Assume that it's not a pointer-to-member.  */
15052   *type = NULL_TREE;
15053   /* And that there are no cv-qualifiers.  */
15054   *cv_quals = TYPE_UNQUALIFIED;
15055
15056   /* Peek at the next token.  */
15057   token = cp_lexer_peek_token (parser->lexer);
15058
15059   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15060   if (token->type == CPP_MULT)
15061     code = INDIRECT_REF;
15062   else if (token->type == CPP_AND)
15063     code = ADDR_EXPR;
15064   else if ((cxx_dialect != cxx98) &&
15065            token->type == CPP_AND_AND) /* C++0x only */
15066     code = NON_LVALUE_EXPR;
15067
15068   if (code != ERROR_MARK)
15069     {
15070       /* Consume the `*', `&' or `&&'.  */
15071       cp_lexer_consume_token (parser->lexer);
15072
15073       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15074          `&', if we are allowing GNU extensions.  (The only qualifier
15075          that can legally appear after `&' is `restrict', but that is
15076          enforced during semantic analysis.  */
15077       if (code == INDIRECT_REF
15078           || cp_parser_allow_gnu_extensions_p (parser))
15079         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15080     }
15081   else
15082     {
15083       /* Try the pointer-to-member case.  */
15084       cp_parser_parse_tentatively (parser);
15085       /* Look for the optional `::' operator.  */
15086       cp_parser_global_scope_opt (parser,
15087                                   /*current_scope_valid_p=*/false);
15088       /* Look for the nested-name specifier.  */
15089       token = cp_lexer_peek_token (parser->lexer);
15090       cp_parser_nested_name_specifier (parser,
15091                                        /*typename_keyword_p=*/false,
15092                                        /*check_dependency_p=*/true,
15093                                        /*type_p=*/false,
15094                                        /*is_declaration=*/false);
15095       /* If we found it, and the next token is a `*', then we are
15096          indeed looking at a pointer-to-member operator.  */
15097       if (!cp_parser_error_occurred (parser)
15098           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15099         {
15100           /* Indicate that the `*' operator was used.  */
15101           code = INDIRECT_REF;
15102
15103           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15104             error_at (token->location, "%qD is a namespace", parser->scope);
15105           else
15106             {
15107               /* The type of which the member is a member is given by the
15108                  current SCOPE.  */
15109               *type = parser->scope;
15110               /* The next name will not be qualified.  */
15111               parser->scope = NULL_TREE;
15112               parser->qualifying_scope = NULL_TREE;
15113               parser->object_scope = NULL_TREE;
15114               /* Look for the optional cv-qualifier-seq.  */
15115               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15116             }
15117         }
15118       /* If that didn't work we don't have a ptr-operator.  */
15119       if (!cp_parser_parse_definitely (parser))
15120         cp_parser_error (parser, "expected ptr-operator");
15121     }
15122
15123   return code;
15124 }
15125
15126 /* Parse an (optional) cv-qualifier-seq.
15127
15128    cv-qualifier-seq:
15129      cv-qualifier cv-qualifier-seq [opt]
15130
15131    cv-qualifier:
15132      const
15133      volatile
15134
15135    GNU Extension:
15136
15137    cv-qualifier:
15138      __restrict__
15139
15140    Returns a bitmask representing the cv-qualifiers.  */
15141
15142 static cp_cv_quals
15143 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15144 {
15145   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15146
15147   while (true)
15148     {
15149       cp_token *token;
15150       cp_cv_quals cv_qualifier;
15151
15152       /* Peek at the next token.  */
15153       token = cp_lexer_peek_token (parser->lexer);
15154       /* See if it's a cv-qualifier.  */
15155       switch (token->keyword)
15156         {
15157         case RID_CONST:
15158           cv_qualifier = TYPE_QUAL_CONST;
15159           break;
15160
15161         case RID_VOLATILE:
15162           cv_qualifier = TYPE_QUAL_VOLATILE;
15163           break;
15164
15165         case RID_RESTRICT:
15166           cv_qualifier = TYPE_QUAL_RESTRICT;
15167           break;
15168
15169         default:
15170           cv_qualifier = TYPE_UNQUALIFIED;
15171           break;
15172         }
15173
15174       if (!cv_qualifier)
15175         break;
15176
15177       if (cv_quals & cv_qualifier)
15178         {
15179           error_at (token->location, "duplicate cv-qualifier");
15180           cp_lexer_purge_token (parser->lexer);
15181         }
15182       else
15183         {
15184           cp_lexer_consume_token (parser->lexer);
15185           cv_quals |= cv_qualifier;
15186         }
15187     }
15188
15189   return cv_quals;
15190 }
15191
15192 /* Parse a late-specified return type, if any.  This is not a separate
15193    non-terminal, but part of a function declarator, which looks like
15194
15195    -> trailing-type-specifier-seq abstract-declarator(opt)
15196
15197    Returns the type indicated by the type-id.  */
15198
15199 static tree
15200 cp_parser_late_return_type_opt (cp_parser* parser)
15201 {
15202   cp_token *token;
15203
15204   /* Peek at the next token.  */
15205   token = cp_lexer_peek_token (parser->lexer);
15206   /* A late-specified return type is indicated by an initial '->'. */
15207   if (token->type != CPP_DEREF)
15208     return NULL_TREE;
15209
15210   /* Consume the ->.  */
15211   cp_lexer_consume_token (parser->lexer);
15212
15213   return cp_parser_trailing_type_id (parser);
15214 }
15215
15216 /* Parse a declarator-id.
15217
15218    declarator-id:
15219      id-expression
15220      :: [opt] nested-name-specifier [opt] type-name
15221
15222    In the `id-expression' case, the value returned is as for
15223    cp_parser_id_expression if the id-expression was an unqualified-id.
15224    If the id-expression was a qualified-id, then a SCOPE_REF is
15225    returned.  The first operand is the scope (either a NAMESPACE_DECL
15226    or TREE_TYPE), but the second is still just a representation of an
15227    unqualified-id.  */
15228
15229 static tree
15230 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15231 {
15232   tree id;
15233   /* The expression must be an id-expression.  Assume that qualified
15234      names are the names of types so that:
15235
15236        template <class T>
15237        int S<T>::R::i = 3;
15238
15239      will work; we must treat `S<T>::R' as the name of a type.
15240      Similarly, assume that qualified names are templates, where
15241      required, so that:
15242
15243        template <class T>
15244        int S<T>::R<T>::i = 3;
15245
15246      will work, too.  */
15247   id = cp_parser_id_expression (parser,
15248                                 /*template_keyword_p=*/false,
15249                                 /*check_dependency_p=*/false,
15250                                 /*template_p=*/NULL,
15251                                 /*declarator_p=*/true,
15252                                 optional_p);
15253   if (id && BASELINK_P (id))
15254     id = BASELINK_FUNCTIONS (id);
15255   return id;
15256 }
15257
15258 /* Parse a type-id.
15259
15260    type-id:
15261      type-specifier-seq abstract-declarator [opt]
15262
15263    Returns the TYPE specified.  */
15264
15265 static tree
15266 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15267                      bool is_trailing_return)
15268 {
15269   cp_decl_specifier_seq type_specifier_seq;
15270   cp_declarator *abstract_declarator;
15271
15272   /* Parse the type-specifier-seq.  */
15273   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15274                                 is_trailing_return,
15275                                 &type_specifier_seq);
15276   if (type_specifier_seq.type == error_mark_node)
15277     return error_mark_node;
15278
15279   /* There might or might not be an abstract declarator.  */
15280   cp_parser_parse_tentatively (parser);
15281   /* Look for the declarator.  */
15282   abstract_declarator
15283     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15284                             /*parenthesized_p=*/NULL,
15285                             /*member_p=*/false);
15286   /* Check to see if there really was a declarator.  */
15287   if (!cp_parser_parse_definitely (parser))
15288     abstract_declarator = NULL;
15289
15290   if (type_specifier_seq.type
15291       && type_uses_auto (type_specifier_seq.type))
15292     {
15293       /* A type-id with type 'auto' is only ok if the abstract declarator
15294          is a function declarator with a late-specified return type.  */
15295       if (abstract_declarator
15296           && abstract_declarator->kind == cdk_function
15297           && abstract_declarator->u.function.late_return_type)
15298         /* OK */;
15299       else
15300         {
15301           error ("invalid use of %<auto%>");
15302           return error_mark_node;
15303         }
15304     }
15305   
15306   return groktypename (&type_specifier_seq, abstract_declarator,
15307                        is_template_arg);
15308 }
15309
15310 static tree cp_parser_type_id (cp_parser *parser)
15311 {
15312   return cp_parser_type_id_1 (parser, false, false);
15313 }
15314
15315 static tree cp_parser_template_type_arg (cp_parser *parser)
15316 {
15317   return cp_parser_type_id_1 (parser, true, false);
15318 }
15319
15320 static tree cp_parser_trailing_type_id (cp_parser *parser)
15321 {
15322   return cp_parser_type_id_1 (parser, false, true);
15323 }
15324
15325 /* Parse a type-specifier-seq.
15326
15327    type-specifier-seq:
15328      type-specifier type-specifier-seq [opt]
15329
15330    GNU extension:
15331
15332    type-specifier-seq:
15333      attributes type-specifier-seq [opt]
15334
15335    If IS_DECLARATION is true, we are at the start of a "condition" or
15336    exception-declaration, so we might be followed by a declarator-id.
15337
15338    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15339    i.e. we've just seen "->".
15340
15341    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15342
15343 static void
15344 cp_parser_type_specifier_seq (cp_parser* parser,
15345                               bool is_declaration,
15346                               bool is_trailing_return,
15347                               cp_decl_specifier_seq *type_specifier_seq)
15348 {
15349   bool seen_type_specifier = false;
15350   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15351   cp_token *start_token = NULL;
15352
15353   /* Clear the TYPE_SPECIFIER_SEQ.  */
15354   clear_decl_specs (type_specifier_seq);
15355
15356   /* In the context of a trailing return type, enum E { } is an
15357      elaborated-type-specifier followed by a function-body, not an
15358      enum-specifier.  */
15359   if (is_trailing_return)
15360     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15361
15362   /* Parse the type-specifiers and attributes.  */
15363   while (true)
15364     {
15365       tree type_specifier;
15366       bool is_cv_qualifier;
15367
15368       /* Check for attributes first.  */
15369       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15370         {
15371           type_specifier_seq->attributes =
15372             chainon (type_specifier_seq->attributes,
15373                      cp_parser_attributes_opt (parser));
15374           continue;
15375         }
15376
15377       /* record the token of the beginning of the type specifier seq,
15378          for error reporting purposes*/
15379      if (!start_token)
15380        start_token = cp_lexer_peek_token (parser->lexer);
15381
15382       /* Look for the type-specifier.  */
15383       type_specifier = cp_parser_type_specifier (parser,
15384                                                  flags,
15385                                                  type_specifier_seq,
15386                                                  /*is_declaration=*/false,
15387                                                  NULL,
15388                                                  &is_cv_qualifier);
15389       if (!type_specifier)
15390         {
15391           /* If the first type-specifier could not be found, this is not a
15392              type-specifier-seq at all.  */
15393           if (!seen_type_specifier)
15394             {
15395               cp_parser_error (parser, "expected type-specifier");
15396               type_specifier_seq->type = error_mark_node;
15397               return;
15398             }
15399           /* If subsequent type-specifiers could not be found, the
15400              type-specifier-seq is complete.  */
15401           break;
15402         }
15403
15404       seen_type_specifier = true;
15405       /* The standard says that a condition can be:
15406
15407             type-specifier-seq declarator = assignment-expression
15408
15409          However, given:
15410
15411            struct S {};
15412            if (int S = ...)
15413
15414          we should treat the "S" as a declarator, not as a
15415          type-specifier.  The standard doesn't say that explicitly for
15416          type-specifier-seq, but it does say that for
15417          decl-specifier-seq in an ordinary declaration.  Perhaps it
15418          would be clearer just to allow a decl-specifier-seq here, and
15419          then add a semantic restriction that if any decl-specifiers
15420          that are not type-specifiers appear, the program is invalid.  */
15421       if (is_declaration && !is_cv_qualifier)
15422         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15423     }
15424
15425   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15426 }
15427
15428 /* Parse a parameter-declaration-clause.
15429
15430    parameter-declaration-clause:
15431      parameter-declaration-list [opt] ... [opt]
15432      parameter-declaration-list , ...
15433
15434    Returns a representation for the parameter declarations.  A return
15435    value of NULL indicates a parameter-declaration-clause consisting
15436    only of an ellipsis.  */
15437
15438 static tree
15439 cp_parser_parameter_declaration_clause (cp_parser* parser)
15440 {
15441   tree parameters;
15442   cp_token *token;
15443   bool ellipsis_p;
15444   bool is_error;
15445
15446   /* Peek at the next token.  */
15447   token = cp_lexer_peek_token (parser->lexer);
15448   /* Check for trivial parameter-declaration-clauses.  */
15449   if (token->type == CPP_ELLIPSIS)
15450     {
15451       /* Consume the `...' token.  */
15452       cp_lexer_consume_token (parser->lexer);
15453       return NULL_TREE;
15454     }
15455   else if (token->type == CPP_CLOSE_PAREN)
15456     /* There are no parameters.  */
15457     {
15458 #ifndef NO_IMPLICIT_EXTERN_C
15459       if (in_system_header && current_class_type == NULL
15460           && current_lang_name == lang_name_c)
15461         return NULL_TREE;
15462       else
15463 #endif
15464         return void_list_node;
15465     }
15466   /* Check for `(void)', too, which is a special case.  */
15467   else if (token->keyword == RID_VOID
15468            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15469                == CPP_CLOSE_PAREN))
15470     {
15471       /* Consume the `void' token.  */
15472       cp_lexer_consume_token (parser->lexer);
15473       /* There are no parameters.  */
15474       return void_list_node;
15475     }
15476
15477   /* Parse the parameter-declaration-list.  */
15478   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15479   /* If a parse error occurred while parsing the
15480      parameter-declaration-list, then the entire
15481      parameter-declaration-clause is erroneous.  */
15482   if (is_error)
15483     return NULL;
15484
15485   /* Peek at the next token.  */
15486   token = cp_lexer_peek_token (parser->lexer);
15487   /* If it's a `,', the clause should terminate with an ellipsis.  */
15488   if (token->type == CPP_COMMA)
15489     {
15490       /* Consume the `,'.  */
15491       cp_lexer_consume_token (parser->lexer);
15492       /* Expect an ellipsis.  */
15493       ellipsis_p
15494         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15495     }
15496   /* It might also be `...' if the optional trailing `,' was
15497      omitted.  */
15498   else if (token->type == CPP_ELLIPSIS)
15499     {
15500       /* Consume the `...' token.  */
15501       cp_lexer_consume_token (parser->lexer);
15502       /* And remember that we saw it.  */
15503       ellipsis_p = true;
15504     }
15505   else
15506     ellipsis_p = false;
15507
15508   /* Finish the parameter list.  */
15509   if (!ellipsis_p)
15510     parameters = chainon (parameters, void_list_node);
15511
15512   return parameters;
15513 }
15514
15515 /* Parse a parameter-declaration-list.
15516
15517    parameter-declaration-list:
15518      parameter-declaration
15519      parameter-declaration-list , parameter-declaration
15520
15521    Returns a representation of the parameter-declaration-list, as for
15522    cp_parser_parameter_declaration_clause.  However, the
15523    `void_list_node' is never appended to the list.  Upon return,
15524    *IS_ERROR will be true iff an error occurred.  */
15525
15526 static tree
15527 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15528 {
15529   tree parameters = NULL_TREE;
15530   tree *tail = &parameters; 
15531   bool saved_in_unbraced_linkage_specification_p;
15532   int index = 0;
15533
15534   /* Assume all will go well.  */
15535   *is_error = false;
15536   /* The special considerations that apply to a function within an
15537      unbraced linkage specifications do not apply to the parameters
15538      to the function.  */
15539   saved_in_unbraced_linkage_specification_p 
15540     = parser->in_unbraced_linkage_specification_p;
15541   parser->in_unbraced_linkage_specification_p = false;
15542
15543   /* Look for more parameters.  */
15544   while (true)
15545     {
15546       cp_parameter_declarator *parameter;
15547       tree decl = error_mark_node;
15548       bool parenthesized_p;
15549       /* Parse the parameter.  */
15550       parameter
15551         = cp_parser_parameter_declaration (parser,
15552                                            /*template_parm_p=*/false,
15553                                            &parenthesized_p);
15554
15555       /* We don't know yet if the enclosing context is deprecated, so wait
15556          and warn in grokparms if appropriate.  */
15557       deprecated_state = DEPRECATED_SUPPRESS;
15558
15559       if (parameter)
15560         decl = grokdeclarator (parameter->declarator,
15561                                &parameter->decl_specifiers,
15562                                PARM,
15563                                parameter->default_argument != NULL_TREE,
15564                                &parameter->decl_specifiers.attributes);
15565
15566       deprecated_state = DEPRECATED_NORMAL;
15567
15568       /* If a parse error occurred parsing the parameter declaration,
15569          then the entire parameter-declaration-list is erroneous.  */
15570       if (decl == error_mark_node)
15571         {
15572           *is_error = true;
15573           parameters = error_mark_node;
15574           break;
15575         }
15576
15577       if (parameter->decl_specifiers.attributes)
15578         cplus_decl_attributes (&decl,
15579                                parameter->decl_specifiers.attributes,
15580                                0);
15581       if (DECL_NAME (decl))
15582         decl = pushdecl (decl);
15583
15584       if (decl != error_mark_node)
15585         {
15586           retrofit_lang_decl (decl);
15587           DECL_PARM_INDEX (decl) = ++index;
15588         }
15589
15590       /* Add the new parameter to the list.  */
15591       *tail = build_tree_list (parameter->default_argument, decl);
15592       tail = &TREE_CHAIN (*tail);
15593
15594       /* Peek at the next token.  */
15595       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15596           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15597           /* These are for Objective-C++ */
15598           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15599           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15600         /* The parameter-declaration-list is complete.  */
15601         break;
15602       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15603         {
15604           cp_token *token;
15605
15606           /* Peek at the next token.  */
15607           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15608           /* If it's an ellipsis, then the list is complete.  */
15609           if (token->type == CPP_ELLIPSIS)
15610             break;
15611           /* Otherwise, there must be more parameters.  Consume the
15612              `,'.  */
15613           cp_lexer_consume_token (parser->lexer);
15614           /* When parsing something like:
15615
15616                 int i(float f, double d)
15617
15618              we can tell after seeing the declaration for "f" that we
15619              are not looking at an initialization of a variable "i",
15620              but rather at the declaration of a function "i".
15621
15622              Due to the fact that the parsing of template arguments
15623              (as specified to a template-id) requires backtracking we
15624              cannot use this technique when inside a template argument
15625              list.  */
15626           if (!parser->in_template_argument_list_p
15627               && !parser->in_type_id_in_expr_p
15628               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15629               /* However, a parameter-declaration of the form
15630                  "foat(f)" (which is a valid declaration of a
15631                  parameter "f") can also be interpreted as an
15632                  expression (the conversion of "f" to "float").  */
15633               && !parenthesized_p)
15634             cp_parser_commit_to_tentative_parse (parser);
15635         }
15636       else
15637         {
15638           cp_parser_error (parser, "expected %<,%> or %<...%>");
15639           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15640             cp_parser_skip_to_closing_parenthesis (parser,
15641                                                    /*recovering=*/true,
15642                                                    /*or_comma=*/false,
15643                                                    /*consume_paren=*/false);
15644           break;
15645         }
15646     }
15647
15648   parser->in_unbraced_linkage_specification_p
15649     = saved_in_unbraced_linkage_specification_p;
15650
15651   return parameters;
15652 }
15653
15654 /* Parse a parameter declaration.
15655
15656    parameter-declaration:
15657      decl-specifier-seq ... [opt] declarator
15658      decl-specifier-seq declarator = assignment-expression
15659      decl-specifier-seq ... [opt] abstract-declarator [opt]
15660      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15661
15662    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15663    declares a template parameter.  (In that case, a non-nested `>'
15664    token encountered during the parsing of the assignment-expression
15665    is not interpreted as a greater-than operator.)
15666
15667    Returns a representation of the parameter, or NULL if an error
15668    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15669    true iff the declarator is of the form "(p)".  */
15670
15671 static cp_parameter_declarator *
15672 cp_parser_parameter_declaration (cp_parser *parser,
15673                                  bool template_parm_p,
15674                                  bool *parenthesized_p)
15675 {
15676   int declares_class_or_enum;
15677   cp_decl_specifier_seq decl_specifiers;
15678   cp_declarator *declarator;
15679   tree default_argument;
15680   cp_token *token = NULL, *declarator_token_start = NULL;
15681   const char *saved_message;
15682
15683   /* In a template parameter, `>' is not an operator.
15684
15685      [temp.param]
15686
15687      When parsing a default template-argument for a non-type
15688      template-parameter, the first non-nested `>' is taken as the end
15689      of the template parameter-list rather than a greater-than
15690      operator.  */
15691
15692   /* Type definitions may not appear in parameter types.  */
15693   saved_message = parser->type_definition_forbidden_message;
15694   parser->type_definition_forbidden_message
15695     = G_("types may not be defined in parameter types");
15696
15697   /* Parse the declaration-specifiers.  */
15698   cp_parser_decl_specifier_seq (parser,
15699                                 CP_PARSER_FLAGS_NONE,
15700                                 &decl_specifiers,
15701                                 &declares_class_or_enum);
15702
15703   /* Complain about missing 'typename' or other invalid type names.  */
15704   if (!decl_specifiers.any_type_specifiers_p)
15705     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15706
15707   /* If an error occurred, there's no reason to attempt to parse the
15708      rest of the declaration.  */
15709   if (cp_parser_error_occurred (parser))
15710     {
15711       parser->type_definition_forbidden_message = saved_message;
15712       return NULL;
15713     }
15714
15715   /* Peek at the next token.  */
15716   token = cp_lexer_peek_token (parser->lexer);
15717
15718   /* If the next token is a `)', `,', `=', `>', or `...', then there
15719      is no declarator. However, when variadic templates are enabled,
15720      there may be a declarator following `...'.  */
15721   if (token->type == CPP_CLOSE_PAREN
15722       || token->type == CPP_COMMA
15723       || token->type == CPP_EQ
15724       || token->type == CPP_GREATER)
15725     {
15726       declarator = NULL;
15727       if (parenthesized_p)
15728         *parenthesized_p = false;
15729     }
15730   /* Otherwise, there should be a declarator.  */
15731   else
15732     {
15733       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15734       parser->default_arg_ok_p = false;
15735
15736       /* After seeing a decl-specifier-seq, if the next token is not a
15737          "(", there is no possibility that the code is a valid
15738          expression.  Therefore, if parsing tentatively, we commit at
15739          this point.  */
15740       if (!parser->in_template_argument_list_p
15741           /* In an expression context, having seen:
15742
15743                (int((char ...
15744
15745              we cannot be sure whether we are looking at a
15746              function-type (taking a "char" as a parameter) or a cast
15747              of some object of type "char" to "int".  */
15748           && !parser->in_type_id_in_expr_p
15749           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15750           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15751         cp_parser_commit_to_tentative_parse (parser);
15752       /* Parse the declarator.  */
15753       declarator_token_start = token;
15754       declarator = cp_parser_declarator (parser,
15755                                          CP_PARSER_DECLARATOR_EITHER,
15756                                          /*ctor_dtor_or_conv_p=*/NULL,
15757                                          parenthesized_p,
15758                                          /*member_p=*/false);
15759       parser->default_arg_ok_p = saved_default_arg_ok_p;
15760       /* After the declarator, allow more attributes.  */
15761       decl_specifiers.attributes
15762         = chainon (decl_specifiers.attributes,
15763                    cp_parser_attributes_opt (parser));
15764     }
15765
15766   /* If the next token is an ellipsis, and we have not seen a
15767      declarator name, and the type of the declarator contains parameter
15768      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15769      a parameter pack expansion expression. Otherwise, leave the
15770      ellipsis for a C-style variadic function. */
15771   token = cp_lexer_peek_token (parser->lexer);
15772   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15773     {
15774       tree type = decl_specifiers.type;
15775
15776       if (type && DECL_P (type))
15777         type = TREE_TYPE (type);
15778
15779       if (type
15780           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15781           && declarator_can_be_parameter_pack (declarator)
15782           && (!declarator || !declarator->parameter_pack_p)
15783           && uses_parameter_packs (type))
15784         {
15785           /* Consume the `...'. */
15786           cp_lexer_consume_token (parser->lexer);
15787           maybe_warn_variadic_templates ();
15788           
15789           /* Build a pack expansion type */
15790           if (declarator)
15791             declarator->parameter_pack_p = true;
15792           else
15793             decl_specifiers.type = make_pack_expansion (type);
15794         }
15795     }
15796
15797   /* The restriction on defining new types applies only to the type
15798      of the parameter, not to the default argument.  */
15799   parser->type_definition_forbidden_message = saved_message;
15800
15801   /* If the next token is `=', then process a default argument.  */
15802   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15803     {
15804       /* Consume the `='.  */
15805       cp_lexer_consume_token (parser->lexer);
15806
15807       /* If we are defining a class, then the tokens that make up the
15808          default argument must be saved and processed later.  */
15809       if (!template_parm_p && at_class_scope_p ()
15810           && TYPE_BEING_DEFINED (current_class_type)
15811           && !LAMBDA_TYPE_P (current_class_type))
15812         {
15813           unsigned depth = 0;
15814           int maybe_template_id = 0;
15815           cp_token *first_token;
15816           cp_token *token;
15817
15818           /* Add tokens until we have processed the entire default
15819              argument.  We add the range [first_token, token).  */
15820           first_token = cp_lexer_peek_token (parser->lexer);
15821           while (true)
15822             {
15823               bool done = false;
15824
15825               /* Peek at the next token.  */
15826               token = cp_lexer_peek_token (parser->lexer);
15827               /* What we do depends on what token we have.  */
15828               switch (token->type)
15829                 {
15830                   /* In valid code, a default argument must be
15831                      immediately followed by a `,' `)', or `...'.  */
15832                 case CPP_COMMA:
15833                   if (depth == 0 && maybe_template_id)
15834                     {
15835                       /* If we've seen a '<', we might be in a
15836                          template-argument-list.  Until Core issue 325 is
15837                          resolved, we don't know how this situation ought
15838                          to be handled, so try to DTRT.  We check whether
15839                          what comes after the comma is a valid parameter
15840                          declaration list.  If it is, then the comma ends
15841                          the default argument; otherwise the default
15842                          argument continues.  */
15843                       bool error = false;
15844                       tree t;
15845
15846                       /* Set ITALP so cp_parser_parameter_declaration_list
15847                          doesn't decide to commit to this parse.  */
15848                       bool saved_italp = parser->in_template_argument_list_p;
15849                       parser->in_template_argument_list_p = true;
15850
15851                       cp_parser_parse_tentatively (parser);
15852                       cp_lexer_consume_token (parser->lexer);
15853                       begin_scope (sk_function_parms, NULL_TREE);
15854                       cp_parser_parameter_declaration_list (parser, &error);
15855                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15856                         pop_binding (DECL_NAME (t), t);
15857                       leave_scope ();
15858                       if (!cp_parser_error_occurred (parser) && !error)
15859                         done = true;
15860                       cp_parser_abort_tentative_parse (parser);
15861
15862                       parser->in_template_argument_list_p = saved_italp;
15863                       break;
15864                     }
15865                 case CPP_CLOSE_PAREN:
15866                 case CPP_ELLIPSIS:
15867                   /* If we run into a non-nested `;', `}', or `]',
15868                      then the code is invalid -- but the default
15869                      argument is certainly over.  */
15870                 case CPP_SEMICOLON:
15871                 case CPP_CLOSE_BRACE:
15872                 case CPP_CLOSE_SQUARE:
15873                   if (depth == 0)
15874                     done = true;
15875                   /* Update DEPTH, if necessary.  */
15876                   else if (token->type == CPP_CLOSE_PAREN
15877                            || token->type == CPP_CLOSE_BRACE
15878                            || token->type == CPP_CLOSE_SQUARE)
15879                     --depth;
15880                   break;
15881
15882                 case CPP_OPEN_PAREN:
15883                 case CPP_OPEN_SQUARE:
15884                 case CPP_OPEN_BRACE:
15885                   ++depth;
15886                   break;
15887
15888                 case CPP_LESS:
15889                   if (depth == 0)
15890                     /* This might be the comparison operator, or it might
15891                        start a template argument list.  */
15892                     ++maybe_template_id;
15893                   break;
15894
15895                 case CPP_RSHIFT:
15896                   if (cxx_dialect == cxx98)
15897                     break;
15898                   /* Fall through for C++0x, which treats the `>>'
15899                      operator like two `>' tokens in certain
15900                      cases.  */
15901
15902                 case CPP_GREATER:
15903                   if (depth == 0)
15904                     {
15905                       /* This might be an operator, or it might close a
15906                          template argument list.  But if a previous '<'
15907                          started a template argument list, this will have
15908                          closed it, so we can't be in one anymore.  */
15909                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15910                       if (maybe_template_id < 0)
15911                         maybe_template_id = 0;
15912                     }
15913                   break;
15914
15915                   /* If we run out of tokens, issue an error message.  */
15916                 case CPP_EOF:
15917                 case CPP_PRAGMA_EOL:
15918                   error_at (token->location, "file ends in default argument");
15919                   done = true;
15920                   break;
15921
15922                 case CPP_NAME:
15923                 case CPP_SCOPE:
15924                   /* In these cases, we should look for template-ids.
15925                      For example, if the default argument is
15926                      `X<int, double>()', we need to do name lookup to
15927                      figure out whether or not `X' is a template; if
15928                      so, the `,' does not end the default argument.
15929
15930                      That is not yet done.  */
15931                   break;
15932
15933                 default:
15934                   break;
15935                 }
15936
15937               /* If we've reached the end, stop.  */
15938               if (done)
15939                 break;
15940
15941               /* Add the token to the token block.  */
15942               token = cp_lexer_consume_token (parser->lexer);
15943             }
15944
15945           /* Create a DEFAULT_ARG to represent the unparsed default
15946              argument.  */
15947           default_argument = make_node (DEFAULT_ARG);
15948           DEFARG_TOKENS (default_argument)
15949             = cp_token_cache_new (first_token, token);
15950           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15951         }
15952       /* Outside of a class definition, we can just parse the
15953          assignment-expression.  */
15954       else
15955         {
15956           token = cp_lexer_peek_token (parser->lexer);
15957           default_argument 
15958             = cp_parser_default_argument (parser, template_parm_p);
15959         }
15960
15961       if (!parser->default_arg_ok_p)
15962         {
15963           if (flag_permissive)
15964             warning (0, "deprecated use of default argument for parameter of non-function");
15965           else
15966             {
15967               error_at (token->location,
15968                         "default arguments are only "
15969                         "permitted for function parameters");
15970               default_argument = NULL_TREE;
15971             }
15972         }
15973       else if ((declarator && declarator->parameter_pack_p)
15974                || (decl_specifiers.type
15975                    && PACK_EXPANSION_P (decl_specifiers.type)))
15976         {
15977           /* Find the name of the parameter pack.  */     
15978           cp_declarator *id_declarator = declarator;
15979           while (id_declarator && id_declarator->kind != cdk_id)
15980             id_declarator = id_declarator->declarator;
15981           
15982           if (id_declarator && id_declarator->kind == cdk_id)
15983             error_at (declarator_token_start->location,
15984                       template_parm_p 
15985                       ? "template parameter pack %qD"
15986                       " cannot have a default argument"
15987                       : "parameter pack %qD cannot have a default argument",
15988                       id_declarator->u.id.unqualified_name);
15989           else
15990             error_at (declarator_token_start->location,
15991                       template_parm_p 
15992                       ? "template parameter pack cannot have a default argument"
15993                       : "parameter pack cannot have a default argument");
15994           
15995           default_argument = NULL_TREE;
15996         }
15997     }
15998   else
15999     default_argument = NULL_TREE;
16000
16001   return make_parameter_declarator (&decl_specifiers,
16002                                     declarator,
16003                                     default_argument);
16004 }
16005
16006 /* Parse a default argument and return it.
16007
16008    TEMPLATE_PARM_P is true if this is a default argument for a
16009    non-type template parameter.  */
16010 static tree
16011 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16012 {
16013   tree default_argument = NULL_TREE;
16014   bool saved_greater_than_is_operator_p;
16015   bool saved_local_variables_forbidden_p;
16016
16017   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16018      set correctly.  */
16019   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16020   parser->greater_than_is_operator_p = !template_parm_p;
16021   /* Local variable names (and the `this' keyword) may not
16022      appear in a default argument.  */
16023   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16024   parser->local_variables_forbidden_p = true;
16025   /* Parse the assignment-expression.  */
16026   if (template_parm_p)
16027     push_deferring_access_checks (dk_no_deferred);
16028   default_argument
16029     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16030   if (template_parm_p)
16031     pop_deferring_access_checks ();
16032   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16033   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16034
16035   return default_argument;
16036 }
16037
16038 /* Parse a function-body.
16039
16040    function-body:
16041      compound_statement  */
16042
16043 static void
16044 cp_parser_function_body (cp_parser *parser)
16045 {
16046   cp_parser_compound_statement (parser, NULL, false);
16047 }
16048
16049 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16050    true if a ctor-initializer was present.  */
16051
16052 static bool
16053 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16054 {
16055   tree body;
16056   bool ctor_initializer_p;
16057
16058   /* Begin the function body.  */
16059   body = begin_function_body ();
16060   /* Parse the optional ctor-initializer.  */
16061   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16062   /* Parse the function-body.  */
16063   cp_parser_function_body (parser);
16064   /* Finish the function body.  */
16065   finish_function_body (body);
16066
16067   return ctor_initializer_p;
16068 }
16069
16070 /* Parse an initializer.
16071
16072    initializer:
16073      = initializer-clause
16074      ( expression-list )
16075
16076    Returns an expression representing the initializer.  If no
16077    initializer is present, NULL_TREE is returned.
16078
16079    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16080    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16081    set to TRUE if there is no initializer present.  If there is an
16082    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16083    is set to true; otherwise it is set to false.  */
16084
16085 static tree
16086 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16087                        bool* non_constant_p)
16088 {
16089   cp_token *token;
16090   tree init;
16091
16092   /* Peek at the next token.  */
16093   token = cp_lexer_peek_token (parser->lexer);
16094
16095   /* Let our caller know whether or not this initializer was
16096      parenthesized.  */
16097   *is_direct_init = (token->type != CPP_EQ);
16098   /* Assume that the initializer is constant.  */
16099   *non_constant_p = false;
16100
16101   if (token->type == CPP_EQ)
16102     {
16103       /* Consume the `='.  */
16104       cp_lexer_consume_token (parser->lexer);
16105       /* Parse the initializer-clause.  */
16106       init = cp_parser_initializer_clause (parser, non_constant_p);
16107     }
16108   else if (token->type == CPP_OPEN_PAREN)
16109     {
16110       VEC(tree,gc) *vec;
16111       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16112                                                      /*cast_p=*/false,
16113                                                      /*allow_expansion_p=*/true,
16114                                                      non_constant_p);
16115       if (vec == NULL)
16116         return error_mark_node;
16117       init = build_tree_list_vec (vec);
16118       release_tree_vector (vec);
16119     }
16120   else if (token->type == CPP_OPEN_BRACE)
16121     {
16122       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16123       init = cp_parser_braced_list (parser, non_constant_p);
16124       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16125     }
16126   else
16127     {
16128       /* Anything else is an error.  */
16129       cp_parser_error (parser, "expected initializer");
16130       init = error_mark_node;
16131     }
16132
16133   return init;
16134 }
16135
16136 /* Parse an initializer-clause.
16137
16138    initializer-clause:
16139      assignment-expression
16140      braced-init-list
16141
16142    Returns an expression representing the initializer.
16143
16144    If the `assignment-expression' production is used the value
16145    returned is simply a representation for the expression.
16146
16147    Otherwise, calls cp_parser_braced_list.  */
16148
16149 static tree
16150 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16151 {
16152   tree initializer;
16153
16154   /* Assume the expression is constant.  */
16155   *non_constant_p = false;
16156
16157   /* If it is not a `{', then we are looking at an
16158      assignment-expression.  */
16159   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16160     {
16161       initializer
16162         = cp_parser_constant_expression (parser,
16163                                         /*allow_non_constant_p=*/true,
16164                                         non_constant_p);
16165       if (!*non_constant_p)
16166         initializer = fold_non_dependent_expr (initializer);
16167     }
16168   else
16169     initializer = cp_parser_braced_list (parser, non_constant_p);
16170
16171   return initializer;
16172 }
16173
16174 /* Parse a brace-enclosed initializer list.
16175
16176    braced-init-list:
16177      { initializer-list , [opt] }
16178      { }
16179
16180    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16181    the elements of the initializer-list (or NULL, if the last
16182    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16183    NULL_TREE.  There is no way to detect whether or not the optional
16184    trailing `,' was provided.  NON_CONSTANT_P is as for
16185    cp_parser_initializer.  */     
16186
16187 static tree
16188 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16189 {
16190   tree initializer;
16191
16192   /* Consume the `{' token.  */
16193   cp_lexer_consume_token (parser->lexer);
16194   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16195   initializer = make_node (CONSTRUCTOR);
16196   /* If it's not a `}', then there is a non-trivial initializer.  */
16197   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16198     {
16199       /* Parse the initializer list.  */
16200       CONSTRUCTOR_ELTS (initializer)
16201         = cp_parser_initializer_list (parser, non_constant_p);
16202       /* A trailing `,' token is allowed.  */
16203       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16204         cp_lexer_consume_token (parser->lexer);
16205     }
16206   /* Now, there should be a trailing `}'.  */
16207   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16208   TREE_TYPE (initializer) = init_list_type_node;
16209   return initializer;
16210 }
16211
16212 /* Parse an initializer-list.
16213
16214    initializer-list:
16215      initializer-clause ... [opt]
16216      initializer-list , initializer-clause ... [opt]
16217
16218    GNU Extension:
16219
16220    initializer-list:
16221      identifier : initializer-clause
16222      initializer-list, identifier : initializer-clause
16223
16224    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16225    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16226    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16227    as for cp_parser_initializer.  */
16228
16229 static VEC(constructor_elt,gc) *
16230 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16231 {
16232   VEC(constructor_elt,gc) *v = NULL;
16233
16234   /* Assume all of the expressions are constant.  */
16235   *non_constant_p = false;
16236
16237   /* Parse the rest of the list.  */
16238   while (true)
16239     {
16240       cp_token *token;
16241       tree identifier;
16242       tree initializer;
16243       bool clause_non_constant_p;
16244
16245       /* If the next token is an identifier and the following one is a
16246          colon, we are looking at the GNU designated-initializer
16247          syntax.  */
16248       if (cp_parser_allow_gnu_extensions_p (parser)
16249           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16250           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16251         {
16252           /* Warn the user that they are using an extension.  */
16253           pedwarn (input_location, OPT_pedantic, 
16254                    "ISO C++ does not allow designated initializers");
16255           /* Consume the identifier.  */
16256           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16257           /* Consume the `:'.  */
16258           cp_lexer_consume_token (parser->lexer);
16259         }
16260       else
16261         identifier = NULL_TREE;
16262
16263       /* Parse the initializer.  */
16264       initializer = cp_parser_initializer_clause (parser,
16265                                                   &clause_non_constant_p);
16266       /* If any clause is non-constant, so is the entire initializer.  */
16267       if (clause_non_constant_p)
16268         *non_constant_p = true;
16269
16270       /* If we have an ellipsis, this is an initializer pack
16271          expansion.  */
16272       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16273         {
16274           /* Consume the `...'.  */
16275           cp_lexer_consume_token (parser->lexer);
16276
16277           /* Turn the initializer into an initializer expansion.  */
16278           initializer = make_pack_expansion (initializer);
16279         }
16280
16281       /* Add it to the vector.  */
16282       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16283
16284       /* If the next token is not a comma, we have reached the end of
16285          the list.  */
16286       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16287         break;
16288
16289       /* Peek at the next token.  */
16290       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16291       /* If the next token is a `}', then we're still done.  An
16292          initializer-clause can have a trailing `,' after the
16293          initializer-list and before the closing `}'.  */
16294       if (token->type == CPP_CLOSE_BRACE)
16295         break;
16296
16297       /* Consume the `,' token.  */
16298       cp_lexer_consume_token (parser->lexer);
16299     }
16300
16301   return v;
16302 }
16303
16304 /* Classes [gram.class] */
16305
16306 /* Parse a class-name.
16307
16308    class-name:
16309      identifier
16310      template-id
16311
16312    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16313    to indicate that names looked up in dependent types should be
16314    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16315    keyword has been used to indicate that the name that appears next
16316    is a template.  TAG_TYPE indicates the explicit tag given before
16317    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16318    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16319    is the class being defined in a class-head.
16320
16321    Returns the TYPE_DECL representing the class.  */
16322
16323 static tree
16324 cp_parser_class_name (cp_parser *parser,
16325                       bool typename_keyword_p,
16326                       bool template_keyword_p,
16327                       enum tag_types tag_type,
16328                       bool check_dependency_p,
16329                       bool class_head_p,
16330                       bool is_declaration)
16331 {
16332   tree decl;
16333   tree scope;
16334   bool typename_p;
16335   cp_token *token;
16336   tree identifier = NULL_TREE;
16337
16338   /* All class-names start with an identifier.  */
16339   token = cp_lexer_peek_token (parser->lexer);
16340   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16341     {
16342       cp_parser_error (parser, "expected class-name");
16343       return error_mark_node;
16344     }
16345
16346   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16347      to a template-id, so we save it here.  */
16348   scope = parser->scope;
16349   if (scope == error_mark_node)
16350     return error_mark_node;
16351
16352   /* Any name names a type if we're following the `typename' keyword
16353      in a qualified name where the enclosing scope is type-dependent.  */
16354   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16355                 && dependent_type_p (scope));
16356   /* Handle the common case (an identifier, but not a template-id)
16357      efficiently.  */
16358   if (token->type == CPP_NAME
16359       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16360     {
16361       cp_token *identifier_token;
16362       bool ambiguous_p;
16363
16364       /* Look for the identifier.  */
16365       identifier_token = cp_lexer_peek_token (parser->lexer);
16366       ambiguous_p = identifier_token->ambiguous_p;
16367       identifier = cp_parser_identifier (parser);
16368       /* If the next token isn't an identifier, we are certainly not
16369          looking at a class-name.  */
16370       if (identifier == error_mark_node)
16371         decl = error_mark_node;
16372       /* If we know this is a type-name, there's no need to look it
16373          up.  */
16374       else if (typename_p)
16375         decl = identifier;
16376       else
16377         {
16378           tree ambiguous_decls;
16379           /* If we already know that this lookup is ambiguous, then
16380              we've already issued an error message; there's no reason
16381              to check again.  */
16382           if (ambiguous_p)
16383             {
16384               cp_parser_simulate_error (parser);
16385               return error_mark_node;
16386             }
16387           /* If the next token is a `::', then the name must be a type
16388              name.
16389
16390              [basic.lookup.qual]
16391
16392              During the lookup for a name preceding the :: scope
16393              resolution operator, object, function, and enumerator
16394              names are ignored.  */
16395           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16396             tag_type = typename_type;
16397           /* Look up the name.  */
16398           decl = cp_parser_lookup_name (parser, identifier,
16399                                         tag_type,
16400                                         /*is_template=*/false,
16401                                         /*is_namespace=*/false,
16402                                         check_dependency_p,
16403                                         &ambiguous_decls,
16404                                         identifier_token->location);
16405           if (ambiguous_decls)
16406             {
16407               if (cp_parser_parsing_tentatively (parser))
16408                 cp_parser_simulate_error (parser);
16409               return error_mark_node;
16410             }
16411         }
16412     }
16413   else
16414     {
16415       /* Try a template-id.  */
16416       decl = cp_parser_template_id (parser, template_keyword_p,
16417                                     check_dependency_p,
16418                                     is_declaration);
16419       if (decl == error_mark_node)
16420         return error_mark_node;
16421     }
16422
16423   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16424
16425   /* If this is a typename, create a TYPENAME_TYPE.  */
16426   if (typename_p && decl != error_mark_node)
16427     {
16428       decl = make_typename_type (scope, decl, typename_type,
16429                                  /*complain=*/tf_error);
16430       if (decl != error_mark_node)
16431         decl = TYPE_NAME (decl);
16432     }
16433
16434   /* Check to see that it is really the name of a class.  */
16435   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16436       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16437       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16438     /* Situations like this:
16439
16440          template <typename T> struct A {
16441            typename T::template X<int>::I i;
16442          };
16443
16444        are problematic.  Is `T::template X<int>' a class-name?  The
16445        standard does not seem to be definitive, but there is no other
16446        valid interpretation of the following `::'.  Therefore, those
16447        names are considered class-names.  */
16448     {
16449       decl = make_typename_type (scope, decl, tag_type, tf_error);
16450       if (decl != error_mark_node)
16451         decl = TYPE_NAME (decl);
16452     }
16453   else if (TREE_CODE (decl) != TYPE_DECL
16454            || TREE_TYPE (decl) == error_mark_node
16455            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16456     decl = error_mark_node;
16457
16458   if (decl == error_mark_node)
16459     cp_parser_error (parser, "expected class-name");
16460   else if (identifier && !parser->scope)
16461     maybe_note_name_used_in_class (identifier, decl);
16462
16463   return decl;
16464 }
16465
16466 /* Parse a class-specifier.
16467
16468    class-specifier:
16469      class-head { member-specification [opt] }
16470
16471    Returns the TREE_TYPE representing the class.  */
16472
16473 static tree
16474 cp_parser_class_specifier (cp_parser* parser)
16475 {
16476   tree type;
16477   tree attributes = NULL_TREE;
16478   bool nested_name_specifier_p;
16479   unsigned saved_num_template_parameter_lists;
16480   bool saved_in_function_body;
16481   bool saved_in_unbraced_linkage_specification_p;
16482   tree old_scope = NULL_TREE;
16483   tree scope = NULL_TREE;
16484   tree bases;
16485
16486   push_deferring_access_checks (dk_no_deferred);
16487
16488   /* Parse the class-head.  */
16489   type = cp_parser_class_head (parser,
16490                                &nested_name_specifier_p,
16491                                &attributes,
16492                                &bases);
16493   /* If the class-head was a semantic disaster, skip the entire body
16494      of the class.  */
16495   if (!type)
16496     {
16497       cp_parser_skip_to_end_of_block_or_statement (parser);
16498       pop_deferring_access_checks ();
16499       return error_mark_node;
16500     }
16501
16502   /* Look for the `{'.  */
16503   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16504     {
16505       pop_deferring_access_checks ();
16506       return error_mark_node;
16507     }
16508
16509   /* Process the base classes. If they're invalid, skip the 
16510      entire class body.  */
16511   if (!xref_basetypes (type, bases))
16512     {
16513       /* Consuming the closing brace yields better error messages
16514          later on.  */
16515       if (cp_parser_skip_to_closing_brace (parser))
16516         cp_lexer_consume_token (parser->lexer);
16517       pop_deferring_access_checks ();
16518       return error_mark_node;
16519     }
16520
16521   /* Issue an error message if type-definitions are forbidden here.  */
16522   cp_parser_check_type_definition (parser);
16523   /* Remember that we are defining one more class.  */
16524   ++parser->num_classes_being_defined;
16525   /* Inside the class, surrounding template-parameter-lists do not
16526      apply.  */
16527   saved_num_template_parameter_lists
16528     = parser->num_template_parameter_lists;
16529   parser->num_template_parameter_lists = 0;
16530   /* We are not in a function body.  */
16531   saved_in_function_body = parser->in_function_body;
16532   parser->in_function_body = false;
16533   /* We are not immediately inside an extern "lang" block.  */
16534   saved_in_unbraced_linkage_specification_p
16535     = parser->in_unbraced_linkage_specification_p;
16536   parser->in_unbraced_linkage_specification_p = false;
16537
16538   /* Start the class.  */
16539   if (nested_name_specifier_p)
16540     {
16541       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16542       old_scope = push_inner_scope (scope);
16543     }
16544   type = begin_class_definition (type, attributes);
16545
16546   if (type == error_mark_node)
16547     /* If the type is erroneous, skip the entire body of the class.  */
16548     cp_parser_skip_to_closing_brace (parser);
16549   else
16550     /* Parse the member-specification.  */
16551     cp_parser_member_specification_opt (parser);
16552
16553   /* Look for the trailing `}'.  */
16554   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16555   /* Look for trailing attributes to apply to this class.  */
16556   if (cp_parser_allow_gnu_extensions_p (parser))
16557     attributes = cp_parser_attributes_opt (parser);
16558   if (type != error_mark_node)
16559     type = finish_struct (type, attributes);
16560   if (nested_name_specifier_p)
16561     pop_inner_scope (old_scope, scope);
16562   /* If this class is not itself within the scope of another class,
16563      then we need to parse the bodies of all of the queued function
16564      definitions.  Note that the queued functions defined in a class
16565      are not always processed immediately following the
16566      class-specifier for that class.  Consider:
16567
16568        struct A {
16569          struct B { void f() { sizeof (A); } };
16570        };
16571
16572      If `f' were processed before the processing of `A' were
16573      completed, there would be no way to compute the size of `A'.
16574      Note that the nesting we are interested in here is lexical --
16575      not the semantic nesting given by TYPE_CONTEXT.  In particular,
16576      for:
16577
16578        struct A { struct B; };
16579        struct A::B { void f() { } };
16580
16581      there is no need to delay the parsing of `A::B::f'.  */
16582   if (--parser->num_classes_being_defined == 0)
16583     {
16584       tree fn;
16585       tree class_type = NULL_TREE;
16586       tree pushed_scope = NULL_TREE;
16587       unsigned ix;
16588       cp_default_arg_entry *e;
16589
16590       /* In a first pass, parse default arguments to the functions.
16591          Then, in a second pass, parse the bodies of the functions.
16592          This two-phased approach handles cases like:
16593
16594             struct S {
16595               void f() { g(); }
16596               void g(int i = 3);
16597             };
16598
16599          */
16600       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16601                         ix, e)
16602         {
16603           fn = e->decl;
16604           /* If there are default arguments that have not yet been processed,
16605              take care of them now.  */
16606           if (class_type != e->class_type)
16607             {
16608               if (pushed_scope)
16609                 pop_scope (pushed_scope);
16610               class_type = e->class_type;
16611               pushed_scope = push_scope (class_type);
16612             }
16613           /* Make sure that any template parameters are in scope.  */
16614           maybe_begin_member_template_processing (fn);
16615           /* Parse the default argument expressions.  */
16616           cp_parser_late_parsing_default_args (parser, fn);
16617           /* Remove any template parameters from the symbol table.  */
16618           maybe_end_member_template_processing ();
16619         }
16620       if (pushed_scope)
16621         pop_scope (pushed_scope);
16622       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16623       /* Now parse the body of the functions.  */
16624       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16625         cp_parser_late_parsing_for_member (parser, fn);
16626       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16627     }
16628
16629   /* Put back any saved access checks.  */
16630   pop_deferring_access_checks ();
16631
16632   /* Restore saved state.  */
16633   parser->in_function_body = saved_in_function_body;
16634   parser->num_template_parameter_lists
16635     = saved_num_template_parameter_lists;
16636   parser->in_unbraced_linkage_specification_p
16637     = saved_in_unbraced_linkage_specification_p;
16638
16639   return type;
16640 }
16641
16642 /* Parse a class-head.
16643
16644    class-head:
16645      class-key identifier [opt] base-clause [opt]
16646      class-key nested-name-specifier identifier base-clause [opt]
16647      class-key nested-name-specifier [opt] template-id
16648        base-clause [opt]
16649
16650    GNU Extensions:
16651      class-key attributes identifier [opt] base-clause [opt]
16652      class-key attributes nested-name-specifier identifier base-clause [opt]
16653      class-key attributes nested-name-specifier [opt] template-id
16654        base-clause [opt]
16655
16656    Upon return BASES is initialized to the list of base classes (or
16657    NULL, if there are none) in the same form returned by
16658    cp_parser_base_clause.
16659
16660    Returns the TYPE of the indicated class.  Sets
16661    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16662    involving a nested-name-specifier was used, and FALSE otherwise.
16663
16664    Returns error_mark_node if this is not a class-head.
16665
16666    Returns NULL_TREE if the class-head is syntactically valid, but
16667    semantically invalid in a way that means we should skip the entire
16668    body of the class.  */
16669
16670 static tree
16671 cp_parser_class_head (cp_parser* parser,
16672                       bool* nested_name_specifier_p,
16673                       tree *attributes_p,
16674                       tree *bases)
16675 {
16676   tree nested_name_specifier;
16677   enum tag_types class_key;
16678   tree id = NULL_TREE;
16679   tree type = NULL_TREE;
16680   tree attributes;
16681   bool template_id_p = false;
16682   bool qualified_p = false;
16683   bool invalid_nested_name_p = false;
16684   bool invalid_explicit_specialization_p = false;
16685   tree pushed_scope = NULL_TREE;
16686   unsigned num_templates;
16687   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16688   /* Assume no nested-name-specifier will be present.  */
16689   *nested_name_specifier_p = false;
16690   /* Assume no template parameter lists will be used in defining the
16691      type.  */
16692   num_templates = 0;
16693
16694   *bases = NULL_TREE;
16695
16696   /* Look for the class-key.  */
16697   class_key = cp_parser_class_key (parser);
16698   if (class_key == none_type)
16699     return error_mark_node;
16700
16701   /* Parse the attributes.  */
16702   attributes = cp_parser_attributes_opt (parser);
16703
16704   /* If the next token is `::', that is invalid -- but sometimes
16705      people do try to write:
16706
16707        struct ::S {};
16708
16709      Handle this gracefully by accepting the extra qualifier, and then
16710      issuing an error about it later if this really is a
16711      class-head.  If it turns out just to be an elaborated type
16712      specifier, remain silent.  */
16713   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16714     qualified_p = true;
16715
16716   push_deferring_access_checks (dk_no_check);
16717
16718   /* Determine the name of the class.  Begin by looking for an
16719      optional nested-name-specifier.  */
16720   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16721   nested_name_specifier
16722     = cp_parser_nested_name_specifier_opt (parser,
16723                                            /*typename_keyword_p=*/false,
16724                                            /*check_dependency_p=*/false,
16725                                            /*type_p=*/false,
16726                                            /*is_declaration=*/false);
16727   /* If there was a nested-name-specifier, then there *must* be an
16728      identifier.  */
16729   if (nested_name_specifier)
16730     {
16731       type_start_token = cp_lexer_peek_token (parser->lexer);
16732       /* Although the grammar says `identifier', it really means
16733          `class-name' or `template-name'.  You are only allowed to
16734          define a class that has already been declared with this
16735          syntax.
16736
16737          The proposed resolution for Core Issue 180 says that wherever
16738          you see `class T::X' you should treat `X' as a type-name.
16739
16740          It is OK to define an inaccessible class; for example:
16741
16742            class A { class B; };
16743            class A::B {};
16744
16745          We do not know if we will see a class-name, or a
16746          template-name.  We look for a class-name first, in case the
16747          class-name is a template-id; if we looked for the
16748          template-name first we would stop after the template-name.  */
16749       cp_parser_parse_tentatively (parser);
16750       type = cp_parser_class_name (parser,
16751                                    /*typename_keyword_p=*/false,
16752                                    /*template_keyword_p=*/false,
16753                                    class_type,
16754                                    /*check_dependency_p=*/false,
16755                                    /*class_head_p=*/true,
16756                                    /*is_declaration=*/false);
16757       /* If that didn't work, ignore the nested-name-specifier.  */
16758       if (!cp_parser_parse_definitely (parser))
16759         {
16760           invalid_nested_name_p = true;
16761           type_start_token = cp_lexer_peek_token (parser->lexer);
16762           id = cp_parser_identifier (parser);
16763           if (id == error_mark_node)
16764             id = NULL_TREE;
16765         }
16766       /* If we could not find a corresponding TYPE, treat this
16767          declaration like an unqualified declaration.  */
16768       if (type == error_mark_node)
16769         nested_name_specifier = NULL_TREE;
16770       /* Otherwise, count the number of templates used in TYPE and its
16771          containing scopes.  */
16772       else
16773         {
16774           tree scope;
16775
16776           for (scope = TREE_TYPE (type);
16777                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16778                scope = (TYPE_P (scope)
16779                         ? TYPE_CONTEXT (scope)
16780                         : DECL_CONTEXT (scope)))
16781             if (TYPE_P (scope)
16782                 && CLASS_TYPE_P (scope)
16783                 && CLASSTYPE_TEMPLATE_INFO (scope)
16784                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16785                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16786               ++num_templates;
16787         }
16788     }
16789   /* Otherwise, the identifier is optional.  */
16790   else
16791     {
16792       /* We don't know whether what comes next is a template-id,
16793          an identifier, or nothing at all.  */
16794       cp_parser_parse_tentatively (parser);
16795       /* Check for a template-id.  */
16796       type_start_token = cp_lexer_peek_token (parser->lexer);
16797       id = cp_parser_template_id (parser,
16798                                   /*template_keyword_p=*/false,
16799                                   /*check_dependency_p=*/true,
16800                                   /*is_declaration=*/true);
16801       /* If that didn't work, it could still be an identifier.  */
16802       if (!cp_parser_parse_definitely (parser))
16803         {
16804           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16805             {
16806               type_start_token = cp_lexer_peek_token (parser->lexer);
16807               id = cp_parser_identifier (parser);
16808             }
16809           else
16810             id = NULL_TREE;
16811         }
16812       else
16813         {
16814           template_id_p = true;
16815           ++num_templates;
16816         }
16817     }
16818
16819   pop_deferring_access_checks ();
16820
16821   if (id)
16822     cp_parser_check_for_invalid_template_id (parser, id,
16823                                              type_start_token->location);
16824
16825   /* If it's not a `:' or a `{' then we can't really be looking at a
16826      class-head, since a class-head only appears as part of a
16827      class-specifier.  We have to detect this situation before calling
16828      xref_tag, since that has irreversible side-effects.  */
16829   if (!cp_parser_next_token_starts_class_definition_p (parser))
16830     {
16831       cp_parser_error (parser, "expected %<{%> or %<:%>");
16832       return error_mark_node;
16833     }
16834
16835   /* At this point, we're going ahead with the class-specifier, even
16836      if some other problem occurs.  */
16837   cp_parser_commit_to_tentative_parse (parser);
16838   /* Issue the error about the overly-qualified name now.  */
16839   if (qualified_p)
16840     {
16841       cp_parser_error (parser,
16842                        "global qualification of class name is invalid");
16843       return error_mark_node;
16844     }
16845   else if (invalid_nested_name_p)
16846     {
16847       cp_parser_error (parser,
16848                        "qualified name does not name a class");
16849       return error_mark_node;
16850     }
16851   else if (nested_name_specifier)
16852     {
16853       tree scope;
16854
16855       /* Reject typedef-names in class heads.  */
16856       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16857         {
16858           error_at (type_start_token->location,
16859                     "invalid class name in declaration of %qD",
16860                     type);
16861           type = NULL_TREE;
16862           goto done;
16863         }
16864
16865       /* Figure out in what scope the declaration is being placed.  */
16866       scope = current_scope ();
16867       /* If that scope does not contain the scope in which the
16868          class was originally declared, the program is invalid.  */
16869       if (scope && !is_ancestor (scope, nested_name_specifier))
16870         {
16871           if (at_namespace_scope_p ())
16872             error_at (type_start_token->location,
16873                       "declaration of %qD in namespace %qD which does not "
16874                       "enclose %qD",
16875                       type, scope, nested_name_specifier);
16876           else
16877             error_at (type_start_token->location,
16878                       "declaration of %qD in %qD which does not enclose %qD",
16879                       type, scope, nested_name_specifier);
16880           type = NULL_TREE;
16881           goto done;
16882         }
16883       /* [dcl.meaning]
16884
16885          A declarator-id shall not be qualified except for the
16886          definition of a ... nested class outside of its class
16887          ... [or] the definition or explicit instantiation of a
16888          class member of a namespace outside of its namespace.  */
16889       if (scope == nested_name_specifier)
16890         {
16891           permerror (nested_name_specifier_token_start->location,
16892                      "extra qualification not allowed");
16893           nested_name_specifier = NULL_TREE;
16894           num_templates = 0;
16895         }
16896     }
16897   /* An explicit-specialization must be preceded by "template <>".  If
16898      it is not, try to recover gracefully.  */
16899   if (at_namespace_scope_p ()
16900       && parser->num_template_parameter_lists == 0
16901       && template_id_p)
16902     {
16903       error_at (type_start_token->location,
16904                 "an explicit specialization must be preceded by %<template <>%>");
16905       invalid_explicit_specialization_p = true;
16906       /* Take the same action that would have been taken by
16907          cp_parser_explicit_specialization.  */
16908       ++parser->num_template_parameter_lists;
16909       begin_specialization ();
16910     }
16911   /* There must be no "return" statements between this point and the
16912      end of this function; set "type "to the correct return value and
16913      use "goto done;" to return.  */
16914   /* Make sure that the right number of template parameters were
16915      present.  */
16916   if (!cp_parser_check_template_parameters (parser, num_templates,
16917                                             type_start_token->location,
16918                                             /*declarator=*/NULL))
16919     {
16920       /* If something went wrong, there is no point in even trying to
16921          process the class-definition.  */
16922       type = NULL_TREE;
16923       goto done;
16924     }
16925
16926   /* Look up the type.  */
16927   if (template_id_p)
16928     {
16929       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16930           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16931               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16932         {
16933           error_at (type_start_token->location,
16934                     "function template %qD redeclared as a class template", id);
16935           type = error_mark_node;
16936         }
16937       else
16938         {
16939           type = TREE_TYPE (id);
16940           type = maybe_process_partial_specialization (type);
16941         }
16942       if (nested_name_specifier)
16943         pushed_scope = push_scope (nested_name_specifier);
16944     }
16945   else if (nested_name_specifier)
16946     {
16947       tree class_type;
16948
16949       /* Given:
16950
16951             template <typename T> struct S { struct T };
16952             template <typename T> struct S<T>::T { };
16953
16954          we will get a TYPENAME_TYPE when processing the definition of
16955          `S::T'.  We need to resolve it to the actual type before we
16956          try to define it.  */
16957       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16958         {
16959           class_type = resolve_typename_type (TREE_TYPE (type),
16960                                               /*only_current_p=*/false);
16961           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16962             type = TYPE_NAME (class_type);
16963           else
16964             {
16965               cp_parser_error (parser, "could not resolve typename type");
16966               type = error_mark_node;
16967             }
16968         }
16969
16970       if (maybe_process_partial_specialization (TREE_TYPE (type))
16971           == error_mark_node)
16972         {
16973           type = NULL_TREE;
16974           goto done;
16975         }
16976
16977       class_type = current_class_type;
16978       /* Enter the scope indicated by the nested-name-specifier.  */
16979       pushed_scope = push_scope (nested_name_specifier);
16980       /* Get the canonical version of this type.  */
16981       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16982       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16983           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16984         {
16985           type = push_template_decl (type);
16986           if (type == error_mark_node)
16987             {
16988               type = NULL_TREE;
16989               goto done;
16990             }
16991         }
16992
16993       type = TREE_TYPE (type);
16994       *nested_name_specifier_p = true;
16995     }
16996   else      /* The name is not a nested name.  */
16997     {
16998       /* If the class was unnamed, create a dummy name.  */
16999       if (!id)
17000         id = make_anon_name ();
17001       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17002                        parser->num_template_parameter_lists);
17003     }
17004
17005   /* Indicate whether this class was declared as a `class' or as a
17006      `struct'.  */
17007   if (TREE_CODE (type) == RECORD_TYPE)
17008     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17009   cp_parser_check_class_key (class_key, type);
17010
17011   /* If this type was already complete, and we see another definition,
17012      that's an error.  */
17013   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17014     {
17015       error_at (type_start_token->location, "redefinition of %q#T",
17016                 type);
17017       error_at (type_start_token->location, "previous definition of %q+#T",
17018                 type);
17019       type = NULL_TREE;
17020       goto done;
17021     }
17022   else if (type == error_mark_node)
17023     type = NULL_TREE;
17024
17025   /* We will have entered the scope containing the class; the names of
17026      base classes should be looked up in that context.  For example:
17027
17028        struct A { struct B {}; struct C; };
17029        struct A::C : B {};
17030
17031      is valid.  */
17032
17033   /* Get the list of base-classes, if there is one.  */
17034   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17035     *bases = cp_parser_base_clause (parser);
17036
17037  done:
17038   /* Leave the scope given by the nested-name-specifier.  We will
17039      enter the class scope itself while processing the members.  */
17040   if (pushed_scope)
17041     pop_scope (pushed_scope);
17042
17043   if (invalid_explicit_specialization_p)
17044     {
17045       end_specialization ();
17046       --parser->num_template_parameter_lists;
17047     }
17048
17049   if (type)
17050     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17051   *attributes_p = attributes;
17052   return type;
17053 }
17054
17055 /* Parse a class-key.
17056
17057    class-key:
17058      class
17059      struct
17060      union
17061
17062    Returns the kind of class-key specified, or none_type to indicate
17063    error.  */
17064
17065 static enum tag_types
17066 cp_parser_class_key (cp_parser* parser)
17067 {
17068   cp_token *token;
17069   enum tag_types tag_type;
17070
17071   /* Look for the class-key.  */
17072   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17073   if (!token)
17074     return none_type;
17075
17076   /* Check to see if the TOKEN is a class-key.  */
17077   tag_type = cp_parser_token_is_class_key (token);
17078   if (!tag_type)
17079     cp_parser_error (parser, "expected class-key");
17080   return tag_type;
17081 }
17082
17083 /* Parse an (optional) member-specification.
17084
17085    member-specification:
17086      member-declaration member-specification [opt]
17087      access-specifier : member-specification [opt]  */
17088
17089 static void
17090 cp_parser_member_specification_opt (cp_parser* parser)
17091 {
17092   while (true)
17093     {
17094       cp_token *token;
17095       enum rid keyword;
17096
17097       /* Peek at the next token.  */
17098       token = cp_lexer_peek_token (parser->lexer);
17099       /* If it's a `}', or EOF then we've seen all the members.  */
17100       if (token->type == CPP_CLOSE_BRACE
17101           || token->type == CPP_EOF
17102           || token->type == CPP_PRAGMA_EOL)
17103         break;
17104
17105       /* See if this token is a keyword.  */
17106       keyword = token->keyword;
17107       switch (keyword)
17108         {
17109         case RID_PUBLIC:
17110         case RID_PROTECTED:
17111         case RID_PRIVATE:
17112           /* Consume the access-specifier.  */
17113           cp_lexer_consume_token (parser->lexer);
17114           /* Remember which access-specifier is active.  */
17115           current_access_specifier = token->u.value;
17116           /* Look for the `:'.  */
17117           cp_parser_require (parser, CPP_COLON, RT_COLON);
17118           break;
17119
17120         default:
17121           /* Accept #pragmas at class scope.  */
17122           if (token->type == CPP_PRAGMA)
17123             {
17124               cp_parser_pragma (parser, pragma_external);
17125               break;
17126             }
17127
17128           /* Otherwise, the next construction must be a
17129              member-declaration.  */
17130           cp_parser_member_declaration (parser);
17131         }
17132     }
17133 }
17134
17135 /* Parse a member-declaration.
17136
17137    member-declaration:
17138      decl-specifier-seq [opt] member-declarator-list [opt] ;
17139      function-definition ; [opt]
17140      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17141      using-declaration
17142      template-declaration
17143
17144    member-declarator-list:
17145      member-declarator
17146      member-declarator-list , member-declarator
17147
17148    member-declarator:
17149      declarator pure-specifier [opt]
17150      declarator constant-initializer [opt]
17151      identifier [opt] : constant-expression
17152
17153    GNU Extensions:
17154
17155    member-declaration:
17156      __extension__ member-declaration
17157
17158    member-declarator:
17159      declarator attributes [opt] pure-specifier [opt]
17160      declarator attributes [opt] constant-initializer [opt]
17161      identifier [opt] attributes [opt] : constant-expression  
17162
17163    C++0x Extensions:
17164
17165    member-declaration:
17166      static_assert-declaration  */
17167
17168 static void
17169 cp_parser_member_declaration (cp_parser* parser)
17170 {
17171   cp_decl_specifier_seq decl_specifiers;
17172   tree prefix_attributes;
17173   tree decl;
17174   int declares_class_or_enum;
17175   bool friend_p;
17176   cp_token *token = NULL;
17177   cp_token *decl_spec_token_start = NULL;
17178   cp_token *initializer_token_start = NULL;
17179   int saved_pedantic;
17180
17181   /* Check for the `__extension__' keyword.  */
17182   if (cp_parser_extension_opt (parser, &saved_pedantic))
17183     {
17184       /* Recurse.  */
17185       cp_parser_member_declaration (parser);
17186       /* Restore the old value of the PEDANTIC flag.  */
17187       pedantic = saved_pedantic;
17188
17189       return;
17190     }
17191
17192   /* Check for a template-declaration.  */
17193   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17194     {
17195       /* An explicit specialization here is an error condition, and we
17196          expect the specialization handler to detect and report this.  */
17197       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17198           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17199         cp_parser_explicit_specialization (parser);
17200       else
17201         cp_parser_template_declaration (parser, /*member_p=*/true);
17202
17203       return;
17204     }
17205
17206   /* Check for a using-declaration.  */
17207   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17208     {
17209       /* Parse the using-declaration.  */
17210       cp_parser_using_declaration (parser,
17211                                    /*access_declaration_p=*/false);
17212       return;
17213     }
17214
17215   /* Check for @defs.  */
17216   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17217     {
17218       tree ivar, member;
17219       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17220       ivar = ivar_chains;
17221       while (ivar)
17222         {
17223           member = ivar;
17224           ivar = TREE_CHAIN (member);
17225           TREE_CHAIN (member) = NULL_TREE;
17226           finish_member_declaration (member);
17227         }
17228       return;
17229     }
17230
17231   /* If the next token is `static_assert' we have a static assertion.  */
17232   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17233     {
17234       cp_parser_static_assert (parser, /*member_p=*/true);
17235       return;
17236     }
17237
17238   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17239     return;
17240
17241   /* Parse the decl-specifier-seq.  */
17242   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17243   cp_parser_decl_specifier_seq (parser,
17244                                 CP_PARSER_FLAGS_OPTIONAL,
17245                                 &decl_specifiers,
17246                                 &declares_class_or_enum);
17247   prefix_attributes = decl_specifiers.attributes;
17248   decl_specifiers.attributes = NULL_TREE;
17249   /* Check for an invalid type-name.  */
17250   if (!decl_specifiers.any_type_specifiers_p
17251       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17252     return;
17253   /* If there is no declarator, then the decl-specifier-seq should
17254      specify a type.  */
17255   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17256     {
17257       /* If there was no decl-specifier-seq, and the next token is a
17258          `;', then we have something like:
17259
17260            struct S { ; };
17261
17262          [class.mem]
17263
17264          Each member-declaration shall declare at least one member
17265          name of the class.  */
17266       if (!decl_specifiers.any_specifiers_p)
17267         {
17268           cp_token *token = cp_lexer_peek_token (parser->lexer);
17269           if (!in_system_header_at (token->location))
17270             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17271         }
17272       else
17273         {
17274           tree type;
17275
17276           /* See if this declaration is a friend.  */
17277           friend_p = cp_parser_friend_p (&decl_specifiers);
17278           /* If there were decl-specifiers, check to see if there was
17279              a class-declaration.  */
17280           type = check_tag_decl (&decl_specifiers);
17281           /* Nested classes have already been added to the class, but
17282              a `friend' needs to be explicitly registered.  */
17283           if (friend_p)
17284             {
17285               /* If the `friend' keyword was present, the friend must
17286                  be introduced with a class-key.  */
17287                if (!declares_class_or_enum)
17288                  error_at (decl_spec_token_start->location,
17289                            "a class-key must be used when declaring a friend");
17290                /* In this case:
17291
17292                     template <typename T> struct A {
17293                       friend struct A<T>::B;
17294                     };
17295
17296                   A<T>::B will be represented by a TYPENAME_TYPE, and
17297                   therefore not recognized by check_tag_decl.  */
17298                if (!type
17299                    && decl_specifiers.type
17300                    && TYPE_P (decl_specifiers.type))
17301                  type = decl_specifiers.type;
17302                if (!type || !TYPE_P (type))
17303                  error_at (decl_spec_token_start->location,
17304                            "friend declaration does not name a class or "
17305                            "function");
17306                else
17307                  make_friend_class (current_class_type, type,
17308                                     /*complain=*/true);
17309             }
17310           /* If there is no TYPE, an error message will already have
17311              been issued.  */
17312           else if (!type || type == error_mark_node)
17313             ;
17314           /* An anonymous aggregate has to be handled specially; such
17315              a declaration really declares a data member (with a
17316              particular type), as opposed to a nested class.  */
17317           else if (ANON_AGGR_TYPE_P (type))
17318             {
17319               /* Remove constructors and such from TYPE, now that we
17320                  know it is an anonymous aggregate.  */
17321               fixup_anonymous_aggr (type);
17322               /* And make the corresponding data member.  */
17323               decl = build_decl (decl_spec_token_start->location,
17324                                  FIELD_DECL, NULL_TREE, type);
17325               /* Add it to the class.  */
17326               finish_member_declaration (decl);
17327             }
17328           else
17329             cp_parser_check_access_in_redeclaration
17330                                               (TYPE_NAME (type),
17331                                                decl_spec_token_start->location);
17332         }
17333     }
17334   else
17335     {
17336       /* See if these declarations will be friends.  */
17337       friend_p = cp_parser_friend_p (&decl_specifiers);
17338
17339       /* Keep going until we hit the `;' at the end of the
17340          declaration.  */
17341       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17342         {
17343           tree attributes = NULL_TREE;
17344           tree first_attribute;
17345
17346           /* Peek at the next token.  */
17347           token = cp_lexer_peek_token (parser->lexer);
17348
17349           /* Check for a bitfield declaration.  */
17350           if (token->type == CPP_COLON
17351               || (token->type == CPP_NAME
17352                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17353                   == CPP_COLON))
17354             {
17355               tree identifier;
17356               tree width;
17357
17358               /* Get the name of the bitfield.  Note that we cannot just
17359                  check TOKEN here because it may have been invalidated by
17360                  the call to cp_lexer_peek_nth_token above.  */
17361               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17362                 identifier = cp_parser_identifier (parser);
17363               else
17364                 identifier = NULL_TREE;
17365
17366               /* Consume the `:' token.  */
17367               cp_lexer_consume_token (parser->lexer);
17368               /* Get the width of the bitfield.  */
17369               width
17370                 = cp_parser_constant_expression (parser,
17371                                                  /*allow_non_constant=*/false,
17372                                                  NULL);
17373
17374               /* Look for attributes that apply to the bitfield.  */
17375               attributes = cp_parser_attributes_opt (parser);
17376               /* Remember which attributes are prefix attributes and
17377                  which are not.  */
17378               first_attribute = attributes;
17379               /* Combine the attributes.  */
17380               attributes = chainon (prefix_attributes, attributes);
17381
17382               /* Create the bitfield declaration.  */
17383               decl = grokbitfield (identifier
17384                                    ? make_id_declarator (NULL_TREE,
17385                                                          identifier,
17386                                                          sfk_none)
17387                                    : NULL,
17388                                    &decl_specifiers,
17389                                    width,
17390                                    attributes);
17391             }
17392           else
17393             {
17394               cp_declarator *declarator;
17395               tree initializer;
17396               tree asm_specification;
17397               int ctor_dtor_or_conv_p;
17398
17399               /* Parse the declarator.  */
17400               declarator
17401                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17402                                         &ctor_dtor_or_conv_p,
17403                                         /*parenthesized_p=*/NULL,
17404                                         /*member_p=*/true);
17405
17406               /* If something went wrong parsing the declarator, make sure
17407                  that we at least consume some tokens.  */
17408               if (declarator == cp_error_declarator)
17409                 {
17410                   /* Skip to the end of the statement.  */
17411                   cp_parser_skip_to_end_of_statement (parser);
17412                   /* If the next token is not a semicolon, that is
17413                      probably because we just skipped over the body of
17414                      a function.  So, we consume a semicolon if
17415                      present, but do not issue an error message if it
17416                      is not present.  */
17417                   if (cp_lexer_next_token_is (parser->lexer,
17418                                               CPP_SEMICOLON))
17419                     cp_lexer_consume_token (parser->lexer);
17420                   return;
17421                 }
17422
17423               if (declares_class_or_enum & 2)
17424                 cp_parser_check_for_definition_in_return_type
17425                                             (declarator, decl_specifiers.type,
17426                                              decl_specifiers.type_location);
17427
17428               /* Look for an asm-specification.  */
17429               asm_specification = cp_parser_asm_specification_opt (parser);
17430               /* Look for attributes that apply to the declaration.  */
17431               attributes = cp_parser_attributes_opt (parser);
17432               /* Remember which attributes are prefix attributes and
17433                  which are not.  */
17434               first_attribute = attributes;
17435               /* Combine the attributes.  */
17436               attributes = chainon (prefix_attributes, attributes);
17437
17438               /* If it's an `=', then we have a constant-initializer or a
17439                  pure-specifier.  It is not correct to parse the
17440                  initializer before registering the member declaration
17441                  since the member declaration should be in scope while
17442                  its initializer is processed.  However, the rest of the
17443                  front end does not yet provide an interface that allows
17444                  us to handle this correctly.  */
17445               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17446                 {
17447                   /* In [class.mem]:
17448
17449                      A pure-specifier shall be used only in the declaration of
17450                      a virtual function.
17451
17452                      A member-declarator can contain a constant-initializer
17453                      only if it declares a static member of integral or
17454                      enumeration type.
17455
17456                      Therefore, if the DECLARATOR is for a function, we look
17457                      for a pure-specifier; otherwise, we look for a
17458                      constant-initializer.  When we call `grokfield', it will
17459                      perform more stringent semantics checks.  */
17460                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17461                   if (function_declarator_p (declarator))
17462                     initializer = cp_parser_pure_specifier (parser);
17463                   else
17464                     /* Parse the initializer.  */
17465                     initializer = cp_parser_constant_initializer (parser);
17466                 }
17467               /* Otherwise, there is no initializer.  */
17468               else
17469                 initializer = NULL_TREE;
17470
17471               /* See if we are probably looking at a function
17472                  definition.  We are certainly not looking at a
17473                  member-declarator.  Calling `grokfield' has
17474                  side-effects, so we must not do it unless we are sure
17475                  that we are looking at a member-declarator.  */
17476               if (cp_parser_token_starts_function_definition_p
17477                   (cp_lexer_peek_token (parser->lexer)))
17478                 {
17479                   /* The grammar does not allow a pure-specifier to be
17480                      used when a member function is defined.  (It is
17481                      possible that this fact is an oversight in the
17482                      standard, since a pure function may be defined
17483                      outside of the class-specifier.  */
17484                   if (initializer)
17485                     error_at (initializer_token_start->location,
17486                               "pure-specifier on function-definition");
17487                   decl = cp_parser_save_member_function_body (parser,
17488                                                               &decl_specifiers,
17489                                                               declarator,
17490                                                               attributes);
17491                   /* If the member was not a friend, declare it here.  */
17492                   if (!friend_p)
17493                     finish_member_declaration (decl);
17494                   /* Peek at the next token.  */
17495                   token = cp_lexer_peek_token (parser->lexer);
17496                   /* If the next token is a semicolon, consume it.  */
17497                   if (token->type == CPP_SEMICOLON)
17498                     cp_lexer_consume_token (parser->lexer);
17499                   return;
17500                 }
17501               else
17502                 if (declarator->kind == cdk_function)
17503                   declarator->id_loc = token->location;
17504                 /* Create the declaration.  */
17505                 decl = grokfield (declarator, &decl_specifiers,
17506                                   initializer, /*init_const_expr_p=*/true,
17507                                   asm_specification,
17508                                   attributes);
17509             }
17510
17511           /* Reset PREFIX_ATTRIBUTES.  */
17512           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17513             attributes = TREE_CHAIN (attributes);
17514           if (attributes)
17515             TREE_CHAIN (attributes) = NULL_TREE;
17516
17517           /* If there is any qualification still in effect, clear it
17518              now; we will be starting fresh with the next declarator.  */
17519           parser->scope = NULL_TREE;
17520           parser->qualifying_scope = NULL_TREE;
17521           parser->object_scope = NULL_TREE;
17522           /* If it's a `,', then there are more declarators.  */
17523           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17524             cp_lexer_consume_token (parser->lexer);
17525           /* If the next token isn't a `;', then we have a parse error.  */
17526           else if (cp_lexer_next_token_is_not (parser->lexer,
17527                                                CPP_SEMICOLON))
17528             {
17529               cp_parser_error (parser, "expected %<;%>");
17530               /* Skip tokens until we find a `;'.  */
17531               cp_parser_skip_to_end_of_statement (parser);
17532
17533               break;
17534             }
17535
17536           if (decl)
17537             {
17538               /* Add DECL to the list of members.  */
17539               if (!friend_p)
17540                 finish_member_declaration (decl);
17541
17542               if (TREE_CODE (decl) == FUNCTION_DECL)
17543                 cp_parser_save_default_args (parser, decl);
17544             }
17545         }
17546     }
17547
17548   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17549 }
17550
17551 /* Parse a pure-specifier.
17552
17553    pure-specifier:
17554      = 0
17555
17556    Returns INTEGER_ZERO_NODE if a pure specifier is found.
17557    Otherwise, ERROR_MARK_NODE is returned.  */
17558
17559 static tree
17560 cp_parser_pure_specifier (cp_parser* parser)
17561 {
17562   cp_token *token;
17563
17564   /* Look for the `=' token.  */
17565   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17566     return error_mark_node;
17567   /* Look for the `0' token.  */
17568   token = cp_lexer_peek_token (parser->lexer);
17569
17570   if (token->type == CPP_EOF
17571       || token->type == CPP_PRAGMA_EOL)
17572     return error_mark_node;
17573
17574   cp_lexer_consume_token (parser->lexer);
17575
17576   /* Accept = default or = delete in c++0x mode.  */
17577   if (token->keyword == RID_DEFAULT
17578       || token->keyword == RID_DELETE)
17579     {
17580       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17581       return token->u.value;
17582     }
17583
17584   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
17585   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17586     {
17587       cp_parser_error (parser,
17588                        "invalid pure specifier (only %<= 0%> is allowed)");
17589       cp_parser_skip_to_end_of_statement (parser);
17590       return error_mark_node;
17591     }
17592   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17593     {
17594       error_at (token->location, "templates may not be %<virtual%>");
17595       return error_mark_node;
17596     }
17597
17598   return integer_zero_node;
17599 }
17600
17601 /* Parse a constant-initializer.
17602
17603    constant-initializer:
17604      = constant-expression
17605
17606    Returns a representation of the constant-expression.  */
17607
17608 static tree
17609 cp_parser_constant_initializer (cp_parser* parser)
17610 {
17611   /* Look for the `=' token.  */
17612   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17613     return error_mark_node;
17614
17615   /* It is invalid to write:
17616
17617        struct S { static const int i = { 7 }; };
17618
17619      */
17620   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17621     {
17622       cp_parser_error (parser,
17623                        "a brace-enclosed initializer is not allowed here");
17624       /* Consume the opening brace.  */
17625       cp_lexer_consume_token (parser->lexer);
17626       /* Skip the initializer.  */
17627       cp_parser_skip_to_closing_brace (parser);
17628       /* Look for the trailing `}'.  */
17629       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17630
17631       return error_mark_node;
17632     }
17633
17634   return cp_parser_constant_expression (parser,
17635                                         /*allow_non_constant=*/false,
17636                                         NULL);
17637 }
17638
17639 /* Derived classes [gram.class.derived] */
17640
17641 /* Parse a base-clause.
17642
17643    base-clause:
17644      : base-specifier-list
17645
17646    base-specifier-list:
17647      base-specifier ... [opt]
17648      base-specifier-list , base-specifier ... [opt]
17649
17650    Returns a TREE_LIST representing the base-classes, in the order in
17651    which they were declared.  The representation of each node is as
17652    described by cp_parser_base_specifier.
17653
17654    In the case that no bases are specified, this function will return
17655    NULL_TREE, not ERROR_MARK_NODE.  */
17656
17657 static tree
17658 cp_parser_base_clause (cp_parser* parser)
17659 {
17660   tree bases = NULL_TREE;
17661
17662   /* Look for the `:' that begins the list.  */
17663   cp_parser_require (parser, CPP_COLON, RT_COLON);
17664
17665   /* Scan the base-specifier-list.  */
17666   while (true)
17667     {
17668       cp_token *token;
17669       tree base;
17670       bool pack_expansion_p = false;
17671
17672       /* Look for the base-specifier.  */
17673       base = cp_parser_base_specifier (parser);
17674       /* Look for the (optional) ellipsis. */
17675       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17676         {
17677           /* Consume the `...'. */
17678           cp_lexer_consume_token (parser->lexer);
17679
17680           pack_expansion_p = true;
17681         }
17682
17683       /* Add BASE to the front of the list.  */
17684       if (base != error_mark_node)
17685         {
17686           if (pack_expansion_p)
17687             /* Make this a pack expansion type. */
17688             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17689           
17690
17691           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17692             {
17693               TREE_CHAIN (base) = bases;
17694               bases = base;
17695             }
17696         }
17697       /* Peek at the next token.  */
17698       token = cp_lexer_peek_token (parser->lexer);
17699       /* If it's not a comma, then the list is complete.  */
17700       if (token->type != CPP_COMMA)
17701         break;
17702       /* Consume the `,'.  */
17703       cp_lexer_consume_token (parser->lexer);
17704     }
17705
17706   /* PARSER->SCOPE may still be non-NULL at this point, if the last
17707      base class had a qualified name.  However, the next name that
17708      appears is certainly not qualified.  */
17709   parser->scope = NULL_TREE;
17710   parser->qualifying_scope = NULL_TREE;
17711   parser->object_scope = NULL_TREE;
17712
17713   return nreverse (bases);
17714 }
17715
17716 /* Parse a base-specifier.
17717
17718    base-specifier:
17719      :: [opt] nested-name-specifier [opt] class-name
17720      virtual access-specifier [opt] :: [opt] nested-name-specifier
17721        [opt] class-name
17722      access-specifier virtual [opt] :: [opt] nested-name-specifier
17723        [opt] class-name
17724
17725    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17726    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17727    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17728    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17729
17730 static tree
17731 cp_parser_base_specifier (cp_parser* parser)
17732 {
17733   cp_token *token;
17734   bool done = false;
17735   bool virtual_p = false;
17736   bool duplicate_virtual_error_issued_p = false;
17737   bool duplicate_access_error_issued_p = false;
17738   bool class_scope_p, template_p;
17739   tree access = access_default_node;
17740   tree type;
17741
17742   /* Process the optional `virtual' and `access-specifier'.  */
17743   while (!done)
17744     {
17745       /* Peek at the next token.  */
17746       token = cp_lexer_peek_token (parser->lexer);
17747       /* Process `virtual'.  */
17748       switch (token->keyword)
17749         {
17750         case RID_VIRTUAL:
17751           /* If `virtual' appears more than once, issue an error.  */
17752           if (virtual_p && !duplicate_virtual_error_issued_p)
17753             {
17754               cp_parser_error (parser,
17755                                "%<virtual%> specified more than once in base-specified");
17756               duplicate_virtual_error_issued_p = true;
17757             }
17758
17759           virtual_p = true;
17760
17761           /* Consume the `virtual' token.  */
17762           cp_lexer_consume_token (parser->lexer);
17763
17764           break;
17765
17766         case RID_PUBLIC:
17767         case RID_PROTECTED:
17768         case RID_PRIVATE:
17769           /* If more than one access specifier appears, issue an
17770              error.  */
17771           if (access != access_default_node
17772               && !duplicate_access_error_issued_p)
17773             {
17774               cp_parser_error (parser,
17775                                "more than one access specifier in base-specified");
17776               duplicate_access_error_issued_p = true;
17777             }
17778
17779           access = ridpointers[(int) token->keyword];
17780
17781           /* Consume the access-specifier.  */
17782           cp_lexer_consume_token (parser->lexer);
17783
17784           break;
17785
17786         default:
17787           done = true;
17788           break;
17789         }
17790     }
17791   /* It is not uncommon to see programs mechanically, erroneously, use
17792      the 'typename' keyword to denote (dependent) qualified types
17793      as base classes.  */
17794   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17795     {
17796       token = cp_lexer_peek_token (parser->lexer);
17797       if (!processing_template_decl)
17798         error_at (token->location,
17799                   "keyword %<typename%> not allowed outside of templates");
17800       else
17801         error_at (token->location,
17802                   "keyword %<typename%> not allowed in this context "
17803                   "(the base class is implicitly a type)");
17804       cp_lexer_consume_token (parser->lexer);
17805     }
17806
17807   /* Look for the optional `::' operator.  */
17808   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17809   /* Look for the nested-name-specifier.  The simplest way to
17810      implement:
17811
17812        [temp.res]
17813
17814        The keyword `typename' is not permitted in a base-specifier or
17815        mem-initializer; in these contexts a qualified name that
17816        depends on a template-parameter is implicitly assumed to be a
17817        type name.
17818
17819      is to pretend that we have seen the `typename' keyword at this
17820      point.  */
17821   cp_parser_nested_name_specifier_opt (parser,
17822                                        /*typename_keyword_p=*/true,
17823                                        /*check_dependency_p=*/true,
17824                                        typename_type,
17825                                        /*is_declaration=*/true);
17826   /* If the base class is given by a qualified name, assume that names
17827      we see are type names or templates, as appropriate.  */
17828   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17829   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17830
17831   /* Finally, look for the class-name.  */
17832   type = cp_parser_class_name (parser,
17833                                class_scope_p,
17834                                template_p,
17835                                typename_type,
17836                                /*check_dependency_p=*/true,
17837                                /*class_head_p=*/false,
17838                                /*is_declaration=*/true);
17839
17840   if (type == error_mark_node)
17841     return error_mark_node;
17842
17843   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17844 }
17845
17846 /* Exception handling [gram.exception] */
17847
17848 /* Parse an (optional) exception-specification.
17849
17850    exception-specification:
17851      throw ( type-id-list [opt] )
17852
17853    Returns a TREE_LIST representing the exception-specification.  The
17854    TREE_VALUE of each node is a type.  */
17855
17856 static tree
17857 cp_parser_exception_specification_opt (cp_parser* parser)
17858 {
17859   cp_token *token;
17860   tree type_id_list;
17861   const char *saved_message;
17862
17863   /* Peek at the next token.  */
17864   token = cp_lexer_peek_token (parser->lexer);
17865
17866   /* Is it a noexcept-specification?  */
17867   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
17868     {
17869       tree expr;
17870       cp_lexer_consume_token (parser->lexer);
17871
17872       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
17873         {
17874           cp_lexer_consume_token (parser->lexer);
17875
17876           /* Types may not be defined in an exception-specification.  */
17877           saved_message = parser->type_definition_forbidden_message;
17878           parser->type_definition_forbidden_message
17879             = G_("types may not be defined in an exception-specification");
17880
17881           expr = cp_parser_constant_expression (parser, false, NULL);
17882
17883           /* Restore the saved message.  */
17884           parser->type_definition_forbidden_message = saved_message;
17885
17886           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17887         }
17888       else
17889         expr = boolean_true_node;
17890
17891       return build_noexcept_spec (expr, tf_warning_or_error);
17892     }
17893
17894   /* If it's not `throw', then there's no exception-specification.  */
17895   if (!cp_parser_is_keyword (token, RID_THROW))
17896     return NULL_TREE;
17897
17898 #if 0
17899   /* Enable this once a lot of code has transitioned to noexcept?  */
17900   if (cxx_dialect == cxx0x && !in_system_header)
17901     warning (OPT_Wdeprecated, "dynamic exception specifications are "
17902              "deprecated in C++0x; use %<noexcept%> instead.");
17903 #endif
17904
17905   /* Consume the `throw'.  */
17906   cp_lexer_consume_token (parser->lexer);
17907
17908   /* Look for the `('.  */
17909   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17910
17911   /* Peek at the next token.  */
17912   token = cp_lexer_peek_token (parser->lexer);
17913   /* If it's not a `)', then there is a type-id-list.  */
17914   if (token->type != CPP_CLOSE_PAREN)
17915     {
17916       /* Types may not be defined in an exception-specification.  */
17917       saved_message = parser->type_definition_forbidden_message;
17918       parser->type_definition_forbidden_message
17919         = G_("types may not be defined in an exception-specification");
17920       /* Parse the type-id-list.  */
17921       type_id_list = cp_parser_type_id_list (parser);
17922       /* Restore the saved message.  */
17923       parser->type_definition_forbidden_message = saved_message;
17924     }
17925   else
17926     type_id_list = empty_except_spec;
17927
17928   /* Look for the `)'.  */
17929   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17930
17931   return type_id_list;
17932 }
17933
17934 /* Parse an (optional) type-id-list.
17935
17936    type-id-list:
17937      type-id ... [opt]
17938      type-id-list , type-id ... [opt]
17939
17940    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17941    in the order that the types were presented.  */
17942
17943 static tree
17944 cp_parser_type_id_list (cp_parser* parser)
17945 {
17946   tree types = NULL_TREE;
17947
17948   while (true)
17949     {
17950       cp_token *token;
17951       tree type;
17952
17953       /* Get the next type-id.  */
17954       type = cp_parser_type_id (parser);
17955       /* Parse the optional ellipsis. */
17956       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17957         {
17958           /* Consume the `...'. */
17959           cp_lexer_consume_token (parser->lexer);
17960
17961           /* Turn the type into a pack expansion expression. */
17962           type = make_pack_expansion (type);
17963         }
17964       /* Add it to the list.  */
17965       types = add_exception_specifier (types, type, /*complain=*/1);
17966       /* Peek at the next token.  */
17967       token = cp_lexer_peek_token (parser->lexer);
17968       /* If it is not a `,', we are done.  */
17969       if (token->type != CPP_COMMA)
17970         break;
17971       /* Consume the `,'.  */
17972       cp_lexer_consume_token (parser->lexer);
17973     }
17974
17975   return nreverse (types);
17976 }
17977
17978 /* Parse a try-block.
17979
17980    try-block:
17981      try compound-statement handler-seq  */
17982
17983 static tree
17984 cp_parser_try_block (cp_parser* parser)
17985 {
17986   tree try_block;
17987
17988   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
17989   try_block = begin_try_block ();
17990   cp_parser_compound_statement (parser, NULL, true);
17991   finish_try_block (try_block);
17992   cp_parser_handler_seq (parser);
17993   finish_handler_sequence (try_block);
17994
17995   return try_block;
17996 }
17997
17998 /* Parse a function-try-block.
17999
18000    function-try-block:
18001      try ctor-initializer [opt] function-body handler-seq  */
18002
18003 static bool
18004 cp_parser_function_try_block (cp_parser* parser)
18005 {
18006   tree compound_stmt;
18007   tree try_block;
18008   bool ctor_initializer_p;
18009
18010   /* Look for the `try' keyword.  */
18011   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18012     return false;
18013   /* Let the rest of the front end know where we are.  */
18014   try_block = begin_function_try_block (&compound_stmt);
18015   /* Parse the function-body.  */
18016   ctor_initializer_p
18017     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18018   /* We're done with the `try' part.  */
18019   finish_function_try_block (try_block);
18020   /* Parse the handlers.  */
18021   cp_parser_handler_seq (parser);
18022   /* We're done with the handlers.  */
18023   finish_function_handler_sequence (try_block, compound_stmt);
18024
18025   return ctor_initializer_p;
18026 }
18027
18028 /* Parse a handler-seq.
18029
18030    handler-seq:
18031      handler handler-seq [opt]  */
18032
18033 static void
18034 cp_parser_handler_seq (cp_parser* parser)
18035 {
18036   while (true)
18037     {
18038       cp_token *token;
18039
18040       /* Parse the handler.  */
18041       cp_parser_handler (parser);
18042       /* Peek at the next token.  */
18043       token = cp_lexer_peek_token (parser->lexer);
18044       /* If it's not `catch' then there are no more handlers.  */
18045       if (!cp_parser_is_keyword (token, RID_CATCH))
18046         break;
18047     }
18048 }
18049
18050 /* Parse a handler.
18051
18052    handler:
18053      catch ( exception-declaration ) compound-statement  */
18054
18055 static void
18056 cp_parser_handler (cp_parser* parser)
18057 {
18058   tree handler;
18059   tree declaration;
18060
18061   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18062   handler = begin_handler ();
18063   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18064   declaration = cp_parser_exception_declaration (parser);
18065   finish_handler_parms (declaration, handler);
18066   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18067   cp_parser_compound_statement (parser, NULL, false);
18068   finish_handler (handler);
18069 }
18070
18071 /* Parse an exception-declaration.
18072
18073    exception-declaration:
18074      type-specifier-seq declarator
18075      type-specifier-seq abstract-declarator
18076      type-specifier-seq
18077      ...
18078
18079    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18080    ellipsis variant is used.  */
18081
18082 static tree
18083 cp_parser_exception_declaration (cp_parser* parser)
18084 {
18085   cp_decl_specifier_seq type_specifiers;
18086   cp_declarator *declarator;
18087   const char *saved_message;
18088
18089   /* If it's an ellipsis, it's easy to handle.  */
18090   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18091     {
18092       /* Consume the `...' token.  */
18093       cp_lexer_consume_token (parser->lexer);
18094       return NULL_TREE;
18095     }
18096
18097   /* Types may not be defined in exception-declarations.  */
18098   saved_message = parser->type_definition_forbidden_message;
18099   parser->type_definition_forbidden_message
18100     = G_("types may not be defined in exception-declarations");
18101
18102   /* Parse the type-specifier-seq.  */
18103   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18104                                 /*is_trailing_return=*/false,
18105                                 &type_specifiers);
18106   /* If it's a `)', then there is no declarator.  */
18107   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18108     declarator = NULL;
18109   else
18110     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18111                                        /*ctor_dtor_or_conv_p=*/NULL,
18112                                        /*parenthesized_p=*/NULL,
18113                                        /*member_p=*/false);
18114
18115   /* Restore the saved message.  */
18116   parser->type_definition_forbidden_message = saved_message;
18117
18118   if (!type_specifiers.any_specifiers_p)
18119     return error_mark_node;
18120
18121   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18122 }
18123
18124 /* Parse a throw-expression.
18125
18126    throw-expression:
18127      throw assignment-expression [opt]
18128
18129    Returns a THROW_EXPR representing the throw-expression.  */
18130
18131 static tree
18132 cp_parser_throw_expression (cp_parser* parser)
18133 {
18134   tree expression;
18135   cp_token* token;
18136
18137   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18138   token = cp_lexer_peek_token (parser->lexer);
18139   /* Figure out whether or not there is an assignment-expression
18140      following the "throw" keyword.  */
18141   if (token->type == CPP_COMMA
18142       || token->type == CPP_SEMICOLON
18143       || token->type == CPP_CLOSE_PAREN
18144       || token->type == CPP_CLOSE_SQUARE
18145       || token->type == CPP_CLOSE_BRACE
18146       || token->type == CPP_COLON)
18147     expression = NULL_TREE;
18148   else
18149     expression = cp_parser_assignment_expression (parser,
18150                                                   /*cast_p=*/false, NULL);
18151
18152   return build_throw (expression);
18153 }
18154
18155 /* GNU Extensions */
18156
18157 /* Parse an (optional) asm-specification.
18158
18159    asm-specification:
18160      asm ( string-literal )
18161
18162    If the asm-specification is present, returns a STRING_CST
18163    corresponding to the string-literal.  Otherwise, returns
18164    NULL_TREE.  */
18165
18166 static tree
18167 cp_parser_asm_specification_opt (cp_parser* parser)
18168 {
18169   cp_token *token;
18170   tree asm_specification;
18171
18172   /* Peek at the next token.  */
18173   token = cp_lexer_peek_token (parser->lexer);
18174   /* If the next token isn't the `asm' keyword, then there's no
18175      asm-specification.  */
18176   if (!cp_parser_is_keyword (token, RID_ASM))
18177     return NULL_TREE;
18178
18179   /* Consume the `asm' token.  */
18180   cp_lexer_consume_token (parser->lexer);
18181   /* Look for the `('.  */
18182   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18183
18184   /* Look for the string-literal.  */
18185   asm_specification = cp_parser_string_literal (parser, false, false);
18186
18187   /* Look for the `)'.  */
18188   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18189
18190   return asm_specification;
18191 }
18192
18193 /* Parse an asm-operand-list.
18194
18195    asm-operand-list:
18196      asm-operand
18197      asm-operand-list , asm-operand
18198
18199    asm-operand:
18200      string-literal ( expression )
18201      [ string-literal ] string-literal ( expression )
18202
18203    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18204    each node is the expression.  The TREE_PURPOSE is itself a
18205    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18206    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18207    is a STRING_CST for the string literal before the parenthesis. Returns
18208    ERROR_MARK_NODE if any of the operands are invalid.  */
18209
18210 static tree
18211 cp_parser_asm_operand_list (cp_parser* parser)
18212 {
18213   tree asm_operands = NULL_TREE;
18214   bool invalid_operands = false;
18215
18216   while (true)
18217     {
18218       tree string_literal;
18219       tree expression;
18220       tree name;
18221
18222       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18223         {
18224           /* Consume the `[' token.  */
18225           cp_lexer_consume_token (parser->lexer);
18226           /* Read the operand name.  */
18227           name = cp_parser_identifier (parser);
18228           if (name != error_mark_node)
18229             name = build_string (IDENTIFIER_LENGTH (name),
18230                                  IDENTIFIER_POINTER (name));
18231           /* Look for the closing `]'.  */
18232           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18233         }
18234       else
18235         name = NULL_TREE;
18236       /* Look for the string-literal.  */
18237       string_literal = cp_parser_string_literal (parser, false, false);
18238
18239       /* Look for the `('.  */
18240       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18241       /* Parse the expression.  */
18242       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18243       /* Look for the `)'.  */
18244       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18245
18246       if (name == error_mark_node 
18247           || string_literal == error_mark_node 
18248           || expression == error_mark_node)
18249         invalid_operands = true;
18250
18251       /* Add this operand to the list.  */
18252       asm_operands = tree_cons (build_tree_list (name, string_literal),
18253                                 expression,
18254                                 asm_operands);
18255       /* If the next token is not a `,', there are no more
18256          operands.  */
18257       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18258         break;
18259       /* Consume the `,'.  */
18260       cp_lexer_consume_token (parser->lexer);
18261     }
18262
18263   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18264 }
18265
18266 /* Parse an asm-clobber-list.
18267
18268    asm-clobber-list:
18269      string-literal
18270      asm-clobber-list , string-literal
18271
18272    Returns a TREE_LIST, indicating the clobbers in the order that they
18273    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18274
18275 static tree
18276 cp_parser_asm_clobber_list (cp_parser* parser)
18277 {
18278   tree clobbers = NULL_TREE;
18279
18280   while (true)
18281     {
18282       tree string_literal;
18283
18284       /* Look for the string literal.  */
18285       string_literal = cp_parser_string_literal (parser, false, false);
18286       /* Add it to the list.  */
18287       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18288       /* If the next token is not a `,', then the list is
18289          complete.  */
18290       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18291         break;
18292       /* Consume the `,' token.  */
18293       cp_lexer_consume_token (parser->lexer);
18294     }
18295
18296   return clobbers;
18297 }
18298
18299 /* Parse an asm-label-list.
18300
18301    asm-label-list:
18302      identifier
18303      asm-label-list , identifier
18304
18305    Returns a TREE_LIST, indicating the labels in the order that they
18306    appeared.  The TREE_VALUE of each node is a label.  */
18307
18308 static tree
18309 cp_parser_asm_label_list (cp_parser* parser)
18310 {
18311   tree labels = NULL_TREE;
18312
18313   while (true)
18314     {
18315       tree identifier, label, name;
18316
18317       /* Look for the identifier.  */
18318       identifier = cp_parser_identifier (parser);
18319       if (!error_operand_p (identifier))
18320         {
18321           label = lookup_label (identifier);
18322           if (TREE_CODE (label) == LABEL_DECL)
18323             {
18324               TREE_USED (label) = 1;
18325               check_goto (label);
18326               name = build_string (IDENTIFIER_LENGTH (identifier),
18327                                    IDENTIFIER_POINTER (identifier));
18328               labels = tree_cons (name, label, labels);
18329             }
18330         }
18331       /* If the next token is not a `,', then the list is
18332          complete.  */
18333       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18334         break;
18335       /* Consume the `,' token.  */
18336       cp_lexer_consume_token (parser->lexer);
18337     }
18338
18339   return nreverse (labels);
18340 }
18341
18342 /* Parse an (optional) series of attributes.
18343
18344    attributes:
18345      attributes attribute
18346
18347    attribute:
18348      __attribute__ (( attribute-list [opt] ))
18349
18350    The return value is as for cp_parser_attribute_list.  */
18351
18352 static tree
18353 cp_parser_attributes_opt (cp_parser* parser)
18354 {
18355   tree attributes = NULL_TREE;
18356
18357   while (true)
18358     {
18359       cp_token *token;
18360       tree attribute_list;
18361
18362       /* Peek at the next token.  */
18363       token = cp_lexer_peek_token (parser->lexer);
18364       /* If it's not `__attribute__', then we're done.  */
18365       if (token->keyword != RID_ATTRIBUTE)
18366         break;
18367
18368       /* Consume the `__attribute__' keyword.  */
18369       cp_lexer_consume_token (parser->lexer);
18370       /* Look for the two `(' tokens.  */
18371       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18372       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18373
18374       /* Peek at the next token.  */
18375       token = cp_lexer_peek_token (parser->lexer);
18376       if (token->type != CPP_CLOSE_PAREN)
18377         /* Parse the attribute-list.  */
18378         attribute_list = cp_parser_attribute_list (parser);
18379       else
18380         /* If the next token is a `)', then there is no attribute
18381            list.  */
18382         attribute_list = NULL;
18383
18384       /* Look for the two `)' tokens.  */
18385       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18386       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18387
18388       /* Add these new attributes to the list.  */
18389       attributes = chainon (attributes, attribute_list);
18390     }
18391
18392   return attributes;
18393 }
18394
18395 /* Parse an attribute-list.
18396
18397    attribute-list:
18398      attribute
18399      attribute-list , attribute
18400
18401    attribute:
18402      identifier
18403      identifier ( identifier )
18404      identifier ( identifier , expression-list )
18405      identifier ( expression-list )
18406
18407    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18408    to an attribute.  The TREE_PURPOSE of each node is the identifier
18409    indicating which attribute is in use.  The TREE_VALUE represents
18410    the arguments, if any.  */
18411
18412 static tree
18413 cp_parser_attribute_list (cp_parser* parser)
18414 {
18415   tree attribute_list = NULL_TREE;
18416   bool save_translate_strings_p = parser->translate_strings_p;
18417
18418   parser->translate_strings_p = false;
18419   while (true)
18420     {
18421       cp_token *token;
18422       tree identifier;
18423       tree attribute;
18424
18425       /* Look for the identifier.  We also allow keywords here; for
18426          example `__attribute__ ((const))' is legal.  */
18427       token = cp_lexer_peek_token (parser->lexer);
18428       if (token->type == CPP_NAME
18429           || token->type == CPP_KEYWORD)
18430         {
18431           tree arguments = NULL_TREE;
18432
18433           /* Consume the token.  */
18434           token = cp_lexer_consume_token (parser->lexer);
18435
18436           /* Save away the identifier that indicates which attribute
18437              this is.  */
18438           identifier = (token->type == CPP_KEYWORD) 
18439             /* For keywords, use the canonical spelling, not the
18440                parsed identifier.  */
18441             ? ridpointers[(int) token->keyword]
18442             : token->u.value;
18443           
18444           attribute = build_tree_list (identifier, NULL_TREE);
18445
18446           /* Peek at the next token.  */
18447           token = cp_lexer_peek_token (parser->lexer);
18448           /* If it's an `(', then parse the attribute arguments.  */
18449           if (token->type == CPP_OPEN_PAREN)
18450             {
18451               VEC(tree,gc) *vec;
18452               int attr_flag = (attribute_takes_identifier_p (identifier)
18453                                ? id_attr : normal_attr);
18454               vec = cp_parser_parenthesized_expression_list
18455                     (parser, attr_flag, /*cast_p=*/false,
18456                      /*allow_expansion_p=*/false,
18457                      /*non_constant_p=*/NULL);
18458               if (vec == NULL)
18459                 arguments = error_mark_node;
18460               else
18461                 {
18462                   arguments = build_tree_list_vec (vec);
18463                   release_tree_vector (vec);
18464                 }
18465               /* Save the arguments away.  */
18466               TREE_VALUE (attribute) = arguments;
18467             }
18468
18469           if (arguments != error_mark_node)
18470             {
18471               /* Add this attribute to the list.  */
18472               TREE_CHAIN (attribute) = attribute_list;
18473               attribute_list = attribute;
18474             }
18475
18476           token = cp_lexer_peek_token (parser->lexer);
18477         }
18478       /* Now, look for more attributes.  If the next token isn't a
18479          `,', we're done.  */
18480       if (token->type != CPP_COMMA)
18481         break;
18482
18483       /* Consume the comma and keep going.  */
18484       cp_lexer_consume_token (parser->lexer);
18485     }
18486   parser->translate_strings_p = save_translate_strings_p;
18487
18488   /* We built up the list in reverse order.  */
18489   return nreverse (attribute_list);
18490 }
18491
18492 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
18493    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
18494    current value of the PEDANTIC flag, regardless of whether or not
18495    the `__extension__' keyword is present.  The caller is responsible
18496    for restoring the value of the PEDANTIC flag.  */
18497
18498 static bool
18499 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18500 {
18501   /* Save the old value of the PEDANTIC flag.  */
18502   *saved_pedantic = pedantic;
18503
18504   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18505     {
18506       /* Consume the `__extension__' token.  */
18507       cp_lexer_consume_token (parser->lexer);
18508       /* We're not being pedantic while the `__extension__' keyword is
18509          in effect.  */
18510       pedantic = 0;
18511
18512       return true;
18513     }
18514
18515   return false;
18516 }
18517
18518 /* Parse a label declaration.
18519
18520    label-declaration:
18521      __label__ label-declarator-seq ;
18522
18523    label-declarator-seq:
18524      identifier , label-declarator-seq
18525      identifier  */
18526
18527 static void
18528 cp_parser_label_declaration (cp_parser* parser)
18529 {
18530   /* Look for the `__label__' keyword.  */
18531   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18532
18533   while (true)
18534     {
18535       tree identifier;
18536
18537       /* Look for an identifier.  */
18538       identifier = cp_parser_identifier (parser);
18539       /* If we failed, stop.  */
18540       if (identifier == error_mark_node)
18541         break;
18542       /* Declare it as a label.  */
18543       finish_label_decl (identifier);
18544       /* If the next token is a `;', stop.  */
18545       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18546         break;
18547       /* Look for the `,' separating the label declarations.  */
18548       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18549     }
18550
18551   /* Look for the final `;'.  */
18552   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18553 }
18554
18555 /* Support Functions */
18556
18557 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18558    NAME should have one of the representations used for an
18559    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18560    is returned.  If PARSER->SCOPE is a dependent type, then a
18561    SCOPE_REF is returned.
18562
18563    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18564    returned; the name was already resolved when the TEMPLATE_ID_EXPR
18565    was formed.  Abstractly, such entities should not be passed to this
18566    function, because they do not need to be looked up, but it is
18567    simpler to check for this special case here, rather than at the
18568    call-sites.
18569
18570    In cases not explicitly covered above, this function returns a
18571    DECL, OVERLOAD, or baselink representing the result of the lookup.
18572    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18573    is returned.
18574
18575    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18576    (e.g., "struct") that was used.  In that case bindings that do not
18577    refer to types are ignored.
18578
18579    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18580    ignored.
18581
18582    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18583    are ignored.
18584
18585    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18586    types.
18587
18588    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18589    TREE_LIST of candidates if name-lookup results in an ambiguity, and
18590    NULL_TREE otherwise.  */
18591
18592 static tree
18593 cp_parser_lookup_name (cp_parser *parser, tree name,
18594                        enum tag_types tag_type,
18595                        bool is_template,
18596                        bool is_namespace,
18597                        bool check_dependency,
18598                        tree *ambiguous_decls,
18599                        location_t name_location)
18600 {
18601   int flags = 0;
18602   tree decl;
18603   tree object_type = parser->context->object_type;
18604
18605   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18606     flags |= LOOKUP_COMPLAIN;
18607
18608   /* Assume that the lookup will be unambiguous.  */
18609   if (ambiguous_decls)
18610     *ambiguous_decls = NULL_TREE;
18611
18612   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18613      no longer valid.  Note that if we are parsing tentatively, and
18614      the parse fails, OBJECT_TYPE will be automatically restored.  */
18615   parser->context->object_type = NULL_TREE;
18616
18617   if (name == error_mark_node)
18618     return error_mark_node;
18619
18620   /* A template-id has already been resolved; there is no lookup to
18621      do.  */
18622   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18623     return name;
18624   if (BASELINK_P (name))
18625     {
18626       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18627                   == TEMPLATE_ID_EXPR);
18628       return name;
18629     }
18630
18631   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
18632      it should already have been checked to make sure that the name
18633      used matches the type being destroyed.  */
18634   if (TREE_CODE (name) == BIT_NOT_EXPR)
18635     {
18636       tree type;
18637
18638       /* Figure out to which type this destructor applies.  */
18639       if (parser->scope)
18640         type = parser->scope;
18641       else if (object_type)
18642         type = object_type;
18643       else
18644         type = current_class_type;
18645       /* If that's not a class type, there is no destructor.  */
18646       if (!type || !CLASS_TYPE_P (type))
18647         return error_mark_node;
18648       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18649         lazily_declare_fn (sfk_destructor, type);
18650       if (!CLASSTYPE_DESTRUCTORS (type))
18651           return error_mark_node;
18652       /* If it was a class type, return the destructor.  */
18653       return CLASSTYPE_DESTRUCTORS (type);
18654     }
18655
18656   /* By this point, the NAME should be an ordinary identifier.  If
18657      the id-expression was a qualified name, the qualifying scope is
18658      stored in PARSER->SCOPE at this point.  */
18659   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18660
18661   /* Perform the lookup.  */
18662   if (parser->scope)
18663     {
18664       bool dependent_p;
18665
18666       if (parser->scope == error_mark_node)
18667         return error_mark_node;
18668
18669       /* If the SCOPE is dependent, the lookup must be deferred until
18670          the template is instantiated -- unless we are explicitly
18671          looking up names in uninstantiated templates.  Even then, we
18672          cannot look up the name if the scope is not a class type; it
18673          might, for example, be a template type parameter.  */
18674       dependent_p = (TYPE_P (parser->scope)
18675                      && dependent_scope_p (parser->scope));
18676       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18677           && dependent_p)
18678         /* Defer lookup.  */
18679         decl = error_mark_node;
18680       else
18681         {
18682           tree pushed_scope = NULL_TREE;
18683
18684           /* If PARSER->SCOPE is a dependent type, then it must be a
18685              class type, and we must not be checking dependencies;
18686              otherwise, we would have processed this lookup above.  So
18687              that PARSER->SCOPE is not considered a dependent base by
18688              lookup_member, we must enter the scope here.  */
18689           if (dependent_p)
18690             pushed_scope = push_scope (parser->scope);
18691
18692           /* If the PARSER->SCOPE is a template specialization, it
18693              may be instantiated during name lookup.  In that case,
18694              errors may be issued.  Even if we rollback the current
18695              tentative parse, those errors are valid.  */
18696           decl = lookup_qualified_name (parser->scope, name,
18697                                         tag_type != none_type,
18698                                         /*complain=*/true);
18699
18700           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18701              lookup result and the nested-name-specifier nominates a class C:
18702                * if the name specified after the nested-name-specifier, when
18703                looked up in C, is the injected-class-name of C (Clause 9), or
18704                * if the name specified after the nested-name-specifier is the
18705                same as the identifier or the simple-template-id's template-
18706                name in the last component of the nested-name-specifier,
18707              the name is instead considered to name the constructor of
18708              class C. [ Note: for example, the constructor is not an
18709              acceptable lookup result in an elaborated-type-specifier so
18710              the constructor would not be used in place of the
18711              injected-class-name. --end note ] Such a constructor name
18712              shall be used only in the declarator-id of a declaration that
18713              names a constructor or in a using-declaration.  */
18714           if (tag_type == none_type
18715               && DECL_SELF_REFERENCE_P (decl)
18716               && same_type_p (DECL_CONTEXT (decl), parser->scope))
18717             decl = lookup_qualified_name (parser->scope, ctor_identifier,
18718                                           tag_type != none_type,
18719                                           /*complain=*/true);
18720
18721           /* If we have a single function from a using decl, pull it out.  */
18722           if (TREE_CODE (decl) == OVERLOAD
18723               && !really_overloaded_fn (decl))
18724             decl = OVL_FUNCTION (decl);
18725
18726           if (pushed_scope)
18727             pop_scope (pushed_scope);
18728         }
18729
18730       /* If the scope is a dependent type and either we deferred lookup or
18731          we did lookup but didn't find the name, rememeber the name.  */
18732       if (decl == error_mark_node && TYPE_P (parser->scope)
18733           && dependent_type_p (parser->scope))
18734         {
18735           if (tag_type)
18736             {
18737               tree type;
18738
18739               /* The resolution to Core Issue 180 says that `struct
18740                  A::B' should be considered a type-name, even if `A'
18741                  is dependent.  */
18742               type = make_typename_type (parser->scope, name, tag_type,
18743                                          /*complain=*/tf_error);
18744               decl = TYPE_NAME (type);
18745             }
18746           else if (is_template
18747                    && (cp_parser_next_token_ends_template_argument_p (parser)
18748                        || cp_lexer_next_token_is (parser->lexer,
18749                                                   CPP_CLOSE_PAREN)))
18750             decl = make_unbound_class_template (parser->scope,
18751                                                 name, NULL_TREE,
18752                                                 /*complain=*/tf_error);
18753           else
18754             decl = build_qualified_name (/*type=*/NULL_TREE,
18755                                          parser->scope, name,
18756                                          is_template);
18757         }
18758       parser->qualifying_scope = parser->scope;
18759       parser->object_scope = NULL_TREE;
18760     }
18761   else if (object_type)
18762     {
18763       tree object_decl = NULL_TREE;
18764       /* Look up the name in the scope of the OBJECT_TYPE, unless the
18765          OBJECT_TYPE is not a class.  */
18766       if (CLASS_TYPE_P (object_type))
18767         /* If the OBJECT_TYPE is a template specialization, it may
18768            be instantiated during name lookup.  In that case, errors
18769            may be issued.  Even if we rollback the current tentative
18770            parse, those errors are valid.  */
18771         object_decl = lookup_member (object_type,
18772                                      name,
18773                                      /*protect=*/0,
18774                                      tag_type != none_type);
18775       /* Look it up in the enclosing context, too.  */
18776       decl = lookup_name_real (name, tag_type != none_type,
18777                                /*nonclass=*/0,
18778                                /*block_p=*/true, is_namespace, flags);
18779       parser->object_scope = object_type;
18780       parser->qualifying_scope = NULL_TREE;
18781       if (object_decl)
18782         decl = object_decl;
18783     }
18784   else
18785     {
18786       decl = lookup_name_real (name, tag_type != none_type,
18787                                /*nonclass=*/0,
18788                                /*block_p=*/true, is_namespace, flags);
18789       parser->qualifying_scope = NULL_TREE;
18790       parser->object_scope = NULL_TREE;
18791     }
18792
18793   /* If the lookup failed, let our caller know.  */
18794   if (!decl || decl == error_mark_node)
18795     return error_mark_node;
18796
18797   /* Pull out the template from an injected-class-name (or multiple).  */
18798   if (is_template)
18799     decl = maybe_get_template_decl_from_type_decl (decl);
18800
18801   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18802   if (TREE_CODE (decl) == TREE_LIST)
18803     {
18804       if (ambiguous_decls)
18805         *ambiguous_decls = decl;
18806       /* The error message we have to print is too complicated for
18807          cp_parser_error, so we incorporate its actions directly.  */
18808       if (!cp_parser_simulate_error (parser))
18809         {
18810           error_at (name_location, "reference to %qD is ambiguous",
18811                     name);
18812           print_candidates (decl);
18813         }
18814       return error_mark_node;
18815     }
18816
18817   gcc_assert (DECL_P (decl)
18818               || TREE_CODE (decl) == OVERLOAD
18819               || TREE_CODE (decl) == SCOPE_REF
18820               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18821               || BASELINK_P (decl));
18822
18823   /* If we have resolved the name of a member declaration, check to
18824      see if the declaration is accessible.  When the name resolves to
18825      set of overloaded functions, accessibility is checked when
18826      overload resolution is done.
18827
18828      During an explicit instantiation, access is not checked at all,
18829      as per [temp.explicit].  */
18830   if (DECL_P (decl))
18831     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18832
18833   return decl;
18834 }
18835
18836 /* Like cp_parser_lookup_name, but for use in the typical case where
18837    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18838    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18839
18840 static tree
18841 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18842 {
18843   return cp_parser_lookup_name (parser, name,
18844                                 none_type,
18845                                 /*is_template=*/false,
18846                                 /*is_namespace=*/false,
18847                                 /*check_dependency=*/true,
18848                                 /*ambiguous_decls=*/NULL,
18849                                 location);
18850 }
18851
18852 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18853    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18854    true, the DECL indicates the class being defined in a class-head,
18855    or declared in an elaborated-type-specifier.
18856
18857    Otherwise, return DECL.  */
18858
18859 static tree
18860 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18861 {
18862   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18863      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18864
18865        struct A {
18866          template <typename T> struct B;
18867        };
18868
18869        template <typename T> struct A::B {};
18870
18871      Similarly, in an elaborated-type-specifier:
18872
18873        namespace N { struct X{}; }
18874
18875        struct A {
18876          template <typename T> friend struct N::X;
18877        };
18878
18879      However, if the DECL refers to a class type, and we are in
18880      the scope of the class, then the name lookup automatically
18881      finds the TYPE_DECL created by build_self_reference rather
18882      than a TEMPLATE_DECL.  For example, in:
18883
18884        template <class T> struct S {
18885          S s;
18886        };
18887
18888      there is no need to handle such case.  */
18889
18890   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18891     return DECL_TEMPLATE_RESULT (decl);
18892
18893   return decl;
18894 }
18895
18896 /* If too many, or too few, template-parameter lists apply to the
18897    declarator, issue an error message.  Returns TRUE if all went well,
18898    and FALSE otherwise.  */
18899
18900 static bool
18901 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18902                                                 cp_declarator *declarator,
18903                                                 location_t declarator_location)
18904 {
18905   unsigned num_templates;
18906
18907   /* We haven't seen any classes that involve template parameters yet.  */
18908   num_templates = 0;
18909
18910   switch (declarator->kind)
18911     {
18912     case cdk_id:
18913       if (declarator->u.id.qualifying_scope)
18914         {
18915           tree scope;
18916
18917           scope = declarator->u.id.qualifying_scope;
18918
18919           while (scope && CLASS_TYPE_P (scope))
18920             {
18921               /* You're supposed to have one `template <...>'
18922                  for every template class, but you don't need one
18923                  for a full specialization.  For example:
18924
18925                  template <class T> struct S{};
18926                  template <> struct S<int> { void f(); };
18927                  void S<int>::f () {}
18928
18929                  is correct; there shouldn't be a `template <>' for
18930                  the definition of `S<int>::f'.  */
18931               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18932                 /* If SCOPE does not have template information of any
18933                    kind, then it is not a template, nor is it nested
18934                    within a template.  */
18935                 break;
18936               if (explicit_class_specialization_p (scope))
18937                 break;
18938               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18939                 ++num_templates;
18940
18941               scope = TYPE_CONTEXT (scope);
18942             }
18943         }
18944       else if (TREE_CODE (declarator->u.id.unqualified_name)
18945                == TEMPLATE_ID_EXPR)
18946         /* If the DECLARATOR has the form `X<y>' then it uses one
18947            additional level of template parameters.  */
18948         ++num_templates;
18949
18950       return cp_parser_check_template_parameters 
18951         (parser, num_templates, declarator_location, declarator);
18952
18953
18954     case cdk_function:
18955     case cdk_array:
18956     case cdk_pointer:
18957     case cdk_reference:
18958     case cdk_ptrmem:
18959       return (cp_parser_check_declarator_template_parameters
18960               (parser, declarator->declarator, declarator_location));
18961
18962     case cdk_error:
18963       return true;
18964
18965     default:
18966       gcc_unreachable ();
18967     }
18968   return false;
18969 }
18970
18971 /* NUM_TEMPLATES were used in the current declaration.  If that is
18972    invalid, return FALSE and issue an error messages.  Otherwise,
18973    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18974    declarator and we can print more accurate diagnostics.  */
18975
18976 static bool
18977 cp_parser_check_template_parameters (cp_parser* parser,
18978                                      unsigned num_templates,
18979                                      location_t location,
18980                                      cp_declarator *declarator)
18981 {
18982   /* If there are the same number of template classes and parameter
18983      lists, that's OK.  */
18984   if (parser->num_template_parameter_lists == num_templates)
18985     return true;
18986   /* If there are more, but only one more, then we are referring to a
18987      member template.  That's OK too.  */
18988   if (parser->num_template_parameter_lists == num_templates + 1)
18989     return true;
18990   /* If there are more template classes than parameter lists, we have
18991      something like:
18992
18993        template <class T> void S<T>::R<T>::f ();  */
18994   if (parser->num_template_parameter_lists < num_templates)
18995     {
18996       if (declarator && !current_function_decl)
18997         error_at (location, "specializing member %<%T::%E%> "
18998                   "requires %<template<>%> syntax", 
18999                   declarator->u.id.qualifying_scope,
19000                   declarator->u.id.unqualified_name);
19001       else if (declarator)
19002         error_at (location, "invalid declaration of %<%T::%E%>",
19003                   declarator->u.id.qualifying_scope,
19004                   declarator->u.id.unqualified_name);
19005       else 
19006         error_at (location, "too few template-parameter-lists");
19007       return false;
19008     }
19009   /* Otherwise, there are too many template parameter lists.  We have
19010      something like:
19011
19012      template <class T> template <class U> void S::f();  */
19013   error_at (location, "too many template-parameter-lists");
19014   return false;
19015 }
19016
19017 /* Parse an optional `::' token indicating that the following name is
19018    from the global namespace.  If so, PARSER->SCOPE is set to the
19019    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19020    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19021    Returns the new value of PARSER->SCOPE, if the `::' token is
19022    present, and NULL_TREE otherwise.  */
19023
19024 static tree
19025 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19026 {
19027   cp_token *token;
19028
19029   /* Peek at the next token.  */
19030   token = cp_lexer_peek_token (parser->lexer);
19031   /* If we're looking at a `::' token then we're starting from the
19032      global namespace, not our current location.  */
19033   if (token->type == CPP_SCOPE)
19034     {
19035       /* Consume the `::' token.  */
19036       cp_lexer_consume_token (parser->lexer);
19037       /* Set the SCOPE so that we know where to start the lookup.  */
19038       parser->scope = global_namespace;
19039       parser->qualifying_scope = global_namespace;
19040       parser->object_scope = NULL_TREE;
19041
19042       return parser->scope;
19043     }
19044   else if (!current_scope_valid_p)
19045     {
19046       parser->scope = NULL_TREE;
19047       parser->qualifying_scope = NULL_TREE;
19048       parser->object_scope = NULL_TREE;
19049     }
19050
19051   return NULL_TREE;
19052 }
19053
19054 /* Returns TRUE if the upcoming token sequence is the start of a
19055    constructor declarator.  If FRIEND_P is true, the declarator is
19056    preceded by the `friend' specifier.  */
19057
19058 static bool
19059 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19060 {
19061   bool constructor_p;
19062   tree nested_name_specifier;
19063   cp_token *next_token;
19064
19065   /* The common case is that this is not a constructor declarator, so
19066      try to avoid doing lots of work if at all possible.  It's not
19067      valid declare a constructor at function scope.  */
19068   if (parser->in_function_body)
19069     return false;
19070   /* And only certain tokens can begin a constructor declarator.  */
19071   next_token = cp_lexer_peek_token (parser->lexer);
19072   if (next_token->type != CPP_NAME
19073       && next_token->type != CPP_SCOPE
19074       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19075       && next_token->type != CPP_TEMPLATE_ID)
19076     return false;
19077
19078   /* Parse tentatively; we are going to roll back all of the tokens
19079      consumed here.  */
19080   cp_parser_parse_tentatively (parser);
19081   /* Assume that we are looking at a constructor declarator.  */
19082   constructor_p = true;
19083
19084   /* Look for the optional `::' operator.  */
19085   cp_parser_global_scope_opt (parser,
19086                               /*current_scope_valid_p=*/false);
19087   /* Look for the nested-name-specifier.  */
19088   nested_name_specifier
19089     = (cp_parser_nested_name_specifier_opt (parser,
19090                                             /*typename_keyword_p=*/false,
19091                                             /*check_dependency_p=*/false,
19092                                             /*type_p=*/false,
19093                                             /*is_declaration=*/false));
19094   /* Outside of a class-specifier, there must be a
19095      nested-name-specifier.  */
19096   if (!nested_name_specifier &&
19097       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19098        || friend_p))
19099     constructor_p = false;
19100   else if (nested_name_specifier == error_mark_node)
19101     constructor_p = false;
19102
19103   /* If we have a class scope, this is easy; DR 147 says that S::S always
19104      names the constructor, and no other qualified name could.  */
19105   if (constructor_p && nested_name_specifier
19106       && TYPE_P (nested_name_specifier))
19107     {
19108       tree id = cp_parser_unqualified_id (parser,
19109                                           /*template_keyword_p=*/false,
19110                                           /*check_dependency_p=*/false,
19111                                           /*declarator_p=*/true,
19112                                           /*optional_p=*/false);
19113       if (is_overloaded_fn (id))
19114         id = DECL_NAME (get_first_fn (id));
19115       if (!constructor_name_p (id, nested_name_specifier))
19116         constructor_p = false;
19117     }
19118   /* If we still think that this might be a constructor-declarator,
19119      look for a class-name.  */
19120   else if (constructor_p)
19121     {
19122       /* If we have:
19123
19124            template <typename T> struct S {
19125              S();
19126            };
19127
19128          we must recognize that the nested `S' names a class.  */
19129       tree type_decl;
19130       type_decl = cp_parser_class_name (parser,
19131                                         /*typename_keyword_p=*/false,
19132                                         /*template_keyword_p=*/false,
19133                                         none_type,
19134                                         /*check_dependency_p=*/false,
19135                                         /*class_head_p=*/false,
19136                                         /*is_declaration=*/false);
19137       /* If there was no class-name, then this is not a constructor.  */
19138       constructor_p = !cp_parser_error_occurred (parser);
19139
19140       /* If we're still considering a constructor, we have to see a `(',
19141          to begin the parameter-declaration-clause, followed by either a
19142          `)', an `...', or a decl-specifier.  We need to check for a
19143          type-specifier to avoid being fooled into thinking that:
19144
19145            S (f) (int);
19146
19147          is a constructor.  (It is actually a function named `f' that
19148          takes one parameter (of type `int') and returns a value of type
19149          `S'.  */
19150       if (constructor_p
19151           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19152         constructor_p = false;
19153
19154       if (constructor_p
19155           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19156           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19157           /* A parameter declaration begins with a decl-specifier,
19158              which is either the "attribute" keyword, a storage class
19159              specifier, or (usually) a type-specifier.  */
19160           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19161         {
19162           tree type;
19163           tree pushed_scope = NULL_TREE;
19164           unsigned saved_num_template_parameter_lists;
19165
19166           /* Names appearing in the type-specifier should be looked up
19167              in the scope of the class.  */
19168           if (current_class_type)
19169             type = NULL_TREE;
19170           else
19171             {
19172               type = TREE_TYPE (type_decl);
19173               if (TREE_CODE (type) == TYPENAME_TYPE)
19174                 {
19175                   type = resolve_typename_type (type,
19176                                                 /*only_current_p=*/false);
19177                   if (TREE_CODE (type) == TYPENAME_TYPE)
19178                     {
19179                       cp_parser_abort_tentative_parse (parser);
19180                       return false;
19181                     }
19182                 }
19183               pushed_scope = push_scope (type);
19184             }
19185
19186           /* Inside the constructor parameter list, surrounding
19187              template-parameter-lists do not apply.  */
19188           saved_num_template_parameter_lists
19189             = parser->num_template_parameter_lists;
19190           parser->num_template_parameter_lists = 0;
19191
19192           /* Look for the type-specifier.  */
19193           cp_parser_type_specifier (parser,
19194                                     CP_PARSER_FLAGS_NONE,
19195                                     /*decl_specs=*/NULL,
19196                                     /*is_declarator=*/true,
19197                                     /*declares_class_or_enum=*/NULL,
19198                                     /*is_cv_qualifier=*/NULL);
19199
19200           parser->num_template_parameter_lists
19201             = saved_num_template_parameter_lists;
19202
19203           /* Leave the scope of the class.  */
19204           if (pushed_scope)
19205             pop_scope (pushed_scope);
19206
19207           constructor_p = !cp_parser_error_occurred (parser);
19208         }
19209     }
19210
19211   /* We did not really want to consume any tokens.  */
19212   cp_parser_abort_tentative_parse (parser);
19213
19214   return constructor_p;
19215 }
19216
19217 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19218    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19219    they must be performed once we are in the scope of the function.
19220
19221    Returns the function defined.  */
19222
19223 static tree
19224 cp_parser_function_definition_from_specifiers_and_declarator
19225   (cp_parser* parser,
19226    cp_decl_specifier_seq *decl_specifiers,
19227    tree attributes,
19228    const cp_declarator *declarator)
19229 {
19230   tree fn;
19231   bool success_p;
19232
19233   /* Begin the function-definition.  */
19234   success_p = start_function (decl_specifiers, declarator, attributes);
19235
19236   /* The things we're about to see are not directly qualified by any
19237      template headers we've seen thus far.  */
19238   reset_specialization ();
19239
19240   /* If there were names looked up in the decl-specifier-seq that we
19241      did not check, check them now.  We must wait until we are in the
19242      scope of the function to perform the checks, since the function
19243      might be a friend.  */
19244   perform_deferred_access_checks ();
19245
19246   if (!success_p)
19247     {
19248       /* Skip the entire function.  */
19249       cp_parser_skip_to_end_of_block_or_statement (parser);
19250       fn = error_mark_node;
19251     }
19252   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19253     {
19254       /* Seen already, skip it.  An error message has already been output.  */
19255       cp_parser_skip_to_end_of_block_or_statement (parser);
19256       fn = current_function_decl;
19257       current_function_decl = NULL_TREE;
19258       /* If this is a function from a class, pop the nested class.  */
19259       if (current_class_name)
19260         pop_nested_class ();
19261     }
19262   else
19263     fn = cp_parser_function_definition_after_declarator (parser,
19264                                                          /*inline_p=*/false);
19265
19266   return fn;
19267 }
19268
19269 /* Parse the part of a function-definition that follows the
19270    declarator.  INLINE_P is TRUE iff this function is an inline
19271    function defined within a class-specifier.
19272
19273    Returns the function defined.  */
19274
19275 static tree
19276 cp_parser_function_definition_after_declarator (cp_parser* parser,
19277                                                 bool inline_p)
19278 {
19279   tree fn;
19280   bool ctor_initializer_p = false;
19281   bool saved_in_unbraced_linkage_specification_p;
19282   bool saved_in_function_body;
19283   unsigned saved_num_template_parameter_lists;
19284   cp_token *token;
19285
19286   saved_in_function_body = parser->in_function_body;
19287   parser->in_function_body = true;
19288   /* If the next token is `return', then the code may be trying to
19289      make use of the "named return value" extension that G++ used to
19290      support.  */
19291   token = cp_lexer_peek_token (parser->lexer);
19292   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19293     {
19294       /* Consume the `return' keyword.  */
19295       cp_lexer_consume_token (parser->lexer);
19296       /* Look for the identifier that indicates what value is to be
19297          returned.  */
19298       cp_parser_identifier (parser);
19299       /* Issue an error message.  */
19300       error_at (token->location,
19301                 "named return values are no longer supported");
19302       /* Skip tokens until we reach the start of the function body.  */
19303       while (true)
19304         {
19305           cp_token *token = cp_lexer_peek_token (parser->lexer);
19306           if (token->type == CPP_OPEN_BRACE
19307               || token->type == CPP_EOF
19308               || token->type == CPP_PRAGMA_EOL)
19309             break;
19310           cp_lexer_consume_token (parser->lexer);
19311         }
19312     }
19313   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19314      anything declared inside `f'.  */
19315   saved_in_unbraced_linkage_specification_p
19316     = parser->in_unbraced_linkage_specification_p;
19317   parser->in_unbraced_linkage_specification_p = false;
19318   /* Inside the function, surrounding template-parameter-lists do not
19319      apply.  */
19320   saved_num_template_parameter_lists
19321     = parser->num_template_parameter_lists;
19322   parser->num_template_parameter_lists = 0;
19323
19324   start_lambda_scope (current_function_decl);
19325
19326   /* If the next token is `try', then we are looking at a
19327      function-try-block.  */
19328   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19329     ctor_initializer_p = cp_parser_function_try_block (parser);
19330   /* A function-try-block includes the function-body, so we only do
19331      this next part if we're not processing a function-try-block.  */
19332   else
19333     ctor_initializer_p
19334       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19335
19336   finish_lambda_scope ();
19337
19338   /* Finish the function.  */
19339   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19340                         (inline_p ? 2 : 0));
19341   /* Generate code for it, if necessary.  */
19342   expand_or_defer_fn (fn);
19343   /* Restore the saved values.  */
19344   parser->in_unbraced_linkage_specification_p
19345     = saved_in_unbraced_linkage_specification_p;
19346   parser->num_template_parameter_lists
19347     = saved_num_template_parameter_lists;
19348   parser->in_function_body = saved_in_function_body;
19349
19350   return fn;
19351 }
19352
19353 /* Parse a template-declaration, assuming that the `export' (and
19354    `extern') keywords, if present, has already been scanned.  MEMBER_P
19355    is as for cp_parser_template_declaration.  */
19356
19357 static void
19358 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19359 {
19360   tree decl = NULL_TREE;
19361   VEC (deferred_access_check,gc) *checks;
19362   tree parameter_list;
19363   bool friend_p = false;
19364   bool need_lang_pop;
19365   cp_token *token;
19366
19367   /* Look for the `template' keyword.  */
19368   token = cp_lexer_peek_token (parser->lexer);
19369   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19370     return;
19371
19372   /* And the `<'.  */
19373   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19374     return;
19375   if (at_class_scope_p () && current_function_decl)
19376     {
19377       /* 14.5.2.2 [temp.mem]
19378
19379          A local class shall not have member templates.  */
19380       error_at (token->location,
19381                 "invalid declaration of member template in local class");
19382       cp_parser_skip_to_end_of_block_or_statement (parser);
19383       return;
19384     }
19385   /* [temp]
19386
19387      A template ... shall not have C linkage.  */
19388   if (current_lang_name == lang_name_c)
19389     {
19390       error_at (token->location, "template with C linkage");
19391       /* Give it C++ linkage to avoid confusing other parts of the
19392          front end.  */
19393       push_lang_context (lang_name_cplusplus);
19394       need_lang_pop = true;
19395     }
19396   else
19397     need_lang_pop = false;
19398
19399   /* We cannot perform access checks on the template parameter
19400      declarations until we know what is being declared, just as we
19401      cannot check the decl-specifier list.  */
19402   push_deferring_access_checks (dk_deferred);
19403
19404   /* If the next token is `>', then we have an invalid
19405      specialization.  Rather than complain about an invalid template
19406      parameter, issue an error message here.  */
19407   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19408     {
19409       cp_parser_error (parser, "invalid explicit specialization");
19410       begin_specialization ();
19411       parameter_list = NULL_TREE;
19412     }
19413   else
19414     /* Parse the template parameters.  */
19415     parameter_list = cp_parser_template_parameter_list (parser);
19416
19417   /* Get the deferred access checks from the parameter list.  These
19418      will be checked once we know what is being declared, as for a
19419      member template the checks must be performed in the scope of the
19420      class containing the member.  */
19421   checks = get_deferred_access_checks ();
19422
19423   /* Look for the `>'.  */
19424   cp_parser_skip_to_end_of_template_parameter_list (parser);
19425   /* We just processed one more parameter list.  */
19426   ++parser->num_template_parameter_lists;
19427   /* If the next token is `template', there are more template
19428      parameters.  */
19429   if (cp_lexer_next_token_is_keyword (parser->lexer,
19430                                       RID_TEMPLATE))
19431     cp_parser_template_declaration_after_export (parser, member_p);
19432   else
19433     {
19434       /* There are no access checks when parsing a template, as we do not
19435          know if a specialization will be a friend.  */
19436       push_deferring_access_checks (dk_no_check);
19437       token = cp_lexer_peek_token (parser->lexer);
19438       decl = cp_parser_single_declaration (parser,
19439                                            checks,
19440                                            member_p,
19441                                            /*explicit_specialization_p=*/false,
19442                                            &friend_p);
19443       pop_deferring_access_checks ();
19444
19445       /* If this is a member template declaration, let the front
19446          end know.  */
19447       if (member_p && !friend_p && decl)
19448         {
19449           if (TREE_CODE (decl) == TYPE_DECL)
19450             cp_parser_check_access_in_redeclaration (decl, token->location);
19451
19452           decl = finish_member_template_decl (decl);
19453         }
19454       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19455         make_friend_class (current_class_type, TREE_TYPE (decl),
19456                            /*complain=*/true);
19457     }
19458   /* We are done with the current parameter list.  */
19459   --parser->num_template_parameter_lists;
19460
19461   pop_deferring_access_checks ();
19462
19463   /* Finish up.  */
19464   finish_template_decl (parameter_list);
19465
19466   /* Register member declarations.  */
19467   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19468     finish_member_declaration (decl);
19469   /* For the erroneous case of a template with C linkage, we pushed an
19470      implicit C++ linkage scope; exit that scope now.  */
19471   if (need_lang_pop)
19472     pop_lang_context ();
19473   /* If DECL is a function template, we must return to parse it later.
19474      (Even though there is no definition, there might be default
19475      arguments that need handling.)  */
19476   if (member_p && decl
19477       && (TREE_CODE (decl) == FUNCTION_DECL
19478           || DECL_FUNCTION_TEMPLATE_P (decl)))
19479     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19480 }
19481
19482 /* Perform the deferred access checks from a template-parameter-list.
19483    CHECKS is a TREE_LIST of access checks, as returned by
19484    get_deferred_access_checks.  */
19485
19486 static void
19487 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19488 {
19489   ++processing_template_parmlist;
19490   perform_access_checks (checks);
19491   --processing_template_parmlist;
19492 }
19493
19494 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19495    `function-definition' sequence.  MEMBER_P is true, this declaration
19496    appears in a class scope.
19497
19498    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
19499    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
19500
19501 static tree
19502 cp_parser_single_declaration (cp_parser* parser,
19503                               VEC (deferred_access_check,gc)* checks,
19504                               bool member_p,
19505                               bool explicit_specialization_p,
19506                               bool* friend_p)
19507 {
19508   int declares_class_or_enum;
19509   tree decl = NULL_TREE;
19510   cp_decl_specifier_seq decl_specifiers;
19511   bool function_definition_p = false;
19512   cp_token *decl_spec_token_start;
19513
19514   /* This function is only used when processing a template
19515      declaration.  */
19516   gcc_assert (innermost_scope_kind () == sk_template_parms
19517               || innermost_scope_kind () == sk_template_spec);
19518
19519   /* Defer access checks until we know what is being declared.  */
19520   push_deferring_access_checks (dk_deferred);
19521
19522   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19523      alternative.  */
19524   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19525   cp_parser_decl_specifier_seq (parser,
19526                                 CP_PARSER_FLAGS_OPTIONAL,
19527                                 &decl_specifiers,
19528                                 &declares_class_or_enum);
19529   if (friend_p)
19530     *friend_p = cp_parser_friend_p (&decl_specifiers);
19531
19532   /* There are no template typedefs.  */
19533   if (decl_specifiers.specs[(int) ds_typedef])
19534     {
19535       error_at (decl_spec_token_start->location,
19536                 "template declaration of %<typedef%>");
19537       decl = error_mark_node;
19538     }
19539
19540   /* Gather up the access checks that occurred the
19541      decl-specifier-seq.  */
19542   stop_deferring_access_checks ();
19543
19544   /* Check for the declaration of a template class.  */
19545   if (declares_class_or_enum)
19546     {
19547       if (cp_parser_declares_only_class_p (parser))
19548         {
19549           decl = shadow_tag (&decl_specifiers);
19550
19551           /* In this case:
19552
19553                struct C {
19554                  friend template <typename T> struct A<T>::B;
19555                };
19556
19557              A<T>::B will be represented by a TYPENAME_TYPE, and
19558              therefore not recognized by shadow_tag.  */
19559           if (friend_p && *friend_p
19560               && !decl
19561               && decl_specifiers.type
19562               && TYPE_P (decl_specifiers.type))
19563             decl = decl_specifiers.type;
19564
19565           if (decl && decl != error_mark_node)
19566             decl = TYPE_NAME (decl);
19567           else
19568             decl = error_mark_node;
19569
19570           /* Perform access checks for template parameters.  */
19571           cp_parser_perform_template_parameter_access_checks (checks);
19572         }
19573     }
19574
19575   /* Complain about missing 'typename' or other invalid type names.  */
19576   if (!decl_specifiers.any_type_specifiers_p)
19577     cp_parser_parse_and_diagnose_invalid_type_name (parser);
19578
19579   /* If it's not a template class, try for a template function.  If
19580      the next token is a `;', then this declaration does not declare
19581      anything.  But, if there were errors in the decl-specifiers, then
19582      the error might well have come from an attempted class-specifier.
19583      In that case, there's no need to warn about a missing declarator.  */
19584   if (!decl
19585       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19586           || decl_specifiers.type != error_mark_node))
19587     {
19588       decl = cp_parser_init_declarator (parser,
19589                                         &decl_specifiers,
19590                                         checks,
19591                                         /*function_definition_allowed_p=*/true,
19592                                         member_p,
19593                                         declares_class_or_enum,
19594                                         &function_definition_p);
19595
19596     /* 7.1.1-1 [dcl.stc]
19597
19598        A storage-class-specifier shall not be specified in an explicit
19599        specialization...  */
19600     if (decl
19601         && explicit_specialization_p
19602         && decl_specifiers.storage_class != sc_none)
19603       {
19604         error_at (decl_spec_token_start->location,
19605                   "explicit template specialization cannot have a storage class");
19606         decl = error_mark_node;
19607       }
19608     }
19609
19610   pop_deferring_access_checks ();
19611
19612   /* Clear any current qualification; whatever comes next is the start
19613      of something new.  */
19614   parser->scope = NULL_TREE;
19615   parser->qualifying_scope = NULL_TREE;
19616   parser->object_scope = NULL_TREE;
19617   /* Look for a trailing `;' after the declaration.  */
19618   if (!function_definition_p
19619       && (decl == error_mark_node
19620           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19621     cp_parser_skip_to_end_of_block_or_statement (parser);
19622
19623   return decl;
19624 }
19625
19626 /* Parse a cast-expression that is not the operand of a unary "&".  */
19627
19628 static tree
19629 cp_parser_simple_cast_expression (cp_parser *parser)
19630 {
19631   return cp_parser_cast_expression (parser, /*address_p=*/false,
19632                                     /*cast_p=*/false, NULL);
19633 }
19634
19635 /* Parse a functional cast to TYPE.  Returns an expression
19636    representing the cast.  */
19637
19638 static tree
19639 cp_parser_functional_cast (cp_parser* parser, tree type)
19640 {
19641   VEC(tree,gc) *vec;
19642   tree expression_list;
19643   tree cast;
19644   bool nonconst_p;
19645
19646   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19647     {
19648       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19649       expression_list = cp_parser_braced_list (parser, &nonconst_p);
19650       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19651       if (TREE_CODE (type) == TYPE_DECL)
19652         type = TREE_TYPE (type);
19653       return finish_compound_literal (type, expression_list);
19654     }
19655
19656
19657   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19658                                                  /*cast_p=*/true,
19659                                                  /*allow_expansion_p=*/true,
19660                                                  /*non_constant_p=*/NULL);
19661   if (vec == NULL)
19662     expression_list = error_mark_node;
19663   else
19664     {
19665       expression_list = build_tree_list_vec (vec);
19666       release_tree_vector (vec);
19667     }
19668
19669   cast = build_functional_cast (type, expression_list,
19670                                 tf_warning_or_error);
19671   /* [expr.const]/1: In an integral constant expression "only type
19672      conversions to integral or enumeration type can be used".  */
19673   if (TREE_CODE (type) == TYPE_DECL)
19674     type = TREE_TYPE (type);
19675   if (cast != error_mark_node
19676       && !cast_valid_in_integral_constant_expression_p (type)
19677       && cp_parser_non_integral_constant_expression (parser,
19678                                                      NIC_CONSTRUCTOR))
19679     return error_mark_node;
19680   return cast;
19681 }
19682
19683 /* Save the tokens that make up the body of a member function defined
19684    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
19685    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
19686    specifiers applied to the declaration.  Returns the FUNCTION_DECL
19687    for the member function.  */
19688
19689 static tree
19690 cp_parser_save_member_function_body (cp_parser* parser,
19691                                      cp_decl_specifier_seq *decl_specifiers,
19692                                      cp_declarator *declarator,
19693                                      tree attributes)
19694 {
19695   cp_token *first;
19696   cp_token *last;
19697   tree fn;
19698
19699   /* Create the FUNCTION_DECL.  */
19700   fn = grokmethod (decl_specifiers, declarator, attributes);
19701   /* If something went badly wrong, bail out now.  */
19702   if (fn == error_mark_node)
19703     {
19704       /* If there's a function-body, skip it.  */
19705       if (cp_parser_token_starts_function_definition_p
19706           (cp_lexer_peek_token (parser->lexer)))
19707         cp_parser_skip_to_end_of_block_or_statement (parser);
19708       return error_mark_node;
19709     }
19710
19711   /* Remember it, if there default args to post process.  */
19712   cp_parser_save_default_args (parser, fn);
19713
19714   /* Save away the tokens that make up the body of the
19715      function.  */
19716   first = parser->lexer->next_token;
19717   /* We can have braced-init-list mem-initializers before the fn body.  */
19718   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19719     {
19720       cp_lexer_consume_token (parser->lexer);
19721       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19722              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19723         {
19724           /* cache_group will stop after an un-nested { } pair, too.  */
19725           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19726             break;
19727
19728           /* variadic mem-inits have ... after the ')'.  */
19729           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19730             cp_lexer_consume_token (parser->lexer);
19731         }
19732     }
19733   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19734   /* Handle function try blocks.  */
19735   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19736     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19737   last = parser->lexer->next_token;
19738
19739   /* Save away the inline definition; we will process it when the
19740      class is complete.  */
19741   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19742   DECL_PENDING_INLINE_P (fn) = 1;
19743
19744   /* We need to know that this was defined in the class, so that
19745      friend templates are handled correctly.  */
19746   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19747
19748   /* Add FN to the queue of functions to be parsed later.  */
19749   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
19750
19751   return fn;
19752 }
19753
19754 /* Parse a template-argument-list, as well as the trailing ">" (but
19755    not the opening ">").  See cp_parser_template_argument_list for the
19756    return value.  */
19757
19758 static tree
19759 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19760 {
19761   tree arguments;
19762   tree saved_scope;
19763   tree saved_qualifying_scope;
19764   tree saved_object_scope;
19765   bool saved_greater_than_is_operator_p;
19766   int saved_unevaluated_operand;
19767   int saved_inhibit_evaluation_warnings;
19768
19769   /* [temp.names]
19770
19771      When parsing a template-id, the first non-nested `>' is taken as
19772      the end of the template-argument-list rather than a greater-than
19773      operator.  */
19774   saved_greater_than_is_operator_p
19775     = parser->greater_than_is_operator_p;
19776   parser->greater_than_is_operator_p = false;
19777   /* Parsing the argument list may modify SCOPE, so we save it
19778      here.  */
19779   saved_scope = parser->scope;
19780   saved_qualifying_scope = parser->qualifying_scope;
19781   saved_object_scope = parser->object_scope;
19782   /* We need to evaluate the template arguments, even though this
19783      template-id may be nested within a "sizeof".  */
19784   saved_unevaluated_operand = cp_unevaluated_operand;
19785   cp_unevaluated_operand = 0;
19786   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19787   c_inhibit_evaluation_warnings = 0;
19788   /* Parse the template-argument-list itself.  */
19789   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19790       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19791     arguments = NULL_TREE;
19792   else
19793     arguments = cp_parser_template_argument_list (parser);
19794   /* Look for the `>' that ends the template-argument-list. If we find
19795      a '>>' instead, it's probably just a typo.  */
19796   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19797     {
19798       if (cxx_dialect != cxx98)
19799         {
19800           /* In C++0x, a `>>' in a template argument list or cast
19801              expression is considered to be two separate `>'
19802              tokens. So, change the current token to a `>', but don't
19803              consume it: it will be consumed later when the outer
19804              template argument list (or cast expression) is parsed.
19805              Note that this replacement of `>' for `>>' is necessary
19806              even if we are parsing tentatively: in the tentative
19807              case, after calling
19808              cp_parser_enclosed_template_argument_list we will always
19809              throw away all of the template arguments and the first
19810              closing `>', either because the template argument list
19811              was erroneous or because we are replacing those tokens
19812              with a CPP_TEMPLATE_ID token.  The second `>' (which will
19813              not have been thrown away) is needed either to close an
19814              outer template argument list or to complete a new-style
19815              cast.  */
19816           cp_token *token = cp_lexer_peek_token (parser->lexer);
19817           token->type = CPP_GREATER;
19818         }
19819       else if (!saved_greater_than_is_operator_p)
19820         {
19821           /* If we're in a nested template argument list, the '>>' has
19822             to be a typo for '> >'. We emit the error message, but we
19823             continue parsing and we push a '>' as next token, so that
19824             the argument list will be parsed correctly.  Note that the
19825             global source location is still on the token before the
19826             '>>', so we need to say explicitly where we want it.  */
19827           cp_token *token = cp_lexer_peek_token (parser->lexer);
19828           error_at (token->location, "%<>>%> should be %<> >%> "
19829                     "within a nested template argument list");
19830
19831           token->type = CPP_GREATER;
19832         }
19833       else
19834         {
19835           /* If this is not a nested template argument list, the '>>'
19836             is a typo for '>'. Emit an error message and continue.
19837             Same deal about the token location, but here we can get it
19838             right by consuming the '>>' before issuing the diagnostic.  */
19839           cp_token *token = cp_lexer_consume_token (parser->lexer);
19840           error_at (token->location,
19841                     "spurious %<>>%>, use %<>%> to terminate "
19842                     "a template argument list");
19843         }
19844     }
19845   else
19846     cp_parser_skip_to_end_of_template_parameter_list (parser);
19847   /* The `>' token might be a greater-than operator again now.  */
19848   parser->greater_than_is_operator_p
19849     = saved_greater_than_is_operator_p;
19850   /* Restore the SAVED_SCOPE.  */
19851   parser->scope = saved_scope;
19852   parser->qualifying_scope = saved_qualifying_scope;
19853   parser->object_scope = saved_object_scope;
19854   cp_unevaluated_operand = saved_unevaluated_operand;
19855   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19856
19857   return arguments;
19858 }
19859
19860 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19861    arguments, or the body of the function have not yet been parsed,
19862    parse them now.  */
19863
19864 static void
19865 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19866 {
19867   /* If this member is a template, get the underlying
19868      FUNCTION_DECL.  */
19869   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19870     member_function = DECL_TEMPLATE_RESULT (member_function);
19871
19872   /* There should not be any class definitions in progress at this
19873      point; the bodies of members are only parsed outside of all class
19874      definitions.  */
19875   gcc_assert (parser->num_classes_being_defined == 0);
19876   /* While we're parsing the member functions we might encounter more
19877      classes.  We want to handle them right away, but we don't want
19878      them getting mixed up with functions that are currently in the
19879      queue.  */
19880   push_unparsed_function_queues (parser);
19881
19882   /* Make sure that any template parameters are in scope.  */
19883   maybe_begin_member_template_processing (member_function);
19884
19885   /* If the body of the function has not yet been parsed, parse it
19886      now.  */
19887   if (DECL_PENDING_INLINE_P (member_function))
19888     {
19889       tree function_scope;
19890       cp_token_cache *tokens;
19891
19892       /* The function is no longer pending; we are processing it.  */
19893       tokens = DECL_PENDING_INLINE_INFO (member_function);
19894       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19895       DECL_PENDING_INLINE_P (member_function) = 0;
19896
19897       /* If this is a local class, enter the scope of the containing
19898          function.  */
19899       function_scope = current_function_decl;
19900       if (function_scope)
19901         push_function_context ();
19902
19903       /* Push the body of the function onto the lexer stack.  */
19904       cp_parser_push_lexer_for_tokens (parser, tokens);
19905
19906       /* Let the front end know that we going to be defining this
19907          function.  */
19908       start_preparsed_function (member_function, NULL_TREE,
19909                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19910
19911       /* Don't do access checking if it is a templated function.  */
19912       if (processing_template_decl)
19913         push_deferring_access_checks (dk_no_check);
19914
19915       /* Now, parse the body of the function.  */
19916       cp_parser_function_definition_after_declarator (parser,
19917                                                       /*inline_p=*/true);
19918
19919       if (processing_template_decl)
19920         pop_deferring_access_checks ();
19921
19922       /* Leave the scope of the containing function.  */
19923       if (function_scope)
19924         pop_function_context ();
19925       cp_parser_pop_lexer (parser);
19926     }
19927
19928   /* Remove any template parameters from the symbol table.  */
19929   maybe_end_member_template_processing ();
19930
19931   /* Restore the queue.  */
19932   pop_unparsed_function_queues (parser);
19933 }
19934
19935 /* If DECL contains any default args, remember it on the unparsed
19936    functions queue.  */
19937
19938 static void
19939 cp_parser_save_default_args (cp_parser* parser, tree decl)
19940 {
19941   tree probe;
19942
19943   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19944        probe;
19945        probe = TREE_CHAIN (probe))
19946     if (TREE_PURPOSE (probe))
19947       {
19948         cp_default_arg_entry *entry
19949           = VEC_safe_push (cp_default_arg_entry, gc,
19950                            unparsed_funs_with_default_args, NULL);
19951         entry->class_type = current_class_type;
19952         entry->decl = decl;
19953         break;
19954       }
19955 }
19956
19957 /* FN is a FUNCTION_DECL which may contains a parameter with an
19958    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19959    assumes that the current scope is the scope in which the default
19960    argument should be processed.  */
19961
19962 static void
19963 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19964 {
19965   bool saved_local_variables_forbidden_p;
19966   tree parm, parmdecl;
19967
19968   /* While we're parsing the default args, we might (due to the
19969      statement expression extension) encounter more classes.  We want
19970      to handle them right away, but we don't want them getting mixed
19971      up with default args that are currently in the queue.  */
19972   push_unparsed_function_queues (parser);
19973
19974   /* Local variable names (and the `this' keyword) may not appear
19975      in a default argument.  */
19976   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19977   parser->local_variables_forbidden_p = true;
19978
19979   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19980          parmdecl = DECL_ARGUMENTS (fn);
19981        parm && parm != void_list_node;
19982        parm = TREE_CHAIN (parm),
19983          parmdecl = DECL_CHAIN (parmdecl))
19984     {
19985       cp_token_cache *tokens;
19986       tree default_arg = TREE_PURPOSE (parm);
19987       tree parsed_arg;
19988       VEC(tree,gc) *insts;
19989       tree copy;
19990       unsigned ix;
19991
19992       if (!default_arg)
19993         continue;
19994
19995       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19996         /* This can happen for a friend declaration for a function
19997            already declared with default arguments.  */
19998         continue;
19999
20000        /* Push the saved tokens for the default argument onto the parser's
20001           lexer stack.  */
20002       tokens = DEFARG_TOKENS (default_arg);
20003       cp_parser_push_lexer_for_tokens (parser, tokens);
20004
20005       start_lambda_scope (parmdecl);
20006
20007       /* Parse the assignment-expression.  */
20008       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20009       if (parsed_arg == error_mark_node)
20010         {
20011           cp_parser_pop_lexer (parser);
20012           continue;
20013         }
20014
20015       if (!processing_template_decl)
20016         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20017
20018       TREE_PURPOSE (parm) = parsed_arg;
20019
20020       /* Update any instantiations we've already created.  */
20021       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20022            VEC_iterate (tree, insts, ix, copy); ix++)
20023         TREE_PURPOSE (copy) = parsed_arg;
20024
20025       finish_lambda_scope ();
20026
20027       /* If the token stream has not been completely used up, then
20028          there was extra junk after the end of the default
20029          argument.  */
20030       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20031         cp_parser_error (parser, "expected %<,%>");
20032
20033       /* Revert to the main lexer.  */
20034       cp_parser_pop_lexer (parser);
20035     }
20036
20037   /* Make sure no default arg is missing.  */
20038   check_default_args (fn);
20039
20040   /* Restore the state of local_variables_forbidden_p.  */
20041   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20042
20043   /* Restore the queue.  */
20044   pop_unparsed_function_queues (parser);
20045 }
20046
20047 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20048    either a TYPE or an expression, depending on the form of the
20049    input.  The KEYWORD indicates which kind of expression we have
20050    encountered.  */
20051
20052 static tree
20053 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20054 {
20055   tree expr = NULL_TREE;
20056   const char *saved_message;
20057   char *tmp;
20058   bool saved_integral_constant_expression_p;
20059   bool saved_non_integral_constant_expression_p;
20060   bool pack_expansion_p = false;
20061
20062   /* Types cannot be defined in a `sizeof' expression.  Save away the
20063      old message.  */
20064   saved_message = parser->type_definition_forbidden_message;
20065   /* And create the new one.  */
20066   tmp = concat ("types may not be defined in %<",
20067                 IDENTIFIER_POINTER (ridpointers[keyword]),
20068                 "%> expressions", NULL);
20069   parser->type_definition_forbidden_message = tmp;
20070
20071   /* The restrictions on constant-expressions do not apply inside
20072      sizeof expressions.  */
20073   saved_integral_constant_expression_p
20074     = parser->integral_constant_expression_p;
20075   saved_non_integral_constant_expression_p
20076     = parser->non_integral_constant_expression_p;
20077   parser->integral_constant_expression_p = false;
20078
20079   /* If it's a `...', then we are computing the length of a parameter
20080      pack.  */
20081   if (keyword == RID_SIZEOF
20082       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20083     {
20084       /* Consume the `...'.  */
20085       cp_lexer_consume_token (parser->lexer);
20086       maybe_warn_variadic_templates ();
20087
20088       /* Note that this is an expansion.  */
20089       pack_expansion_p = true;
20090     }
20091
20092   /* Do not actually evaluate the expression.  */
20093   ++cp_unevaluated_operand;
20094   ++c_inhibit_evaluation_warnings;
20095   /* If it's a `(', then we might be looking at the type-id
20096      construction.  */
20097   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20098     {
20099       tree type;
20100       bool saved_in_type_id_in_expr_p;
20101
20102       /* We can't be sure yet whether we're looking at a type-id or an
20103          expression.  */
20104       cp_parser_parse_tentatively (parser);
20105       /* Consume the `('.  */
20106       cp_lexer_consume_token (parser->lexer);
20107       /* Parse the type-id.  */
20108       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20109       parser->in_type_id_in_expr_p = true;
20110       type = cp_parser_type_id (parser);
20111       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20112       /* Now, look for the trailing `)'.  */
20113       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20114       /* If all went well, then we're done.  */
20115       if (cp_parser_parse_definitely (parser))
20116         {
20117           cp_decl_specifier_seq decl_specs;
20118
20119           /* Build a trivial decl-specifier-seq.  */
20120           clear_decl_specs (&decl_specs);
20121           decl_specs.type = type;
20122
20123           /* Call grokdeclarator to figure out what type this is.  */
20124           expr = grokdeclarator (NULL,
20125                                  &decl_specs,
20126                                  TYPENAME,
20127                                  /*initialized=*/0,
20128                                  /*attrlist=*/NULL);
20129         }
20130     }
20131
20132   /* If the type-id production did not work out, then we must be
20133      looking at the unary-expression production.  */
20134   if (!expr)
20135     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20136                                        /*cast_p=*/false, NULL);
20137
20138   if (pack_expansion_p)
20139     /* Build a pack expansion. */
20140     expr = make_pack_expansion (expr);
20141
20142   /* Go back to evaluating expressions.  */
20143   --cp_unevaluated_operand;
20144   --c_inhibit_evaluation_warnings;
20145
20146   /* Free the message we created.  */
20147   free (tmp);
20148   /* And restore the old one.  */
20149   parser->type_definition_forbidden_message = saved_message;
20150   parser->integral_constant_expression_p
20151     = saved_integral_constant_expression_p;
20152   parser->non_integral_constant_expression_p
20153     = saved_non_integral_constant_expression_p;
20154
20155   return expr;
20156 }
20157
20158 /* If the current declaration has no declarator, return true.  */
20159
20160 static bool
20161 cp_parser_declares_only_class_p (cp_parser *parser)
20162 {
20163   /* If the next token is a `;' or a `,' then there is no
20164      declarator.  */
20165   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20166           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20167 }
20168
20169 /* Update the DECL_SPECS to reflect the storage class indicated by
20170    KEYWORD.  */
20171
20172 static void
20173 cp_parser_set_storage_class (cp_parser *parser,
20174                              cp_decl_specifier_seq *decl_specs,
20175                              enum rid keyword,
20176                              location_t location)
20177 {
20178   cp_storage_class storage_class;
20179
20180   if (parser->in_unbraced_linkage_specification_p)
20181     {
20182       error_at (location, "invalid use of %qD in linkage specification",
20183                 ridpointers[keyword]);
20184       return;
20185     }
20186   else if (decl_specs->storage_class != sc_none)
20187     {
20188       decl_specs->conflicting_specifiers_p = true;
20189       return;
20190     }
20191
20192   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20193       && decl_specs->specs[(int) ds_thread])
20194     {
20195       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20196       decl_specs->specs[(int) ds_thread] = 0;
20197     }
20198
20199   switch (keyword)
20200     {
20201     case RID_AUTO:
20202       storage_class = sc_auto;
20203       break;
20204     case RID_REGISTER:
20205       storage_class = sc_register;
20206       break;
20207     case RID_STATIC:
20208       storage_class = sc_static;
20209       break;
20210     case RID_EXTERN:
20211       storage_class = sc_extern;
20212       break;
20213     case RID_MUTABLE:
20214       storage_class = sc_mutable;
20215       break;
20216     default:
20217       gcc_unreachable ();
20218     }
20219   decl_specs->storage_class = storage_class;
20220
20221   /* A storage class specifier cannot be applied alongside a typedef 
20222      specifier. If there is a typedef specifier present then set 
20223      conflicting_specifiers_p which will trigger an error later
20224      on in grokdeclarator. */
20225   if (decl_specs->specs[(int)ds_typedef])
20226     decl_specs->conflicting_specifiers_p = true;
20227 }
20228
20229 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20230    is true, the type is a user-defined type; otherwise it is a
20231    built-in type specified by a keyword.  */
20232
20233 static void
20234 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20235                               tree type_spec,
20236                               location_t location,
20237                               bool user_defined_p)
20238 {
20239   decl_specs->any_specifiers_p = true;
20240
20241   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20242      (with, for example, in "typedef int wchar_t;") we remember that
20243      this is what happened.  In system headers, we ignore these
20244      declarations so that G++ can work with system headers that are not
20245      C++-safe.  */
20246   if (decl_specs->specs[(int) ds_typedef]
20247       && !user_defined_p
20248       && (type_spec == boolean_type_node
20249           || type_spec == char16_type_node
20250           || type_spec == char32_type_node
20251           || type_spec == wchar_type_node)
20252       && (decl_specs->type
20253           || decl_specs->specs[(int) ds_long]
20254           || decl_specs->specs[(int) ds_short]
20255           || decl_specs->specs[(int) ds_unsigned]
20256           || decl_specs->specs[(int) ds_signed]))
20257     {
20258       decl_specs->redefined_builtin_type = type_spec;
20259       if (!decl_specs->type)
20260         {
20261           decl_specs->type = type_spec;
20262           decl_specs->user_defined_type_p = false;
20263           decl_specs->type_location = location;
20264         }
20265     }
20266   else if (decl_specs->type)
20267     decl_specs->multiple_types_p = true;
20268   else
20269     {
20270       decl_specs->type = type_spec;
20271       decl_specs->user_defined_type_p = user_defined_p;
20272       decl_specs->redefined_builtin_type = NULL_TREE;
20273       decl_specs->type_location = location;
20274     }
20275 }
20276
20277 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20278    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20279
20280 static bool
20281 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20282 {
20283   return decl_specifiers->specs[(int) ds_friend] != 0;
20284 }
20285
20286 /* Issue an error message indicating that TOKEN_DESC was expected.
20287    If KEYWORD is true, it indicated this function is called by
20288    cp_parser_require_keword and the required token can only be
20289    a indicated keyword. */
20290
20291 static void
20292 cp_parser_required_error (cp_parser *parser,
20293                           required_token token_desc,
20294                           bool keyword)
20295 {
20296   switch (token_desc)
20297     {
20298       case RT_NEW:
20299         cp_parser_error (parser, "expected %<new%>");
20300         return;
20301       case RT_DELETE:
20302         cp_parser_error (parser, "expected %<delete%>");
20303         return;
20304       case RT_RETURN:
20305         cp_parser_error (parser, "expected %<return%>");
20306         return;
20307       case RT_WHILE:
20308         cp_parser_error (parser, "expected %<while%>");
20309         return;
20310       case RT_EXTERN:
20311         cp_parser_error (parser, "expected %<extern%>");
20312         return;
20313       case RT_STATIC_ASSERT:
20314         cp_parser_error (parser, "expected %<static_assert%>");
20315         return;
20316       case RT_DECLTYPE:
20317         cp_parser_error (parser, "expected %<decltype%>");
20318         return;
20319       case RT_OPERATOR:
20320         cp_parser_error (parser, "expected %<operator%>");
20321         return;
20322       case RT_CLASS:
20323         cp_parser_error (parser, "expected %<class%>");
20324         return;
20325       case RT_TEMPLATE:
20326         cp_parser_error (parser, "expected %<template%>");
20327         return;
20328       case RT_NAMESPACE:
20329         cp_parser_error (parser, "expected %<namespace%>");
20330         return;
20331       case RT_USING:
20332         cp_parser_error (parser, "expected %<using%>");
20333         return;
20334       case RT_ASM:
20335         cp_parser_error (parser, "expected %<asm%>");
20336         return;
20337       case RT_TRY:
20338         cp_parser_error (parser, "expected %<try%>");
20339         return;
20340       case RT_CATCH:
20341         cp_parser_error (parser, "expected %<catch%>");
20342         return;
20343       case RT_THROW:
20344         cp_parser_error (parser, "expected %<throw%>");
20345         return;
20346       case RT_LABEL:
20347         cp_parser_error (parser, "expected %<__label__%>");
20348         return;
20349       case RT_AT_TRY:
20350         cp_parser_error (parser, "expected %<@try%>");
20351         return;
20352       case RT_AT_SYNCHRONIZED:
20353         cp_parser_error (parser, "expected %<@synchronized%>");
20354         return;
20355       case RT_AT_THROW:
20356         cp_parser_error (parser, "expected %<@throw%>");
20357         return;
20358       default:
20359         break;
20360     }
20361   if (!keyword)
20362     {
20363       switch (token_desc)
20364         {
20365           case RT_SEMICOLON:
20366             cp_parser_error (parser, "expected %<;%>");
20367             return;
20368           case RT_OPEN_PAREN:
20369             cp_parser_error (parser, "expected %<(%>");
20370             return;
20371           case RT_CLOSE_BRACE:
20372             cp_parser_error (parser, "expected %<}%>");
20373             return;
20374           case RT_OPEN_BRACE:
20375             cp_parser_error (parser, "expected %<{%>");
20376             return;
20377           case RT_CLOSE_SQUARE:
20378             cp_parser_error (parser, "expected %<]%>");
20379             return;
20380           case RT_OPEN_SQUARE:
20381             cp_parser_error (parser, "expected %<[%>");
20382             return;
20383           case RT_COMMA:
20384             cp_parser_error (parser, "expected %<,%>");
20385             return;
20386           case RT_SCOPE:
20387             cp_parser_error (parser, "expected %<::%>");
20388             return;
20389           case RT_LESS:
20390             cp_parser_error (parser, "expected %<<%>");
20391             return;
20392           case RT_GREATER:
20393             cp_parser_error (parser, "expected %<>%>");
20394             return;
20395           case RT_EQ:
20396             cp_parser_error (parser, "expected %<=%>");
20397             return;
20398           case RT_ELLIPSIS:
20399             cp_parser_error (parser, "expected %<...%>");
20400             return;
20401           case RT_MULT:
20402             cp_parser_error (parser, "expected %<*%>");
20403             return;
20404           case RT_COMPL:
20405             cp_parser_error (parser, "expected %<~%>");
20406             return;
20407           case RT_COLON:
20408             cp_parser_error (parser, "expected %<:%>");
20409             return;
20410           case RT_COLON_SCOPE:
20411             cp_parser_error (parser, "expected %<:%> or %<::%>");
20412             return;
20413           case RT_CLOSE_PAREN:
20414             cp_parser_error (parser, "expected %<)%>");
20415             return;
20416           case RT_COMMA_CLOSE_PAREN:
20417             cp_parser_error (parser, "expected %<,%> or %<)%>");
20418             return;
20419           case RT_PRAGMA_EOL:
20420             cp_parser_error (parser, "expected end of line");
20421             return;
20422           case RT_NAME:
20423             cp_parser_error (parser, "expected identifier");
20424             return;
20425           case RT_SELECT:
20426             cp_parser_error (parser, "expected selection-statement");
20427             return;
20428           case RT_INTERATION:
20429             cp_parser_error (parser, "expected iteration-statement");
20430             return;
20431           case RT_JUMP:
20432             cp_parser_error (parser, "expected jump-statement");
20433             return;
20434           case RT_CLASS_KEY:
20435             cp_parser_error (parser, "expected class-key");
20436             return;
20437           case RT_CLASS_TYPENAME_TEMPLATE:
20438             cp_parser_error (parser,
20439                  "expected %<class%>, %<typename%>, or %<template%>");
20440             return;
20441           default:
20442             gcc_unreachable ();
20443         }
20444     }
20445   else
20446     gcc_unreachable ();
20447 }
20448
20449
20450
20451 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20452    issue an error message indicating that TOKEN_DESC was expected.
20453
20454    Returns the token consumed, if the token had the appropriate type.
20455    Otherwise, returns NULL.  */
20456
20457 static cp_token *
20458 cp_parser_require (cp_parser* parser,
20459                    enum cpp_ttype type,
20460                    required_token token_desc)
20461 {
20462   if (cp_lexer_next_token_is (parser->lexer, type))
20463     return cp_lexer_consume_token (parser->lexer);
20464   else
20465     {
20466       /* Output the MESSAGE -- unless we're parsing tentatively.  */
20467       if (!cp_parser_simulate_error (parser))
20468         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20469       return NULL;
20470     }
20471 }
20472
20473 /* An error message is produced if the next token is not '>'.
20474    All further tokens are skipped until the desired token is
20475    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
20476
20477 static void
20478 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20479 {
20480   /* Current level of '< ... >'.  */
20481   unsigned level = 0;
20482   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
20483   unsigned nesting_depth = 0;
20484
20485   /* Are we ready, yet?  If not, issue error message.  */
20486   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20487     return;
20488
20489   /* Skip tokens until the desired token is found.  */
20490   while (true)
20491     {
20492       /* Peek at the next token.  */
20493       switch (cp_lexer_peek_token (parser->lexer)->type)
20494         {
20495         case CPP_LESS:
20496           if (!nesting_depth)
20497             ++level;
20498           break;
20499
20500         case CPP_RSHIFT:
20501           if (cxx_dialect == cxx98)
20502             /* C++0x views the `>>' operator as two `>' tokens, but
20503                C++98 does not. */
20504             break;
20505           else if (!nesting_depth && level-- == 0)
20506             {
20507               /* We've hit a `>>' where the first `>' closes the
20508                  template argument list, and the second `>' is
20509                  spurious.  Just consume the `>>' and stop; we've
20510                  already produced at least one error.  */
20511               cp_lexer_consume_token (parser->lexer);
20512               return;
20513             }
20514           /* Fall through for C++0x, so we handle the second `>' in
20515              the `>>'.  */
20516
20517         case CPP_GREATER:
20518           if (!nesting_depth && level-- == 0)
20519             {
20520               /* We've reached the token we want, consume it and stop.  */
20521               cp_lexer_consume_token (parser->lexer);
20522               return;
20523             }
20524           break;
20525
20526         case CPP_OPEN_PAREN:
20527         case CPP_OPEN_SQUARE:
20528           ++nesting_depth;
20529           break;
20530
20531         case CPP_CLOSE_PAREN:
20532         case CPP_CLOSE_SQUARE:
20533           if (nesting_depth-- == 0)
20534             return;
20535           break;
20536
20537         case CPP_EOF:
20538         case CPP_PRAGMA_EOL:
20539         case CPP_SEMICOLON:
20540         case CPP_OPEN_BRACE:
20541         case CPP_CLOSE_BRACE:
20542           /* The '>' was probably forgotten, don't look further.  */
20543           return;
20544
20545         default:
20546           break;
20547         }
20548
20549       /* Consume this token.  */
20550       cp_lexer_consume_token (parser->lexer);
20551     }
20552 }
20553
20554 /* If the next token is the indicated keyword, consume it.  Otherwise,
20555    issue an error message indicating that TOKEN_DESC was expected.
20556
20557    Returns the token consumed, if the token had the appropriate type.
20558    Otherwise, returns NULL.  */
20559
20560 static cp_token *
20561 cp_parser_require_keyword (cp_parser* parser,
20562                            enum rid keyword,
20563                            required_token token_desc)
20564 {
20565   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20566
20567   if (token && token->keyword != keyword)
20568     {
20569       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
20570       return NULL;
20571     }
20572
20573   return token;
20574 }
20575
20576 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20577    function-definition.  */
20578
20579 static bool
20580 cp_parser_token_starts_function_definition_p (cp_token* token)
20581 {
20582   return (/* An ordinary function-body begins with an `{'.  */
20583           token->type == CPP_OPEN_BRACE
20584           /* A ctor-initializer begins with a `:'.  */
20585           || token->type == CPP_COLON
20586           /* A function-try-block begins with `try'.  */
20587           || token->keyword == RID_TRY
20588           /* The named return value extension begins with `return'.  */
20589           || token->keyword == RID_RETURN);
20590 }
20591
20592 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20593    definition.  */
20594
20595 static bool
20596 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20597 {
20598   cp_token *token;
20599
20600   token = cp_lexer_peek_token (parser->lexer);
20601   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20602 }
20603
20604 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20605    C++0x) ending a template-argument.  */
20606
20607 static bool
20608 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20609 {
20610   cp_token *token;
20611
20612   token = cp_lexer_peek_token (parser->lexer);
20613   return (token->type == CPP_COMMA 
20614           || token->type == CPP_GREATER
20615           || token->type == CPP_ELLIPSIS
20616           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20617 }
20618
20619 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20620    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
20621
20622 static bool
20623 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20624                                                      size_t n)
20625 {
20626   cp_token *token;
20627
20628   token = cp_lexer_peek_nth_token (parser->lexer, n);
20629   if (token->type == CPP_LESS)
20630     return true;
20631   /* Check for the sequence `<::' in the original code. It would be lexed as
20632      `[:', where `[' is a digraph, and there is no whitespace before
20633      `:'.  */
20634   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20635     {
20636       cp_token *token2;
20637       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20638       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20639         return true;
20640     }
20641   return false;
20642 }
20643
20644 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20645    or none_type otherwise.  */
20646
20647 static enum tag_types
20648 cp_parser_token_is_class_key (cp_token* token)
20649 {
20650   switch (token->keyword)
20651     {
20652     case RID_CLASS:
20653       return class_type;
20654     case RID_STRUCT:
20655       return record_type;
20656     case RID_UNION:
20657       return union_type;
20658
20659     default:
20660       return none_type;
20661     }
20662 }
20663
20664 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
20665
20666 static void
20667 cp_parser_check_class_key (enum tag_types class_key, tree type)
20668 {
20669   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20670     permerror (input_location, "%qs tag used in naming %q#T",
20671             class_key == union_type ? "union"
20672              : class_key == record_type ? "struct" : "class",
20673              type);
20674 }
20675
20676 /* Issue an error message if DECL is redeclared with different
20677    access than its original declaration [class.access.spec/3].
20678    This applies to nested classes and nested class templates.
20679    [class.mem/1].  */
20680
20681 static void
20682 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20683 {
20684   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20685     return;
20686
20687   if ((TREE_PRIVATE (decl)
20688        != (current_access_specifier == access_private_node))
20689       || (TREE_PROTECTED (decl)
20690           != (current_access_specifier == access_protected_node)))
20691     error_at (location, "%qD redeclared with different access", decl);
20692 }
20693
20694 /* Look for the `template' keyword, as a syntactic disambiguator.
20695    Return TRUE iff it is present, in which case it will be
20696    consumed.  */
20697
20698 static bool
20699 cp_parser_optional_template_keyword (cp_parser *parser)
20700 {
20701   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20702     {
20703       /* The `template' keyword can only be used within templates;
20704          outside templates the parser can always figure out what is a
20705          template and what is not.  */
20706       if (!processing_template_decl)
20707         {
20708           cp_token *token = cp_lexer_peek_token (parser->lexer);
20709           error_at (token->location,
20710                     "%<template%> (as a disambiguator) is only allowed "
20711                     "within templates");
20712           /* If this part of the token stream is rescanned, the same
20713              error message would be generated.  So, we purge the token
20714              from the stream.  */
20715           cp_lexer_purge_token (parser->lexer);
20716           return false;
20717         }
20718       else
20719         {
20720           /* Consume the `template' keyword.  */
20721           cp_lexer_consume_token (parser->lexer);
20722           return true;
20723         }
20724     }
20725
20726   return false;
20727 }
20728
20729 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
20730    set PARSER->SCOPE, and perform other related actions.  */
20731
20732 static void
20733 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20734 {
20735   int i;
20736   struct tree_check *check_value;
20737   deferred_access_check *chk;
20738   VEC (deferred_access_check,gc) *checks;
20739
20740   /* Get the stored value.  */
20741   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
20742   /* Perform any access checks that were deferred.  */
20743   checks = check_value->checks;
20744   if (checks)
20745     {
20746       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
20747         perform_or_defer_access_check (chk->binfo,
20748                                        chk->decl,
20749                                        chk->diag_decl);
20750     }
20751   /* Set the scope from the stored value.  */
20752   parser->scope = check_value->value;
20753   parser->qualifying_scope = check_value->qualifying_scope;
20754   parser->object_scope = NULL_TREE;
20755 }
20756
20757 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
20758    encounter the end of a block before what we were looking for.  */
20759
20760 static bool
20761 cp_parser_cache_group (cp_parser *parser,
20762                        enum cpp_ttype end,
20763                        unsigned depth)
20764 {
20765   while (true)
20766     {
20767       cp_token *token = cp_lexer_peek_token (parser->lexer);
20768
20769       /* Abort a parenthesized expression if we encounter a semicolon.  */
20770       if ((end == CPP_CLOSE_PAREN || depth == 0)
20771           && token->type == CPP_SEMICOLON)
20772         return true;
20773       /* If we've reached the end of the file, stop.  */
20774       if (token->type == CPP_EOF
20775           || (end != CPP_PRAGMA_EOL
20776               && token->type == CPP_PRAGMA_EOL))
20777         return true;
20778       if (token->type == CPP_CLOSE_BRACE && depth == 0)
20779         /* We've hit the end of an enclosing block, so there's been some
20780            kind of syntax error.  */
20781         return true;
20782
20783       /* Consume the token.  */
20784       cp_lexer_consume_token (parser->lexer);
20785       /* See if it starts a new group.  */
20786       if (token->type == CPP_OPEN_BRACE)
20787         {
20788           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
20789           /* In theory this should probably check end == '}', but
20790              cp_parser_save_member_function_body needs it to exit
20791              after either '}' or ')' when called with ')'.  */
20792           if (depth == 0)
20793             return false;
20794         }
20795       else if (token->type == CPP_OPEN_PAREN)
20796         {
20797           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
20798           if (depth == 0 && end == CPP_CLOSE_PAREN)
20799             return false;
20800         }
20801       else if (token->type == CPP_PRAGMA)
20802         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
20803       else if (token->type == end)
20804         return false;
20805     }
20806 }
20807
20808 /* Begin parsing tentatively.  We always save tokens while parsing
20809    tentatively so that if the tentative parsing fails we can restore the
20810    tokens.  */
20811
20812 static void
20813 cp_parser_parse_tentatively (cp_parser* parser)
20814 {
20815   /* Enter a new parsing context.  */
20816   parser->context = cp_parser_context_new (parser->context);
20817   /* Begin saving tokens.  */
20818   cp_lexer_save_tokens (parser->lexer);
20819   /* In order to avoid repetitive access control error messages,
20820      access checks are queued up until we are no longer parsing
20821      tentatively.  */
20822   push_deferring_access_checks (dk_deferred);
20823 }
20824
20825 /* Commit to the currently active tentative parse.  */
20826
20827 static void
20828 cp_parser_commit_to_tentative_parse (cp_parser* parser)
20829 {
20830   cp_parser_context *context;
20831   cp_lexer *lexer;
20832
20833   /* Mark all of the levels as committed.  */
20834   lexer = parser->lexer;
20835   for (context = parser->context; context->next; context = context->next)
20836     {
20837       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20838         break;
20839       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20840       while (!cp_lexer_saving_tokens (lexer))
20841         lexer = lexer->next;
20842       cp_lexer_commit_tokens (lexer);
20843     }
20844 }
20845
20846 /* Abort the currently active tentative parse.  All consumed tokens
20847    will be rolled back, and no diagnostics will be issued.  */
20848
20849 static void
20850 cp_parser_abort_tentative_parse (cp_parser* parser)
20851 {
20852   cp_parser_simulate_error (parser);
20853   /* Now, pretend that we want to see if the construct was
20854      successfully parsed.  */
20855   cp_parser_parse_definitely (parser);
20856 }
20857
20858 /* Stop parsing tentatively.  If a parse error has occurred, restore the
20859    token stream.  Otherwise, commit to the tokens we have consumed.
20860    Returns true if no error occurred; false otherwise.  */
20861
20862 static bool
20863 cp_parser_parse_definitely (cp_parser* parser)
20864 {
20865   bool error_occurred;
20866   cp_parser_context *context;
20867
20868   /* Remember whether or not an error occurred, since we are about to
20869      destroy that information.  */
20870   error_occurred = cp_parser_error_occurred (parser);
20871   /* Remove the topmost context from the stack.  */
20872   context = parser->context;
20873   parser->context = context->next;
20874   /* If no parse errors occurred, commit to the tentative parse.  */
20875   if (!error_occurred)
20876     {
20877       /* Commit to the tokens read tentatively, unless that was
20878          already done.  */
20879       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20880         cp_lexer_commit_tokens (parser->lexer);
20881
20882       pop_to_parent_deferring_access_checks ();
20883     }
20884   /* Otherwise, if errors occurred, roll back our state so that things
20885      are just as they were before we began the tentative parse.  */
20886   else
20887     {
20888       cp_lexer_rollback_tokens (parser->lexer);
20889       pop_deferring_access_checks ();
20890     }
20891   /* Add the context to the front of the free list.  */
20892   context->next = cp_parser_context_free_list;
20893   cp_parser_context_free_list = context;
20894
20895   return !error_occurred;
20896 }
20897
20898 /* Returns true if we are parsing tentatively and are not committed to
20899    this tentative parse.  */
20900
20901 static bool
20902 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20903 {
20904   return (cp_parser_parsing_tentatively (parser)
20905           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20906 }
20907
20908 /* Returns nonzero iff an error has occurred during the most recent
20909    tentative parse.  */
20910
20911 static bool
20912 cp_parser_error_occurred (cp_parser* parser)
20913 {
20914   return (cp_parser_parsing_tentatively (parser)
20915           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20916 }
20917
20918 /* Returns nonzero if GNU extensions are allowed.  */
20919
20920 static bool
20921 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20922 {
20923   return parser->allow_gnu_extensions_p;
20924 }
20925 \f
20926 /* Objective-C++ Productions */
20927
20928
20929 /* Parse an Objective-C expression, which feeds into a primary-expression
20930    above.
20931
20932    objc-expression:
20933      objc-message-expression
20934      objc-string-literal
20935      objc-encode-expression
20936      objc-protocol-expression
20937      objc-selector-expression
20938
20939   Returns a tree representation of the expression.  */
20940
20941 static tree
20942 cp_parser_objc_expression (cp_parser* parser)
20943 {
20944   /* Try to figure out what kind of declaration is present.  */
20945   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20946
20947   switch (kwd->type)
20948     {
20949     case CPP_OPEN_SQUARE:
20950       return cp_parser_objc_message_expression (parser);
20951
20952     case CPP_OBJC_STRING:
20953       kwd = cp_lexer_consume_token (parser->lexer);
20954       return objc_build_string_object (kwd->u.value);
20955
20956     case CPP_KEYWORD:
20957       switch (kwd->keyword)
20958         {
20959         case RID_AT_ENCODE:
20960           return cp_parser_objc_encode_expression (parser);
20961
20962         case RID_AT_PROTOCOL:
20963           return cp_parser_objc_protocol_expression (parser);
20964
20965         case RID_AT_SELECTOR:
20966           return cp_parser_objc_selector_expression (parser);
20967
20968         default:
20969           break;
20970         }
20971     default:
20972       error_at (kwd->location,
20973                 "misplaced %<@%D%> Objective-C++ construct",
20974                 kwd->u.value);
20975       cp_parser_skip_to_end_of_block_or_statement (parser);
20976     }
20977
20978   return error_mark_node;
20979 }
20980
20981 /* Parse an Objective-C message expression.
20982
20983    objc-message-expression:
20984      [ objc-message-receiver objc-message-args ]
20985
20986    Returns a representation of an Objective-C message.  */
20987
20988 static tree
20989 cp_parser_objc_message_expression (cp_parser* parser)
20990 {
20991   tree receiver, messageargs;
20992
20993   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20994   receiver = cp_parser_objc_message_receiver (parser);
20995   messageargs = cp_parser_objc_message_args (parser);
20996   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20997
20998   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20999 }
21000
21001 /* Parse an objc-message-receiver.
21002
21003    objc-message-receiver:
21004      expression
21005      simple-type-specifier
21006
21007   Returns a representation of the type or expression.  */
21008
21009 static tree
21010 cp_parser_objc_message_receiver (cp_parser* parser)
21011 {
21012   tree rcv;
21013
21014   /* An Objective-C message receiver may be either (1) a type
21015      or (2) an expression.  */
21016   cp_parser_parse_tentatively (parser);
21017   rcv = cp_parser_expression (parser, false, NULL);
21018
21019   if (cp_parser_parse_definitely (parser))
21020     return rcv;
21021
21022   rcv = cp_parser_simple_type_specifier (parser,
21023                                          /*decl_specs=*/NULL,
21024                                          CP_PARSER_FLAGS_NONE);
21025
21026   return objc_get_class_reference (rcv);
21027 }
21028
21029 /* Parse the arguments and selectors comprising an Objective-C message.
21030
21031    objc-message-args:
21032      objc-selector
21033      objc-selector-args
21034      objc-selector-args , objc-comma-args
21035
21036    objc-selector-args:
21037      objc-selector [opt] : assignment-expression
21038      objc-selector-args objc-selector [opt] : assignment-expression
21039
21040    objc-comma-args:
21041      assignment-expression
21042      objc-comma-args , assignment-expression
21043
21044    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21045    selector arguments and TREE_VALUE containing a list of comma
21046    arguments.  */
21047
21048 static tree
21049 cp_parser_objc_message_args (cp_parser* parser)
21050 {
21051   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21052   bool maybe_unary_selector_p = true;
21053   cp_token *token = cp_lexer_peek_token (parser->lexer);
21054
21055   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21056     {
21057       tree selector = NULL_TREE, arg;
21058
21059       if (token->type != CPP_COLON)
21060         selector = cp_parser_objc_selector (parser);
21061
21062       /* Detect if we have a unary selector.  */
21063       if (maybe_unary_selector_p
21064           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21065         return build_tree_list (selector, NULL_TREE);
21066
21067       maybe_unary_selector_p = false;
21068       cp_parser_require (parser, CPP_COLON, RT_COLON);
21069       arg = cp_parser_assignment_expression (parser, false, NULL);
21070
21071       sel_args
21072         = chainon (sel_args,
21073                    build_tree_list (selector, arg));
21074
21075       token = cp_lexer_peek_token (parser->lexer);
21076     }
21077
21078   /* Handle non-selector arguments, if any. */
21079   while (token->type == CPP_COMMA)
21080     {
21081       tree arg;
21082
21083       cp_lexer_consume_token (parser->lexer);
21084       arg = cp_parser_assignment_expression (parser, false, NULL);
21085
21086       addl_args
21087         = chainon (addl_args,
21088                    build_tree_list (NULL_TREE, arg));
21089
21090       token = cp_lexer_peek_token (parser->lexer);
21091     }
21092
21093   return build_tree_list (sel_args, addl_args);
21094 }
21095
21096 /* Parse an Objective-C encode expression.
21097
21098    objc-encode-expression:
21099      @encode objc-typename
21100
21101    Returns an encoded representation of the type argument.  */
21102
21103 static tree
21104 cp_parser_objc_encode_expression (cp_parser* parser)
21105 {
21106   tree type;
21107   cp_token *token;
21108
21109   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21110   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21111   token = cp_lexer_peek_token (parser->lexer);
21112   type = complete_type (cp_parser_type_id (parser));
21113   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21114
21115   if (!type)
21116     {
21117       error_at (token->location, 
21118                 "%<@encode%> must specify a type as an argument");
21119       return error_mark_node;
21120     }
21121
21122   return objc_build_encode_expr (type);
21123 }
21124
21125 /* Parse an Objective-C @defs expression.  */
21126
21127 static tree
21128 cp_parser_objc_defs_expression (cp_parser *parser)
21129 {
21130   tree name;
21131
21132   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21133   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21134   name = cp_parser_identifier (parser);
21135   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21136
21137   return objc_get_class_ivars (name);
21138 }
21139
21140 /* Parse an Objective-C protocol expression.
21141
21142   objc-protocol-expression:
21143     @protocol ( identifier )
21144
21145   Returns a representation of the protocol expression.  */
21146
21147 static tree
21148 cp_parser_objc_protocol_expression (cp_parser* parser)
21149 {
21150   tree proto;
21151
21152   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21153   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21154   proto = cp_parser_identifier (parser);
21155   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21156
21157   return objc_build_protocol_expr (proto);
21158 }
21159
21160 /* Parse an Objective-C selector expression.
21161
21162    objc-selector-expression:
21163      @selector ( objc-method-signature )
21164
21165    objc-method-signature:
21166      objc-selector
21167      objc-selector-seq
21168
21169    objc-selector-seq:
21170      objc-selector :
21171      objc-selector-seq objc-selector :
21172
21173   Returns a representation of the method selector.  */
21174
21175 static tree
21176 cp_parser_objc_selector_expression (cp_parser* parser)
21177 {
21178   tree sel_seq = NULL_TREE;
21179   bool maybe_unary_selector_p = true;
21180   cp_token *token;
21181   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21182
21183   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21184   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21185   token = cp_lexer_peek_token (parser->lexer);
21186
21187   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21188          || token->type == CPP_SCOPE)
21189     {
21190       tree selector = NULL_TREE;
21191
21192       if (token->type != CPP_COLON
21193           || token->type == CPP_SCOPE)
21194         selector = cp_parser_objc_selector (parser);
21195
21196       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21197           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21198         {
21199           /* Detect if we have a unary selector.  */
21200           if (maybe_unary_selector_p)
21201             {
21202               sel_seq = selector;
21203               goto finish_selector;
21204             }
21205           else
21206             {
21207               cp_parser_error (parser, "expected %<:%>");
21208             }
21209         }
21210       maybe_unary_selector_p = false;
21211       token = cp_lexer_consume_token (parser->lexer);
21212
21213       if (token->type == CPP_SCOPE)
21214         {
21215           sel_seq
21216             = chainon (sel_seq,
21217                        build_tree_list (selector, NULL_TREE));
21218           sel_seq
21219             = chainon (sel_seq,
21220                        build_tree_list (NULL_TREE, NULL_TREE));
21221         }
21222       else
21223         sel_seq
21224           = chainon (sel_seq,
21225                      build_tree_list (selector, NULL_TREE));
21226
21227       token = cp_lexer_peek_token (parser->lexer);
21228     }
21229
21230  finish_selector:
21231   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21232
21233   return objc_build_selector_expr (loc, sel_seq);
21234 }
21235
21236 /* Parse a list of identifiers.
21237
21238    objc-identifier-list:
21239      identifier
21240      objc-identifier-list , identifier
21241
21242    Returns a TREE_LIST of identifier nodes.  */
21243
21244 static tree
21245 cp_parser_objc_identifier_list (cp_parser* parser)
21246 {
21247   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
21248   cp_token *sep = cp_lexer_peek_token (parser->lexer);
21249
21250   while (sep->type == CPP_COMMA)
21251     {
21252       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21253       list = chainon (list,
21254                       build_tree_list (NULL_TREE,
21255                                        cp_parser_identifier (parser)));
21256       sep = cp_lexer_peek_token (parser->lexer);
21257     }
21258
21259   return list;
21260 }
21261
21262 /* Parse an Objective-C alias declaration.
21263
21264    objc-alias-declaration:
21265      @compatibility_alias identifier identifier ;
21266
21267    This function registers the alias mapping with the Objective-C front end.
21268    It returns nothing.  */
21269
21270 static void
21271 cp_parser_objc_alias_declaration (cp_parser* parser)
21272 {
21273   tree alias, orig;
21274
21275   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21276   alias = cp_parser_identifier (parser);
21277   orig = cp_parser_identifier (parser);
21278   objc_declare_alias (alias, orig);
21279   cp_parser_consume_semicolon_at_end_of_statement (parser);
21280 }
21281
21282 /* Parse an Objective-C class forward-declaration.
21283
21284    objc-class-declaration:
21285      @class objc-identifier-list ;
21286
21287    The function registers the forward declarations with the Objective-C
21288    front end.  It returns nothing.  */
21289
21290 static void
21291 cp_parser_objc_class_declaration (cp_parser* parser)
21292 {
21293   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21294   objc_declare_class (cp_parser_objc_identifier_list (parser));
21295   cp_parser_consume_semicolon_at_end_of_statement (parser);
21296 }
21297
21298 /* Parse a list of Objective-C protocol references.
21299
21300    objc-protocol-refs-opt:
21301      objc-protocol-refs [opt]
21302
21303    objc-protocol-refs:
21304      < objc-identifier-list >
21305
21306    Returns a TREE_LIST of identifiers, if any.  */
21307
21308 static tree
21309 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21310 {
21311   tree protorefs = NULL_TREE;
21312
21313   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21314     {
21315       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21316       protorefs = cp_parser_objc_identifier_list (parser);
21317       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21318     }
21319
21320   return protorefs;
21321 }
21322
21323 /* Parse a Objective-C visibility specification.  */
21324
21325 static void
21326 cp_parser_objc_visibility_spec (cp_parser* parser)
21327 {
21328   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21329
21330   switch (vis->keyword)
21331     {
21332     case RID_AT_PRIVATE:
21333       objc_set_visibility (2);
21334       break;
21335     case RID_AT_PROTECTED:
21336       objc_set_visibility (0);
21337       break;
21338     case RID_AT_PUBLIC:
21339       objc_set_visibility (1);
21340       break;
21341     default:
21342       return;
21343     }
21344
21345   /* Eat '@private'/'@protected'/'@public'.  */
21346   cp_lexer_consume_token (parser->lexer);
21347 }
21348
21349 /* Parse an Objective-C method type.  */
21350
21351 static void
21352 cp_parser_objc_method_type (cp_parser* parser)
21353 {
21354   objc_set_method_type
21355    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
21356     ? PLUS_EXPR
21357     : MINUS_EXPR);
21358 }
21359
21360 /* Parse an Objective-C protocol qualifier.  */
21361
21362 static tree
21363 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21364 {
21365   tree quals = NULL_TREE, node;
21366   cp_token *token = cp_lexer_peek_token (parser->lexer);
21367
21368   node = token->u.value;
21369
21370   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21371          && (node == ridpointers [(int) RID_IN]
21372              || node == ridpointers [(int) RID_OUT]
21373              || node == ridpointers [(int) RID_INOUT]
21374              || node == ridpointers [(int) RID_BYCOPY]
21375              || node == ridpointers [(int) RID_BYREF]
21376              || node == ridpointers [(int) RID_ONEWAY]))
21377     {
21378       quals = tree_cons (NULL_TREE, node, quals);
21379       cp_lexer_consume_token (parser->lexer);
21380       token = cp_lexer_peek_token (parser->lexer);
21381       node = token->u.value;
21382     }
21383
21384   return quals;
21385 }
21386
21387 /* Parse an Objective-C typename.  */
21388
21389 static tree
21390 cp_parser_objc_typename (cp_parser* parser)
21391 {
21392   tree type_name = NULL_TREE;
21393
21394   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21395     {
21396       tree proto_quals, cp_type = NULL_TREE;
21397
21398       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21399       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21400
21401       /* An ObjC type name may consist of just protocol qualifiers, in which
21402          case the type shall default to 'id'.  */
21403       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21404         cp_type = cp_parser_type_id (parser);
21405
21406       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21407       type_name = build_tree_list (proto_quals, cp_type);
21408     }
21409
21410   return type_name;
21411 }
21412
21413 /* Check to see if TYPE refers to an Objective-C selector name.  */
21414
21415 static bool
21416 cp_parser_objc_selector_p (enum cpp_ttype type)
21417 {
21418   return (type == CPP_NAME || type == CPP_KEYWORD
21419           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21420           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21421           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21422           || type == CPP_XOR || type == CPP_XOR_EQ);
21423 }
21424
21425 /* Parse an Objective-C selector.  */
21426
21427 static tree
21428 cp_parser_objc_selector (cp_parser* parser)
21429 {
21430   cp_token *token = cp_lexer_consume_token (parser->lexer);
21431
21432   if (!cp_parser_objc_selector_p (token->type))
21433     {
21434       error_at (token->location, "invalid Objective-C++ selector name");
21435       return error_mark_node;
21436     }
21437
21438   /* C++ operator names are allowed to appear in ObjC selectors.  */
21439   switch (token->type)
21440     {
21441     case CPP_AND_AND: return get_identifier ("and");
21442     case CPP_AND_EQ: return get_identifier ("and_eq");
21443     case CPP_AND: return get_identifier ("bitand");
21444     case CPP_OR: return get_identifier ("bitor");
21445     case CPP_COMPL: return get_identifier ("compl");
21446     case CPP_NOT: return get_identifier ("not");
21447     case CPP_NOT_EQ: return get_identifier ("not_eq");
21448     case CPP_OR_OR: return get_identifier ("or");
21449     case CPP_OR_EQ: return get_identifier ("or_eq");
21450     case CPP_XOR: return get_identifier ("xor");
21451     case CPP_XOR_EQ: return get_identifier ("xor_eq");
21452     default: return token->u.value;
21453     }
21454 }
21455
21456 /* Parse an Objective-C params list.  */
21457
21458 static tree
21459 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21460 {
21461   tree params = NULL_TREE;
21462   bool maybe_unary_selector_p = true;
21463   cp_token *token = cp_lexer_peek_token (parser->lexer);
21464
21465   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21466     {
21467       tree selector = NULL_TREE, type_name, identifier;
21468       tree parm_attr = NULL_TREE;
21469
21470       if (token->keyword == RID_ATTRIBUTE)
21471         break;
21472
21473       if (token->type != CPP_COLON)
21474         selector = cp_parser_objc_selector (parser);
21475
21476       /* Detect if we have a unary selector.  */
21477       if (maybe_unary_selector_p
21478           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21479         {
21480           params = selector; /* Might be followed by attributes.  */
21481           break;
21482         }
21483
21484       maybe_unary_selector_p = false;
21485       cp_parser_require (parser, CPP_COLON, RT_COLON);
21486       type_name = cp_parser_objc_typename (parser);
21487       /* New ObjC allows attributes on parameters too.  */
21488       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21489         parm_attr = cp_parser_attributes_opt (parser);
21490       identifier = cp_parser_identifier (parser);
21491
21492       params
21493         = chainon (params,
21494                    objc_build_keyword_decl (selector,
21495                                             type_name,
21496                                             identifier,
21497                                             parm_attr));
21498
21499       token = cp_lexer_peek_token (parser->lexer);
21500     }
21501
21502   if (params == NULL_TREE)
21503     {
21504       cp_parser_error (parser, "objective-c++ method declaration is expected");
21505       return error_mark_node;
21506     }
21507
21508   /* We allow tail attributes for the method.  */
21509   if (token->keyword == RID_ATTRIBUTE)
21510     {
21511       *attributes = cp_parser_attributes_opt (parser);
21512       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21513           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21514         return params;
21515       cp_parser_error (parser, 
21516                        "method attributes must be specified at the end");
21517       return error_mark_node;
21518     }
21519
21520   return params;
21521 }
21522
21523 /* Parse the non-keyword Objective-C params.  */
21524
21525 static tree
21526 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
21527                                        tree* attributes)
21528 {
21529   tree params = make_node (TREE_LIST);
21530   cp_token *token = cp_lexer_peek_token (parser->lexer);
21531   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
21532
21533   while (token->type == CPP_COMMA)
21534     {
21535       cp_parameter_declarator *parmdecl;
21536       tree parm;
21537
21538       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21539       token = cp_lexer_peek_token (parser->lexer);
21540
21541       if (token->type == CPP_ELLIPSIS)
21542         {
21543           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
21544           *ellipsisp = true;
21545           break;
21546         }
21547
21548       /* TODO: parse attributes for tail parameters.  */
21549       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21550       parm = grokdeclarator (parmdecl->declarator,
21551                              &parmdecl->decl_specifiers,
21552                              PARM, /*initialized=*/0,
21553                              /*attrlist=*/NULL);
21554
21555       chainon (params, build_tree_list (NULL_TREE, parm));
21556       token = cp_lexer_peek_token (parser->lexer);
21557     }
21558
21559   /* We allow tail attributes for the method.  */
21560   if (token->keyword == RID_ATTRIBUTE)
21561     {
21562       if (*attributes == NULL_TREE)
21563         {
21564           *attributes = cp_parser_attributes_opt (parser);
21565           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21566               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21567             return params;
21568         }
21569       else        
21570         /* We have an error, but parse the attributes, so that we can 
21571            carry on.  */
21572         *attributes = cp_parser_attributes_opt (parser);
21573
21574       cp_parser_error (parser, 
21575                        "method attributes must be specified at the end");
21576       return error_mark_node;
21577     }
21578
21579   return params;
21580 }
21581
21582 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
21583
21584 static void
21585 cp_parser_objc_interstitial_code (cp_parser* parser)
21586 {
21587   cp_token *token = cp_lexer_peek_token (parser->lexer);
21588
21589   /* If the next token is `extern' and the following token is a string
21590      literal, then we have a linkage specification.  */
21591   if (token->keyword == RID_EXTERN
21592       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21593     cp_parser_linkage_specification (parser);
21594   /* Handle #pragma, if any.  */
21595   else if (token->type == CPP_PRAGMA)
21596     cp_parser_pragma (parser, pragma_external);
21597   /* Allow stray semicolons.  */
21598   else if (token->type == CPP_SEMICOLON)
21599     cp_lexer_consume_token (parser->lexer);
21600   /* Finally, try to parse a block-declaration, or a function-definition.  */
21601   else
21602     cp_parser_block_declaration (parser, /*statement_p=*/false);
21603 }
21604
21605 /* Parse a method signature.  */
21606
21607 static tree
21608 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
21609 {
21610   tree rettype, kwdparms, optparms;
21611   bool ellipsis = false;
21612
21613   cp_parser_objc_method_type (parser);
21614   rettype = cp_parser_objc_typename (parser);
21615   *attributes = NULL_TREE;
21616   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
21617   if (kwdparms == error_mark_node)
21618     return error_mark_node;
21619   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
21620   if (optparms == error_mark_node)
21621     return error_mark_node;
21622
21623   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
21624 }
21625
21626 static bool
21627 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
21628 {
21629   tree tattr;  
21630   cp_lexer_save_tokens (parser->lexer);
21631   tattr = cp_parser_attributes_opt (parser);
21632   gcc_assert (tattr) ;
21633   
21634   /* If the attributes are followed by a method introducer, this is not allowed.
21635      Dump the attributes and flag the situation.  */
21636   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
21637       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
21638     return true;
21639
21640   /* Otherwise, the attributes introduce some interstitial code, possibly so
21641      rewind to allow that check.  */
21642   cp_lexer_rollback_tokens (parser->lexer);
21643   return false;  
21644 }
21645
21646 /* Parse an Objective-C method prototype list.  */
21647
21648 static void
21649 cp_parser_objc_method_prototype_list (cp_parser* parser)
21650 {
21651   cp_token *token = cp_lexer_peek_token (parser->lexer);
21652
21653   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
21654     {
21655       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21656         {
21657           tree attributes, sig;
21658           sig = cp_parser_objc_method_signature (parser, &attributes);
21659           if (sig == error_mark_node)
21660             {
21661               cp_parser_skip_to_end_of_block_or_statement (parser);
21662               continue;
21663             }
21664           objc_add_method_declaration (sig, attributes);
21665           cp_parser_consume_semicolon_at_end_of_statement (parser);
21666         }
21667       else if (token->keyword == RID_ATTRIBUTE 
21668                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
21669         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
21670                     OPT_Wattributes, 
21671                     "prefix attributes are ignored for methods");
21672       else
21673         /* Allow for interspersed non-ObjC++ code.  */
21674         cp_parser_objc_interstitial_code (parser);
21675
21676       token = cp_lexer_peek_token (parser->lexer);
21677     }
21678
21679   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21680   objc_finish_interface ();
21681 }
21682
21683 /* Parse an Objective-C method definition list.  */
21684
21685 static void
21686 cp_parser_objc_method_definition_list (cp_parser* parser)
21687 {
21688   cp_token *token = cp_lexer_peek_token (parser->lexer);
21689
21690   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
21691     {
21692       tree meth;
21693
21694       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21695         {
21696           cp_token *ptk;
21697           tree sig, attribute;
21698           push_deferring_access_checks (dk_deferred);
21699           sig = cp_parser_objc_method_signature (parser, &attribute);
21700           if (sig == error_mark_node)
21701             {
21702               cp_parser_skip_to_end_of_block_or_statement (parser);
21703               continue;
21704             }
21705           objc_start_method_definition (sig, attribute);
21706
21707           /* For historical reasons, we accept an optional semicolon.  */
21708           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21709             cp_lexer_consume_token (parser->lexer);
21710
21711           ptk = cp_lexer_peek_token (parser->lexer);
21712           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
21713                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
21714             {
21715               perform_deferred_access_checks ();
21716               stop_deferring_access_checks ();
21717               meth = cp_parser_function_definition_after_declarator (parser,
21718                                                                  false);
21719               pop_deferring_access_checks ();
21720               objc_finish_method_definition (meth);
21721             }
21722         }
21723       else if (token->keyword == RID_ATTRIBUTE 
21724                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
21725         warning_at (token->location, OPT_Wattributes,
21726                     "prefix attributes are ignored for methods");
21727       else
21728         /* Allow for interspersed non-ObjC++ code.  */
21729         cp_parser_objc_interstitial_code (parser);
21730
21731       token = cp_lexer_peek_token (parser->lexer);
21732     }
21733
21734   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21735   objc_finish_implementation ();
21736 }
21737
21738 /* Parse Objective-C ivars.  */
21739
21740 static void
21741 cp_parser_objc_class_ivars (cp_parser* parser)
21742 {
21743   cp_token *token = cp_lexer_peek_token (parser->lexer);
21744
21745   if (token->type != CPP_OPEN_BRACE)
21746     return;     /* No ivars specified.  */
21747
21748   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
21749   token = cp_lexer_peek_token (parser->lexer);
21750
21751   while (token->type != CPP_CLOSE_BRACE)
21752     {
21753       cp_decl_specifier_seq declspecs;
21754       int decl_class_or_enum_p;
21755       tree prefix_attributes;
21756
21757       cp_parser_objc_visibility_spec (parser);
21758
21759       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21760         break;
21761
21762       cp_parser_decl_specifier_seq (parser,
21763                                     CP_PARSER_FLAGS_OPTIONAL,
21764                                     &declspecs,
21765                                     &decl_class_or_enum_p);
21766       prefix_attributes = declspecs.attributes;
21767       declspecs.attributes = NULL_TREE;
21768
21769       /* Keep going until we hit the `;' at the end of the
21770          declaration.  */
21771       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21772         {
21773           tree width = NULL_TREE, attributes, first_attribute, decl;
21774           cp_declarator *declarator = NULL;
21775           int ctor_dtor_or_conv_p;
21776
21777           /* Check for a (possibly unnamed) bitfield declaration.  */
21778           token = cp_lexer_peek_token (parser->lexer);
21779           if (token->type == CPP_COLON)
21780             goto eat_colon;
21781
21782           if (token->type == CPP_NAME
21783               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21784                   == CPP_COLON))
21785             {
21786               /* Get the name of the bitfield.  */
21787               declarator = make_id_declarator (NULL_TREE,
21788                                                cp_parser_identifier (parser),
21789                                                sfk_none);
21790
21791              eat_colon:
21792               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
21793               /* Get the width of the bitfield.  */
21794               width
21795                 = cp_parser_constant_expression (parser,
21796                                                  /*allow_non_constant=*/false,
21797                                                  NULL);
21798             }
21799           else
21800             {
21801               /* Parse the declarator.  */
21802               declarator
21803                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21804                                         &ctor_dtor_or_conv_p,
21805                                         /*parenthesized_p=*/NULL,
21806                                         /*member_p=*/false);
21807             }
21808
21809           /* Look for attributes that apply to the ivar.  */
21810           attributes = cp_parser_attributes_opt (parser);
21811           /* Remember which attributes are prefix attributes and
21812              which are not.  */
21813           first_attribute = attributes;
21814           /* Combine the attributes.  */
21815           attributes = chainon (prefix_attributes, attributes);
21816
21817           if (width)
21818               /* Create the bitfield declaration.  */
21819               decl = grokbitfield (declarator, &declspecs,
21820                                    width,
21821                                    attributes);
21822           else
21823             decl = grokfield (declarator, &declspecs,
21824                               NULL_TREE, /*init_const_expr_p=*/false,
21825                               NULL_TREE, attributes);
21826
21827           /* Add the instance variable.  */
21828           objc_add_instance_variable (decl);
21829
21830           /* Reset PREFIX_ATTRIBUTES.  */
21831           while (attributes && TREE_CHAIN (attributes) != first_attribute)
21832             attributes = TREE_CHAIN (attributes);
21833           if (attributes)
21834             TREE_CHAIN (attributes) = NULL_TREE;
21835
21836           token = cp_lexer_peek_token (parser->lexer);
21837
21838           if (token->type == CPP_COMMA)
21839             {
21840               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21841               continue;
21842             }
21843           break;
21844         }
21845
21846       cp_parser_consume_semicolon_at_end_of_statement (parser);
21847       token = cp_lexer_peek_token (parser->lexer);
21848     }
21849
21850   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
21851   /* For historical reasons, we accept an optional semicolon.  */
21852   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21853     cp_lexer_consume_token (parser->lexer);
21854 }
21855
21856 /* Parse an Objective-C protocol declaration.  */
21857
21858 static void
21859 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
21860 {
21861   tree proto, protorefs;
21862   cp_token *tok;
21863
21864   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21865   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
21866     {
21867       tok = cp_lexer_peek_token (parser->lexer);
21868       error_at (tok->location, "identifier expected after %<@protocol%>");
21869       goto finish;
21870     }
21871
21872   /* See if we have a forward declaration or a definition.  */
21873   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
21874
21875   /* Try a forward declaration first.  */
21876   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
21877     {
21878       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
21879      finish:
21880       cp_parser_consume_semicolon_at_end_of_statement (parser);
21881     }
21882
21883   /* Ok, we got a full-fledged definition (or at least should).  */
21884   else
21885     {
21886       proto = cp_parser_identifier (parser);
21887       protorefs = cp_parser_objc_protocol_refs_opt (parser);
21888       objc_start_protocol (proto, protorefs, attributes);
21889       cp_parser_objc_method_prototype_list (parser);
21890     }
21891 }
21892
21893 /* Parse an Objective-C superclass or category.  */
21894
21895 static void
21896 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
21897                                                           tree *categ)
21898 {
21899   cp_token *next = cp_lexer_peek_token (parser->lexer);
21900
21901   *super = *categ = NULL_TREE;
21902   if (next->type == CPP_COLON)
21903     {
21904       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
21905       *super = cp_parser_identifier (parser);
21906     }
21907   else if (next->type == CPP_OPEN_PAREN)
21908     {
21909       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21910       *categ = cp_parser_identifier (parser);
21911       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21912     }
21913 }
21914
21915 /* Parse an Objective-C class interface.  */
21916
21917 static void
21918 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
21919 {
21920   tree name, super, categ, protos;
21921
21922   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
21923   name = cp_parser_identifier (parser);
21924   cp_parser_objc_superclass_or_category (parser, &super, &categ);
21925   protos = cp_parser_objc_protocol_refs_opt (parser);
21926
21927   /* We have either a class or a category on our hands.  */
21928   if (categ)
21929     objc_start_category_interface (name, categ, protos, attributes);
21930   else
21931     {
21932       objc_start_class_interface (name, super, protos, attributes);
21933       /* Handle instance variable declarations, if any.  */
21934       cp_parser_objc_class_ivars (parser);
21935       objc_continue_interface ();
21936     }
21937
21938   cp_parser_objc_method_prototype_list (parser);
21939 }
21940
21941 /* Parse an Objective-C class implementation.  */
21942
21943 static void
21944 cp_parser_objc_class_implementation (cp_parser* parser)
21945 {
21946   tree name, super, categ;
21947
21948   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
21949   name = cp_parser_identifier (parser);
21950   cp_parser_objc_superclass_or_category (parser, &super, &categ);
21951
21952   /* We have either a class or a category on our hands.  */
21953   if (categ)
21954     objc_start_category_implementation (name, categ);
21955   else
21956     {
21957       objc_start_class_implementation (name, super);
21958       /* Handle instance variable declarations, if any.  */
21959       cp_parser_objc_class_ivars (parser);
21960       objc_continue_implementation ();
21961     }
21962
21963   cp_parser_objc_method_definition_list (parser);
21964 }
21965
21966 /* Consume the @end token and finish off the implementation.  */
21967
21968 static void
21969 cp_parser_objc_end_implementation (cp_parser* parser)
21970 {
21971   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21972   objc_finish_implementation ();
21973 }
21974
21975 /* Parse an Objective-C declaration.  */
21976
21977 static void
21978 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
21979 {
21980   /* Try to figure out what kind of declaration is present.  */
21981   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21982
21983   if (attributes)
21984     switch (kwd->keyword)
21985       {
21986         case RID_AT_ALIAS:
21987         case RID_AT_CLASS:
21988         case RID_AT_END:
21989           error_at (kwd->location, "attributes may not be specified before"
21990                     " the %<@%D%> Objective-C++ keyword",
21991                     kwd->u.value);
21992           attributes = NULL;
21993           break;
21994         case RID_AT_IMPLEMENTATION:
21995           warning_at (kwd->location, OPT_Wattributes,
21996                       "prefix attributes are ignored before %<@%D%>",
21997                       kwd->u.value);
21998           attributes = NULL;
21999         default:
22000           break;
22001       }
22002
22003   switch (kwd->keyword)
22004     {
22005     case RID_AT_ALIAS:
22006       cp_parser_objc_alias_declaration (parser);
22007       break;
22008     case RID_AT_CLASS:
22009       cp_parser_objc_class_declaration (parser);
22010       break;
22011     case RID_AT_PROTOCOL:
22012       cp_parser_objc_protocol_declaration (parser, attributes);
22013       break;
22014     case RID_AT_INTERFACE:
22015       cp_parser_objc_class_interface (parser, attributes);
22016       break;
22017     case RID_AT_IMPLEMENTATION:
22018       cp_parser_objc_class_implementation (parser);
22019       break;
22020     case RID_AT_END:
22021       cp_parser_objc_end_implementation (parser);
22022       break;
22023     default:
22024       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22025                 kwd->u.value);
22026       cp_parser_skip_to_end_of_block_or_statement (parser);
22027     }
22028 }
22029
22030 /* Parse an Objective-C try-catch-finally statement.
22031
22032    objc-try-catch-finally-stmt:
22033      @try compound-statement objc-catch-clause-seq [opt]
22034        objc-finally-clause [opt]
22035
22036    objc-catch-clause-seq:
22037      objc-catch-clause objc-catch-clause-seq [opt]
22038
22039    objc-catch-clause:
22040      @catch ( exception-declaration ) compound-statement
22041
22042    objc-finally-clause
22043      @finally compound-statement
22044
22045    Returns NULL_TREE.  */
22046
22047 static tree
22048 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22049   location_t location;
22050   tree stmt;
22051
22052   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22053   location = cp_lexer_peek_token (parser->lexer)->location;
22054   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22055      node, lest it get absorbed into the surrounding block.  */
22056   stmt = push_stmt_list ();
22057   cp_parser_compound_statement (parser, NULL, false);
22058   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22059
22060   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22061     {
22062       cp_parameter_declarator *parmdecl;
22063       tree parm;
22064
22065       cp_lexer_consume_token (parser->lexer);
22066       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22067       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22068       parm = grokdeclarator (parmdecl->declarator,
22069                              &parmdecl->decl_specifiers,
22070                              PARM, /*initialized=*/0,
22071                              /*attrlist=*/NULL);
22072       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22073       objc_begin_catch_clause (parm);
22074       cp_parser_compound_statement (parser, NULL, false);
22075       objc_finish_catch_clause ();
22076     }
22077
22078   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22079     {
22080       cp_lexer_consume_token (parser->lexer);
22081       location = cp_lexer_peek_token (parser->lexer)->location;
22082       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22083          node, lest it get absorbed into the surrounding block.  */
22084       stmt = push_stmt_list ();
22085       cp_parser_compound_statement (parser, NULL, false);
22086       objc_build_finally_clause (location, pop_stmt_list (stmt));
22087     }
22088
22089   return objc_finish_try_stmt ();
22090 }
22091
22092 /* Parse an Objective-C synchronized statement.
22093
22094    objc-synchronized-stmt:
22095      @synchronized ( expression ) compound-statement
22096
22097    Returns NULL_TREE.  */
22098
22099 static tree
22100 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22101   location_t location;
22102   tree lock, stmt;
22103
22104   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22105
22106   location = cp_lexer_peek_token (parser->lexer)->location;
22107   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22108   lock = cp_parser_expression (parser, false, NULL);
22109   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22110
22111   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22112      node, lest it get absorbed into the surrounding block.  */
22113   stmt = push_stmt_list ();
22114   cp_parser_compound_statement (parser, NULL, false);
22115
22116   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22117 }
22118
22119 /* Parse an Objective-C throw statement.
22120
22121    objc-throw-stmt:
22122      @throw assignment-expression [opt] ;
22123
22124    Returns a constructed '@throw' statement.  */
22125
22126 static tree
22127 cp_parser_objc_throw_statement (cp_parser *parser) {
22128   tree expr = NULL_TREE;
22129   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22130
22131   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22132
22133   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22134     expr = cp_parser_assignment_expression (parser, false, NULL);
22135
22136   cp_parser_consume_semicolon_at_end_of_statement (parser);
22137
22138   return objc_build_throw_stmt (loc, expr);
22139 }
22140
22141 /* Parse an Objective-C statement.  */
22142
22143 static tree
22144 cp_parser_objc_statement (cp_parser * parser) {
22145   /* Try to figure out what kind of declaration is present.  */
22146   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22147
22148   switch (kwd->keyword)
22149     {
22150     case RID_AT_TRY:
22151       return cp_parser_objc_try_catch_finally_statement (parser);
22152     case RID_AT_SYNCHRONIZED:
22153       return cp_parser_objc_synchronized_statement (parser);
22154     case RID_AT_THROW:
22155       return cp_parser_objc_throw_statement (parser);
22156     default:
22157       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22158                kwd->u.value);
22159       cp_parser_skip_to_end_of_block_or_statement (parser);
22160     }
22161
22162   return error_mark_node;
22163 }
22164
22165 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22166    look ahead to see if an objc keyword follows the attributes.  This
22167    is to detect the use of prefix attributes on ObjC @interface and 
22168    @protocol.  */
22169
22170 static bool
22171 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22172 {
22173   cp_lexer_save_tokens (parser->lexer);
22174   *attrib = cp_parser_attributes_opt (parser);
22175   gcc_assert (*attrib);
22176   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22177     {
22178       cp_lexer_commit_tokens (parser->lexer);
22179       return true;
22180     }
22181   cp_lexer_rollback_tokens (parser->lexer);
22182   return false;  
22183 }
22184 \f
22185 /* OpenMP 2.5 parsing routines.  */
22186
22187 /* Returns name of the next clause.
22188    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
22189    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
22190    returned and the token is consumed.  */
22191
22192 static pragma_omp_clause
22193 cp_parser_omp_clause_name (cp_parser *parser)
22194 {
22195   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
22196
22197   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
22198     result = PRAGMA_OMP_CLAUSE_IF;
22199   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
22200     result = PRAGMA_OMP_CLAUSE_DEFAULT;
22201   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
22202     result = PRAGMA_OMP_CLAUSE_PRIVATE;
22203   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22204     {
22205       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22206       const char *p = IDENTIFIER_POINTER (id);
22207
22208       switch (p[0])
22209         {
22210         case 'c':
22211           if (!strcmp ("collapse", p))
22212             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
22213           else if (!strcmp ("copyin", p))
22214             result = PRAGMA_OMP_CLAUSE_COPYIN;
22215           else if (!strcmp ("copyprivate", p))
22216             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
22217           break;
22218         case 'f':
22219           if (!strcmp ("firstprivate", p))
22220             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
22221           break;
22222         case 'l':
22223           if (!strcmp ("lastprivate", p))
22224             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
22225           break;
22226         case 'n':
22227           if (!strcmp ("nowait", p))
22228             result = PRAGMA_OMP_CLAUSE_NOWAIT;
22229           else if (!strcmp ("num_threads", p))
22230             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
22231           break;
22232         case 'o':
22233           if (!strcmp ("ordered", p))
22234             result = PRAGMA_OMP_CLAUSE_ORDERED;
22235           break;
22236         case 'r':
22237           if (!strcmp ("reduction", p))
22238             result = PRAGMA_OMP_CLAUSE_REDUCTION;
22239           break;
22240         case 's':
22241           if (!strcmp ("schedule", p))
22242             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
22243           else if (!strcmp ("shared", p))
22244             result = PRAGMA_OMP_CLAUSE_SHARED;
22245           break;
22246         case 'u':
22247           if (!strcmp ("untied", p))
22248             result = PRAGMA_OMP_CLAUSE_UNTIED;
22249           break;
22250         }
22251     }
22252
22253   if (result != PRAGMA_OMP_CLAUSE_NONE)
22254     cp_lexer_consume_token (parser->lexer);
22255
22256   return result;
22257 }
22258
22259 /* Validate that a clause of the given type does not already exist.  */
22260
22261 static void
22262 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
22263                            const char *name, location_t location)
22264 {
22265   tree c;
22266
22267   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22268     if (OMP_CLAUSE_CODE (c) == code)
22269       {
22270         error_at (location, "too many %qs clauses", name);
22271         break;
22272       }
22273 }
22274
22275 /* OpenMP 2.5:
22276    variable-list:
22277      identifier
22278      variable-list , identifier
22279
22280    In addition, we match a closing parenthesis.  An opening parenthesis
22281    will have been consumed by the caller.
22282
22283    If KIND is nonzero, create the appropriate node and install the decl
22284    in OMP_CLAUSE_DECL and add the node to the head of the list.
22285
22286    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
22287    return the list created.  */
22288
22289 static tree
22290 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
22291                                 tree list)
22292 {
22293   cp_token *token;
22294   while (1)
22295     {
22296       tree name, decl;
22297
22298       token = cp_lexer_peek_token (parser->lexer);
22299       name = cp_parser_id_expression (parser, /*template_p=*/false,
22300                                       /*check_dependency_p=*/true,
22301                                       /*template_p=*/NULL,
22302                                       /*declarator_p=*/false,
22303                                       /*optional_p=*/false);
22304       if (name == error_mark_node)
22305         goto skip_comma;
22306
22307       decl = cp_parser_lookup_name_simple (parser, name, token->location);
22308       if (decl == error_mark_node)
22309         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
22310                                      token->location);
22311       else if (kind != 0)
22312         {
22313           tree u = build_omp_clause (token->location, kind);
22314           OMP_CLAUSE_DECL (u) = decl;
22315           OMP_CLAUSE_CHAIN (u) = list;
22316           list = u;
22317         }
22318       else
22319         list = tree_cons (decl, NULL_TREE, list);
22320
22321     get_comma:
22322       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22323         break;
22324       cp_lexer_consume_token (parser->lexer);
22325     }
22326
22327   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22328     {
22329       int ending;
22330
22331       /* Try to resync to an unnested comma.  Copied from
22332          cp_parser_parenthesized_expression_list.  */
22333     skip_comma:
22334       ending = cp_parser_skip_to_closing_parenthesis (parser,
22335                                                       /*recovering=*/true,
22336                                                       /*or_comma=*/true,
22337                                                       /*consume_paren=*/true);
22338       if (ending < 0)
22339         goto get_comma;
22340     }
22341
22342   return list;
22343 }
22344
22345 /* Similarly, but expect leading and trailing parenthesis.  This is a very
22346    common case for omp clauses.  */
22347
22348 static tree
22349 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
22350 {
22351   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22352     return cp_parser_omp_var_list_no_open (parser, kind, list);
22353   return list;
22354 }
22355
22356 /* OpenMP 3.0:
22357    collapse ( constant-expression ) */
22358
22359 static tree
22360 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
22361 {
22362   tree c, num;
22363   location_t loc;
22364   HOST_WIDE_INT n;
22365
22366   loc = cp_lexer_peek_token (parser->lexer)->location;
22367   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22368     return list;
22369
22370   num = cp_parser_constant_expression (parser, false, NULL);
22371
22372   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22373     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22374                                            /*or_comma=*/false,
22375                                            /*consume_paren=*/true);
22376
22377   if (num == error_mark_node)
22378     return list;
22379   num = fold_non_dependent_expr (num);
22380   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
22381       || !host_integerp (num, 0)
22382       || (n = tree_low_cst (num, 0)) <= 0
22383       || (int) n != n)
22384     {
22385       error_at (loc, "collapse argument needs positive constant integer expression");
22386       return list;
22387     }
22388
22389   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
22390   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
22391   OMP_CLAUSE_CHAIN (c) = list;
22392   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
22393
22394   return c;
22395 }
22396
22397 /* OpenMP 2.5:
22398    default ( shared | none ) */
22399
22400 static tree
22401 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
22402 {
22403   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
22404   tree c;
22405
22406   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22407     return list;
22408   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22409     {
22410       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22411       const char *p = IDENTIFIER_POINTER (id);
22412
22413       switch (p[0])
22414         {
22415         case 'n':
22416           if (strcmp ("none", p) != 0)
22417             goto invalid_kind;
22418           kind = OMP_CLAUSE_DEFAULT_NONE;
22419           break;
22420
22421         case 's':
22422           if (strcmp ("shared", p) != 0)
22423             goto invalid_kind;
22424           kind = OMP_CLAUSE_DEFAULT_SHARED;
22425           break;
22426
22427         default:
22428           goto invalid_kind;
22429         }
22430
22431       cp_lexer_consume_token (parser->lexer);
22432     }
22433   else
22434     {
22435     invalid_kind:
22436       cp_parser_error (parser, "expected %<none%> or %<shared%>");
22437     }
22438
22439   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22440     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22441                                            /*or_comma=*/false,
22442                                            /*consume_paren=*/true);
22443
22444   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
22445     return list;
22446
22447   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
22448   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
22449   OMP_CLAUSE_CHAIN (c) = list;
22450   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
22451
22452   return c;
22453 }
22454
22455 /* OpenMP 2.5:
22456    if ( expression ) */
22457
22458 static tree
22459 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
22460 {
22461   tree t, c;
22462
22463   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22464     return list;
22465
22466   t = cp_parser_condition (parser);
22467
22468   if (t == error_mark_node
22469       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22470     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22471                                            /*or_comma=*/false,
22472                                            /*consume_paren=*/true);
22473
22474   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
22475
22476   c = build_omp_clause (location, OMP_CLAUSE_IF);
22477   OMP_CLAUSE_IF_EXPR (c) = t;
22478   OMP_CLAUSE_CHAIN (c) = list;
22479
22480   return c;
22481 }
22482
22483 /* OpenMP 2.5:
22484    nowait */
22485
22486 static tree
22487 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
22488                              tree list, location_t location)
22489 {
22490   tree c;
22491
22492   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
22493
22494   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
22495   OMP_CLAUSE_CHAIN (c) = list;
22496   return c;
22497 }
22498
22499 /* OpenMP 2.5:
22500    num_threads ( expression ) */
22501
22502 static tree
22503 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
22504                                   location_t location)
22505 {
22506   tree t, c;
22507
22508   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22509     return list;
22510
22511   t = cp_parser_expression (parser, false, NULL);
22512
22513   if (t == error_mark_node
22514       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22515     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22516                                            /*or_comma=*/false,
22517                                            /*consume_paren=*/true);
22518
22519   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
22520                              "num_threads", location);
22521
22522   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
22523   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
22524   OMP_CLAUSE_CHAIN (c) = list;
22525
22526   return c;
22527 }
22528
22529 /* OpenMP 2.5:
22530    ordered */
22531
22532 static tree
22533 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
22534                               tree list, location_t location)
22535 {
22536   tree c;
22537
22538   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
22539                              "ordered", location);
22540
22541   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
22542   OMP_CLAUSE_CHAIN (c) = list;
22543   return c;
22544 }
22545
22546 /* OpenMP 2.5:
22547    reduction ( reduction-operator : variable-list )
22548
22549    reduction-operator:
22550      One of: + * - & ^ | && || */
22551
22552 static tree
22553 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
22554 {
22555   enum tree_code code;
22556   tree nlist, c;
22557
22558   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22559     return list;
22560
22561   switch (cp_lexer_peek_token (parser->lexer)->type)
22562     {
22563     case CPP_PLUS:
22564       code = PLUS_EXPR;
22565       break;
22566     case CPP_MULT:
22567       code = MULT_EXPR;
22568       break;
22569     case CPP_MINUS:
22570       code = MINUS_EXPR;
22571       break;
22572     case CPP_AND:
22573       code = BIT_AND_EXPR;
22574       break;
22575     case CPP_XOR:
22576       code = BIT_XOR_EXPR;
22577       break;
22578     case CPP_OR:
22579       code = BIT_IOR_EXPR;
22580       break;
22581     case CPP_AND_AND:
22582       code = TRUTH_ANDIF_EXPR;
22583       break;
22584     case CPP_OR_OR:
22585       code = TRUTH_ORIF_EXPR;
22586       break;
22587     default:
22588       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
22589                                "%<|%>, %<&&%>, or %<||%>");
22590     resync_fail:
22591       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22592                                              /*or_comma=*/false,
22593                                              /*consume_paren=*/true);
22594       return list;
22595     }
22596   cp_lexer_consume_token (parser->lexer);
22597
22598   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22599     goto resync_fail;
22600
22601   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
22602   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
22603     OMP_CLAUSE_REDUCTION_CODE (c) = code;
22604
22605   return nlist;
22606 }
22607
22608 /* OpenMP 2.5:
22609    schedule ( schedule-kind )
22610    schedule ( schedule-kind , expression )
22611
22612    schedule-kind:
22613      static | dynamic | guided | runtime | auto  */
22614
22615 static tree
22616 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
22617 {
22618   tree c, t;
22619
22620   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22621     return list;
22622
22623   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
22624
22625   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22626     {
22627       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22628       const char *p = IDENTIFIER_POINTER (id);
22629
22630       switch (p[0])
22631         {
22632         case 'd':
22633           if (strcmp ("dynamic", p) != 0)
22634             goto invalid_kind;
22635           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
22636           break;
22637
22638         case 'g':
22639           if (strcmp ("guided", p) != 0)
22640             goto invalid_kind;
22641           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
22642           break;
22643
22644         case 'r':
22645           if (strcmp ("runtime", p) != 0)
22646             goto invalid_kind;
22647           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
22648           break;
22649
22650         default:
22651           goto invalid_kind;
22652         }
22653     }
22654   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
22655     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
22656   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
22657     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
22658   else
22659     goto invalid_kind;
22660   cp_lexer_consume_token (parser->lexer);
22661
22662   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22663     {
22664       cp_token *token;
22665       cp_lexer_consume_token (parser->lexer);
22666
22667       token = cp_lexer_peek_token (parser->lexer);
22668       t = cp_parser_assignment_expression (parser, false, NULL);
22669
22670       if (t == error_mark_node)
22671         goto resync_fail;
22672       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
22673         error_at (token->location, "schedule %<runtime%> does not take "
22674                   "a %<chunk_size%> parameter");
22675       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
22676         error_at (token->location, "schedule %<auto%> does not take "
22677                   "a %<chunk_size%> parameter");
22678       else
22679         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
22680
22681       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22682         goto resync_fail;
22683     }
22684   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
22685     goto resync_fail;
22686
22687   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
22688   OMP_CLAUSE_CHAIN (c) = list;
22689   return c;
22690
22691  invalid_kind:
22692   cp_parser_error (parser, "invalid schedule kind");
22693  resync_fail:
22694   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22695                                          /*or_comma=*/false,
22696                                          /*consume_paren=*/true);
22697   return list;
22698 }
22699
22700 /* OpenMP 3.0:
22701    untied */
22702
22703 static tree
22704 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
22705                              tree list, location_t location)
22706 {
22707   tree c;
22708
22709   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
22710
22711   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
22712   OMP_CLAUSE_CHAIN (c) = list;
22713   return c;
22714 }
22715
22716 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
22717    is a bitmask in MASK.  Return the list of clauses found; the result
22718    of clause default goes in *pdefault.  */
22719
22720 static tree
22721 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
22722                            const char *where, cp_token *pragma_tok)
22723 {
22724   tree clauses = NULL;
22725   bool first = true;
22726   cp_token *token = NULL;
22727
22728   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
22729     {
22730       pragma_omp_clause c_kind;
22731       const char *c_name;
22732       tree prev = clauses;
22733
22734       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22735         cp_lexer_consume_token (parser->lexer);
22736
22737       token = cp_lexer_peek_token (parser->lexer);
22738       c_kind = cp_parser_omp_clause_name (parser);
22739       first = false;
22740
22741       switch (c_kind)
22742         {
22743         case PRAGMA_OMP_CLAUSE_COLLAPSE:
22744           clauses = cp_parser_omp_clause_collapse (parser, clauses,
22745                                                    token->location);
22746           c_name = "collapse";
22747           break;
22748         case PRAGMA_OMP_CLAUSE_COPYIN:
22749           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
22750           c_name = "copyin";
22751           break;
22752         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
22753           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
22754                                             clauses);
22755           c_name = "copyprivate";
22756           break;
22757         case PRAGMA_OMP_CLAUSE_DEFAULT:
22758           clauses = cp_parser_omp_clause_default (parser, clauses,
22759                                                   token->location);
22760           c_name = "default";
22761           break;
22762         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
22763           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
22764                                             clauses);
22765           c_name = "firstprivate";
22766           break;
22767         case PRAGMA_OMP_CLAUSE_IF:
22768           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
22769           c_name = "if";
22770           break;
22771         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
22772           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
22773                                             clauses);
22774           c_name = "lastprivate";
22775           break;
22776         case PRAGMA_OMP_CLAUSE_NOWAIT:
22777           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
22778           c_name = "nowait";
22779           break;
22780         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
22781           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
22782                                                       token->location);
22783           c_name = "num_threads";
22784           break;
22785         case PRAGMA_OMP_CLAUSE_ORDERED:
22786           clauses = cp_parser_omp_clause_ordered (parser, clauses,
22787                                                   token->location);
22788           c_name = "ordered";
22789           break;
22790         case PRAGMA_OMP_CLAUSE_PRIVATE:
22791           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
22792                                             clauses);
22793           c_name = "private";
22794           break;
22795         case PRAGMA_OMP_CLAUSE_REDUCTION:
22796           clauses = cp_parser_omp_clause_reduction (parser, clauses);
22797           c_name = "reduction";
22798           break;
22799         case PRAGMA_OMP_CLAUSE_SCHEDULE:
22800           clauses = cp_parser_omp_clause_schedule (parser, clauses,
22801                                                    token->location);
22802           c_name = "schedule";
22803           break;
22804         case PRAGMA_OMP_CLAUSE_SHARED:
22805           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
22806                                             clauses);
22807           c_name = "shared";
22808           break;
22809         case PRAGMA_OMP_CLAUSE_UNTIED:
22810           clauses = cp_parser_omp_clause_untied (parser, clauses,
22811                                                  token->location);
22812           c_name = "nowait";
22813           break;
22814         default:
22815           cp_parser_error (parser, "expected %<#pragma omp%> clause");
22816           goto saw_error;
22817         }
22818
22819       if (((mask >> c_kind) & 1) == 0)
22820         {
22821           /* Remove the invalid clause(s) from the list to avoid
22822              confusing the rest of the compiler.  */
22823           clauses = prev;
22824           error_at (token->location, "%qs is not valid for %qs", c_name, where);
22825         }
22826     }
22827  saw_error:
22828   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22829   return finish_omp_clauses (clauses);
22830 }
22831
22832 /* OpenMP 2.5:
22833    structured-block:
22834      statement
22835
22836    In practice, we're also interested in adding the statement to an
22837    outer node.  So it is convenient if we work around the fact that
22838    cp_parser_statement calls add_stmt.  */
22839
22840 static unsigned
22841 cp_parser_begin_omp_structured_block (cp_parser *parser)
22842 {
22843   unsigned save = parser->in_statement;
22844
22845   /* Only move the values to IN_OMP_BLOCK if they weren't false.
22846      This preserves the "not within loop or switch" style error messages
22847      for nonsense cases like
22848         void foo() {
22849         #pragma omp single
22850           break;
22851         }
22852   */
22853   if (parser->in_statement)
22854     parser->in_statement = IN_OMP_BLOCK;
22855
22856   return save;
22857 }
22858
22859 static void
22860 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
22861 {
22862   parser->in_statement = save;
22863 }
22864
22865 static tree
22866 cp_parser_omp_structured_block (cp_parser *parser)
22867 {
22868   tree stmt = begin_omp_structured_block ();
22869   unsigned int save = cp_parser_begin_omp_structured_block (parser);
22870
22871   cp_parser_statement (parser, NULL_TREE, false, NULL);
22872
22873   cp_parser_end_omp_structured_block (parser, save);
22874   return finish_omp_structured_block (stmt);
22875 }
22876
22877 /* OpenMP 2.5:
22878    # pragma omp atomic new-line
22879      expression-stmt
22880
22881    expression-stmt:
22882      x binop= expr | x++ | ++x | x-- | --x
22883    binop:
22884      +, *, -, /, &, ^, |, <<, >>
22885
22886   where x is an lvalue expression with scalar type.  */
22887
22888 static void
22889 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
22890 {
22891   tree lhs, rhs;
22892   enum tree_code code;
22893
22894   cp_parser_require_pragma_eol (parser, pragma_tok);
22895
22896   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
22897                                     /*cast_p=*/false, NULL);
22898   switch (TREE_CODE (lhs))
22899     {
22900     case ERROR_MARK:
22901       goto saw_error;
22902
22903     case PREINCREMENT_EXPR:
22904     case POSTINCREMENT_EXPR:
22905       lhs = TREE_OPERAND (lhs, 0);
22906       code = PLUS_EXPR;
22907       rhs = integer_one_node;
22908       break;
22909
22910     case PREDECREMENT_EXPR:
22911     case POSTDECREMENT_EXPR:
22912       lhs = TREE_OPERAND (lhs, 0);
22913       code = MINUS_EXPR;
22914       rhs = integer_one_node;
22915       break;
22916
22917     case COMPOUND_EXPR:
22918       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
22919          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
22920          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
22921          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
22922          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
22923                                              (TREE_OPERAND (lhs, 1), 0), 0)))
22924             == BOOLEAN_TYPE)
22925        /* Undo effects of boolean_increment for post {in,de}crement.  */
22926        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
22927       /* FALLTHRU */
22928     case MODIFY_EXPR:
22929       if (TREE_CODE (lhs) == MODIFY_EXPR
22930          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
22931        {
22932          /* Undo effects of boolean_increment.  */
22933          if (integer_onep (TREE_OPERAND (lhs, 1)))
22934            {
22935              /* This is pre or post increment.  */
22936              rhs = TREE_OPERAND (lhs, 1);
22937              lhs = TREE_OPERAND (lhs, 0);
22938              code = NOP_EXPR;
22939              break;
22940            }
22941        }
22942       /* FALLTHRU */
22943     default:
22944       switch (cp_lexer_peek_token (parser->lexer)->type)
22945         {
22946         case CPP_MULT_EQ:
22947           code = MULT_EXPR;
22948           break;
22949         case CPP_DIV_EQ:
22950           code = TRUNC_DIV_EXPR;
22951           break;
22952         case CPP_PLUS_EQ:
22953           code = PLUS_EXPR;
22954           break;
22955         case CPP_MINUS_EQ:
22956           code = MINUS_EXPR;
22957           break;
22958         case CPP_LSHIFT_EQ:
22959           code = LSHIFT_EXPR;
22960           break;
22961         case CPP_RSHIFT_EQ:
22962           code = RSHIFT_EXPR;
22963           break;
22964         case CPP_AND_EQ:
22965           code = BIT_AND_EXPR;
22966           break;
22967         case CPP_OR_EQ:
22968           code = BIT_IOR_EXPR;
22969           break;
22970         case CPP_XOR_EQ:
22971           code = BIT_XOR_EXPR;
22972           break;
22973         default:
22974           cp_parser_error (parser,
22975                            "invalid operator for %<#pragma omp atomic%>");
22976           goto saw_error;
22977         }
22978       cp_lexer_consume_token (parser->lexer);
22979
22980       rhs = cp_parser_expression (parser, false, NULL);
22981       if (rhs == error_mark_node)
22982         goto saw_error;
22983       break;
22984     }
22985   finish_omp_atomic (code, lhs, rhs);
22986   cp_parser_consume_semicolon_at_end_of_statement (parser);
22987   return;
22988
22989  saw_error:
22990   cp_parser_skip_to_end_of_block_or_statement (parser);
22991 }
22992
22993
22994 /* OpenMP 2.5:
22995    # pragma omp barrier new-line  */
22996
22997 static void
22998 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
22999 {
23000   cp_parser_require_pragma_eol (parser, pragma_tok);
23001   finish_omp_barrier ();
23002 }
23003
23004 /* OpenMP 2.5:
23005    # pragma omp critical [(name)] new-line
23006      structured-block  */
23007
23008 static tree
23009 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23010 {
23011   tree stmt, name = NULL;
23012
23013   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23014     {
23015       cp_lexer_consume_token (parser->lexer);
23016
23017       name = cp_parser_identifier (parser);
23018
23019       if (name == error_mark_node
23020           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23021         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23022                                                /*or_comma=*/false,
23023                                                /*consume_paren=*/true);
23024       if (name == error_mark_node)
23025         name = NULL;
23026     }
23027   cp_parser_require_pragma_eol (parser, pragma_tok);
23028
23029   stmt = cp_parser_omp_structured_block (parser);
23030   return c_finish_omp_critical (input_location, stmt, name);
23031 }
23032
23033 /* OpenMP 2.5:
23034    # pragma omp flush flush-vars[opt] new-line
23035
23036    flush-vars:
23037      ( variable-list ) */
23038
23039 static void
23040 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23041 {
23042   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23043     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23044   cp_parser_require_pragma_eol (parser, pragma_tok);
23045
23046   finish_omp_flush ();
23047 }
23048
23049 /* Helper function, to parse omp for increment expression.  */
23050
23051 static tree
23052 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23053 {
23054   tree cond = cp_parser_binary_expression (parser, false, true,
23055                                            PREC_NOT_OPERATOR, NULL);
23056   bool overloaded_p;
23057
23058   if (cond == error_mark_node
23059       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23060     {
23061       cp_parser_skip_to_end_of_statement (parser);
23062       return error_mark_node;
23063     }
23064
23065   switch (TREE_CODE (cond))
23066     {
23067     case GT_EXPR:
23068     case GE_EXPR:
23069     case LT_EXPR:
23070     case LE_EXPR:
23071       break;
23072     default:
23073       return error_mark_node;
23074     }
23075
23076   /* If decl is an iterator, preserve LHS and RHS of the relational
23077      expr until finish_omp_for.  */
23078   if (decl
23079       && (type_dependent_expression_p (decl)
23080           || CLASS_TYPE_P (TREE_TYPE (decl))))
23081     return cond;
23082
23083   return build_x_binary_op (TREE_CODE (cond),
23084                             TREE_OPERAND (cond, 0), ERROR_MARK,
23085                             TREE_OPERAND (cond, 1), ERROR_MARK,
23086                             &overloaded_p, tf_warning_or_error);
23087 }
23088
23089 /* Helper function, to parse omp for increment expression.  */
23090
23091 static tree
23092 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
23093 {
23094   cp_token *token = cp_lexer_peek_token (parser->lexer);
23095   enum tree_code op;
23096   tree lhs, rhs;
23097   cp_id_kind idk;
23098   bool decl_first;
23099
23100   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23101     {
23102       op = (token->type == CPP_PLUS_PLUS
23103             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
23104       cp_lexer_consume_token (parser->lexer);
23105       lhs = cp_parser_cast_expression (parser, false, false, NULL);
23106       if (lhs != decl)
23107         return error_mark_node;
23108       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23109     }
23110
23111   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
23112   if (lhs != decl)
23113     return error_mark_node;
23114
23115   token = cp_lexer_peek_token (parser->lexer);
23116   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23117     {
23118       op = (token->type == CPP_PLUS_PLUS
23119             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
23120       cp_lexer_consume_token (parser->lexer);
23121       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23122     }
23123
23124   op = cp_parser_assignment_operator_opt (parser);
23125   if (op == ERROR_MARK)
23126     return error_mark_node;
23127
23128   if (op != NOP_EXPR)
23129     {
23130       rhs = cp_parser_assignment_expression (parser, false, NULL);
23131       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
23132       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23133     }
23134
23135   lhs = cp_parser_binary_expression (parser, false, false,
23136                                      PREC_ADDITIVE_EXPRESSION, NULL);
23137   token = cp_lexer_peek_token (parser->lexer);
23138   decl_first = lhs == decl;
23139   if (decl_first)
23140     lhs = NULL_TREE;
23141   if (token->type != CPP_PLUS
23142       && token->type != CPP_MINUS)
23143     return error_mark_node;
23144
23145   do
23146     {
23147       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
23148       cp_lexer_consume_token (parser->lexer);
23149       rhs = cp_parser_binary_expression (parser, false, false,
23150                                          PREC_ADDITIVE_EXPRESSION, NULL);
23151       token = cp_lexer_peek_token (parser->lexer);
23152       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
23153         {
23154           if (lhs == NULL_TREE)
23155             {
23156               if (op == PLUS_EXPR)
23157                 lhs = rhs;
23158               else
23159                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
23160             }
23161           else
23162             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
23163                                      NULL, tf_warning_or_error);
23164         }
23165     }
23166   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
23167
23168   if (!decl_first)
23169     {
23170       if (rhs != decl || op == MINUS_EXPR)
23171         return error_mark_node;
23172       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
23173     }
23174   else
23175     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
23176
23177   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23178 }
23179
23180 /* Parse the restricted form of the for statement allowed by OpenMP.  */
23181
23182 static tree
23183 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
23184 {
23185   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
23186   tree real_decl, initv, condv, incrv, declv;
23187   tree this_pre_body, cl;
23188   location_t loc_first;
23189   bool collapse_err = false;
23190   int i, collapse = 1, nbraces = 0;
23191   VEC(tree,gc) *for_block = make_tree_vector ();
23192
23193   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
23194     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
23195       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
23196
23197   gcc_assert (collapse >= 1);
23198
23199   declv = make_tree_vec (collapse);
23200   initv = make_tree_vec (collapse);
23201   condv = make_tree_vec (collapse);
23202   incrv = make_tree_vec (collapse);
23203
23204   loc_first = cp_lexer_peek_token (parser->lexer)->location;
23205
23206   for (i = 0; i < collapse; i++)
23207     {
23208       int bracecount = 0;
23209       bool add_private_clause = false;
23210       location_t loc;
23211
23212       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23213         {
23214           cp_parser_error (parser, "for statement expected");
23215           return NULL;
23216         }
23217       loc = cp_lexer_consume_token (parser->lexer)->location;
23218
23219       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23220         return NULL;
23221
23222       init = decl = real_decl = NULL;
23223       this_pre_body = push_stmt_list ();
23224       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23225         {
23226           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
23227
23228              init-expr:
23229                        var = lb
23230                        integer-type var = lb
23231                        random-access-iterator-type var = lb
23232                        pointer-type var = lb
23233           */
23234           cp_decl_specifier_seq type_specifiers;
23235
23236           /* First, try to parse as an initialized declaration.  See
23237              cp_parser_condition, from whence the bulk of this is copied.  */
23238
23239           cp_parser_parse_tentatively (parser);
23240           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23241                                         /*is_trailing_return=*/false,
23242                                         &type_specifiers);
23243           if (cp_parser_parse_definitely (parser))
23244             {
23245               /* If parsing a type specifier seq succeeded, then this
23246                  MUST be a initialized declaration.  */
23247               tree asm_specification, attributes;
23248               cp_declarator *declarator;
23249
23250               declarator = cp_parser_declarator (parser,
23251                                                  CP_PARSER_DECLARATOR_NAMED,
23252                                                  /*ctor_dtor_or_conv_p=*/NULL,
23253                                                  /*parenthesized_p=*/NULL,
23254                                                  /*member_p=*/false);
23255               attributes = cp_parser_attributes_opt (parser);
23256               asm_specification = cp_parser_asm_specification_opt (parser);
23257
23258               if (declarator == cp_error_declarator) 
23259                 cp_parser_skip_to_end_of_statement (parser);
23260
23261               else 
23262                 {
23263                   tree pushed_scope, auto_node;
23264
23265                   decl = start_decl (declarator, &type_specifiers,
23266                                      SD_INITIALIZED, attributes,
23267                                      /*prefix_attributes=*/NULL_TREE,
23268                                      &pushed_scope);
23269
23270                   auto_node = type_uses_auto (TREE_TYPE (decl));
23271                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23272                     {
23273                       if (cp_lexer_next_token_is (parser->lexer, 
23274                                                   CPP_OPEN_PAREN))
23275                         error ("parenthesized initialization is not allowed in "
23276                                "OpenMP %<for%> loop");
23277                       else
23278                         /* Trigger an error.  */
23279                         cp_parser_require (parser, CPP_EQ, RT_EQ);
23280
23281                       init = error_mark_node;
23282                       cp_parser_skip_to_end_of_statement (parser);
23283                     }
23284                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
23285                            || type_dependent_expression_p (decl)
23286                            || auto_node)
23287                     {
23288                       bool is_direct_init, is_non_constant_init;
23289
23290                       init = cp_parser_initializer (parser,
23291                                                     &is_direct_init,
23292                                                     &is_non_constant_init);
23293
23294                       if (auto_node && describable_type (init))
23295                         {
23296                           TREE_TYPE (decl)
23297                             = do_auto_deduction (TREE_TYPE (decl), init,
23298                                                  auto_node);
23299
23300                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
23301                               && !type_dependent_expression_p (decl))
23302                             goto non_class;
23303                         }
23304                       
23305                       cp_finish_decl (decl, init, !is_non_constant_init,
23306                                       asm_specification,
23307                                       LOOKUP_ONLYCONVERTING);
23308                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
23309                         {
23310                           VEC_safe_push (tree, gc, for_block, this_pre_body);
23311                           init = NULL_TREE;
23312                         }
23313                       else
23314                         init = pop_stmt_list (this_pre_body);
23315                       this_pre_body = NULL_TREE;
23316                     }
23317                   else
23318                     {
23319                       /* Consume '='.  */
23320                       cp_lexer_consume_token (parser->lexer);
23321                       init = cp_parser_assignment_expression (parser, false, NULL);
23322
23323                     non_class:
23324                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
23325                         init = error_mark_node;
23326                       else
23327                         cp_finish_decl (decl, NULL_TREE,
23328                                         /*init_const_expr_p=*/false,
23329                                         asm_specification,
23330                                         LOOKUP_ONLYCONVERTING);
23331                     }
23332
23333                   if (pushed_scope)
23334                     pop_scope (pushed_scope);
23335                 }
23336             }
23337           else 
23338             {
23339               cp_id_kind idk;
23340               /* If parsing a type specifier sequence failed, then
23341                  this MUST be a simple expression.  */
23342               cp_parser_parse_tentatively (parser);
23343               decl = cp_parser_primary_expression (parser, false, false,
23344                                                    false, &idk);
23345               if (!cp_parser_error_occurred (parser)
23346                   && decl
23347                   && DECL_P (decl)
23348                   && CLASS_TYPE_P (TREE_TYPE (decl)))
23349                 {
23350                   tree rhs;
23351
23352                   cp_parser_parse_definitely (parser);
23353                   cp_parser_require (parser, CPP_EQ, RT_EQ);
23354                   rhs = cp_parser_assignment_expression (parser, false, NULL);
23355                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
23356                                                          rhs,
23357                                                          tf_warning_or_error));
23358                   add_private_clause = true;
23359                 }
23360               else
23361                 {
23362                   decl = NULL;
23363                   cp_parser_abort_tentative_parse (parser);
23364                   init = cp_parser_expression (parser, false, NULL);
23365                   if (init)
23366                     {
23367                       if (TREE_CODE (init) == MODIFY_EXPR
23368                           || TREE_CODE (init) == MODOP_EXPR)
23369                         real_decl = TREE_OPERAND (init, 0);
23370                     }
23371                 }
23372             }
23373         }
23374       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23375       if (this_pre_body)
23376         {
23377           this_pre_body = pop_stmt_list (this_pre_body);
23378           if (pre_body)
23379             {
23380               tree t = pre_body;
23381               pre_body = push_stmt_list ();
23382               add_stmt (t);
23383               add_stmt (this_pre_body);
23384               pre_body = pop_stmt_list (pre_body);
23385             }
23386           else
23387             pre_body = this_pre_body;
23388         }
23389
23390       if (decl)
23391         real_decl = decl;
23392       if (par_clauses != NULL && real_decl != NULL_TREE)
23393         {
23394           tree *c;
23395           for (c = par_clauses; *c ; )
23396             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
23397                 && OMP_CLAUSE_DECL (*c) == real_decl)
23398               {
23399                 error_at (loc, "iteration variable %qD"
23400                           " should not be firstprivate", real_decl);
23401                 *c = OMP_CLAUSE_CHAIN (*c);
23402               }
23403             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
23404                      && OMP_CLAUSE_DECL (*c) == real_decl)
23405               {
23406                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
23407                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
23408                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
23409                 OMP_CLAUSE_DECL (l) = real_decl;
23410                 OMP_CLAUSE_CHAIN (l) = clauses;
23411                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
23412                 clauses = l;
23413                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
23414                 CP_OMP_CLAUSE_INFO (*c) = NULL;
23415                 add_private_clause = false;
23416               }
23417             else
23418               {
23419                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
23420                     && OMP_CLAUSE_DECL (*c) == real_decl)
23421                   add_private_clause = false;
23422                 c = &OMP_CLAUSE_CHAIN (*c);
23423               }
23424         }
23425
23426       if (add_private_clause)
23427         {
23428           tree c;
23429           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23430             {
23431               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
23432                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
23433                   && OMP_CLAUSE_DECL (c) == decl)
23434                 break;
23435               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
23436                        && OMP_CLAUSE_DECL (c) == decl)
23437                 error_at (loc, "iteration variable %qD "
23438                           "should not be firstprivate",
23439                           decl);
23440               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
23441                        && OMP_CLAUSE_DECL (c) == decl)
23442                 error_at (loc, "iteration variable %qD should not be reduction",
23443                           decl);
23444             }
23445           if (c == NULL)
23446             {
23447               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
23448               OMP_CLAUSE_DECL (c) = decl;
23449               c = finish_omp_clauses (c);
23450               if (c)
23451                 {
23452                   OMP_CLAUSE_CHAIN (c) = clauses;
23453                   clauses = c;
23454                 }
23455             }
23456         }
23457
23458       cond = NULL;
23459       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23460         cond = cp_parser_omp_for_cond (parser, decl);
23461       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23462
23463       incr = NULL;
23464       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23465         {
23466           /* If decl is an iterator, preserve the operator on decl
23467              until finish_omp_for.  */
23468           if (decl
23469               && (type_dependent_expression_p (decl)
23470                   || CLASS_TYPE_P (TREE_TYPE (decl))))
23471             incr = cp_parser_omp_for_incr (parser, decl);
23472           else
23473             incr = cp_parser_expression (parser, false, NULL);
23474         }
23475
23476       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23477         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23478                                                /*or_comma=*/false,
23479                                                /*consume_paren=*/true);
23480
23481       TREE_VEC_ELT (declv, i) = decl;
23482       TREE_VEC_ELT (initv, i) = init;
23483       TREE_VEC_ELT (condv, i) = cond;
23484       TREE_VEC_ELT (incrv, i) = incr;
23485
23486       if (i == collapse - 1)
23487         break;
23488
23489       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
23490          in between the collapsed for loops to be still considered perfectly
23491          nested.  Hopefully the final version clarifies this.
23492          For now handle (multiple) {'s and empty statements.  */
23493       cp_parser_parse_tentatively (parser);
23494       do
23495         {
23496           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23497             break;
23498           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23499             {
23500               cp_lexer_consume_token (parser->lexer);
23501               bracecount++;
23502             }
23503           else if (bracecount
23504                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23505             cp_lexer_consume_token (parser->lexer);
23506           else
23507             {
23508               loc = cp_lexer_peek_token (parser->lexer)->location;
23509               error_at (loc, "not enough collapsed for loops");
23510               collapse_err = true;
23511               cp_parser_abort_tentative_parse (parser);
23512               declv = NULL_TREE;
23513               break;
23514             }
23515         }
23516       while (1);
23517
23518       if (declv)
23519         {
23520           cp_parser_parse_definitely (parser);
23521           nbraces += bracecount;
23522         }
23523     }
23524
23525   /* Note that we saved the original contents of this flag when we entered
23526      the structured block, and so we don't need to re-save it here.  */
23527   parser->in_statement = IN_OMP_FOR;
23528
23529   /* Note that the grammar doesn't call for a structured block here,
23530      though the loop as a whole is a structured block.  */
23531   body = push_stmt_list ();
23532   cp_parser_statement (parser, NULL_TREE, false, NULL);
23533   body = pop_stmt_list (body);
23534
23535   if (declv == NULL_TREE)
23536     ret = NULL_TREE;
23537   else
23538     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
23539                           pre_body, clauses);
23540
23541   while (nbraces)
23542     {
23543       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23544         {
23545           cp_lexer_consume_token (parser->lexer);
23546           nbraces--;
23547         }
23548       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23549         cp_lexer_consume_token (parser->lexer);
23550       else
23551         {
23552           if (!collapse_err)
23553             {
23554               error_at (cp_lexer_peek_token (parser->lexer)->location,
23555                         "collapsed loops not perfectly nested");
23556             }
23557           collapse_err = true;
23558           cp_parser_statement_seq_opt (parser, NULL);
23559           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23560             break;
23561         }
23562     }
23563
23564   while (!VEC_empty (tree, for_block))
23565     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
23566   release_tree_vector (for_block);
23567
23568   return ret;
23569 }
23570
23571 /* OpenMP 2.5:
23572    #pragma omp for for-clause[optseq] new-line
23573      for-loop  */
23574
23575 #define OMP_FOR_CLAUSE_MASK                             \
23576         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23577         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23578         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
23579         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
23580         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
23581         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
23582         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
23583         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
23584
23585 static tree
23586 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
23587 {
23588   tree clauses, sb, ret;
23589   unsigned int save;
23590
23591   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
23592                                        "#pragma omp for", pragma_tok);
23593
23594   sb = begin_omp_structured_block ();
23595   save = cp_parser_begin_omp_structured_block (parser);
23596
23597   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
23598
23599   cp_parser_end_omp_structured_block (parser, save);
23600   add_stmt (finish_omp_structured_block (sb));
23601
23602   return ret;
23603 }
23604
23605 /* OpenMP 2.5:
23606    # pragma omp master new-line
23607      structured-block  */
23608
23609 static tree
23610 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
23611 {
23612   cp_parser_require_pragma_eol (parser, pragma_tok);
23613   return c_finish_omp_master (input_location,
23614                               cp_parser_omp_structured_block (parser));
23615 }
23616
23617 /* OpenMP 2.5:
23618    # pragma omp ordered new-line
23619      structured-block  */
23620
23621 static tree
23622 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
23623 {
23624   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23625   cp_parser_require_pragma_eol (parser, pragma_tok);
23626   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
23627 }
23628
23629 /* OpenMP 2.5:
23630
23631    section-scope:
23632      { section-sequence }
23633
23634    section-sequence:
23635      section-directive[opt] structured-block
23636      section-sequence section-directive structured-block  */
23637
23638 static tree
23639 cp_parser_omp_sections_scope (cp_parser *parser)
23640 {
23641   tree stmt, substmt;
23642   bool error_suppress = false;
23643   cp_token *tok;
23644
23645   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23646     return NULL_TREE;
23647
23648   stmt = push_stmt_list ();
23649
23650   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
23651     {
23652       unsigned save;
23653
23654       substmt = begin_omp_structured_block ();
23655       save = cp_parser_begin_omp_structured_block (parser);
23656
23657       while (1)
23658         {
23659           cp_parser_statement (parser, NULL_TREE, false, NULL);
23660
23661           tok = cp_lexer_peek_token (parser->lexer);
23662           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23663             break;
23664           if (tok->type == CPP_CLOSE_BRACE)
23665             break;
23666           if (tok->type == CPP_EOF)
23667             break;
23668         }
23669
23670       cp_parser_end_omp_structured_block (parser, save);
23671       substmt = finish_omp_structured_block (substmt);
23672       substmt = build1 (OMP_SECTION, void_type_node, substmt);
23673       add_stmt (substmt);
23674     }
23675
23676   while (1)
23677     {
23678       tok = cp_lexer_peek_token (parser->lexer);
23679       if (tok->type == CPP_CLOSE_BRACE)
23680         break;
23681       if (tok->type == CPP_EOF)
23682         break;
23683
23684       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23685         {
23686           cp_lexer_consume_token (parser->lexer);
23687           cp_parser_require_pragma_eol (parser, tok);
23688           error_suppress = false;
23689         }
23690       else if (!error_suppress)
23691         {
23692           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
23693           error_suppress = true;
23694         }
23695
23696       substmt = cp_parser_omp_structured_block (parser);
23697       substmt = build1 (OMP_SECTION, void_type_node, substmt);
23698       add_stmt (substmt);
23699     }
23700   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23701
23702   substmt = pop_stmt_list (stmt);
23703
23704   stmt = make_node (OMP_SECTIONS);
23705   TREE_TYPE (stmt) = void_type_node;
23706   OMP_SECTIONS_BODY (stmt) = substmt;
23707
23708   add_stmt (stmt);
23709   return stmt;
23710 }
23711
23712 /* OpenMP 2.5:
23713    # pragma omp sections sections-clause[optseq] newline
23714      sections-scope  */
23715
23716 #define OMP_SECTIONS_CLAUSE_MASK                        \
23717         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23718         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23719         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
23720         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
23721         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23722
23723 static tree
23724 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
23725 {
23726   tree clauses, ret;
23727
23728   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
23729                                        "#pragma omp sections", pragma_tok);
23730
23731   ret = cp_parser_omp_sections_scope (parser);
23732   if (ret)
23733     OMP_SECTIONS_CLAUSES (ret) = clauses;
23734
23735   return ret;
23736 }
23737
23738 /* OpenMP 2.5:
23739    # pragma parallel parallel-clause new-line
23740    # pragma parallel for parallel-for-clause new-line
23741    # pragma parallel sections parallel-sections-clause new-line  */
23742
23743 #define OMP_PARALLEL_CLAUSE_MASK                        \
23744         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
23745         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23746         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23747         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
23748         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
23749         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
23750         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
23751         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
23752
23753 static tree
23754 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
23755 {
23756   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
23757   const char *p_name = "#pragma omp parallel";
23758   tree stmt, clauses, par_clause, ws_clause, block;
23759   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
23760   unsigned int save;
23761   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23762
23763   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23764     {
23765       cp_lexer_consume_token (parser->lexer);
23766       p_kind = PRAGMA_OMP_PARALLEL_FOR;
23767       p_name = "#pragma omp parallel for";
23768       mask |= OMP_FOR_CLAUSE_MASK;
23769       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23770     }
23771   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23772     {
23773       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23774       const char *p = IDENTIFIER_POINTER (id);
23775       if (strcmp (p, "sections") == 0)
23776         {
23777           cp_lexer_consume_token (parser->lexer);
23778           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
23779           p_name = "#pragma omp parallel sections";
23780           mask |= OMP_SECTIONS_CLAUSE_MASK;
23781           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23782         }
23783     }
23784
23785   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
23786   block = begin_omp_parallel ();
23787   save = cp_parser_begin_omp_structured_block (parser);
23788
23789   switch (p_kind)
23790     {
23791     case PRAGMA_OMP_PARALLEL:
23792       cp_parser_statement (parser, NULL_TREE, false, NULL);
23793       par_clause = clauses;
23794       break;
23795
23796     case PRAGMA_OMP_PARALLEL_FOR:
23797       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23798       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
23799       break;
23800
23801     case PRAGMA_OMP_PARALLEL_SECTIONS:
23802       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23803       stmt = cp_parser_omp_sections_scope (parser);
23804       if (stmt)
23805         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
23806       break;
23807
23808     default:
23809       gcc_unreachable ();
23810     }
23811
23812   cp_parser_end_omp_structured_block (parser, save);
23813   stmt = finish_omp_parallel (par_clause, block);
23814   if (p_kind != PRAGMA_OMP_PARALLEL)
23815     OMP_PARALLEL_COMBINED (stmt) = 1;
23816   return stmt;
23817 }
23818
23819 /* OpenMP 2.5:
23820    # pragma omp single single-clause[optseq] new-line
23821      structured-block  */
23822
23823 #define OMP_SINGLE_CLAUSE_MASK                          \
23824         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23825         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23826         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
23827         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23828
23829 static tree
23830 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
23831 {
23832   tree stmt = make_node (OMP_SINGLE);
23833   TREE_TYPE (stmt) = void_type_node;
23834
23835   OMP_SINGLE_CLAUSES (stmt)
23836     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
23837                                  "#pragma omp single", pragma_tok);
23838   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
23839
23840   return add_stmt (stmt);
23841 }
23842
23843 /* OpenMP 3.0:
23844    # pragma omp task task-clause[optseq] new-line
23845      structured-block  */
23846
23847 #define OMP_TASK_CLAUSE_MASK                            \
23848         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
23849         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
23850         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
23851         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
23852         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
23853         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
23854
23855 static tree
23856 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
23857 {
23858   tree clauses, block;
23859   unsigned int save;
23860
23861   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
23862                                        "#pragma omp task", pragma_tok);
23863   block = begin_omp_task ();
23864   save = cp_parser_begin_omp_structured_block (parser);
23865   cp_parser_statement (parser, NULL_TREE, false, NULL);
23866   cp_parser_end_omp_structured_block (parser, save);
23867   return finish_omp_task (clauses, block);
23868 }
23869
23870 /* OpenMP 3.0:
23871    # pragma omp taskwait new-line  */
23872
23873 static void
23874 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
23875 {
23876   cp_parser_require_pragma_eol (parser, pragma_tok);
23877   finish_omp_taskwait ();
23878 }
23879
23880 /* OpenMP 2.5:
23881    # pragma omp threadprivate (variable-list) */
23882
23883 static void
23884 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
23885 {
23886   tree vars;
23887
23888   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23889   cp_parser_require_pragma_eol (parser, pragma_tok);
23890
23891   finish_omp_threadprivate (vars);
23892 }
23893
23894 /* Main entry point to OpenMP statement pragmas.  */
23895
23896 static void
23897 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
23898 {
23899   tree stmt;
23900
23901   switch (pragma_tok->pragma_kind)
23902     {
23903     case PRAGMA_OMP_ATOMIC:
23904       cp_parser_omp_atomic (parser, pragma_tok);
23905       return;
23906     case PRAGMA_OMP_CRITICAL:
23907       stmt = cp_parser_omp_critical (parser, pragma_tok);
23908       break;
23909     case PRAGMA_OMP_FOR:
23910       stmt = cp_parser_omp_for (parser, pragma_tok);
23911       break;
23912     case PRAGMA_OMP_MASTER:
23913       stmt = cp_parser_omp_master (parser, pragma_tok);
23914       break;
23915     case PRAGMA_OMP_ORDERED:
23916       stmt = cp_parser_omp_ordered (parser, pragma_tok);
23917       break;
23918     case PRAGMA_OMP_PARALLEL:
23919       stmt = cp_parser_omp_parallel (parser, pragma_tok);
23920       break;
23921     case PRAGMA_OMP_SECTIONS:
23922       stmt = cp_parser_omp_sections (parser, pragma_tok);
23923       break;
23924     case PRAGMA_OMP_SINGLE:
23925       stmt = cp_parser_omp_single (parser, pragma_tok);
23926       break;
23927     case PRAGMA_OMP_TASK:
23928       stmt = cp_parser_omp_task (parser, pragma_tok);
23929       break;
23930     default:
23931       gcc_unreachable ();
23932     }
23933
23934   if (stmt)
23935     SET_EXPR_LOCATION (stmt, pragma_tok->location);
23936 }
23937 \f
23938 /* The parser.  */
23939
23940 static GTY (()) cp_parser *the_parser;
23941
23942 \f
23943 /* Special handling for the first token or line in the file.  The first
23944    thing in the file might be #pragma GCC pch_preprocess, which loads a
23945    PCH file, which is a GC collection point.  So we need to handle this
23946    first pragma without benefit of an existing lexer structure.
23947
23948    Always returns one token to the caller in *FIRST_TOKEN.  This is
23949    either the true first token of the file, or the first token after
23950    the initial pragma.  */
23951
23952 static void
23953 cp_parser_initial_pragma (cp_token *first_token)
23954 {
23955   tree name = NULL;
23956
23957   cp_lexer_get_preprocessor_token (NULL, first_token);
23958   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
23959     return;
23960
23961   cp_lexer_get_preprocessor_token (NULL, first_token);
23962   if (first_token->type == CPP_STRING)
23963     {
23964       name = first_token->u.value;
23965
23966       cp_lexer_get_preprocessor_token (NULL, first_token);
23967       if (first_token->type != CPP_PRAGMA_EOL)
23968         error_at (first_token->location,
23969                   "junk at end of %<#pragma GCC pch_preprocess%>");
23970     }
23971   else
23972     error_at (first_token->location, "expected string literal");
23973
23974   /* Skip to the end of the pragma.  */
23975   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
23976     cp_lexer_get_preprocessor_token (NULL, first_token);
23977
23978   /* Now actually load the PCH file.  */
23979   if (name)
23980     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
23981
23982   /* Read one more token to return to our caller.  We have to do this
23983      after reading the PCH file in, since its pointers have to be
23984      live.  */
23985   cp_lexer_get_preprocessor_token (NULL, first_token);
23986 }
23987
23988 /* Normal parsing of a pragma token.  Here we can (and must) use the
23989    regular lexer.  */
23990
23991 static bool
23992 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
23993 {
23994   cp_token *pragma_tok;
23995   unsigned int id;
23996
23997   pragma_tok = cp_lexer_consume_token (parser->lexer);
23998   gcc_assert (pragma_tok->type == CPP_PRAGMA);
23999   parser->lexer->in_pragma = true;
24000
24001   id = pragma_tok->pragma_kind;
24002   switch (id)
24003     {
24004     case PRAGMA_GCC_PCH_PREPROCESS:
24005       error_at (pragma_tok->location,
24006                 "%<#pragma GCC pch_preprocess%> must be first");
24007       break;
24008
24009     case PRAGMA_OMP_BARRIER:
24010       switch (context)
24011         {
24012         case pragma_compound:
24013           cp_parser_omp_barrier (parser, pragma_tok);
24014           return false;
24015         case pragma_stmt:
24016           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24017                     "used in compound statements");
24018           break;
24019         default:
24020           goto bad_stmt;
24021         }
24022       break;
24023
24024     case PRAGMA_OMP_FLUSH:
24025       switch (context)
24026         {
24027         case pragma_compound:
24028           cp_parser_omp_flush (parser, pragma_tok);
24029           return false;
24030         case pragma_stmt:
24031           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24032                     "used in compound statements");
24033           break;
24034         default:
24035           goto bad_stmt;
24036         }
24037       break;
24038
24039     case PRAGMA_OMP_TASKWAIT:
24040       switch (context)
24041         {
24042         case pragma_compound:
24043           cp_parser_omp_taskwait (parser, pragma_tok);
24044           return false;
24045         case pragma_stmt:
24046           error_at (pragma_tok->location,
24047                     "%<#pragma omp taskwait%> may only be "
24048                     "used in compound statements");
24049           break;
24050         default:
24051           goto bad_stmt;
24052         }
24053       break;
24054
24055     case PRAGMA_OMP_THREADPRIVATE:
24056       cp_parser_omp_threadprivate (parser, pragma_tok);
24057       return false;
24058
24059     case PRAGMA_OMP_ATOMIC:
24060     case PRAGMA_OMP_CRITICAL:
24061     case PRAGMA_OMP_FOR:
24062     case PRAGMA_OMP_MASTER:
24063     case PRAGMA_OMP_ORDERED:
24064     case PRAGMA_OMP_PARALLEL:
24065     case PRAGMA_OMP_SECTIONS:
24066     case PRAGMA_OMP_SINGLE:
24067     case PRAGMA_OMP_TASK:
24068       if (context == pragma_external)
24069         goto bad_stmt;
24070       cp_parser_omp_construct (parser, pragma_tok);
24071       return true;
24072
24073     case PRAGMA_OMP_SECTION:
24074       error_at (pragma_tok->location, 
24075                 "%<#pragma omp section%> may only be used in "
24076                 "%<#pragma omp sections%> construct");
24077       break;
24078
24079     default:
24080       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
24081       c_invoke_pragma_handler (id);
24082       break;
24083
24084     bad_stmt:
24085       cp_parser_error (parser, "expected declaration specifiers");
24086       break;
24087     }
24088
24089   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24090   return false;
24091 }
24092
24093 /* The interface the pragma parsers have to the lexer.  */
24094
24095 enum cpp_ttype
24096 pragma_lex (tree *value)
24097 {
24098   cp_token *tok;
24099   enum cpp_ttype ret;
24100
24101   tok = cp_lexer_peek_token (the_parser->lexer);
24102
24103   ret = tok->type;
24104   *value = tok->u.value;
24105
24106   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
24107     ret = CPP_EOF;
24108   else if (ret == CPP_STRING)
24109     *value = cp_parser_string_literal (the_parser, false, false);
24110   else
24111     {
24112       cp_lexer_consume_token (the_parser->lexer);
24113       if (ret == CPP_KEYWORD)
24114         ret = CPP_NAME;
24115     }
24116
24117   return ret;
24118 }
24119
24120 \f
24121 /* External interface.  */
24122
24123 /* Parse one entire translation unit.  */
24124
24125 void
24126 c_parse_file (void)
24127 {
24128   static bool already_called = false;
24129
24130   if (already_called)
24131     {
24132       sorry ("inter-module optimizations not implemented for C++");
24133       return;
24134     }
24135   already_called = true;
24136
24137   the_parser = cp_parser_new ();
24138   push_deferring_access_checks (flag_access_control
24139                                 ? dk_no_deferred : dk_no_check);
24140   cp_parser_translation_unit (the_parser);
24141   the_parser = NULL;
24142 }
24143
24144 #include "gt-cp-parser.h"