OSDN Git Service

Restore canonical type comparison for dependent type(def)s
[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   /* When parsing a decl-specifier-seq, only allow type-specifier or
1339      constexpr.  */
1340   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1341 };
1342
1343 /* This type is used for parameters and variables which hold
1344    combinations of the above flags.  */
1345 typedef int cp_parser_flags;
1346
1347 /* The different kinds of declarators we want to parse.  */
1348
1349 typedef enum cp_parser_declarator_kind
1350 {
1351   /* We want an abstract declarator.  */
1352   CP_PARSER_DECLARATOR_ABSTRACT,
1353   /* We want a named declarator.  */
1354   CP_PARSER_DECLARATOR_NAMED,
1355   /* We don't mind, but the name must be an unqualified-id.  */
1356   CP_PARSER_DECLARATOR_EITHER
1357 } cp_parser_declarator_kind;
1358
1359 /* The precedence values used to parse binary expressions.  The minimum value
1360    of PREC must be 1, because zero is reserved to quickly discriminate
1361    binary operators from other tokens.  */
1362
1363 enum cp_parser_prec
1364 {
1365   PREC_NOT_OPERATOR,
1366   PREC_LOGICAL_OR_EXPRESSION,
1367   PREC_LOGICAL_AND_EXPRESSION,
1368   PREC_INCLUSIVE_OR_EXPRESSION,
1369   PREC_EXCLUSIVE_OR_EXPRESSION,
1370   PREC_AND_EXPRESSION,
1371   PREC_EQUALITY_EXPRESSION,
1372   PREC_RELATIONAL_EXPRESSION,
1373   PREC_SHIFT_EXPRESSION,
1374   PREC_ADDITIVE_EXPRESSION,
1375   PREC_MULTIPLICATIVE_EXPRESSION,
1376   PREC_PM_EXPRESSION,
1377   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1378 };
1379
1380 /* A mapping from a token type to a corresponding tree node type, with a
1381    precedence value.  */
1382
1383 typedef struct cp_parser_binary_operations_map_node
1384 {
1385   /* The token type.  */
1386   enum cpp_ttype token_type;
1387   /* The corresponding tree code.  */
1388   enum tree_code tree_type;
1389   /* The precedence of this operator.  */
1390   enum cp_parser_prec prec;
1391 } cp_parser_binary_operations_map_node;
1392
1393 /* The status of a tentative parse.  */
1394
1395 typedef enum cp_parser_status_kind
1396 {
1397   /* No errors have occurred.  */
1398   CP_PARSER_STATUS_KIND_NO_ERROR,
1399   /* An error has occurred.  */
1400   CP_PARSER_STATUS_KIND_ERROR,
1401   /* We are committed to this tentative parse, whether or not an error
1402      has occurred.  */
1403   CP_PARSER_STATUS_KIND_COMMITTED
1404 } cp_parser_status_kind;
1405
1406 typedef struct cp_parser_expression_stack_entry
1407 {
1408   /* Left hand side of the binary operation we are currently
1409      parsing.  */
1410   tree lhs;
1411   /* Original tree code for left hand side, if it was a binary
1412      expression itself (used for -Wparentheses).  */
1413   enum tree_code lhs_type;
1414   /* Tree code for the binary operation we are parsing.  */
1415   enum tree_code tree_type;
1416   /* Precedence of the binary operation we are parsing.  */
1417   enum cp_parser_prec prec;
1418 } cp_parser_expression_stack_entry;
1419
1420 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1421    entries because precedence levels on the stack are monotonically
1422    increasing.  */
1423 typedef struct cp_parser_expression_stack_entry
1424   cp_parser_expression_stack[NUM_PREC_VALUES];
1425
1426 /* Context that is saved and restored when parsing tentatively.  */
1427 typedef struct GTY (()) cp_parser_context {
1428   /* If this is a tentative parsing context, the status of the
1429      tentative parse.  */
1430   enum cp_parser_status_kind status;
1431   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1432      that are looked up in this context must be looked up both in the
1433      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1434      the context of the containing expression.  */
1435   tree object_type;
1436
1437   /* The next parsing context in the stack.  */
1438   struct cp_parser_context *next;
1439 } cp_parser_context;
1440
1441 /* Prototypes.  */
1442
1443 /* Constructors and destructors.  */
1444
1445 static cp_parser_context *cp_parser_context_new
1446   (cp_parser_context *);
1447
1448 /* Class variables.  */
1449
1450 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1451
1452 /* The operator-precedence table used by cp_parser_binary_expression.
1453    Transformed into an associative array (binops_by_token) by
1454    cp_parser_new.  */
1455
1456 static const cp_parser_binary_operations_map_node binops[] = {
1457   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1458   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1459
1460   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1461   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1462   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1463
1464   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1465   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1466
1467   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1468   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1469
1470   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1471   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1472   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1473   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1474
1475   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1476   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1477
1478   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1479
1480   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1481
1482   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1483
1484   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1485
1486   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1487 };
1488
1489 /* The same as binops, but initialized by cp_parser_new so that
1490    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1491    for speed.  */
1492 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1493
1494 /* Constructors and destructors.  */
1495
1496 /* Construct a new context.  The context below this one on the stack
1497    is given by NEXT.  */
1498
1499 static cp_parser_context *
1500 cp_parser_context_new (cp_parser_context* next)
1501 {
1502   cp_parser_context *context;
1503
1504   /* Allocate the storage.  */
1505   if (cp_parser_context_free_list != NULL)
1506     {
1507       /* Pull the first entry from the free list.  */
1508       context = cp_parser_context_free_list;
1509       cp_parser_context_free_list = context->next;
1510       memset (context, 0, sizeof (*context));
1511     }
1512   else
1513     context = ggc_alloc_cleared_cp_parser_context ();
1514
1515   /* No errors have occurred yet in this context.  */
1516   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1517   /* If this is not the bottommost context, copy information that we
1518      need from the previous context.  */
1519   if (next)
1520     {
1521       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1522          expression, then we are parsing one in this context, too.  */
1523       context->object_type = next->object_type;
1524       /* Thread the stack.  */
1525       context->next = next;
1526     }
1527
1528   return context;
1529 }
1530
1531 /* An entry in a queue of function arguments that require post-processing.  */
1532
1533 typedef struct GTY(()) cp_default_arg_entry_d {
1534   /* The current_class_type when we parsed this arg.  */
1535   tree class_type;
1536
1537   /* The function decl itself.  */
1538   tree decl;
1539 } cp_default_arg_entry;
1540
1541 DEF_VEC_O(cp_default_arg_entry);
1542 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1543
1544 /* An entry in a stack for member functions of local classes.  */
1545
1546 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1547   /* Functions with default arguments that require post-processing.
1548      Functions appear in this list in declaration order.  */
1549   VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1550
1551   /* Functions with defintions that require post-processing.  Functions
1552      appear in this list in declaration order.  */
1553   VEC(tree,gc) *funs_with_definitions;
1554 } cp_unparsed_functions_entry;
1555
1556 DEF_VEC_O(cp_unparsed_functions_entry);
1557 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1558
1559 /* The cp_parser structure represents the C++ parser.  */
1560
1561 typedef struct GTY(()) cp_parser {
1562   /* The lexer from which we are obtaining tokens.  */
1563   cp_lexer *lexer;
1564
1565   /* The scope in which names should be looked up.  If NULL_TREE, then
1566      we look up names in the scope that is currently open in the
1567      source program.  If non-NULL, this is either a TYPE or
1568      NAMESPACE_DECL for the scope in which we should look.  It can
1569      also be ERROR_MARK, when we've parsed a bogus scope.
1570
1571      This value is not cleared automatically after a name is looked
1572      up, so we must be careful to clear it before starting a new look
1573      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1574      will look up `Z' in the scope of `X', rather than the current
1575      scope.)  Unfortunately, it is difficult to tell when name lookup
1576      is complete, because we sometimes peek at a token, look it up,
1577      and then decide not to consume it.   */
1578   tree scope;
1579
1580   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1581      last lookup took place.  OBJECT_SCOPE is used if an expression
1582      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1583      respectively.  QUALIFYING_SCOPE is used for an expression of the
1584      form "X::Y"; it refers to X.  */
1585   tree object_scope;
1586   tree qualifying_scope;
1587
1588   /* A stack of parsing contexts.  All but the bottom entry on the
1589      stack will be tentative contexts.
1590
1591      We parse tentatively in order to determine which construct is in
1592      use in some situations.  For example, in order to determine
1593      whether a statement is an expression-statement or a
1594      declaration-statement we parse it tentatively as a
1595      declaration-statement.  If that fails, we then reparse the same
1596      token stream as an expression-statement.  */
1597   cp_parser_context *context;
1598
1599   /* True if we are parsing GNU C++.  If this flag is not set, then
1600      GNU extensions are not recognized.  */
1601   bool allow_gnu_extensions_p;
1602
1603   /* TRUE if the `>' token should be interpreted as the greater-than
1604      operator.  FALSE if it is the end of a template-id or
1605      template-parameter-list. In C++0x mode, this flag also applies to
1606      `>>' tokens, which are viewed as two consecutive `>' tokens when
1607      this flag is FALSE.  */
1608   bool greater_than_is_operator_p;
1609
1610   /* TRUE if default arguments are allowed within a parameter list
1611      that starts at this point. FALSE if only a gnu extension makes
1612      them permissible.  */
1613   bool default_arg_ok_p;
1614
1615   /* TRUE if we are parsing an integral constant-expression.  See
1616      [expr.const] for a precise definition.  */
1617   bool integral_constant_expression_p;
1618
1619   /* TRUE if we are parsing an integral constant-expression -- but a
1620      non-constant expression should be permitted as well.  This flag
1621      is used when parsing an array bound so that GNU variable-length
1622      arrays are tolerated.  */
1623   bool allow_non_integral_constant_expression_p;
1624
1625   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1626      been seen that makes the expression non-constant.  */
1627   bool non_integral_constant_expression_p;
1628
1629   /* TRUE if local variable names and `this' are forbidden in the
1630      current context.  */
1631   bool local_variables_forbidden_p;
1632
1633   /* TRUE if the declaration we are parsing is part of a
1634      linkage-specification of the form `extern string-literal
1635      declaration'.  */
1636   bool in_unbraced_linkage_specification_p;
1637
1638   /* TRUE if we are presently parsing a declarator, after the
1639      direct-declarator.  */
1640   bool in_declarator_p;
1641
1642   /* TRUE if we are presently parsing a template-argument-list.  */
1643   bool in_template_argument_list_p;
1644
1645   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1646      to IN_OMP_BLOCK if parsing OpenMP structured block and
1647      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1648      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1649      iteration-statement, OpenMP block or loop within that switch.  */
1650 #define IN_SWITCH_STMT          1
1651 #define IN_ITERATION_STMT       2
1652 #define IN_OMP_BLOCK            4
1653 #define IN_OMP_FOR              8
1654 #define IN_IF_STMT             16
1655   unsigned char in_statement;
1656
1657   /* TRUE if we are presently parsing the body of a switch statement.
1658      Note that this doesn't quite overlap with in_statement above.
1659      The difference relates to giving the right sets of error messages:
1660      "case not in switch" vs "break statement used with OpenMP...".  */
1661   bool in_switch_statement_p;
1662
1663   /* TRUE if we are parsing a type-id in an expression context.  In
1664      such a situation, both "type (expr)" and "type (type)" are valid
1665      alternatives.  */
1666   bool in_type_id_in_expr_p;
1667
1668   /* TRUE if we are currently in a header file where declarations are
1669      implicitly extern "C".  */
1670   bool implicit_extern_c;
1671
1672   /* TRUE if strings in expressions should be translated to the execution
1673      character set.  */
1674   bool translate_strings_p;
1675
1676   /* TRUE if we are presently parsing the body of a function, but not
1677      a local class.  */
1678   bool in_function_body;
1679
1680   /* If non-NULL, then we are parsing a construct where new type
1681      definitions are not permitted.  The string stored here will be
1682      issued as an error message if a type is defined.  */
1683   const char *type_definition_forbidden_message;
1684
1685   /* A stack used for member functions of local classes.  The lists
1686      contained in an individual entry can only be processed once the
1687      outermost class being defined is complete.  */
1688   VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1689
1690   /* The number of classes whose definitions are currently in
1691      progress.  */
1692   unsigned num_classes_being_defined;
1693
1694   /* The number of template parameter lists that apply directly to the
1695      current declaration.  */
1696   unsigned num_template_parameter_lists;
1697 } cp_parser;
1698
1699 /* Managing the unparsed function queues.  */
1700
1701 #define unparsed_funs_with_default_args \
1702   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1703 #define unparsed_funs_with_definitions \
1704   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1705
1706 static void
1707 push_unparsed_function_queues (cp_parser *parser)
1708 {
1709   VEC_safe_push (cp_unparsed_functions_entry, gc,
1710                  parser->unparsed_queues, NULL);
1711   unparsed_funs_with_default_args = NULL;
1712   unparsed_funs_with_definitions = make_tree_vector ();
1713 }
1714
1715 static void
1716 pop_unparsed_function_queues (cp_parser *parser)
1717 {
1718   release_tree_vector (unparsed_funs_with_definitions);
1719   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1720 }
1721
1722 /* Prototypes.  */
1723
1724 /* Constructors and destructors.  */
1725
1726 static cp_parser *cp_parser_new
1727   (void);
1728
1729 /* Routines to parse various constructs.
1730
1731    Those that return `tree' will return the error_mark_node (rather
1732    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1733    Sometimes, they will return an ordinary node if error-recovery was
1734    attempted, even though a parse error occurred.  So, to check
1735    whether or not a parse error occurred, you should always use
1736    cp_parser_error_occurred.  If the construct is optional (indicated
1737    either by an `_opt' in the name of the function that does the
1738    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1739    the construct is not present.  */
1740
1741 /* Lexical conventions [gram.lex]  */
1742
1743 static tree cp_parser_identifier
1744   (cp_parser *);
1745 static tree cp_parser_string_literal
1746   (cp_parser *, bool, bool);
1747
1748 /* Basic concepts [gram.basic]  */
1749
1750 static bool cp_parser_translation_unit
1751   (cp_parser *);
1752
1753 /* Expressions [gram.expr]  */
1754
1755 static tree cp_parser_primary_expression
1756   (cp_parser *, bool, bool, bool, cp_id_kind *);
1757 static tree cp_parser_id_expression
1758   (cp_parser *, bool, bool, bool *, bool, bool);
1759 static tree cp_parser_unqualified_id
1760   (cp_parser *, bool, bool, bool, bool);
1761 static tree cp_parser_nested_name_specifier_opt
1762   (cp_parser *, bool, bool, bool, bool);
1763 static tree cp_parser_nested_name_specifier
1764   (cp_parser *, bool, bool, bool, bool);
1765 static tree cp_parser_qualifying_entity
1766   (cp_parser *, bool, bool, bool, bool, bool);
1767 static tree cp_parser_postfix_expression
1768   (cp_parser *, bool, bool, bool, cp_id_kind *);
1769 static tree cp_parser_postfix_open_square_expression
1770   (cp_parser *, tree, bool);
1771 static tree cp_parser_postfix_dot_deref_expression
1772   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1773 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1774   (cp_parser *, int, bool, bool, bool *);
1775 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1776 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1777 static void cp_parser_pseudo_destructor_name
1778   (cp_parser *, tree *, tree *);
1779 static tree cp_parser_unary_expression
1780   (cp_parser *, bool, bool, cp_id_kind *);
1781 static enum tree_code cp_parser_unary_operator
1782   (cp_token *);
1783 static tree cp_parser_new_expression
1784   (cp_parser *);
1785 static VEC(tree,gc) *cp_parser_new_placement
1786   (cp_parser *);
1787 static tree cp_parser_new_type_id
1788   (cp_parser *, tree *);
1789 static cp_declarator *cp_parser_new_declarator_opt
1790   (cp_parser *);
1791 static cp_declarator *cp_parser_direct_new_declarator
1792   (cp_parser *);
1793 static VEC(tree,gc) *cp_parser_new_initializer
1794   (cp_parser *);
1795 static tree cp_parser_delete_expression
1796   (cp_parser *);
1797 static tree cp_parser_cast_expression
1798   (cp_parser *, bool, bool, cp_id_kind *);
1799 static tree cp_parser_binary_expression
1800   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1801 static tree cp_parser_question_colon_clause
1802   (cp_parser *, tree);
1803 static tree cp_parser_assignment_expression
1804   (cp_parser *, bool, cp_id_kind *);
1805 static enum tree_code cp_parser_assignment_operator_opt
1806   (cp_parser *);
1807 static tree cp_parser_expression
1808   (cp_parser *, bool, cp_id_kind *);
1809 static tree cp_parser_constant_expression
1810   (cp_parser *, bool, bool *);
1811 static tree cp_parser_builtin_offsetof
1812   (cp_parser *);
1813 static tree cp_parser_lambda_expression
1814   (cp_parser *);
1815 static void cp_parser_lambda_introducer
1816   (cp_parser *, tree);
1817 static void cp_parser_lambda_declarator_opt
1818   (cp_parser *, tree);
1819 static void cp_parser_lambda_body
1820   (cp_parser *, tree);
1821
1822 /* Statements [gram.stmt.stmt]  */
1823
1824 static void cp_parser_statement
1825   (cp_parser *, tree, bool, bool *);
1826 static void cp_parser_label_for_labeled_statement
1827   (cp_parser *);
1828 static tree cp_parser_expression_statement
1829   (cp_parser *, tree);
1830 static tree cp_parser_compound_statement
1831   (cp_parser *, tree, bool);
1832 static void cp_parser_statement_seq_opt
1833   (cp_parser *, tree);
1834 static tree cp_parser_selection_statement
1835   (cp_parser *, bool *);
1836 static tree cp_parser_condition
1837   (cp_parser *);
1838 static tree cp_parser_iteration_statement
1839   (cp_parser *);
1840 static void cp_parser_for_init_statement
1841   (cp_parser *);
1842 static tree  cp_parser_c_for
1843   (cp_parser *);
1844 static tree  cp_parser_range_for
1845   (cp_parser *);
1846 static tree cp_parser_jump_statement
1847   (cp_parser *);
1848 static void cp_parser_declaration_statement
1849   (cp_parser *);
1850
1851 static tree cp_parser_implicitly_scoped_statement
1852   (cp_parser *, bool *);
1853 static void cp_parser_already_scoped_statement
1854   (cp_parser *);
1855
1856 /* Declarations [gram.dcl.dcl] */
1857
1858 static void cp_parser_declaration_seq_opt
1859   (cp_parser *);
1860 static void cp_parser_declaration
1861   (cp_parser *);
1862 static void cp_parser_block_declaration
1863   (cp_parser *, bool);
1864 static void cp_parser_simple_declaration
1865   (cp_parser *, bool);
1866 static void cp_parser_decl_specifier_seq
1867   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1868 static tree cp_parser_storage_class_specifier_opt
1869   (cp_parser *);
1870 static tree cp_parser_function_specifier_opt
1871   (cp_parser *, cp_decl_specifier_seq *);
1872 static tree cp_parser_type_specifier
1873   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1874    int *, bool *);
1875 static tree cp_parser_simple_type_specifier
1876   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1877 static tree cp_parser_type_name
1878   (cp_parser *);
1879 static tree cp_parser_nonclass_name 
1880   (cp_parser* parser);
1881 static tree cp_parser_elaborated_type_specifier
1882   (cp_parser *, bool, bool);
1883 static tree cp_parser_enum_specifier
1884   (cp_parser *);
1885 static void cp_parser_enumerator_list
1886   (cp_parser *, tree);
1887 static void cp_parser_enumerator_definition
1888   (cp_parser *, tree);
1889 static tree cp_parser_namespace_name
1890   (cp_parser *);
1891 static void cp_parser_namespace_definition
1892   (cp_parser *);
1893 static void cp_parser_namespace_body
1894   (cp_parser *);
1895 static tree cp_parser_qualified_namespace_specifier
1896   (cp_parser *);
1897 static void cp_parser_namespace_alias_definition
1898   (cp_parser *);
1899 static bool cp_parser_using_declaration
1900   (cp_parser *, bool);
1901 static void cp_parser_using_directive
1902   (cp_parser *);
1903 static void cp_parser_asm_definition
1904   (cp_parser *);
1905 static void cp_parser_linkage_specification
1906   (cp_parser *);
1907 static void cp_parser_static_assert
1908   (cp_parser *, bool);
1909 static tree cp_parser_decltype
1910   (cp_parser *);
1911
1912 /* Declarators [gram.dcl.decl] */
1913
1914 static tree cp_parser_init_declarator
1915   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1916 static cp_declarator *cp_parser_declarator
1917   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1918 static cp_declarator *cp_parser_direct_declarator
1919   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1920 static enum tree_code cp_parser_ptr_operator
1921   (cp_parser *, tree *, cp_cv_quals *);
1922 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1923   (cp_parser *);
1924 static tree cp_parser_late_return_type_opt
1925   (cp_parser *);
1926 static tree cp_parser_declarator_id
1927   (cp_parser *, bool);
1928 static tree cp_parser_type_id
1929   (cp_parser *);
1930 static tree cp_parser_template_type_arg
1931   (cp_parser *);
1932 static tree cp_parser_trailing_type_id (cp_parser *);
1933 static tree cp_parser_type_id_1
1934   (cp_parser *, bool, bool);
1935 static void cp_parser_type_specifier_seq
1936   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1937 static tree cp_parser_parameter_declaration_clause
1938   (cp_parser *);
1939 static tree cp_parser_parameter_declaration_list
1940   (cp_parser *, bool *);
1941 static cp_parameter_declarator *cp_parser_parameter_declaration
1942   (cp_parser *, bool, bool *);
1943 static tree cp_parser_default_argument 
1944   (cp_parser *, bool);
1945 static void cp_parser_function_body
1946   (cp_parser *);
1947 static tree cp_parser_initializer
1948   (cp_parser *, bool *, bool *);
1949 static tree cp_parser_initializer_clause
1950   (cp_parser *, bool *);
1951 static tree cp_parser_braced_list
1952   (cp_parser*, bool*);
1953 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1954   (cp_parser *, bool *);
1955
1956 static bool cp_parser_ctor_initializer_opt_and_function_body
1957   (cp_parser *);
1958
1959 /* Classes [gram.class] */
1960
1961 static tree cp_parser_class_name
1962   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1963 static tree cp_parser_class_specifier
1964   (cp_parser *);
1965 static tree cp_parser_class_head
1966   (cp_parser *, bool *, tree *, tree *);
1967 static enum tag_types cp_parser_class_key
1968   (cp_parser *);
1969 static void cp_parser_member_specification_opt
1970   (cp_parser *);
1971 static void cp_parser_member_declaration
1972   (cp_parser *);
1973 static tree cp_parser_pure_specifier
1974   (cp_parser *);
1975 static tree cp_parser_constant_initializer
1976   (cp_parser *);
1977
1978 /* Derived classes [gram.class.derived] */
1979
1980 static tree cp_parser_base_clause
1981   (cp_parser *);
1982 static tree cp_parser_base_specifier
1983   (cp_parser *);
1984
1985 /* Special member functions [gram.special] */
1986
1987 static tree cp_parser_conversion_function_id
1988   (cp_parser *);
1989 static tree cp_parser_conversion_type_id
1990   (cp_parser *);
1991 static cp_declarator *cp_parser_conversion_declarator_opt
1992   (cp_parser *);
1993 static bool cp_parser_ctor_initializer_opt
1994   (cp_parser *);
1995 static void cp_parser_mem_initializer_list
1996   (cp_parser *);
1997 static tree cp_parser_mem_initializer
1998   (cp_parser *);
1999 static tree cp_parser_mem_initializer_id
2000   (cp_parser *);
2001
2002 /* Overloading [gram.over] */
2003
2004 static tree cp_parser_operator_function_id
2005   (cp_parser *);
2006 static tree cp_parser_operator
2007   (cp_parser *);
2008
2009 /* Templates [gram.temp] */
2010
2011 static void cp_parser_template_declaration
2012   (cp_parser *, bool);
2013 static tree cp_parser_template_parameter_list
2014   (cp_parser *);
2015 static tree cp_parser_template_parameter
2016   (cp_parser *, bool *, bool *);
2017 static tree cp_parser_type_parameter
2018   (cp_parser *, bool *);
2019 static tree cp_parser_template_id
2020   (cp_parser *, bool, bool, bool);
2021 static tree cp_parser_template_name
2022   (cp_parser *, bool, bool, bool, bool *);
2023 static tree cp_parser_template_argument_list
2024   (cp_parser *);
2025 static tree cp_parser_template_argument
2026   (cp_parser *);
2027 static void cp_parser_explicit_instantiation
2028   (cp_parser *);
2029 static void cp_parser_explicit_specialization
2030   (cp_parser *);
2031
2032 /* Exception handling [gram.exception] */
2033
2034 static tree cp_parser_try_block
2035   (cp_parser *);
2036 static bool cp_parser_function_try_block
2037   (cp_parser *);
2038 static void cp_parser_handler_seq
2039   (cp_parser *);
2040 static void cp_parser_handler
2041   (cp_parser *);
2042 static tree cp_parser_exception_declaration
2043   (cp_parser *);
2044 static tree cp_parser_throw_expression
2045   (cp_parser *);
2046 static tree cp_parser_exception_specification_opt
2047   (cp_parser *);
2048 static tree cp_parser_type_id_list
2049   (cp_parser *);
2050
2051 /* GNU Extensions */
2052
2053 static tree cp_parser_asm_specification_opt
2054   (cp_parser *);
2055 static tree cp_parser_asm_operand_list
2056   (cp_parser *);
2057 static tree cp_parser_asm_clobber_list
2058   (cp_parser *);
2059 static tree cp_parser_asm_label_list
2060   (cp_parser *);
2061 static tree cp_parser_attributes_opt
2062   (cp_parser *);
2063 static tree cp_parser_attribute_list
2064   (cp_parser *);
2065 static bool cp_parser_extension_opt
2066   (cp_parser *, int *);
2067 static void cp_parser_label_declaration
2068   (cp_parser *);
2069
2070 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2071 static bool cp_parser_pragma
2072   (cp_parser *, enum pragma_context);
2073
2074 /* Objective-C++ Productions */
2075
2076 static tree cp_parser_objc_message_receiver
2077   (cp_parser *);
2078 static tree cp_parser_objc_message_args
2079   (cp_parser *);
2080 static tree cp_parser_objc_message_expression
2081   (cp_parser *);
2082 static tree cp_parser_objc_encode_expression
2083   (cp_parser *);
2084 static tree cp_parser_objc_defs_expression
2085   (cp_parser *);
2086 static tree cp_parser_objc_protocol_expression
2087   (cp_parser *);
2088 static tree cp_parser_objc_selector_expression
2089   (cp_parser *);
2090 static tree cp_parser_objc_expression
2091   (cp_parser *);
2092 static bool cp_parser_objc_selector_p
2093   (enum cpp_ttype);
2094 static tree cp_parser_objc_selector
2095   (cp_parser *);
2096 static tree cp_parser_objc_protocol_refs_opt
2097   (cp_parser *);
2098 static void cp_parser_objc_declaration
2099   (cp_parser *, tree);
2100 static tree cp_parser_objc_statement
2101   (cp_parser *);
2102 static bool cp_parser_objc_valid_prefix_attributes
2103   (cp_parser *, tree *);
2104 static void cp_parser_objc_at_property_declaration 
2105   (cp_parser *) ;
2106 static void cp_parser_objc_at_synthesize_declaration 
2107   (cp_parser *) ;
2108 static void cp_parser_objc_at_dynamic_declaration
2109   (cp_parser *) ;
2110 static tree cp_parser_objc_struct_declaration
2111   (cp_parser *) ;
2112
2113 /* Utility Routines */
2114
2115 static tree cp_parser_lookup_name
2116   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2117 static tree cp_parser_lookup_name_simple
2118   (cp_parser *, tree, location_t);
2119 static tree cp_parser_maybe_treat_template_as_class
2120   (tree, bool);
2121 static bool cp_parser_check_declarator_template_parameters
2122   (cp_parser *, cp_declarator *, location_t);
2123 static bool cp_parser_check_template_parameters
2124   (cp_parser *, unsigned, location_t, cp_declarator *);
2125 static tree cp_parser_simple_cast_expression
2126   (cp_parser *);
2127 static tree cp_parser_global_scope_opt
2128   (cp_parser *, bool);
2129 static bool cp_parser_constructor_declarator_p
2130   (cp_parser *, bool);
2131 static tree cp_parser_function_definition_from_specifiers_and_declarator
2132   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2133 static tree cp_parser_function_definition_after_declarator
2134   (cp_parser *, bool);
2135 static void cp_parser_template_declaration_after_export
2136   (cp_parser *, bool);
2137 static void cp_parser_perform_template_parameter_access_checks
2138   (VEC (deferred_access_check,gc)*);
2139 static tree cp_parser_single_declaration
2140   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2141 static tree cp_parser_functional_cast
2142   (cp_parser *, tree);
2143 static tree cp_parser_save_member_function_body
2144   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2145 static tree cp_parser_enclosed_template_argument_list
2146   (cp_parser *);
2147 static void cp_parser_save_default_args
2148   (cp_parser *, tree);
2149 static void cp_parser_late_parsing_for_member
2150   (cp_parser *, tree);
2151 static void cp_parser_late_parsing_default_args
2152   (cp_parser *, tree);
2153 static tree cp_parser_sizeof_operand
2154   (cp_parser *, enum rid);
2155 static tree cp_parser_trait_expr
2156   (cp_parser *, enum rid);
2157 static bool cp_parser_declares_only_class_p
2158   (cp_parser *);
2159 static void cp_parser_set_storage_class
2160   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2161 static void cp_parser_set_decl_spec_type
2162   (cp_decl_specifier_seq *, tree, location_t, bool);
2163 static bool cp_parser_friend_p
2164   (const cp_decl_specifier_seq *);
2165 static void cp_parser_required_error
2166   (cp_parser *, required_token, bool);
2167 static cp_token *cp_parser_require
2168   (cp_parser *, enum cpp_ttype, required_token);
2169 static cp_token *cp_parser_require_keyword
2170   (cp_parser *, enum rid, required_token);
2171 static bool cp_parser_token_starts_function_definition_p
2172   (cp_token *);
2173 static bool cp_parser_next_token_starts_class_definition_p
2174   (cp_parser *);
2175 static bool cp_parser_next_token_ends_template_argument_p
2176   (cp_parser *);
2177 static bool cp_parser_nth_token_starts_template_argument_list_p
2178   (cp_parser *, size_t);
2179 static enum tag_types cp_parser_token_is_class_key
2180   (cp_token *);
2181 static void cp_parser_check_class_key
2182   (enum tag_types, tree type);
2183 static void cp_parser_check_access_in_redeclaration
2184   (tree type, location_t location);
2185 static bool cp_parser_optional_template_keyword
2186   (cp_parser *);
2187 static void cp_parser_pre_parsed_nested_name_specifier
2188   (cp_parser *);
2189 static bool cp_parser_cache_group
2190   (cp_parser *, enum cpp_ttype, unsigned);
2191 static void cp_parser_parse_tentatively
2192   (cp_parser *);
2193 static void cp_parser_commit_to_tentative_parse
2194   (cp_parser *);
2195 static void cp_parser_abort_tentative_parse
2196   (cp_parser *);
2197 static bool cp_parser_parse_definitely
2198   (cp_parser *);
2199 static inline bool cp_parser_parsing_tentatively
2200   (cp_parser *);
2201 static bool cp_parser_uncommitted_to_tentative_parse_p
2202   (cp_parser *);
2203 static void cp_parser_error
2204   (cp_parser *, const char *);
2205 static void cp_parser_name_lookup_error
2206   (cp_parser *, tree, tree, name_lookup_error, location_t);
2207 static bool cp_parser_simulate_error
2208   (cp_parser *);
2209 static bool cp_parser_check_type_definition
2210   (cp_parser *);
2211 static void cp_parser_check_for_definition_in_return_type
2212   (cp_declarator *, tree, location_t type_location);
2213 static void cp_parser_check_for_invalid_template_id
2214   (cp_parser *, tree, location_t location);
2215 static bool cp_parser_non_integral_constant_expression
2216   (cp_parser *, non_integral_constant);
2217 static void cp_parser_diagnose_invalid_type_name
2218   (cp_parser *, tree, tree, location_t);
2219 static bool cp_parser_parse_and_diagnose_invalid_type_name
2220   (cp_parser *);
2221 static int cp_parser_skip_to_closing_parenthesis
2222   (cp_parser *, bool, bool, bool);
2223 static void cp_parser_skip_to_end_of_statement
2224   (cp_parser *);
2225 static void cp_parser_consume_semicolon_at_end_of_statement
2226   (cp_parser *);
2227 static void cp_parser_skip_to_end_of_block_or_statement
2228   (cp_parser *);
2229 static bool cp_parser_skip_to_closing_brace
2230   (cp_parser *);
2231 static void cp_parser_skip_to_end_of_template_parameter_list
2232   (cp_parser *);
2233 static void cp_parser_skip_to_pragma_eol
2234   (cp_parser*, cp_token *);
2235 static bool cp_parser_error_occurred
2236   (cp_parser *);
2237 static bool cp_parser_allow_gnu_extensions_p
2238   (cp_parser *);
2239 static bool cp_parser_is_string_literal
2240   (cp_token *);
2241 static bool cp_parser_is_keyword
2242   (cp_token *, enum rid);
2243 static tree cp_parser_make_typename_type
2244   (cp_parser *, tree, tree, location_t location);
2245 static cp_declarator * cp_parser_make_indirect_declarator
2246   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2247
2248 /* Returns nonzero if we are parsing tentatively.  */
2249
2250 static inline bool
2251 cp_parser_parsing_tentatively (cp_parser* parser)
2252 {
2253   return parser->context->next != NULL;
2254 }
2255
2256 /* Returns nonzero if TOKEN is a string literal.  */
2257
2258 static bool
2259 cp_parser_is_string_literal (cp_token* token)
2260 {
2261   return (token->type == CPP_STRING ||
2262           token->type == CPP_STRING16 ||
2263           token->type == CPP_STRING32 ||
2264           token->type == CPP_WSTRING ||
2265           token->type == CPP_UTF8STRING);
2266 }
2267
2268 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2269
2270 static bool
2271 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2272 {
2273   return token->keyword == keyword;
2274 }
2275
2276 /* If not parsing tentatively, issue a diagnostic of the form
2277       FILE:LINE: MESSAGE before TOKEN
2278    where TOKEN is the next token in the input stream.  MESSAGE
2279    (specified by the caller) is usually of the form "expected
2280    OTHER-TOKEN".  */
2281
2282 static void
2283 cp_parser_error (cp_parser* parser, const char* gmsgid)
2284 {
2285   if (!cp_parser_simulate_error (parser))
2286     {
2287       cp_token *token = cp_lexer_peek_token (parser->lexer);
2288       /* This diagnostic makes more sense if it is tagged to the line
2289          of the token we just peeked at.  */
2290       cp_lexer_set_source_position_from_token (token);
2291
2292       if (token->type == CPP_PRAGMA)
2293         {
2294           error_at (token->location,
2295                     "%<#pragma%> is not allowed here");
2296           cp_parser_skip_to_pragma_eol (parser, token);
2297           return;
2298         }
2299
2300       c_parse_error (gmsgid,
2301                      /* Because c_parser_error does not understand
2302                         CPP_KEYWORD, keywords are treated like
2303                         identifiers.  */
2304                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2305                      token->u.value, token->flags);
2306     }
2307 }
2308
2309 /* Issue an error about name-lookup failing.  NAME is the
2310    IDENTIFIER_NODE DECL is the result of
2311    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2312    the thing that we hoped to find.  */
2313
2314 static void
2315 cp_parser_name_lookup_error (cp_parser* parser,
2316                              tree name,
2317                              tree decl,
2318                              name_lookup_error desired,
2319                              location_t location)
2320 {
2321   /* If name lookup completely failed, tell the user that NAME was not
2322      declared.  */
2323   if (decl == error_mark_node)
2324     {
2325       if (parser->scope && parser->scope != global_namespace)
2326         error_at (location, "%<%E::%E%> has not been declared",
2327                   parser->scope, name);
2328       else if (parser->scope == global_namespace)
2329         error_at (location, "%<::%E%> has not been declared", name);
2330       else if (parser->object_scope
2331                && !CLASS_TYPE_P (parser->object_scope))
2332         error_at (location, "request for member %qE in non-class type %qT",
2333                   name, parser->object_scope);
2334       else if (parser->object_scope)
2335         error_at (location, "%<%T::%E%> has not been declared",
2336                   parser->object_scope, name);
2337       else
2338         error_at (location, "%qE has not been declared", name);
2339     }
2340   else if (parser->scope && parser->scope != global_namespace)
2341     {
2342       switch (desired)
2343         {
2344           case NLE_TYPE:
2345             error_at (location, "%<%E::%E%> is not a type",
2346                                 parser->scope, name);
2347             break;
2348           case NLE_CXX98:
2349             error_at (location, "%<%E::%E%> is not a class or namespace",
2350                                 parser->scope, name);
2351             break;
2352           case NLE_NOT_CXX98:
2353             error_at (location,
2354                       "%<%E::%E%> is not a class, namespace, or enumeration",
2355                       parser->scope, name);
2356             break;
2357           default:
2358             gcc_unreachable ();
2359             
2360         }
2361     }
2362   else if (parser->scope == global_namespace)
2363     {
2364       switch (desired)
2365         {
2366           case NLE_TYPE:
2367             error_at (location, "%<::%E%> is not a type", name);
2368             break;
2369           case NLE_CXX98:
2370             error_at (location, "%<::%E%> is not a class or namespace", name);
2371             break;
2372           case NLE_NOT_CXX98:
2373             error_at (location,
2374                       "%<::%E%> is not a class, namespace, or enumeration",
2375                       name);
2376             break;
2377           default:
2378             gcc_unreachable ();
2379         }
2380     }
2381   else
2382     {
2383       switch (desired)
2384         {
2385           case NLE_TYPE:
2386             error_at (location, "%qE is not a type", name);
2387             break;
2388           case NLE_CXX98:
2389             error_at (location, "%qE is not a class or namespace", name);
2390             break;
2391           case NLE_NOT_CXX98:
2392             error_at (location,
2393                       "%qE is not a class, namespace, or enumeration", name);
2394             break;
2395           default:
2396             gcc_unreachable ();
2397         }
2398     }
2399 }
2400
2401 /* If we are parsing tentatively, remember that an error has occurred
2402    during this tentative parse.  Returns true if the error was
2403    simulated; false if a message should be issued by the caller.  */
2404
2405 static bool
2406 cp_parser_simulate_error (cp_parser* parser)
2407 {
2408   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2409     {
2410       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2411       return true;
2412     }
2413   return false;
2414 }
2415
2416 /* Check for repeated decl-specifiers.  */
2417
2418 static void
2419 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2420                            location_t location)
2421 {
2422   int ds;
2423
2424   for (ds = ds_first; ds != ds_last; ++ds)
2425     {
2426       unsigned count = decl_specs->specs[ds];
2427       if (count < 2)
2428         continue;
2429       /* The "long" specifier is a special case because of "long long".  */
2430       if (ds == ds_long)
2431         {
2432           if (count > 2)
2433             error_at (location, "%<long long long%> is too long for GCC");
2434           else 
2435             pedwarn_cxx98 (location, OPT_Wlong_long, 
2436                            "ISO C++ 1998 does not support %<long long%>");
2437         }
2438       else if (count > 1)
2439         {
2440           static const char *const decl_spec_names[] = {
2441             "signed",
2442             "unsigned",
2443             "short",
2444             "long",
2445             "const",
2446             "volatile",
2447             "restrict",
2448             "inline",
2449             "virtual",
2450             "explicit",
2451             "friend",
2452             "typedef",
2453             "constexpr",
2454             "__complex",
2455             "__thread"
2456           };
2457           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2458         }
2459     }
2460 }
2461
2462 /* This function is called when a type is defined.  If type
2463    definitions are forbidden at this point, an error message is
2464    issued.  */
2465
2466 static bool
2467 cp_parser_check_type_definition (cp_parser* parser)
2468 {
2469   /* If types are forbidden here, issue a message.  */
2470   if (parser->type_definition_forbidden_message)
2471     {
2472       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2473          in the message need to be interpreted.  */
2474       error (parser->type_definition_forbidden_message);
2475       return false;
2476     }
2477   return true;
2478 }
2479
2480 /* This function is called when the DECLARATOR is processed.  The TYPE
2481    was a type defined in the decl-specifiers.  If it is invalid to
2482    define a type in the decl-specifiers for DECLARATOR, an error is
2483    issued. TYPE_LOCATION is the location of TYPE and is used
2484    for error reporting.  */
2485
2486 static void
2487 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2488                                                tree type, location_t type_location)
2489 {
2490   /* [dcl.fct] forbids type definitions in return types.
2491      Unfortunately, it's not easy to know whether or not we are
2492      processing a return type until after the fact.  */
2493   while (declarator
2494          && (declarator->kind == cdk_pointer
2495              || declarator->kind == cdk_reference
2496              || declarator->kind == cdk_ptrmem))
2497     declarator = declarator->declarator;
2498   if (declarator
2499       && declarator->kind == cdk_function)
2500     {
2501       error_at (type_location,
2502                 "new types may not be defined in a return type");
2503       inform (type_location, 
2504               "(perhaps a semicolon is missing after the definition of %qT)",
2505               type);
2506     }
2507 }
2508
2509 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2510    "<" in any valid C++ program.  If the next token is indeed "<",
2511    issue a message warning the user about what appears to be an
2512    invalid attempt to form a template-id. LOCATION is the location
2513    of the type-specifier (TYPE) */
2514
2515 static void
2516 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2517                                          tree type, location_t location)
2518 {
2519   cp_token_position start = 0;
2520
2521   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2522     {
2523       if (TYPE_P (type))
2524         error_at (location, "%qT is not a template", type);
2525       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2526         error_at (location, "%qE is not a template", type);
2527       else
2528         error_at (location, "invalid template-id");
2529       /* Remember the location of the invalid "<".  */
2530       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2531         start = cp_lexer_token_position (parser->lexer, true);
2532       /* Consume the "<".  */
2533       cp_lexer_consume_token (parser->lexer);
2534       /* Parse the template arguments.  */
2535       cp_parser_enclosed_template_argument_list (parser);
2536       /* Permanently remove the invalid template arguments so that
2537          this error message is not issued again.  */
2538       if (start)
2539         cp_lexer_purge_tokens_after (parser->lexer, start);
2540     }
2541 }
2542
2543 /* If parsing an integral constant-expression, issue an error message
2544    about the fact that THING appeared and return true.  Otherwise,
2545    return false.  In either case, set
2546    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2547
2548 static bool
2549 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2550                                             non_integral_constant thing)
2551 {
2552   parser->non_integral_constant_expression_p = true;
2553   if (parser->integral_constant_expression_p)
2554     {
2555       if (!parser->allow_non_integral_constant_expression_p)
2556         {
2557           const char *msg = NULL;
2558           switch (thing)
2559             {
2560               case NIC_FLOAT:
2561                 error ("floating-point literal "
2562                        "cannot appear in a constant-expression");
2563                 return true;
2564               case NIC_CAST:
2565                 error ("a cast to a type other than an integral or "
2566                        "enumeration type cannot appear in a "
2567                        "constant-expression");
2568                 return true;
2569               case NIC_TYPEID:
2570                 error ("%<typeid%> operator "
2571                        "cannot appear in a constant-expression");
2572                 return true;
2573               case NIC_NCC:
2574                 error ("non-constant compound literals "
2575                        "cannot appear in a constant-expression");
2576                 return true;
2577               case NIC_FUNC_CALL:
2578                 error ("a function call "
2579                        "cannot appear in a constant-expression");
2580                 return true;
2581               case NIC_INC:
2582                 error ("an increment "
2583                        "cannot appear in a constant-expression");
2584                 return true;
2585               case NIC_DEC:
2586                 error ("an decrement "
2587                        "cannot appear in a constant-expression");
2588                 return true;
2589               case NIC_ARRAY_REF:
2590                 error ("an array reference "
2591                        "cannot appear in a constant-expression");
2592                 return true;
2593               case NIC_ADDR_LABEL:
2594                 error ("the address of a label "
2595                        "cannot appear in a constant-expression");
2596                 return true;
2597               case NIC_OVERLOADED:
2598                 error ("calls to overloaded operators "
2599                        "cannot appear in a constant-expression");
2600                 return true;
2601               case NIC_ASSIGNMENT:
2602                 error ("an assignment cannot appear in a constant-expression");
2603                 return true;
2604               case NIC_COMMA:
2605                 error ("a comma operator "
2606                        "cannot appear in a constant-expression");
2607                 return true;
2608               case NIC_CONSTRUCTOR:
2609                 error ("a call to a constructor "
2610                        "cannot appear in a constant-expression");
2611                 return true;
2612               case NIC_THIS:
2613                 msg = "this";
2614                 break;
2615               case NIC_FUNC_NAME:
2616                 msg = "__FUNCTION__";
2617                 break;
2618               case NIC_PRETTY_FUNC:
2619                 msg = "__PRETTY_FUNCTION__";
2620                 break;
2621               case NIC_C99_FUNC:
2622                 msg = "__func__";
2623                 break;
2624               case NIC_VA_ARG:
2625                 msg = "va_arg";
2626                 break;
2627               case NIC_ARROW:
2628                 msg = "->";
2629                 break;
2630               case NIC_POINT:
2631                 msg = ".";
2632                 break;
2633               case NIC_STAR:
2634                 msg = "*";
2635                 break;
2636               case NIC_ADDR:
2637                 msg = "&";
2638                 break;
2639               case NIC_PREINCREMENT:
2640                 msg = "++";
2641                 break;
2642               case NIC_PREDECREMENT:
2643                 msg = "--";
2644                 break;
2645               case NIC_NEW:
2646                 msg = "new";
2647                 break;
2648               case NIC_DEL:
2649                 msg = "delete";
2650                 break;
2651               default:
2652                 gcc_unreachable ();
2653             }
2654           if (msg)
2655             error ("%qs cannot appear in a constant-expression", msg);
2656           return true;
2657         }
2658     }
2659   return false;
2660 }
2661
2662 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2663    qualifying scope (or NULL, if none) for ID.  This function commits
2664    to the current active tentative parse, if any.  (Otherwise, the
2665    problematic construct might be encountered again later, resulting
2666    in duplicate error messages.) LOCATION is the location of ID.  */
2667
2668 static void
2669 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2670                                       tree scope, tree id,
2671                                       location_t location)
2672 {
2673   tree decl, old_scope;
2674   /* Try to lookup the identifier.  */
2675   old_scope = parser->scope;
2676   parser->scope = scope;
2677   decl = cp_parser_lookup_name_simple (parser, id, location);
2678   parser->scope = old_scope;
2679   /* If the lookup found a template-name, it means that the user forgot
2680   to specify an argument list. Emit a useful error message.  */
2681   if (TREE_CODE (decl) == TEMPLATE_DECL)
2682     error_at (location,
2683               "invalid use of template-name %qE without an argument list",
2684               decl);
2685   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2686     error_at (location, "invalid use of destructor %qD as a type", id);
2687   else if (TREE_CODE (decl) == TYPE_DECL)
2688     /* Something like 'unsigned A a;'  */
2689     error_at (location, "invalid combination of multiple type-specifiers");
2690   else if (!parser->scope)
2691     {
2692       /* Issue an error message.  */
2693       error_at (location, "%qE does not name a type", id);
2694       /* If we're in a template class, it's possible that the user was
2695          referring to a type from a base class.  For example:
2696
2697            template <typename T> struct A { typedef T X; };
2698            template <typename T> struct B : public A<T> { X x; };
2699
2700          The user should have said "typename A<T>::X".  */
2701       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2702         inform (location, "C++0x %<constexpr%> only available with "
2703                 "-std=c++0x or -std=gnu++0x");
2704       else if (processing_template_decl && current_class_type
2705                && TYPE_BINFO (current_class_type))
2706         {
2707           tree b;
2708
2709           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2710                b;
2711                b = TREE_CHAIN (b))
2712             {
2713               tree base_type = BINFO_TYPE (b);
2714               if (CLASS_TYPE_P (base_type)
2715                   && dependent_type_p (base_type))
2716                 {
2717                   tree field;
2718                   /* Go from a particular instantiation of the
2719                      template (which will have an empty TYPE_FIELDs),
2720                      to the main version.  */
2721                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2722                   for (field = TYPE_FIELDS (base_type);
2723                        field;
2724                        field = DECL_CHAIN (field))
2725                     if (TREE_CODE (field) == TYPE_DECL
2726                         && DECL_NAME (field) == id)
2727                       {
2728                         inform (location, 
2729                                 "(perhaps %<typename %T::%E%> was intended)",
2730                                 BINFO_TYPE (b), id);
2731                         break;
2732                       }
2733                   if (field)
2734                     break;
2735                 }
2736             }
2737         }
2738     }
2739   /* Here we diagnose qualified-ids where the scope is actually correct,
2740      but the identifier does not resolve to a valid type name.  */
2741   else if (parser->scope != error_mark_node)
2742     {
2743       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2744         error_at (location, "%qE in namespace %qE does not name a type",
2745                   id, parser->scope);
2746       else if (CLASS_TYPE_P (parser->scope)
2747                && constructor_name_p (id, parser->scope))
2748         {
2749           /* A<T>::A<T>() */
2750           error_at (location, "%<%T::%E%> names the constructor, not"
2751                     " the type", parser->scope, id);
2752           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2753             error_at (location, "and %qT has no template constructors",
2754                       parser->scope);
2755         }
2756       else if (TYPE_P (parser->scope)
2757                && dependent_scope_p (parser->scope))
2758         error_at (location, "need %<typename%> before %<%T::%E%> because "
2759                   "%qT is a dependent scope",
2760                   parser->scope, id, parser->scope);
2761       else if (TYPE_P (parser->scope))
2762         error_at (location, "%qE in class %qT does not name a type",
2763                   id, parser->scope);
2764       else
2765         gcc_unreachable ();
2766     }
2767   cp_parser_commit_to_tentative_parse (parser);
2768 }
2769
2770 /* Check for a common situation where a type-name should be present,
2771    but is not, and issue a sensible error message.  Returns true if an
2772    invalid type-name was detected.
2773
2774    The situation handled by this function are variable declarations of the
2775    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2776    Usually, `ID' should name a type, but if we got here it means that it
2777    does not. We try to emit the best possible error message depending on
2778    how exactly the id-expression looks like.  */
2779
2780 static bool
2781 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2782 {
2783   tree id;
2784   cp_token *token = cp_lexer_peek_token (parser->lexer);
2785
2786   /* Avoid duplicate error about ambiguous lookup.  */
2787   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2788     {
2789       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2790       if (next->type == CPP_NAME && next->ambiguous_p)
2791         goto out;
2792     }
2793
2794   cp_parser_parse_tentatively (parser);
2795   id = cp_parser_id_expression (parser,
2796                                 /*template_keyword_p=*/false,
2797                                 /*check_dependency_p=*/true,
2798                                 /*template_p=*/NULL,
2799                                 /*declarator_p=*/true,
2800                                 /*optional_p=*/false);
2801   /* If the next token is a (, this is a function with no explicit return
2802      type, i.e. constructor, destructor or conversion op.  */
2803   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2804       || TREE_CODE (id) == TYPE_DECL)
2805     {
2806       cp_parser_abort_tentative_parse (parser);
2807       return false;
2808     }
2809   if (!cp_parser_parse_definitely (parser))
2810     return false;
2811
2812   /* Emit a diagnostic for the invalid type.  */
2813   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2814                                         id, token->location);
2815  out:
2816   /* If we aren't in the middle of a declarator (i.e. in a
2817      parameter-declaration-clause), skip to the end of the declaration;
2818      there's no point in trying to process it.  */
2819   if (!parser->in_declarator_p)
2820     cp_parser_skip_to_end_of_block_or_statement (parser);
2821   return true;
2822 }
2823
2824 /* Consume tokens up to, and including, the next non-nested closing `)'.
2825    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2826    are doing error recovery. Returns -1 if OR_COMMA is true and we
2827    found an unnested comma.  */
2828
2829 static int
2830 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2831                                        bool recovering,
2832                                        bool or_comma,
2833                                        bool consume_paren)
2834 {
2835   unsigned paren_depth = 0;
2836   unsigned brace_depth = 0;
2837   unsigned square_depth = 0;
2838
2839   if (recovering && !or_comma
2840       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2841     return 0;
2842
2843   while (true)
2844     {
2845       cp_token * token = cp_lexer_peek_token (parser->lexer);
2846
2847       switch (token->type)
2848         {
2849         case CPP_EOF:
2850         case CPP_PRAGMA_EOL:
2851           /* If we've run out of tokens, then there is no closing `)'.  */
2852           return 0;
2853
2854         /* This is good for lambda expression capture-lists.  */
2855         case CPP_OPEN_SQUARE:
2856           ++square_depth;
2857           break;
2858         case CPP_CLOSE_SQUARE:
2859           if (!square_depth--)
2860             return 0;
2861           break;
2862
2863         case CPP_SEMICOLON:
2864           /* This matches the processing in skip_to_end_of_statement.  */
2865           if (!brace_depth)
2866             return 0;
2867           break;
2868
2869         case CPP_OPEN_BRACE:
2870           ++brace_depth;
2871           break;
2872         case CPP_CLOSE_BRACE:
2873           if (!brace_depth--)
2874             return 0;
2875           break;
2876
2877         case CPP_COMMA:
2878           if (recovering && or_comma && !brace_depth && !paren_depth
2879               && !square_depth)
2880             return -1;
2881           break;
2882
2883         case CPP_OPEN_PAREN:
2884           if (!brace_depth)
2885             ++paren_depth;
2886           break;
2887
2888         case CPP_CLOSE_PAREN:
2889           if (!brace_depth && !paren_depth--)
2890             {
2891               if (consume_paren)
2892                 cp_lexer_consume_token (parser->lexer);
2893               return 1;
2894             }
2895           break;
2896
2897         default:
2898           break;
2899         }
2900
2901       /* Consume the token.  */
2902       cp_lexer_consume_token (parser->lexer);
2903     }
2904 }
2905
2906 /* Consume tokens until we reach the end of the current statement.
2907    Normally, that will be just before consuming a `;'.  However, if a
2908    non-nested `}' comes first, then we stop before consuming that.  */
2909
2910 static void
2911 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2912 {
2913   unsigned nesting_depth = 0;
2914
2915   while (true)
2916     {
2917       cp_token *token = cp_lexer_peek_token (parser->lexer);
2918
2919       switch (token->type)
2920         {
2921         case CPP_EOF:
2922         case CPP_PRAGMA_EOL:
2923           /* If we've run out of tokens, stop.  */
2924           return;
2925
2926         case CPP_SEMICOLON:
2927           /* If the next token is a `;', we have reached the end of the
2928              statement.  */
2929           if (!nesting_depth)
2930             return;
2931           break;
2932
2933         case CPP_CLOSE_BRACE:
2934           /* If this is a non-nested '}', stop before consuming it.
2935              That way, when confronted with something like:
2936
2937                { 3 + }
2938
2939              we stop before consuming the closing '}', even though we
2940              have not yet reached a `;'.  */
2941           if (nesting_depth == 0)
2942             return;
2943
2944           /* If it is the closing '}' for a block that we have
2945              scanned, stop -- but only after consuming the token.
2946              That way given:
2947
2948                 void f g () { ... }
2949                 typedef int I;
2950
2951              we will stop after the body of the erroneously declared
2952              function, but before consuming the following `typedef'
2953              declaration.  */
2954           if (--nesting_depth == 0)
2955             {
2956               cp_lexer_consume_token (parser->lexer);
2957               return;
2958             }
2959
2960         case CPP_OPEN_BRACE:
2961           ++nesting_depth;
2962           break;
2963
2964         default:
2965           break;
2966         }
2967
2968       /* Consume the token.  */
2969       cp_lexer_consume_token (parser->lexer);
2970     }
2971 }
2972
2973 /* This function is called at the end of a statement or declaration.
2974    If the next token is a semicolon, it is consumed; otherwise, error
2975    recovery is attempted.  */
2976
2977 static void
2978 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2979 {
2980   /* Look for the trailing `;'.  */
2981   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2982     {
2983       /* If there is additional (erroneous) input, skip to the end of
2984          the statement.  */
2985       cp_parser_skip_to_end_of_statement (parser);
2986       /* If the next token is now a `;', consume it.  */
2987       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2988         cp_lexer_consume_token (parser->lexer);
2989     }
2990 }
2991
2992 /* Skip tokens until we have consumed an entire block, or until we
2993    have consumed a non-nested `;'.  */
2994
2995 static void
2996 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2997 {
2998   int nesting_depth = 0;
2999
3000   while (nesting_depth >= 0)
3001     {
3002       cp_token *token = cp_lexer_peek_token (parser->lexer);
3003
3004       switch (token->type)
3005         {
3006         case CPP_EOF:
3007         case CPP_PRAGMA_EOL:
3008           /* If we've run out of tokens, stop.  */
3009           return;
3010
3011         case CPP_SEMICOLON:
3012           /* Stop if this is an unnested ';'. */
3013           if (!nesting_depth)
3014             nesting_depth = -1;
3015           break;
3016
3017         case CPP_CLOSE_BRACE:
3018           /* Stop if this is an unnested '}', or closes the outermost
3019              nesting level.  */
3020           nesting_depth--;
3021           if (nesting_depth < 0)
3022             return;
3023           if (!nesting_depth)
3024             nesting_depth = -1;
3025           break;
3026
3027         case CPP_OPEN_BRACE:
3028           /* Nest. */
3029           nesting_depth++;
3030           break;
3031
3032         default:
3033           break;
3034         }
3035
3036       /* Consume the token.  */
3037       cp_lexer_consume_token (parser->lexer);
3038     }
3039 }
3040
3041 /* Skip tokens until a non-nested closing curly brace is the next
3042    token, or there are no more tokens. Return true in the first case,
3043    false otherwise.  */
3044
3045 static bool
3046 cp_parser_skip_to_closing_brace (cp_parser *parser)
3047 {
3048   unsigned nesting_depth = 0;
3049
3050   while (true)
3051     {
3052       cp_token *token = cp_lexer_peek_token (parser->lexer);
3053
3054       switch (token->type)
3055         {
3056         case CPP_EOF:
3057         case CPP_PRAGMA_EOL:
3058           /* If we've run out of tokens, stop.  */
3059           return false;
3060
3061         case CPP_CLOSE_BRACE:
3062           /* If the next token is a non-nested `}', then we have reached
3063              the end of the current block.  */
3064           if (nesting_depth-- == 0)
3065             return true;
3066           break;
3067
3068         case CPP_OPEN_BRACE:
3069           /* If it the next token is a `{', then we are entering a new
3070              block.  Consume the entire block.  */
3071           ++nesting_depth;
3072           break;
3073
3074         default:
3075           break;
3076         }
3077
3078       /* Consume the token.  */
3079       cp_lexer_consume_token (parser->lexer);
3080     }
3081 }
3082
3083 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3084    parameter is the PRAGMA token, allowing us to purge the entire pragma
3085    sequence.  */
3086
3087 static void
3088 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3089 {
3090   cp_token *token;
3091
3092   parser->lexer->in_pragma = false;
3093
3094   do
3095     token = cp_lexer_consume_token (parser->lexer);
3096   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3097
3098   /* Ensure that the pragma is not parsed again.  */
3099   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3100 }
3101
3102 /* Require pragma end of line, resyncing with it as necessary.  The
3103    arguments are as for cp_parser_skip_to_pragma_eol.  */
3104
3105 static void
3106 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3107 {
3108   parser->lexer->in_pragma = false;
3109   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3110     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3111 }
3112
3113 /* This is a simple wrapper around make_typename_type. When the id is
3114    an unresolved identifier node, we can provide a superior diagnostic
3115    using cp_parser_diagnose_invalid_type_name.  */
3116
3117 static tree
3118 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3119                               tree id, location_t id_location)
3120 {
3121   tree result;
3122   if (TREE_CODE (id) == IDENTIFIER_NODE)
3123     {
3124       result = make_typename_type (scope, id, typename_type,
3125                                    /*complain=*/tf_none);
3126       if (result == error_mark_node)
3127         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3128       return result;
3129     }
3130   return make_typename_type (scope, id, typename_type, tf_error);
3131 }
3132
3133 /* This is a wrapper around the
3134    make_{pointer,ptrmem,reference}_declarator functions that decides
3135    which one to call based on the CODE and CLASS_TYPE arguments. The
3136    CODE argument should be one of the values returned by
3137    cp_parser_ptr_operator. */
3138 static cp_declarator *
3139 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3140                                     cp_cv_quals cv_qualifiers,
3141                                     cp_declarator *target)
3142 {
3143   if (code == ERROR_MARK)
3144     return cp_error_declarator;
3145
3146   if (code == INDIRECT_REF)
3147     if (class_type == NULL_TREE)
3148       return make_pointer_declarator (cv_qualifiers, target);
3149     else
3150       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3151   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3152     return make_reference_declarator (cv_qualifiers, target, false);
3153   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3154     return make_reference_declarator (cv_qualifiers, target, true);
3155   gcc_unreachable ();
3156 }
3157
3158 /* Create a new C++ parser.  */
3159
3160 static cp_parser *
3161 cp_parser_new (void)
3162 {
3163   cp_parser *parser;
3164   cp_lexer *lexer;
3165   unsigned i;
3166
3167   /* cp_lexer_new_main is called before doing GC allocation because
3168      cp_lexer_new_main might load a PCH file.  */
3169   lexer = cp_lexer_new_main ();
3170
3171   /* Initialize the binops_by_token so that we can get the tree
3172      directly from the token.  */
3173   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3174     binops_by_token[binops[i].token_type] = binops[i];
3175
3176   parser = ggc_alloc_cleared_cp_parser ();
3177   parser->lexer = lexer;
3178   parser->context = cp_parser_context_new (NULL);
3179
3180   /* For now, we always accept GNU extensions.  */
3181   parser->allow_gnu_extensions_p = 1;
3182
3183   /* The `>' token is a greater-than operator, not the end of a
3184      template-id.  */
3185   parser->greater_than_is_operator_p = true;
3186
3187   parser->default_arg_ok_p = true;
3188
3189   /* We are not parsing a constant-expression.  */
3190   parser->integral_constant_expression_p = false;
3191   parser->allow_non_integral_constant_expression_p = false;
3192   parser->non_integral_constant_expression_p = false;
3193
3194   /* Local variable names are not forbidden.  */
3195   parser->local_variables_forbidden_p = false;
3196
3197   /* We are not processing an `extern "C"' declaration.  */
3198   parser->in_unbraced_linkage_specification_p = false;
3199
3200   /* We are not processing a declarator.  */
3201   parser->in_declarator_p = false;
3202
3203   /* We are not processing a template-argument-list.  */
3204   parser->in_template_argument_list_p = false;
3205
3206   /* We are not in an iteration statement.  */
3207   parser->in_statement = 0;
3208
3209   /* We are not in a switch statement.  */
3210   parser->in_switch_statement_p = false;
3211
3212   /* We are not parsing a type-id inside an expression.  */
3213   parser->in_type_id_in_expr_p = false;
3214
3215   /* Declarations aren't implicitly extern "C".  */
3216   parser->implicit_extern_c = false;
3217
3218   /* String literals should be translated to the execution character set.  */
3219   parser->translate_strings_p = true;
3220
3221   /* We are not parsing a function body.  */
3222   parser->in_function_body = false;
3223
3224   /* The unparsed function queue is empty.  */
3225   push_unparsed_function_queues (parser);
3226
3227   /* There are no classes being defined.  */
3228   parser->num_classes_being_defined = 0;
3229
3230   /* No template parameters apply.  */
3231   parser->num_template_parameter_lists = 0;
3232
3233   return parser;
3234 }
3235
3236 /* Create a cp_lexer structure which will emit the tokens in CACHE
3237    and push it onto the parser's lexer stack.  This is used for delayed
3238    parsing of in-class method bodies and default arguments, and should
3239    not be confused with tentative parsing.  */
3240 static void
3241 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3242 {
3243   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3244   lexer->next = parser->lexer;
3245   parser->lexer = lexer;
3246
3247   /* Move the current source position to that of the first token in the
3248      new lexer.  */
3249   cp_lexer_set_source_position_from_token (lexer->next_token);
3250 }
3251
3252 /* Pop the top lexer off the parser stack.  This is never used for the
3253    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3254 static void
3255 cp_parser_pop_lexer (cp_parser *parser)
3256 {
3257   cp_lexer *lexer = parser->lexer;
3258   parser->lexer = lexer->next;
3259   cp_lexer_destroy (lexer);
3260
3261   /* Put the current source position back where it was before this
3262      lexer was pushed.  */
3263   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3264 }
3265
3266 /* Lexical conventions [gram.lex]  */
3267
3268 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3269    identifier.  */
3270
3271 static tree
3272 cp_parser_identifier (cp_parser* parser)
3273 {
3274   cp_token *token;
3275
3276   /* Look for the identifier.  */
3277   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3278   /* Return the value.  */
3279   return token ? token->u.value : error_mark_node;
3280 }
3281
3282 /* Parse a sequence of adjacent string constants.  Returns a
3283    TREE_STRING representing the combined, nul-terminated string
3284    constant.  If TRANSLATE is true, translate the string to the
3285    execution character set.  If WIDE_OK is true, a wide string is
3286    invalid here.
3287
3288    C++98 [lex.string] says that if a narrow string literal token is
3289    adjacent to a wide string literal token, the behavior is undefined.
3290    However, C99 6.4.5p4 says that this results in a wide string literal.
3291    We follow C99 here, for consistency with the C front end.
3292
3293    This code is largely lifted from lex_string() in c-lex.c.
3294
3295    FUTURE: ObjC++ will need to handle @-strings here.  */
3296 static tree
3297 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3298 {
3299   tree value;
3300   size_t count;
3301   struct obstack str_ob;
3302   cpp_string str, istr, *strs;
3303   cp_token *tok;
3304   enum cpp_ttype type;
3305
3306   tok = cp_lexer_peek_token (parser->lexer);
3307   if (!cp_parser_is_string_literal (tok))
3308     {
3309       cp_parser_error (parser, "expected string-literal");
3310       return error_mark_node;
3311     }
3312
3313   type = tok->type;
3314
3315   /* Try to avoid the overhead of creating and destroying an obstack
3316      for the common case of just one string.  */
3317   if (!cp_parser_is_string_literal
3318       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3319     {
3320       cp_lexer_consume_token (parser->lexer);
3321
3322       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3323       str.len = TREE_STRING_LENGTH (tok->u.value);
3324       count = 1;
3325
3326       strs = &str;
3327     }
3328   else
3329     {
3330       gcc_obstack_init (&str_ob);
3331       count = 0;
3332
3333       do
3334         {
3335           cp_lexer_consume_token (parser->lexer);
3336           count++;
3337           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3338           str.len = TREE_STRING_LENGTH (tok->u.value);
3339
3340           if (type != tok->type)
3341             {
3342               if (type == CPP_STRING)
3343                 type = tok->type;
3344               else if (tok->type != CPP_STRING)
3345                 error_at (tok->location,
3346                           "unsupported non-standard concatenation "
3347                           "of string literals");
3348             }
3349
3350           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3351
3352           tok = cp_lexer_peek_token (parser->lexer);
3353         }
3354       while (cp_parser_is_string_literal (tok));
3355
3356       strs = (cpp_string *) obstack_finish (&str_ob);
3357     }
3358
3359   if (type != CPP_STRING && !wide_ok)
3360     {
3361       cp_parser_error (parser, "a wide string is invalid in this context");
3362       type = CPP_STRING;
3363     }
3364
3365   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3366       (parse_in, strs, count, &istr, type))
3367     {
3368       value = build_string (istr.len, (const char *)istr.text);
3369       free (CONST_CAST (unsigned char *, istr.text));
3370
3371       switch (type)
3372         {
3373         default:
3374         case CPP_STRING:
3375         case CPP_UTF8STRING:
3376           TREE_TYPE (value) = char_array_type_node;
3377           break;
3378         case CPP_STRING16:
3379           TREE_TYPE (value) = char16_array_type_node;
3380           break;
3381         case CPP_STRING32:
3382           TREE_TYPE (value) = char32_array_type_node;
3383           break;
3384         case CPP_WSTRING:
3385           TREE_TYPE (value) = wchar_array_type_node;
3386           break;
3387         }
3388
3389       value = fix_string_type (value);
3390     }
3391   else
3392     /* cpp_interpret_string has issued an error.  */
3393     value = error_mark_node;
3394
3395   if (count > 1)
3396     obstack_free (&str_ob, 0);
3397
3398   return value;
3399 }
3400
3401
3402 /* Basic concepts [gram.basic]  */
3403
3404 /* Parse a translation-unit.
3405
3406    translation-unit:
3407      declaration-seq [opt]
3408
3409    Returns TRUE if all went well.  */
3410
3411 static bool
3412 cp_parser_translation_unit (cp_parser* parser)
3413 {
3414   /* The address of the first non-permanent object on the declarator
3415      obstack.  */
3416   static void *declarator_obstack_base;
3417
3418   bool success;
3419
3420   /* Create the declarator obstack, if necessary.  */
3421   if (!cp_error_declarator)
3422     {
3423       gcc_obstack_init (&declarator_obstack);
3424       /* Create the error declarator.  */
3425       cp_error_declarator = make_declarator (cdk_error);
3426       /* Create the empty parameter list.  */
3427       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3428       /* Remember where the base of the declarator obstack lies.  */
3429       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3430     }
3431
3432   cp_parser_declaration_seq_opt (parser);
3433
3434   /* If there are no tokens left then all went well.  */
3435   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3436     {
3437       /* Get rid of the token array; we don't need it any more.  */
3438       cp_lexer_destroy (parser->lexer);
3439       parser->lexer = NULL;
3440
3441       /* This file might have been a context that's implicitly extern
3442          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3443       if (parser->implicit_extern_c)
3444         {
3445           pop_lang_context ();
3446           parser->implicit_extern_c = false;
3447         }
3448
3449       /* Finish up.  */
3450       finish_translation_unit ();
3451
3452       success = true;
3453     }
3454   else
3455     {
3456       cp_parser_error (parser, "expected declaration");
3457       success = false;
3458     }
3459
3460   /* Make sure the declarator obstack was fully cleaned up.  */
3461   gcc_assert (obstack_next_free (&declarator_obstack)
3462               == declarator_obstack_base);
3463
3464   /* All went well.  */
3465   return success;
3466 }
3467
3468 /* Expressions [gram.expr] */
3469
3470 /* Parse a primary-expression.
3471
3472    primary-expression:
3473      literal
3474      this
3475      ( expression )
3476      id-expression
3477
3478    GNU Extensions:
3479
3480    primary-expression:
3481      ( compound-statement )
3482      __builtin_va_arg ( assignment-expression , type-id )
3483      __builtin_offsetof ( type-id , offsetof-expression )
3484
3485    C++ Extensions:
3486      __has_nothrow_assign ( type-id )   
3487      __has_nothrow_constructor ( type-id )
3488      __has_nothrow_copy ( type-id )
3489      __has_trivial_assign ( type-id )   
3490      __has_trivial_constructor ( type-id )
3491      __has_trivial_copy ( type-id )
3492      __has_trivial_destructor ( type-id )
3493      __has_virtual_destructor ( type-id )     
3494      __is_abstract ( type-id )
3495      __is_base_of ( type-id , type-id )
3496      __is_class ( type-id )
3497      __is_convertible_to ( type-id , type-id )     
3498      __is_empty ( type-id )
3499      __is_enum ( type-id )
3500      __is_pod ( type-id )
3501      __is_polymorphic ( type-id )
3502      __is_union ( type-id )
3503
3504    Objective-C++ Extension:
3505
3506    primary-expression:
3507      objc-expression
3508
3509    literal:
3510      __null
3511
3512    ADDRESS_P is true iff this expression was immediately preceded by
3513    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3514    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3515    true iff this expression is a template argument.
3516
3517    Returns a representation of the expression.  Upon return, *IDK
3518    indicates what kind of id-expression (if any) was present.  */
3519
3520 static tree
3521 cp_parser_primary_expression (cp_parser *parser,
3522                               bool address_p,
3523                               bool cast_p,
3524                               bool template_arg_p,
3525                               cp_id_kind *idk)
3526 {
3527   cp_token *token = NULL;
3528
3529   /* Assume the primary expression is not an id-expression.  */
3530   *idk = CP_ID_KIND_NONE;
3531
3532   /* Peek at the next token.  */
3533   token = cp_lexer_peek_token (parser->lexer);
3534   switch (token->type)
3535     {
3536       /* literal:
3537            integer-literal
3538            character-literal
3539            floating-literal
3540            string-literal
3541            boolean-literal  */
3542     case CPP_CHAR:
3543     case CPP_CHAR16:
3544     case CPP_CHAR32:
3545     case CPP_WCHAR:
3546     case CPP_NUMBER:
3547       token = cp_lexer_consume_token (parser->lexer);
3548       if (TREE_CODE (token->u.value) == FIXED_CST)
3549         {
3550           error_at (token->location,
3551                     "fixed-point types not supported in C++");
3552           return error_mark_node;
3553         }
3554       /* Floating-point literals are only allowed in an integral
3555          constant expression if they are cast to an integral or
3556          enumeration type.  */
3557       if (TREE_CODE (token->u.value) == REAL_CST
3558           && parser->integral_constant_expression_p
3559           && pedantic)
3560         {
3561           /* CAST_P will be set even in invalid code like "int(2.7 +
3562              ...)".   Therefore, we have to check that the next token
3563              is sure to end the cast.  */
3564           if (cast_p)
3565             {
3566               cp_token *next_token;
3567
3568               next_token = cp_lexer_peek_token (parser->lexer);
3569               if (/* The comma at the end of an
3570                      enumerator-definition.  */
3571                   next_token->type != CPP_COMMA
3572                   /* The curly brace at the end of an enum-specifier.  */
3573                   && next_token->type != CPP_CLOSE_BRACE
3574                   /* The end of a statement.  */
3575                   && next_token->type != CPP_SEMICOLON
3576                   /* The end of the cast-expression.  */
3577                   && next_token->type != CPP_CLOSE_PAREN
3578                   /* The end of an array bound.  */
3579                   && next_token->type != CPP_CLOSE_SQUARE
3580                   /* The closing ">" in a template-argument-list.  */
3581                   && (next_token->type != CPP_GREATER
3582                       || parser->greater_than_is_operator_p)
3583                   /* C++0x only: A ">>" treated like two ">" tokens,
3584                      in a template-argument-list.  */
3585                   && (next_token->type != CPP_RSHIFT
3586                       || (cxx_dialect == cxx98)
3587                       || parser->greater_than_is_operator_p))
3588                 cast_p = false;
3589             }
3590
3591           /* If we are within a cast, then the constraint that the
3592              cast is to an integral or enumeration type will be
3593              checked at that point.  If we are not within a cast, then
3594              this code is invalid.  */
3595           if (!cast_p)
3596             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3597         }
3598       return token->u.value;
3599
3600     case CPP_STRING:
3601     case CPP_STRING16:
3602     case CPP_STRING32:
3603     case CPP_WSTRING:
3604     case CPP_UTF8STRING:
3605       /* ??? Should wide strings be allowed when parser->translate_strings_p
3606          is false (i.e. in attributes)?  If not, we can kill the third
3607          argument to cp_parser_string_literal.  */
3608       return cp_parser_string_literal (parser,
3609                                        parser->translate_strings_p,
3610                                        true);
3611
3612     case CPP_OPEN_PAREN:
3613       {
3614         tree expr;
3615         bool saved_greater_than_is_operator_p;
3616
3617         /* Consume the `('.  */
3618         cp_lexer_consume_token (parser->lexer);
3619         /* Within a parenthesized expression, a `>' token is always
3620            the greater-than operator.  */
3621         saved_greater_than_is_operator_p
3622           = parser->greater_than_is_operator_p;
3623         parser->greater_than_is_operator_p = true;
3624         /* If we see `( { ' then we are looking at the beginning of
3625            a GNU statement-expression.  */
3626         if (cp_parser_allow_gnu_extensions_p (parser)
3627             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3628           {
3629             /* Statement-expressions are not allowed by the standard.  */
3630             pedwarn (token->location, OPT_pedantic, 
3631                      "ISO C++ forbids braced-groups within expressions");
3632
3633             /* And they're not allowed outside of a function-body; you
3634                cannot, for example, write:
3635
3636                  int i = ({ int j = 3; j + 1; });
3637
3638                at class or namespace scope.  */
3639             if (!parser->in_function_body
3640                 || parser->in_template_argument_list_p)
3641               {
3642                 error_at (token->location,
3643                           "statement-expressions are not allowed outside "
3644                           "functions nor in template-argument lists");
3645                 cp_parser_skip_to_end_of_block_or_statement (parser);
3646                 expr = error_mark_node;
3647               }
3648             else
3649               {
3650                 /* Start the statement-expression.  */
3651                 expr = begin_stmt_expr ();
3652                 /* Parse the compound-statement.  */
3653                 cp_parser_compound_statement (parser, expr, false);
3654                 /* Finish up.  */
3655                 expr = finish_stmt_expr (expr, false);
3656               }
3657           }
3658         else
3659           {
3660             /* Parse the parenthesized expression.  */
3661             expr = cp_parser_expression (parser, cast_p, idk);
3662             /* Let the front end know that this expression was
3663                enclosed in parentheses. This matters in case, for
3664                example, the expression is of the form `A::B', since
3665                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3666                not.  */
3667             finish_parenthesized_expr (expr);
3668           }
3669         /* The `>' token might be the end of a template-id or
3670            template-parameter-list now.  */
3671         parser->greater_than_is_operator_p
3672           = saved_greater_than_is_operator_p;
3673         /* Consume the `)'.  */
3674         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3675           cp_parser_skip_to_end_of_statement (parser);
3676
3677         return expr;
3678       }
3679
3680     case CPP_OPEN_SQUARE:
3681       if (c_dialect_objc ())
3682         /* We have an Objective-C++ message. */
3683         return cp_parser_objc_expression (parser);
3684       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3685       return cp_parser_lambda_expression (parser);
3686
3687     case CPP_OBJC_STRING:
3688       if (c_dialect_objc ())
3689         /* We have an Objective-C++ string literal. */
3690         return cp_parser_objc_expression (parser);
3691       cp_parser_error (parser, "expected primary-expression");
3692       return error_mark_node;
3693
3694     case CPP_KEYWORD:
3695       switch (token->keyword)
3696         {
3697           /* These two are the boolean literals.  */
3698         case RID_TRUE:
3699           cp_lexer_consume_token (parser->lexer);
3700           return boolean_true_node;
3701         case RID_FALSE:
3702           cp_lexer_consume_token (parser->lexer);
3703           return boolean_false_node;
3704
3705           /* The `__null' literal.  */
3706         case RID_NULL:
3707           cp_lexer_consume_token (parser->lexer);
3708           return null_node;
3709
3710           /* The `nullptr' literal.  */
3711         case RID_NULLPTR:
3712           cp_lexer_consume_token (parser->lexer);
3713           return nullptr_node;
3714
3715           /* Recognize the `this' keyword.  */
3716         case RID_THIS:
3717           cp_lexer_consume_token (parser->lexer);
3718           if (parser->local_variables_forbidden_p)
3719             {
3720               error_at (token->location,
3721                         "%<this%> may not be used in this context");
3722               return error_mark_node;
3723             }
3724           /* Pointers cannot appear in constant-expressions.  */
3725           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3726             return error_mark_node;
3727           return finish_this_expr ();
3728
3729           /* The `operator' keyword can be the beginning of an
3730              id-expression.  */
3731         case RID_OPERATOR:
3732           goto id_expression;
3733
3734         case RID_FUNCTION_NAME:
3735         case RID_PRETTY_FUNCTION_NAME:
3736         case RID_C99_FUNCTION_NAME:
3737           {
3738             non_integral_constant name;
3739
3740             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3741                __func__ are the names of variables -- but they are
3742                treated specially.  Therefore, they are handled here,
3743                rather than relying on the generic id-expression logic
3744                below.  Grammatically, these names are id-expressions.
3745
3746                Consume the token.  */
3747             token = cp_lexer_consume_token (parser->lexer);
3748
3749             switch (token->keyword)
3750               {
3751               case RID_FUNCTION_NAME:
3752                 name = NIC_FUNC_NAME;
3753                 break;
3754               case RID_PRETTY_FUNCTION_NAME:
3755                 name = NIC_PRETTY_FUNC;
3756                 break;
3757               case RID_C99_FUNCTION_NAME:
3758                 name = NIC_C99_FUNC;
3759                 break;
3760               default:
3761                 gcc_unreachable ();
3762               }
3763
3764             if (cp_parser_non_integral_constant_expression (parser, name))
3765               return error_mark_node;
3766
3767             /* Look up the name.  */
3768             return finish_fname (token->u.value);
3769           }
3770
3771         case RID_VA_ARG:
3772           {
3773             tree expression;
3774             tree type;
3775
3776             /* The `__builtin_va_arg' construct is used to handle
3777                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3778             cp_lexer_consume_token (parser->lexer);
3779             /* Look for the opening `('.  */
3780             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3781             /* Now, parse the assignment-expression.  */
3782             expression = cp_parser_assignment_expression (parser,
3783                                                           /*cast_p=*/false, NULL);
3784             /* Look for the `,'.  */
3785             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3786             /* Parse the type-id.  */
3787             type = cp_parser_type_id (parser);
3788             /* Look for the closing `)'.  */
3789             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3790             /* Using `va_arg' in a constant-expression is not
3791                allowed.  */
3792             if (cp_parser_non_integral_constant_expression (parser,
3793                                                             NIC_VA_ARG))
3794               return error_mark_node;
3795             return build_x_va_arg (expression, type);
3796           }
3797
3798         case RID_OFFSETOF:
3799           return cp_parser_builtin_offsetof (parser);
3800
3801         case RID_HAS_NOTHROW_ASSIGN:
3802         case RID_HAS_NOTHROW_CONSTRUCTOR:
3803         case RID_HAS_NOTHROW_COPY:        
3804         case RID_HAS_TRIVIAL_ASSIGN:
3805         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3806         case RID_HAS_TRIVIAL_COPY:        
3807         case RID_HAS_TRIVIAL_DESTRUCTOR:
3808         case RID_HAS_VIRTUAL_DESTRUCTOR:
3809         case RID_IS_ABSTRACT:
3810         case RID_IS_BASE_OF:
3811         case RID_IS_CLASS:
3812         case RID_IS_CONVERTIBLE_TO:
3813         case RID_IS_EMPTY:
3814         case RID_IS_ENUM:
3815         case RID_IS_POD:
3816         case RID_IS_POLYMORPHIC:
3817         case RID_IS_STD_LAYOUT:
3818         case RID_IS_TRIVIAL:
3819         case RID_IS_UNION:
3820         case RID_IS_LITERAL_TYPE:
3821           return cp_parser_trait_expr (parser, token->keyword);
3822
3823         /* Objective-C++ expressions.  */
3824         case RID_AT_ENCODE:
3825         case RID_AT_PROTOCOL:
3826         case RID_AT_SELECTOR:
3827           return cp_parser_objc_expression (parser);
3828
3829         case RID_TEMPLATE:
3830           if (parser->in_function_body
3831               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3832                   == CPP_LESS))
3833             {
3834               error_at (token->location,
3835                         "a template declaration cannot appear at block scope");
3836               cp_parser_skip_to_end_of_block_or_statement (parser);
3837               return error_mark_node;
3838             }
3839         default:
3840           cp_parser_error (parser, "expected primary-expression");
3841           return error_mark_node;
3842         }
3843
3844       /* An id-expression can start with either an identifier, a
3845          `::' as the beginning of a qualified-id, or the "operator"
3846          keyword.  */
3847     case CPP_NAME:
3848     case CPP_SCOPE:
3849     case CPP_TEMPLATE_ID:
3850     case CPP_NESTED_NAME_SPECIFIER:
3851       {
3852         tree id_expression;
3853         tree decl;
3854         const char *error_msg;
3855         bool template_p;
3856         bool done;
3857         cp_token *id_expr_token;
3858
3859       id_expression:
3860         /* Parse the id-expression.  */
3861         id_expression
3862           = cp_parser_id_expression (parser,
3863                                      /*template_keyword_p=*/false,
3864                                      /*check_dependency_p=*/true,
3865                                      &template_p,
3866                                      /*declarator_p=*/false,
3867                                      /*optional_p=*/false);
3868         if (id_expression == error_mark_node)
3869           return error_mark_node;
3870         id_expr_token = token;
3871         token = cp_lexer_peek_token (parser->lexer);
3872         done = (token->type != CPP_OPEN_SQUARE
3873                 && token->type != CPP_OPEN_PAREN
3874                 && token->type != CPP_DOT
3875                 && token->type != CPP_DEREF
3876                 && token->type != CPP_PLUS_PLUS
3877                 && token->type != CPP_MINUS_MINUS);
3878         /* If we have a template-id, then no further lookup is
3879            required.  If the template-id was for a template-class, we
3880            will sometimes have a TYPE_DECL at this point.  */
3881         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3882                  || TREE_CODE (id_expression) == TYPE_DECL)
3883           decl = id_expression;
3884         /* Look up the name.  */
3885         else
3886           {
3887             tree ambiguous_decls;
3888
3889             /* If we already know that this lookup is ambiguous, then
3890                we've already issued an error message; there's no reason
3891                to check again.  */
3892             if (id_expr_token->type == CPP_NAME
3893                 && id_expr_token->ambiguous_p)
3894               {
3895                 cp_parser_simulate_error (parser);
3896                 return error_mark_node;
3897               }
3898
3899             decl = cp_parser_lookup_name (parser, id_expression,
3900                                           none_type,
3901                                           template_p,
3902                                           /*is_namespace=*/false,
3903                                           /*check_dependency=*/true,
3904                                           &ambiguous_decls,
3905                                           id_expr_token->location);
3906             /* If the lookup was ambiguous, an error will already have
3907                been issued.  */
3908             if (ambiguous_decls)
3909               return error_mark_node;
3910
3911             /* In Objective-C++, an instance variable (ivar) may be preferred
3912                to whatever cp_parser_lookup_name() found.  */
3913             decl = objc_lookup_ivar (decl, id_expression);
3914
3915             /* If name lookup gives us a SCOPE_REF, then the
3916                qualifying scope was dependent.  */
3917             if (TREE_CODE (decl) == SCOPE_REF)
3918               {
3919                 /* At this point, we do not know if DECL is a valid
3920                    integral constant expression.  We assume that it is
3921                    in fact such an expression, so that code like:
3922
3923                       template <int N> struct A {
3924                         int a[B<N>::i];
3925                       };
3926                      
3927                    is accepted.  At template-instantiation time, we
3928                    will check that B<N>::i is actually a constant.  */
3929                 return decl;
3930               }
3931             /* Check to see if DECL is a local variable in a context
3932                where that is forbidden.  */
3933             if (parser->local_variables_forbidden_p
3934                 && local_variable_p (decl))
3935               {
3936                 /* It might be that we only found DECL because we are
3937                    trying to be generous with pre-ISO scoping rules.
3938                    For example, consider:
3939
3940                      int i;
3941                      void g() {
3942                        for (int i = 0; i < 10; ++i) {}
3943                        extern void f(int j = i);
3944                      }
3945
3946                    Here, name look up will originally find the out
3947                    of scope `i'.  We need to issue a warning message,
3948                    but then use the global `i'.  */
3949                 decl = check_for_out_of_scope_variable (decl);
3950                 if (local_variable_p (decl))
3951                   {
3952                     error_at (id_expr_token->location,
3953                               "local variable %qD may not appear in this context",
3954                               decl);
3955                     return error_mark_node;
3956                   }
3957               }
3958           }
3959
3960         decl = (finish_id_expression
3961                 (id_expression, decl, parser->scope,
3962                  idk,
3963                  parser->integral_constant_expression_p,
3964                  parser->allow_non_integral_constant_expression_p,
3965                  &parser->non_integral_constant_expression_p,
3966                  template_p, done, address_p,
3967                  template_arg_p,
3968                  &error_msg,
3969                  id_expr_token->location));
3970         if (error_msg)
3971           cp_parser_error (parser, error_msg);
3972         return decl;
3973       }
3974
3975       /* Anything else is an error.  */
3976     default:
3977       cp_parser_error (parser, "expected primary-expression");
3978       return error_mark_node;
3979     }
3980 }
3981
3982 /* Parse an id-expression.
3983
3984    id-expression:
3985      unqualified-id
3986      qualified-id
3987
3988    qualified-id:
3989      :: [opt] nested-name-specifier template [opt] unqualified-id
3990      :: identifier
3991      :: operator-function-id
3992      :: template-id
3993
3994    Return a representation of the unqualified portion of the
3995    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3996    a `::' or nested-name-specifier.
3997
3998    Often, if the id-expression was a qualified-id, the caller will
3999    want to make a SCOPE_REF to represent the qualified-id.  This
4000    function does not do this in order to avoid wastefully creating
4001    SCOPE_REFs when they are not required.
4002
4003    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4004    `template' keyword.
4005
4006    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4007    uninstantiated templates.
4008
4009    If *TEMPLATE_P is non-NULL, it is set to true iff the
4010    `template' keyword is used to explicitly indicate that the entity
4011    named is a template.
4012
4013    If DECLARATOR_P is true, the id-expression is appearing as part of
4014    a declarator, rather than as part of an expression.  */
4015
4016 static tree
4017 cp_parser_id_expression (cp_parser *parser,
4018                          bool template_keyword_p,
4019                          bool check_dependency_p,
4020                          bool *template_p,
4021                          bool declarator_p,
4022                          bool optional_p)
4023 {
4024   bool global_scope_p;
4025   bool nested_name_specifier_p;
4026
4027   /* Assume the `template' keyword was not used.  */
4028   if (template_p)
4029     *template_p = template_keyword_p;
4030
4031   /* Look for the optional `::' operator.  */
4032   global_scope_p
4033     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4034        != NULL_TREE);
4035   /* Look for the optional nested-name-specifier.  */
4036   nested_name_specifier_p
4037     = (cp_parser_nested_name_specifier_opt (parser,
4038                                             /*typename_keyword_p=*/false,
4039                                             check_dependency_p,
4040                                             /*type_p=*/false,
4041                                             declarator_p)
4042        != NULL_TREE);
4043   /* If there is a nested-name-specifier, then we are looking at
4044      the first qualified-id production.  */
4045   if (nested_name_specifier_p)
4046     {
4047       tree saved_scope;
4048       tree saved_object_scope;
4049       tree saved_qualifying_scope;
4050       tree unqualified_id;
4051       bool is_template;
4052
4053       /* See if the next token is the `template' keyword.  */
4054       if (!template_p)
4055         template_p = &is_template;
4056       *template_p = cp_parser_optional_template_keyword (parser);
4057       /* Name lookup we do during the processing of the
4058          unqualified-id might obliterate SCOPE.  */
4059       saved_scope = parser->scope;
4060       saved_object_scope = parser->object_scope;
4061       saved_qualifying_scope = parser->qualifying_scope;
4062       /* Process the final unqualified-id.  */
4063       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4064                                                  check_dependency_p,
4065                                                  declarator_p,
4066                                                  /*optional_p=*/false);
4067       /* Restore the SAVED_SCOPE for our caller.  */
4068       parser->scope = saved_scope;
4069       parser->object_scope = saved_object_scope;
4070       parser->qualifying_scope = saved_qualifying_scope;
4071
4072       return unqualified_id;
4073     }
4074   /* Otherwise, if we are in global scope, then we are looking at one
4075      of the other qualified-id productions.  */
4076   else if (global_scope_p)
4077     {
4078       cp_token *token;
4079       tree id;
4080
4081       /* Peek at the next token.  */
4082       token = cp_lexer_peek_token (parser->lexer);
4083
4084       /* If it's an identifier, and the next token is not a "<", then
4085          we can avoid the template-id case.  This is an optimization
4086          for this common case.  */
4087       if (token->type == CPP_NAME
4088           && !cp_parser_nth_token_starts_template_argument_list_p
4089                (parser, 2))
4090         return cp_parser_identifier (parser);
4091
4092       cp_parser_parse_tentatively (parser);
4093       /* Try a template-id.  */
4094       id = cp_parser_template_id (parser,
4095                                   /*template_keyword_p=*/false,
4096                                   /*check_dependency_p=*/true,
4097                                   declarator_p);
4098       /* If that worked, we're done.  */
4099       if (cp_parser_parse_definitely (parser))
4100         return id;
4101
4102       /* Peek at the next token.  (Changes in the token buffer may
4103          have invalidated the pointer obtained above.)  */
4104       token = cp_lexer_peek_token (parser->lexer);
4105
4106       switch (token->type)
4107         {
4108         case CPP_NAME:
4109           return cp_parser_identifier (parser);
4110
4111         case CPP_KEYWORD:
4112           if (token->keyword == RID_OPERATOR)
4113             return cp_parser_operator_function_id (parser);
4114           /* Fall through.  */
4115
4116         default:
4117           cp_parser_error (parser, "expected id-expression");
4118           return error_mark_node;
4119         }
4120     }
4121   else
4122     return cp_parser_unqualified_id (parser, template_keyword_p,
4123                                      /*check_dependency_p=*/true,
4124                                      declarator_p,
4125                                      optional_p);
4126 }
4127
4128 /* Parse an unqualified-id.
4129
4130    unqualified-id:
4131      identifier
4132      operator-function-id
4133      conversion-function-id
4134      ~ class-name
4135      template-id
4136
4137    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4138    keyword, in a construct like `A::template ...'.
4139
4140    Returns a representation of unqualified-id.  For the `identifier'
4141    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4142    production a BIT_NOT_EXPR is returned; the operand of the
4143    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4144    other productions, see the documentation accompanying the
4145    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4146    names are looked up in uninstantiated templates.  If DECLARATOR_P
4147    is true, the unqualified-id is appearing as part of a declarator,
4148    rather than as part of an expression.  */
4149
4150 static tree
4151 cp_parser_unqualified_id (cp_parser* parser,
4152                           bool template_keyword_p,
4153                           bool check_dependency_p,
4154                           bool declarator_p,
4155                           bool optional_p)
4156 {
4157   cp_token *token;
4158
4159   /* Peek at the next token.  */
4160   token = cp_lexer_peek_token (parser->lexer);
4161
4162   switch (token->type)
4163     {
4164     case CPP_NAME:
4165       {
4166         tree id;
4167
4168         /* We don't know yet whether or not this will be a
4169            template-id.  */
4170         cp_parser_parse_tentatively (parser);
4171         /* Try a template-id.  */
4172         id = cp_parser_template_id (parser, template_keyword_p,
4173                                     check_dependency_p,
4174                                     declarator_p);
4175         /* If it worked, we're done.  */
4176         if (cp_parser_parse_definitely (parser))
4177           return id;
4178         /* Otherwise, it's an ordinary identifier.  */
4179         return cp_parser_identifier (parser);
4180       }
4181
4182     case CPP_TEMPLATE_ID:
4183       return cp_parser_template_id (parser, template_keyword_p,
4184                                     check_dependency_p,
4185                                     declarator_p);
4186
4187     case CPP_COMPL:
4188       {
4189         tree type_decl;
4190         tree qualifying_scope;
4191         tree object_scope;
4192         tree scope;
4193         bool done;
4194
4195         /* Consume the `~' token.  */
4196         cp_lexer_consume_token (parser->lexer);
4197         /* Parse the class-name.  The standard, as written, seems to
4198            say that:
4199
4200              template <typename T> struct S { ~S (); };
4201              template <typename T> S<T>::~S() {}
4202
4203            is invalid, since `~' must be followed by a class-name, but
4204            `S<T>' is dependent, and so not known to be a class.
4205            That's not right; we need to look in uninstantiated
4206            templates.  A further complication arises from:
4207
4208              template <typename T> void f(T t) {
4209                t.T::~T();
4210              }
4211
4212            Here, it is not possible to look up `T' in the scope of `T'
4213            itself.  We must look in both the current scope, and the
4214            scope of the containing complete expression.
4215
4216            Yet another issue is:
4217
4218              struct S {
4219                int S;
4220                ~S();
4221              };
4222
4223              S::~S() {}
4224
4225            The standard does not seem to say that the `S' in `~S'
4226            should refer to the type `S' and not the data member
4227            `S::S'.  */
4228
4229         /* DR 244 says that we look up the name after the "~" in the
4230            same scope as we looked up the qualifying name.  That idea
4231            isn't fully worked out; it's more complicated than that.  */
4232         scope = parser->scope;
4233         object_scope = parser->object_scope;
4234         qualifying_scope = parser->qualifying_scope;
4235
4236         /* Check for invalid scopes.  */
4237         if (scope == error_mark_node)
4238           {
4239             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4240               cp_lexer_consume_token (parser->lexer);
4241             return error_mark_node;
4242           }
4243         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4244           {
4245             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4246               error_at (token->location,
4247                         "scope %qT before %<~%> is not a class-name",
4248                         scope);
4249             cp_parser_simulate_error (parser);
4250             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4251               cp_lexer_consume_token (parser->lexer);
4252             return error_mark_node;
4253           }
4254         gcc_assert (!scope || TYPE_P (scope));
4255
4256         /* If the name is of the form "X::~X" it's OK even if X is a
4257            typedef.  */
4258         token = cp_lexer_peek_token (parser->lexer);
4259         if (scope
4260             && token->type == CPP_NAME
4261             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4262                 != CPP_LESS)
4263             && (token->u.value == TYPE_IDENTIFIER (scope)
4264                 || constructor_name_p (token->u.value, scope)))
4265           {
4266             cp_lexer_consume_token (parser->lexer);
4267             return build_nt (BIT_NOT_EXPR, scope);
4268           }
4269
4270         /* If there was an explicit qualification (S::~T), first look
4271            in the scope given by the qualification (i.e., S).
4272
4273            Note: in the calls to cp_parser_class_name below we pass
4274            typename_type so that lookup finds the injected-class-name
4275            rather than the constructor.  */
4276         done = false;
4277         type_decl = NULL_TREE;
4278         if (scope)
4279           {
4280             cp_parser_parse_tentatively (parser);
4281             type_decl = cp_parser_class_name (parser,
4282                                               /*typename_keyword_p=*/false,
4283                                               /*template_keyword_p=*/false,
4284                                               typename_type,
4285                                               /*check_dependency=*/false,
4286                                               /*class_head_p=*/false,
4287                                               declarator_p);
4288             if (cp_parser_parse_definitely (parser))
4289               done = true;
4290           }
4291         /* In "N::S::~S", look in "N" as well.  */
4292         if (!done && scope && qualifying_scope)
4293           {
4294             cp_parser_parse_tentatively (parser);
4295             parser->scope = qualifying_scope;
4296             parser->object_scope = NULL_TREE;
4297             parser->qualifying_scope = NULL_TREE;
4298             type_decl
4299               = cp_parser_class_name (parser,
4300                                       /*typename_keyword_p=*/false,
4301                                       /*template_keyword_p=*/false,
4302                                       typename_type,
4303                                       /*check_dependency=*/false,
4304                                       /*class_head_p=*/false,
4305                                       declarator_p);
4306             if (cp_parser_parse_definitely (parser))
4307               done = true;
4308           }
4309         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4310         else if (!done && object_scope)
4311           {
4312             cp_parser_parse_tentatively (parser);
4313             parser->scope = object_scope;
4314             parser->object_scope = NULL_TREE;
4315             parser->qualifying_scope = NULL_TREE;
4316             type_decl
4317               = cp_parser_class_name (parser,
4318                                       /*typename_keyword_p=*/false,
4319                                       /*template_keyword_p=*/false,
4320                                       typename_type,
4321                                       /*check_dependency=*/false,
4322                                       /*class_head_p=*/false,
4323                                       declarator_p);
4324             if (cp_parser_parse_definitely (parser))
4325               done = true;
4326           }
4327         /* Look in the surrounding context.  */
4328         if (!done)
4329           {
4330             parser->scope = NULL_TREE;
4331             parser->object_scope = NULL_TREE;
4332             parser->qualifying_scope = NULL_TREE;
4333             if (processing_template_decl)
4334               cp_parser_parse_tentatively (parser);
4335             type_decl
4336               = cp_parser_class_name (parser,
4337                                       /*typename_keyword_p=*/false,
4338                                       /*template_keyword_p=*/false,
4339                                       typename_type,
4340                                       /*check_dependency=*/false,
4341                                       /*class_head_p=*/false,
4342                                       declarator_p);
4343             if (processing_template_decl
4344                 && ! cp_parser_parse_definitely (parser))
4345               {
4346                 /* We couldn't find a type with this name, so just accept
4347                    it and check for a match at instantiation time.  */
4348                 type_decl = cp_parser_identifier (parser);
4349                 if (type_decl != error_mark_node)
4350                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4351                 return type_decl;
4352               }
4353           }
4354         /* If an error occurred, assume that the name of the
4355            destructor is the same as the name of the qualifying
4356            class.  That allows us to keep parsing after running
4357            into ill-formed destructor names.  */
4358         if (type_decl == error_mark_node && scope)
4359           return build_nt (BIT_NOT_EXPR, scope);
4360         else if (type_decl == error_mark_node)
4361           return error_mark_node;
4362
4363         /* Check that destructor name and scope match.  */
4364         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4365           {
4366             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4367               error_at (token->location,
4368                         "declaration of %<~%T%> as member of %qT",
4369                         type_decl, scope);
4370             cp_parser_simulate_error (parser);
4371             return error_mark_node;
4372           }
4373
4374         /* [class.dtor]
4375
4376            A typedef-name that names a class shall not be used as the
4377            identifier in the declarator for a destructor declaration.  */
4378         if (declarator_p
4379             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4380             && !DECL_SELF_REFERENCE_P (type_decl)
4381             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4382           error_at (token->location,
4383                     "typedef-name %qD used as destructor declarator",
4384                     type_decl);
4385
4386         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4387       }
4388
4389     case CPP_KEYWORD:
4390       if (token->keyword == RID_OPERATOR)
4391         {
4392           tree id;
4393
4394           /* This could be a template-id, so we try that first.  */
4395           cp_parser_parse_tentatively (parser);
4396           /* Try a template-id.  */
4397           id = cp_parser_template_id (parser, template_keyword_p,
4398                                       /*check_dependency_p=*/true,
4399                                       declarator_p);
4400           /* If that worked, we're done.  */
4401           if (cp_parser_parse_definitely (parser))
4402             return id;
4403           /* We still don't know whether we're looking at an
4404              operator-function-id or a conversion-function-id.  */
4405           cp_parser_parse_tentatively (parser);
4406           /* Try an operator-function-id.  */
4407           id = cp_parser_operator_function_id (parser);
4408           /* If that didn't work, try a conversion-function-id.  */
4409           if (!cp_parser_parse_definitely (parser))
4410             id = cp_parser_conversion_function_id (parser);
4411
4412           return id;
4413         }
4414       /* Fall through.  */
4415
4416     default:
4417       if (optional_p)
4418         return NULL_TREE;
4419       cp_parser_error (parser, "expected unqualified-id");
4420       return error_mark_node;
4421     }
4422 }
4423
4424 /* Parse an (optional) nested-name-specifier.
4425
4426    nested-name-specifier: [C++98]
4427      class-or-namespace-name :: nested-name-specifier [opt]
4428      class-or-namespace-name :: template nested-name-specifier [opt]
4429
4430    nested-name-specifier: [C++0x]
4431      type-name ::
4432      namespace-name ::
4433      nested-name-specifier identifier ::
4434      nested-name-specifier template [opt] simple-template-id ::
4435
4436    PARSER->SCOPE should be set appropriately before this function is
4437    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4438    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4439    in name lookups.
4440
4441    Sets PARSER->SCOPE to the class (TYPE) or namespace
4442    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4443    it unchanged if there is no nested-name-specifier.  Returns the new
4444    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4445
4446    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4447    part of a declaration and/or decl-specifier.  */
4448
4449 static tree
4450 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4451                                      bool typename_keyword_p,
4452                                      bool check_dependency_p,
4453                                      bool type_p,
4454                                      bool is_declaration)
4455 {
4456   bool success = false;
4457   cp_token_position start = 0;
4458   cp_token *token;
4459
4460   /* Remember where the nested-name-specifier starts.  */
4461   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4462     {
4463       start = cp_lexer_token_position (parser->lexer, false);
4464       push_deferring_access_checks (dk_deferred);
4465     }
4466
4467   while (true)
4468     {
4469       tree new_scope;
4470       tree old_scope;
4471       tree saved_qualifying_scope;
4472       bool template_keyword_p;
4473
4474       /* Spot cases that cannot be the beginning of a
4475          nested-name-specifier.  */
4476       token = cp_lexer_peek_token (parser->lexer);
4477
4478       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4479          the already parsed nested-name-specifier.  */
4480       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4481         {
4482           /* Grab the nested-name-specifier and continue the loop.  */
4483           cp_parser_pre_parsed_nested_name_specifier (parser);
4484           /* If we originally encountered this nested-name-specifier
4485              with IS_DECLARATION set to false, we will not have
4486              resolved TYPENAME_TYPEs, so we must do so here.  */
4487           if (is_declaration
4488               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4489             {
4490               new_scope = resolve_typename_type (parser->scope,
4491                                                  /*only_current_p=*/false);
4492               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4493                 parser->scope = new_scope;
4494             }
4495           success = true;
4496           continue;
4497         }
4498
4499       /* Spot cases that cannot be the beginning of a
4500          nested-name-specifier.  On the second and subsequent times
4501          through the loop, we look for the `template' keyword.  */
4502       if (success && token->keyword == RID_TEMPLATE)
4503         ;
4504       /* A template-id can start a nested-name-specifier.  */
4505       else if (token->type == CPP_TEMPLATE_ID)
4506         ;
4507       else
4508         {
4509           /* If the next token is not an identifier, then it is
4510              definitely not a type-name or namespace-name.  */
4511           if (token->type != CPP_NAME)
4512             break;
4513           /* If the following token is neither a `<' (to begin a
4514              template-id), nor a `::', then we are not looking at a
4515              nested-name-specifier.  */
4516           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4517           if (token->type != CPP_SCOPE
4518               && !cp_parser_nth_token_starts_template_argument_list_p
4519                   (parser, 2))
4520             break;
4521         }
4522
4523       /* The nested-name-specifier is optional, so we parse
4524          tentatively.  */
4525       cp_parser_parse_tentatively (parser);
4526
4527       /* Look for the optional `template' keyword, if this isn't the
4528          first time through the loop.  */
4529       if (success)
4530         template_keyword_p = cp_parser_optional_template_keyword (parser);
4531       else
4532         template_keyword_p = false;
4533
4534       /* Save the old scope since the name lookup we are about to do
4535          might destroy it.  */
4536       old_scope = parser->scope;
4537       saved_qualifying_scope = parser->qualifying_scope;
4538       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4539          look up names in "X<T>::I" in order to determine that "Y" is
4540          a template.  So, if we have a typename at this point, we make
4541          an effort to look through it.  */
4542       if (is_declaration
4543           && !typename_keyword_p
4544           && parser->scope
4545           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4546         parser->scope = resolve_typename_type (parser->scope,
4547                                                /*only_current_p=*/false);
4548       /* Parse the qualifying entity.  */
4549       new_scope
4550         = cp_parser_qualifying_entity (parser,
4551                                        typename_keyword_p,
4552                                        template_keyword_p,
4553                                        check_dependency_p,
4554                                        type_p,
4555                                        is_declaration);
4556       /* Look for the `::' token.  */
4557       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4558
4559       /* If we found what we wanted, we keep going; otherwise, we're
4560          done.  */
4561       if (!cp_parser_parse_definitely (parser))
4562         {
4563           bool error_p = false;
4564
4565           /* Restore the OLD_SCOPE since it was valid before the
4566              failed attempt at finding the last
4567              class-or-namespace-name.  */
4568           parser->scope = old_scope;
4569           parser->qualifying_scope = saved_qualifying_scope;
4570           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4571             break;
4572           /* If the next token is an identifier, and the one after
4573              that is a `::', then any valid interpretation would have
4574              found a class-or-namespace-name.  */
4575           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4576                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4577                      == CPP_SCOPE)
4578                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4579                      != CPP_COMPL))
4580             {
4581               token = cp_lexer_consume_token (parser->lexer);
4582               if (!error_p)
4583                 {
4584                   if (!token->ambiguous_p)
4585                     {
4586                       tree decl;
4587                       tree ambiguous_decls;
4588
4589                       decl = cp_parser_lookup_name (parser, token->u.value,
4590                                                     none_type,
4591                                                     /*is_template=*/false,
4592                                                     /*is_namespace=*/false,
4593                                                     /*check_dependency=*/true,
4594                                                     &ambiguous_decls,
4595                                                     token->location);
4596                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4597                         error_at (token->location,
4598                                   "%qD used without template parameters",
4599                                   decl);
4600                       else if (ambiguous_decls)
4601                         {
4602                           error_at (token->location,
4603                                     "reference to %qD is ambiguous",
4604                                     token->u.value);
4605                           print_candidates (ambiguous_decls);
4606                           decl = error_mark_node;
4607                         }
4608                       else
4609                         {
4610                           if (cxx_dialect != cxx98)
4611                             cp_parser_name_lookup_error
4612                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4613                              token->location);
4614                           else
4615                             cp_parser_name_lookup_error
4616                             (parser, token->u.value, decl, NLE_CXX98,
4617                              token->location);
4618                         }
4619                     }
4620                   parser->scope = error_mark_node;
4621                   error_p = true;
4622                   /* Treat this as a successful nested-name-specifier
4623                      due to:
4624
4625                      [basic.lookup.qual]
4626
4627                      If the name found is not a class-name (clause
4628                      _class_) or namespace-name (_namespace.def_), the
4629                      program is ill-formed.  */
4630                   success = true;
4631                 }
4632               cp_lexer_consume_token (parser->lexer);
4633             }
4634           break;
4635         }
4636       /* We've found one valid nested-name-specifier.  */
4637       success = true;
4638       /* Name lookup always gives us a DECL.  */
4639       if (TREE_CODE (new_scope) == TYPE_DECL)
4640         new_scope = TREE_TYPE (new_scope);
4641       /* Uses of "template" must be followed by actual templates.  */
4642       if (template_keyword_p
4643           && !(CLASS_TYPE_P (new_scope)
4644                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4645                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4646                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4647           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4648                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4649                    == TEMPLATE_ID_EXPR)))
4650         permerror (input_location, TYPE_P (new_scope)
4651                    ? "%qT is not a template"
4652                    : "%qD is not a template",
4653                    new_scope);
4654       /* If it is a class scope, try to complete it; we are about to
4655          be looking up names inside the class.  */
4656       if (TYPE_P (new_scope)
4657           /* Since checking types for dependency can be expensive,
4658              avoid doing it if the type is already complete.  */
4659           && !COMPLETE_TYPE_P (new_scope)
4660           /* Do not try to complete dependent types.  */
4661           && !dependent_type_p (new_scope))
4662         {
4663           new_scope = complete_type (new_scope);
4664           /* If it is a typedef to current class, use the current
4665              class instead, as the typedef won't have any names inside
4666              it yet.  */
4667           if (!COMPLETE_TYPE_P (new_scope)
4668               && currently_open_class (new_scope))
4669             new_scope = TYPE_MAIN_VARIANT (new_scope);
4670         }
4671       /* Make sure we look in the right scope the next time through
4672          the loop.  */
4673       parser->scope = new_scope;
4674     }
4675
4676   /* If parsing tentatively, replace the sequence of tokens that makes
4677      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4678      token.  That way, should we re-parse the token stream, we will
4679      not have to repeat the effort required to do the parse, nor will
4680      we issue duplicate error messages.  */
4681   if (success && start)
4682     {
4683       cp_token *token;
4684
4685       token = cp_lexer_token_at (parser->lexer, start);
4686       /* Reset the contents of the START token.  */
4687       token->type = CPP_NESTED_NAME_SPECIFIER;
4688       /* Retrieve any deferred checks.  Do not pop this access checks yet
4689          so the memory will not be reclaimed during token replacing below.  */
4690       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4691       token->u.tree_check_value->value = parser->scope;
4692       token->u.tree_check_value->checks = get_deferred_access_checks ();
4693       token->u.tree_check_value->qualifying_scope =
4694         parser->qualifying_scope;
4695       token->keyword = RID_MAX;
4696
4697       /* Purge all subsequent tokens.  */
4698       cp_lexer_purge_tokens_after (parser->lexer, start);
4699     }
4700
4701   if (start)
4702     pop_to_parent_deferring_access_checks ();
4703
4704   return success ? parser->scope : NULL_TREE;
4705 }
4706
4707 /* Parse a nested-name-specifier.  See
4708    cp_parser_nested_name_specifier_opt for details.  This function
4709    behaves identically, except that it will an issue an error if no
4710    nested-name-specifier is present.  */
4711
4712 static tree
4713 cp_parser_nested_name_specifier (cp_parser *parser,
4714                                  bool typename_keyword_p,
4715                                  bool check_dependency_p,
4716                                  bool type_p,
4717                                  bool is_declaration)
4718 {
4719   tree scope;
4720
4721   /* Look for the nested-name-specifier.  */
4722   scope = cp_parser_nested_name_specifier_opt (parser,
4723                                                typename_keyword_p,
4724                                                check_dependency_p,
4725                                                type_p,
4726                                                is_declaration);
4727   /* If it was not present, issue an error message.  */
4728   if (!scope)
4729     {
4730       cp_parser_error (parser, "expected nested-name-specifier");
4731       parser->scope = NULL_TREE;
4732     }
4733
4734   return scope;
4735 }
4736
4737 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4738    this is either a class-name or a namespace-name (which corresponds
4739    to the class-or-namespace-name production in the grammar). For
4740    C++0x, it can also be a type-name that refers to an enumeration
4741    type.
4742
4743    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4744    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4745    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4746    TYPE_P is TRUE iff the next name should be taken as a class-name,
4747    even the same name is declared to be another entity in the same
4748    scope.
4749
4750    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4751    specified by the class-or-namespace-name.  If neither is found the
4752    ERROR_MARK_NODE is returned.  */
4753
4754 static tree
4755 cp_parser_qualifying_entity (cp_parser *parser,
4756                              bool typename_keyword_p,
4757                              bool template_keyword_p,
4758                              bool check_dependency_p,
4759                              bool type_p,
4760                              bool is_declaration)
4761 {
4762   tree saved_scope;
4763   tree saved_qualifying_scope;
4764   tree saved_object_scope;
4765   tree scope;
4766   bool only_class_p;
4767   bool successful_parse_p;
4768
4769   /* Before we try to parse the class-name, we must save away the
4770      current PARSER->SCOPE since cp_parser_class_name will destroy
4771      it.  */
4772   saved_scope = parser->scope;
4773   saved_qualifying_scope = parser->qualifying_scope;
4774   saved_object_scope = parser->object_scope;
4775   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4776      there is no need to look for a namespace-name.  */
4777   only_class_p = template_keyword_p 
4778     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4779   if (!only_class_p)
4780     cp_parser_parse_tentatively (parser);
4781   scope = cp_parser_class_name (parser,
4782                                 typename_keyword_p,
4783                                 template_keyword_p,
4784                                 type_p ? class_type : none_type,
4785                                 check_dependency_p,
4786                                 /*class_head_p=*/false,
4787                                 is_declaration);
4788   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4789   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4790   if (!only_class_p 
4791       && cxx_dialect != cxx98
4792       && !successful_parse_p)
4793     {
4794       /* Restore the saved scope.  */
4795       parser->scope = saved_scope;
4796       parser->qualifying_scope = saved_qualifying_scope;
4797       parser->object_scope = saved_object_scope;
4798
4799       /* Parse tentatively.  */
4800       cp_parser_parse_tentatively (parser);
4801      
4802       /* Parse a typedef-name or enum-name.  */
4803       scope = cp_parser_nonclass_name (parser);
4804
4805       /* "If the name found does not designate a namespace or a class,
4806          enumeration, or dependent type, the program is ill-formed."
4807
4808          We cover classes and dependent types above and namespaces below,
4809          so this code is only looking for enums.  */
4810       if (!scope || TREE_CODE (scope) != TYPE_DECL
4811           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4812         cp_parser_simulate_error (parser);
4813
4814       successful_parse_p = cp_parser_parse_definitely (parser);
4815     }
4816   /* If that didn't work, try for a namespace-name.  */
4817   if (!only_class_p && !successful_parse_p)
4818     {
4819       /* Restore the saved scope.  */
4820       parser->scope = saved_scope;
4821       parser->qualifying_scope = saved_qualifying_scope;
4822       parser->object_scope = saved_object_scope;
4823       /* If we are not looking at an identifier followed by the scope
4824          resolution operator, then this is not part of a
4825          nested-name-specifier.  (Note that this function is only used
4826          to parse the components of a nested-name-specifier.)  */
4827       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4828           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4829         return error_mark_node;
4830       scope = cp_parser_namespace_name (parser);
4831     }
4832
4833   return scope;
4834 }
4835
4836 /* Parse a postfix-expression.
4837
4838    postfix-expression:
4839      primary-expression
4840      postfix-expression [ expression ]
4841      postfix-expression ( expression-list [opt] )
4842      simple-type-specifier ( expression-list [opt] )
4843      typename :: [opt] nested-name-specifier identifier
4844        ( expression-list [opt] )
4845      typename :: [opt] nested-name-specifier template [opt] template-id
4846        ( expression-list [opt] )
4847      postfix-expression . template [opt] id-expression
4848      postfix-expression -> template [opt] id-expression
4849      postfix-expression . pseudo-destructor-name
4850      postfix-expression -> pseudo-destructor-name
4851      postfix-expression ++
4852      postfix-expression --
4853      dynamic_cast < type-id > ( expression )
4854      static_cast < type-id > ( expression )
4855      reinterpret_cast < type-id > ( expression )
4856      const_cast < type-id > ( expression )
4857      typeid ( expression )
4858      typeid ( type-id )
4859
4860    GNU Extension:
4861
4862    postfix-expression:
4863      ( type-id ) { initializer-list , [opt] }
4864
4865    This extension is a GNU version of the C99 compound-literal
4866    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4867    but they are essentially the same concept.)
4868
4869    If ADDRESS_P is true, the postfix expression is the operand of the
4870    `&' operator.  CAST_P is true if this expression is the target of a
4871    cast.
4872
4873    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4874    class member access expressions [expr.ref].
4875
4876    Returns a representation of the expression.  */
4877
4878 static tree
4879 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4880                               bool member_access_only_p,
4881                               cp_id_kind * pidk_return)
4882 {
4883   cp_token *token;
4884   enum rid keyword;
4885   cp_id_kind idk = CP_ID_KIND_NONE;
4886   tree postfix_expression = NULL_TREE;
4887   bool is_member_access = false;
4888
4889   /* Peek at the next token.  */
4890   token = cp_lexer_peek_token (parser->lexer);
4891   /* Some of the productions are determined by keywords.  */
4892   keyword = token->keyword;
4893   switch (keyword)
4894     {
4895     case RID_DYNCAST:
4896     case RID_STATCAST:
4897     case RID_REINTCAST:
4898     case RID_CONSTCAST:
4899       {
4900         tree type;
4901         tree expression;
4902         const char *saved_message;
4903
4904         /* All of these can be handled in the same way from the point
4905            of view of parsing.  Begin by consuming the token
4906            identifying the cast.  */
4907         cp_lexer_consume_token (parser->lexer);
4908
4909         /* New types cannot be defined in the cast.  */
4910         saved_message = parser->type_definition_forbidden_message;
4911         parser->type_definition_forbidden_message
4912           = G_("types may not be defined in casts");
4913
4914         /* Look for the opening `<'.  */
4915         cp_parser_require (parser, CPP_LESS, RT_LESS);
4916         /* Parse the type to which we are casting.  */
4917         type = cp_parser_type_id (parser);
4918         /* Look for the closing `>'.  */
4919         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4920         /* Restore the old message.  */
4921         parser->type_definition_forbidden_message = saved_message;
4922
4923         /* And the expression which is being cast.  */
4924         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4925         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4926         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4927
4928         /* Only type conversions to integral or enumeration types
4929            can be used in constant-expressions.  */
4930         if (!cast_valid_in_integral_constant_expression_p (type)
4931             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4932           return error_mark_node;
4933
4934         switch (keyword)
4935           {
4936           case RID_DYNCAST:
4937             postfix_expression
4938               = build_dynamic_cast (type, expression, tf_warning_or_error);
4939             break;
4940           case RID_STATCAST:
4941             postfix_expression
4942               = build_static_cast (type, expression, tf_warning_or_error);
4943             break;
4944           case RID_REINTCAST:
4945             postfix_expression
4946               = build_reinterpret_cast (type, expression, 
4947                                         tf_warning_or_error);
4948             break;
4949           case RID_CONSTCAST:
4950             postfix_expression
4951               = build_const_cast (type, expression, tf_warning_or_error);
4952             break;
4953           default:
4954             gcc_unreachable ();
4955           }
4956       }
4957       break;
4958
4959     case RID_TYPEID:
4960       {
4961         tree type;
4962         const char *saved_message;
4963         bool saved_in_type_id_in_expr_p;
4964
4965         /* Consume the `typeid' token.  */
4966         cp_lexer_consume_token (parser->lexer);
4967         /* Look for the `(' token.  */
4968         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4969         /* Types cannot be defined in a `typeid' expression.  */
4970         saved_message = parser->type_definition_forbidden_message;
4971         parser->type_definition_forbidden_message
4972           = G_("types may not be defined in a %<typeid%> expression");
4973         /* We can't be sure yet whether we're looking at a type-id or an
4974            expression.  */
4975         cp_parser_parse_tentatively (parser);
4976         /* Try a type-id first.  */
4977         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4978         parser->in_type_id_in_expr_p = true;
4979         type = cp_parser_type_id (parser);
4980         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4981         /* Look for the `)' token.  Otherwise, we can't be sure that
4982            we're not looking at an expression: consider `typeid (int
4983            (3))', for example.  */
4984         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4985         /* If all went well, simply lookup the type-id.  */
4986         if (cp_parser_parse_definitely (parser))
4987           postfix_expression = get_typeid (type);
4988         /* Otherwise, fall back to the expression variant.  */
4989         else
4990           {
4991             tree expression;
4992
4993             /* Look for an expression.  */
4994             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4995             /* Compute its typeid.  */
4996             postfix_expression = build_typeid (expression);
4997             /* Look for the `)' token.  */
4998             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4999           }
5000         /* Restore the saved message.  */
5001         parser->type_definition_forbidden_message = saved_message;
5002         /* `typeid' may not appear in an integral constant expression.  */
5003         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5004           return error_mark_node;
5005       }
5006       break;
5007
5008     case RID_TYPENAME:
5009       {
5010         tree type;
5011         /* The syntax permitted here is the same permitted for an
5012            elaborated-type-specifier.  */
5013         type = cp_parser_elaborated_type_specifier (parser,
5014                                                     /*is_friend=*/false,
5015                                                     /*is_declaration=*/false);
5016         postfix_expression = cp_parser_functional_cast (parser, type);
5017       }
5018       break;
5019
5020     default:
5021       {
5022         tree type;
5023
5024         /* If the next thing is a simple-type-specifier, we may be
5025            looking at a functional cast.  We could also be looking at
5026            an id-expression.  So, we try the functional cast, and if
5027            that doesn't work we fall back to the primary-expression.  */
5028         cp_parser_parse_tentatively (parser);
5029         /* Look for the simple-type-specifier.  */
5030         type = cp_parser_simple_type_specifier (parser,
5031                                                 /*decl_specs=*/NULL,
5032                                                 CP_PARSER_FLAGS_NONE);
5033         /* Parse the cast itself.  */
5034         if (!cp_parser_error_occurred (parser))
5035           postfix_expression
5036             = cp_parser_functional_cast (parser, type);
5037         /* If that worked, we're done.  */
5038         if (cp_parser_parse_definitely (parser))
5039           break;
5040
5041         /* If the functional-cast didn't work out, try a
5042            compound-literal.  */
5043         if (cp_parser_allow_gnu_extensions_p (parser)
5044             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5045           {
5046             VEC(constructor_elt,gc) *initializer_list = NULL;
5047             bool saved_in_type_id_in_expr_p;
5048
5049             cp_parser_parse_tentatively (parser);
5050             /* Consume the `('.  */
5051             cp_lexer_consume_token (parser->lexer);
5052             /* Parse the type.  */
5053             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5054             parser->in_type_id_in_expr_p = true;
5055             type = cp_parser_type_id (parser);
5056             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5057             /* Look for the `)'.  */
5058             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5059             /* Look for the `{'.  */
5060             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5061             /* If things aren't going well, there's no need to
5062                keep going.  */
5063             if (!cp_parser_error_occurred (parser))
5064               {
5065                 bool non_constant_p;
5066                 /* Parse the initializer-list.  */
5067                 initializer_list
5068                   = cp_parser_initializer_list (parser, &non_constant_p);
5069                 /* Allow a trailing `,'.  */
5070                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5071                   cp_lexer_consume_token (parser->lexer);
5072                 /* Look for the final `}'.  */
5073                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5074               }
5075             /* If that worked, we're definitely looking at a
5076                compound-literal expression.  */
5077             if (cp_parser_parse_definitely (parser))
5078               {
5079                 /* Warn the user that a compound literal is not
5080                    allowed in standard C++.  */
5081                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5082                 /* For simplicity, we disallow compound literals in
5083                    constant-expressions.  We could
5084                    allow compound literals of integer type, whose
5085                    initializer was a constant, in constant
5086                    expressions.  Permitting that usage, as a further
5087                    extension, would not change the meaning of any
5088                    currently accepted programs.  (Of course, as
5089                    compound literals are not part of ISO C++, the
5090                    standard has nothing to say.)  */
5091                 if (cp_parser_non_integral_constant_expression (parser,
5092                                                                 NIC_NCC))
5093                   {
5094                     postfix_expression = error_mark_node;
5095                     break;
5096                   }
5097                 /* Form the representation of the compound-literal.  */
5098                 postfix_expression
5099                   = (finish_compound_literal
5100                      (type, build_constructor (init_list_type_node,
5101                                                initializer_list)));
5102                 break;
5103               }
5104           }
5105
5106         /* It must be a primary-expression.  */
5107         postfix_expression
5108           = cp_parser_primary_expression (parser, address_p, cast_p,
5109                                           /*template_arg_p=*/false,
5110                                           &idk);
5111       }
5112       break;
5113     }
5114
5115   /* Keep looping until the postfix-expression is complete.  */
5116   while (true)
5117     {
5118       if (idk == CP_ID_KIND_UNQUALIFIED
5119           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5120           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5121         /* It is not a Koenig lookup function call.  */
5122         postfix_expression
5123           = unqualified_name_lookup_error (postfix_expression);
5124
5125       /* Peek at the next token.  */
5126       token = cp_lexer_peek_token (parser->lexer);
5127
5128       switch (token->type)
5129         {
5130         case CPP_OPEN_SQUARE:
5131           postfix_expression
5132             = cp_parser_postfix_open_square_expression (parser,
5133                                                         postfix_expression,
5134                                                         false);
5135           idk = CP_ID_KIND_NONE;
5136           is_member_access = false;
5137           break;
5138
5139         case CPP_OPEN_PAREN:
5140           /* postfix-expression ( expression-list [opt] ) */
5141           {
5142             bool koenig_p;
5143             bool is_builtin_constant_p;
5144             bool saved_integral_constant_expression_p = false;
5145             bool saved_non_integral_constant_expression_p = false;
5146             VEC(tree,gc) *args;
5147
5148             is_member_access = false;
5149
5150             is_builtin_constant_p
5151               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5152             if (is_builtin_constant_p)
5153               {
5154                 /* The whole point of __builtin_constant_p is to allow
5155                    non-constant expressions to appear as arguments.  */
5156                 saved_integral_constant_expression_p
5157                   = parser->integral_constant_expression_p;
5158                 saved_non_integral_constant_expression_p
5159                   = parser->non_integral_constant_expression_p;
5160                 parser->integral_constant_expression_p = false;
5161               }
5162             args = (cp_parser_parenthesized_expression_list
5163                     (parser, non_attr,
5164                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5165                      /*non_constant_p=*/NULL));
5166             if (is_builtin_constant_p)
5167               {
5168                 parser->integral_constant_expression_p
5169                   = saved_integral_constant_expression_p;
5170                 parser->non_integral_constant_expression_p
5171                   = saved_non_integral_constant_expression_p;
5172               }
5173
5174             if (args == NULL)
5175               {
5176                 postfix_expression = error_mark_node;
5177                 break;
5178               }
5179
5180             /* Function calls are not permitted in
5181                constant-expressions.  */
5182             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5183                 && cp_parser_non_integral_constant_expression (parser,
5184                                                                NIC_FUNC_CALL))
5185               {
5186                 postfix_expression = error_mark_node;
5187                 release_tree_vector (args);
5188                 break;
5189               }
5190
5191             koenig_p = false;
5192             if (idk == CP_ID_KIND_UNQUALIFIED
5193                 || idk == CP_ID_KIND_TEMPLATE_ID)
5194               {
5195                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5196                   {
5197                     if (!VEC_empty (tree, args))
5198                       {
5199                         koenig_p = true;
5200                         if (!any_type_dependent_arguments_p (args))
5201                           postfix_expression
5202                             = perform_koenig_lookup (postfix_expression, args,
5203                                                      /*include_std=*/false);
5204                       }
5205                     else
5206                       postfix_expression
5207                         = unqualified_fn_lookup_error (postfix_expression);
5208                   }
5209                 /* We do not perform argument-dependent lookup if
5210                    normal lookup finds a non-function, in accordance
5211                    with the expected resolution of DR 218.  */
5212                 else if (!VEC_empty (tree, args)
5213                          && is_overloaded_fn (postfix_expression))
5214                   {
5215                     tree fn = get_first_fn (postfix_expression);
5216                     fn = STRIP_TEMPLATE (fn);
5217
5218                     /* Do not do argument dependent lookup if regular
5219                        lookup finds a member function or a block-scope
5220                        function declaration.  [basic.lookup.argdep]/3  */
5221                     if (!DECL_FUNCTION_MEMBER_P (fn)
5222                         && !DECL_LOCAL_FUNCTION_P (fn))
5223                       {
5224                         koenig_p = true;
5225                         if (!any_type_dependent_arguments_p (args))
5226                           postfix_expression
5227                             = perform_koenig_lookup (postfix_expression, args,
5228                                                      /*include_std=*/false);
5229                       }
5230                   }
5231               }
5232
5233             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5234               {
5235                 tree instance = TREE_OPERAND (postfix_expression, 0);
5236                 tree fn = TREE_OPERAND (postfix_expression, 1);
5237
5238                 if (processing_template_decl
5239                     && (type_dependent_expression_p (instance)
5240                         || (!BASELINK_P (fn)
5241                             && TREE_CODE (fn) != FIELD_DECL)
5242                         || type_dependent_expression_p (fn)
5243                         || any_type_dependent_arguments_p (args)))
5244                   {
5245                     postfix_expression
5246                       = build_nt_call_vec (postfix_expression, args);
5247                     release_tree_vector (args);
5248                     break;
5249                   }
5250
5251                 if (BASELINK_P (fn))
5252                   {
5253                   postfix_expression
5254                     = (build_new_method_call
5255                        (instance, fn, &args, NULL_TREE,
5256                         (idk == CP_ID_KIND_QUALIFIED
5257                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5258                         /*fn_p=*/NULL,
5259                         tf_warning_or_error));
5260                   }
5261                 else
5262                   postfix_expression
5263                     = finish_call_expr (postfix_expression, &args,
5264                                         /*disallow_virtual=*/false,
5265                                         /*koenig_p=*/false,
5266                                         tf_warning_or_error);
5267               }
5268             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5269                      || TREE_CODE (postfix_expression) == MEMBER_REF
5270                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5271               postfix_expression = (build_offset_ref_call_from_tree
5272                                     (postfix_expression, &args));
5273             else if (idk == CP_ID_KIND_QUALIFIED)
5274               /* A call to a static class member, or a namespace-scope
5275                  function.  */
5276               postfix_expression
5277                 = finish_call_expr (postfix_expression, &args,
5278                                     /*disallow_virtual=*/true,
5279                                     koenig_p,
5280                                     tf_warning_or_error);
5281             else
5282               /* All other function calls.  */
5283               postfix_expression
5284                 = finish_call_expr (postfix_expression, &args,
5285                                     /*disallow_virtual=*/false,
5286                                     koenig_p,
5287                                     tf_warning_or_error);
5288
5289             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5290             idk = CP_ID_KIND_NONE;
5291
5292             release_tree_vector (args);
5293           }
5294           break;
5295
5296         case CPP_DOT:
5297         case CPP_DEREF:
5298           /* postfix-expression . template [opt] id-expression
5299              postfix-expression . pseudo-destructor-name
5300              postfix-expression -> template [opt] id-expression
5301              postfix-expression -> pseudo-destructor-name */
5302
5303           /* Consume the `.' or `->' operator.  */
5304           cp_lexer_consume_token (parser->lexer);
5305
5306           postfix_expression
5307             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5308                                                       postfix_expression,
5309                                                       false, &idk,
5310                                                       token->location);
5311
5312           is_member_access = true;
5313           break;
5314
5315         case CPP_PLUS_PLUS:
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                                      POSTINCREMENT_EXPR);
5323           /* Increments may not appear in constant-expressions.  */
5324           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5325             postfix_expression = error_mark_node;
5326           idk = CP_ID_KIND_NONE;
5327           is_member_access = false;
5328           break;
5329
5330         case CPP_MINUS_MINUS:
5331           /* postfix-expression -- */
5332           /* Consume the `--' token.  */
5333           cp_lexer_consume_token (parser->lexer);
5334           /* Generate a representation for the complete expression.  */
5335           postfix_expression
5336             = finish_increment_expr (postfix_expression,
5337                                      POSTDECREMENT_EXPR);
5338           /* Decrements may not appear in constant-expressions.  */
5339           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5340             postfix_expression = error_mark_node;
5341           idk = CP_ID_KIND_NONE;
5342           is_member_access = false;
5343           break;
5344
5345         default:
5346           if (pidk_return != NULL)
5347             * pidk_return = idk;
5348           if (member_access_only_p)
5349             return is_member_access? postfix_expression : error_mark_node;
5350           else
5351             return postfix_expression;
5352         }
5353     }
5354
5355   /* We should never get here.  */
5356   gcc_unreachable ();
5357   return error_mark_node;
5358 }
5359
5360 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5361    by cp_parser_builtin_offsetof.  We're looking for
5362
5363      postfix-expression [ expression ]
5364
5365    FOR_OFFSETOF is set if we're being called in that context, which
5366    changes how we deal with integer constant expressions.  */
5367
5368 static tree
5369 cp_parser_postfix_open_square_expression (cp_parser *parser,
5370                                           tree postfix_expression,
5371                                           bool for_offsetof)
5372 {
5373   tree index;
5374
5375   /* Consume the `[' token.  */
5376   cp_lexer_consume_token (parser->lexer);
5377
5378   /* Parse the index expression.  */
5379   /* ??? For offsetof, there is a question of what to allow here.  If
5380      offsetof is not being used in an integral constant expression context,
5381      then we *could* get the right answer by computing the value at runtime.
5382      If we are in an integral constant expression context, then we might
5383      could accept any constant expression; hard to say without analysis.
5384      Rather than open the barn door too wide right away, allow only integer
5385      constant expressions here.  */
5386   if (for_offsetof)
5387     index = cp_parser_constant_expression (parser, false, NULL);
5388   else
5389     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5390
5391   /* Look for the closing `]'.  */
5392   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5393
5394   /* Build the ARRAY_REF.  */
5395   postfix_expression = grok_array_decl (postfix_expression, index);
5396
5397   /* When not doing offsetof, array references are not permitted in
5398      constant-expressions.  */
5399   if (!for_offsetof
5400       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5401     postfix_expression = error_mark_node;
5402
5403   return postfix_expression;
5404 }
5405
5406 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5407    by cp_parser_builtin_offsetof.  We're looking for
5408
5409      postfix-expression . template [opt] id-expression
5410      postfix-expression . pseudo-destructor-name
5411      postfix-expression -> template [opt] id-expression
5412      postfix-expression -> pseudo-destructor-name
5413
5414    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5415    limits what of the above we'll actually accept, but nevermind.
5416    TOKEN_TYPE is the "." or "->" token, which will already have been
5417    removed from the stream.  */
5418
5419 static tree
5420 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5421                                         enum cpp_ttype token_type,
5422                                         tree postfix_expression,
5423                                         bool for_offsetof, cp_id_kind *idk,
5424                                         location_t location)
5425 {
5426   tree name;
5427   bool dependent_p;
5428   bool pseudo_destructor_p;
5429   tree scope = NULL_TREE;
5430
5431   /* If this is a `->' operator, dereference the pointer.  */
5432   if (token_type == CPP_DEREF)
5433     postfix_expression = build_x_arrow (postfix_expression);
5434   /* Check to see whether or not the expression is type-dependent.  */
5435   dependent_p = type_dependent_expression_p (postfix_expression);
5436   /* The identifier following the `->' or `.' is not qualified.  */
5437   parser->scope = NULL_TREE;
5438   parser->qualifying_scope = NULL_TREE;
5439   parser->object_scope = NULL_TREE;
5440   *idk = CP_ID_KIND_NONE;
5441
5442   /* Enter the scope corresponding to the type of the object
5443      given by the POSTFIX_EXPRESSION.  */
5444   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5445     {
5446       scope = TREE_TYPE (postfix_expression);
5447       /* According to the standard, no expression should ever have
5448          reference type.  Unfortunately, we do not currently match
5449          the standard in this respect in that our internal representation
5450          of an expression may have reference type even when the standard
5451          says it does not.  Therefore, we have to manually obtain the
5452          underlying type here.  */
5453       scope = non_reference (scope);
5454       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5455       if (scope == unknown_type_node)
5456         {
5457           error_at (location, "%qE does not have class type",
5458                     postfix_expression);
5459           scope = NULL_TREE;
5460         }
5461       else
5462         scope = complete_type_or_else (scope, NULL_TREE);
5463       /* Let the name lookup machinery know that we are processing a
5464          class member access expression.  */
5465       parser->context->object_type = scope;
5466       /* If something went wrong, we want to be able to discern that case,
5467          as opposed to the case where there was no SCOPE due to the type
5468          of expression being dependent.  */
5469       if (!scope)
5470         scope = error_mark_node;
5471       /* If the SCOPE was erroneous, make the various semantic analysis
5472          functions exit quickly -- and without issuing additional error
5473          messages.  */
5474       if (scope == error_mark_node)
5475         postfix_expression = error_mark_node;
5476     }
5477
5478   /* Assume this expression is not a pseudo-destructor access.  */
5479   pseudo_destructor_p = false;
5480
5481   /* If the SCOPE is a scalar type, then, if this is a valid program,
5482      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5483      is type dependent, it can be pseudo-destructor-name or something else.
5484      Try to parse it as pseudo-destructor-name first.  */
5485   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5486     {
5487       tree s;
5488       tree type;
5489
5490       cp_parser_parse_tentatively (parser);
5491       /* Parse the pseudo-destructor-name.  */
5492       s = NULL_TREE;
5493       cp_parser_pseudo_destructor_name (parser, &s, &type);
5494       if (dependent_p
5495           && (cp_parser_error_occurred (parser)
5496               || TREE_CODE (type) != TYPE_DECL
5497               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5498         cp_parser_abort_tentative_parse (parser);
5499       else if (cp_parser_parse_definitely (parser))
5500         {
5501           pseudo_destructor_p = true;
5502           postfix_expression
5503             = finish_pseudo_destructor_expr (postfix_expression,
5504                                              s, TREE_TYPE (type));
5505         }
5506     }
5507
5508   if (!pseudo_destructor_p)
5509     {
5510       /* If the SCOPE is not a scalar type, we are looking at an
5511          ordinary class member access expression, rather than a
5512          pseudo-destructor-name.  */
5513       bool template_p;
5514       cp_token *token = cp_lexer_peek_token (parser->lexer);
5515       /* Parse the id-expression.  */
5516       name = (cp_parser_id_expression
5517               (parser,
5518                cp_parser_optional_template_keyword (parser),
5519                /*check_dependency_p=*/true,
5520                &template_p,
5521                /*declarator_p=*/false,
5522                /*optional_p=*/false));
5523       /* In general, build a SCOPE_REF if the member name is qualified.
5524          However, if the name was not dependent and has already been
5525          resolved; there is no need to build the SCOPE_REF.  For example;
5526
5527              struct X { void f(); };
5528              template <typename T> void f(T* t) { t->X::f(); }
5529
5530          Even though "t" is dependent, "X::f" is not and has been resolved
5531          to a BASELINK; there is no need to include scope information.  */
5532
5533       /* But we do need to remember that there was an explicit scope for
5534          virtual function calls.  */
5535       if (parser->scope)
5536         *idk = CP_ID_KIND_QUALIFIED;
5537
5538       /* If the name is a template-id that names a type, we will get a
5539          TYPE_DECL here.  That is invalid code.  */
5540       if (TREE_CODE (name) == TYPE_DECL)
5541         {
5542           error_at (token->location, "invalid use of %qD", name);
5543           postfix_expression = error_mark_node;
5544         }
5545       else
5546         {
5547           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5548             {
5549               name = build_qualified_name (/*type=*/NULL_TREE,
5550                                            parser->scope,
5551                                            name,
5552                                            template_p);
5553               parser->scope = NULL_TREE;
5554               parser->qualifying_scope = NULL_TREE;
5555               parser->object_scope = NULL_TREE;
5556             }
5557           if (scope && name && BASELINK_P (name))
5558             adjust_result_of_qualified_name_lookup
5559               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5560           postfix_expression
5561             = finish_class_member_access_expr (postfix_expression, name,
5562                                                template_p, 
5563                                                tf_warning_or_error);
5564         }
5565     }
5566
5567   /* We no longer need to look up names in the scope of the object on
5568      the left-hand side of the `.' or `->' operator.  */
5569   parser->context->object_type = NULL_TREE;
5570
5571   /* Outside of offsetof, these operators may not appear in
5572      constant-expressions.  */
5573   if (!for_offsetof
5574       && (cp_parser_non_integral_constant_expression
5575           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5576     postfix_expression = error_mark_node;
5577
5578   return postfix_expression;
5579 }
5580
5581 /* Parse a parenthesized expression-list.
5582
5583    expression-list:
5584      assignment-expression
5585      expression-list, assignment-expression
5586
5587    attribute-list:
5588      expression-list
5589      identifier
5590      identifier, expression-list
5591
5592    CAST_P is true if this expression is the target of a cast.
5593
5594    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5595    argument pack.
5596
5597    Returns a vector of trees.  Each element is a representation of an
5598    assignment-expression.  NULL is returned if the ( and or ) are
5599    missing.  An empty, but allocated, vector is returned on no
5600    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5601    if we are parsing an attribute list for an attribute that wants a
5602    plain identifier argument, normal_attr for an attribute that wants
5603    an expression, or non_attr if we aren't parsing an attribute list.  If
5604    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5605    not all of the expressions in the list were constant.  */
5606
5607 static VEC(tree,gc) *
5608 cp_parser_parenthesized_expression_list (cp_parser* parser,
5609                                          int is_attribute_list,
5610                                          bool cast_p,
5611                                          bool allow_expansion_p,
5612                                          bool *non_constant_p)
5613 {
5614   VEC(tree,gc) *expression_list;
5615   bool fold_expr_p = is_attribute_list != non_attr;
5616   tree identifier = NULL_TREE;
5617   bool saved_greater_than_is_operator_p;
5618
5619   /* Assume all the expressions will be constant.  */
5620   if (non_constant_p)
5621     *non_constant_p = false;
5622
5623   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5624     return NULL;
5625
5626   expression_list = make_tree_vector ();
5627
5628   /* Within a parenthesized expression, a `>' token is always
5629      the greater-than operator.  */
5630   saved_greater_than_is_operator_p
5631     = parser->greater_than_is_operator_p;
5632   parser->greater_than_is_operator_p = true;
5633
5634   /* Consume expressions until there are no more.  */
5635   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5636     while (true)
5637       {
5638         tree expr;
5639
5640         /* At the beginning of attribute lists, check to see if the
5641            next token is an identifier.  */
5642         if (is_attribute_list == id_attr
5643             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5644           {
5645             cp_token *token;
5646
5647             /* Consume the identifier.  */
5648             token = cp_lexer_consume_token (parser->lexer);
5649             /* Save the identifier.  */
5650             identifier = token->u.value;
5651           }
5652         else
5653           {
5654             bool expr_non_constant_p;
5655
5656             /* Parse the next assignment-expression.  */
5657             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5658               {
5659                 /* A braced-init-list.  */
5660                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5661                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5662                 if (non_constant_p && expr_non_constant_p)
5663                   *non_constant_p = true;
5664               }
5665             else if (non_constant_p)
5666               {
5667                 expr = (cp_parser_constant_expression
5668                         (parser, /*allow_non_constant_p=*/true,
5669                          &expr_non_constant_p));
5670                 if (expr_non_constant_p)
5671                   *non_constant_p = true;
5672               }
5673             else
5674               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5675
5676             if (fold_expr_p)
5677               expr = fold_non_dependent_expr (expr);
5678
5679             /* If we have an ellipsis, then this is an expression
5680                expansion.  */
5681             if (allow_expansion_p
5682                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5683               {
5684                 /* Consume the `...'.  */
5685                 cp_lexer_consume_token (parser->lexer);
5686
5687                 /* Build the argument pack.  */
5688                 expr = make_pack_expansion (expr);
5689               }
5690
5691              /* Add it to the list.  We add error_mark_node
5692                 expressions to the list, so that we can still tell if
5693                 the correct form for a parenthesized expression-list
5694                 is found. That gives better errors.  */
5695             VEC_safe_push (tree, gc, expression_list, expr);
5696
5697             if (expr == error_mark_node)
5698               goto skip_comma;
5699           }
5700
5701         /* After the first item, attribute lists look the same as
5702            expression lists.  */
5703         is_attribute_list = non_attr;
5704
5705       get_comma:;
5706         /* If the next token isn't a `,', then we are done.  */
5707         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5708           break;
5709
5710         /* Otherwise, consume the `,' and keep going.  */
5711         cp_lexer_consume_token (parser->lexer);
5712       }
5713
5714   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5715     {
5716       int ending;
5717
5718     skip_comma:;
5719       /* We try and resync to an unnested comma, as that will give the
5720          user better diagnostics.  */
5721       ending = cp_parser_skip_to_closing_parenthesis (parser,
5722                                                       /*recovering=*/true,
5723                                                       /*or_comma=*/true,
5724                                                       /*consume_paren=*/true);
5725       if (ending < 0)
5726         goto get_comma;
5727       if (!ending)
5728         {
5729           parser->greater_than_is_operator_p
5730             = saved_greater_than_is_operator_p;
5731           return NULL;
5732         }
5733     }
5734
5735   parser->greater_than_is_operator_p
5736     = saved_greater_than_is_operator_p;
5737
5738   if (identifier)
5739     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5740
5741   return expression_list;
5742 }
5743
5744 /* Parse a pseudo-destructor-name.
5745
5746    pseudo-destructor-name:
5747      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5748      :: [opt] nested-name-specifier template template-id :: ~ type-name
5749      :: [opt] nested-name-specifier [opt] ~ type-name
5750
5751    If either of the first two productions is used, sets *SCOPE to the
5752    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5753    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5754    or ERROR_MARK_NODE if the parse fails.  */
5755
5756 static void
5757 cp_parser_pseudo_destructor_name (cp_parser* parser,
5758                                   tree* scope,
5759                                   tree* type)
5760 {
5761   bool nested_name_specifier_p;
5762
5763   /* Assume that things will not work out.  */
5764   *type = error_mark_node;
5765
5766   /* Look for the optional `::' operator.  */
5767   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5768   /* Look for the optional nested-name-specifier.  */
5769   nested_name_specifier_p
5770     = (cp_parser_nested_name_specifier_opt (parser,
5771                                             /*typename_keyword_p=*/false,
5772                                             /*check_dependency_p=*/true,
5773                                             /*type_p=*/false,
5774                                             /*is_declaration=*/false)
5775        != NULL_TREE);
5776   /* Now, if we saw a nested-name-specifier, we might be doing the
5777      second production.  */
5778   if (nested_name_specifier_p
5779       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5780     {
5781       /* Consume the `template' keyword.  */
5782       cp_lexer_consume_token (parser->lexer);
5783       /* Parse the template-id.  */
5784       cp_parser_template_id (parser,
5785                              /*template_keyword_p=*/true,
5786                              /*check_dependency_p=*/false,
5787                              /*is_declaration=*/true);
5788       /* Look for the `::' token.  */
5789       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5790     }
5791   /* If the next token is not a `~', then there might be some
5792      additional qualification.  */
5793   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5794     {
5795       /* At this point, we're looking for "type-name :: ~".  The type-name
5796          must not be a class-name, since this is a pseudo-destructor.  So,
5797          it must be either an enum-name, or a typedef-name -- both of which
5798          are just identifiers.  So, we peek ahead to check that the "::"
5799          and "~" tokens are present; if they are not, then we can avoid
5800          calling type_name.  */
5801       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5802           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5803           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5804         {
5805           cp_parser_error (parser, "non-scalar type");
5806           return;
5807         }
5808
5809       /* Look for the type-name.  */
5810       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5811       if (*scope == error_mark_node)
5812         return;
5813
5814       /* Look for the `::' token.  */
5815       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5816     }
5817   else
5818     *scope = NULL_TREE;
5819
5820   /* Look for the `~'.  */
5821   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5822   /* Look for the type-name again.  We are not responsible for
5823      checking that it matches the first type-name.  */
5824   *type = cp_parser_nonclass_name (parser);
5825 }
5826
5827 /* Parse a unary-expression.
5828
5829    unary-expression:
5830      postfix-expression
5831      ++ cast-expression
5832      -- cast-expression
5833      unary-operator cast-expression
5834      sizeof unary-expression
5835      sizeof ( type-id )
5836      new-expression
5837      delete-expression
5838
5839    GNU Extensions:
5840
5841    unary-expression:
5842      __extension__ cast-expression
5843      __alignof__ unary-expression
5844      __alignof__ ( type-id )
5845      __real__ cast-expression
5846      __imag__ cast-expression
5847      && identifier
5848
5849    ADDRESS_P is true iff the unary-expression is appearing as the
5850    operand of the `&' operator.   CAST_P is true if this expression is
5851    the target of a cast.
5852
5853    Returns a representation of the expression.  */
5854
5855 static tree
5856 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5857                             cp_id_kind * pidk)
5858 {
5859   cp_token *token;
5860   enum tree_code unary_operator;
5861
5862   /* Peek at the next token.  */
5863   token = cp_lexer_peek_token (parser->lexer);
5864   /* Some keywords give away the kind of expression.  */
5865   if (token->type == CPP_KEYWORD)
5866     {
5867       enum rid keyword = token->keyword;
5868
5869       switch (keyword)
5870         {
5871         case RID_ALIGNOF:
5872         case RID_SIZEOF:
5873           {
5874             tree operand;
5875             enum tree_code op;
5876
5877             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5878             /* Consume the token.  */
5879             cp_lexer_consume_token (parser->lexer);
5880             /* Parse the operand.  */
5881             operand = cp_parser_sizeof_operand (parser, keyword);
5882
5883             if (TYPE_P (operand))
5884               return cxx_sizeof_or_alignof_type (operand, op, true);
5885             else
5886               return cxx_sizeof_or_alignof_expr (operand, op, true);
5887           }
5888
5889         case RID_NEW:
5890           return cp_parser_new_expression (parser);
5891
5892         case RID_DELETE:
5893           return cp_parser_delete_expression (parser);
5894
5895         case RID_EXTENSION:
5896           {
5897             /* The saved value of the PEDANTIC flag.  */
5898             int saved_pedantic;
5899             tree expr;
5900
5901             /* Save away the PEDANTIC flag.  */
5902             cp_parser_extension_opt (parser, &saved_pedantic);
5903             /* Parse the cast-expression.  */
5904             expr = cp_parser_simple_cast_expression (parser);
5905             /* Restore the PEDANTIC flag.  */
5906             pedantic = saved_pedantic;
5907
5908             return expr;
5909           }
5910
5911         case RID_REALPART:
5912         case RID_IMAGPART:
5913           {
5914             tree expression;
5915
5916             /* Consume the `__real__' or `__imag__' token.  */
5917             cp_lexer_consume_token (parser->lexer);
5918             /* Parse the cast-expression.  */
5919             expression = cp_parser_simple_cast_expression (parser);
5920             /* Create the complete representation.  */
5921             return build_x_unary_op ((keyword == RID_REALPART
5922                                       ? REALPART_EXPR : IMAGPART_EXPR),
5923                                      expression,
5924                                      tf_warning_or_error);
5925           }
5926           break;
5927
5928         case RID_NOEXCEPT:
5929           {
5930             tree expr;
5931             const char *saved_message;
5932             bool saved_integral_constant_expression_p;
5933             bool saved_non_integral_constant_expression_p;
5934             bool saved_greater_than_is_operator_p;
5935
5936             cp_lexer_consume_token (parser->lexer);
5937             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5938
5939             saved_message = parser->type_definition_forbidden_message;
5940             parser->type_definition_forbidden_message
5941               = G_("types may not be defined in %<noexcept%> expressions");
5942
5943             saved_integral_constant_expression_p
5944               = parser->integral_constant_expression_p;
5945             saved_non_integral_constant_expression_p
5946               = parser->non_integral_constant_expression_p;
5947             parser->integral_constant_expression_p = false;
5948
5949             saved_greater_than_is_operator_p
5950               = parser->greater_than_is_operator_p;
5951             parser->greater_than_is_operator_p = true;
5952
5953             ++cp_unevaluated_operand;
5954             ++c_inhibit_evaluation_warnings;
5955             expr = cp_parser_expression (parser, false, NULL);
5956             --c_inhibit_evaluation_warnings;
5957             --cp_unevaluated_operand;
5958
5959             parser->greater_than_is_operator_p
5960               = saved_greater_than_is_operator_p;
5961
5962             parser->integral_constant_expression_p
5963               = saved_integral_constant_expression_p;
5964             parser->non_integral_constant_expression_p
5965               = saved_non_integral_constant_expression_p;
5966
5967             parser->type_definition_forbidden_message = saved_message;
5968
5969             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5970             return finish_noexcept_expr (expr, tf_warning_or_error);
5971           }
5972
5973         default:
5974           break;
5975         }
5976     }
5977
5978   /* Look for the `:: new' and `:: delete', which also signal the
5979      beginning of a new-expression, or delete-expression,
5980      respectively.  If the next token is `::', then it might be one of
5981      these.  */
5982   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5983     {
5984       enum rid keyword;
5985
5986       /* See if the token after the `::' is one of the keywords in
5987          which we're interested.  */
5988       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5989       /* If it's `new', we have a new-expression.  */
5990       if (keyword == RID_NEW)
5991         return cp_parser_new_expression (parser);
5992       /* Similarly, for `delete'.  */
5993       else if (keyword == RID_DELETE)
5994         return cp_parser_delete_expression (parser);
5995     }
5996
5997   /* Look for a unary operator.  */
5998   unary_operator = cp_parser_unary_operator (token);
5999   /* The `++' and `--' operators can be handled similarly, even though
6000      they are not technically unary-operators in the grammar.  */
6001   if (unary_operator == ERROR_MARK)
6002     {
6003       if (token->type == CPP_PLUS_PLUS)
6004         unary_operator = PREINCREMENT_EXPR;
6005       else if (token->type == CPP_MINUS_MINUS)
6006         unary_operator = PREDECREMENT_EXPR;
6007       /* Handle the GNU address-of-label extension.  */
6008       else if (cp_parser_allow_gnu_extensions_p (parser)
6009                && token->type == CPP_AND_AND)
6010         {
6011           tree identifier;
6012           tree expression;
6013           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6014
6015           /* Consume the '&&' token.  */
6016           cp_lexer_consume_token (parser->lexer);
6017           /* Look for the identifier.  */
6018           identifier = cp_parser_identifier (parser);
6019           /* Create an expression representing the address.  */
6020           expression = finish_label_address_expr (identifier, loc);
6021           if (cp_parser_non_integral_constant_expression (parser,
6022                                                           NIC_ADDR_LABEL))
6023             expression = error_mark_node;
6024           return expression;
6025         }
6026     }
6027   if (unary_operator != ERROR_MARK)
6028     {
6029       tree cast_expression;
6030       tree expression = error_mark_node;
6031       non_integral_constant non_constant_p = NIC_NONE;
6032
6033       /* Consume the operator token.  */
6034       token = cp_lexer_consume_token (parser->lexer);
6035       /* Parse the cast-expression.  */
6036       cast_expression
6037         = cp_parser_cast_expression (parser,
6038                                      unary_operator == ADDR_EXPR,
6039                                      /*cast_p=*/false, pidk);
6040       /* Now, build an appropriate representation.  */
6041       switch (unary_operator)
6042         {
6043         case INDIRECT_REF:
6044           non_constant_p = NIC_STAR;
6045           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6046                                              tf_warning_or_error);
6047           break;
6048
6049         case ADDR_EXPR:
6050            non_constant_p = NIC_ADDR;
6051           /* Fall through.  */
6052         case BIT_NOT_EXPR:
6053           expression = build_x_unary_op (unary_operator, cast_expression,
6054                                          tf_warning_or_error);
6055           break;
6056
6057         case PREINCREMENT_EXPR:
6058         case PREDECREMENT_EXPR:
6059           non_constant_p = unary_operator == PREINCREMENT_EXPR
6060                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6061           /* Fall through.  */
6062         case UNARY_PLUS_EXPR:
6063         case NEGATE_EXPR:
6064         case TRUTH_NOT_EXPR:
6065           expression = finish_unary_op_expr (unary_operator, cast_expression);
6066           break;
6067
6068         default:
6069           gcc_unreachable ();
6070         }
6071
6072       if (non_constant_p != NIC_NONE
6073           && cp_parser_non_integral_constant_expression (parser,
6074                                                          non_constant_p))
6075         expression = error_mark_node;
6076
6077       return expression;
6078     }
6079
6080   return cp_parser_postfix_expression (parser, address_p, cast_p,
6081                                        /*member_access_only_p=*/false,
6082                                        pidk);
6083 }
6084
6085 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6086    unary-operator, the corresponding tree code is returned.  */
6087
6088 static enum tree_code
6089 cp_parser_unary_operator (cp_token* token)
6090 {
6091   switch (token->type)
6092     {
6093     case CPP_MULT:
6094       return INDIRECT_REF;
6095
6096     case CPP_AND:
6097       return ADDR_EXPR;
6098
6099     case CPP_PLUS:
6100       return UNARY_PLUS_EXPR;
6101
6102     case CPP_MINUS:
6103       return NEGATE_EXPR;
6104
6105     case CPP_NOT:
6106       return TRUTH_NOT_EXPR;
6107
6108     case CPP_COMPL:
6109       return BIT_NOT_EXPR;
6110
6111     default:
6112       return ERROR_MARK;
6113     }
6114 }
6115
6116 /* Parse a new-expression.
6117
6118    new-expression:
6119      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6120      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6121
6122    Returns a representation of the expression.  */
6123
6124 static tree
6125 cp_parser_new_expression (cp_parser* parser)
6126 {
6127   bool global_scope_p;
6128   VEC(tree,gc) *placement;
6129   tree type;
6130   VEC(tree,gc) *initializer;
6131   tree nelts;
6132   tree ret;
6133
6134   /* Look for the optional `::' operator.  */
6135   global_scope_p
6136     = (cp_parser_global_scope_opt (parser,
6137                                    /*current_scope_valid_p=*/false)
6138        != NULL_TREE);
6139   /* Look for the `new' operator.  */
6140   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6141   /* There's no easy way to tell a new-placement from the
6142      `( type-id )' construct.  */
6143   cp_parser_parse_tentatively (parser);
6144   /* Look for a new-placement.  */
6145   placement = cp_parser_new_placement (parser);
6146   /* If that didn't work out, there's no new-placement.  */
6147   if (!cp_parser_parse_definitely (parser))
6148     {
6149       if (placement != NULL)
6150         release_tree_vector (placement);
6151       placement = NULL;
6152     }
6153
6154   /* If the next token is a `(', then we have a parenthesized
6155      type-id.  */
6156   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6157     {
6158       cp_token *token;
6159       /* Consume the `('.  */
6160       cp_lexer_consume_token (parser->lexer);
6161       /* Parse the type-id.  */
6162       type = cp_parser_type_id (parser);
6163       /* Look for the closing `)'.  */
6164       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6165       token = cp_lexer_peek_token (parser->lexer);
6166       /* There should not be a direct-new-declarator in this production,
6167          but GCC used to allowed this, so we check and emit a sensible error
6168          message for this case.  */
6169       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6170         {
6171           error_at (token->location,
6172                     "array bound forbidden after parenthesized type-id");
6173           inform (token->location, 
6174                   "try removing the parentheses around the type-id");
6175           cp_parser_direct_new_declarator (parser);
6176         }
6177       nelts = NULL_TREE;
6178     }
6179   /* Otherwise, there must be a new-type-id.  */
6180   else
6181     type = cp_parser_new_type_id (parser, &nelts);
6182
6183   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6184   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6185       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6186     initializer = cp_parser_new_initializer (parser);
6187   else
6188     initializer = NULL;
6189
6190   /* A new-expression may not appear in an integral constant
6191      expression.  */
6192   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6193     ret = error_mark_node;
6194   else
6195     {
6196       /* Create a representation of the new-expression.  */
6197       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6198                        tf_warning_or_error);
6199     }
6200
6201   if (placement != NULL)
6202     release_tree_vector (placement);
6203   if (initializer != NULL)
6204     release_tree_vector (initializer);
6205
6206   return ret;
6207 }
6208
6209 /* Parse a new-placement.
6210
6211    new-placement:
6212      ( expression-list )
6213
6214    Returns the same representation as for an expression-list.  */
6215
6216 static VEC(tree,gc) *
6217 cp_parser_new_placement (cp_parser* parser)
6218 {
6219   VEC(tree,gc) *expression_list;
6220
6221   /* Parse the expression-list.  */
6222   expression_list = (cp_parser_parenthesized_expression_list
6223                      (parser, non_attr, /*cast_p=*/false,
6224                       /*allow_expansion_p=*/true,
6225                       /*non_constant_p=*/NULL));
6226
6227   return expression_list;
6228 }
6229
6230 /* Parse a new-type-id.
6231
6232    new-type-id:
6233      type-specifier-seq new-declarator [opt]
6234
6235    Returns the TYPE allocated.  If the new-type-id indicates an array
6236    type, *NELTS is set to the number of elements in the last array
6237    bound; the TYPE will not include the last array bound.  */
6238
6239 static tree
6240 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6241 {
6242   cp_decl_specifier_seq type_specifier_seq;
6243   cp_declarator *new_declarator;
6244   cp_declarator *declarator;
6245   cp_declarator *outer_declarator;
6246   const char *saved_message;
6247   tree type;
6248
6249   /* The type-specifier sequence must not contain type definitions.
6250      (It cannot contain declarations of new types either, but if they
6251      are not definitions we will catch that because they are not
6252      complete.)  */
6253   saved_message = parser->type_definition_forbidden_message;
6254   parser->type_definition_forbidden_message
6255     = G_("types may not be defined in a new-type-id");
6256   /* Parse the type-specifier-seq.  */
6257   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6258                                 /*is_trailing_return=*/false,
6259                                 &type_specifier_seq);
6260   /* Restore the old message.  */
6261   parser->type_definition_forbidden_message = saved_message;
6262   /* Parse the new-declarator.  */
6263   new_declarator = cp_parser_new_declarator_opt (parser);
6264
6265   /* Determine the number of elements in the last array dimension, if
6266      any.  */
6267   *nelts = NULL_TREE;
6268   /* Skip down to the last array dimension.  */
6269   declarator = new_declarator;
6270   outer_declarator = NULL;
6271   while (declarator && (declarator->kind == cdk_pointer
6272                         || declarator->kind == cdk_ptrmem))
6273     {
6274       outer_declarator = declarator;
6275       declarator = declarator->declarator;
6276     }
6277   while (declarator
6278          && declarator->kind == cdk_array
6279          && declarator->declarator
6280          && declarator->declarator->kind == cdk_array)
6281     {
6282       outer_declarator = declarator;
6283       declarator = declarator->declarator;
6284     }
6285
6286   if (declarator && declarator->kind == cdk_array)
6287     {
6288       *nelts = declarator->u.array.bounds;
6289       if (*nelts == error_mark_node)
6290         *nelts = integer_one_node;
6291
6292       if (outer_declarator)
6293         outer_declarator->declarator = declarator->declarator;
6294       else
6295         new_declarator = NULL;
6296     }
6297
6298   type = groktypename (&type_specifier_seq, new_declarator, false);
6299   return type;
6300 }
6301
6302 /* Parse an (optional) new-declarator.
6303
6304    new-declarator:
6305      ptr-operator new-declarator [opt]
6306      direct-new-declarator
6307
6308    Returns the declarator.  */
6309
6310 static cp_declarator *
6311 cp_parser_new_declarator_opt (cp_parser* parser)
6312 {
6313   enum tree_code code;
6314   tree type;
6315   cp_cv_quals cv_quals;
6316
6317   /* We don't know if there's a ptr-operator next, or not.  */
6318   cp_parser_parse_tentatively (parser);
6319   /* Look for a ptr-operator.  */
6320   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6321   /* If that worked, look for more new-declarators.  */
6322   if (cp_parser_parse_definitely (parser))
6323     {
6324       cp_declarator *declarator;
6325
6326       /* Parse another optional declarator.  */
6327       declarator = cp_parser_new_declarator_opt (parser);
6328
6329       return cp_parser_make_indirect_declarator
6330         (code, type, cv_quals, declarator);
6331     }
6332
6333   /* If the next token is a `[', there is a direct-new-declarator.  */
6334   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6335     return cp_parser_direct_new_declarator (parser);
6336
6337   return NULL;
6338 }
6339
6340 /* Parse a direct-new-declarator.
6341
6342    direct-new-declarator:
6343      [ expression ]
6344      direct-new-declarator [constant-expression]
6345
6346    */
6347
6348 static cp_declarator *
6349 cp_parser_direct_new_declarator (cp_parser* parser)
6350 {
6351   cp_declarator *declarator = NULL;
6352
6353   while (true)
6354     {
6355       tree expression;
6356
6357       /* Look for the opening `['.  */
6358       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6359       /* The first expression is not required to be constant.  */
6360       if (!declarator)
6361         {
6362           cp_token *token = cp_lexer_peek_token (parser->lexer);
6363           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6364           /* The standard requires that the expression have integral
6365              type.  DR 74 adds enumeration types.  We believe that the
6366              real intent is that these expressions be handled like the
6367              expression in a `switch' condition, which also allows
6368              classes with a single conversion to integral or
6369              enumeration type.  */
6370           if (!processing_template_decl)
6371             {
6372               expression
6373                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6374                                               expression,
6375                                               /*complain=*/true);
6376               if (!expression)
6377                 {
6378                   error_at (token->location,
6379                             "expression in new-declarator must have integral "
6380                             "or enumeration type");
6381                   expression = error_mark_node;
6382                 }
6383             }
6384         }
6385       /* But all the other expressions must be.  */
6386       else
6387         expression
6388           = cp_parser_constant_expression (parser,
6389                                            /*allow_non_constant=*/false,
6390                                            NULL);
6391       /* Look for the closing `]'.  */
6392       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6393
6394       /* Add this bound to the declarator.  */
6395       declarator = make_array_declarator (declarator, expression);
6396
6397       /* If the next token is not a `[', then there are no more
6398          bounds.  */
6399       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6400         break;
6401     }
6402
6403   return declarator;
6404 }
6405
6406 /* Parse a new-initializer.
6407
6408    new-initializer:
6409      ( expression-list [opt] )
6410      braced-init-list
6411
6412    Returns a representation of the expression-list.  */
6413
6414 static VEC(tree,gc) *
6415 cp_parser_new_initializer (cp_parser* parser)
6416 {
6417   VEC(tree,gc) *expression_list;
6418
6419   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6420     {
6421       tree t;
6422       bool expr_non_constant_p;
6423       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6424       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6425       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6426       expression_list = make_tree_vector_single (t);
6427     }
6428   else
6429     expression_list = (cp_parser_parenthesized_expression_list
6430                        (parser, non_attr, /*cast_p=*/false,
6431                         /*allow_expansion_p=*/true,
6432                         /*non_constant_p=*/NULL));
6433
6434   return expression_list;
6435 }
6436
6437 /* Parse a delete-expression.
6438
6439    delete-expression:
6440      :: [opt] delete cast-expression
6441      :: [opt] delete [ ] cast-expression
6442
6443    Returns a representation of the expression.  */
6444
6445 static tree
6446 cp_parser_delete_expression (cp_parser* parser)
6447 {
6448   bool global_scope_p;
6449   bool array_p;
6450   tree expression;
6451
6452   /* Look for the optional `::' operator.  */
6453   global_scope_p
6454     = (cp_parser_global_scope_opt (parser,
6455                                    /*current_scope_valid_p=*/false)
6456        != NULL_TREE);
6457   /* Look for the `delete' keyword.  */
6458   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6459   /* See if the array syntax is in use.  */
6460   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6461     {
6462       /* Consume the `[' token.  */
6463       cp_lexer_consume_token (parser->lexer);
6464       /* Look for the `]' token.  */
6465       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6466       /* Remember that this is the `[]' construct.  */
6467       array_p = true;
6468     }
6469   else
6470     array_p = false;
6471
6472   /* Parse the cast-expression.  */
6473   expression = cp_parser_simple_cast_expression (parser);
6474
6475   /* A delete-expression may not appear in an integral constant
6476      expression.  */
6477   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6478     return error_mark_node;
6479
6480   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6481 }
6482
6483 /* Returns true if TOKEN may start a cast-expression and false
6484    otherwise.  */
6485
6486 static bool
6487 cp_parser_token_starts_cast_expression (cp_token *token)
6488 {
6489   switch (token->type)
6490     {
6491     case CPP_COMMA:
6492     case CPP_SEMICOLON:
6493     case CPP_QUERY:
6494     case CPP_COLON:
6495     case CPP_CLOSE_SQUARE:
6496     case CPP_CLOSE_PAREN:
6497     case CPP_CLOSE_BRACE:
6498     case CPP_DOT:
6499     case CPP_DOT_STAR:
6500     case CPP_DEREF:
6501     case CPP_DEREF_STAR:
6502     case CPP_DIV:
6503     case CPP_MOD:
6504     case CPP_LSHIFT:
6505     case CPP_RSHIFT:
6506     case CPP_LESS:
6507     case CPP_GREATER:
6508     case CPP_LESS_EQ:
6509     case CPP_GREATER_EQ:
6510     case CPP_EQ_EQ:
6511     case CPP_NOT_EQ:
6512     case CPP_EQ:
6513     case CPP_MULT_EQ:
6514     case CPP_DIV_EQ:
6515     case CPP_MOD_EQ:
6516     case CPP_PLUS_EQ:
6517     case CPP_MINUS_EQ:
6518     case CPP_RSHIFT_EQ:
6519     case CPP_LSHIFT_EQ:
6520     case CPP_AND_EQ:
6521     case CPP_XOR_EQ:
6522     case CPP_OR_EQ:
6523     case CPP_XOR:
6524     case CPP_OR:
6525     case CPP_OR_OR:
6526     case CPP_EOF:
6527       return false;
6528
6529       /* '[' may start a primary-expression in obj-c++.  */
6530     case CPP_OPEN_SQUARE:
6531       return c_dialect_objc ();
6532
6533     default:
6534       return true;
6535     }
6536 }
6537
6538 /* Parse a cast-expression.
6539
6540    cast-expression:
6541      unary-expression
6542      ( type-id ) cast-expression
6543
6544    ADDRESS_P is true iff the unary-expression is appearing as the
6545    operand of the `&' operator.   CAST_P is true if this expression is
6546    the target of a cast.
6547
6548    Returns a representation of the expression.  */
6549
6550 static tree
6551 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6552                            cp_id_kind * pidk)
6553 {
6554   /* If it's a `(', then we might be looking at a cast.  */
6555   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6556     {
6557       tree type = NULL_TREE;
6558       tree expr = NULL_TREE;
6559       bool compound_literal_p;
6560       const char *saved_message;
6561
6562       /* There's no way to know yet whether or not this is a cast.
6563          For example, `(int (3))' is a unary-expression, while `(int)
6564          3' is a cast.  So, we resort to parsing tentatively.  */
6565       cp_parser_parse_tentatively (parser);
6566       /* Types may not be defined in a cast.  */
6567       saved_message = parser->type_definition_forbidden_message;
6568       parser->type_definition_forbidden_message
6569         = G_("types may not be defined in casts");
6570       /* Consume the `('.  */
6571       cp_lexer_consume_token (parser->lexer);
6572       /* A very tricky bit is that `(struct S) { 3 }' is a
6573          compound-literal (which we permit in C++ as an extension).
6574          But, that construct is not a cast-expression -- it is a
6575          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6576          is legal; if the compound-literal were a cast-expression,
6577          you'd need an extra set of parentheses.)  But, if we parse
6578          the type-id, and it happens to be a class-specifier, then we
6579          will commit to the parse at that point, because we cannot
6580          undo the action that is done when creating a new class.  So,
6581          then we cannot back up and do a postfix-expression.
6582
6583          Therefore, we scan ahead to the closing `)', and check to see
6584          if the token after the `)' is a `{'.  If so, we are not
6585          looking at a cast-expression.
6586
6587          Save tokens so that we can put them back.  */
6588       cp_lexer_save_tokens (parser->lexer);
6589       /* Skip tokens until the next token is a closing parenthesis.
6590          If we find the closing `)', and the next token is a `{', then
6591          we are looking at a compound-literal.  */
6592       compound_literal_p
6593         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6594                                                   /*consume_paren=*/true)
6595            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6596       /* Roll back the tokens we skipped.  */
6597       cp_lexer_rollback_tokens (parser->lexer);
6598       /* If we were looking at a compound-literal, simulate an error
6599          so that the call to cp_parser_parse_definitely below will
6600          fail.  */
6601       if (compound_literal_p)
6602         cp_parser_simulate_error (parser);
6603       else
6604         {
6605           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6606           parser->in_type_id_in_expr_p = true;
6607           /* Look for the type-id.  */
6608           type = cp_parser_type_id (parser);
6609           /* Look for the closing `)'.  */
6610           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6611           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6612         }
6613
6614       /* Restore the saved message.  */
6615       parser->type_definition_forbidden_message = saved_message;
6616
6617       /* At this point this can only be either a cast or a
6618          parenthesized ctor such as `(T ())' that looks like a cast to
6619          function returning T.  */
6620       if (!cp_parser_error_occurred (parser)
6621           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6622                                                      (parser->lexer)))
6623         {
6624           cp_parser_parse_definitely (parser);
6625           expr = cp_parser_cast_expression (parser,
6626                                             /*address_p=*/false,
6627                                             /*cast_p=*/true, pidk);
6628
6629           /* Warn about old-style casts, if so requested.  */
6630           if (warn_old_style_cast
6631               && !in_system_header
6632               && !VOID_TYPE_P (type)
6633               && current_lang_name != lang_name_c)
6634             warning (OPT_Wold_style_cast, "use of old-style cast");
6635
6636           /* Only type conversions to integral or enumeration types
6637              can be used in constant-expressions.  */
6638           if (!cast_valid_in_integral_constant_expression_p (type)
6639               && cp_parser_non_integral_constant_expression (parser,
6640                                                              NIC_CAST))
6641             return error_mark_node;
6642
6643           /* Perform the cast.  */
6644           expr = build_c_cast (input_location, type, expr);
6645           return expr;
6646         }
6647       else 
6648         cp_parser_abort_tentative_parse (parser);
6649     }
6650
6651   /* If we get here, then it's not a cast, so it must be a
6652      unary-expression.  */
6653   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6654 }
6655
6656 /* Parse a binary expression of the general form:
6657
6658    pm-expression:
6659      cast-expression
6660      pm-expression .* cast-expression
6661      pm-expression ->* cast-expression
6662
6663    multiplicative-expression:
6664      pm-expression
6665      multiplicative-expression * pm-expression
6666      multiplicative-expression / pm-expression
6667      multiplicative-expression % pm-expression
6668
6669    additive-expression:
6670      multiplicative-expression
6671      additive-expression + multiplicative-expression
6672      additive-expression - multiplicative-expression
6673
6674    shift-expression:
6675      additive-expression
6676      shift-expression << additive-expression
6677      shift-expression >> additive-expression
6678
6679    relational-expression:
6680      shift-expression
6681      relational-expression < shift-expression
6682      relational-expression > shift-expression
6683      relational-expression <= shift-expression
6684      relational-expression >= shift-expression
6685
6686   GNU Extension:
6687
6688    relational-expression:
6689      relational-expression <? shift-expression
6690      relational-expression >? shift-expression
6691
6692    equality-expression:
6693      relational-expression
6694      equality-expression == relational-expression
6695      equality-expression != relational-expression
6696
6697    and-expression:
6698      equality-expression
6699      and-expression & equality-expression
6700
6701    exclusive-or-expression:
6702      and-expression
6703      exclusive-or-expression ^ and-expression
6704
6705    inclusive-or-expression:
6706      exclusive-or-expression
6707      inclusive-or-expression | exclusive-or-expression
6708
6709    logical-and-expression:
6710      inclusive-or-expression
6711      logical-and-expression && inclusive-or-expression
6712
6713    logical-or-expression:
6714      logical-and-expression
6715      logical-or-expression || logical-and-expression
6716
6717    All these are implemented with a single function like:
6718
6719    binary-expression:
6720      simple-cast-expression
6721      binary-expression <token> binary-expression
6722
6723    CAST_P is true if this expression is the target of a cast.
6724
6725    The binops_by_token map is used to get the tree codes for each <token> type.
6726    binary-expressions are associated according to a precedence table.  */
6727
6728 #define TOKEN_PRECEDENCE(token)                              \
6729 (((token->type == CPP_GREATER                                \
6730    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6731   && !parser->greater_than_is_operator_p)                    \
6732  ? PREC_NOT_OPERATOR                                         \
6733  : binops_by_token[token->type].prec)
6734
6735 static tree
6736 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6737                              bool no_toplevel_fold_p,
6738                              enum cp_parser_prec prec,
6739                              cp_id_kind * pidk)
6740 {
6741   cp_parser_expression_stack stack;
6742   cp_parser_expression_stack_entry *sp = &stack[0];
6743   tree lhs, rhs;
6744   cp_token *token;
6745   enum tree_code tree_type, lhs_type, rhs_type;
6746   enum cp_parser_prec new_prec, lookahead_prec;
6747   bool overloaded_p;
6748
6749   /* Parse the first expression.  */
6750   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6751   lhs_type = ERROR_MARK;
6752
6753   for (;;)
6754     {
6755       /* Get an operator token.  */
6756       token = cp_lexer_peek_token (parser->lexer);
6757
6758       if (warn_cxx0x_compat
6759           && token->type == CPP_RSHIFT
6760           && !parser->greater_than_is_operator_p)
6761         {
6762           if (warning_at (token->location, OPT_Wc__0x_compat, 
6763                           "%<>>%> operator will be treated as"
6764                           " two right angle brackets in C++0x"))
6765             inform (token->location,
6766                     "suggest parentheses around %<>>%> expression");
6767         }
6768
6769       new_prec = TOKEN_PRECEDENCE (token);
6770
6771       /* Popping an entry off the stack means we completed a subexpression:
6772          - either we found a token which is not an operator (`>' where it is not
6773            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6774            will happen repeatedly;
6775          - or, we found an operator which has lower priority.  This is the case
6776            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6777            parsing `3 * 4'.  */
6778       if (new_prec <= prec)
6779         {
6780           if (sp == stack)
6781             break;
6782           else
6783             goto pop;
6784         }
6785
6786      get_rhs:
6787       tree_type = binops_by_token[token->type].tree_type;
6788
6789       /* We used the operator token.  */
6790       cp_lexer_consume_token (parser->lexer);
6791
6792       /* For "false && x" or "true || x", x will never be executed;
6793          disable warnings while evaluating it.  */
6794       if (tree_type == TRUTH_ANDIF_EXPR)
6795         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6796       else if (tree_type == TRUTH_ORIF_EXPR)
6797         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6798
6799       /* Extract another operand.  It may be the RHS of this expression
6800          or the LHS of a new, higher priority expression.  */
6801       rhs = cp_parser_simple_cast_expression (parser);
6802       rhs_type = ERROR_MARK;
6803
6804       /* Get another operator token.  Look up its precedence to avoid
6805          building a useless (immediately popped) stack entry for common
6806          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6807       token = cp_lexer_peek_token (parser->lexer);
6808       lookahead_prec = TOKEN_PRECEDENCE (token);
6809       if (lookahead_prec > new_prec)
6810         {
6811           /* ... and prepare to parse the RHS of the new, higher priority
6812              expression.  Since precedence levels on the stack are
6813              monotonically increasing, we do not have to care about
6814              stack overflows.  */
6815           sp->prec = prec;
6816           sp->tree_type = tree_type;
6817           sp->lhs = lhs;
6818           sp->lhs_type = lhs_type;
6819           sp++;
6820           lhs = rhs;
6821           lhs_type = rhs_type;
6822           prec = new_prec;
6823           new_prec = lookahead_prec;
6824           goto get_rhs;
6825
6826          pop:
6827           lookahead_prec = new_prec;
6828           /* If the stack is not empty, we have parsed into LHS the right side
6829              (`4' in the example above) of an expression we had suspended.
6830              We can use the information on the stack to recover the LHS (`3')
6831              from the stack together with the tree code (`MULT_EXPR'), and
6832              the precedence of the higher level subexpression
6833              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6834              which will be used to actually build the additive expression.  */
6835           --sp;
6836           prec = sp->prec;
6837           tree_type = sp->tree_type;
6838           rhs = lhs;
6839           rhs_type = lhs_type;
6840           lhs = sp->lhs;
6841           lhs_type = sp->lhs_type;
6842         }
6843
6844       /* Undo the disabling of warnings done above.  */
6845       if (tree_type == TRUTH_ANDIF_EXPR)
6846         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6847       else if (tree_type == TRUTH_ORIF_EXPR)
6848         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6849
6850       overloaded_p = false;
6851       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6852          ERROR_MARK for everything that is not a binary expression.
6853          This makes warn_about_parentheses miss some warnings that
6854          involve unary operators.  For unary expressions we should
6855          pass the correct tree_code unless the unary expression was
6856          surrounded by parentheses.
6857       */
6858       if (no_toplevel_fold_p
6859           && lookahead_prec <= prec
6860           && sp == stack
6861           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6862         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6863       else
6864         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6865                                  &overloaded_p, tf_warning_or_error);
6866       lhs_type = tree_type;
6867
6868       /* If the binary operator required the use of an overloaded operator,
6869          then this expression cannot be an integral constant-expression.
6870          An overloaded operator can be used even if both operands are
6871          otherwise permissible in an integral constant-expression if at
6872          least one of the operands is of enumeration type.  */
6873
6874       if (overloaded_p
6875           && cp_parser_non_integral_constant_expression (parser,
6876                                                          NIC_OVERLOADED))
6877         return error_mark_node;
6878     }
6879
6880   return lhs;
6881 }
6882
6883
6884 /* Parse the `? expression : assignment-expression' part of a
6885    conditional-expression.  The LOGICAL_OR_EXPR is the
6886    logical-or-expression that started the conditional-expression.
6887    Returns a representation of the entire conditional-expression.
6888
6889    This routine is used by cp_parser_assignment_expression.
6890
6891      ? expression : assignment-expression
6892
6893    GNU Extensions:
6894
6895      ? : assignment-expression */
6896
6897 static tree
6898 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6899 {
6900   tree expr;
6901   tree assignment_expr;
6902   struct cp_token *token;
6903
6904   /* Consume the `?' token.  */
6905   cp_lexer_consume_token (parser->lexer);
6906   token = cp_lexer_peek_token (parser->lexer);
6907   if (cp_parser_allow_gnu_extensions_p (parser)
6908       && token->type == CPP_COLON)
6909     {
6910       pedwarn (token->location, OPT_pedantic, 
6911                "ISO C++ does not allow ?: with omitted middle operand");
6912       /* Implicit true clause.  */
6913       expr = NULL_TREE;
6914       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6915       warn_for_omitted_condop (token->location, logical_or_expr);
6916     }
6917   else
6918     {
6919       /* Parse the expression.  */
6920       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6921       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6922       c_inhibit_evaluation_warnings +=
6923         ((logical_or_expr == truthvalue_true_node)
6924          - (logical_or_expr == truthvalue_false_node));
6925     }
6926
6927   /* The next token should be a `:'.  */
6928   cp_parser_require (parser, CPP_COLON, RT_COLON);
6929   /* Parse the assignment-expression.  */
6930   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6931   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6932
6933   /* Build the conditional-expression.  */
6934   return build_x_conditional_expr (logical_or_expr,
6935                                    expr,
6936                                    assignment_expr,
6937                                    tf_warning_or_error);
6938 }
6939
6940 /* Parse an assignment-expression.
6941
6942    assignment-expression:
6943      conditional-expression
6944      logical-or-expression assignment-operator assignment_expression
6945      throw-expression
6946
6947    CAST_P is true if this expression is the target of a cast.
6948
6949    Returns a representation for the expression.  */
6950
6951 static tree
6952 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6953                                  cp_id_kind * pidk)
6954 {
6955   tree expr;
6956
6957   /* If the next token is the `throw' keyword, then we're looking at
6958      a throw-expression.  */
6959   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6960     expr = cp_parser_throw_expression (parser);
6961   /* Otherwise, it must be that we are looking at a
6962      logical-or-expression.  */
6963   else
6964     {
6965       /* Parse the binary expressions (logical-or-expression).  */
6966       expr = cp_parser_binary_expression (parser, cast_p, false,
6967                                           PREC_NOT_OPERATOR, pidk);
6968       /* If the next token is a `?' then we're actually looking at a
6969          conditional-expression.  */
6970       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6971         return cp_parser_question_colon_clause (parser, expr);
6972       else
6973         {
6974           enum tree_code assignment_operator;
6975
6976           /* If it's an assignment-operator, we're using the second
6977              production.  */
6978           assignment_operator
6979             = cp_parser_assignment_operator_opt (parser);
6980           if (assignment_operator != ERROR_MARK)
6981             {
6982               bool non_constant_p;
6983
6984               /* Parse the right-hand side of the assignment.  */
6985               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6986
6987               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6988                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6989
6990               /* An assignment may not appear in a
6991                  constant-expression.  */
6992               if (cp_parser_non_integral_constant_expression (parser,
6993                                                               NIC_ASSIGNMENT))
6994                 return error_mark_node;
6995               /* Build the assignment expression.  */
6996               expr = build_x_modify_expr (expr,
6997                                           assignment_operator,
6998                                           rhs,
6999                                           tf_warning_or_error);
7000             }
7001         }
7002     }
7003
7004   return expr;
7005 }
7006
7007 /* Parse an (optional) assignment-operator.
7008
7009    assignment-operator: one of
7010      = *= /= %= += -= >>= <<= &= ^= |=
7011
7012    GNU Extension:
7013
7014    assignment-operator: one of
7015      <?= >?=
7016
7017    If the next token is an assignment operator, the corresponding tree
7018    code is returned, and the token is consumed.  For example, for
7019    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7020    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7021    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7022    operator, ERROR_MARK is returned.  */
7023
7024 static enum tree_code
7025 cp_parser_assignment_operator_opt (cp_parser* parser)
7026 {
7027   enum tree_code op;
7028   cp_token *token;
7029
7030   /* Peek at the next token.  */
7031   token = cp_lexer_peek_token (parser->lexer);
7032
7033   switch (token->type)
7034     {
7035     case CPP_EQ:
7036       op = NOP_EXPR;
7037       break;
7038
7039     case CPP_MULT_EQ:
7040       op = MULT_EXPR;
7041       break;
7042
7043     case CPP_DIV_EQ:
7044       op = TRUNC_DIV_EXPR;
7045       break;
7046
7047     case CPP_MOD_EQ:
7048       op = TRUNC_MOD_EXPR;
7049       break;
7050
7051     case CPP_PLUS_EQ:
7052       op = PLUS_EXPR;
7053       break;
7054
7055     case CPP_MINUS_EQ:
7056       op = MINUS_EXPR;
7057       break;
7058
7059     case CPP_RSHIFT_EQ:
7060       op = RSHIFT_EXPR;
7061       break;
7062
7063     case CPP_LSHIFT_EQ:
7064       op = LSHIFT_EXPR;
7065       break;
7066
7067     case CPP_AND_EQ:
7068       op = BIT_AND_EXPR;
7069       break;
7070
7071     case CPP_XOR_EQ:
7072       op = BIT_XOR_EXPR;
7073       break;
7074
7075     case CPP_OR_EQ:
7076       op = BIT_IOR_EXPR;
7077       break;
7078
7079     default:
7080       /* Nothing else is an assignment operator.  */
7081       op = ERROR_MARK;
7082     }
7083
7084   /* If it was an assignment operator, consume it.  */
7085   if (op != ERROR_MARK)
7086     cp_lexer_consume_token (parser->lexer);
7087
7088   return op;
7089 }
7090
7091 /* Parse an expression.
7092
7093    expression:
7094      assignment-expression
7095      expression , assignment-expression
7096
7097    CAST_P is true if this expression is the target of a cast.
7098
7099    Returns a representation of the expression.  */
7100
7101 static tree
7102 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7103 {
7104   tree expression = NULL_TREE;
7105
7106   while (true)
7107     {
7108       tree assignment_expression;
7109
7110       /* Parse the next assignment-expression.  */
7111       assignment_expression
7112         = cp_parser_assignment_expression (parser, cast_p, pidk);
7113       /* If this is the first assignment-expression, we can just
7114          save it away.  */
7115       if (!expression)
7116         expression = assignment_expression;
7117       else
7118         expression = build_x_compound_expr (expression,
7119                                             assignment_expression,
7120                                             tf_warning_or_error);
7121       /* If the next token is not a comma, then we are done with the
7122          expression.  */
7123       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7124         break;
7125       /* Consume the `,'.  */
7126       cp_lexer_consume_token (parser->lexer);
7127       /* A comma operator cannot appear in a constant-expression.  */
7128       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7129         expression = error_mark_node;
7130     }
7131
7132   return expression;
7133 }
7134
7135 /* Parse a constant-expression.
7136
7137    constant-expression:
7138      conditional-expression
7139
7140   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7141   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7142   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7143   is false, NON_CONSTANT_P should be NULL.  */
7144
7145 static tree
7146 cp_parser_constant_expression (cp_parser* parser,
7147                                bool allow_non_constant_p,
7148                                bool *non_constant_p)
7149 {
7150   bool saved_integral_constant_expression_p;
7151   bool saved_allow_non_integral_constant_expression_p;
7152   bool saved_non_integral_constant_expression_p;
7153   tree expression;
7154
7155   /* It might seem that we could simply parse the
7156      conditional-expression, and then check to see if it were
7157      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7158      one that the compiler can figure out is constant, possibly after
7159      doing some simplifications or optimizations.  The standard has a
7160      precise definition of constant-expression, and we must honor
7161      that, even though it is somewhat more restrictive.
7162
7163      For example:
7164
7165        int i[(2, 3)];
7166
7167      is not a legal declaration, because `(2, 3)' is not a
7168      constant-expression.  The `,' operator is forbidden in a
7169      constant-expression.  However, GCC's constant-folding machinery
7170      will fold this operation to an INTEGER_CST for `3'.  */
7171
7172   /* Save the old settings.  */
7173   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7174   saved_allow_non_integral_constant_expression_p
7175     = parser->allow_non_integral_constant_expression_p;
7176   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7177   /* We are now parsing a constant-expression.  */
7178   parser->integral_constant_expression_p = true;
7179   parser->allow_non_integral_constant_expression_p
7180     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7181   parser->non_integral_constant_expression_p = false;
7182   /* Although the grammar says "conditional-expression", we parse an
7183      "assignment-expression", which also permits "throw-expression"
7184      and the use of assignment operators.  In the case that
7185      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7186      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7187      actually essential that we look for an assignment-expression.
7188      For example, cp_parser_initializer_clauses uses this function to
7189      determine whether a particular assignment-expression is in fact
7190      constant.  */
7191   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7192   /* Restore the old settings.  */
7193   parser->integral_constant_expression_p
7194     = saved_integral_constant_expression_p;
7195   parser->allow_non_integral_constant_expression_p
7196     = saved_allow_non_integral_constant_expression_p;
7197   if (allow_non_constant_p)
7198     *non_constant_p = parser->non_integral_constant_expression_p;
7199   else if (parser->non_integral_constant_expression_p
7200            && cxx_dialect < cxx0x)
7201     expression = error_mark_node;
7202   parser->non_integral_constant_expression_p
7203     = saved_non_integral_constant_expression_p;
7204
7205   return expression;
7206 }
7207
7208 /* Parse __builtin_offsetof.
7209
7210    offsetof-expression:
7211      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7212
7213    offsetof-member-designator:
7214      id-expression
7215      | offsetof-member-designator "." id-expression
7216      | offsetof-member-designator "[" expression "]"
7217      | offsetof-member-designator "->" id-expression  */
7218
7219 static tree
7220 cp_parser_builtin_offsetof (cp_parser *parser)
7221 {
7222   int save_ice_p, save_non_ice_p;
7223   tree type, expr;
7224   cp_id_kind dummy;
7225   cp_token *token;
7226
7227   /* We're about to accept non-integral-constant things, but will
7228      definitely yield an integral constant expression.  Save and
7229      restore these values around our local parsing.  */
7230   save_ice_p = parser->integral_constant_expression_p;
7231   save_non_ice_p = parser->non_integral_constant_expression_p;
7232
7233   /* Consume the "__builtin_offsetof" token.  */
7234   cp_lexer_consume_token (parser->lexer);
7235   /* Consume the opening `('.  */
7236   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7237   /* Parse the type-id.  */
7238   type = cp_parser_type_id (parser);
7239   /* Look for the `,'.  */
7240   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7241   token = cp_lexer_peek_token (parser->lexer);
7242
7243   /* Build the (type *)null that begins the traditional offsetof macro.  */
7244   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7245                             tf_warning_or_error);
7246
7247   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7248   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7249                                                  true, &dummy, token->location);
7250   while (true)
7251     {
7252       token = cp_lexer_peek_token (parser->lexer);
7253       switch (token->type)
7254         {
7255         case CPP_OPEN_SQUARE:
7256           /* offsetof-member-designator "[" expression "]" */
7257           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7258           break;
7259
7260         case CPP_DEREF:
7261           /* offsetof-member-designator "->" identifier */
7262           expr = grok_array_decl (expr, integer_zero_node);
7263           /* FALLTHRU */
7264
7265         case CPP_DOT:
7266           /* offsetof-member-designator "." identifier */
7267           cp_lexer_consume_token (parser->lexer);
7268           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7269                                                          expr, true, &dummy,
7270                                                          token->location);
7271           break;
7272
7273         case CPP_CLOSE_PAREN:
7274           /* Consume the ")" token.  */
7275           cp_lexer_consume_token (parser->lexer);
7276           goto success;
7277
7278         default:
7279           /* Error.  We know the following require will fail, but
7280              that gives the proper error message.  */
7281           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7282           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7283           expr = error_mark_node;
7284           goto failure;
7285         }
7286     }
7287
7288  success:
7289   /* If we're processing a template, we can't finish the semantics yet.
7290      Otherwise we can fold the entire expression now.  */
7291   if (processing_template_decl)
7292     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7293   else
7294     expr = finish_offsetof (expr);
7295
7296  failure:
7297   parser->integral_constant_expression_p = save_ice_p;
7298   parser->non_integral_constant_expression_p = save_non_ice_p;
7299
7300   return expr;
7301 }
7302
7303 /* Parse a trait expression.  */
7304
7305 static tree
7306 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7307 {
7308   cp_trait_kind kind;
7309   tree type1, type2 = NULL_TREE;
7310   bool binary = false;
7311   cp_decl_specifier_seq decl_specs;
7312
7313   switch (keyword)
7314     {
7315     case RID_HAS_NOTHROW_ASSIGN:
7316       kind = CPTK_HAS_NOTHROW_ASSIGN;
7317       break;
7318     case RID_HAS_NOTHROW_CONSTRUCTOR:
7319       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7320       break;
7321     case RID_HAS_NOTHROW_COPY:
7322       kind = CPTK_HAS_NOTHROW_COPY;
7323       break;
7324     case RID_HAS_TRIVIAL_ASSIGN:
7325       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7326       break;
7327     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7328       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7329       break;
7330     case RID_HAS_TRIVIAL_COPY:
7331       kind = CPTK_HAS_TRIVIAL_COPY;
7332       break;
7333     case RID_HAS_TRIVIAL_DESTRUCTOR:
7334       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7335       break;
7336     case RID_HAS_VIRTUAL_DESTRUCTOR:
7337       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7338       break;
7339     case RID_IS_ABSTRACT:
7340       kind = CPTK_IS_ABSTRACT;
7341       break;
7342     case RID_IS_BASE_OF:
7343       kind = CPTK_IS_BASE_OF;
7344       binary = true;
7345       break;
7346     case RID_IS_CLASS:
7347       kind = CPTK_IS_CLASS;
7348       break;
7349     case RID_IS_CONVERTIBLE_TO:
7350       kind = CPTK_IS_CONVERTIBLE_TO;
7351       binary = true;
7352       break;
7353     case RID_IS_EMPTY:
7354       kind = CPTK_IS_EMPTY;
7355       break;
7356     case RID_IS_ENUM:
7357       kind = CPTK_IS_ENUM;
7358       break;
7359     case RID_IS_POD:
7360       kind = CPTK_IS_POD;
7361       break;
7362     case RID_IS_POLYMORPHIC:
7363       kind = CPTK_IS_POLYMORPHIC;
7364       break;
7365     case RID_IS_STD_LAYOUT:
7366       kind = CPTK_IS_STD_LAYOUT;
7367       break;
7368     case RID_IS_TRIVIAL:
7369       kind = CPTK_IS_TRIVIAL;
7370       break;
7371     case RID_IS_UNION:
7372       kind = CPTK_IS_UNION;
7373       break;
7374     case RID_IS_LITERAL_TYPE:
7375       kind = CPTK_IS_LITERAL_TYPE;
7376       break;
7377     default:
7378       gcc_unreachable ();
7379     }
7380
7381   /* Consume the token.  */
7382   cp_lexer_consume_token (parser->lexer);
7383
7384   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7385
7386   type1 = cp_parser_type_id (parser);
7387
7388   if (type1 == error_mark_node)
7389     return error_mark_node;
7390
7391   /* Build a trivial decl-specifier-seq.  */
7392   clear_decl_specs (&decl_specs);
7393   decl_specs.type = type1;
7394
7395   /* Call grokdeclarator to figure out what type this is.  */
7396   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7397                           /*initialized=*/0, /*attrlist=*/NULL);
7398
7399   if (binary)
7400     {
7401       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7402  
7403       type2 = cp_parser_type_id (parser);
7404
7405       if (type2 == error_mark_node)
7406         return error_mark_node;
7407
7408       /* Build a trivial decl-specifier-seq.  */
7409       clear_decl_specs (&decl_specs);
7410       decl_specs.type = type2;
7411
7412       /* Call grokdeclarator to figure out what type this is.  */
7413       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7414                               /*initialized=*/0, /*attrlist=*/NULL);
7415     }
7416
7417   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7418
7419   /* Complete the trait expression, which may mean either processing
7420      the trait expr now or saving it for template instantiation.  */
7421   return finish_trait_expr (kind, type1, type2);
7422 }
7423
7424 /* Lambdas that appear in variable initializer or default argument scope
7425    get that in their mangling, so we need to record it.  We might as well
7426    use the count for function and namespace scopes as well.  */
7427 static GTY(()) tree lambda_scope;
7428 static GTY(()) int lambda_count;
7429 typedef struct GTY(()) tree_int
7430 {
7431   tree t;
7432   int i;
7433 } tree_int;
7434 DEF_VEC_O(tree_int);
7435 DEF_VEC_ALLOC_O(tree_int,gc);
7436 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7437
7438 static void
7439 start_lambda_scope (tree decl)
7440 {
7441   tree_int ti;
7442   gcc_assert (decl);
7443   /* Once we're inside a function, we ignore other scopes and just push
7444      the function again so that popping works properly.  */
7445   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7446     decl = current_function_decl;
7447   ti.t = lambda_scope;
7448   ti.i = lambda_count;
7449   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7450   if (lambda_scope != decl)
7451     {
7452       /* Don't reset the count if we're still in the same function.  */
7453       lambda_scope = decl;
7454       lambda_count = 0;
7455     }
7456 }
7457
7458 static void
7459 record_lambda_scope (tree lambda)
7460 {
7461   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7462   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7463 }
7464
7465 static void
7466 finish_lambda_scope (void)
7467 {
7468   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7469   if (lambda_scope != p->t)
7470     {
7471       lambda_scope = p->t;
7472       lambda_count = p->i;
7473     }
7474   VEC_pop (tree_int, lambda_scope_stack);
7475 }
7476
7477 /* Parse a lambda expression.
7478
7479    lambda-expression:
7480      lambda-introducer lambda-declarator [opt] compound-statement
7481
7482    Returns a representation of the expression.  */
7483
7484 static tree
7485 cp_parser_lambda_expression (cp_parser* parser)
7486 {
7487   tree lambda_expr = build_lambda_expr ();
7488   tree type;
7489
7490   LAMBDA_EXPR_LOCATION (lambda_expr)
7491     = cp_lexer_peek_token (parser->lexer)->location;
7492
7493   if (cp_unevaluated_operand)
7494     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7495               "lambda-expression in unevaluated context");
7496
7497   /* We may be in the middle of deferred access check.  Disable
7498      it now.  */
7499   push_deferring_access_checks (dk_no_deferred);
7500
7501   cp_parser_lambda_introducer (parser, lambda_expr);
7502
7503   type = begin_lambda_type (lambda_expr);
7504
7505   record_lambda_scope (lambda_expr);
7506
7507   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7508   determine_visibility (TYPE_NAME (type));
7509
7510   /* Now that we've started the type, add the capture fields for any
7511      explicit captures.  */
7512   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7513
7514   {
7515     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7516     unsigned int saved_num_template_parameter_lists
7517         = parser->num_template_parameter_lists;
7518
7519     parser->num_template_parameter_lists = 0;
7520
7521     /* By virtue of defining a local class, a lambda expression has access to
7522        the private variables of enclosing classes.  */
7523
7524     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7525
7526     cp_parser_lambda_body (parser, lambda_expr);
7527
7528     /* The capture list was built up in reverse order; fix that now.  */
7529     {
7530       tree newlist = NULL_TREE;
7531       tree elt, next;
7532
7533       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7534            elt; elt = next)
7535         {
7536           tree field = TREE_PURPOSE (elt);
7537           char *buf;
7538
7539           next = TREE_CHAIN (elt);
7540           TREE_CHAIN (elt) = newlist;
7541           newlist = elt;
7542
7543           /* Also add __ to the beginning of the field name so that code
7544              outside the lambda body can't see the captured name.  We could
7545              just remove the name entirely, but this is more useful for
7546              debugging.  */
7547           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7548             /* The 'this' capture already starts with __.  */
7549             continue;
7550
7551           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7552           buf[1] = buf[0] = '_';
7553           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7554                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7555           DECL_NAME (field) = get_identifier (buf);
7556         }
7557       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7558     }
7559
7560     maybe_add_lambda_conv_op (type);
7561
7562     type = finish_struct (type, /*attributes=*/NULL_TREE);
7563
7564     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7565   }
7566
7567   pop_deferring_access_checks ();
7568
7569   return build_lambda_object (lambda_expr);
7570 }
7571
7572 /* Parse the beginning of a lambda expression.
7573
7574    lambda-introducer:
7575      [ lambda-capture [opt] ]
7576
7577    LAMBDA_EXPR is the current representation of the lambda expression.  */
7578
7579 static void
7580 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7581 {
7582   /* Need commas after the first capture.  */
7583   bool first = true;
7584
7585   /* Eat the leading `['.  */
7586   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7587
7588   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7589   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7590       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7591     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7592   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7593     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7594
7595   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7596     {
7597       cp_lexer_consume_token (parser->lexer);
7598       first = false;
7599     }
7600
7601   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7602     {
7603       cp_token* capture_token;
7604       tree capture_id;
7605       tree capture_init_expr;
7606       cp_id_kind idk = CP_ID_KIND_NONE;
7607       bool explicit_init_p = false;
7608
7609       enum capture_kind_type
7610       {
7611         BY_COPY,
7612         BY_REFERENCE
7613       };
7614       enum capture_kind_type capture_kind = BY_COPY;
7615
7616       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7617         {
7618           error ("expected end of capture-list");
7619           return;
7620         }
7621
7622       if (first)
7623         first = false;
7624       else
7625         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7626
7627       /* Possibly capture `this'.  */
7628       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7629         {
7630           cp_lexer_consume_token (parser->lexer);
7631           add_capture (lambda_expr,
7632                        /*id=*/get_identifier ("__this"),
7633                        /*initializer=*/finish_this_expr(),
7634                        /*by_reference_p=*/false,
7635                        explicit_init_p);
7636           continue;
7637         }
7638
7639       /* Remember whether we want to capture as a reference or not.  */
7640       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7641         {
7642           capture_kind = BY_REFERENCE;
7643           cp_lexer_consume_token (parser->lexer);
7644         }
7645
7646       /* Get the identifier.  */
7647       capture_token = cp_lexer_peek_token (parser->lexer);
7648       capture_id = cp_parser_identifier (parser);
7649
7650       if (capture_id == error_mark_node)
7651         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7652            delimiters, but I modified this to stop on unnested ']' as well.  It
7653            was already changed to stop on unnested '}', so the
7654            "closing_parenthesis" name is no more misleading with my change.  */
7655         {
7656           cp_parser_skip_to_closing_parenthesis (parser,
7657                                                  /*recovering=*/true,
7658                                                  /*or_comma=*/true,
7659                                                  /*consume_paren=*/true);
7660           break;
7661         }
7662
7663       /* Find the initializer for this capture.  */
7664       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7665         {
7666           /* An explicit expression exists.  */
7667           cp_lexer_consume_token (parser->lexer);
7668           pedwarn (input_location, OPT_pedantic,
7669                    "ISO C++ does not allow initializers "
7670                    "in lambda expression capture lists");
7671           capture_init_expr = cp_parser_assignment_expression (parser,
7672                                                                /*cast_p=*/true,
7673                                                                &idk);
7674           explicit_init_p = true;
7675         }
7676       else
7677         {
7678           const char* error_msg;
7679
7680           /* Turn the identifier into an id-expression.  */
7681           capture_init_expr
7682             = cp_parser_lookup_name
7683                 (parser,
7684                  capture_id,
7685                  none_type,
7686                  /*is_template=*/false,
7687                  /*is_namespace=*/false,
7688                  /*check_dependency=*/true,
7689                  /*ambiguous_decls=*/NULL,
7690                  capture_token->location);
7691
7692           capture_init_expr
7693             = finish_id_expression
7694                 (capture_id,
7695                  capture_init_expr,
7696                  parser->scope,
7697                  &idk,
7698                  /*integral_constant_expression_p=*/false,
7699                  /*allow_non_integral_constant_expression_p=*/false,
7700                  /*non_integral_constant_expression_p=*/NULL,
7701                  /*template_p=*/false,
7702                  /*done=*/true,
7703                  /*address_p=*/false,
7704                  /*template_arg_p=*/false,
7705                  &error_msg,
7706                  capture_token->location);
7707         }
7708
7709       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7710         capture_init_expr
7711           = unqualified_name_lookup_error (capture_init_expr);
7712
7713       add_capture (lambda_expr,
7714                    capture_id,
7715                    capture_init_expr,
7716                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7717                    explicit_init_p);
7718     }
7719
7720   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7721 }
7722
7723 /* Parse the (optional) middle of a lambda expression.
7724
7725    lambda-declarator:
7726      ( parameter-declaration-clause [opt] )
7727        attribute-specifier [opt]
7728        mutable [opt]
7729        exception-specification [opt]
7730        lambda-return-type-clause [opt]
7731
7732    LAMBDA_EXPR is the current representation of the lambda expression.  */
7733
7734 static void
7735 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7736 {
7737   /* 5.1.1.4 of the standard says:
7738        If a lambda-expression does not include a lambda-declarator, it is as if
7739        the lambda-declarator were ().
7740      This means an empty parameter list, no attributes, and no exception
7741      specification.  */
7742   tree param_list = void_list_node;
7743   tree attributes = NULL_TREE;
7744   tree exception_spec = NULL_TREE;
7745   tree t;
7746
7747   /* The lambda-declarator is optional, but must begin with an opening
7748      parenthesis if present.  */
7749   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7750     {
7751       cp_lexer_consume_token (parser->lexer);
7752
7753       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7754
7755       /* Parse parameters.  */
7756       param_list = cp_parser_parameter_declaration_clause (parser);
7757
7758       /* Default arguments shall not be specified in the
7759          parameter-declaration-clause of a lambda-declarator.  */
7760       for (t = param_list; t; t = TREE_CHAIN (t))
7761         if (TREE_PURPOSE (t))
7762           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7763                    "default argument specified for lambda parameter");
7764
7765       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7766
7767       attributes = cp_parser_attributes_opt (parser);
7768
7769       /* Parse optional `mutable' keyword.  */
7770       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7771         {
7772           cp_lexer_consume_token (parser->lexer);
7773           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7774         }
7775
7776       /* Parse optional exception specification.  */
7777       exception_spec = cp_parser_exception_specification_opt (parser);
7778
7779       /* Parse optional trailing return type.  */
7780       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7781         {
7782           cp_lexer_consume_token (parser->lexer);
7783           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7784         }
7785
7786       /* The function parameters must be in scope all the way until after the
7787          trailing-return-type in case of decltype.  */
7788       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7789         pop_binding (DECL_NAME (t), t);
7790
7791       leave_scope ();
7792     }
7793
7794   /* Create the function call operator.
7795
7796      Messing with declarators like this is no uglier than building up the
7797      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7798      other code.  */
7799   {
7800     cp_decl_specifier_seq return_type_specs;
7801     cp_declarator* declarator;
7802     tree fco;
7803     int quals;
7804     void *p;
7805
7806     clear_decl_specs (&return_type_specs);
7807     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7808       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7809     else
7810       /* Maybe we will deduce the return type later, but we can use void
7811          as a placeholder return type anyways.  */
7812       return_type_specs.type = void_type_node;
7813
7814     p = obstack_alloc (&declarator_obstack, 0);
7815
7816     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7817                                      sfk_none);
7818
7819     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7820              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7821     declarator = make_call_declarator (declarator, param_list, quals,
7822                                        exception_spec,
7823                                        /*late_return_type=*/NULL_TREE);
7824     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7825
7826     fco = grokmethod (&return_type_specs,
7827                       declarator,
7828                       attributes);
7829     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7830     DECL_ARTIFICIAL (fco) = 1;
7831
7832     finish_member_declaration (fco);
7833
7834     obstack_free (&declarator_obstack, p);
7835   }
7836 }
7837
7838 /* Parse the body of a lambda expression, which is simply
7839
7840    compound-statement
7841
7842    but which requires special handling.
7843    LAMBDA_EXPR is the current representation of the lambda expression.  */
7844
7845 static void
7846 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7847 {
7848   bool nested = (current_function_decl != NULL_TREE);
7849   if (nested)
7850     push_function_context ();
7851
7852   /* Finish the function call operator
7853      - class_specifier
7854      + late_parsing_for_member
7855      + function_definition_after_declarator
7856      + ctor_initializer_opt_and_function_body  */
7857   {
7858     tree fco = lambda_function (lambda_expr);
7859     tree body;
7860     bool done = false;
7861
7862     /* Let the front end know that we are going to be defining this
7863        function.  */
7864     start_preparsed_function (fco,
7865                               NULL_TREE,
7866                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7867
7868     start_lambda_scope (fco);
7869     body = begin_function_body ();
7870
7871     /* 5.1.1.4 of the standard says:
7872          If a lambda-expression does not include a trailing-return-type, it
7873          is as if the trailing-return-type denotes the following type:
7874           * if the compound-statement is of the form
7875                { return attribute-specifier [opt] expression ; }
7876              the type of the returned expression after lvalue-to-rvalue
7877              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7878              (_conv.array_ 4.2), and function-to-pointer conversion
7879              (_conv.func_ 4.3);
7880           * otherwise, void.  */
7881
7882     /* In a lambda that has neither a lambda-return-type-clause
7883        nor a deducible form, errors should be reported for return statements
7884        in the body.  Since we used void as the placeholder return type, parsing
7885        the body as usual will give such desired behavior.  */
7886     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7887         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7888         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7889         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7890       {
7891         tree compound_stmt;
7892         tree expr = NULL_TREE;
7893         cp_id_kind idk = CP_ID_KIND_NONE;
7894
7895         /* Parse tentatively in case there's more after the initial return
7896            statement.  */
7897         cp_parser_parse_tentatively (parser);
7898
7899         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7900         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7901
7902         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7903
7904         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7905         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7906
7907         if (cp_parser_parse_definitely (parser))
7908           {
7909             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7910
7911             compound_stmt = begin_compound_stmt (0);
7912             /* Will get error here if type not deduced yet.  */
7913             finish_return_stmt (expr);
7914             finish_compound_stmt (compound_stmt);
7915
7916             done = true;
7917           }
7918       }
7919
7920     if (!done)
7921       {
7922         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7923           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7924         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7925            cp_parser_compound_stmt does not pass it.  */
7926         cp_parser_function_body (parser);
7927         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7928       }
7929
7930     finish_function_body (body);
7931     finish_lambda_scope ();
7932
7933     /* Finish the function and generate code for it if necessary.  */
7934     expand_or_defer_fn (finish_function (/*inline*/2));
7935   }
7936
7937   if (nested)
7938     pop_function_context();
7939 }
7940
7941 /* Statements [gram.stmt.stmt]  */
7942
7943 /* Parse a statement.
7944
7945    statement:
7946      labeled-statement
7947      expression-statement
7948      compound-statement
7949      selection-statement
7950      iteration-statement
7951      jump-statement
7952      declaration-statement
7953      try-block
7954
7955   IN_COMPOUND is true when the statement is nested inside a
7956   cp_parser_compound_statement; this matters for certain pragmas.
7957
7958   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7959   is a (possibly labeled) if statement which is not enclosed in braces
7960   and has an else clause.  This is used to implement -Wparentheses.  */
7961
7962 static void
7963 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7964                      bool in_compound, bool *if_p)
7965 {
7966   tree statement;
7967   cp_token *token;
7968   location_t statement_location;
7969
7970  restart:
7971   if (if_p != NULL)
7972     *if_p = false;
7973   /* There is no statement yet.  */
7974   statement = NULL_TREE;
7975   /* Peek at the next token.  */
7976   token = cp_lexer_peek_token (parser->lexer);
7977   /* Remember the location of the first token in the statement.  */
7978   statement_location = token->location;
7979   /* If this is a keyword, then that will often determine what kind of
7980      statement we have.  */
7981   if (token->type == CPP_KEYWORD)
7982     {
7983       enum rid keyword = token->keyword;
7984
7985       switch (keyword)
7986         {
7987         case RID_CASE:
7988         case RID_DEFAULT:
7989           /* Looks like a labeled-statement with a case label.
7990              Parse the label, and then use tail recursion to parse
7991              the statement.  */
7992           cp_parser_label_for_labeled_statement (parser);
7993           goto restart;
7994
7995         case RID_IF:
7996         case RID_SWITCH:
7997           statement = cp_parser_selection_statement (parser, if_p);
7998           break;
7999
8000         case RID_WHILE:
8001         case RID_DO:
8002         case RID_FOR:
8003           statement = cp_parser_iteration_statement (parser);
8004           break;
8005
8006         case RID_BREAK:
8007         case RID_CONTINUE:
8008         case RID_RETURN:
8009         case RID_GOTO:
8010           statement = cp_parser_jump_statement (parser);
8011           break;
8012
8013           /* Objective-C++ exception-handling constructs.  */
8014         case RID_AT_TRY:
8015         case RID_AT_CATCH:
8016         case RID_AT_FINALLY:
8017         case RID_AT_SYNCHRONIZED:
8018         case RID_AT_THROW:
8019           statement = cp_parser_objc_statement (parser);
8020           break;
8021
8022         case RID_TRY:
8023           statement = cp_parser_try_block (parser);
8024           break;
8025
8026         case RID_NAMESPACE:
8027           /* This must be a namespace alias definition.  */
8028           cp_parser_declaration_statement (parser);
8029           return;
8030           
8031         default:
8032           /* It might be a keyword like `int' that can start a
8033              declaration-statement.  */
8034           break;
8035         }
8036     }
8037   else if (token->type == CPP_NAME)
8038     {
8039       /* If the next token is a `:', then we are looking at a
8040          labeled-statement.  */
8041       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8042       if (token->type == CPP_COLON)
8043         {
8044           /* Looks like a labeled-statement with an ordinary label.
8045              Parse the label, and then use tail recursion to parse
8046              the statement.  */
8047           cp_parser_label_for_labeled_statement (parser);
8048           goto restart;
8049         }
8050     }
8051   /* Anything that starts with a `{' must be a compound-statement.  */
8052   else if (token->type == CPP_OPEN_BRACE)
8053     statement = cp_parser_compound_statement (parser, NULL, false);
8054   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8055      a statement all its own.  */
8056   else if (token->type == CPP_PRAGMA)
8057     {
8058       /* Only certain OpenMP pragmas are attached to statements, and thus
8059          are considered statements themselves.  All others are not.  In
8060          the context of a compound, accept the pragma as a "statement" and
8061          return so that we can check for a close brace.  Otherwise we
8062          require a real statement and must go back and read one.  */
8063       if (in_compound)
8064         cp_parser_pragma (parser, pragma_compound);
8065       else if (!cp_parser_pragma (parser, pragma_stmt))
8066         goto restart;
8067       return;
8068     }
8069   else if (token->type == CPP_EOF)
8070     {
8071       cp_parser_error (parser, "expected statement");
8072       return;
8073     }
8074
8075   /* Everything else must be a declaration-statement or an
8076      expression-statement.  Try for the declaration-statement
8077      first, unless we are looking at a `;', in which case we know that
8078      we have an expression-statement.  */
8079   if (!statement)
8080     {
8081       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8082         {
8083           cp_parser_parse_tentatively (parser);
8084           /* Try to parse the declaration-statement.  */
8085           cp_parser_declaration_statement (parser);
8086           /* If that worked, we're done.  */
8087           if (cp_parser_parse_definitely (parser))
8088             return;
8089         }
8090       /* Look for an expression-statement instead.  */
8091       statement = cp_parser_expression_statement (parser, in_statement_expr);
8092     }
8093
8094   /* Set the line number for the statement.  */
8095   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8096     SET_EXPR_LOCATION (statement, statement_location);
8097 }
8098
8099 /* Parse the label for a labeled-statement, i.e.
8100
8101    identifier :
8102    case constant-expression :
8103    default :
8104
8105    GNU Extension:
8106    case constant-expression ... constant-expression : statement
8107
8108    When a label is parsed without errors, the label is added to the
8109    parse tree by the finish_* functions, so this function doesn't
8110    have to return the label.  */
8111
8112 static void
8113 cp_parser_label_for_labeled_statement (cp_parser* parser)
8114 {
8115   cp_token *token;
8116   tree label = NULL_TREE;
8117
8118   /* The next token should be an identifier.  */
8119   token = cp_lexer_peek_token (parser->lexer);
8120   if (token->type != CPP_NAME
8121       && token->type != CPP_KEYWORD)
8122     {
8123       cp_parser_error (parser, "expected labeled-statement");
8124       return;
8125     }
8126
8127   switch (token->keyword)
8128     {
8129     case RID_CASE:
8130       {
8131         tree expr, expr_hi;
8132         cp_token *ellipsis;
8133
8134         /* Consume the `case' token.  */
8135         cp_lexer_consume_token (parser->lexer);
8136         /* Parse the constant-expression.  */
8137         expr = cp_parser_constant_expression (parser,
8138                                               /*allow_non_constant_p=*/false,
8139                                               NULL);
8140
8141         ellipsis = cp_lexer_peek_token (parser->lexer);
8142         if (ellipsis->type == CPP_ELLIPSIS)
8143           {
8144             /* Consume the `...' token.  */
8145             cp_lexer_consume_token (parser->lexer);
8146             expr_hi =
8147               cp_parser_constant_expression (parser,
8148                                              /*allow_non_constant_p=*/false,
8149                                              NULL);
8150             /* We don't need to emit warnings here, as the common code
8151                will do this for us.  */
8152           }
8153         else
8154           expr_hi = NULL_TREE;
8155
8156         if (parser->in_switch_statement_p)
8157           finish_case_label (token->location, expr, expr_hi);
8158         else
8159           error_at (token->location,
8160                     "case label %qE not within a switch statement",
8161                     expr);
8162       }
8163       break;
8164
8165     case RID_DEFAULT:
8166       /* Consume the `default' token.  */
8167       cp_lexer_consume_token (parser->lexer);
8168
8169       if (parser->in_switch_statement_p)
8170         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8171       else
8172         error_at (token->location, "case label not within a switch statement");
8173       break;
8174
8175     default:
8176       /* Anything else must be an ordinary label.  */
8177       label = finish_label_stmt (cp_parser_identifier (parser));
8178       break;
8179     }
8180
8181   /* Require the `:' token.  */
8182   cp_parser_require (parser, CPP_COLON, RT_COLON);
8183
8184   /* An ordinary label may optionally be followed by attributes.
8185      However, this is only permitted if the attributes are then
8186      followed by a semicolon.  This is because, for backward
8187      compatibility, when parsing
8188        lab: __attribute__ ((unused)) int i;
8189      we want the attribute to attach to "i", not "lab".  */
8190   if (label != NULL_TREE
8191       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8192     {
8193       tree attrs;
8194
8195       cp_parser_parse_tentatively (parser);
8196       attrs = cp_parser_attributes_opt (parser);
8197       if (attrs == NULL_TREE
8198           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8199         cp_parser_abort_tentative_parse (parser);
8200       else if (!cp_parser_parse_definitely (parser))
8201         ;
8202       else
8203         cplus_decl_attributes (&label, attrs, 0);
8204     }
8205 }
8206
8207 /* Parse an expression-statement.
8208
8209    expression-statement:
8210      expression [opt] ;
8211
8212    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8213    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8214    indicates whether this expression-statement is part of an
8215    expression statement.  */
8216
8217 static tree
8218 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8219 {
8220   tree statement = NULL_TREE;
8221   cp_token *token = cp_lexer_peek_token (parser->lexer);
8222
8223   /* If the next token is a ';', then there is no expression
8224      statement.  */
8225   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8226     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8227
8228   /* Give a helpful message for "A<T>::type t;" and the like.  */
8229   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8230       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8231     {
8232       if (TREE_CODE (statement) == SCOPE_REF)
8233         error_at (token->location, "need %<typename%> before %qE because "
8234                   "%qT is a dependent scope",
8235                   statement, TREE_OPERAND (statement, 0));
8236       else if (is_overloaded_fn (statement)
8237                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8238         {
8239           /* A::A a; */
8240           tree fn = get_first_fn (statement);
8241           error_at (token->location,
8242                     "%<%T::%D%> names the constructor, not the type",
8243                     DECL_CONTEXT (fn), DECL_NAME (fn));
8244         }
8245     }
8246
8247   /* Consume the final `;'.  */
8248   cp_parser_consume_semicolon_at_end_of_statement (parser);
8249
8250   if (in_statement_expr
8251       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8252     /* This is the final expression statement of a statement
8253        expression.  */
8254     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8255   else if (statement)
8256     statement = finish_expr_stmt (statement);
8257   else
8258     finish_stmt ();
8259
8260   return statement;
8261 }
8262
8263 /* Parse a compound-statement.
8264
8265    compound-statement:
8266      { statement-seq [opt] }
8267
8268    GNU extension:
8269
8270    compound-statement:
8271      { label-declaration-seq [opt] statement-seq [opt] }
8272
8273    label-declaration-seq:
8274      label-declaration
8275      label-declaration-seq label-declaration
8276
8277    Returns a tree representing the statement.  */
8278
8279 static tree
8280 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8281                               bool in_try)
8282 {
8283   tree compound_stmt;
8284
8285   /* Consume the `{'.  */
8286   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8287     return error_mark_node;
8288   /* Begin the compound-statement.  */
8289   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8290   /* If the next keyword is `__label__' we have a label declaration.  */
8291   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8292     cp_parser_label_declaration (parser);
8293   /* Parse an (optional) statement-seq.  */
8294   cp_parser_statement_seq_opt (parser, in_statement_expr);
8295   /* Finish the compound-statement.  */
8296   finish_compound_stmt (compound_stmt);
8297   /* Consume the `}'.  */
8298   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8299
8300   return compound_stmt;
8301 }
8302
8303 /* Parse an (optional) statement-seq.
8304
8305    statement-seq:
8306      statement
8307      statement-seq [opt] statement  */
8308
8309 static void
8310 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8311 {
8312   /* Scan statements until there aren't any more.  */
8313   while (true)
8314     {
8315       cp_token *token = cp_lexer_peek_token (parser->lexer);
8316
8317       /* If we are looking at a `}', then we have run out of
8318          statements; the same is true if we have reached the end
8319          of file, or have stumbled upon a stray '@end'.  */
8320       if (token->type == CPP_CLOSE_BRACE
8321           || token->type == CPP_EOF
8322           || token->type == CPP_PRAGMA_EOL
8323           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8324         break;
8325       
8326       /* If we are in a compound statement and find 'else' then
8327          something went wrong.  */
8328       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8329         {
8330           if (parser->in_statement & IN_IF_STMT) 
8331             break;
8332           else
8333             {
8334               token = cp_lexer_consume_token (parser->lexer);
8335               error_at (token->location, "%<else%> without a previous %<if%>");
8336             }
8337         }
8338
8339       /* Parse the statement.  */
8340       cp_parser_statement (parser, in_statement_expr, true, NULL);
8341     }
8342 }
8343
8344 /* Parse a selection-statement.
8345
8346    selection-statement:
8347      if ( condition ) statement
8348      if ( condition ) statement else statement
8349      switch ( condition ) statement
8350
8351    Returns the new IF_STMT or SWITCH_STMT.
8352
8353    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8354    is a (possibly labeled) if statement which is not enclosed in
8355    braces and has an else clause.  This is used to implement
8356    -Wparentheses.  */
8357
8358 static tree
8359 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8360 {
8361   cp_token *token;
8362   enum rid keyword;
8363
8364   if (if_p != NULL)
8365     *if_p = false;
8366
8367   /* Peek at the next token.  */
8368   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8369
8370   /* See what kind of keyword it is.  */
8371   keyword = token->keyword;
8372   switch (keyword)
8373     {
8374     case RID_IF:
8375     case RID_SWITCH:
8376       {
8377         tree statement;
8378         tree condition;
8379
8380         /* Look for the `('.  */
8381         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8382           {
8383             cp_parser_skip_to_end_of_statement (parser);
8384             return error_mark_node;
8385           }
8386
8387         /* Begin the selection-statement.  */
8388         if (keyword == RID_IF)
8389           statement = begin_if_stmt ();
8390         else
8391           statement = begin_switch_stmt ();
8392
8393         /* Parse the condition.  */
8394         condition = cp_parser_condition (parser);
8395         /* Look for the `)'.  */
8396         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8397           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8398                                                  /*consume_paren=*/true);
8399
8400         if (keyword == RID_IF)
8401           {
8402             bool nested_if;
8403             unsigned char in_statement;
8404
8405             /* Add the condition.  */
8406             finish_if_stmt_cond (condition, statement);
8407
8408             /* Parse the then-clause.  */
8409             in_statement = parser->in_statement;
8410             parser->in_statement |= IN_IF_STMT;
8411             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8412               {
8413                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8414                 add_stmt (build_empty_stmt (loc));
8415                 cp_lexer_consume_token (parser->lexer);
8416                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8417                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8418                               "empty body in an %<if%> statement");
8419                 nested_if = false;
8420               }
8421             else
8422               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8423             parser->in_statement = in_statement;
8424
8425             finish_then_clause (statement);
8426
8427             /* If the next token is `else', parse the else-clause.  */
8428             if (cp_lexer_next_token_is_keyword (parser->lexer,
8429                                                 RID_ELSE))
8430               {
8431                 /* Consume the `else' keyword.  */
8432                 cp_lexer_consume_token (parser->lexer);
8433                 begin_else_clause (statement);
8434                 /* Parse the else-clause.  */
8435                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8436                   {
8437                     location_t loc;
8438                     loc = cp_lexer_peek_token (parser->lexer)->location;
8439                     warning_at (loc,
8440                                 OPT_Wempty_body, "suggest braces around "
8441                                 "empty body in an %<else%> statement");
8442                     add_stmt (build_empty_stmt (loc));
8443                     cp_lexer_consume_token (parser->lexer);
8444                   }
8445                 else
8446                   cp_parser_implicitly_scoped_statement (parser, NULL);
8447
8448                 finish_else_clause (statement);
8449
8450                 /* If we are currently parsing a then-clause, then
8451                    IF_P will not be NULL.  We set it to true to
8452                    indicate that this if statement has an else clause.
8453                    This may trigger the Wparentheses warning below
8454                    when we get back up to the parent if statement.  */
8455                 if (if_p != NULL)
8456                   *if_p = true;
8457               }
8458             else
8459               {
8460                 /* This if statement does not have an else clause.  If
8461                    NESTED_IF is true, then the then-clause is an if
8462                    statement which does have an else clause.  We warn
8463                    about the potential ambiguity.  */
8464                 if (nested_if)
8465                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8466                               "suggest explicit braces to avoid ambiguous"
8467                               " %<else%>");
8468               }
8469
8470             /* Now we're all done with the if-statement.  */
8471             finish_if_stmt (statement);
8472           }
8473         else
8474           {
8475             bool in_switch_statement_p;
8476             unsigned char in_statement;
8477
8478             /* Add the condition.  */
8479             finish_switch_cond (condition, statement);
8480
8481             /* Parse the body of the switch-statement.  */
8482             in_switch_statement_p = parser->in_switch_statement_p;
8483             in_statement = parser->in_statement;
8484             parser->in_switch_statement_p = true;
8485             parser->in_statement |= IN_SWITCH_STMT;
8486             cp_parser_implicitly_scoped_statement (parser, NULL);
8487             parser->in_switch_statement_p = in_switch_statement_p;
8488             parser->in_statement = in_statement;
8489
8490             /* Now we're all done with the switch-statement.  */
8491             finish_switch_stmt (statement);
8492           }
8493
8494         return statement;
8495       }
8496       break;
8497
8498     default:
8499       cp_parser_error (parser, "expected selection-statement");
8500       return error_mark_node;
8501     }
8502 }
8503
8504 /* Parse a condition.
8505
8506    condition:
8507      expression
8508      type-specifier-seq declarator = initializer-clause
8509      type-specifier-seq declarator braced-init-list
8510
8511    GNU Extension:
8512
8513    condition:
8514      type-specifier-seq declarator asm-specification [opt]
8515        attributes [opt] = assignment-expression
8516
8517    Returns the expression that should be tested.  */
8518
8519 static tree
8520 cp_parser_condition (cp_parser* parser)
8521 {
8522   cp_decl_specifier_seq type_specifiers;
8523   const char *saved_message;
8524   int declares_class_or_enum;
8525
8526   /* Try the declaration first.  */
8527   cp_parser_parse_tentatively (parser);
8528   /* New types are not allowed in the type-specifier-seq for a
8529      condition.  */
8530   saved_message = parser->type_definition_forbidden_message;
8531   parser->type_definition_forbidden_message
8532     = G_("types may not be defined in conditions");
8533   /* Parse the type-specifier-seq.  */
8534   cp_parser_decl_specifier_seq (parser,
8535                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8536                                 &type_specifiers,
8537                                 &declares_class_or_enum);
8538   /* Restore the saved message.  */
8539   parser->type_definition_forbidden_message = saved_message;
8540   /* If all is well, we might be looking at a declaration.  */
8541   if (!cp_parser_error_occurred (parser))
8542     {
8543       tree decl;
8544       tree asm_specification;
8545       tree attributes;
8546       cp_declarator *declarator;
8547       tree initializer = NULL_TREE;
8548
8549       /* Parse the declarator.  */
8550       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8551                                          /*ctor_dtor_or_conv_p=*/NULL,
8552                                          /*parenthesized_p=*/NULL,
8553                                          /*member_p=*/false);
8554       /* Parse the attributes.  */
8555       attributes = cp_parser_attributes_opt (parser);
8556       /* Parse the asm-specification.  */
8557       asm_specification = cp_parser_asm_specification_opt (parser);
8558       /* If the next token is not an `=' or '{', then we might still be
8559          looking at an expression.  For example:
8560
8561            if (A(a).x)
8562
8563          looks like a decl-specifier-seq and a declarator -- but then
8564          there is no `=', so this is an expression.  */
8565       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8566           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8567         cp_parser_simulate_error (parser);
8568         
8569       /* If we did see an `=' or '{', then we are looking at a declaration
8570          for sure.  */
8571       if (cp_parser_parse_definitely (parser))
8572         {
8573           tree pushed_scope;
8574           bool non_constant_p;
8575           bool flags = LOOKUP_ONLYCONVERTING;
8576
8577           /* Create the declaration.  */
8578           decl = start_decl (declarator, &type_specifiers,
8579                              /*initialized_p=*/true,
8580                              attributes, /*prefix_attributes=*/NULL_TREE,
8581                              &pushed_scope);
8582
8583           /* Parse the initializer.  */
8584           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8585             {
8586               initializer = cp_parser_braced_list (parser, &non_constant_p);
8587               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8588               flags = 0;
8589             }
8590           else
8591             {
8592               /* Consume the `='.  */
8593               cp_parser_require (parser, CPP_EQ, RT_EQ);
8594               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8595             }
8596           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8597             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8598
8599           if (!non_constant_p)
8600             initializer = fold_non_dependent_expr (initializer);
8601
8602           /* Process the initializer.  */
8603           cp_finish_decl (decl,
8604                           initializer, !non_constant_p,
8605                           asm_specification,
8606                           flags);
8607
8608           if (pushed_scope)
8609             pop_scope (pushed_scope);
8610
8611           return convert_from_reference (decl);
8612         }
8613     }
8614   /* If we didn't even get past the declarator successfully, we are
8615      definitely not looking at a declaration.  */
8616   else
8617     cp_parser_abort_tentative_parse (parser);
8618
8619   /* Otherwise, we are looking at an expression.  */
8620   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8621 }
8622
8623 /* Parses a traditional for-statement until the closing ')', not included. */
8624
8625 static tree
8626 cp_parser_c_for (cp_parser *parser)
8627 {
8628   /* Normal for loop */
8629   tree stmt;
8630   tree condition = NULL_TREE;
8631   tree expression = NULL_TREE;
8632
8633   /* Begin the for-statement.  */
8634   stmt = begin_for_stmt ();
8635
8636   /* Parse the initialization.  */
8637   cp_parser_for_init_statement (parser);
8638   finish_for_init_stmt (stmt);
8639
8640   /* If there's a condition, process it.  */
8641   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8642     condition = cp_parser_condition (parser);
8643   finish_for_cond (condition, stmt);
8644   /* Look for the `;'.  */
8645   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8646
8647   /* If there's an expression, process it.  */
8648   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8649     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8650   finish_for_expr (expression, stmt);
8651
8652   return stmt;
8653 }
8654
8655 /* Tries to parse a range-based for-statement:
8656
8657   range-based-for:
8658     type-specifier-seq declarator : expression
8659
8660   If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8661   expression. Note that the *DECL is returned unfinished, so
8662   later you should call cp_finish_decl().
8663
8664   Returns TRUE iff a range-based for is parsed. */
8665
8666 static tree
8667 cp_parser_range_for (cp_parser *parser)
8668 {
8669   tree stmt, range_decl, range_expr;
8670   cp_decl_specifier_seq type_specifiers;
8671   cp_declarator *declarator;
8672   const char *saved_message;
8673   tree attributes, pushed_scope;
8674
8675   cp_parser_parse_tentatively (parser);
8676   /* New types are not allowed in the type-specifier-seq for a
8677      range-based for loop.  */
8678   saved_message = parser->type_definition_forbidden_message;
8679   parser->type_definition_forbidden_message
8680     = G_("types may not be defined in range-based for loops");
8681   /* Parse the type-specifier-seq.  */
8682   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8683                                 /*is_trailing_return=*/false,
8684                                 &type_specifiers);
8685   /* Restore the saved message.  */
8686   parser->type_definition_forbidden_message = saved_message;
8687   /* If all is well, we might be looking at a declaration.  */
8688   if (cp_parser_error_occurred (parser))
8689     {
8690       cp_parser_abort_tentative_parse (parser);
8691       return NULL_TREE;
8692     }
8693   /* Parse the declarator.  */
8694   declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8695                                      /*ctor_dtor_or_conv_p=*/NULL,
8696                                      /*parenthesized_p=*/NULL,
8697                                      /*member_p=*/false);
8698   /* Parse the attributes.  */
8699   attributes = cp_parser_attributes_opt (parser);
8700   /* The next token should be `:'. */
8701   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8702     cp_parser_simulate_error (parser);
8703
8704   /* Check if it is a range-based for */
8705   if (!cp_parser_parse_definitely (parser))
8706     return NULL_TREE;
8707
8708   cp_parser_require (parser, CPP_COLON, RT_COLON);
8709   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8710     {
8711       bool expr_non_constant_p;
8712       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8713     }
8714   else
8715     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8716
8717   /* If in template, STMT is converted to a normal for-statements
8718      at instantiation. If not, it is done just ahead. */
8719   if (processing_template_decl)
8720     stmt = begin_range_for_stmt ();
8721   else
8722     stmt = begin_for_stmt ();
8723
8724   /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8725   range_decl = start_decl (declarator, &type_specifiers,
8726                            /*initialized_p=*/SD_INITIALIZED,
8727                            attributes, /*prefix_attributes=*/NULL_TREE,
8728                            &pushed_scope);
8729   /* No scope allowed here */
8730   pop_scope (pushed_scope);
8731
8732   if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8733     finish_range_for_decl (stmt, range_decl, range_expr);
8734   else
8735     /* Convert the range-based for loop into a normal for-statement. */
8736     stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8737
8738   return stmt;
8739 }
8740
8741 /* Converts a range-based for-statement into a normal
8742    for-statement, as per the definition.
8743
8744       for (RANGE_DECL : RANGE_EXPR)
8745         BLOCK
8746
8747    should be equivalent to:
8748
8749       {
8750         auto &&__range = RANGE_EXPR;
8751         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8752               __begin != __end;
8753               ++__begin)
8754           {
8755               RANGE_DECL = *__begin;
8756               BLOCK
8757           }
8758       }
8759
8760    If RANGE_EXPR is an array:
8761        BEGIN_EXPR = __range
8762        END_EXPR = __range + ARRAY_SIZE(__range)
8763    Else:
8764         BEGIN_EXPR = begin(__range)
8765         END_EXPR = end(__range);
8766
8767    When calling begin()/end() we must use argument dependent
8768    lookup, but always considering 'std' as an associated namespace.  */
8769
8770 tree
8771 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8772 {
8773   tree range_type, range_temp;
8774   tree begin, end;
8775   tree iter_type, begin_expr, end_expr;
8776   tree condition, expression;
8777
8778   /* Find out the type deduced by the declaration
8779    * `auto &&__range = range_expr' */
8780   range_type = cp_build_reference_type (make_auto (), true);
8781   range_type = do_auto_deduction (range_type, range_expr,
8782                                   type_uses_auto (range_type));
8783
8784   /* Create the __range variable */
8785   range_temp = build_decl (input_location, VAR_DECL,
8786                            get_identifier ("__for_range"), range_type);
8787   TREE_USED (range_temp) = 1;
8788   DECL_ARTIFICIAL (range_temp) = 1;
8789   pushdecl (range_temp);
8790   cp_finish_decl (range_temp, range_expr,
8791                   /*is_constant_init*/false, NULL_TREE,
8792                   LOOKUP_ONLYCONVERTING);
8793
8794   range_temp = convert_from_reference (range_temp);
8795
8796   if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8797     {
8798       /* If RANGE_TEMP is an array we will use pointer arithmetic */
8799       iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8800       begin_expr = range_temp;
8801       end_expr
8802         = build_binary_op (input_location, PLUS_EXPR,
8803                            range_temp,
8804                            array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8805     }
8806   else
8807     {
8808       /* If it is not an array, we must call begin(__range)/end__range() */
8809       VEC(tree,gc) *vec;
8810
8811       begin_expr = get_identifier ("begin");
8812       vec = make_tree_vector ();
8813       VEC_safe_push (tree, gc, vec, range_temp);
8814       begin_expr = perform_koenig_lookup (begin_expr, vec,
8815                                           /*include_std=*/true);
8816       begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8817                                      tf_warning_or_error);
8818       release_tree_vector (vec);
8819
8820       end_expr = get_identifier ("end");
8821       vec = make_tree_vector ();
8822       VEC_safe_push (tree, gc, vec, range_temp);
8823       end_expr = perform_koenig_lookup (end_expr, vec,
8824                                         /*include_std=*/true);
8825       end_expr = finish_call_expr (end_expr, &vec, false, true,
8826                                    tf_warning_or_error);
8827       release_tree_vector (vec);
8828
8829       /* The unqualified type of the __begin and __end temporaries should
8830       * be the same as required by the multiple auto declaration */
8831       iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8832       if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8833         error ("inconsistent begin/end types in range-based for: %qT and %qT",
8834                TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8835     }
8836
8837   /* The new for initialization statement */
8838   begin = build_decl (input_location, VAR_DECL,
8839                       get_identifier ("__for_begin"), iter_type);
8840   TREE_USED (begin) = 1;
8841   DECL_ARTIFICIAL (begin) = 1;
8842   pushdecl (begin);
8843   cp_finish_decl (begin, begin_expr,
8844                   /*is_constant_init*/false, NULL_TREE,
8845                   LOOKUP_ONLYCONVERTING);
8846
8847   end = build_decl (input_location, VAR_DECL,
8848                     get_identifier ("__for_end"), iter_type);
8849   TREE_USED (end) = 1;
8850   DECL_ARTIFICIAL (end) = 1;
8851   pushdecl (end);
8852   cp_finish_decl (end, end_expr,
8853                   /*is_constant_init*/false, NULL_TREE,
8854                   LOOKUP_ONLYCONVERTING);
8855
8856   finish_for_init_stmt (statement);
8857
8858 /* The new for condition */
8859   condition = build_x_binary_op (NE_EXPR,
8860                                  begin, ERROR_MARK,
8861                                  end, ERROR_MARK,
8862                                  NULL, tf_warning_or_error);
8863   finish_for_cond (condition, statement);
8864
8865   /* The new increment expression */
8866   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8867   finish_for_expr (expression, statement);
8868
8869   /* The declaration is initialized with *__begin inside the loop body */
8870   cp_finish_decl (range_decl,
8871                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8872                   /*is_constant_init*/false, NULL_TREE,
8873                   LOOKUP_ONLYCONVERTING);
8874
8875   return statement;
8876 }
8877
8878
8879 /* Parse an iteration-statement.
8880
8881    iteration-statement:
8882      while ( condition ) statement
8883      do statement while ( expression ) ;
8884      for ( for-init-statement condition [opt] ; expression [opt] )
8885        statement
8886
8887    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8888
8889 static tree
8890 cp_parser_iteration_statement (cp_parser* parser)
8891 {
8892   cp_token *token;
8893   enum rid keyword;
8894   tree statement;
8895   unsigned char in_statement;
8896
8897   /* Peek at the next token.  */
8898   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8899   if (!token)
8900     return error_mark_node;
8901
8902   /* Remember whether or not we are already within an iteration
8903      statement.  */
8904   in_statement = parser->in_statement;
8905
8906   /* See what kind of keyword it is.  */
8907   keyword = token->keyword;
8908   switch (keyword)
8909     {
8910     case RID_WHILE:
8911       {
8912         tree condition;
8913
8914         /* Begin the while-statement.  */
8915         statement = begin_while_stmt ();
8916         /* Look for the `('.  */
8917         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8918         /* Parse the condition.  */
8919         condition = cp_parser_condition (parser);
8920         finish_while_stmt_cond (condition, statement);
8921         /* Look for the `)'.  */
8922         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8923         /* Parse the dependent statement.  */
8924         parser->in_statement = IN_ITERATION_STMT;
8925         cp_parser_already_scoped_statement (parser);
8926         parser->in_statement = in_statement;
8927         /* We're done with the while-statement.  */
8928         finish_while_stmt (statement);
8929       }
8930       break;
8931
8932     case RID_DO:
8933       {
8934         tree expression;
8935
8936         /* Begin the do-statement.  */
8937         statement = begin_do_stmt ();
8938         /* Parse the body of the do-statement.  */
8939         parser->in_statement = IN_ITERATION_STMT;
8940         cp_parser_implicitly_scoped_statement (parser, NULL);
8941         parser->in_statement = in_statement;
8942         finish_do_body (statement);
8943         /* Look for the `while' keyword.  */
8944         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8945         /* Look for the `('.  */
8946         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8947         /* Parse the expression.  */
8948         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8949         /* We're done with the do-statement.  */
8950         finish_do_stmt (expression, statement);
8951         /* Look for the `)'.  */
8952         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8953         /* Look for the `;'.  */
8954         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8955       }
8956       break;
8957
8958     case RID_FOR:
8959       {
8960         /* Look for the `('.  */
8961         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8962
8963         if (cxx_dialect == cxx0x)
8964           statement = cp_parser_range_for (parser);
8965         else
8966           statement = NULL_TREE;
8967         if (statement == NULL_TREE)
8968           statement = cp_parser_c_for (parser);
8969
8970         /* Look for the `)'.  */
8971         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8972
8973         /* Parse the body of the for-statement.  */
8974         parser->in_statement = IN_ITERATION_STMT;
8975         cp_parser_already_scoped_statement (parser);
8976         parser->in_statement = in_statement;
8977
8978         /* We're done with the for-statement.  */
8979         finish_for_stmt (statement);
8980       }
8981       break;
8982
8983     default:
8984       cp_parser_error (parser, "expected iteration-statement");
8985       statement = error_mark_node;
8986       break;
8987     }
8988
8989   return statement;
8990 }
8991
8992 /* Parse a for-init-statement.
8993
8994    for-init-statement:
8995      expression-statement
8996      simple-declaration  */
8997
8998 static void
8999 cp_parser_for_init_statement (cp_parser* parser)
9000 {
9001   /* If the next token is a `;', then we have an empty
9002      expression-statement.  Grammatically, this is also a
9003      simple-declaration, but an invalid one, because it does not
9004      declare anything.  Therefore, if we did not handle this case
9005      specially, we would issue an error message about an invalid
9006      declaration.  */
9007   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9008     {
9009       /* We're going to speculatively look for a declaration, falling back
9010          to an expression, if necessary.  */
9011       cp_parser_parse_tentatively (parser);
9012       /* Parse the declaration.  */
9013       cp_parser_simple_declaration (parser,
9014                                     /*function_definition_allowed_p=*/false);
9015       /* If the tentative parse failed, then we shall need to look for an
9016          expression-statement.  */
9017       if (cp_parser_parse_definitely (parser))
9018         return;
9019     }
9020
9021   cp_parser_expression_statement (parser, NULL_TREE);
9022 }
9023
9024 /* Parse a jump-statement.
9025
9026    jump-statement:
9027      break ;
9028      continue ;
9029      return expression [opt] ;
9030      return braced-init-list ;
9031      goto identifier ;
9032
9033    GNU extension:
9034
9035    jump-statement:
9036      goto * expression ;
9037
9038    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9039
9040 static tree
9041 cp_parser_jump_statement (cp_parser* parser)
9042 {
9043   tree statement = error_mark_node;
9044   cp_token *token;
9045   enum rid keyword;
9046   unsigned char in_statement;
9047
9048   /* Peek at the next token.  */
9049   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9050   if (!token)
9051     return error_mark_node;
9052
9053   /* See what kind of keyword it is.  */
9054   keyword = token->keyword;
9055   switch (keyword)
9056     {
9057     case RID_BREAK:
9058       in_statement = parser->in_statement & ~IN_IF_STMT;      
9059       switch (in_statement)
9060         {
9061         case 0:
9062           error_at (token->location, "break statement not within loop or switch");
9063           break;
9064         default:
9065           gcc_assert ((in_statement & IN_SWITCH_STMT)
9066                       || in_statement == IN_ITERATION_STMT);
9067           statement = finish_break_stmt ();
9068           break;
9069         case IN_OMP_BLOCK:
9070           error_at (token->location, "invalid exit from OpenMP structured block");
9071           break;
9072         case IN_OMP_FOR:
9073           error_at (token->location, "break statement used with OpenMP for loop");
9074           break;
9075         }
9076       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9077       break;
9078
9079     case RID_CONTINUE:
9080       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9081         {
9082         case 0:
9083           error_at (token->location, "continue statement not within a loop");
9084           break;
9085         case IN_ITERATION_STMT:
9086         case IN_OMP_FOR:
9087           statement = finish_continue_stmt ();
9088           break;
9089         case IN_OMP_BLOCK:
9090           error_at (token->location, "invalid exit from OpenMP structured block");
9091           break;
9092         default:
9093           gcc_unreachable ();
9094         }
9095       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9096       break;
9097
9098     case RID_RETURN:
9099       {
9100         tree expr;
9101         bool expr_non_constant_p;
9102
9103         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9104           {
9105             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9106             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9107           }
9108         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9109           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9110         else
9111           /* If the next token is a `;', then there is no
9112              expression.  */
9113           expr = NULL_TREE;
9114         /* Build the return-statement.  */
9115         statement = finish_return_stmt (expr);
9116         /* Look for the final `;'.  */
9117         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9118       }
9119       break;
9120
9121     case RID_GOTO:
9122       /* Create the goto-statement.  */
9123       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9124         {
9125           /* Issue a warning about this use of a GNU extension.  */
9126           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9127           /* Consume the '*' token.  */
9128           cp_lexer_consume_token (parser->lexer);
9129           /* Parse the dependent expression.  */
9130           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9131         }
9132       else
9133         finish_goto_stmt (cp_parser_identifier (parser));
9134       /* Look for the final `;'.  */
9135       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9136       break;
9137
9138     default:
9139       cp_parser_error (parser, "expected jump-statement");
9140       break;
9141     }
9142
9143   return statement;
9144 }
9145
9146 /* Parse a declaration-statement.
9147
9148    declaration-statement:
9149      block-declaration  */
9150
9151 static void
9152 cp_parser_declaration_statement (cp_parser* parser)
9153 {
9154   void *p;
9155
9156   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9157   p = obstack_alloc (&declarator_obstack, 0);
9158
9159  /* Parse the block-declaration.  */
9160   cp_parser_block_declaration (parser, /*statement_p=*/true);
9161
9162   /* Free any declarators allocated.  */
9163   obstack_free (&declarator_obstack, p);
9164
9165   /* Finish off the statement.  */
9166   finish_stmt ();
9167 }
9168
9169 /* Some dependent statements (like `if (cond) statement'), are
9170    implicitly in their own scope.  In other words, if the statement is
9171    a single statement (as opposed to a compound-statement), it is
9172    none-the-less treated as if it were enclosed in braces.  Any
9173    declarations appearing in the dependent statement are out of scope
9174    after control passes that point.  This function parses a statement,
9175    but ensures that is in its own scope, even if it is not a
9176    compound-statement.
9177
9178    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9179    is a (possibly labeled) if statement which is not enclosed in
9180    braces and has an else clause.  This is used to implement
9181    -Wparentheses.
9182
9183    Returns the new statement.  */
9184
9185 static tree
9186 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9187 {
9188   tree statement;
9189
9190   if (if_p != NULL)
9191     *if_p = false;
9192
9193   /* Mark if () ; with a special NOP_EXPR.  */
9194   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9195     {
9196       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9197       cp_lexer_consume_token (parser->lexer);
9198       statement = add_stmt (build_empty_stmt (loc));
9199     }
9200   /* if a compound is opened, we simply parse the statement directly.  */
9201   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9202     statement = cp_parser_compound_statement (parser, NULL, false);
9203   /* If the token is not a `{', then we must take special action.  */
9204   else
9205     {
9206       /* Create a compound-statement.  */
9207       statement = begin_compound_stmt (0);
9208       /* Parse the dependent-statement.  */
9209       cp_parser_statement (parser, NULL_TREE, false, if_p);
9210       /* Finish the dummy compound-statement.  */
9211       finish_compound_stmt (statement);
9212     }
9213
9214   /* Return the statement.  */
9215   return statement;
9216 }
9217
9218 /* For some dependent statements (like `while (cond) statement'), we
9219    have already created a scope.  Therefore, even if the dependent
9220    statement is a compound-statement, we do not want to create another
9221    scope.  */
9222
9223 static void
9224 cp_parser_already_scoped_statement (cp_parser* parser)
9225 {
9226   /* If the token is a `{', then we must take special action.  */
9227   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9228     cp_parser_statement (parser, NULL_TREE, false, NULL);
9229   else
9230     {
9231       /* Avoid calling cp_parser_compound_statement, so that we
9232          don't create a new scope.  Do everything else by hand.  */
9233       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9234       /* If the next keyword is `__label__' we have a label declaration.  */
9235       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9236         cp_parser_label_declaration (parser);
9237       /* Parse an (optional) statement-seq.  */
9238       cp_parser_statement_seq_opt (parser, NULL_TREE);
9239       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9240     }
9241 }
9242
9243 /* Declarations [gram.dcl.dcl] */
9244
9245 /* Parse an optional declaration-sequence.
9246
9247    declaration-seq:
9248      declaration
9249      declaration-seq declaration  */
9250
9251 static void
9252 cp_parser_declaration_seq_opt (cp_parser* parser)
9253 {
9254   while (true)
9255     {
9256       cp_token *token;
9257
9258       token = cp_lexer_peek_token (parser->lexer);
9259
9260       if (token->type == CPP_CLOSE_BRACE
9261           || token->type == CPP_EOF
9262           || token->type == CPP_PRAGMA_EOL)
9263         break;
9264
9265       if (token->type == CPP_SEMICOLON)
9266         {
9267           /* A declaration consisting of a single semicolon is
9268              invalid.  Allow it unless we're being pedantic.  */
9269           cp_lexer_consume_token (parser->lexer);
9270           if (!in_system_header)
9271             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9272           continue;
9273         }
9274
9275       /* If we're entering or exiting a region that's implicitly
9276          extern "C", modify the lang context appropriately.  */
9277       if (!parser->implicit_extern_c && token->implicit_extern_c)
9278         {
9279           push_lang_context (lang_name_c);
9280           parser->implicit_extern_c = true;
9281         }
9282       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9283         {
9284           pop_lang_context ();
9285           parser->implicit_extern_c = false;
9286         }
9287
9288       if (token->type == CPP_PRAGMA)
9289         {
9290           /* A top-level declaration can consist solely of a #pragma.
9291              A nested declaration cannot, so this is done here and not
9292              in cp_parser_declaration.  (A #pragma at block scope is
9293              handled in cp_parser_statement.)  */
9294           cp_parser_pragma (parser, pragma_external);
9295           continue;
9296         }
9297
9298       /* Parse the declaration itself.  */
9299       cp_parser_declaration (parser);
9300     }
9301 }
9302
9303 /* Parse a declaration.
9304
9305    declaration:
9306      block-declaration
9307      function-definition
9308      template-declaration
9309      explicit-instantiation
9310      explicit-specialization
9311      linkage-specification
9312      namespace-definition
9313
9314    GNU extension:
9315
9316    declaration:
9317       __extension__ declaration */
9318
9319 static void
9320 cp_parser_declaration (cp_parser* parser)
9321 {
9322   cp_token token1;
9323   cp_token token2;
9324   int saved_pedantic;
9325   void *p;
9326   tree attributes = NULL_TREE;
9327
9328   /* Check for the `__extension__' keyword.  */
9329   if (cp_parser_extension_opt (parser, &saved_pedantic))
9330     {
9331       /* Parse the qualified declaration.  */
9332       cp_parser_declaration (parser);
9333       /* Restore the PEDANTIC flag.  */
9334       pedantic = saved_pedantic;
9335
9336       return;
9337     }
9338
9339   /* Try to figure out what kind of declaration is present.  */
9340   token1 = *cp_lexer_peek_token (parser->lexer);
9341
9342   if (token1.type != CPP_EOF)
9343     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9344   else
9345     {
9346       token2.type = CPP_EOF;
9347       token2.keyword = RID_MAX;
9348     }
9349
9350   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9351   p = obstack_alloc (&declarator_obstack, 0);
9352
9353   /* If the next token is `extern' and the following token is a string
9354      literal, then we have a linkage specification.  */
9355   if (token1.keyword == RID_EXTERN
9356       && cp_parser_is_string_literal (&token2))
9357     cp_parser_linkage_specification (parser);
9358   /* If the next token is `template', then we have either a template
9359      declaration, an explicit instantiation, or an explicit
9360      specialization.  */
9361   else if (token1.keyword == RID_TEMPLATE)
9362     {
9363       /* `template <>' indicates a template specialization.  */
9364       if (token2.type == CPP_LESS
9365           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9366         cp_parser_explicit_specialization (parser);
9367       /* `template <' indicates a template declaration.  */
9368       else if (token2.type == CPP_LESS)
9369         cp_parser_template_declaration (parser, /*member_p=*/false);
9370       /* Anything else must be an explicit instantiation.  */
9371       else
9372         cp_parser_explicit_instantiation (parser);
9373     }
9374   /* If the next token is `export', then we have a template
9375      declaration.  */
9376   else if (token1.keyword == RID_EXPORT)
9377     cp_parser_template_declaration (parser, /*member_p=*/false);
9378   /* If the next token is `extern', 'static' or 'inline' and the one
9379      after that is `template', we have a GNU extended explicit
9380      instantiation directive.  */
9381   else if (cp_parser_allow_gnu_extensions_p (parser)
9382            && (token1.keyword == RID_EXTERN
9383                || token1.keyword == RID_STATIC
9384                || token1.keyword == RID_INLINE)
9385            && token2.keyword == RID_TEMPLATE)
9386     cp_parser_explicit_instantiation (parser);
9387   /* If the next token is `namespace', check for a named or unnamed
9388      namespace definition.  */
9389   else if (token1.keyword == RID_NAMESPACE
9390            && (/* A named namespace definition.  */
9391                (token2.type == CPP_NAME
9392                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9393                     != CPP_EQ))
9394                /* An unnamed namespace definition.  */
9395                || token2.type == CPP_OPEN_BRACE
9396                || token2.keyword == RID_ATTRIBUTE))
9397     cp_parser_namespace_definition (parser);
9398   /* An inline (associated) namespace definition.  */
9399   else if (token1.keyword == RID_INLINE
9400            && token2.keyword == RID_NAMESPACE)
9401     cp_parser_namespace_definition (parser);
9402   /* Objective-C++ declaration/definition.  */
9403   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9404     cp_parser_objc_declaration (parser, NULL_TREE);
9405   else if (c_dialect_objc ()
9406            && token1.keyword == RID_ATTRIBUTE
9407            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9408     cp_parser_objc_declaration (parser, attributes);
9409   /* We must have either a block declaration or a function
9410      definition.  */
9411   else
9412     /* Try to parse a block-declaration, or a function-definition.  */
9413     cp_parser_block_declaration (parser, /*statement_p=*/false);
9414
9415   /* Free any declarators allocated.  */
9416   obstack_free (&declarator_obstack, p);
9417 }
9418
9419 /* Parse a block-declaration.
9420
9421    block-declaration:
9422      simple-declaration
9423      asm-definition
9424      namespace-alias-definition
9425      using-declaration
9426      using-directive
9427
9428    GNU Extension:
9429
9430    block-declaration:
9431      __extension__ block-declaration
9432
9433    C++0x Extension:
9434
9435    block-declaration:
9436      static_assert-declaration
9437
9438    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9439    part of a declaration-statement.  */
9440
9441 static void
9442 cp_parser_block_declaration (cp_parser *parser,
9443                              bool      statement_p)
9444 {
9445   cp_token *token1;
9446   int saved_pedantic;
9447
9448   /* Check for the `__extension__' keyword.  */
9449   if (cp_parser_extension_opt (parser, &saved_pedantic))
9450     {
9451       /* Parse the qualified declaration.  */
9452       cp_parser_block_declaration (parser, statement_p);
9453       /* Restore the PEDANTIC flag.  */
9454       pedantic = saved_pedantic;
9455
9456       return;
9457     }
9458
9459   /* Peek at the next token to figure out which kind of declaration is
9460      present.  */
9461   token1 = cp_lexer_peek_token (parser->lexer);
9462
9463   /* If the next keyword is `asm', we have an asm-definition.  */
9464   if (token1->keyword == RID_ASM)
9465     {
9466       if (statement_p)
9467         cp_parser_commit_to_tentative_parse (parser);
9468       cp_parser_asm_definition (parser);
9469     }
9470   /* If the next keyword is `namespace', we have a
9471      namespace-alias-definition.  */
9472   else if (token1->keyword == RID_NAMESPACE)
9473     cp_parser_namespace_alias_definition (parser);
9474   /* If the next keyword is `using', we have either a
9475      using-declaration or a using-directive.  */
9476   else if (token1->keyword == RID_USING)
9477     {
9478       cp_token *token2;
9479
9480       if (statement_p)
9481         cp_parser_commit_to_tentative_parse (parser);
9482       /* If the token after `using' is `namespace', then we have a
9483          using-directive.  */
9484       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9485       if (token2->keyword == RID_NAMESPACE)
9486         cp_parser_using_directive (parser);
9487       /* Otherwise, it's a using-declaration.  */
9488       else
9489         cp_parser_using_declaration (parser,
9490                                      /*access_declaration_p=*/false);
9491     }
9492   /* If the next keyword is `__label__' we have a misplaced label
9493      declaration.  */
9494   else if (token1->keyword == RID_LABEL)
9495     {
9496       cp_lexer_consume_token (parser->lexer);
9497       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9498       cp_parser_skip_to_end_of_statement (parser);
9499       /* If the next token is now a `;', consume it.  */
9500       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9501         cp_lexer_consume_token (parser->lexer);
9502     }
9503   /* If the next token is `static_assert' we have a static assertion.  */
9504   else if (token1->keyword == RID_STATIC_ASSERT)
9505     cp_parser_static_assert (parser, /*member_p=*/false);
9506   /* Anything else must be a simple-declaration.  */
9507   else
9508     cp_parser_simple_declaration (parser, !statement_p);
9509 }
9510
9511 /* Parse a simple-declaration.
9512
9513    simple-declaration:
9514      decl-specifier-seq [opt] init-declarator-list [opt] ;
9515
9516    init-declarator-list:
9517      init-declarator
9518      init-declarator-list , init-declarator
9519
9520    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9521    function-definition as a simple-declaration.  */
9522
9523 static void
9524 cp_parser_simple_declaration (cp_parser* parser,
9525                               bool function_definition_allowed_p)
9526 {
9527   cp_decl_specifier_seq decl_specifiers;
9528   int declares_class_or_enum;
9529   bool saw_declarator;
9530
9531   /* Defer access checks until we know what is being declared; the
9532      checks for names appearing in the decl-specifier-seq should be
9533      done as if we were in the scope of the thing being declared.  */
9534   push_deferring_access_checks (dk_deferred);
9535
9536   /* Parse the decl-specifier-seq.  We have to keep track of whether
9537      or not the decl-specifier-seq declares a named class or
9538      enumeration type, since that is the only case in which the
9539      init-declarator-list is allowed to be empty.
9540
9541      [dcl.dcl]
9542
9543      In a simple-declaration, the optional init-declarator-list can be
9544      omitted only when declaring a class or enumeration, that is when
9545      the decl-specifier-seq contains either a class-specifier, an
9546      elaborated-type-specifier, or an enum-specifier.  */
9547   cp_parser_decl_specifier_seq (parser,
9548                                 CP_PARSER_FLAGS_OPTIONAL,
9549                                 &decl_specifiers,
9550                                 &declares_class_or_enum);
9551   /* We no longer need to defer access checks.  */
9552   stop_deferring_access_checks ();
9553
9554   /* In a block scope, a valid declaration must always have a
9555      decl-specifier-seq.  By not trying to parse declarators, we can
9556      resolve the declaration/expression ambiguity more quickly.  */
9557   if (!function_definition_allowed_p
9558       && !decl_specifiers.any_specifiers_p)
9559     {
9560       cp_parser_error (parser, "expected declaration");
9561       goto done;
9562     }
9563
9564   /* If the next two tokens are both identifiers, the code is
9565      erroneous. The usual cause of this situation is code like:
9566
9567        T t;
9568
9569      where "T" should name a type -- but does not.  */
9570   if (!decl_specifiers.any_type_specifiers_p
9571       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9572     {
9573       /* If parsing tentatively, we should commit; we really are
9574          looking at a declaration.  */
9575       cp_parser_commit_to_tentative_parse (parser);
9576       /* Give up.  */
9577       goto done;
9578     }
9579
9580   /* If we have seen at least one decl-specifier, and the next token
9581      is not a parenthesis, then we must be looking at a declaration.
9582      (After "int (" we might be looking at a functional cast.)  */
9583   if (decl_specifiers.any_specifiers_p
9584       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9585       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9586       && !cp_parser_error_occurred (parser))
9587     cp_parser_commit_to_tentative_parse (parser);
9588
9589   /* Keep going until we hit the `;' at the end of the simple
9590      declaration.  */
9591   saw_declarator = false;
9592   while (cp_lexer_next_token_is_not (parser->lexer,
9593                                      CPP_SEMICOLON))
9594     {
9595       cp_token *token;
9596       bool function_definition_p;
9597       tree decl;
9598
9599       if (saw_declarator)
9600         {
9601           /* If we are processing next declarator, coma is expected */
9602           token = cp_lexer_peek_token (parser->lexer);
9603           gcc_assert (token->type == CPP_COMMA);
9604           cp_lexer_consume_token (parser->lexer);
9605         }
9606       else
9607         saw_declarator = true;
9608
9609       /* Parse the init-declarator.  */
9610       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9611                                         /*checks=*/NULL,
9612                                         function_definition_allowed_p,
9613                                         /*member_p=*/false,
9614                                         declares_class_or_enum,
9615                                         &function_definition_p);
9616       /* If an error occurred while parsing tentatively, exit quickly.
9617          (That usually happens when in the body of a function; each
9618          statement is treated as a declaration-statement until proven
9619          otherwise.)  */
9620       if (cp_parser_error_occurred (parser))
9621         goto done;
9622       /* Handle function definitions specially.  */
9623       if (function_definition_p)
9624         {
9625           /* If the next token is a `,', then we are probably
9626              processing something like:
9627
9628                void f() {}, *p;
9629
9630              which is erroneous.  */
9631           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9632             {
9633               cp_token *token = cp_lexer_peek_token (parser->lexer);
9634               error_at (token->location,
9635                         "mixing"
9636                         " declarations and function-definitions is forbidden");
9637             }
9638           /* Otherwise, we're done with the list of declarators.  */
9639           else
9640             {
9641               pop_deferring_access_checks ();
9642               return;
9643             }
9644         }
9645       /* The next token should be either a `,' or a `;'.  */
9646       token = cp_lexer_peek_token (parser->lexer);
9647       /* If it's a `,', there are more declarators to come.  */
9648       if (token->type == CPP_COMMA)
9649         /* will be consumed next time around */;
9650       /* If it's a `;', we are done.  */
9651       else if (token->type == CPP_SEMICOLON)
9652         break;
9653       /* Anything else is an error.  */
9654       else
9655         {
9656           /* If we have already issued an error message we don't need
9657              to issue another one.  */
9658           if (decl != error_mark_node
9659               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9660             cp_parser_error (parser, "expected %<,%> or %<;%>");
9661           /* Skip tokens until we reach the end of the statement.  */
9662           cp_parser_skip_to_end_of_statement (parser);
9663           /* If the next token is now a `;', consume it.  */
9664           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9665             cp_lexer_consume_token (parser->lexer);
9666           goto done;
9667         }
9668       /* After the first time around, a function-definition is not
9669          allowed -- even if it was OK at first.  For example:
9670
9671            int i, f() {}
9672
9673          is not valid.  */
9674       function_definition_allowed_p = false;
9675     }
9676
9677   /* Issue an error message if no declarators are present, and the
9678      decl-specifier-seq does not itself declare a class or
9679      enumeration.  */
9680   if (!saw_declarator)
9681     {
9682       if (cp_parser_declares_only_class_p (parser))
9683         shadow_tag (&decl_specifiers);
9684       /* Perform any deferred access checks.  */
9685       perform_deferred_access_checks ();
9686     }
9687
9688   /* Consume the `;'.  */
9689   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9690
9691  done:
9692   pop_deferring_access_checks ();
9693 }
9694
9695 /* Parse a decl-specifier-seq.
9696
9697    decl-specifier-seq:
9698      decl-specifier-seq [opt] decl-specifier
9699
9700    decl-specifier:
9701      storage-class-specifier
9702      type-specifier
9703      function-specifier
9704      friend
9705      typedef
9706
9707    GNU Extension:
9708
9709    decl-specifier:
9710      attributes
9711
9712    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9713
9714    The parser flags FLAGS is used to control type-specifier parsing.
9715
9716    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9717    flags:
9718
9719      1: one of the decl-specifiers is an elaborated-type-specifier
9720         (i.e., a type declaration)
9721      2: one of the decl-specifiers is an enum-specifier or a
9722         class-specifier (i.e., a type definition)
9723
9724    */
9725
9726 static void
9727 cp_parser_decl_specifier_seq (cp_parser* parser,
9728                               cp_parser_flags flags,
9729                               cp_decl_specifier_seq *decl_specs,
9730                               int* declares_class_or_enum)
9731 {
9732   bool constructor_possible_p = !parser->in_declarator_p;
9733   cp_token *start_token = NULL;
9734
9735   /* Clear DECL_SPECS.  */
9736   clear_decl_specs (decl_specs);
9737
9738   /* Assume no class or enumeration type is declared.  */
9739   *declares_class_or_enum = 0;
9740
9741   /* Keep reading specifiers until there are no more to read.  */
9742   while (true)
9743     {
9744       bool constructor_p;
9745       bool found_decl_spec;
9746       cp_token *token;
9747
9748       /* Peek at the next token.  */
9749       token = cp_lexer_peek_token (parser->lexer);
9750
9751       /* Save the first token of the decl spec list for error
9752          reporting.  */
9753       if (!start_token)
9754         start_token = token;
9755       /* Handle attributes.  */
9756       if (token->keyword == RID_ATTRIBUTE)
9757         {
9758           /* Parse the attributes.  */
9759           decl_specs->attributes
9760             = chainon (decl_specs->attributes,
9761                        cp_parser_attributes_opt (parser));
9762           continue;
9763         }
9764       /* Assume we will find a decl-specifier keyword.  */
9765       found_decl_spec = true;
9766       /* If the next token is an appropriate keyword, we can simply
9767          add it to the list.  */
9768       switch (token->keyword)
9769         {
9770           /* decl-specifier:
9771                friend
9772                constexpr */
9773         case RID_FRIEND:
9774           if (!at_class_scope_p ())
9775             {
9776               error_at (token->location, "%<friend%> used outside of class");
9777               cp_lexer_purge_token (parser->lexer);
9778             }
9779           else
9780             {
9781               ++decl_specs->specs[(int) ds_friend];
9782               /* Consume the token.  */
9783               cp_lexer_consume_token (parser->lexer);
9784             }
9785           break;
9786
9787         case RID_CONSTEXPR:
9788           ++decl_specs->specs[(int) ds_constexpr];
9789           cp_lexer_consume_token (parser->lexer);
9790           break;
9791
9792           /* function-specifier:
9793                inline
9794                virtual
9795                explicit  */
9796         case RID_INLINE:
9797         case RID_VIRTUAL:
9798         case RID_EXPLICIT:
9799           cp_parser_function_specifier_opt (parser, decl_specs);
9800           break;
9801
9802           /* decl-specifier:
9803                typedef  */
9804         case RID_TYPEDEF:
9805           ++decl_specs->specs[(int) ds_typedef];
9806           /* Consume the token.  */
9807           cp_lexer_consume_token (parser->lexer);
9808           /* A constructor declarator cannot appear in a typedef.  */
9809           constructor_possible_p = false;
9810           /* The "typedef" keyword can only occur in a declaration; we
9811              may as well commit at this point.  */
9812           cp_parser_commit_to_tentative_parse (parser);
9813
9814           if (decl_specs->storage_class != sc_none)
9815             decl_specs->conflicting_specifiers_p = true;
9816           break;
9817
9818           /* storage-class-specifier:
9819                auto
9820                register
9821                static
9822                extern
9823                mutable
9824
9825              GNU Extension:
9826                thread  */
9827         case RID_AUTO:
9828           if (cxx_dialect == cxx98) 
9829             {
9830               /* Consume the token.  */
9831               cp_lexer_consume_token (parser->lexer);
9832
9833               /* Complain about `auto' as a storage specifier, if
9834                  we're complaining about C++0x compatibility.  */
9835               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9836                           " will change meaning in C++0x; please remove it");
9837
9838               /* Set the storage class anyway.  */
9839               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9840                                            token->location);
9841             }
9842           else
9843             /* C++0x auto type-specifier.  */
9844             found_decl_spec = false;
9845           break;
9846
9847         case RID_REGISTER:
9848         case RID_STATIC:
9849         case RID_EXTERN:
9850         case RID_MUTABLE:
9851           /* Consume the token.  */
9852           cp_lexer_consume_token (parser->lexer);
9853           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9854                                        token->location);
9855           break;
9856         case RID_THREAD:
9857           /* Consume the token.  */
9858           cp_lexer_consume_token (parser->lexer);
9859           ++decl_specs->specs[(int) ds_thread];
9860           break;
9861
9862         default:
9863           /* We did not yet find a decl-specifier yet.  */
9864           found_decl_spec = false;
9865           break;
9866         }
9867
9868       if (found_decl_spec
9869           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9870           && token->keyword != RID_CONSTEXPR)
9871         error ("decl-specifier invalid in condition");
9872
9873       /* Constructors are a special case.  The `S' in `S()' is not a
9874          decl-specifier; it is the beginning of the declarator.  */
9875       constructor_p
9876         = (!found_decl_spec
9877            && constructor_possible_p
9878            && (cp_parser_constructor_declarator_p
9879                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9880
9881       /* If we don't have a DECL_SPEC yet, then we must be looking at
9882          a type-specifier.  */
9883       if (!found_decl_spec && !constructor_p)
9884         {
9885           int decl_spec_declares_class_or_enum;
9886           bool is_cv_qualifier;
9887           tree type_spec;
9888
9889           type_spec
9890             = cp_parser_type_specifier (parser, flags,
9891                                         decl_specs,
9892                                         /*is_declaration=*/true,
9893                                         &decl_spec_declares_class_or_enum,
9894                                         &is_cv_qualifier);
9895           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9896
9897           /* If this type-specifier referenced a user-defined type
9898              (a typedef, class-name, etc.), then we can't allow any
9899              more such type-specifiers henceforth.
9900
9901              [dcl.spec]
9902
9903              The longest sequence of decl-specifiers that could
9904              possibly be a type name is taken as the
9905              decl-specifier-seq of a declaration.  The sequence shall
9906              be self-consistent as described below.
9907
9908              [dcl.type]
9909
9910              As a general rule, at most one type-specifier is allowed
9911              in the complete decl-specifier-seq of a declaration.  The
9912              only exceptions are the following:
9913
9914              -- const or volatile can be combined with any other
9915                 type-specifier.
9916
9917              -- signed or unsigned can be combined with char, long,
9918                 short, or int.
9919
9920              -- ..
9921
9922              Example:
9923
9924                typedef char* Pc;
9925                void g (const int Pc);
9926
9927              Here, Pc is *not* part of the decl-specifier seq; it's
9928              the declarator.  Therefore, once we see a type-specifier
9929              (other than a cv-qualifier), we forbid any additional
9930              user-defined types.  We *do* still allow things like `int
9931              int' to be considered a decl-specifier-seq, and issue the
9932              error message later.  */
9933           if (type_spec && !is_cv_qualifier)
9934             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9935           /* A constructor declarator cannot follow a type-specifier.  */
9936           if (type_spec)
9937             {
9938               constructor_possible_p = false;
9939               found_decl_spec = true;
9940               if (!is_cv_qualifier)
9941                 decl_specs->any_type_specifiers_p = true;
9942             }
9943         }
9944
9945       /* If we still do not have a DECL_SPEC, then there are no more
9946          decl-specifiers.  */
9947       if (!found_decl_spec)
9948         break;
9949
9950       decl_specs->any_specifiers_p = true;
9951       /* After we see one decl-specifier, further decl-specifiers are
9952          always optional.  */
9953       flags |= CP_PARSER_FLAGS_OPTIONAL;
9954     }
9955
9956   cp_parser_check_decl_spec (decl_specs, start_token->location);
9957
9958   /* Don't allow a friend specifier with a class definition.  */
9959   if (decl_specs->specs[(int) ds_friend] != 0
9960       && (*declares_class_or_enum & 2))
9961     error_at (start_token->location,
9962               "class definition may not be declared a friend");
9963 }
9964
9965 /* Parse an (optional) storage-class-specifier.
9966
9967    storage-class-specifier:
9968      auto
9969      register
9970      static
9971      extern
9972      mutable
9973
9974    GNU Extension:
9975
9976    storage-class-specifier:
9977      thread
9978
9979    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9980
9981 static tree
9982 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9983 {
9984   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9985     {
9986     case RID_AUTO:
9987       if (cxx_dialect != cxx98)
9988         return NULL_TREE;
9989       /* Fall through for C++98.  */
9990
9991     case RID_REGISTER:
9992     case RID_STATIC:
9993     case RID_EXTERN:
9994     case RID_MUTABLE:
9995     case RID_THREAD:
9996       /* Consume the token.  */
9997       return cp_lexer_consume_token (parser->lexer)->u.value;
9998
9999     default:
10000       return NULL_TREE;
10001     }
10002 }
10003
10004 /* Parse an (optional) function-specifier.
10005
10006    function-specifier:
10007      inline
10008      virtual
10009      explicit
10010
10011    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10012    Updates DECL_SPECS, if it is non-NULL.  */
10013
10014 static tree
10015 cp_parser_function_specifier_opt (cp_parser* parser,
10016                                   cp_decl_specifier_seq *decl_specs)
10017 {
10018   cp_token *token = cp_lexer_peek_token (parser->lexer);
10019   switch (token->keyword)
10020     {
10021     case RID_INLINE:
10022       if (decl_specs)
10023         ++decl_specs->specs[(int) ds_inline];
10024       break;
10025
10026     case RID_VIRTUAL:
10027       /* 14.5.2.3 [temp.mem]
10028
10029          A member function template shall not be virtual.  */
10030       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10031         error_at (token->location, "templates may not be %<virtual%>");
10032       else if (decl_specs)
10033         ++decl_specs->specs[(int) ds_virtual];
10034       break;
10035
10036     case RID_EXPLICIT:
10037       if (decl_specs)
10038         ++decl_specs->specs[(int) ds_explicit];
10039       break;
10040
10041     default:
10042       return NULL_TREE;
10043     }
10044
10045   /* Consume the token.  */
10046   return cp_lexer_consume_token (parser->lexer)->u.value;
10047 }
10048
10049 /* Parse a linkage-specification.
10050
10051    linkage-specification:
10052      extern string-literal { declaration-seq [opt] }
10053      extern string-literal declaration  */
10054
10055 static void
10056 cp_parser_linkage_specification (cp_parser* parser)
10057 {
10058   tree linkage;
10059
10060   /* Look for the `extern' keyword.  */
10061   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10062
10063   /* Look for the string-literal.  */
10064   linkage = cp_parser_string_literal (parser, false, false);
10065
10066   /* Transform the literal into an identifier.  If the literal is a
10067      wide-character string, or contains embedded NULs, then we can't
10068      handle it as the user wants.  */
10069   if (strlen (TREE_STRING_POINTER (linkage))
10070       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10071     {
10072       cp_parser_error (parser, "invalid linkage-specification");
10073       /* Assume C++ linkage.  */
10074       linkage = lang_name_cplusplus;
10075     }
10076   else
10077     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10078
10079   /* We're now using the new linkage.  */
10080   push_lang_context (linkage);
10081
10082   /* If the next token is a `{', then we're using the first
10083      production.  */
10084   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10085     {
10086       /* Consume the `{' token.  */
10087       cp_lexer_consume_token (parser->lexer);
10088       /* Parse the declarations.  */
10089       cp_parser_declaration_seq_opt (parser);
10090       /* Look for the closing `}'.  */
10091       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10092     }
10093   /* Otherwise, there's just one declaration.  */
10094   else
10095     {
10096       bool saved_in_unbraced_linkage_specification_p;
10097
10098       saved_in_unbraced_linkage_specification_p
10099         = parser->in_unbraced_linkage_specification_p;
10100       parser->in_unbraced_linkage_specification_p = true;
10101       cp_parser_declaration (parser);
10102       parser->in_unbraced_linkage_specification_p
10103         = saved_in_unbraced_linkage_specification_p;
10104     }
10105
10106   /* We're done with the linkage-specification.  */
10107   pop_lang_context ();
10108 }
10109
10110 /* Parse a static_assert-declaration.
10111
10112    static_assert-declaration:
10113      static_assert ( constant-expression , string-literal ) ; 
10114
10115    If MEMBER_P, this static_assert is a class member.  */
10116
10117 static void 
10118 cp_parser_static_assert(cp_parser *parser, bool member_p)
10119 {
10120   tree condition;
10121   tree message;
10122   cp_token *token;
10123   location_t saved_loc;
10124
10125   /* Peek at the `static_assert' token so we can keep track of exactly
10126      where the static assertion started.  */
10127   token = cp_lexer_peek_token (parser->lexer);
10128   saved_loc = token->location;
10129
10130   /* Look for the `static_assert' keyword.  */
10131   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10132                                   RT_STATIC_ASSERT))
10133     return;
10134
10135   /*  We know we are in a static assertion; commit to any tentative
10136       parse.  */
10137   if (cp_parser_parsing_tentatively (parser))
10138     cp_parser_commit_to_tentative_parse (parser);
10139
10140   /* Parse the `(' starting the static assertion condition.  */
10141   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10142
10143   /* Parse the constant-expression.  */
10144   condition = 
10145     cp_parser_constant_expression (parser,
10146                                    /*allow_non_constant_p=*/false,
10147                                    /*non_constant_p=*/NULL);
10148
10149   /* Parse the separating `,'.  */
10150   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10151
10152   /* Parse the string-literal message.  */
10153   message = cp_parser_string_literal (parser, 
10154                                       /*translate=*/false,
10155                                       /*wide_ok=*/true);
10156
10157   /* A `)' completes the static assertion.  */
10158   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10159     cp_parser_skip_to_closing_parenthesis (parser, 
10160                                            /*recovering=*/true, 
10161                                            /*or_comma=*/false,
10162                                            /*consume_paren=*/true);
10163
10164   /* A semicolon terminates the declaration.  */
10165   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10166
10167   /* Complete the static assertion, which may mean either processing 
10168      the static assert now or saving it for template instantiation.  */
10169   finish_static_assert (condition, message, saved_loc, member_p);
10170 }
10171
10172 /* Parse a `decltype' type. Returns the type. 
10173
10174    simple-type-specifier:
10175      decltype ( expression )  */
10176
10177 static tree
10178 cp_parser_decltype (cp_parser *parser)
10179 {
10180   tree expr;
10181   bool id_expression_or_member_access_p = false;
10182   const char *saved_message;
10183   bool saved_integral_constant_expression_p;
10184   bool saved_non_integral_constant_expression_p;
10185   cp_token *id_expr_start_token;
10186
10187   /* Look for the `decltype' token.  */
10188   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10189     return error_mark_node;
10190
10191   /* Types cannot be defined in a `decltype' expression.  Save away the
10192      old message.  */
10193   saved_message = parser->type_definition_forbidden_message;
10194
10195   /* And create the new one.  */
10196   parser->type_definition_forbidden_message
10197     = G_("types may not be defined in %<decltype%> expressions");
10198
10199   /* The restrictions on constant-expressions do not apply inside
10200      decltype expressions.  */
10201   saved_integral_constant_expression_p
10202     = parser->integral_constant_expression_p;
10203   saved_non_integral_constant_expression_p
10204     = parser->non_integral_constant_expression_p;
10205   parser->integral_constant_expression_p = false;
10206
10207   /* Do not actually evaluate the expression.  */
10208   ++cp_unevaluated_operand;
10209
10210   /* Do not warn about problems with the expression.  */
10211   ++c_inhibit_evaluation_warnings;
10212
10213   /* Parse the opening `('.  */
10214   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10215     return error_mark_node;
10216   
10217   /* First, try parsing an id-expression.  */
10218   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10219   cp_parser_parse_tentatively (parser);
10220   expr = cp_parser_id_expression (parser,
10221                                   /*template_keyword_p=*/false,
10222                                   /*check_dependency_p=*/true,
10223                                   /*template_p=*/NULL,
10224                                   /*declarator_p=*/false,
10225                                   /*optional_p=*/false);
10226
10227   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10228     {
10229       bool non_integral_constant_expression_p = false;
10230       tree id_expression = expr;
10231       cp_id_kind idk;
10232       const char *error_msg;
10233
10234       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10235         /* Lookup the name we got back from the id-expression.  */
10236         expr = cp_parser_lookup_name (parser, expr,
10237                                       none_type,
10238                                       /*is_template=*/false,
10239                                       /*is_namespace=*/false,
10240                                       /*check_dependency=*/true,
10241                                       /*ambiguous_decls=*/NULL,
10242                                       id_expr_start_token->location);
10243
10244       if (expr
10245           && expr != error_mark_node
10246           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10247           && TREE_CODE (expr) != TYPE_DECL
10248           && (TREE_CODE (expr) != BIT_NOT_EXPR
10249               || !TYPE_P (TREE_OPERAND (expr, 0)))
10250           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10251         {
10252           /* Complete lookup of the id-expression.  */
10253           expr = (finish_id_expression
10254                   (id_expression, expr, parser->scope, &idk,
10255                    /*integral_constant_expression_p=*/false,
10256                    /*allow_non_integral_constant_expression_p=*/true,
10257                    &non_integral_constant_expression_p,
10258                    /*template_p=*/false,
10259                    /*done=*/true,
10260                    /*address_p=*/false,
10261                    /*template_arg_p=*/false,
10262                    &error_msg,
10263                    id_expr_start_token->location));
10264
10265           if (expr == error_mark_node)
10266             /* We found an id-expression, but it was something that we
10267                should not have found. This is an error, not something
10268                we can recover from, so note that we found an
10269                id-expression and we'll recover as gracefully as
10270                possible.  */
10271             id_expression_or_member_access_p = true;
10272         }
10273
10274       if (expr 
10275           && expr != error_mark_node
10276           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10277         /* We have an id-expression.  */
10278         id_expression_or_member_access_p = true;
10279     }
10280
10281   if (!id_expression_or_member_access_p)
10282     {
10283       /* Abort the id-expression parse.  */
10284       cp_parser_abort_tentative_parse (parser);
10285
10286       /* Parsing tentatively, again.  */
10287       cp_parser_parse_tentatively (parser);
10288
10289       /* Parse a class member access.  */
10290       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10291                                            /*cast_p=*/false,
10292                                            /*member_access_only_p=*/true, NULL);
10293
10294       if (expr 
10295           && expr != error_mark_node
10296           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10297         /* We have an id-expression.  */
10298         id_expression_or_member_access_p = true;
10299     }
10300
10301   if (id_expression_or_member_access_p)
10302     /* We have parsed the complete id-expression or member access.  */
10303     cp_parser_parse_definitely (parser);
10304   else
10305     {
10306       bool saved_greater_than_is_operator_p;
10307
10308       /* Abort our attempt to parse an id-expression or member access
10309          expression.  */
10310       cp_parser_abort_tentative_parse (parser);
10311
10312       /* Within a parenthesized expression, a `>' token is always
10313          the greater-than operator.  */
10314       saved_greater_than_is_operator_p
10315         = parser->greater_than_is_operator_p;
10316       parser->greater_than_is_operator_p = true;
10317
10318       /* Parse a full expression.  */
10319       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10320
10321       /* The `>' token might be the end of a template-id or
10322          template-parameter-list now.  */
10323       parser->greater_than_is_operator_p
10324         = saved_greater_than_is_operator_p;
10325     }
10326
10327   /* Go back to evaluating expressions.  */
10328   --cp_unevaluated_operand;
10329   --c_inhibit_evaluation_warnings;
10330
10331   /* Restore the old message and the integral constant expression
10332      flags.  */
10333   parser->type_definition_forbidden_message = saved_message;
10334   parser->integral_constant_expression_p
10335     = saved_integral_constant_expression_p;
10336   parser->non_integral_constant_expression_p
10337     = saved_non_integral_constant_expression_p;
10338
10339   if (expr == error_mark_node)
10340     {
10341       /* Skip everything up to the closing `)'.  */
10342       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10343                                              /*consume_paren=*/true);
10344       return error_mark_node;
10345     }
10346   
10347   /* Parse to the closing `)'.  */
10348   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10349     {
10350       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10351                                              /*consume_paren=*/true);
10352       return error_mark_node;
10353     }
10354
10355   return finish_decltype_type (expr, id_expression_or_member_access_p);
10356 }
10357
10358 /* Special member functions [gram.special] */
10359
10360 /* Parse a conversion-function-id.
10361
10362    conversion-function-id:
10363      operator conversion-type-id
10364
10365    Returns an IDENTIFIER_NODE representing the operator.  */
10366
10367 static tree
10368 cp_parser_conversion_function_id (cp_parser* parser)
10369 {
10370   tree type;
10371   tree saved_scope;
10372   tree saved_qualifying_scope;
10373   tree saved_object_scope;
10374   tree pushed_scope = NULL_TREE;
10375
10376   /* Look for the `operator' token.  */
10377   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10378     return error_mark_node;
10379   /* When we parse the conversion-type-id, the current scope will be
10380      reset.  However, we need that information in able to look up the
10381      conversion function later, so we save it here.  */
10382   saved_scope = parser->scope;
10383   saved_qualifying_scope = parser->qualifying_scope;
10384   saved_object_scope = parser->object_scope;
10385   /* We must enter the scope of the class so that the names of
10386      entities declared within the class are available in the
10387      conversion-type-id.  For example, consider:
10388
10389        struct S {
10390          typedef int I;
10391          operator I();
10392        };
10393
10394        S::operator I() { ... }
10395
10396      In order to see that `I' is a type-name in the definition, we
10397      must be in the scope of `S'.  */
10398   if (saved_scope)
10399     pushed_scope = push_scope (saved_scope);
10400   /* Parse the conversion-type-id.  */
10401   type = cp_parser_conversion_type_id (parser);
10402   /* Leave the scope of the class, if any.  */
10403   if (pushed_scope)
10404     pop_scope (pushed_scope);
10405   /* Restore the saved scope.  */
10406   parser->scope = saved_scope;
10407   parser->qualifying_scope = saved_qualifying_scope;
10408   parser->object_scope = saved_object_scope;
10409   /* If the TYPE is invalid, indicate failure.  */
10410   if (type == error_mark_node)
10411     return error_mark_node;
10412   return mangle_conv_op_name_for_type (type);
10413 }
10414
10415 /* Parse a conversion-type-id:
10416
10417    conversion-type-id:
10418      type-specifier-seq conversion-declarator [opt]
10419
10420    Returns the TYPE specified.  */
10421
10422 static tree
10423 cp_parser_conversion_type_id (cp_parser* parser)
10424 {
10425   tree attributes;
10426   cp_decl_specifier_seq type_specifiers;
10427   cp_declarator *declarator;
10428   tree type_specified;
10429
10430   /* Parse the attributes.  */
10431   attributes = cp_parser_attributes_opt (parser);
10432   /* Parse the type-specifiers.  */
10433   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10434                                 /*is_trailing_return=*/false,
10435                                 &type_specifiers);
10436   /* If that didn't work, stop.  */
10437   if (type_specifiers.type == error_mark_node)
10438     return error_mark_node;
10439   /* Parse the conversion-declarator.  */
10440   declarator = cp_parser_conversion_declarator_opt (parser);
10441
10442   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10443                                     /*initialized=*/0, &attributes);
10444   if (attributes)
10445     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10446
10447   /* Don't give this error when parsing tentatively.  This happens to
10448      work because we always parse this definitively once.  */
10449   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10450       && type_uses_auto (type_specified))
10451     {
10452       error ("invalid use of %<auto%> in conversion operator");
10453       return error_mark_node;
10454     }
10455
10456   return type_specified;
10457 }
10458
10459 /* Parse an (optional) conversion-declarator.
10460
10461    conversion-declarator:
10462      ptr-operator conversion-declarator [opt]
10463
10464    */
10465
10466 static cp_declarator *
10467 cp_parser_conversion_declarator_opt (cp_parser* parser)
10468 {
10469   enum tree_code code;
10470   tree class_type;
10471   cp_cv_quals cv_quals;
10472
10473   /* We don't know if there's a ptr-operator next, or not.  */
10474   cp_parser_parse_tentatively (parser);
10475   /* Try the ptr-operator.  */
10476   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10477   /* If it worked, look for more conversion-declarators.  */
10478   if (cp_parser_parse_definitely (parser))
10479     {
10480       cp_declarator *declarator;
10481
10482       /* Parse another optional declarator.  */
10483       declarator = cp_parser_conversion_declarator_opt (parser);
10484
10485       return cp_parser_make_indirect_declarator
10486         (code, class_type, cv_quals, declarator);
10487    }
10488
10489   return NULL;
10490 }
10491
10492 /* Parse an (optional) ctor-initializer.
10493
10494    ctor-initializer:
10495      : mem-initializer-list
10496
10497    Returns TRUE iff the ctor-initializer was actually present.  */
10498
10499 static bool
10500 cp_parser_ctor_initializer_opt (cp_parser* parser)
10501 {
10502   /* If the next token is not a `:', then there is no
10503      ctor-initializer.  */
10504   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10505     {
10506       /* Do default initialization of any bases and members.  */
10507       if (DECL_CONSTRUCTOR_P (current_function_decl))
10508         finish_mem_initializers (NULL_TREE);
10509
10510       return false;
10511     }
10512
10513   /* Consume the `:' token.  */
10514   cp_lexer_consume_token (parser->lexer);
10515   /* And the mem-initializer-list.  */
10516   cp_parser_mem_initializer_list (parser);
10517
10518   return true;
10519 }
10520
10521 /* Parse a mem-initializer-list.
10522
10523    mem-initializer-list:
10524      mem-initializer ... [opt]
10525      mem-initializer ... [opt] , mem-initializer-list  */
10526
10527 static void
10528 cp_parser_mem_initializer_list (cp_parser* parser)
10529 {
10530   tree mem_initializer_list = NULL_TREE;
10531   cp_token *token = cp_lexer_peek_token (parser->lexer);
10532
10533   /* Let the semantic analysis code know that we are starting the
10534      mem-initializer-list.  */
10535   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10536     error_at (token->location,
10537               "only constructors take member initializers");
10538
10539   /* Loop through the list.  */
10540   while (true)
10541     {
10542       tree mem_initializer;
10543
10544       token = cp_lexer_peek_token (parser->lexer);
10545       /* Parse the mem-initializer.  */
10546       mem_initializer = cp_parser_mem_initializer (parser);
10547       /* If the next token is a `...', we're expanding member initializers. */
10548       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10549         {
10550           /* Consume the `...'. */
10551           cp_lexer_consume_token (parser->lexer);
10552
10553           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10554              can be expanded but members cannot. */
10555           if (mem_initializer != error_mark_node
10556               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10557             {
10558               error_at (token->location,
10559                         "cannot expand initializer for member %<%D%>",
10560                         TREE_PURPOSE (mem_initializer));
10561               mem_initializer = error_mark_node;
10562             }
10563
10564           /* Construct the pack expansion type. */
10565           if (mem_initializer != error_mark_node)
10566             mem_initializer = make_pack_expansion (mem_initializer);
10567         }
10568       /* Add it to the list, unless it was erroneous.  */
10569       if (mem_initializer != error_mark_node)
10570         {
10571           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10572           mem_initializer_list = mem_initializer;
10573         }
10574       /* If the next token is not a `,', we're done.  */
10575       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10576         break;
10577       /* Consume the `,' token.  */
10578       cp_lexer_consume_token (parser->lexer);
10579     }
10580
10581   /* Perform semantic analysis.  */
10582   if (DECL_CONSTRUCTOR_P (current_function_decl))
10583     finish_mem_initializers (mem_initializer_list);
10584 }
10585
10586 /* Parse a mem-initializer.
10587
10588    mem-initializer:
10589      mem-initializer-id ( expression-list [opt] )
10590      mem-initializer-id braced-init-list
10591
10592    GNU extension:
10593
10594    mem-initializer:
10595      ( expression-list [opt] )
10596
10597    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10598    class) or FIELD_DECL (for a non-static data member) to initialize;
10599    the TREE_VALUE is the expression-list.  An empty initialization
10600    list is represented by void_list_node.  */
10601
10602 static tree
10603 cp_parser_mem_initializer (cp_parser* parser)
10604 {
10605   tree mem_initializer_id;
10606   tree expression_list;
10607   tree member;
10608   cp_token *token = cp_lexer_peek_token (parser->lexer);
10609
10610   /* Find out what is being initialized.  */
10611   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10612     {
10613       permerror (token->location,
10614                  "anachronistic old-style base class initializer");
10615       mem_initializer_id = NULL_TREE;
10616     }
10617   else
10618     {
10619       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10620       if (mem_initializer_id == error_mark_node)
10621         return mem_initializer_id;
10622     }
10623   member = expand_member_init (mem_initializer_id);
10624   if (member && !DECL_P (member))
10625     in_base_initializer = 1;
10626
10627   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10628     {
10629       bool expr_non_constant_p;
10630       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10631       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10632       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10633       expression_list = build_tree_list (NULL_TREE, expression_list);
10634     }
10635   else
10636     {
10637       VEC(tree,gc)* vec;
10638       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10639                                                      /*cast_p=*/false,
10640                                                      /*allow_expansion_p=*/true,
10641                                                      /*non_constant_p=*/NULL);
10642       if (vec == NULL)
10643         return error_mark_node;
10644       expression_list = build_tree_list_vec (vec);
10645       release_tree_vector (vec);
10646     }
10647
10648   if (expression_list == error_mark_node)
10649     return error_mark_node;
10650   if (!expression_list)
10651     expression_list = void_type_node;
10652
10653   in_base_initializer = 0;
10654
10655   return member ? build_tree_list (member, expression_list) : error_mark_node;
10656 }
10657
10658 /* Parse a mem-initializer-id.
10659
10660    mem-initializer-id:
10661      :: [opt] nested-name-specifier [opt] class-name
10662      identifier
10663
10664    Returns a TYPE indicating the class to be initializer for the first
10665    production.  Returns an IDENTIFIER_NODE indicating the data member
10666    to be initialized for the second production.  */
10667
10668 static tree
10669 cp_parser_mem_initializer_id (cp_parser* parser)
10670 {
10671   bool global_scope_p;
10672   bool nested_name_specifier_p;
10673   bool template_p = false;
10674   tree id;
10675
10676   cp_token *token = cp_lexer_peek_token (parser->lexer);
10677
10678   /* `typename' is not allowed in this context ([temp.res]).  */
10679   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10680     {
10681       error_at (token->location, 
10682                 "keyword %<typename%> not allowed in this context (a qualified "
10683                 "member initializer is implicitly a type)");
10684       cp_lexer_consume_token (parser->lexer);
10685     }
10686   /* Look for the optional `::' operator.  */
10687   global_scope_p
10688     = (cp_parser_global_scope_opt (parser,
10689                                    /*current_scope_valid_p=*/false)
10690        != NULL_TREE);
10691   /* Look for the optional nested-name-specifier.  The simplest way to
10692      implement:
10693
10694        [temp.res]
10695
10696        The keyword `typename' is not permitted in a base-specifier or
10697        mem-initializer; in these contexts a qualified name that
10698        depends on a template-parameter is implicitly assumed to be a
10699        type name.
10700
10701      is to assume that we have seen the `typename' keyword at this
10702      point.  */
10703   nested_name_specifier_p
10704     = (cp_parser_nested_name_specifier_opt (parser,
10705                                             /*typename_keyword_p=*/true,
10706                                             /*check_dependency_p=*/true,
10707                                             /*type_p=*/true,
10708                                             /*is_declaration=*/true)
10709        != NULL_TREE);
10710   if (nested_name_specifier_p)
10711     template_p = cp_parser_optional_template_keyword (parser);
10712   /* If there is a `::' operator or a nested-name-specifier, then we
10713      are definitely looking for a class-name.  */
10714   if (global_scope_p || nested_name_specifier_p)
10715     return cp_parser_class_name (parser,
10716                                  /*typename_keyword_p=*/true,
10717                                  /*template_keyword_p=*/template_p,
10718                                  typename_type,
10719                                  /*check_dependency_p=*/true,
10720                                  /*class_head_p=*/false,
10721                                  /*is_declaration=*/true);
10722   /* Otherwise, we could also be looking for an ordinary identifier.  */
10723   cp_parser_parse_tentatively (parser);
10724   /* Try a class-name.  */
10725   id = cp_parser_class_name (parser,
10726                              /*typename_keyword_p=*/true,
10727                              /*template_keyword_p=*/false,
10728                              none_type,
10729                              /*check_dependency_p=*/true,
10730                              /*class_head_p=*/false,
10731                              /*is_declaration=*/true);
10732   /* If we found one, we're done.  */
10733   if (cp_parser_parse_definitely (parser))
10734     return id;
10735   /* Otherwise, look for an ordinary identifier.  */
10736   return cp_parser_identifier (parser);
10737 }
10738
10739 /* Overloading [gram.over] */
10740
10741 /* Parse an operator-function-id.
10742
10743    operator-function-id:
10744      operator operator
10745
10746    Returns an IDENTIFIER_NODE for the operator which is a
10747    human-readable spelling of the identifier, e.g., `operator +'.  */
10748
10749 static tree
10750 cp_parser_operator_function_id (cp_parser* parser)
10751 {
10752   /* Look for the `operator' keyword.  */
10753   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10754     return error_mark_node;
10755   /* And then the name of the operator itself.  */
10756   return cp_parser_operator (parser);
10757 }
10758
10759 /* Parse an operator.
10760
10761    operator:
10762      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10763      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10764      || ++ -- , ->* -> () []
10765
10766    GNU Extensions:
10767
10768    operator:
10769      <? >? <?= >?=
10770
10771    Returns an IDENTIFIER_NODE for the operator which is a
10772    human-readable spelling of the identifier, e.g., `operator +'.  */
10773
10774 static tree
10775 cp_parser_operator (cp_parser* parser)
10776 {
10777   tree id = NULL_TREE;
10778   cp_token *token;
10779
10780   /* Peek at the next token.  */
10781   token = cp_lexer_peek_token (parser->lexer);
10782   /* Figure out which operator we have.  */
10783   switch (token->type)
10784     {
10785     case CPP_KEYWORD:
10786       {
10787         enum tree_code op;
10788
10789         /* The keyword should be either `new' or `delete'.  */
10790         if (token->keyword == RID_NEW)
10791           op = NEW_EXPR;
10792         else if (token->keyword == RID_DELETE)
10793           op = DELETE_EXPR;
10794         else
10795           break;
10796
10797         /* Consume the `new' or `delete' token.  */
10798         cp_lexer_consume_token (parser->lexer);
10799
10800         /* Peek at the next token.  */
10801         token = cp_lexer_peek_token (parser->lexer);
10802         /* If it's a `[' token then this is the array variant of the
10803            operator.  */
10804         if (token->type == CPP_OPEN_SQUARE)
10805           {
10806             /* Consume the `[' token.  */
10807             cp_lexer_consume_token (parser->lexer);
10808             /* Look for the `]' token.  */
10809             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10810             id = ansi_opname (op == NEW_EXPR
10811                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10812           }
10813         /* Otherwise, we have the non-array variant.  */
10814         else
10815           id = ansi_opname (op);
10816
10817         return id;
10818       }
10819
10820     case CPP_PLUS:
10821       id = ansi_opname (PLUS_EXPR);
10822       break;
10823
10824     case CPP_MINUS:
10825       id = ansi_opname (MINUS_EXPR);
10826       break;
10827
10828     case CPP_MULT:
10829       id = ansi_opname (MULT_EXPR);
10830       break;
10831
10832     case CPP_DIV:
10833       id = ansi_opname (TRUNC_DIV_EXPR);
10834       break;
10835
10836     case CPP_MOD:
10837       id = ansi_opname (TRUNC_MOD_EXPR);
10838       break;
10839
10840     case CPP_XOR:
10841       id = ansi_opname (BIT_XOR_EXPR);
10842       break;
10843
10844     case CPP_AND:
10845       id = ansi_opname (BIT_AND_EXPR);
10846       break;
10847
10848     case CPP_OR:
10849       id = ansi_opname (BIT_IOR_EXPR);
10850       break;
10851
10852     case CPP_COMPL:
10853       id = ansi_opname (BIT_NOT_EXPR);
10854       break;
10855
10856     case CPP_NOT:
10857       id = ansi_opname (TRUTH_NOT_EXPR);
10858       break;
10859
10860     case CPP_EQ:
10861       id = ansi_assopname (NOP_EXPR);
10862       break;
10863
10864     case CPP_LESS:
10865       id = ansi_opname (LT_EXPR);
10866       break;
10867
10868     case CPP_GREATER:
10869       id = ansi_opname (GT_EXPR);
10870       break;
10871
10872     case CPP_PLUS_EQ:
10873       id = ansi_assopname (PLUS_EXPR);
10874       break;
10875
10876     case CPP_MINUS_EQ:
10877       id = ansi_assopname (MINUS_EXPR);
10878       break;
10879
10880     case CPP_MULT_EQ:
10881       id = ansi_assopname (MULT_EXPR);
10882       break;
10883
10884     case CPP_DIV_EQ:
10885       id = ansi_assopname (TRUNC_DIV_EXPR);
10886       break;
10887
10888     case CPP_MOD_EQ:
10889       id = ansi_assopname (TRUNC_MOD_EXPR);
10890       break;
10891
10892     case CPP_XOR_EQ:
10893       id = ansi_assopname (BIT_XOR_EXPR);
10894       break;
10895
10896     case CPP_AND_EQ:
10897       id = ansi_assopname (BIT_AND_EXPR);
10898       break;
10899
10900     case CPP_OR_EQ:
10901       id = ansi_assopname (BIT_IOR_EXPR);
10902       break;
10903
10904     case CPP_LSHIFT:
10905       id = ansi_opname (LSHIFT_EXPR);
10906       break;
10907
10908     case CPP_RSHIFT:
10909       id = ansi_opname (RSHIFT_EXPR);
10910       break;
10911
10912     case CPP_LSHIFT_EQ:
10913       id = ansi_assopname (LSHIFT_EXPR);
10914       break;
10915
10916     case CPP_RSHIFT_EQ:
10917       id = ansi_assopname (RSHIFT_EXPR);
10918       break;
10919
10920     case CPP_EQ_EQ:
10921       id = ansi_opname (EQ_EXPR);
10922       break;
10923
10924     case CPP_NOT_EQ:
10925       id = ansi_opname (NE_EXPR);
10926       break;
10927
10928     case CPP_LESS_EQ:
10929       id = ansi_opname (LE_EXPR);
10930       break;
10931
10932     case CPP_GREATER_EQ:
10933       id = ansi_opname (GE_EXPR);
10934       break;
10935
10936     case CPP_AND_AND:
10937       id = ansi_opname (TRUTH_ANDIF_EXPR);
10938       break;
10939
10940     case CPP_OR_OR:
10941       id = ansi_opname (TRUTH_ORIF_EXPR);
10942       break;
10943
10944     case CPP_PLUS_PLUS:
10945       id = ansi_opname (POSTINCREMENT_EXPR);
10946       break;
10947
10948     case CPP_MINUS_MINUS:
10949       id = ansi_opname (PREDECREMENT_EXPR);
10950       break;
10951
10952     case CPP_COMMA:
10953       id = ansi_opname (COMPOUND_EXPR);
10954       break;
10955
10956     case CPP_DEREF_STAR:
10957       id = ansi_opname (MEMBER_REF);
10958       break;
10959
10960     case CPP_DEREF:
10961       id = ansi_opname (COMPONENT_REF);
10962       break;
10963
10964     case CPP_OPEN_PAREN:
10965       /* Consume the `('.  */
10966       cp_lexer_consume_token (parser->lexer);
10967       /* Look for the matching `)'.  */
10968       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10969       return ansi_opname (CALL_EXPR);
10970
10971     case CPP_OPEN_SQUARE:
10972       /* Consume the `['.  */
10973       cp_lexer_consume_token (parser->lexer);
10974       /* Look for the matching `]'.  */
10975       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10976       return ansi_opname (ARRAY_REF);
10977
10978     default:
10979       /* Anything else is an error.  */
10980       break;
10981     }
10982
10983   /* If we have selected an identifier, we need to consume the
10984      operator token.  */
10985   if (id)
10986     cp_lexer_consume_token (parser->lexer);
10987   /* Otherwise, no valid operator name was present.  */
10988   else
10989     {
10990       cp_parser_error (parser, "expected operator");
10991       id = error_mark_node;
10992     }
10993
10994   return id;
10995 }
10996
10997 /* Parse a template-declaration.
10998
10999    template-declaration:
11000      export [opt] template < template-parameter-list > declaration
11001
11002    If MEMBER_P is TRUE, this template-declaration occurs within a
11003    class-specifier.
11004
11005    The grammar rule given by the standard isn't correct.  What
11006    is really meant is:
11007
11008    template-declaration:
11009      export [opt] template-parameter-list-seq
11010        decl-specifier-seq [opt] init-declarator [opt] ;
11011      export [opt] template-parameter-list-seq
11012        function-definition
11013
11014    template-parameter-list-seq:
11015      template-parameter-list-seq [opt]
11016      template < template-parameter-list >  */
11017
11018 static void
11019 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11020 {
11021   /* Check for `export'.  */
11022   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11023     {
11024       /* Consume the `export' token.  */
11025       cp_lexer_consume_token (parser->lexer);
11026       /* Warn that we do not support `export'.  */
11027       warning (0, "keyword %<export%> not implemented, and will be ignored");
11028     }
11029
11030   cp_parser_template_declaration_after_export (parser, member_p);
11031 }
11032
11033 /* Parse a template-parameter-list.
11034
11035    template-parameter-list:
11036      template-parameter
11037      template-parameter-list , template-parameter
11038
11039    Returns a TREE_LIST.  Each node represents a template parameter.
11040    The nodes are connected via their TREE_CHAINs.  */
11041
11042 static tree
11043 cp_parser_template_parameter_list (cp_parser* parser)
11044 {
11045   tree parameter_list = NULL_TREE;
11046
11047   begin_template_parm_list ();
11048
11049   /* The loop below parses the template parms.  We first need to know
11050      the total number of template parms to be able to compute proper
11051      canonical types of each dependent type. So after the loop, when
11052      we know the total number of template parms,
11053      end_template_parm_list computes the proper canonical types and
11054      fixes up the dependent types accordingly.  */
11055   while (true)
11056     {
11057       tree parameter;
11058       bool is_non_type;
11059       bool is_parameter_pack;
11060       location_t parm_loc;
11061
11062       /* Parse the template-parameter.  */
11063       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11064       parameter = cp_parser_template_parameter (parser, 
11065                                                 &is_non_type,
11066                                                 &is_parameter_pack);
11067       /* Add it to the list.  */
11068       if (parameter != error_mark_node)
11069         parameter_list = process_template_parm (parameter_list,
11070                                                 parm_loc,
11071                                                 parameter,
11072                                                 is_non_type,
11073                                                 is_parameter_pack,
11074                                                 0);
11075       else
11076        {
11077          tree err_parm = build_tree_list (parameter, parameter);
11078          parameter_list = chainon (parameter_list, err_parm);
11079        }
11080
11081       /* If the next token is not a `,', we're done.  */
11082       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11083         break;
11084       /* Otherwise, consume the `,' token.  */
11085       cp_lexer_consume_token (parser->lexer);
11086     }
11087
11088   return end_template_parm_list (parameter_list);
11089 }
11090
11091 /* Parse a template-parameter.
11092
11093    template-parameter:
11094      type-parameter
11095      parameter-declaration
11096
11097    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11098    the parameter.  The TREE_PURPOSE is the default value, if any.
11099    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11100    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11101    set to true iff this parameter is a parameter pack. */
11102
11103 static tree
11104 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11105                               bool *is_parameter_pack)
11106 {
11107   cp_token *token;
11108   cp_parameter_declarator *parameter_declarator;
11109   cp_declarator *id_declarator;
11110   tree parm;
11111
11112   /* Assume it is a type parameter or a template parameter.  */
11113   *is_non_type = false;
11114   /* Assume it not a parameter pack. */
11115   *is_parameter_pack = false;
11116   /* Peek at the next token.  */
11117   token = cp_lexer_peek_token (parser->lexer);
11118   /* If it is `class' or `template', we have a type-parameter.  */
11119   if (token->keyword == RID_TEMPLATE)
11120     return cp_parser_type_parameter (parser, is_parameter_pack);
11121   /* If it is `class' or `typename' we do not know yet whether it is a
11122      type parameter or a non-type parameter.  Consider:
11123
11124        template <typename T, typename T::X X> ...
11125
11126      or:
11127
11128        template <class C, class D*> ...
11129
11130      Here, the first parameter is a type parameter, and the second is
11131      a non-type parameter.  We can tell by looking at the token after
11132      the identifier -- if it is a `,', `=', or `>' then we have a type
11133      parameter.  */
11134   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11135     {
11136       /* Peek at the token after `class' or `typename'.  */
11137       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11138       /* If it's an ellipsis, we have a template type parameter
11139          pack. */
11140       if (token->type == CPP_ELLIPSIS)
11141         return cp_parser_type_parameter (parser, is_parameter_pack);
11142       /* If it's an identifier, skip it.  */
11143       if (token->type == CPP_NAME)
11144         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11145       /* Now, see if the token looks like the end of a template
11146          parameter.  */
11147       if (token->type == CPP_COMMA
11148           || token->type == CPP_EQ
11149           || token->type == CPP_GREATER)
11150         return cp_parser_type_parameter (parser, is_parameter_pack);
11151     }
11152
11153   /* Otherwise, it is a non-type parameter.
11154
11155      [temp.param]
11156
11157      When parsing a default template-argument for a non-type
11158      template-parameter, the first non-nested `>' is taken as the end
11159      of the template parameter-list rather than a greater-than
11160      operator.  */
11161   *is_non_type = true;
11162   parameter_declarator
11163      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11164                                         /*parenthesized_p=*/NULL);
11165
11166   /* If the parameter declaration is marked as a parameter pack, set
11167      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11168      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11169      grokdeclarator. */
11170   if (parameter_declarator
11171       && parameter_declarator->declarator
11172       && parameter_declarator->declarator->parameter_pack_p)
11173     {
11174       *is_parameter_pack = true;
11175       parameter_declarator->declarator->parameter_pack_p = false;
11176     }
11177
11178   /* If the next token is an ellipsis, and we don't already have it
11179      marked as a parameter pack, then we have a parameter pack (that
11180      has no declarator).  */
11181   if (!*is_parameter_pack
11182       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11183       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11184     {
11185       /* Consume the `...'.  */
11186       cp_lexer_consume_token (parser->lexer);
11187       maybe_warn_variadic_templates ();
11188       
11189       *is_parameter_pack = true;
11190     }
11191   /* We might end up with a pack expansion as the type of the non-type
11192      template parameter, in which case this is a non-type template
11193      parameter pack.  */
11194   else if (parameter_declarator
11195            && parameter_declarator->decl_specifiers.type
11196            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11197     {
11198       *is_parameter_pack = true;
11199       parameter_declarator->decl_specifiers.type = 
11200         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11201     }
11202
11203   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11204     {
11205       /* Parameter packs cannot have default arguments.  However, a
11206          user may try to do so, so we'll parse them and give an
11207          appropriate diagnostic here.  */
11208
11209       /* Consume the `='.  */
11210       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11211       cp_lexer_consume_token (parser->lexer);
11212       
11213       /* Find the name of the parameter pack.  */     
11214       id_declarator = parameter_declarator->declarator;
11215       while (id_declarator && id_declarator->kind != cdk_id)
11216         id_declarator = id_declarator->declarator;
11217       
11218       if (id_declarator && id_declarator->kind == cdk_id)
11219         error_at (start_token->location,
11220                   "template parameter pack %qD cannot have a default argument",
11221                   id_declarator->u.id.unqualified_name);
11222       else
11223         error_at (start_token->location,
11224                   "template parameter pack cannot have a default argument");
11225       
11226       /* Parse the default argument, but throw away the result.  */
11227       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11228     }
11229
11230   parm = grokdeclarator (parameter_declarator->declarator,
11231                          &parameter_declarator->decl_specifiers,
11232                          TPARM, /*initialized=*/0,
11233                          /*attrlist=*/NULL);
11234   if (parm == error_mark_node)
11235     return error_mark_node;
11236
11237   return build_tree_list (parameter_declarator->default_argument, parm);
11238 }
11239
11240 /* Parse a type-parameter.
11241
11242    type-parameter:
11243      class identifier [opt]
11244      class identifier [opt] = type-id
11245      typename identifier [opt]
11246      typename identifier [opt] = type-id
11247      template < template-parameter-list > class identifier [opt]
11248      template < template-parameter-list > class identifier [opt]
11249        = id-expression
11250
11251    GNU Extension (variadic templates):
11252
11253    type-parameter:
11254      class ... identifier [opt]
11255      typename ... identifier [opt]
11256
11257    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11258    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11259    the declaration of the parameter.
11260
11261    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11262
11263 static tree
11264 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11265 {
11266   cp_token *token;
11267   tree parameter;
11268
11269   /* Look for a keyword to tell us what kind of parameter this is.  */
11270   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11271   if (!token)
11272     return error_mark_node;
11273
11274   switch (token->keyword)
11275     {
11276     case RID_CLASS:
11277     case RID_TYPENAME:
11278       {
11279         tree identifier;
11280         tree default_argument;
11281
11282         /* If the next token is an ellipsis, we have a template
11283            argument pack. */
11284         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11285           {
11286             /* Consume the `...' token. */
11287             cp_lexer_consume_token (parser->lexer);
11288             maybe_warn_variadic_templates ();
11289
11290             *is_parameter_pack = true;
11291           }
11292
11293         /* If the next token is an identifier, then it names the
11294            parameter.  */
11295         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11296           identifier = cp_parser_identifier (parser);
11297         else
11298           identifier = NULL_TREE;
11299
11300         /* Create the parameter.  */
11301         parameter = finish_template_type_parm (class_type_node, identifier);
11302
11303         /* If the next token is an `=', we have a default argument.  */
11304         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11305           {
11306             /* Consume the `=' token.  */
11307             cp_lexer_consume_token (parser->lexer);
11308             /* Parse the default-argument.  */
11309             push_deferring_access_checks (dk_no_deferred);
11310             default_argument = cp_parser_type_id (parser);
11311
11312             /* Template parameter packs cannot have default
11313                arguments. */
11314             if (*is_parameter_pack)
11315               {
11316                 if (identifier)
11317                   error_at (token->location,
11318                             "template parameter pack %qD cannot have a "
11319                             "default argument", identifier);
11320                 else
11321                   error_at (token->location,
11322                             "template parameter packs cannot have "
11323                             "default arguments");
11324                 default_argument = NULL_TREE;
11325               }
11326             pop_deferring_access_checks ();
11327           }
11328         else
11329           default_argument = NULL_TREE;
11330
11331         /* Create the combined representation of the parameter and the
11332            default argument.  */
11333         parameter = build_tree_list (default_argument, parameter);
11334       }
11335       break;
11336
11337     case RID_TEMPLATE:
11338       {
11339         tree identifier;
11340         tree default_argument;
11341
11342         /* Look for the `<'.  */
11343         cp_parser_require (parser, CPP_LESS, RT_LESS);
11344         /* Parse the template-parameter-list.  */
11345         cp_parser_template_parameter_list (parser);
11346         /* Look for the `>'.  */
11347         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11348         /* Look for the `class' keyword.  */
11349         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11350         /* If the next token is an ellipsis, we have a template
11351            argument pack. */
11352         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11353           {
11354             /* Consume the `...' token. */
11355             cp_lexer_consume_token (parser->lexer);
11356             maybe_warn_variadic_templates ();
11357
11358             *is_parameter_pack = true;
11359           }
11360         /* If the next token is an `=', then there is a
11361            default-argument.  If the next token is a `>', we are at
11362            the end of the parameter-list.  If the next token is a `,',
11363            then we are at the end of this parameter.  */
11364         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11365             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11366             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11367           {
11368             identifier = cp_parser_identifier (parser);
11369             /* Treat invalid names as if the parameter were nameless.  */
11370             if (identifier == error_mark_node)
11371               identifier = NULL_TREE;
11372           }
11373         else
11374           identifier = NULL_TREE;
11375
11376         /* Create the template parameter.  */
11377         parameter = finish_template_template_parm (class_type_node,
11378                                                    identifier);
11379
11380         /* If the next token is an `=', then there is a
11381            default-argument.  */
11382         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11383           {
11384             bool is_template;
11385
11386             /* Consume the `='.  */
11387             cp_lexer_consume_token (parser->lexer);
11388             /* Parse the id-expression.  */
11389             push_deferring_access_checks (dk_no_deferred);
11390             /* save token before parsing the id-expression, for error
11391                reporting */
11392             token = cp_lexer_peek_token (parser->lexer);
11393             default_argument
11394               = cp_parser_id_expression (parser,
11395                                          /*template_keyword_p=*/false,
11396                                          /*check_dependency_p=*/true,
11397                                          /*template_p=*/&is_template,
11398                                          /*declarator_p=*/false,
11399                                          /*optional_p=*/false);
11400             if (TREE_CODE (default_argument) == TYPE_DECL)
11401               /* If the id-expression was a template-id that refers to
11402                  a template-class, we already have the declaration here,
11403                  so no further lookup is needed.  */
11404                  ;
11405             else
11406               /* Look up the name.  */
11407               default_argument
11408                 = cp_parser_lookup_name (parser, default_argument,
11409                                          none_type,
11410                                          /*is_template=*/is_template,
11411                                          /*is_namespace=*/false,
11412                                          /*check_dependency=*/true,
11413                                          /*ambiguous_decls=*/NULL,
11414                                          token->location);
11415             /* See if the default argument is valid.  */
11416             default_argument
11417               = check_template_template_default_arg (default_argument);
11418
11419             /* Template parameter packs cannot have default
11420                arguments. */
11421             if (*is_parameter_pack)
11422               {
11423                 if (identifier)
11424                   error_at (token->location,
11425                             "template parameter pack %qD cannot "
11426                             "have a default argument",
11427                             identifier);
11428                 else
11429                   error_at (token->location, "template parameter packs cannot "
11430                             "have default arguments");
11431                 default_argument = NULL_TREE;
11432               }
11433             pop_deferring_access_checks ();
11434           }
11435         else
11436           default_argument = NULL_TREE;
11437
11438         /* Create the combined representation of the parameter and the
11439            default argument.  */
11440         parameter = build_tree_list (default_argument, parameter);
11441       }
11442       break;
11443
11444     default:
11445       gcc_unreachable ();
11446       break;
11447     }
11448
11449   return parameter;
11450 }
11451
11452 /* Parse a template-id.
11453
11454    template-id:
11455      template-name < template-argument-list [opt] >
11456
11457    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11458    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11459    returned.  Otherwise, if the template-name names a function, or set
11460    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11461    names a class, returns a TYPE_DECL for the specialization.
11462
11463    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11464    uninstantiated templates.  */
11465
11466 static tree
11467 cp_parser_template_id (cp_parser *parser,
11468                        bool template_keyword_p,
11469                        bool check_dependency_p,
11470                        bool is_declaration)
11471 {
11472   int i;
11473   tree templ;
11474   tree arguments;
11475   tree template_id;
11476   cp_token_position start_of_id = 0;
11477   deferred_access_check *chk;
11478   VEC (deferred_access_check,gc) *access_check;
11479   cp_token *next_token = NULL, *next_token_2 = NULL;
11480   bool is_identifier;
11481
11482   /* If the next token corresponds to a template-id, there is no need
11483      to reparse it.  */
11484   next_token = cp_lexer_peek_token (parser->lexer);
11485   if (next_token->type == CPP_TEMPLATE_ID)
11486     {
11487       struct tree_check *check_value;
11488
11489       /* Get the stored value.  */
11490       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11491       /* Perform any access checks that were deferred.  */
11492       access_check = check_value->checks;
11493       if (access_check)
11494         {
11495           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11496             perform_or_defer_access_check (chk->binfo,
11497                                            chk->decl,
11498                                            chk->diag_decl);
11499         }
11500       /* Return the stored value.  */
11501       return check_value->value;
11502     }
11503
11504   /* Avoid performing name lookup if there is no possibility of
11505      finding a template-id.  */
11506   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11507       || (next_token->type == CPP_NAME
11508           && !cp_parser_nth_token_starts_template_argument_list_p
11509                (parser, 2)))
11510     {
11511       cp_parser_error (parser, "expected template-id");
11512       return error_mark_node;
11513     }
11514
11515   /* Remember where the template-id starts.  */
11516   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11517     start_of_id = cp_lexer_token_position (parser->lexer, false);
11518
11519   push_deferring_access_checks (dk_deferred);
11520
11521   /* Parse the template-name.  */
11522   is_identifier = false;
11523   templ = cp_parser_template_name (parser, template_keyword_p,
11524                                    check_dependency_p,
11525                                    is_declaration,
11526                                    &is_identifier);
11527   if (templ == error_mark_node || is_identifier)
11528     {
11529       pop_deferring_access_checks ();
11530       return templ;
11531     }
11532
11533   /* If we find the sequence `[:' after a template-name, it's probably
11534      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11535      parse correctly the argument list.  */
11536   next_token = cp_lexer_peek_token (parser->lexer);
11537   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11538   if (next_token->type == CPP_OPEN_SQUARE
11539       && next_token->flags & DIGRAPH
11540       && next_token_2->type == CPP_COLON
11541       && !(next_token_2->flags & PREV_WHITE))
11542     {
11543       cp_parser_parse_tentatively (parser);
11544       /* Change `:' into `::'.  */
11545       next_token_2->type = CPP_SCOPE;
11546       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11547          CPP_LESS.  */
11548       cp_lexer_consume_token (parser->lexer);
11549
11550       /* Parse the arguments.  */
11551       arguments = cp_parser_enclosed_template_argument_list (parser);
11552       if (!cp_parser_parse_definitely (parser))
11553         {
11554           /* If we couldn't parse an argument list, then we revert our changes
11555              and return simply an error. Maybe this is not a template-id
11556              after all.  */
11557           next_token_2->type = CPP_COLON;
11558           cp_parser_error (parser, "expected %<<%>");
11559           pop_deferring_access_checks ();
11560           return error_mark_node;
11561         }
11562       /* Otherwise, emit an error about the invalid digraph, but continue
11563          parsing because we got our argument list.  */
11564       if (permerror (next_token->location,
11565                      "%<<::%> cannot begin a template-argument list"))
11566         {
11567           static bool hint = false;
11568           inform (next_token->location,
11569                   "%<<:%> is an alternate spelling for %<[%>."
11570                   " Insert whitespace between %<<%> and %<::%>");
11571           if (!hint && !flag_permissive)
11572             {
11573               inform (next_token->location, "(if you use %<-fpermissive%>"
11574                       " G++ will accept your code)");
11575               hint = true;
11576             }
11577         }
11578     }
11579   else
11580     {
11581       /* Look for the `<' that starts the template-argument-list.  */
11582       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11583         {
11584           pop_deferring_access_checks ();
11585           return error_mark_node;
11586         }
11587       /* Parse the arguments.  */
11588       arguments = cp_parser_enclosed_template_argument_list (parser);
11589     }
11590
11591   /* Build a representation of the specialization.  */
11592   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11593     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11594   else if (DECL_CLASS_TEMPLATE_P (templ)
11595            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11596     {
11597       bool entering_scope;
11598       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11599          template (rather than some instantiation thereof) only if
11600          is not nested within some other construct.  For example, in
11601          "template <typename T> void f(T) { A<T>::", A<T> is just an
11602          instantiation of A.  */
11603       entering_scope = (template_parm_scope_p ()
11604                         && cp_lexer_next_token_is (parser->lexer,
11605                                                    CPP_SCOPE));
11606       template_id
11607         = finish_template_type (templ, arguments, entering_scope);
11608     }
11609   else
11610     {
11611       /* If it's not a class-template or a template-template, it should be
11612          a function-template.  */
11613       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11614                    || TREE_CODE (templ) == OVERLOAD
11615                    || BASELINK_P (templ)));
11616
11617       template_id = lookup_template_function (templ, arguments);
11618     }
11619
11620   /* If parsing tentatively, replace the sequence of tokens that makes
11621      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11622      should we re-parse the token stream, we will not have to repeat
11623      the effort required to do the parse, nor will we issue duplicate
11624      error messages about problems during instantiation of the
11625      template.  */
11626   if (start_of_id)
11627     {
11628       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11629
11630       /* Reset the contents of the START_OF_ID token.  */
11631       token->type = CPP_TEMPLATE_ID;
11632       /* Retrieve any deferred checks.  Do not pop this access checks yet
11633          so the memory will not be reclaimed during token replacing below.  */
11634       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11635       token->u.tree_check_value->value = template_id;
11636       token->u.tree_check_value->checks = get_deferred_access_checks ();
11637       token->keyword = RID_MAX;
11638
11639       /* Purge all subsequent tokens.  */
11640       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11641
11642       /* ??? Can we actually assume that, if template_id ==
11643          error_mark_node, we will have issued a diagnostic to the
11644          user, as opposed to simply marking the tentative parse as
11645          failed?  */
11646       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11647         error_at (token->location, "parse error in template argument list");
11648     }
11649
11650   pop_deferring_access_checks ();
11651   return template_id;
11652 }
11653
11654 /* Parse a template-name.
11655
11656    template-name:
11657      identifier
11658
11659    The standard should actually say:
11660
11661    template-name:
11662      identifier
11663      operator-function-id
11664
11665    A defect report has been filed about this issue.
11666
11667    A conversion-function-id cannot be a template name because they cannot
11668    be part of a template-id. In fact, looking at this code:
11669
11670    a.operator K<int>()
11671
11672    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11673    It is impossible to call a templated conversion-function-id with an
11674    explicit argument list, since the only allowed template parameter is
11675    the type to which it is converting.
11676
11677    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11678    `template' keyword, in a construction like:
11679
11680      T::template f<3>()
11681
11682    In that case `f' is taken to be a template-name, even though there
11683    is no way of knowing for sure.
11684
11685    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11686    name refers to a set of overloaded functions, at least one of which
11687    is a template, or an IDENTIFIER_NODE with the name of the template,
11688    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11689    names are looked up inside uninstantiated templates.  */
11690
11691 static tree
11692 cp_parser_template_name (cp_parser* parser,
11693                          bool template_keyword_p,
11694                          bool check_dependency_p,
11695                          bool is_declaration,
11696                          bool *is_identifier)
11697 {
11698   tree identifier;
11699   tree decl;
11700   tree fns;
11701   cp_token *token = cp_lexer_peek_token (parser->lexer);
11702
11703   /* If the next token is `operator', then we have either an
11704      operator-function-id or a conversion-function-id.  */
11705   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11706     {
11707       /* We don't know whether we're looking at an
11708          operator-function-id or a conversion-function-id.  */
11709       cp_parser_parse_tentatively (parser);
11710       /* Try an operator-function-id.  */
11711       identifier = cp_parser_operator_function_id (parser);
11712       /* If that didn't work, try a conversion-function-id.  */
11713       if (!cp_parser_parse_definitely (parser))
11714         {
11715           cp_parser_error (parser, "expected template-name");
11716           return error_mark_node;
11717         }
11718     }
11719   /* Look for the identifier.  */
11720   else
11721     identifier = cp_parser_identifier (parser);
11722
11723   /* If we didn't find an identifier, we don't have a template-id.  */
11724   if (identifier == error_mark_node)
11725     return error_mark_node;
11726
11727   /* If the name immediately followed the `template' keyword, then it
11728      is a template-name.  However, if the next token is not `<', then
11729      we do not treat it as a template-name, since it is not being used
11730      as part of a template-id.  This enables us to handle constructs
11731      like:
11732
11733        template <typename T> struct S { S(); };
11734        template <typename T> S<T>::S();
11735
11736      correctly.  We would treat `S' as a template -- if it were `S<T>'
11737      -- but we do not if there is no `<'.  */
11738
11739   if (processing_template_decl
11740       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11741     {
11742       /* In a declaration, in a dependent context, we pretend that the
11743          "template" keyword was present in order to improve error
11744          recovery.  For example, given:
11745
11746            template <typename T> void f(T::X<int>);
11747
11748          we want to treat "X<int>" as a template-id.  */
11749       if (is_declaration
11750           && !template_keyword_p
11751           && parser->scope && TYPE_P (parser->scope)
11752           && check_dependency_p
11753           && dependent_scope_p (parser->scope)
11754           /* Do not do this for dtors (or ctors), since they never
11755              need the template keyword before their name.  */
11756           && !constructor_name_p (identifier, parser->scope))
11757         {
11758           cp_token_position start = 0;
11759
11760           /* Explain what went wrong.  */
11761           error_at (token->location, "non-template %qD used as template",
11762                     identifier);
11763           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11764                   parser->scope, identifier);
11765           /* If parsing tentatively, find the location of the "<" token.  */
11766           if (cp_parser_simulate_error (parser))
11767             start = cp_lexer_token_position (parser->lexer, true);
11768           /* Parse the template arguments so that we can issue error
11769              messages about them.  */
11770           cp_lexer_consume_token (parser->lexer);
11771           cp_parser_enclosed_template_argument_list (parser);
11772           /* Skip tokens until we find a good place from which to
11773              continue parsing.  */
11774           cp_parser_skip_to_closing_parenthesis (parser,
11775                                                  /*recovering=*/true,
11776                                                  /*or_comma=*/true,
11777                                                  /*consume_paren=*/false);
11778           /* If parsing tentatively, permanently remove the
11779              template argument list.  That will prevent duplicate
11780              error messages from being issued about the missing
11781              "template" keyword.  */
11782           if (start)
11783             cp_lexer_purge_tokens_after (parser->lexer, start);
11784           if (is_identifier)
11785             *is_identifier = true;
11786           return identifier;
11787         }
11788
11789       /* If the "template" keyword is present, then there is generally
11790          no point in doing name-lookup, so we just return IDENTIFIER.
11791          But, if the qualifying scope is non-dependent then we can
11792          (and must) do name-lookup normally.  */
11793       if (template_keyword_p
11794           && (!parser->scope
11795               || (TYPE_P (parser->scope)
11796                   && dependent_type_p (parser->scope))))
11797         return identifier;
11798     }
11799
11800   /* Look up the name.  */
11801   decl = cp_parser_lookup_name (parser, identifier,
11802                                 none_type,
11803                                 /*is_template=*/true,
11804                                 /*is_namespace=*/false,
11805                                 check_dependency_p,
11806                                 /*ambiguous_decls=*/NULL,
11807                                 token->location);
11808
11809   /* If DECL is a template, then the name was a template-name.  */
11810   if (TREE_CODE (decl) == TEMPLATE_DECL)
11811     ;
11812   else
11813     {
11814       tree fn = NULL_TREE;
11815
11816       /* The standard does not explicitly indicate whether a name that
11817          names a set of overloaded declarations, some of which are
11818          templates, is a template-name.  However, such a name should
11819          be a template-name; otherwise, there is no way to form a
11820          template-id for the overloaded templates.  */
11821       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11822       if (TREE_CODE (fns) == OVERLOAD)
11823         for (fn = fns; fn; fn = OVL_NEXT (fn))
11824           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11825             break;
11826
11827       if (!fn)
11828         {
11829           /* The name does not name a template.  */
11830           cp_parser_error (parser, "expected template-name");
11831           return error_mark_node;
11832         }
11833     }
11834
11835   /* If DECL is dependent, and refers to a function, then just return
11836      its name; we will look it up again during template instantiation.  */
11837   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11838     {
11839       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11840       if (TYPE_P (scope) && dependent_type_p (scope))
11841         return identifier;
11842     }
11843
11844   return decl;
11845 }
11846
11847 /* Parse a template-argument-list.
11848
11849    template-argument-list:
11850      template-argument ... [opt]
11851      template-argument-list , template-argument ... [opt]
11852
11853    Returns a TREE_VEC containing the arguments.  */
11854
11855 static tree
11856 cp_parser_template_argument_list (cp_parser* parser)
11857 {
11858   tree fixed_args[10];
11859   unsigned n_args = 0;
11860   unsigned alloced = 10;
11861   tree *arg_ary = fixed_args;
11862   tree vec;
11863   bool saved_in_template_argument_list_p;
11864   bool saved_ice_p;
11865   bool saved_non_ice_p;
11866
11867   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11868   parser->in_template_argument_list_p = true;
11869   /* Even if the template-id appears in an integral
11870      constant-expression, the contents of the argument list do
11871      not.  */
11872   saved_ice_p = parser->integral_constant_expression_p;
11873   parser->integral_constant_expression_p = false;
11874   saved_non_ice_p = parser->non_integral_constant_expression_p;
11875   parser->non_integral_constant_expression_p = false;
11876   /* Parse the arguments.  */
11877   do
11878     {
11879       tree argument;
11880
11881       if (n_args)
11882         /* Consume the comma.  */
11883         cp_lexer_consume_token (parser->lexer);
11884
11885       /* Parse the template-argument.  */
11886       argument = cp_parser_template_argument (parser);
11887
11888       /* If the next token is an ellipsis, we're expanding a template
11889          argument pack. */
11890       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11891         {
11892           if (argument == error_mark_node)
11893             {
11894               cp_token *token = cp_lexer_peek_token (parser->lexer);
11895               error_at (token->location,
11896                         "expected parameter pack before %<...%>");
11897             }
11898           /* Consume the `...' token. */
11899           cp_lexer_consume_token (parser->lexer);
11900
11901           /* Make the argument into a TYPE_PACK_EXPANSION or
11902              EXPR_PACK_EXPANSION. */
11903           argument = make_pack_expansion (argument);
11904         }
11905
11906       if (n_args == alloced)
11907         {
11908           alloced *= 2;
11909
11910           if (arg_ary == fixed_args)
11911             {
11912               arg_ary = XNEWVEC (tree, alloced);
11913               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11914             }
11915           else
11916             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11917         }
11918       arg_ary[n_args++] = argument;
11919     }
11920   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11921
11922   vec = make_tree_vec (n_args);
11923
11924   while (n_args--)
11925     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11926
11927   if (arg_ary != fixed_args)
11928     free (arg_ary);
11929   parser->non_integral_constant_expression_p = saved_non_ice_p;
11930   parser->integral_constant_expression_p = saved_ice_p;
11931   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11932 #ifdef ENABLE_CHECKING
11933   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11934 #endif
11935   return vec;
11936 }
11937
11938 /* Parse a template-argument.
11939
11940    template-argument:
11941      assignment-expression
11942      type-id
11943      id-expression
11944
11945    The representation is that of an assignment-expression, type-id, or
11946    id-expression -- except that the qualified id-expression is
11947    evaluated, so that the value returned is either a DECL or an
11948    OVERLOAD.
11949
11950    Although the standard says "assignment-expression", it forbids
11951    throw-expressions or assignments in the template argument.
11952    Therefore, we use "conditional-expression" instead.  */
11953
11954 static tree
11955 cp_parser_template_argument (cp_parser* parser)
11956 {
11957   tree argument;
11958   bool template_p;
11959   bool address_p;
11960   bool maybe_type_id = false;
11961   cp_token *token = NULL, *argument_start_token = NULL;
11962   cp_id_kind idk;
11963
11964   /* There's really no way to know what we're looking at, so we just
11965      try each alternative in order.
11966
11967        [temp.arg]
11968
11969        In a template-argument, an ambiguity between a type-id and an
11970        expression is resolved to a type-id, regardless of the form of
11971        the corresponding template-parameter.
11972
11973      Therefore, we try a type-id first.  */
11974   cp_parser_parse_tentatively (parser);
11975   argument = cp_parser_template_type_arg (parser);
11976   /* If there was no error parsing the type-id but the next token is a
11977      '>>', our behavior depends on which dialect of C++ we're
11978      parsing. In C++98, we probably found a typo for '> >'. But there
11979      are type-id which are also valid expressions. For instance:
11980
11981      struct X { int operator >> (int); };
11982      template <int V> struct Foo {};
11983      Foo<X () >> 5> r;
11984
11985      Here 'X()' is a valid type-id of a function type, but the user just
11986      wanted to write the expression "X() >> 5". Thus, we remember that we
11987      found a valid type-id, but we still try to parse the argument as an
11988      expression to see what happens. 
11989
11990      In C++0x, the '>>' will be considered two separate '>'
11991      tokens.  */
11992   if (!cp_parser_error_occurred (parser)
11993       && cxx_dialect == cxx98
11994       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11995     {
11996       maybe_type_id = true;
11997       cp_parser_abort_tentative_parse (parser);
11998     }
11999   else
12000     {
12001       /* If the next token isn't a `,' or a `>', then this argument wasn't
12002       really finished. This means that the argument is not a valid
12003       type-id.  */
12004       if (!cp_parser_next_token_ends_template_argument_p (parser))
12005         cp_parser_error (parser, "expected template-argument");
12006       /* If that worked, we're done.  */
12007       if (cp_parser_parse_definitely (parser))
12008         return argument;
12009     }
12010   /* We're still not sure what the argument will be.  */
12011   cp_parser_parse_tentatively (parser);
12012   /* Try a template.  */
12013   argument_start_token = cp_lexer_peek_token (parser->lexer);
12014   argument = cp_parser_id_expression (parser,
12015                                       /*template_keyword_p=*/false,
12016                                       /*check_dependency_p=*/true,
12017                                       &template_p,
12018                                       /*declarator_p=*/false,
12019                                       /*optional_p=*/false);
12020   /* If the next token isn't a `,' or a `>', then this argument wasn't
12021      really finished.  */
12022   if (!cp_parser_next_token_ends_template_argument_p (parser))
12023     cp_parser_error (parser, "expected template-argument");
12024   if (!cp_parser_error_occurred (parser))
12025     {
12026       /* Figure out what is being referred to.  If the id-expression
12027          was for a class template specialization, then we will have a
12028          TYPE_DECL at this point.  There is no need to do name lookup
12029          at this point in that case.  */
12030       if (TREE_CODE (argument) != TYPE_DECL)
12031         argument = cp_parser_lookup_name (parser, argument,
12032                                           none_type,
12033                                           /*is_template=*/template_p,
12034                                           /*is_namespace=*/false,
12035                                           /*check_dependency=*/true,
12036                                           /*ambiguous_decls=*/NULL,
12037                                           argument_start_token->location);
12038       if (TREE_CODE (argument) != TEMPLATE_DECL
12039           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12040         cp_parser_error (parser, "expected template-name");
12041     }
12042   if (cp_parser_parse_definitely (parser))
12043     return argument;
12044   /* It must be a non-type argument.  There permitted cases are given
12045      in [temp.arg.nontype]:
12046
12047      -- an integral constant-expression of integral or enumeration
12048         type; or
12049
12050      -- the name of a non-type template-parameter; or
12051
12052      -- the name of an object or function with external linkage...
12053
12054      -- the address of an object or function with external linkage...
12055
12056      -- a pointer to member...  */
12057   /* Look for a non-type template parameter.  */
12058   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12059     {
12060       cp_parser_parse_tentatively (parser);
12061       argument = cp_parser_primary_expression (parser,
12062                                                /*address_p=*/false,
12063                                                /*cast_p=*/false,
12064                                                /*template_arg_p=*/true,
12065                                                &idk);
12066       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12067           || !cp_parser_next_token_ends_template_argument_p (parser))
12068         cp_parser_simulate_error (parser);
12069       if (cp_parser_parse_definitely (parser))
12070         return argument;
12071     }
12072
12073   /* If the next token is "&", the argument must be the address of an
12074      object or function with external linkage.  */
12075   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12076   if (address_p)
12077     cp_lexer_consume_token (parser->lexer);
12078   /* See if we might have an id-expression.  */
12079   token = cp_lexer_peek_token (parser->lexer);
12080   if (token->type == CPP_NAME
12081       || token->keyword == RID_OPERATOR
12082       || token->type == CPP_SCOPE
12083       || token->type == CPP_TEMPLATE_ID
12084       || token->type == CPP_NESTED_NAME_SPECIFIER)
12085     {
12086       cp_parser_parse_tentatively (parser);
12087       argument = cp_parser_primary_expression (parser,
12088                                                address_p,
12089                                                /*cast_p=*/false,
12090                                                /*template_arg_p=*/true,
12091                                                &idk);
12092       if (cp_parser_error_occurred (parser)
12093           || !cp_parser_next_token_ends_template_argument_p (parser))
12094         cp_parser_abort_tentative_parse (parser);
12095       else
12096         {
12097           tree probe;
12098
12099           if (TREE_CODE (argument) == INDIRECT_REF)
12100             {
12101               gcc_assert (REFERENCE_REF_P (argument));
12102               argument = TREE_OPERAND (argument, 0);
12103             }
12104
12105           /* If we're in a template, we represent a qualified-id referring
12106              to a static data member as a SCOPE_REF even if the scope isn't
12107              dependent so that we can check access control later.  */
12108           probe = argument;
12109           if (TREE_CODE (probe) == SCOPE_REF)
12110             probe = TREE_OPERAND (probe, 1);
12111           if (TREE_CODE (probe) == VAR_DECL)
12112             {
12113               /* A variable without external linkage might still be a
12114                  valid constant-expression, so no error is issued here
12115                  if the external-linkage check fails.  */
12116               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12117                 cp_parser_simulate_error (parser);
12118             }
12119           else if (is_overloaded_fn (argument))
12120             /* All overloaded functions are allowed; if the external
12121                linkage test does not pass, an error will be issued
12122                later.  */
12123             ;
12124           else if (address_p
12125                    && (TREE_CODE (argument) == OFFSET_REF
12126                        || TREE_CODE (argument) == SCOPE_REF))
12127             /* A pointer-to-member.  */
12128             ;
12129           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12130             ;
12131           else
12132             cp_parser_simulate_error (parser);
12133
12134           if (cp_parser_parse_definitely (parser))
12135             {
12136               if (address_p)
12137                 argument = build_x_unary_op (ADDR_EXPR, argument,
12138                                              tf_warning_or_error);
12139               return argument;
12140             }
12141         }
12142     }
12143   /* If the argument started with "&", there are no other valid
12144      alternatives at this point.  */
12145   if (address_p)
12146     {
12147       cp_parser_error (parser, "invalid non-type template argument");
12148       return error_mark_node;
12149     }
12150
12151   /* If the argument wasn't successfully parsed as a type-id followed
12152      by '>>', the argument can only be a constant expression now.
12153      Otherwise, we try parsing the constant-expression tentatively,
12154      because the argument could really be a type-id.  */
12155   if (maybe_type_id)
12156     cp_parser_parse_tentatively (parser);
12157   argument = cp_parser_constant_expression (parser,
12158                                             /*allow_non_constant_p=*/false,
12159                                             /*non_constant_p=*/NULL);
12160   argument = fold_non_dependent_expr (argument);
12161   if (!maybe_type_id)
12162     return argument;
12163   if (!cp_parser_next_token_ends_template_argument_p (parser))
12164     cp_parser_error (parser, "expected template-argument");
12165   if (cp_parser_parse_definitely (parser))
12166     return argument;
12167   /* We did our best to parse the argument as a non type-id, but that
12168      was the only alternative that matched (albeit with a '>' after
12169      it). We can assume it's just a typo from the user, and a
12170      diagnostic will then be issued.  */
12171   return cp_parser_template_type_arg (parser);
12172 }
12173
12174 /* Parse an explicit-instantiation.
12175
12176    explicit-instantiation:
12177      template declaration
12178
12179    Although the standard says `declaration', what it really means is:
12180
12181    explicit-instantiation:
12182      template decl-specifier-seq [opt] declarator [opt] ;
12183
12184    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12185    supposed to be allowed.  A defect report has been filed about this
12186    issue.
12187
12188    GNU Extension:
12189
12190    explicit-instantiation:
12191      storage-class-specifier template
12192        decl-specifier-seq [opt] declarator [opt] ;
12193      function-specifier template
12194        decl-specifier-seq [opt] declarator [opt] ;  */
12195
12196 static void
12197 cp_parser_explicit_instantiation (cp_parser* parser)
12198 {
12199   int declares_class_or_enum;
12200   cp_decl_specifier_seq decl_specifiers;
12201   tree extension_specifier = NULL_TREE;
12202
12203   /* Look for an (optional) storage-class-specifier or
12204      function-specifier.  */
12205   if (cp_parser_allow_gnu_extensions_p (parser))
12206     {
12207       extension_specifier
12208         = cp_parser_storage_class_specifier_opt (parser);
12209       if (!extension_specifier)
12210         extension_specifier
12211           = cp_parser_function_specifier_opt (parser,
12212                                               /*decl_specs=*/NULL);
12213     }
12214
12215   /* Look for the `template' keyword.  */
12216   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12217   /* Let the front end know that we are processing an explicit
12218      instantiation.  */
12219   begin_explicit_instantiation ();
12220   /* [temp.explicit] says that we are supposed to ignore access
12221      control while processing explicit instantiation directives.  */
12222   push_deferring_access_checks (dk_no_check);
12223   /* Parse a decl-specifier-seq.  */
12224   cp_parser_decl_specifier_seq (parser,
12225                                 CP_PARSER_FLAGS_OPTIONAL,
12226                                 &decl_specifiers,
12227                                 &declares_class_or_enum);
12228   /* If there was exactly one decl-specifier, and it declared a class,
12229      and there's no declarator, then we have an explicit type
12230      instantiation.  */
12231   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12232     {
12233       tree type;
12234
12235       type = check_tag_decl (&decl_specifiers);
12236       /* Turn access control back on for names used during
12237          template instantiation.  */
12238       pop_deferring_access_checks ();
12239       if (type)
12240         do_type_instantiation (type, extension_specifier,
12241                                /*complain=*/tf_error);
12242     }
12243   else
12244     {
12245       cp_declarator *declarator;
12246       tree decl;
12247
12248       /* Parse the declarator.  */
12249       declarator
12250         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12251                                 /*ctor_dtor_or_conv_p=*/NULL,
12252                                 /*parenthesized_p=*/NULL,
12253                                 /*member_p=*/false);
12254       if (declares_class_or_enum & 2)
12255         cp_parser_check_for_definition_in_return_type (declarator,
12256                                                        decl_specifiers.type,
12257                                                        decl_specifiers.type_location);
12258       if (declarator != cp_error_declarator)
12259         {
12260           if (decl_specifiers.specs[(int)ds_inline])
12261             permerror (input_location, "explicit instantiation shall not use"
12262                        " %<inline%> specifier");
12263           if (decl_specifiers.specs[(int)ds_constexpr])
12264             permerror (input_location, "explicit instantiation shall not use"
12265                        " %<constexpr%> specifier");
12266
12267           decl = grokdeclarator (declarator, &decl_specifiers,
12268                                  NORMAL, 0, &decl_specifiers.attributes);
12269           /* Turn access control back on for names used during
12270              template instantiation.  */
12271           pop_deferring_access_checks ();
12272           /* Do the explicit instantiation.  */
12273           do_decl_instantiation (decl, extension_specifier);
12274         }
12275       else
12276         {
12277           pop_deferring_access_checks ();
12278           /* Skip the body of the explicit instantiation.  */
12279           cp_parser_skip_to_end_of_statement (parser);
12280         }
12281     }
12282   /* We're done with the instantiation.  */
12283   end_explicit_instantiation ();
12284
12285   cp_parser_consume_semicolon_at_end_of_statement (parser);
12286 }
12287
12288 /* Parse an explicit-specialization.
12289
12290    explicit-specialization:
12291      template < > declaration
12292
12293    Although the standard says `declaration', what it really means is:
12294
12295    explicit-specialization:
12296      template <> decl-specifier [opt] init-declarator [opt] ;
12297      template <> function-definition
12298      template <> explicit-specialization
12299      template <> template-declaration  */
12300
12301 static void
12302 cp_parser_explicit_specialization (cp_parser* parser)
12303 {
12304   bool need_lang_pop;
12305   cp_token *token = cp_lexer_peek_token (parser->lexer);
12306
12307   /* Look for the `template' keyword.  */
12308   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12309   /* Look for the `<'.  */
12310   cp_parser_require (parser, CPP_LESS, RT_LESS);
12311   /* Look for the `>'.  */
12312   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12313   /* We have processed another parameter list.  */
12314   ++parser->num_template_parameter_lists;
12315   /* [temp]
12316
12317      A template ... explicit specialization ... shall not have C
12318      linkage.  */
12319   if (current_lang_name == lang_name_c)
12320     {
12321       error_at (token->location, "template specialization with C linkage");
12322       /* Give it C++ linkage to avoid confusing other parts of the
12323          front end.  */
12324       push_lang_context (lang_name_cplusplus);
12325       need_lang_pop = true;
12326     }
12327   else
12328     need_lang_pop = false;
12329   /* Let the front end know that we are beginning a specialization.  */
12330   if (!begin_specialization ())
12331     {
12332       end_specialization ();
12333       return;
12334     }
12335
12336   /* If the next keyword is `template', we need to figure out whether
12337      or not we're looking a template-declaration.  */
12338   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12339     {
12340       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12341           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12342         cp_parser_template_declaration_after_export (parser,
12343                                                      /*member_p=*/false);
12344       else
12345         cp_parser_explicit_specialization (parser);
12346     }
12347   else
12348     /* Parse the dependent declaration.  */
12349     cp_parser_single_declaration (parser,
12350                                   /*checks=*/NULL,
12351                                   /*member_p=*/false,
12352                                   /*explicit_specialization_p=*/true,
12353                                   /*friend_p=*/NULL);
12354   /* We're done with the specialization.  */
12355   end_specialization ();
12356   /* For the erroneous case of a template with C linkage, we pushed an
12357      implicit C++ linkage scope; exit that scope now.  */
12358   if (need_lang_pop)
12359     pop_lang_context ();
12360   /* We're done with this parameter list.  */
12361   --parser->num_template_parameter_lists;
12362 }
12363
12364 /* Parse a type-specifier.
12365
12366    type-specifier:
12367      simple-type-specifier
12368      class-specifier
12369      enum-specifier
12370      elaborated-type-specifier
12371      cv-qualifier
12372
12373    GNU Extension:
12374
12375    type-specifier:
12376      __complex__
12377
12378    Returns a representation of the type-specifier.  For a
12379    class-specifier, enum-specifier, or elaborated-type-specifier, a
12380    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12381
12382    The parser flags FLAGS is used to control type-specifier parsing.
12383
12384    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12385    in a decl-specifier-seq.
12386
12387    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12388    class-specifier, enum-specifier, or elaborated-type-specifier, then
12389    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12390    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12391    zero.
12392
12393    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12394    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12395    is set to FALSE.  */
12396
12397 static tree
12398 cp_parser_type_specifier (cp_parser* parser,
12399                           cp_parser_flags flags,
12400                           cp_decl_specifier_seq *decl_specs,
12401                           bool is_declaration,
12402                           int* declares_class_or_enum,
12403                           bool* is_cv_qualifier)
12404 {
12405   tree type_spec = NULL_TREE;
12406   cp_token *token;
12407   enum rid keyword;
12408   cp_decl_spec ds = ds_last;
12409
12410   /* Assume this type-specifier does not declare a new type.  */
12411   if (declares_class_or_enum)
12412     *declares_class_or_enum = 0;
12413   /* And that it does not specify a cv-qualifier.  */
12414   if (is_cv_qualifier)
12415     *is_cv_qualifier = false;
12416   /* Peek at the next token.  */
12417   token = cp_lexer_peek_token (parser->lexer);
12418
12419   /* If we're looking at a keyword, we can use that to guide the
12420      production we choose.  */
12421   keyword = token->keyword;
12422   switch (keyword)
12423     {
12424     case RID_ENUM:
12425       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12426         goto elaborated_type_specifier;
12427
12428       /* Look for the enum-specifier.  */
12429       type_spec = cp_parser_enum_specifier (parser);
12430       /* If that worked, we're done.  */
12431       if (type_spec)
12432         {
12433           if (declares_class_or_enum)
12434             *declares_class_or_enum = 2;
12435           if (decl_specs)
12436             cp_parser_set_decl_spec_type (decl_specs,
12437                                           type_spec,
12438                                           token->location,
12439                                           /*user_defined_p=*/true);
12440           return type_spec;
12441         }
12442       else
12443         goto elaborated_type_specifier;
12444
12445       /* Any of these indicate either a class-specifier, or an
12446          elaborated-type-specifier.  */
12447     case RID_CLASS:
12448     case RID_STRUCT:
12449     case RID_UNION:
12450       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12451         goto elaborated_type_specifier;
12452
12453       /* Parse tentatively so that we can back up if we don't find a
12454          class-specifier.  */
12455       cp_parser_parse_tentatively (parser);
12456       /* Look for the class-specifier.  */
12457       type_spec = cp_parser_class_specifier (parser);
12458       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12459       /* If that worked, we're done.  */
12460       if (cp_parser_parse_definitely (parser))
12461         {
12462           if (declares_class_or_enum)
12463             *declares_class_or_enum = 2;
12464           if (decl_specs)
12465             cp_parser_set_decl_spec_type (decl_specs,
12466                                           type_spec,
12467                                           token->location,
12468                                           /*user_defined_p=*/true);
12469           return type_spec;
12470         }
12471
12472       /* Fall through.  */
12473     elaborated_type_specifier:
12474       /* We're declaring (not defining) a class or enum.  */
12475       if (declares_class_or_enum)
12476         *declares_class_or_enum = 1;
12477
12478       /* Fall through.  */
12479     case RID_TYPENAME:
12480       /* Look for an elaborated-type-specifier.  */
12481       type_spec
12482         = (cp_parser_elaborated_type_specifier
12483            (parser,
12484             decl_specs && decl_specs->specs[(int) ds_friend],
12485             is_declaration));
12486       if (decl_specs)
12487         cp_parser_set_decl_spec_type (decl_specs,
12488                                       type_spec,
12489                                       token->location,
12490                                       /*user_defined_p=*/true);
12491       return type_spec;
12492
12493     case RID_CONST:
12494       ds = ds_const;
12495       if (is_cv_qualifier)
12496         *is_cv_qualifier = true;
12497       break;
12498
12499     case RID_VOLATILE:
12500       ds = ds_volatile;
12501       if (is_cv_qualifier)
12502         *is_cv_qualifier = true;
12503       break;
12504
12505     case RID_RESTRICT:
12506       ds = ds_restrict;
12507       if (is_cv_qualifier)
12508         *is_cv_qualifier = true;
12509       break;
12510
12511     case RID_COMPLEX:
12512       /* The `__complex__' keyword is a GNU extension.  */
12513       ds = ds_complex;
12514       break;
12515
12516     default:
12517       break;
12518     }
12519
12520   /* Handle simple keywords.  */
12521   if (ds != ds_last)
12522     {
12523       if (decl_specs)
12524         {
12525           ++decl_specs->specs[(int)ds];
12526           decl_specs->any_specifiers_p = true;
12527         }
12528       return cp_lexer_consume_token (parser->lexer)->u.value;
12529     }
12530
12531   /* If we do not already have a type-specifier, assume we are looking
12532      at a simple-type-specifier.  */
12533   type_spec = cp_parser_simple_type_specifier (parser,
12534                                                decl_specs,
12535                                                flags);
12536
12537   /* If we didn't find a type-specifier, and a type-specifier was not
12538      optional in this context, issue an error message.  */
12539   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12540     {
12541       cp_parser_error (parser, "expected type specifier");
12542       return error_mark_node;
12543     }
12544
12545   return type_spec;
12546 }
12547
12548 /* Parse a simple-type-specifier.
12549
12550    simple-type-specifier:
12551      :: [opt] nested-name-specifier [opt] type-name
12552      :: [opt] nested-name-specifier template template-id
12553      char
12554      wchar_t
12555      bool
12556      short
12557      int
12558      long
12559      signed
12560      unsigned
12561      float
12562      double
12563      void
12564
12565    C++0x Extension:
12566
12567    simple-type-specifier:
12568      auto
12569      decltype ( expression )   
12570      char16_t
12571      char32_t
12572
12573    GNU Extension:
12574
12575    simple-type-specifier:
12576      __int128
12577      __typeof__ unary-expression
12578      __typeof__ ( type-id )
12579
12580    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12581    appropriately updated.  */
12582
12583 static tree
12584 cp_parser_simple_type_specifier (cp_parser* parser,
12585                                  cp_decl_specifier_seq *decl_specs,
12586                                  cp_parser_flags flags)
12587 {
12588   tree type = NULL_TREE;
12589   cp_token *token;
12590
12591   /* Peek at the next token.  */
12592   token = cp_lexer_peek_token (parser->lexer);
12593
12594   /* If we're looking at a keyword, things are easy.  */
12595   switch (token->keyword)
12596     {
12597     case RID_CHAR:
12598       if (decl_specs)
12599         decl_specs->explicit_char_p = true;
12600       type = char_type_node;
12601       break;
12602     case RID_CHAR16:
12603       type = char16_type_node;
12604       break;
12605     case RID_CHAR32:
12606       type = char32_type_node;
12607       break;
12608     case RID_WCHAR:
12609       type = wchar_type_node;
12610       break;
12611     case RID_BOOL:
12612       type = boolean_type_node;
12613       break;
12614     case RID_SHORT:
12615       if (decl_specs)
12616         ++decl_specs->specs[(int) ds_short];
12617       type = short_integer_type_node;
12618       break;
12619     case RID_INT:
12620       if (decl_specs)
12621         decl_specs->explicit_int_p = true;
12622       type = integer_type_node;
12623       break;
12624     case RID_INT128:
12625       if (!int128_integer_type_node)
12626         break;
12627       if (decl_specs)
12628         decl_specs->explicit_int128_p = true;
12629       type = int128_integer_type_node;
12630       break;
12631     case RID_LONG:
12632       if (decl_specs)
12633         ++decl_specs->specs[(int) ds_long];
12634       type = long_integer_type_node;
12635       break;
12636     case RID_SIGNED:
12637       if (decl_specs)
12638         ++decl_specs->specs[(int) ds_signed];
12639       type = integer_type_node;
12640       break;
12641     case RID_UNSIGNED:
12642       if (decl_specs)
12643         ++decl_specs->specs[(int) ds_unsigned];
12644       type = unsigned_type_node;
12645       break;
12646     case RID_FLOAT:
12647       type = float_type_node;
12648       break;
12649     case RID_DOUBLE:
12650       type = double_type_node;
12651       break;
12652     case RID_VOID:
12653       type = void_type_node;
12654       break;
12655       
12656     case RID_AUTO:
12657       maybe_warn_cpp0x (CPP0X_AUTO);
12658       type = make_auto ();
12659       break;
12660
12661     case RID_DECLTYPE:
12662       /* Parse the `decltype' type.  */
12663       type = cp_parser_decltype (parser);
12664
12665       if (decl_specs)
12666         cp_parser_set_decl_spec_type (decl_specs, type,
12667                                       token->location,
12668                                       /*user_defined_p=*/true);
12669
12670       return type;
12671
12672     case RID_TYPEOF:
12673       /* Consume the `typeof' token.  */
12674       cp_lexer_consume_token (parser->lexer);
12675       /* Parse the operand to `typeof'.  */
12676       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12677       /* If it is not already a TYPE, take its type.  */
12678       if (!TYPE_P (type))
12679         type = finish_typeof (type);
12680
12681       if (decl_specs)
12682         cp_parser_set_decl_spec_type (decl_specs, type,
12683                                       token->location,
12684                                       /*user_defined_p=*/true);
12685
12686       return type;
12687
12688     default:
12689       break;
12690     }
12691
12692   /* If the type-specifier was for a built-in type, we're done.  */
12693   if (type)
12694     {
12695       /* Record the type.  */
12696       if (decl_specs
12697           && (token->keyword != RID_SIGNED
12698               && token->keyword != RID_UNSIGNED
12699               && token->keyword != RID_SHORT
12700               && token->keyword != RID_LONG))
12701         cp_parser_set_decl_spec_type (decl_specs,
12702                                       type,
12703                                       token->location,
12704                                       /*user_defined=*/false);
12705       if (decl_specs)
12706         decl_specs->any_specifiers_p = true;
12707
12708       /* Consume the token.  */
12709       cp_lexer_consume_token (parser->lexer);
12710
12711       /* There is no valid C++ program where a non-template type is
12712          followed by a "<".  That usually indicates that the user thought
12713          that the type was a template.  */
12714       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12715
12716       return TYPE_NAME (type);
12717     }
12718
12719   /* The type-specifier must be a user-defined type.  */
12720   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12721     {
12722       bool qualified_p;
12723       bool global_p;
12724
12725       /* Don't gobble tokens or issue error messages if this is an
12726          optional type-specifier.  */
12727       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12728         cp_parser_parse_tentatively (parser);
12729
12730       /* Look for the optional `::' operator.  */
12731       global_p
12732         = (cp_parser_global_scope_opt (parser,
12733                                        /*current_scope_valid_p=*/false)
12734            != NULL_TREE);
12735       /* Look for the nested-name specifier.  */
12736       qualified_p
12737         = (cp_parser_nested_name_specifier_opt (parser,
12738                                                 /*typename_keyword_p=*/false,
12739                                                 /*check_dependency_p=*/true,
12740                                                 /*type_p=*/false,
12741                                                 /*is_declaration=*/false)
12742            != NULL_TREE);
12743       token = cp_lexer_peek_token (parser->lexer);
12744       /* If we have seen a nested-name-specifier, and the next token
12745          is `template', then we are using the template-id production.  */
12746       if (parser->scope
12747           && cp_parser_optional_template_keyword (parser))
12748         {
12749           /* Look for the template-id.  */
12750           type = cp_parser_template_id (parser,
12751                                         /*template_keyword_p=*/true,
12752                                         /*check_dependency_p=*/true,
12753                                         /*is_declaration=*/false);
12754           /* If the template-id did not name a type, we are out of
12755              luck.  */
12756           if (TREE_CODE (type) != TYPE_DECL)
12757             {
12758               cp_parser_error (parser, "expected template-id for type");
12759               type = NULL_TREE;
12760             }
12761         }
12762       /* Otherwise, look for a type-name.  */
12763       else
12764         type = cp_parser_type_name (parser);
12765       /* Keep track of all name-lookups performed in class scopes.  */
12766       if (type
12767           && !global_p
12768           && !qualified_p
12769           && TREE_CODE (type) == TYPE_DECL
12770           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12771         maybe_note_name_used_in_class (DECL_NAME (type), type);
12772       /* If it didn't work out, we don't have a TYPE.  */
12773       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12774           && !cp_parser_parse_definitely (parser))
12775         type = NULL_TREE;
12776       if (type && decl_specs)
12777         cp_parser_set_decl_spec_type (decl_specs, type,
12778                                       token->location,
12779                                       /*user_defined=*/true);
12780     }
12781
12782   /* If we didn't get a type-name, issue an error message.  */
12783   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12784     {
12785       cp_parser_error (parser, "expected type-name");
12786       return error_mark_node;
12787     }
12788
12789   /* There is no valid C++ program where a non-template type is
12790      followed by a "<".  That usually indicates that the user thought
12791      that the type was a template.  */
12792   if (type && type != error_mark_node)
12793     {
12794       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12795          If it is, then the '<'...'>' enclose protocol names rather than
12796          template arguments, and so everything is fine.  */
12797       if (c_dialect_objc () && !parser->scope
12798           && (objc_is_id (type) || objc_is_class_name (type)))
12799         {
12800           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12801           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12802
12803           /* Clobber the "unqualified" type previously entered into
12804              DECL_SPECS with the new, improved protocol-qualified version.  */
12805           if (decl_specs)
12806             decl_specs->type = qual_type;
12807
12808           return qual_type;
12809         }
12810
12811       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12812                                                token->location);
12813     }
12814
12815   return type;
12816 }
12817
12818 /* Parse a type-name.
12819
12820    type-name:
12821      class-name
12822      enum-name
12823      typedef-name
12824
12825    enum-name:
12826      identifier
12827
12828    typedef-name:
12829      identifier
12830
12831    Returns a TYPE_DECL for the type.  */
12832
12833 static tree
12834 cp_parser_type_name (cp_parser* parser)
12835 {
12836   tree type_decl;
12837
12838   /* We can't know yet whether it is a class-name or not.  */
12839   cp_parser_parse_tentatively (parser);
12840   /* Try a class-name.  */
12841   type_decl = cp_parser_class_name (parser,
12842                                     /*typename_keyword_p=*/false,
12843                                     /*template_keyword_p=*/false,
12844                                     none_type,
12845                                     /*check_dependency_p=*/true,
12846                                     /*class_head_p=*/false,
12847                                     /*is_declaration=*/false);
12848   /* If it's not a class-name, keep looking.  */
12849   if (!cp_parser_parse_definitely (parser))
12850     {
12851       /* It must be a typedef-name or an enum-name.  */
12852       return cp_parser_nonclass_name (parser);
12853     }
12854
12855   return type_decl;
12856 }
12857
12858 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12859
12860    enum-name:
12861      identifier
12862
12863    typedef-name:
12864      identifier
12865
12866    Returns a TYPE_DECL for the type.  */
12867
12868 static tree
12869 cp_parser_nonclass_name (cp_parser* parser)
12870 {
12871   tree type_decl;
12872   tree identifier;
12873
12874   cp_token *token = cp_lexer_peek_token (parser->lexer);
12875   identifier = cp_parser_identifier (parser);
12876   if (identifier == error_mark_node)
12877     return error_mark_node;
12878
12879   /* Look up the type-name.  */
12880   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12881
12882   if (TREE_CODE (type_decl) != TYPE_DECL
12883       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12884     {
12885       /* See if this is an Objective-C type.  */
12886       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12887       tree type = objc_get_protocol_qualified_type (identifier, protos);
12888       if (type)
12889         type_decl = TYPE_NAME (type);
12890     }
12891   
12892   /* Issue an error if we did not find a type-name.  */
12893   if (TREE_CODE (type_decl) != TYPE_DECL)
12894     {
12895       if (!cp_parser_simulate_error (parser))
12896         cp_parser_name_lookup_error (parser, identifier, type_decl,
12897                                      NLE_TYPE, token->location);
12898       return error_mark_node;
12899     }
12900   /* Remember that the name was used in the definition of the
12901      current class so that we can check later to see if the
12902      meaning would have been different after the class was
12903      entirely defined.  */
12904   else if (type_decl != error_mark_node
12905            && !parser->scope)
12906     maybe_note_name_used_in_class (identifier, type_decl);
12907   
12908   return type_decl;
12909 }
12910
12911 /* Parse an elaborated-type-specifier.  Note that the grammar given
12912    here incorporates the resolution to DR68.
12913
12914    elaborated-type-specifier:
12915      class-key :: [opt] nested-name-specifier [opt] identifier
12916      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12917      enum-key :: [opt] nested-name-specifier [opt] identifier
12918      typename :: [opt] nested-name-specifier identifier
12919      typename :: [opt] nested-name-specifier template [opt]
12920        template-id
12921
12922    GNU extension:
12923
12924    elaborated-type-specifier:
12925      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12926      class-key attributes :: [opt] nested-name-specifier [opt]
12927                template [opt] template-id
12928      enum attributes :: [opt] nested-name-specifier [opt] identifier
12929
12930    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12931    declared `friend'.  If IS_DECLARATION is TRUE, then this
12932    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12933    something is being declared.
12934
12935    Returns the TYPE specified.  */
12936
12937 static tree
12938 cp_parser_elaborated_type_specifier (cp_parser* parser,
12939                                      bool is_friend,
12940                                      bool is_declaration)
12941 {
12942   enum tag_types tag_type;
12943   tree identifier;
12944   tree type = NULL_TREE;
12945   tree attributes = NULL_TREE;
12946   tree globalscope;
12947   cp_token *token = NULL;
12948
12949   /* See if we're looking at the `enum' keyword.  */
12950   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12951     {
12952       /* Consume the `enum' token.  */
12953       cp_lexer_consume_token (parser->lexer);
12954       /* Remember that it's an enumeration type.  */
12955       tag_type = enum_type;
12956       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12957          enums) is used here.  */
12958       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12959           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12960         {
12961             pedwarn (input_location, 0, "elaborated-type-specifier "
12962                       "for a scoped enum must not use the %<%D%> keyword",
12963                       cp_lexer_peek_token (parser->lexer)->u.value);
12964           /* Consume the `struct' or `class' and parse it anyway.  */
12965           cp_lexer_consume_token (parser->lexer);
12966         }
12967       /* Parse the attributes.  */
12968       attributes = cp_parser_attributes_opt (parser);
12969     }
12970   /* Or, it might be `typename'.  */
12971   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12972                                            RID_TYPENAME))
12973     {
12974       /* Consume the `typename' token.  */
12975       cp_lexer_consume_token (parser->lexer);
12976       /* Remember that it's a `typename' type.  */
12977       tag_type = typename_type;
12978     }
12979   /* Otherwise it must be a class-key.  */
12980   else
12981     {
12982       tag_type = cp_parser_class_key (parser);
12983       if (tag_type == none_type)
12984         return error_mark_node;
12985       /* Parse the attributes.  */
12986       attributes = cp_parser_attributes_opt (parser);
12987     }
12988
12989   /* Look for the `::' operator.  */
12990   globalscope =  cp_parser_global_scope_opt (parser,
12991                                              /*current_scope_valid_p=*/false);
12992   /* Look for the nested-name-specifier.  */
12993   if (tag_type == typename_type && !globalscope)
12994     {
12995       if (!cp_parser_nested_name_specifier (parser,
12996                                            /*typename_keyword_p=*/true,
12997                                            /*check_dependency_p=*/true,
12998                                            /*type_p=*/true,
12999                                             is_declaration))
13000         return error_mark_node;
13001     }
13002   else
13003     /* Even though `typename' is not present, the proposed resolution
13004        to Core Issue 180 says that in `class A<T>::B', `B' should be
13005        considered a type-name, even if `A<T>' is dependent.  */
13006     cp_parser_nested_name_specifier_opt (parser,
13007                                          /*typename_keyword_p=*/true,
13008                                          /*check_dependency_p=*/true,
13009                                          /*type_p=*/true,
13010                                          is_declaration);
13011  /* For everything but enumeration types, consider a template-id.
13012     For an enumeration type, consider only a plain identifier.  */
13013   if (tag_type != enum_type)
13014     {
13015       bool template_p = false;
13016       tree decl;
13017
13018       /* Allow the `template' keyword.  */
13019       template_p = cp_parser_optional_template_keyword (parser);
13020       /* If we didn't see `template', we don't know if there's a
13021          template-id or not.  */
13022       if (!template_p)
13023         cp_parser_parse_tentatively (parser);
13024       /* Parse the template-id.  */
13025       token = cp_lexer_peek_token (parser->lexer);
13026       decl = cp_parser_template_id (parser, template_p,
13027                                     /*check_dependency_p=*/true,
13028                                     is_declaration);
13029       /* If we didn't find a template-id, look for an ordinary
13030          identifier.  */
13031       if (!template_p && !cp_parser_parse_definitely (parser))
13032         ;
13033       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13034          in effect, then we must assume that, upon instantiation, the
13035          template will correspond to a class.  */
13036       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13037                && tag_type == typename_type)
13038         type = make_typename_type (parser->scope, decl,
13039                                    typename_type,
13040                                    /*complain=*/tf_error);
13041       /* If the `typename' keyword is in effect and DECL is not a type
13042          decl. Then type is non existant.   */
13043       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13044         type = NULL_TREE; 
13045       else 
13046         type = TREE_TYPE (decl);
13047     }
13048
13049   if (!type)
13050     {
13051       token = cp_lexer_peek_token (parser->lexer);
13052       identifier = cp_parser_identifier (parser);
13053
13054       if (identifier == error_mark_node)
13055         {
13056           parser->scope = NULL_TREE;
13057           return error_mark_node;
13058         }
13059
13060       /* For a `typename', we needn't call xref_tag.  */
13061       if (tag_type == typename_type
13062           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13063         return cp_parser_make_typename_type (parser, parser->scope,
13064                                              identifier,
13065                                              token->location);
13066       /* Look up a qualified name in the usual way.  */
13067       if (parser->scope)
13068         {
13069           tree decl;
13070           tree ambiguous_decls;
13071
13072           decl = cp_parser_lookup_name (parser, identifier,
13073                                         tag_type,
13074                                         /*is_template=*/false,
13075                                         /*is_namespace=*/false,
13076                                         /*check_dependency=*/true,
13077                                         &ambiguous_decls,
13078                                         token->location);
13079
13080           /* If the lookup was ambiguous, an error will already have been
13081              issued.  */
13082           if (ambiguous_decls)
13083             return error_mark_node;
13084
13085           /* If we are parsing friend declaration, DECL may be a
13086              TEMPLATE_DECL tree node here.  However, we need to check
13087              whether this TEMPLATE_DECL results in valid code.  Consider
13088              the following example:
13089
13090                namespace N {
13091                  template <class T> class C {};
13092                }
13093                class X {
13094                  template <class T> friend class N::C; // #1, valid code
13095                };
13096                template <class T> class Y {
13097                  friend class N::C;                    // #2, invalid code
13098                };
13099
13100              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13101              name lookup of `N::C'.  We see that friend declaration must
13102              be template for the code to be valid.  Note that
13103              processing_template_decl does not work here since it is
13104              always 1 for the above two cases.  */
13105
13106           decl = (cp_parser_maybe_treat_template_as_class
13107                   (decl, /*tag_name_p=*/is_friend
13108                          && parser->num_template_parameter_lists));
13109
13110           if (TREE_CODE (decl) != TYPE_DECL)
13111             {
13112               cp_parser_diagnose_invalid_type_name (parser,
13113                                                     parser->scope,
13114                                                     identifier,
13115                                                     token->location);
13116               return error_mark_node;
13117             }
13118
13119           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13120             {
13121               bool allow_template = (parser->num_template_parameter_lists
13122                                       || DECL_SELF_REFERENCE_P (decl));
13123               type = check_elaborated_type_specifier (tag_type, decl, 
13124                                                       allow_template);
13125
13126               if (type == error_mark_node)
13127                 return error_mark_node;
13128             }
13129
13130           /* Forward declarations of nested types, such as
13131
13132                class C1::C2;
13133                class C1::C2::C3;
13134
13135              are invalid unless all components preceding the final '::'
13136              are complete.  If all enclosing types are complete, these
13137              declarations become merely pointless.
13138
13139              Invalid forward declarations of nested types are errors
13140              caught elsewhere in parsing.  Those that are pointless arrive
13141              here.  */
13142
13143           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13144               && !is_friend && !processing_explicit_instantiation)
13145             warning (0, "declaration %qD does not declare anything", decl);
13146
13147           type = TREE_TYPE (decl);
13148         }
13149       else
13150         {
13151           /* An elaborated-type-specifier sometimes introduces a new type and
13152              sometimes names an existing type.  Normally, the rule is that it
13153              introduces a new type only if there is not an existing type of
13154              the same name already in scope.  For example, given:
13155
13156                struct S {};
13157                void f() { struct S s; }
13158
13159              the `struct S' in the body of `f' is the same `struct S' as in
13160              the global scope; the existing definition is used.  However, if
13161              there were no global declaration, this would introduce a new
13162              local class named `S'.
13163
13164              An exception to this rule applies to the following code:
13165
13166                namespace N { struct S; }
13167
13168              Here, the elaborated-type-specifier names a new type
13169              unconditionally; even if there is already an `S' in the
13170              containing scope this declaration names a new type.
13171              This exception only applies if the elaborated-type-specifier
13172              forms the complete declaration:
13173
13174                [class.name]
13175
13176                A declaration consisting solely of `class-key identifier ;' is
13177                either a redeclaration of the name in the current scope or a
13178                forward declaration of the identifier as a class name.  It
13179                introduces the name into the current scope.
13180
13181              We are in this situation precisely when the next token is a `;'.
13182
13183              An exception to the exception is that a `friend' declaration does
13184              *not* name a new type; i.e., given:
13185
13186                struct S { friend struct T; };
13187
13188              `T' is not a new type in the scope of `S'.
13189
13190              Also, `new struct S' or `sizeof (struct S)' never results in the
13191              definition of a new type; a new type can only be declared in a
13192              declaration context.  */
13193
13194           tag_scope ts;
13195           bool template_p;
13196
13197           if (is_friend)
13198             /* Friends have special name lookup rules.  */
13199             ts = ts_within_enclosing_non_class;
13200           else if (is_declaration
13201                    && cp_lexer_next_token_is (parser->lexer,
13202                                               CPP_SEMICOLON))
13203             /* This is a `class-key identifier ;' */
13204             ts = ts_current;
13205           else
13206             ts = ts_global;
13207
13208           template_p =
13209             (parser->num_template_parameter_lists
13210              && (cp_parser_next_token_starts_class_definition_p (parser)
13211                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13212           /* An unqualified name was used to reference this type, so
13213              there were no qualifying templates.  */
13214           if (!cp_parser_check_template_parameters (parser,
13215                                                     /*num_templates=*/0,
13216                                                     token->location,
13217                                                     /*declarator=*/NULL))
13218             return error_mark_node;
13219           type = xref_tag (tag_type, identifier, ts, template_p);
13220         }
13221     }
13222
13223   if (type == error_mark_node)
13224     return error_mark_node;
13225
13226   /* Allow attributes on forward declarations of classes.  */
13227   if (attributes)
13228     {
13229       if (TREE_CODE (type) == TYPENAME_TYPE)
13230         warning (OPT_Wattributes,
13231                  "attributes ignored on uninstantiated type");
13232       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13233                && ! processing_explicit_instantiation)
13234         warning (OPT_Wattributes,
13235                  "attributes ignored on template instantiation");
13236       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13237         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13238       else
13239         warning (OPT_Wattributes,
13240                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13241     }
13242
13243   if (tag_type != enum_type)
13244     cp_parser_check_class_key (tag_type, type);
13245
13246   /* A "<" cannot follow an elaborated type specifier.  If that
13247      happens, the user was probably trying to form a template-id.  */
13248   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13249
13250   return type;
13251 }
13252
13253 /* Parse an enum-specifier.
13254
13255    enum-specifier:
13256      enum-head { enumerator-list [opt] }
13257
13258    enum-head:
13259      enum-key identifier [opt] enum-base [opt]
13260      enum-key nested-name-specifier identifier enum-base [opt]
13261
13262    enum-key:
13263      enum
13264      enum class   [C++0x]
13265      enum struct  [C++0x]
13266
13267    enum-base:   [C++0x]
13268      : type-specifier-seq
13269
13270    opaque-enum-specifier:
13271      enum-key identifier enum-base [opt] ;
13272
13273    GNU Extensions:
13274      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13275        { enumerator-list [opt] }attributes[opt]
13276
13277    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13278    if the token stream isn't an enum-specifier after all.  */
13279
13280 static tree
13281 cp_parser_enum_specifier (cp_parser* parser)
13282 {
13283   tree identifier;
13284   tree type = NULL_TREE;
13285   tree prev_scope;
13286   tree nested_name_specifier = NULL_TREE;
13287   tree attributes;
13288   bool scoped_enum_p = false;
13289   bool has_underlying_type = false;
13290   bool nested_being_defined = false;
13291   bool new_value_list = false;
13292   bool is_new_type = false;
13293   bool is_anonymous = false;
13294   tree underlying_type = NULL_TREE;
13295   cp_token *type_start_token = NULL;
13296
13297   /* Parse tentatively so that we can back up if we don't find a
13298      enum-specifier.  */
13299   cp_parser_parse_tentatively (parser);
13300
13301   /* Caller guarantees that the current token is 'enum', an identifier
13302      possibly follows, and the token after that is an opening brace.
13303      If we don't have an identifier, fabricate an anonymous name for
13304      the enumeration being defined.  */
13305   cp_lexer_consume_token (parser->lexer);
13306
13307   /* Parse the "class" or "struct", which indicates a scoped
13308      enumeration type in C++0x.  */
13309   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13310       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13311     {
13312       if (cxx_dialect < cxx0x)
13313         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13314
13315       /* Consume the `struct' or `class' token.  */
13316       cp_lexer_consume_token (parser->lexer);
13317
13318       scoped_enum_p = true;
13319     }
13320
13321   attributes = cp_parser_attributes_opt (parser);
13322
13323   /* Clear the qualification.  */
13324   parser->scope = NULL_TREE;
13325   parser->qualifying_scope = NULL_TREE;
13326   parser->object_scope = NULL_TREE;
13327
13328   /* Figure out in what scope the declaration is being placed.  */
13329   prev_scope = current_scope ();
13330
13331   type_start_token = cp_lexer_peek_token (parser->lexer);
13332
13333   push_deferring_access_checks (dk_no_check);
13334   nested_name_specifier
13335       = cp_parser_nested_name_specifier_opt (parser,
13336                                              /*typename_keyword_p=*/true,
13337                                              /*check_dependency_p=*/false,
13338                                              /*type_p=*/false,
13339                                              /*is_declaration=*/false);
13340
13341   if (nested_name_specifier)
13342     {
13343       tree name;
13344
13345       identifier = cp_parser_identifier (parser);
13346       name =  cp_parser_lookup_name (parser, identifier,
13347                                      enum_type,
13348                                      /*is_template=*/false,
13349                                      /*is_namespace=*/false,
13350                                      /*check_dependency=*/true,
13351                                      /*ambiguous_decls=*/NULL,
13352                                      input_location);
13353       if (name)
13354         {
13355           type = TREE_TYPE (name);
13356           if (TREE_CODE (type) == TYPENAME_TYPE)
13357             {
13358               /* Are template enums allowed in ISO? */
13359               if (template_parm_scope_p ())
13360                 pedwarn (type_start_token->location, OPT_pedantic,
13361                          "%qD is an enumeration template", name);
13362               /* ignore a typename reference, for it will be solved by name
13363                  in start_enum.  */
13364               type = NULL_TREE;
13365             }
13366         }
13367       else
13368         error_at (type_start_token->location,
13369                   "%qD is not an enumerator-name", identifier);
13370     }
13371   else
13372     {
13373       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13374         identifier = cp_parser_identifier (parser);
13375       else
13376         {
13377           identifier = make_anon_name ();
13378           is_anonymous = true;
13379         }
13380     }
13381   pop_deferring_access_checks ();
13382
13383   /* Check for the `:' that denotes a specified underlying type in C++0x.
13384      Note that a ':' could also indicate a bitfield width, however.  */
13385   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13386     {
13387       cp_decl_specifier_seq type_specifiers;
13388
13389       /* Consume the `:'.  */
13390       cp_lexer_consume_token (parser->lexer);
13391
13392       /* Parse the type-specifier-seq.  */
13393       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13394                                     /*is_trailing_return=*/false,
13395                                     &type_specifiers);
13396
13397       /* At this point this is surely not elaborated type specifier.  */
13398       if (!cp_parser_parse_definitely (parser))
13399         return NULL_TREE;
13400
13401       if (cxx_dialect < cxx0x)
13402         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13403
13404       has_underlying_type = true;
13405
13406       /* If that didn't work, stop.  */
13407       if (type_specifiers.type != error_mark_node)
13408         {
13409           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13410                                             /*initialized=*/0, NULL);
13411           if (underlying_type == error_mark_node)
13412             underlying_type = NULL_TREE;
13413         }
13414     }
13415
13416   /* Look for the `{' but don't consume it yet.  */
13417   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13418     {
13419       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13420         {
13421           cp_parser_error (parser, "expected %<{%>");
13422           if (has_underlying_type)
13423             return NULL_TREE;
13424         }
13425       /* An opaque-enum-specifier must have a ';' here.  */
13426       if ((scoped_enum_p || underlying_type)
13427           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13428         {
13429           cp_parser_error (parser, "expected %<;%> or %<{%>");
13430           if (has_underlying_type)
13431             return NULL_TREE;
13432         }
13433     }
13434
13435   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13436     return NULL_TREE;
13437
13438   if (nested_name_specifier)
13439     {
13440       if (CLASS_TYPE_P (nested_name_specifier))
13441         {
13442           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13443           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13444           push_scope (nested_name_specifier);
13445         }
13446       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13447         {
13448           push_nested_namespace (nested_name_specifier);
13449         }
13450     }
13451
13452   /* Issue an error message if type-definitions are forbidden here.  */
13453   if (!cp_parser_check_type_definition (parser))
13454     type = error_mark_node;
13455   else
13456     /* Create the new type.  We do this before consuming the opening
13457        brace so the enum will be recorded as being on the line of its
13458        tag (or the 'enum' keyword, if there is no tag).  */
13459     type = start_enum (identifier, type, underlying_type,
13460                        scoped_enum_p, &is_new_type);
13461
13462   /* If the next token is not '{' it is an opaque-enum-specifier or an
13463      elaborated-type-specifier.  */
13464   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13465     {
13466       if (nested_name_specifier)
13467         {
13468           /* The following catches invalid code such as:
13469              enum class S<int>::E { A, B, C }; */
13470           if (!processing_specialization
13471               && CLASS_TYPE_P (nested_name_specifier)
13472               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13473             error_at (type_start_token->location, "cannot add an enumerator "
13474                       "list to a template instantiation");
13475
13476           /* If that scope does not contain the scope in which the
13477              class was originally declared, the program is invalid.  */
13478           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13479             {
13480               if (at_namespace_scope_p ())
13481                 error_at (type_start_token->location,
13482                           "declaration of %qD in namespace %qD which does not "
13483                           "enclose %qD",
13484                           type, prev_scope, nested_name_specifier);
13485               else
13486                 error_at (type_start_token->location,
13487                           "declaration of %qD in %qD which does not enclose %qD",
13488                           type, prev_scope, nested_name_specifier);
13489               type = error_mark_node;
13490             }
13491         }
13492
13493       if (scoped_enum_p)
13494         begin_scope (sk_scoped_enum, type);
13495
13496       /* Consume the opening brace.  */
13497       cp_lexer_consume_token (parser->lexer);
13498
13499       if (type == error_mark_node)
13500         ; /* Nothing to add */
13501       else if (OPAQUE_ENUM_P (type)
13502                || (cxx_dialect > cxx98 && processing_specialization))
13503         {
13504           new_value_list = true;
13505           SET_OPAQUE_ENUM_P (type, false);
13506           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13507         }
13508       else
13509         {
13510           error_at (type_start_token->location, "multiple definition of %q#T", type);
13511           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13512                     "previous definition here");
13513           type = error_mark_node;
13514         }
13515
13516       if (type == error_mark_node)
13517         cp_parser_skip_to_end_of_block_or_statement (parser);
13518       /* If the next token is not '}', then there are some enumerators.  */
13519       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13520         cp_parser_enumerator_list (parser, type);
13521
13522       /* Consume the final '}'.  */
13523       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13524
13525       if (scoped_enum_p)
13526         finish_scope ();
13527     }
13528   else
13529     {
13530       /* If a ';' follows, then it is an opaque-enum-specifier
13531         and additional restrictions apply.  */
13532       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13533         {
13534           if (is_anonymous)
13535             error_at (type_start_token->location,
13536                       "opaque-enum-specifier without name");
13537           else if (nested_name_specifier)
13538             error_at (type_start_token->location,
13539                       "opaque-enum-specifier must use a simple identifier");
13540         }
13541     }
13542
13543   /* Look for trailing attributes to apply to this enumeration, and
13544      apply them if appropriate.  */
13545   if (cp_parser_allow_gnu_extensions_p (parser))
13546     {
13547       tree trailing_attr = cp_parser_attributes_opt (parser);
13548       trailing_attr = chainon (trailing_attr, attributes);
13549       cplus_decl_attributes (&type,
13550                              trailing_attr,
13551                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13552     }
13553
13554   /* Finish up the enumeration.  */
13555   if (type != error_mark_node)
13556     {
13557       if (new_value_list)
13558         finish_enum_value_list (type);
13559       if (is_new_type)
13560         finish_enum (type);
13561     }
13562
13563   if (nested_name_specifier)
13564     {
13565       if (CLASS_TYPE_P (nested_name_specifier))
13566         {
13567           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13568           pop_scope (nested_name_specifier);
13569         }
13570       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13571         {
13572           pop_nested_namespace (nested_name_specifier);
13573         }
13574     }
13575   return type;
13576 }
13577
13578 /* Parse an enumerator-list.  The enumerators all have the indicated
13579    TYPE.
13580
13581    enumerator-list:
13582      enumerator-definition
13583      enumerator-list , enumerator-definition  */
13584
13585 static void
13586 cp_parser_enumerator_list (cp_parser* parser, tree type)
13587 {
13588   while (true)
13589     {
13590       /* Parse an enumerator-definition.  */
13591       cp_parser_enumerator_definition (parser, type);
13592
13593       /* If the next token is not a ',', we've reached the end of
13594          the list.  */
13595       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13596         break;
13597       /* Otherwise, consume the `,' and keep going.  */
13598       cp_lexer_consume_token (parser->lexer);
13599       /* If the next token is a `}', there is a trailing comma.  */
13600       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13601         {
13602           if (!in_system_header)
13603             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13604           break;
13605         }
13606     }
13607 }
13608
13609 /* Parse an enumerator-definition.  The enumerator has the indicated
13610    TYPE.
13611
13612    enumerator-definition:
13613      enumerator
13614      enumerator = constant-expression
13615
13616    enumerator:
13617      identifier  */
13618
13619 static void
13620 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13621 {
13622   tree identifier;
13623   tree value;
13624   location_t loc;
13625
13626   /* Save the input location because we are interested in the location
13627      of the identifier and not the location of the explicit value.  */
13628   loc = cp_lexer_peek_token (parser->lexer)->location;
13629
13630   /* Look for the identifier.  */
13631   identifier = cp_parser_identifier (parser);
13632   if (identifier == error_mark_node)
13633     return;
13634
13635   /* If the next token is an '=', then there is an explicit value.  */
13636   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13637     {
13638       /* Consume the `=' token.  */
13639       cp_lexer_consume_token (parser->lexer);
13640       /* Parse the value.  */
13641       value = cp_parser_constant_expression (parser,
13642                                              /*allow_non_constant_p=*/false,
13643                                              NULL);
13644     }
13645   else
13646     value = NULL_TREE;
13647
13648   /* If we are processing a template, make sure the initializer of the
13649      enumerator doesn't contain any bare template parameter pack.  */
13650   if (check_for_bare_parameter_packs (value))
13651     value = error_mark_node;
13652
13653   /* Create the enumerator.  */
13654   build_enumerator (identifier, value, type, loc);
13655 }
13656
13657 /* Parse a namespace-name.
13658
13659    namespace-name:
13660      original-namespace-name
13661      namespace-alias
13662
13663    Returns the NAMESPACE_DECL for the namespace.  */
13664
13665 static tree
13666 cp_parser_namespace_name (cp_parser* parser)
13667 {
13668   tree identifier;
13669   tree namespace_decl;
13670
13671   cp_token *token = cp_lexer_peek_token (parser->lexer);
13672
13673   /* Get the name of the namespace.  */
13674   identifier = cp_parser_identifier (parser);
13675   if (identifier == error_mark_node)
13676     return error_mark_node;
13677
13678   /* Look up the identifier in the currently active scope.  Look only
13679      for namespaces, due to:
13680
13681        [basic.lookup.udir]
13682
13683        When looking up a namespace-name in a using-directive or alias
13684        definition, only namespace names are considered.
13685
13686      And:
13687
13688        [basic.lookup.qual]
13689
13690        During the lookup of a name preceding the :: scope resolution
13691        operator, object, function, and enumerator names are ignored.
13692
13693      (Note that cp_parser_qualifying_entity only calls this
13694      function if the token after the name is the scope resolution
13695      operator.)  */
13696   namespace_decl = cp_parser_lookup_name (parser, identifier,
13697                                           none_type,
13698                                           /*is_template=*/false,
13699                                           /*is_namespace=*/true,
13700                                           /*check_dependency=*/true,
13701                                           /*ambiguous_decls=*/NULL,
13702                                           token->location);
13703   /* If it's not a namespace, issue an error.  */
13704   if (namespace_decl == error_mark_node
13705       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13706     {
13707       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13708         error_at (token->location, "%qD is not a namespace-name", identifier);
13709       cp_parser_error (parser, "expected namespace-name");
13710       namespace_decl = error_mark_node;
13711     }
13712
13713   return namespace_decl;
13714 }
13715
13716 /* Parse a namespace-definition.
13717
13718    namespace-definition:
13719      named-namespace-definition
13720      unnamed-namespace-definition
13721
13722    named-namespace-definition:
13723      original-namespace-definition
13724      extension-namespace-definition
13725
13726    original-namespace-definition:
13727      namespace identifier { namespace-body }
13728
13729    extension-namespace-definition:
13730      namespace original-namespace-name { namespace-body }
13731
13732    unnamed-namespace-definition:
13733      namespace { namespace-body } */
13734
13735 static void
13736 cp_parser_namespace_definition (cp_parser* parser)
13737 {
13738   tree identifier, attribs;
13739   bool has_visibility;
13740   bool is_inline;
13741
13742   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13743     {
13744       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13745       is_inline = true;
13746       cp_lexer_consume_token (parser->lexer);
13747     }
13748   else
13749     is_inline = false;
13750
13751   /* Look for the `namespace' keyword.  */
13752   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13753
13754   /* Get the name of the namespace.  We do not attempt to distinguish
13755      between an original-namespace-definition and an
13756      extension-namespace-definition at this point.  The semantic
13757      analysis routines are responsible for that.  */
13758   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13759     identifier = cp_parser_identifier (parser);
13760   else
13761     identifier = NULL_TREE;
13762
13763   /* Parse any specified attributes.  */
13764   attribs = cp_parser_attributes_opt (parser);
13765
13766   /* Look for the `{' to start the namespace.  */
13767   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13768   /* Start the namespace.  */
13769   push_namespace (identifier);
13770
13771   /* "inline namespace" is equivalent to a stub namespace definition
13772      followed by a strong using directive.  */
13773   if (is_inline)
13774     {
13775       tree name_space = current_namespace;
13776       /* Set up namespace association.  */
13777       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13778         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13779                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13780       /* Import the contents of the inline namespace.  */
13781       pop_namespace ();
13782       do_using_directive (name_space);
13783       push_namespace (identifier);
13784     }
13785
13786   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13787
13788   /* Parse the body of the namespace.  */
13789   cp_parser_namespace_body (parser);
13790
13791 #ifdef HANDLE_PRAGMA_VISIBILITY
13792   if (has_visibility)
13793     pop_visibility (1);
13794 #endif
13795
13796   /* Finish the namespace.  */
13797   pop_namespace ();
13798   /* Look for the final `}'.  */
13799   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13800 }
13801
13802 /* Parse a namespace-body.
13803
13804    namespace-body:
13805      declaration-seq [opt]  */
13806
13807 static void
13808 cp_parser_namespace_body (cp_parser* parser)
13809 {
13810   cp_parser_declaration_seq_opt (parser);
13811 }
13812
13813 /* Parse a namespace-alias-definition.
13814
13815    namespace-alias-definition:
13816      namespace identifier = qualified-namespace-specifier ;  */
13817
13818 static void
13819 cp_parser_namespace_alias_definition (cp_parser* parser)
13820 {
13821   tree identifier;
13822   tree namespace_specifier;
13823
13824   cp_token *token = cp_lexer_peek_token (parser->lexer);
13825
13826   /* Look for the `namespace' keyword.  */
13827   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13828   /* Look for the identifier.  */
13829   identifier = cp_parser_identifier (parser);
13830   if (identifier == error_mark_node)
13831     return;
13832   /* Look for the `=' token.  */
13833   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13834       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13835     {
13836       error_at (token->location, "%<namespace%> definition is not allowed here");
13837       /* Skip the definition.  */
13838       cp_lexer_consume_token (parser->lexer);
13839       if (cp_parser_skip_to_closing_brace (parser))
13840         cp_lexer_consume_token (parser->lexer);
13841       return;
13842     }
13843   cp_parser_require (parser, CPP_EQ, RT_EQ);
13844   /* Look for the qualified-namespace-specifier.  */
13845   namespace_specifier
13846     = cp_parser_qualified_namespace_specifier (parser);
13847   /* Look for the `;' token.  */
13848   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13849
13850   /* Register the alias in the symbol table.  */
13851   do_namespace_alias (identifier, namespace_specifier);
13852 }
13853
13854 /* Parse a qualified-namespace-specifier.
13855
13856    qualified-namespace-specifier:
13857      :: [opt] nested-name-specifier [opt] namespace-name
13858
13859    Returns a NAMESPACE_DECL corresponding to the specified
13860    namespace.  */
13861
13862 static tree
13863 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13864 {
13865   /* Look for the optional `::'.  */
13866   cp_parser_global_scope_opt (parser,
13867                               /*current_scope_valid_p=*/false);
13868
13869   /* Look for the optional nested-name-specifier.  */
13870   cp_parser_nested_name_specifier_opt (parser,
13871                                        /*typename_keyword_p=*/false,
13872                                        /*check_dependency_p=*/true,
13873                                        /*type_p=*/false,
13874                                        /*is_declaration=*/true);
13875
13876   return cp_parser_namespace_name (parser);
13877 }
13878
13879 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13880    access declaration.
13881
13882    using-declaration:
13883      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13884      using :: unqualified-id ;  
13885
13886    access-declaration:
13887      qualified-id ;  
13888
13889    */
13890
13891 static bool
13892 cp_parser_using_declaration (cp_parser* parser, 
13893                              bool access_declaration_p)
13894 {
13895   cp_token *token;
13896   bool typename_p = false;
13897   bool global_scope_p;
13898   tree decl;
13899   tree identifier;
13900   tree qscope;
13901
13902   if (access_declaration_p)
13903     cp_parser_parse_tentatively (parser);
13904   else
13905     {
13906       /* Look for the `using' keyword.  */
13907       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13908       
13909       /* Peek at the next token.  */
13910       token = cp_lexer_peek_token (parser->lexer);
13911       /* See if it's `typename'.  */
13912       if (token->keyword == RID_TYPENAME)
13913         {
13914           /* Remember that we've seen it.  */
13915           typename_p = true;
13916           /* Consume the `typename' token.  */
13917           cp_lexer_consume_token (parser->lexer);
13918         }
13919     }
13920
13921   /* Look for the optional global scope qualification.  */
13922   global_scope_p
13923     = (cp_parser_global_scope_opt (parser,
13924                                    /*current_scope_valid_p=*/false)
13925        != NULL_TREE);
13926
13927   /* If we saw `typename', or didn't see `::', then there must be a
13928      nested-name-specifier present.  */
13929   if (typename_p || !global_scope_p)
13930     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13931                                               /*check_dependency_p=*/true,
13932                                               /*type_p=*/false,
13933                                               /*is_declaration=*/true);
13934   /* Otherwise, we could be in either of the two productions.  In that
13935      case, treat the nested-name-specifier as optional.  */
13936   else
13937     qscope = cp_parser_nested_name_specifier_opt (parser,
13938                                                   /*typename_keyword_p=*/false,
13939                                                   /*check_dependency_p=*/true,
13940                                                   /*type_p=*/false,
13941                                                   /*is_declaration=*/true);
13942   if (!qscope)
13943     qscope = global_namespace;
13944
13945   if (access_declaration_p && cp_parser_error_occurred (parser))
13946     /* Something has already gone wrong; there's no need to parse
13947        further.  Since an error has occurred, the return value of
13948        cp_parser_parse_definitely will be false, as required.  */
13949     return cp_parser_parse_definitely (parser);
13950
13951   token = cp_lexer_peek_token (parser->lexer);
13952   /* Parse the unqualified-id.  */
13953   identifier = cp_parser_unqualified_id (parser,
13954                                          /*template_keyword_p=*/false,
13955                                          /*check_dependency_p=*/true,
13956                                          /*declarator_p=*/true,
13957                                          /*optional_p=*/false);
13958
13959   if (access_declaration_p)
13960     {
13961       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13962         cp_parser_simulate_error (parser);
13963       if (!cp_parser_parse_definitely (parser))
13964         return false;
13965     }
13966
13967   /* The function we call to handle a using-declaration is different
13968      depending on what scope we are in.  */
13969   if (qscope == error_mark_node || identifier == error_mark_node)
13970     ;
13971   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13972            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13973     /* [namespace.udecl]
13974
13975        A using declaration shall not name a template-id.  */
13976     error_at (token->location,
13977               "a template-id may not appear in a using-declaration");
13978   else
13979     {
13980       if (at_class_scope_p ())
13981         {
13982           /* Create the USING_DECL.  */
13983           decl = do_class_using_decl (parser->scope, identifier);
13984
13985           if (check_for_bare_parameter_packs (decl))
13986             return false;
13987           else
13988             /* Add it to the list of members in this class.  */
13989             finish_member_declaration (decl);
13990         }
13991       else
13992         {
13993           decl = cp_parser_lookup_name_simple (parser,
13994                                                identifier,
13995                                                token->location);
13996           if (decl == error_mark_node)
13997             cp_parser_name_lookup_error (parser, identifier,
13998                                          decl, NLE_NULL,
13999                                          token->location);
14000           else if (check_for_bare_parameter_packs (decl))
14001             return false;
14002           else if (!at_namespace_scope_p ())
14003             do_local_using_decl (decl, qscope, identifier);
14004           else
14005             do_toplevel_using_decl (decl, qscope, identifier);
14006         }
14007     }
14008
14009   /* Look for the final `;'.  */
14010   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14011   
14012   return true;
14013 }
14014
14015 /* Parse a using-directive.
14016
14017    using-directive:
14018      using namespace :: [opt] nested-name-specifier [opt]
14019        namespace-name ;  */
14020
14021 static void
14022 cp_parser_using_directive (cp_parser* parser)
14023 {
14024   tree namespace_decl;
14025   tree attribs;
14026
14027   /* Look for the `using' keyword.  */
14028   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14029   /* And the `namespace' keyword.  */
14030   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14031   /* Look for the optional `::' operator.  */
14032   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14033   /* And the optional nested-name-specifier.  */
14034   cp_parser_nested_name_specifier_opt (parser,
14035                                        /*typename_keyword_p=*/false,
14036                                        /*check_dependency_p=*/true,
14037                                        /*type_p=*/false,
14038                                        /*is_declaration=*/true);
14039   /* Get the namespace being used.  */
14040   namespace_decl = cp_parser_namespace_name (parser);
14041   /* And any specified attributes.  */
14042   attribs = cp_parser_attributes_opt (parser);
14043   /* Update the symbol table.  */
14044   parse_using_directive (namespace_decl, attribs);
14045   /* Look for the final `;'.  */
14046   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14047 }
14048
14049 /* Parse an asm-definition.
14050
14051    asm-definition:
14052      asm ( string-literal ) ;
14053
14054    GNU Extension:
14055
14056    asm-definition:
14057      asm volatile [opt] ( string-literal ) ;
14058      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14059      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14060                           : asm-operand-list [opt] ) ;
14061      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14062                           : asm-operand-list [opt]
14063                           : asm-clobber-list [opt] ) ;
14064      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14065                                : asm-clobber-list [opt]
14066                                : asm-goto-list ) ;  */
14067
14068 static void
14069 cp_parser_asm_definition (cp_parser* parser)
14070 {
14071   tree string;
14072   tree outputs = NULL_TREE;
14073   tree inputs = NULL_TREE;
14074   tree clobbers = NULL_TREE;
14075   tree labels = NULL_TREE;
14076   tree asm_stmt;
14077   bool volatile_p = false;
14078   bool extended_p = false;
14079   bool invalid_inputs_p = false;
14080   bool invalid_outputs_p = false;
14081   bool goto_p = false;
14082   required_token missing = RT_NONE;
14083
14084   /* Look for the `asm' keyword.  */
14085   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14086   /* See if the next token is `volatile'.  */
14087   if (cp_parser_allow_gnu_extensions_p (parser)
14088       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14089     {
14090       /* Remember that we saw the `volatile' keyword.  */
14091       volatile_p = true;
14092       /* Consume the token.  */
14093       cp_lexer_consume_token (parser->lexer);
14094     }
14095   if (cp_parser_allow_gnu_extensions_p (parser)
14096       && parser->in_function_body
14097       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14098     {
14099       /* Remember that we saw the `goto' keyword.  */
14100       goto_p = true;
14101       /* Consume the token.  */
14102       cp_lexer_consume_token (parser->lexer);
14103     }
14104   /* Look for the opening `('.  */
14105   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14106     return;
14107   /* Look for the string.  */
14108   string = cp_parser_string_literal (parser, false, false);
14109   if (string == error_mark_node)
14110     {
14111       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14112                                              /*consume_paren=*/true);
14113       return;
14114     }
14115
14116   /* If we're allowing GNU extensions, check for the extended assembly
14117      syntax.  Unfortunately, the `:' tokens need not be separated by
14118      a space in C, and so, for compatibility, we tolerate that here
14119      too.  Doing that means that we have to treat the `::' operator as
14120      two `:' tokens.  */
14121   if (cp_parser_allow_gnu_extensions_p (parser)
14122       && parser->in_function_body
14123       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14124           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14125     {
14126       bool inputs_p = false;
14127       bool clobbers_p = false;
14128       bool labels_p = false;
14129
14130       /* The extended syntax was used.  */
14131       extended_p = true;
14132
14133       /* Look for outputs.  */
14134       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14135         {
14136           /* Consume the `:'.  */
14137           cp_lexer_consume_token (parser->lexer);
14138           /* Parse the output-operands.  */
14139           if (cp_lexer_next_token_is_not (parser->lexer,
14140                                           CPP_COLON)
14141               && cp_lexer_next_token_is_not (parser->lexer,
14142                                              CPP_SCOPE)
14143               && cp_lexer_next_token_is_not (parser->lexer,
14144                                              CPP_CLOSE_PAREN)
14145               && !goto_p)
14146             outputs = cp_parser_asm_operand_list (parser);
14147
14148             if (outputs == error_mark_node)
14149               invalid_outputs_p = true;
14150         }
14151       /* If the next token is `::', there are no outputs, and the
14152          next token is the beginning of the inputs.  */
14153       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14154         /* The inputs are coming next.  */
14155         inputs_p = true;
14156
14157       /* Look for inputs.  */
14158       if (inputs_p
14159           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14160         {
14161           /* Consume the `:' or `::'.  */
14162           cp_lexer_consume_token (parser->lexer);
14163           /* Parse the output-operands.  */
14164           if (cp_lexer_next_token_is_not (parser->lexer,
14165                                           CPP_COLON)
14166               && cp_lexer_next_token_is_not (parser->lexer,
14167                                              CPP_SCOPE)
14168               && cp_lexer_next_token_is_not (parser->lexer,
14169                                              CPP_CLOSE_PAREN))
14170             inputs = cp_parser_asm_operand_list (parser);
14171
14172             if (inputs == error_mark_node)
14173               invalid_inputs_p = true;
14174         }
14175       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14176         /* The clobbers are coming next.  */
14177         clobbers_p = true;
14178
14179       /* Look for clobbers.  */
14180       if (clobbers_p
14181           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14182         {
14183           clobbers_p = true;
14184           /* Consume the `:' or `::'.  */
14185           cp_lexer_consume_token (parser->lexer);
14186           /* Parse the clobbers.  */
14187           if (cp_lexer_next_token_is_not (parser->lexer,
14188                                           CPP_COLON)
14189               && cp_lexer_next_token_is_not (parser->lexer,
14190                                              CPP_CLOSE_PAREN))
14191             clobbers = cp_parser_asm_clobber_list (parser);
14192         }
14193       else if (goto_p
14194                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14195         /* The labels are coming next.  */
14196         labels_p = true;
14197
14198       /* Look for labels.  */
14199       if (labels_p
14200           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14201         {
14202           labels_p = true;
14203           /* Consume the `:' or `::'.  */
14204           cp_lexer_consume_token (parser->lexer);
14205           /* Parse the labels.  */
14206           labels = cp_parser_asm_label_list (parser);
14207         }
14208
14209       if (goto_p && !labels_p)
14210         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14211     }
14212   else if (goto_p)
14213     missing = RT_COLON_SCOPE;
14214
14215   /* Look for the closing `)'.  */
14216   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14217                           missing ? missing : RT_CLOSE_PAREN))
14218     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14219                                            /*consume_paren=*/true);
14220   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14221
14222   if (!invalid_inputs_p && !invalid_outputs_p)
14223     {
14224       /* Create the ASM_EXPR.  */
14225       if (parser->in_function_body)
14226         {
14227           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14228                                       inputs, clobbers, labels);
14229           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14230           if (!extended_p)
14231             {
14232               tree temp = asm_stmt;
14233               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14234                 temp = TREE_OPERAND (temp, 0);
14235
14236               ASM_INPUT_P (temp) = 1;
14237             }
14238         }
14239       else
14240         cgraph_add_asm_node (string);
14241     }
14242 }
14243
14244 /* Declarators [gram.dcl.decl] */
14245
14246 /* Parse an init-declarator.
14247
14248    init-declarator:
14249      declarator initializer [opt]
14250
14251    GNU Extension:
14252
14253    init-declarator:
14254      declarator asm-specification [opt] attributes [opt] initializer [opt]
14255
14256    function-definition:
14257      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14258        function-body
14259      decl-specifier-seq [opt] declarator function-try-block
14260
14261    GNU Extension:
14262
14263    function-definition:
14264      __extension__ function-definition
14265
14266    The DECL_SPECIFIERS apply to this declarator.  Returns a
14267    representation of the entity declared.  If MEMBER_P is TRUE, then
14268    this declarator appears in a class scope.  The new DECL created by
14269    this declarator is returned.
14270
14271    The CHECKS are access checks that should be performed once we know
14272    what entity is being declared (and, therefore, what classes have
14273    befriended it).
14274
14275    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14276    for a function-definition here as well.  If the declarator is a
14277    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14278    be TRUE upon return.  By that point, the function-definition will
14279    have been completely parsed.
14280
14281    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14282    is FALSE.  */
14283
14284 static tree
14285 cp_parser_init_declarator (cp_parser* parser,
14286                            cp_decl_specifier_seq *decl_specifiers,
14287                            VEC (deferred_access_check,gc)* checks,
14288                            bool function_definition_allowed_p,
14289                            bool member_p,
14290                            int declares_class_or_enum,
14291                            bool* function_definition_p)
14292 {
14293   cp_token *token = NULL, *asm_spec_start_token = NULL,
14294            *attributes_start_token = NULL;
14295   cp_declarator *declarator;
14296   tree prefix_attributes;
14297   tree attributes;
14298   tree asm_specification;
14299   tree initializer;
14300   tree decl = NULL_TREE;
14301   tree scope;
14302   int is_initialized;
14303   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14304      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14305      "(...)".  */
14306   enum cpp_ttype initialization_kind;
14307   bool is_direct_init = false;
14308   bool is_non_constant_init;
14309   int ctor_dtor_or_conv_p;
14310   bool friend_p;
14311   tree pushed_scope = NULL;
14312
14313   /* Gather the attributes that were provided with the
14314      decl-specifiers.  */
14315   prefix_attributes = decl_specifiers->attributes;
14316
14317   /* Assume that this is not the declarator for a function
14318      definition.  */
14319   if (function_definition_p)
14320     *function_definition_p = false;
14321
14322   /* Defer access checks while parsing the declarator; we cannot know
14323      what names are accessible until we know what is being
14324      declared.  */
14325   resume_deferring_access_checks ();
14326
14327   /* Parse the declarator.  */
14328   token = cp_lexer_peek_token (parser->lexer);
14329   declarator
14330     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14331                             &ctor_dtor_or_conv_p,
14332                             /*parenthesized_p=*/NULL,
14333                             /*member_p=*/false);
14334   /* Gather up the deferred checks.  */
14335   stop_deferring_access_checks ();
14336
14337   /* If the DECLARATOR was erroneous, there's no need to go
14338      further.  */
14339   if (declarator == cp_error_declarator)
14340     return error_mark_node;
14341
14342   /* Check that the number of template-parameter-lists is OK.  */
14343   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14344                                                        token->location))
14345     return error_mark_node;
14346
14347   if (declares_class_or_enum & 2)
14348     cp_parser_check_for_definition_in_return_type (declarator,
14349                                                    decl_specifiers->type,
14350                                                    decl_specifiers->type_location);
14351
14352   /* Figure out what scope the entity declared by the DECLARATOR is
14353      located in.  `grokdeclarator' sometimes changes the scope, so
14354      we compute it now.  */
14355   scope = get_scope_of_declarator (declarator);
14356
14357   /* Perform any lookups in the declared type which were thought to be
14358      dependent, but are not in the scope of the declarator.  */
14359   decl_specifiers->type
14360     = maybe_update_decl_type (decl_specifiers->type, scope);
14361
14362   /* If we're allowing GNU extensions, look for an asm-specification
14363      and attributes.  */
14364   if (cp_parser_allow_gnu_extensions_p (parser))
14365     {
14366       /* Look for an asm-specification.  */
14367       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14368       asm_specification = cp_parser_asm_specification_opt (parser);
14369       /* And attributes.  */
14370       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14371       attributes = cp_parser_attributes_opt (parser);
14372     }
14373   else
14374     {
14375       asm_specification = NULL_TREE;
14376       attributes = NULL_TREE;
14377     }
14378
14379   /* Peek at the next token.  */
14380   token = cp_lexer_peek_token (parser->lexer);
14381   /* Check to see if the token indicates the start of a
14382      function-definition.  */
14383   if (function_declarator_p (declarator)
14384       && cp_parser_token_starts_function_definition_p (token))
14385     {
14386       if (!function_definition_allowed_p)
14387         {
14388           /* If a function-definition should not appear here, issue an
14389              error message.  */
14390           cp_parser_error (parser,
14391                            "a function-definition is not allowed here");
14392           return error_mark_node;
14393         }
14394       else
14395         {
14396           location_t func_brace_location
14397             = cp_lexer_peek_token (parser->lexer)->location;
14398
14399           /* Neither attributes nor an asm-specification are allowed
14400              on a function-definition.  */
14401           if (asm_specification)
14402             error_at (asm_spec_start_token->location,
14403                       "an asm-specification is not allowed "
14404                       "on a function-definition");
14405           if (attributes)
14406             error_at (attributes_start_token->location,
14407                       "attributes are not allowed on a function-definition");
14408           /* This is a function-definition.  */
14409           *function_definition_p = true;
14410
14411           /* Parse the function definition.  */
14412           if (member_p)
14413             decl = cp_parser_save_member_function_body (parser,
14414                                                         decl_specifiers,
14415                                                         declarator,
14416                                                         prefix_attributes);
14417           else
14418             decl
14419               = (cp_parser_function_definition_from_specifiers_and_declarator
14420                  (parser, decl_specifiers, prefix_attributes, declarator));
14421
14422           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14423             {
14424               /* This is where the prologue starts...  */
14425               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14426                 = func_brace_location;
14427             }
14428
14429           return decl;
14430         }
14431     }
14432
14433   /* [dcl.dcl]
14434
14435      Only in function declarations for constructors, destructors, and
14436      type conversions can the decl-specifier-seq be omitted.
14437
14438      We explicitly postpone this check past the point where we handle
14439      function-definitions because we tolerate function-definitions
14440      that are missing their return types in some modes.  */
14441   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14442     {
14443       cp_parser_error (parser,
14444                        "expected constructor, destructor, or type conversion");
14445       return error_mark_node;
14446     }
14447
14448   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14449   if (token->type == CPP_EQ
14450       || token->type == CPP_OPEN_PAREN
14451       || token->type == CPP_OPEN_BRACE)
14452     {
14453       is_initialized = SD_INITIALIZED;
14454       initialization_kind = token->type;
14455
14456       if (token->type == CPP_EQ
14457           && function_declarator_p (declarator))
14458         {
14459           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14460           if (t2->keyword == RID_DEFAULT)
14461             is_initialized = SD_DEFAULTED;
14462           else if (t2->keyword == RID_DELETE)
14463             is_initialized = SD_DELETED;
14464         }
14465     }
14466   else
14467     {
14468       /* If the init-declarator isn't initialized and isn't followed by a
14469          `,' or `;', it's not a valid init-declarator.  */
14470       if (token->type != CPP_COMMA
14471           && token->type != CPP_SEMICOLON)
14472         {
14473           cp_parser_error (parser, "expected initializer");
14474           return error_mark_node;
14475         }
14476       is_initialized = SD_UNINITIALIZED;
14477       initialization_kind = CPP_EOF;
14478     }
14479
14480   /* Because start_decl has side-effects, we should only call it if we
14481      know we're going ahead.  By this point, we know that we cannot
14482      possibly be looking at any other construct.  */
14483   cp_parser_commit_to_tentative_parse (parser);
14484
14485   /* If the decl specifiers were bad, issue an error now that we're
14486      sure this was intended to be a declarator.  Then continue
14487      declaring the variable(s), as int, to try to cut down on further
14488      errors.  */
14489   if (decl_specifiers->any_specifiers_p
14490       && decl_specifiers->type == error_mark_node)
14491     {
14492       cp_parser_error (parser, "invalid type in declaration");
14493       decl_specifiers->type = integer_type_node;
14494     }
14495
14496   /* Check to see whether or not this declaration is a friend.  */
14497   friend_p = cp_parser_friend_p (decl_specifiers);
14498
14499   /* Enter the newly declared entry in the symbol table.  If we're
14500      processing a declaration in a class-specifier, we wait until
14501      after processing the initializer.  */
14502   if (!member_p)
14503     {
14504       if (parser->in_unbraced_linkage_specification_p)
14505         decl_specifiers->storage_class = sc_extern;
14506       decl = start_decl (declarator, decl_specifiers,
14507                          is_initialized, attributes, prefix_attributes,
14508                          &pushed_scope);
14509       /* Adjust location of decl if declarator->id_loc is more appropriate:
14510          set, and decl wasn't merged with another decl, in which case its
14511          location would be different from input_location, and more accurate.  */
14512       if (DECL_P (decl)
14513           && declarator->id_loc != UNKNOWN_LOCATION
14514           && DECL_SOURCE_LOCATION (decl) == input_location)
14515         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14516     }
14517   else if (scope)
14518     /* Enter the SCOPE.  That way unqualified names appearing in the
14519        initializer will be looked up in SCOPE.  */
14520     pushed_scope = push_scope (scope);
14521
14522   /* Perform deferred access control checks, now that we know in which
14523      SCOPE the declared entity resides.  */
14524   if (!member_p && decl)
14525     {
14526       tree saved_current_function_decl = NULL_TREE;
14527
14528       /* If the entity being declared is a function, pretend that we
14529          are in its scope.  If it is a `friend', it may have access to
14530          things that would not otherwise be accessible.  */
14531       if (TREE_CODE (decl) == FUNCTION_DECL)
14532         {
14533           saved_current_function_decl = current_function_decl;
14534           current_function_decl = decl;
14535         }
14536
14537       /* Perform access checks for template parameters.  */
14538       cp_parser_perform_template_parameter_access_checks (checks);
14539
14540       /* Perform the access control checks for the declarator and the
14541          decl-specifiers.  */
14542       perform_deferred_access_checks ();
14543
14544       /* Restore the saved value.  */
14545       if (TREE_CODE (decl) == FUNCTION_DECL)
14546         current_function_decl = saved_current_function_decl;
14547     }
14548
14549   /* Parse the initializer.  */
14550   initializer = NULL_TREE;
14551   is_direct_init = false;
14552   is_non_constant_init = true;
14553   if (is_initialized)
14554     {
14555       if (function_declarator_p (declarator))
14556         {
14557           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14558            if (initialization_kind == CPP_EQ)
14559              initializer = cp_parser_pure_specifier (parser);
14560            else
14561              {
14562                /* If the declaration was erroneous, we don't really
14563                   know what the user intended, so just silently
14564                   consume the initializer.  */
14565                if (decl != error_mark_node)
14566                  error_at (initializer_start_token->location,
14567                            "initializer provided for function");
14568                cp_parser_skip_to_closing_parenthesis (parser,
14569                                                       /*recovering=*/true,
14570                                                       /*or_comma=*/false,
14571                                                       /*consume_paren=*/true);
14572              }
14573         }
14574       else
14575         {
14576           /* We want to record the extra mangling scope for in-class
14577              initializers of class members and initializers of static data
14578              member templates.  The former is a C++0x feature which isn't
14579              implemented yet, and I expect it will involve deferring
14580              parsing of the initializer until end of class as with default
14581              arguments.  So right here we only handle the latter.  */
14582           if (!member_p && processing_template_decl)
14583             start_lambda_scope (decl);
14584           initializer = cp_parser_initializer (parser,
14585                                                &is_direct_init,
14586                                                &is_non_constant_init);
14587           if (!member_p && processing_template_decl)
14588             finish_lambda_scope ();
14589         }
14590     }
14591
14592   /* The old parser allows attributes to appear after a parenthesized
14593      initializer.  Mark Mitchell proposed removing this functionality
14594      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14595      attributes -- but ignores them.  */
14596   if (cp_parser_allow_gnu_extensions_p (parser)
14597       && initialization_kind == CPP_OPEN_PAREN)
14598     if (cp_parser_attributes_opt (parser))
14599       warning (OPT_Wattributes,
14600                "attributes after parenthesized initializer ignored");
14601
14602   /* For an in-class declaration, use `grokfield' to create the
14603      declaration.  */
14604   if (member_p)
14605     {
14606       if (pushed_scope)
14607         {
14608           pop_scope (pushed_scope);
14609           pushed_scope = false;
14610         }
14611       decl = grokfield (declarator, decl_specifiers,
14612                         initializer, !is_non_constant_init,
14613                         /*asmspec=*/NULL_TREE,
14614                         prefix_attributes);
14615       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14616         cp_parser_save_default_args (parser, decl);
14617     }
14618
14619   /* Finish processing the declaration.  But, skip friend
14620      declarations.  */
14621   if (!friend_p && decl && decl != error_mark_node)
14622     {
14623       cp_finish_decl (decl,
14624                       initializer, !is_non_constant_init,
14625                       asm_specification,
14626                       /* If the initializer is in parentheses, then this is
14627                          a direct-initialization, which means that an
14628                          `explicit' constructor is OK.  Otherwise, an
14629                          `explicit' constructor cannot be used.  */
14630                       ((is_direct_init || !is_initialized)
14631                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14632     }
14633   else if ((cxx_dialect != cxx98) && friend_p
14634            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14635     /* Core issue #226 (C++0x only): A default template-argument
14636        shall not be specified in a friend class template
14637        declaration. */
14638     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14639                              /*is_partial=*/0, /*is_friend_decl=*/1);
14640
14641   if (!friend_p && pushed_scope)
14642     pop_scope (pushed_scope);
14643
14644   return decl;
14645 }
14646
14647 /* Parse a declarator.
14648
14649    declarator:
14650      direct-declarator
14651      ptr-operator declarator
14652
14653    abstract-declarator:
14654      ptr-operator abstract-declarator [opt]
14655      direct-abstract-declarator
14656
14657    GNU Extensions:
14658
14659    declarator:
14660      attributes [opt] direct-declarator
14661      attributes [opt] ptr-operator declarator
14662
14663    abstract-declarator:
14664      attributes [opt] ptr-operator abstract-declarator [opt]
14665      attributes [opt] direct-abstract-declarator
14666
14667    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14668    detect constructor, destructor or conversion operators. It is set
14669    to -1 if the declarator is a name, and +1 if it is a
14670    function. Otherwise it is set to zero. Usually you just want to
14671    test for >0, but internally the negative value is used.
14672
14673    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14674    a decl-specifier-seq unless it declares a constructor, destructor,
14675    or conversion.  It might seem that we could check this condition in
14676    semantic analysis, rather than parsing, but that makes it difficult
14677    to handle something like `f()'.  We want to notice that there are
14678    no decl-specifiers, and therefore realize that this is an
14679    expression, not a declaration.)
14680
14681    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14682    the declarator is a direct-declarator of the form "(...)".
14683
14684    MEMBER_P is true iff this declarator is a member-declarator.  */
14685
14686 static cp_declarator *
14687 cp_parser_declarator (cp_parser* parser,
14688                       cp_parser_declarator_kind dcl_kind,
14689                       int* ctor_dtor_or_conv_p,
14690                       bool* parenthesized_p,
14691                       bool member_p)
14692 {
14693   cp_declarator *declarator;
14694   enum tree_code code;
14695   cp_cv_quals cv_quals;
14696   tree class_type;
14697   tree attributes = NULL_TREE;
14698
14699   /* Assume this is not a constructor, destructor, or type-conversion
14700      operator.  */
14701   if (ctor_dtor_or_conv_p)
14702     *ctor_dtor_or_conv_p = 0;
14703
14704   if (cp_parser_allow_gnu_extensions_p (parser))
14705     attributes = cp_parser_attributes_opt (parser);
14706
14707   /* Check for the ptr-operator production.  */
14708   cp_parser_parse_tentatively (parser);
14709   /* Parse the ptr-operator.  */
14710   code = cp_parser_ptr_operator (parser,
14711                                  &class_type,
14712                                  &cv_quals);
14713   /* If that worked, then we have a ptr-operator.  */
14714   if (cp_parser_parse_definitely (parser))
14715     {
14716       /* If a ptr-operator was found, then this declarator was not
14717          parenthesized.  */
14718       if (parenthesized_p)
14719         *parenthesized_p = true;
14720       /* The dependent declarator is optional if we are parsing an
14721          abstract-declarator.  */
14722       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14723         cp_parser_parse_tentatively (parser);
14724
14725       /* Parse the dependent declarator.  */
14726       declarator = cp_parser_declarator (parser, dcl_kind,
14727                                          /*ctor_dtor_or_conv_p=*/NULL,
14728                                          /*parenthesized_p=*/NULL,
14729                                          /*member_p=*/false);
14730
14731       /* If we are parsing an abstract-declarator, we must handle the
14732          case where the dependent declarator is absent.  */
14733       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14734           && !cp_parser_parse_definitely (parser))
14735         declarator = NULL;
14736
14737       declarator = cp_parser_make_indirect_declarator
14738         (code, class_type, cv_quals, declarator);
14739     }
14740   /* Everything else is a direct-declarator.  */
14741   else
14742     {
14743       if (parenthesized_p)
14744         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14745                                                    CPP_OPEN_PAREN);
14746       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14747                                                 ctor_dtor_or_conv_p,
14748                                                 member_p);
14749     }
14750
14751   if (attributes && declarator && declarator != cp_error_declarator)
14752     declarator->attributes = attributes;
14753
14754   return declarator;
14755 }
14756
14757 /* Parse a direct-declarator or direct-abstract-declarator.
14758
14759    direct-declarator:
14760      declarator-id
14761      direct-declarator ( parameter-declaration-clause )
14762        cv-qualifier-seq [opt]
14763        exception-specification [opt]
14764      direct-declarator [ constant-expression [opt] ]
14765      ( declarator )
14766
14767    direct-abstract-declarator:
14768      direct-abstract-declarator [opt]
14769        ( parameter-declaration-clause )
14770        cv-qualifier-seq [opt]
14771        exception-specification [opt]
14772      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14773      ( abstract-declarator )
14774
14775    Returns a representation of the declarator.  DCL_KIND is
14776    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14777    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14778    we are parsing a direct-declarator.  It is
14779    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14780    of ambiguity we prefer an abstract declarator, as per
14781    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14782    cp_parser_declarator.  */
14783
14784 static cp_declarator *
14785 cp_parser_direct_declarator (cp_parser* parser,
14786                              cp_parser_declarator_kind dcl_kind,
14787                              int* ctor_dtor_or_conv_p,
14788                              bool member_p)
14789 {
14790   cp_token *token;
14791   cp_declarator *declarator = NULL;
14792   tree scope = NULL_TREE;
14793   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14794   bool saved_in_declarator_p = parser->in_declarator_p;
14795   bool first = true;
14796   tree pushed_scope = NULL_TREE;
14797
14798   while (true)
14799     {
14800       /* Peek at the next token.  */
14801       token = cp_lexer_peek_token (parser->lexer);
14802       if (token->type == CPP_OPEN_PAREN)
14803         {
14804           /* This is either a parameter-declaration-clause, or a
14805              parenthesized declarator. When we know we are parsing a
14806              named declarator, it must be a parenthesized declarator
14807              if FIRST is true. For instance, `(int)' is a
14808              parameter-declaration-clause, with an omitted
14809              direct-abstract-declarator. But `((*))', is a
14810              parenthesized abstract declarator. Finally, when T is a
14811              template parameter `(T)' is a
14812              parameter-declaration-clause, and not a parenthesized
14813              named declarator.
14814
14815              We first try and parse a parameter-declaration-clause,
14816              and then try a nested declarator (if FIRST is true).
14817
14818              It is not an error for it not to be a
14819              parameter-declaration-clause, even when FIRST is
14820              false. Consider,
14821
14822                int i (int);
14823                int i (3);
14824
14825              The first is the declaration of a function while the
14826              second is the definition of a variable, including its
14827              initializer.
14828
14829              Having seen only the parenthesis, we cannot know which of
14830              these two alternatives should be selected.  Even more
14831              complex are examples like:
14832
14833                int i (int (a));
14834                int i (int (3));
14835
14836              The former is a function-declaration; the latter is a
14837              variable initialization.
14838
14839              Thus again, we try a parameter-declaration-clause, and if
14840              that fails, we back out and return.  */
14841
14842           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14843             {
14844               tree params;
14845               unsigned saved_num_template_parameter_lists;
14846               bool is_declarator = false;
14847               tree t;
14848
14849               /* In a member-declarator, the only valid interpretation
14850                  of a parenthesis is the start of a
14851                  parameter-declaration-clause.  (It is invalid to
14852                  initialize a static data member with a parenthesized
14853                  initializer; only the "=" form of initialization is
14854                  permitted.)  */
14855               if (!member_p)
14856                 cp_parser_parse_tentatively (parser);
14857
14858               /* Consume the `('.  */
14859               cp_lexer_consume_token (parser->lexer);
14860               if (first)
14861                 {
14862                   /* If this is going to be an abstract declarator, we're
14863                      in a declarator and we can't have default args.  */
14864                   parser->default_arg_ok_p = false;
14865                   parser->in_declarator_p = true;
14866                 }
14867
14868               /* Inside the function parameter list, surrounding
14869                  template-parameter-lists do not apply.  */
14870               saved_num_template_parameter_lists
14871                 = parser->num_template_parameter_lists;
14872               parser->num_template_parameter_lists = 0;
14873
14874               begin_scope (sk_function_parms, NULL_TREE);
14875
14876               /* Parse the parameter-declaration-clause.  */
14877               params = cp_parser_parameter_declaration_clause (parser);
14878
14879               parser->num_template_parameter_lists
14880                 = saved_num_template_parameter_lists;
14881
14882               /* If all went well, parse the cv-qualifier-seq and the
14883                  exception-specification.  */
14884               if (member_p || cp_parser_parse_definitely (parser))
14885                 {
14886                   cp_cv_quals cv_quals;
14887                   tree exception_specification;
14888                   tree late_return;
14889
14890                   is_declarator = true;
14891
14892                   if (ctor_dtor_or_conv_p)
14893                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14894                   first = false;
14895                   /* Consume the `)'.  */
14896                   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14897
14898                   /* Parse the cv-qualifier-seq.  */
14899                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14900                   /* And the exception-specification.  */
14901                   exception_specification
14902                     = cp_parser_exception_specification_opt (parser);
14903
14904                   late_return
14905                     = cp_parser_late_return_type_opt (parser);
14906
14907                   /* Create the function-declarator.  */
14908                   declarator = make_call_declarator (declarator,
14909                                                      params,
14910                                                      cv_quals,
14911                                                      exception_specification,
14912                                                      late_return);
14913                   /* Any subsequent parameter lists are to do with
14914                      return type, so are not those of the declared
14915                      function.  */
14916                   parser->default_arg_ok_p = false;
14917                 }
14918
14919               /* Remove the function parms from scope.  */
14920               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14921                 pop_binding (DECL_NAME (t), t);
14922               leave_scope();
14923
14924               if (is_declarator)
14925                 /* Repeat the main loop.  */
14926                 continue;
14927             }
14928
14929           /* If this is the first, we can try a parenthesized
14930              declarator.  */
14931           if (first)
14932             {
14933               bool saved_in_type_id_in_expr_p;
14934
14935               parser->default_arg_ok_p = saved_default_arg_ok_p;
14936               parser->in_declarator_p = saved_in_declarator_p;
14937
14938               /* Consume the `('.  */
14939               cp_lexer_consume_token (parser->lexer);
14940               /* Parse the nested declarator.  */
14941               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14942               parser->in_type_id_in_expr_p = true;
14943               declarator
14944                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14945                                         /*parenthesized_p=*/NULL,
14946                                         member_p);
14947               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14948               first = false;
14949               /* Expect a `)'.  */
14950               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14951                 declarator = cp_error_declarator;
14952               if (declarator == cp_error_declarator)
14953                 break;
14954
14955               goto handle_declarator;
14956             }
14957           /* Otherwise, we must be done.  */
14958           else
14959             break;
14960         }
14961       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14962                && token->type == CPP_OPEN_SQUARE)
14963         {
14964           /* Parse an array-declarator.  */
14965           tree bounds;
14966
14967           if (ctor_dtor_or_conv_p)
14968             *ctor_dtor_or_conv_p = 0;
14969
14970           first = false;
14971           parser->default_arg_ok_p = false;
14972           parser->in_declarator_p = true;
14973           /* Consume the `['.  */
14974           cp_lexer_consume_token (parser->lexer);
14975           /* Peek at the next token.  */
14976           token = cp_lexer_peek_token (parser->lexer);
14977           /* If the next token is `]', then there is no
14978              constant-expression.  */
14979           if (token->type != CPP_CLOSE_SQUARE)
14980             {
14981               bool non_constant_p;
14982
14983               bounds
14984                 = cp_parser_constant_expression (parser,
14985                                                  /*allow_non_constant=*/true,
14986                                                  &non_constant_p);
14987               if (!non_constant_p || cxx_dialect >= cxx0x)
14988                 /* OK */;
14989               /* Normally, the array bound must be an integral constant
14990                  expression.  However, as an extension, we allow VLAs
14991                  in function scopes as long as they aren't part of a
14992                  parameter declaration.  */
14993               else if (!parser->in_function_body
14994                        || current_binding_level->kind == sk_function_parms)
14995                 {
14996                   cp_parser_error (parser,
14997                                    "array bound is not an integer constant");
14998                   bounds = error_mark_node;
14999                 }
15000               else if (processing_template_decl && !error_operand_p (bounds))
15001                 {
15002                   /* Remember this wasn't a constant-expression.  */
15003                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15004                   TREE_SIDE_EFFECTS (bounds) = 1;
15005                 }
15006             }
15007           else
15008             bounds = NULL_TREE;
15009           /* Look for the closing `]'.  */
15010           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15011             {
15012               declarator = cp_error_declarator;
15013               break;
15014             }
15015
15016           declarator = make_array_declarator (declarator, bounds);
15017         }
15018       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15019         {
15020           {
15021             tree qualifying_scope;
15022             tree unqualified_name;
15023             special_function_kind sfk;
15024             bool abstract_ok;
15025             bool pack_expansion_p = false;
15026             cp_token *declarator_id_start_token;
15027
15028             /* Parse a declarator-id */
15029             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15030             if (abstract_ok)
15031               {
15032                 cp_parser_parse_tentatively (parser);
15033
15034                 /* If we see an ellipsis, we should be looking at a
15035                    parameter pack. */
15036                 if (token->type == CPP_ELLIPSIS)
15037                   {
15038                     /* Consume the `...' */
15039                     cp_lexer_consume_token (parser->lexer);
15040
15041                     pack_expansion_p = true;
15042                   }
15043               }
15044
15045             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15046             unqualified_name
15047               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15048             qualifying_scope = parser->scope;
15049             if (abstract_ok)
15050               {
15051                 bool okay = false;
15052
15053                 if (!unqualified_name && pack_expansion_p)
15054                   {
15055                     /* Check whether an error occurred. */
15056                     okay = !cp_parser_error_occurred (parser);
15057
15058                     /* We already consumed the ellipsis to mark a
15059                        parameter pack, but we have no way to report it,
15060                        so abort the tentative parse. We will be exiting
15061                        immediately anyway. */
15062                     cp_parser_abort_tentative_parse (parser);
15063                   }
15064                 else
15065                   okay = cp_parser_parse_definitely (parser);
15066
15067                 if (!okay)
15068                   unqualified_name = error_mark_node;
15069                 else if (unqualified_name
15070                          && (qualifying_scope
15071                              || (TREE_CODE (unqualified_name)
15072                                  != IDENTIFIER_NODE)))
15073                   {
15074                     cp_parser_error (parser, "expected unqualified-id");
15075                     unqualified_name = error_mark_node;
15076                   }
15077               }
15078
15079             if (!unqualified_name)
15080               return NULL;
15081             if (unqualified_name == error_mark_node)
15082               {
15083                 declarator = cp_error_declarator;
15084                 pack_expansion_p = false;
15085                 declarator->parameter_pack_p = false;
15086                 break;
15087               }
15088
15089             if (qualifying_scope && at_namespace_scope_p ()
15090                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15091               {
15092                 /* In the declaration of a member of a template class
15093                    outside of the class itself, the SCOPE will sometimes
15094                    be a TYPENAME_TYPE.  For example, given:
15095
15096                    template <typename T>
15097                    int S<T>::R::i = 3;
15098
15099                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15100                    this context, we must resolve S<T>::R to an ordinary
15101                    type, rather than a typename type.
15102
15103                    The reason we normally avoid resolving TYPENAME_TYPEs
15104                    is that a specialization of `S' might render
15105                    `S<T>::R' not a type.  However, if `S' is
15106                    specialized, then this `i' will not be used, so there
15107                    is no harm in resolving the types here.  */
15108                 tree type;
15109
15110                 /* Resolve the TYPENAME_TYPE.  */
15111                 type = resolve_typename_type (qualifying_scope,
15112                                               /*only_current_p=*/false);
15113                 /* If that failed, the declarator is invalid.  */
15114                 if (TREE_CODE (type) == TYPENAME_TYPE)
15115                   {
15116                     if (typedef_variant_p (type))
15117                       error_at (declarator_id_start_token->location,
15118                                 "cannot define member of dependent typedef "
15119                                 "%qT", type);
15120                     else
15121                       error_at (declarator_id_start_token->location,
15122                                 "%<%T::%E%> is not a type",
15123                                 TYPE_CONTEXT (qualifying_scope),
15124                                 TYPE_IDENTIFIER (qualifying_scope));
15125                   }
15126                 qualifying_scope = type;
15127               }
15128
15129             sfk = sfk_none;
15130
15131             if (unqualified_name)
15132               {
15133                 tree class_type;
15134
15135                 if (qualifying_scope
15136                     && CLASS_TYPE_P (qualifying_scope))
15137                   class_type = qualifying_scope;
15138                 else
15139                   class_type = current_class_type;
15140
15141                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15142                   {
15143                     tree name_type = TREE_TYPE (unqualified_name);
15144                     if (class_type && same_type_p (name_type, class_type))
15145                       {
15146                         if (qualifying_scope
15147                             && CLASSTYPE_USE_TEMPLATE (name_type))
15148                           {
15149                             error_at (declarator_id_start_token->location,
15150                                       "invalid use of constructor as a template");
15151                             inform (declarator_id_start_token->location,
15152                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15153                                     "name the constructor in a qualified name",
15154                                     class_type,
15155                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15156                                     class_type, name_type);
15157                             declarator = cp_error_declarator;
15158                             break;
15159                           }
15160                         else
15161                           unqualified_name = constructor_name (class_type);
15162                       }
15163                     else
15164                       {
15165                         /* We do not attempt to print the declarator
15166                            here because we do not have enough
15167                            information about its original syntactic
15168                            form.  */
15169                         cp_parser_error (parser, "invalid declarator");
15170                         declarator = cp_error_declarator;
15171                         break;
15172                       }
15173                   }
15174
15175                 if (class_type)
15176                   {
15177                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15178                       sfk = sfk_destructor;
15179                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15180                       sfk = sfk_conversion;
15181                     else if (/* There's no way to declare a constructor
15182                                 for an anonymous type, even if the type
15183                                 got a name for linkage purposes.  */
15184                              !TYPE_WAS_ANONYMOUS (class_type)
15185                              && constructor_name_p (unqualified_name,
15186                                                     class_type))
15187                       {
15188                         unqualified_name = constructor_name (class_type);
15189                         sfk = sfk_constructor;
15190                       }
15191                     else if (is_overloaded_fn (unqualified_name)
15192                              && DECL_CONSTRUCTOR_P (get_first_fn
15193                                                     (unqualified_name)))
15194                       sfk = sfk_constructor;
15195
15196                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15197                       *ctor_dtor_or_conv_p = -1;
15198                   }
15199               }
15200             declarator = make_id_declarator (qualifying_scope,
15201                                              unqualified_name,
15202                                              sfk);
15203             declarator->id_loc = token->location;
15204             declarator->parameter_pack_p = pack_expansion_p;
15205
15206             if (pack_expansion_p)
15207               maybe_warn_variadic_templates ();
15208           }
15209
15210         handle_declarator:;
15211           scope = get_scope_of_declarator (declarator);
15212           if (scope)
15213             /* Any names that appear after the declarator-id for a
15214                member are looked up in the containing scope.  */
15215             pushed_scope = push_scope (scope);
15216           parser->in_declarator_p = true;
15217           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15218               || (declarator && declarator->kind == cdk_id))
15219             /* Default args are only allowed on function
15220                declarations.  */
15221             parser->default_arg_ok_p = saved_default_arg_ok_p;
15222           else
15223             parser->default_arg_ok_p = false;
15224
15225           first = false;
15226         }
15227       /* We're done.  */
15228       else
15229         break;
15230     }
15231
15232   /* For an abstract declarator, we might wind up with nothing at this
15233      point.  That's an error; the declarator is not optional.  */
15234   if (!declarator)
15235     cp_parser_error (parser, "expected declarator");
15236
15237   /* If we entered a scope, we must exit it now.  */
15238   if (pushed_scope)
15239     pop_scope (pushed_scope);
15240
15241   parser->default_arg_ok_p = saved_default_arg_ok_p;
15242   parser->in_declarator_p = saved_in_declarator_p;
15243
15244   return declarator;
15245 }
15246
15247 /* Parse a ptr-operator.
15248
15249    ptr-operator:
15250      * cv-qualifier-seq [opt]
15251      &
15252      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15253
15254    GNU Extension:
15255
15256    ptr-operator:
15257      & cv-qualifier-seq [opt]
15258
15259    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15260    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15261    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15262    filled in with the TYPE containing the member.  *CV_QUALS is
15263    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15264    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15265    Note that the tree codes returned by this function have nothing
15266    to do with the types of trees that will be eventually be created
15267    to represent the pointer or reference type being parsed. They are
15268    just constants with suggestive names. */
15269 static enum tree_code
15270 cp_parser_ptr_operator (cp_parser* parser,
15271                         tree* type,
15272                         cp_cv_quals *cv_quals)
15273 {
15274   enum tree_code code = ERROR_MARK;
15275   cp_token *token;
15276
15277   /* Assume that it's not a pointer-to-member.  */
15278   *type = NULL_TREE;
15279   /* And that there are no cv-qualifiers.  */
15280   *cv_quals = TYPE_UNQUALIFIED;
15281
15282   /* Peek at the next token.  */
15283   token = cp_lexer_peek_token (parser->lexer);
15284
15285   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15286   if (token->type == CPP_MULT)
15287     code = INDIRECT_REF;
15288   else if (token->type == CPP_AND)
15289     code = ADDR_EXPR;
15290   else if ((cxx_dialect != cxx98) &&
15291            token->type == CPP_AND_AND) /* C++0x only */
15292     code = NON_LVALUE_EXPR;
15293
15294   if (code != ERROR_MARK)
15295     {
15296       /* Consume the `*', `&' or `&&'.  */
15297       cp_lexer_consume_token (parser->lexer);
15298
15299       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15300          `&', if we are allowing GNU extensions.  (The only qualifier
15301          that can legally appear after `&' is `restrict', but that is
15302          enforced during semantic analysis.  */
15303       if (code == INDIRECT_REF
15304           || cp_parser_allow_gnu_extensions_p (parser))
15305         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15306     }
15307   else
15308     {
15309       /* Try the pointer-to-member case.  */
15310       cp_parser_parse_tentatively (parser);
15311       /* Look for the optional `::' operator.  */
15312       cp_parser_global_scope_opt (parser,
15313                                   /*current_scope_valid_p=*/false);
15314       /* Look for the nested-name specifier.  */
15315       token = cp_lexer_peek_token (parser->lexer);
15316       cp_parser_nested_name_specifier (parser,
15317                                        /*typename_keyword_p=*/false,
15318                                        /*check_dependency_p=*/true,
15319                                        /*type_p=*/false,
15320                                        /*is_declaration=*/false);
15321       /* If we found it, and the next token is a `*', then we are
15322          indeed looking at a pointer-to-member operator.  */
15323       if (!cp_parser_error_occurred (parser)
15324           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15325         {
15326           /* Indicate that the `*' operator was used.  */
15327           code = INDIRECT_REF;
15328
15329           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15330             error_at (token->location, "%qD is a namespace", parser->scope);
15331           else
15332             {
15333               /* The type of which the member is a member is given by the
15334                  current SCOPE.  */
15335               *type = parser->scope;
15336               /* The next name will not be qualified.  */
15337               parser->scope = NULL_TREE;
15338               parser->qualifying_scope = NULL_TREE;
15339               parser->object_scope = NULL_TREE;
15340               /* Look for the optional cv-qualifier-seq.  */
15341               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15342             }
15343         }
15344       /* If that didn't work we don't have a ptr-operator.  */
15345       if (!cp_parser_parse_definitely (parser))
15346         cp_parser_error (parser, "expected ptr-operator");
15347     }
15348
15349   return code;
15350 }
15351
15352 /* Parse an (optional) cv-qualifier-seq.
15353
15354    cv-qualifier-seq:
15355      cv-qualifier cv-qualifier-seq [opt]
15356
15357    cv-qualifier:
15358      const
15359      volatile
15360
15361    GNU Extension:
15362
15363    cv-qualifier:
15364      __restrict__
15365
15366    Returns a bitmask representing the cv-qualifiers.  */
15367
15368 static cp_cv_quals
15369 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15370 {
15371   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15372
15373   while (true)
15374     {
15375       cp_token *token;
15376       cp_cv_quals cv_qualifier;
15377
15378       /* Peek at the next token.  */
15379       token = cp_lexer_peek_token (parser->lexer);
15380       /* See if it's a cv-qualifier.  */
15381       switch (token->keyword)
15382         {
15383         case RID_CONST:
15384           cv_qualifier = TYPE_QUAL_CONST;
15385           break;
15386
15387         case RID_VOLATILE:
15388           cv_qualifier = TYPE_QUAL_VOLATILE;
15389           break;
15390
15391         case RID_RESTRICT:
15392           cv_qualifier = TYPE_QUAL_RESTRICT;
15393           break;
15394
15395         default:
15396           cv_qualifier = TYPE_UNQUALIFIED;
15397           break;
15398         }
15399
15400       if (!cv_qualifier)
15401         break;
15402
15403       if (cv_quals & cv_qualifier)
15404         {
15405           error_at (token->location, "duplicate cv-qualifier");
15406           cp_lexer_purge_token (parser->lexer);
15407         }
15408       else
15409         {
15410           cp_lexer_consume_token (parser->lexer);
15411           cv_quals |= cv_qualifier;
15412         }
15413     }
15414
15415   return cv_quals;
15416 }
15417
15418 /* Parse a late-specified return type, if any.  This is not a separate
15419    non-terminal, but part of a function declarator, which looks like
15420
15421    -> trailing-type-specifier-seq abstract-declarator(opt)
15422
15423    Returns the type indicated by the type-id.  */
15424
15425 static tree
15426 cp_parser_late_return_type_opt (cp_parser* parser)
15427 {
15428   cp_token *token;
15429
15430   /* Peek at the next token.  */
15431   token = cp_lexer_peek_token (parser->lexer);
15432   /* A late-specified return type is indicated by an initial '->'. */
15433   if (token->type != CPP_DEREF)
15434     return NULL_TREE;
15435
15436   /* Consume the ->.  */
15437   cp_lexer_consume_token (parser->lexer);
15438
15439   return cp_parser_trailing_type_id (parser);
15440 }
15441
15442 /* Parse a declarator-id.
15443
15444    declarator-id:
15445      id-expression
15446      :: [opt] nested-name-specifier [opt] type-name
15447
15448    In the `id-expression' case, the value returned is as for
15449    cp_parser_id_expression if the id-expression was an unqualified-id.
15450    If the id-expression was a qualified-id, then a SCOPE_REF is
15451    returned.  The first operand is the scope (either a NAMESPACE_DECL
15452    or TREE_TYPE), but the second is still just a representation of an
15453    unqualified-id.  */
15454
15455 static tree
15456 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15457 {
15458   tree id;
15459   /* The expression must be an id-expression.  Assume that qualified
15460      names are the names of types so that:
15461
15462        template <class T>
15463        int S<T>::R::i = 3;
15464
15465      will work; we must treat `S<T>::R' as the name of a type.
15466      Similarly, assume that qualified names are templates, where
15467      required, so that:
15468
15469        template <class T>
15470        int S<T>::R<T>::i = 3;
15471
15472      will work, too.  */
15473   id = cp_parser_id_expression (parser,
15474                                 /*template_keyword_p=*/false,
15475                                 /*check_dependency_p=*/false,
15476                                 /*template_p=*/NULL,
15477                                 /*declarator_p=*/true,
15478                                 optional_p);
15479   if (id && BASELINK_P (id))
15480     id = BASELINK_FUNCTIONS (id);
15481   return id;
15482 }
15483
15484 /* Parse a type-id.
15485
15486    type-id:
15487      type-specifier-seq abstract-declarator [opt]
15488
15489    Returns the TYPE specified.  */
15490
15491 static tree
15492 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15493                      bool is_trailing_return)
15494 {
15495   cp_decl_specifier_seq type_specifier_seq;
15496   cp_declarator *abstract_declarator;
15497
15498   /* Parse the type-specifier-seq.  */
15499   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15500                                 is_trailing_return,
15501                                 &type_specifier_seq);
15502   if (type_specifier_seq.type == error_mark_node)
15503     return error_mark_node;
15504
15505   /* There might or might not be an abstract declarator.  */
15506   cp_parser_parse_tentatively (parser);
15507   /* Look for the declarator.  */
15508   abstract_declarator
15509     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15510                             /*parenthesized_p=*/NULL,
15511                             /*member_p=*/false);
15512   /* Check to see if there really was a declarator.  */
15513   if (!cp_parser_parse_definitely (parser))
15514     abstract_declarator = NULL;
15515
15516   if (type_specifier_seq.type
15517       && type_uses_auto (type_specifier_seq.type))
15518     {
15519       /* A type-id with type 'auto' is only ok if the abstract declarator
15520          is a function declarator with a late-specified return type.  */
15521       if (abstract_declarator
15522           && abstract_declarator->kind == cdk_function
15523           && abstract_declarator->u.function.late_return_type)
15524         /* OK */;
15525       else
15526         {
15527           error ("invalid use of %<auto%>");
15528           return error_mark_node;
15529         }
15530     }
15531   
15532   return groktypename (&type_specifier_seq, abstract_declarator,
15533                        is_template_arg);
15534 }
15535
15536 static tree cp_parser_type_id (cp_parser *parser)
15537 {
15538   return cp_parser_type_id_1 (parser, false, false);
15539 }
15540
15541 static tree cp_parser_template_type_arg (cp_parser *parser)
15542 {
15543   return cp_parser_type_id_1 (parser, true, false);
15544 }
15545
15546 static tree cp_parser_trailing_type_id (cp_parser *parser)
15547 {
15548   return cp_parser_type_id_1 (parser, false, true);
15549 }
15550
15551 /* Parse a type-specifier-seq.
15552
15553    type-specifier-seq:
15554      type-specifier type-specifier-seq [opt]
15555
15556    GNU extension:
15557
15558    type-specifier-seq:
15559      attributes type-specifier-seq [opt]
15560
15561    If IS_DECLARATION is true, we are at the start of a "condition" or
15562    exception-declaration, so we might be followed by a declarator-id.
15563
15564    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15565    i.e. we've just seen "->".
15566
15567    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15568
15569 static void
15570 cp_parser_type_specifier_seq (cp_parser* parser,
15571                               bool is_declaration,
15572                               bool is_trailing_return,
15573                               cp_decl_specifier_seq *type_specifier_seq)
15574 {
15575   bool seen_type_specifier = false;
15576   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15577   cp_token *start_token = NULL;
15578
15579   /* Clear the TYPE_SPECIFIER_SEQ.  */
15580   clear_decl_specs (type_specifier_seq);
15581
15582   /* In the context of a trailing return type, enum E { } is an
15583      elaborated-type-specifier followed by a function-body, not an
15584      enum-specifier.  */
15585   if (is_trailing_return)
15586     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15587
15588   /* Parse the type-specifiers and attributes.  */
15589   while (true)
15590     {
15591       tree type_specifier;
15592       bool is_cv_qualifier;
15593
15594       /* Check for attributes first.  */
15595       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15596         {
15597           type_specifier_seq->attributes =
15598             chainon (type_specifier_seq->attributes,
15599                      cp_parser_attributes_opt (parser));
15600           continue;
15601         }
15602
15603       /* record the token of the beginning of the type specifier seq,
15604          for error reporting purposes*/
15605      if (!start_token)
15606        start_token = cp_lexer_peek_token (parser->lexer);
15607
15608       /* Look for the type-specifier.  */
15609       type_specifier = cp_parser_type_specifier (parser,
15610                                                  flags,
15611                                                  type_specifier_seq,
15612                                                  /*is_declaration=*/false,
15613                                                  NULL,
15614                                                  &is_cv_qualifier);
15615       if (!type_specifier)
15616         {
15617           /* If the first type-specifier could not be found, this is not a
15618              type-specifier-seq at all.  */
15619           if (!seen_type_specifier)
15620             {
15621               cp_parser_error (parser, "expected type-specifier");
15622               type_specifier_seq->type = error_mark_node;
15623               return;
15624             }
15625           /* If subsequent type-specifiers could not be found, the
15626              type-specifier-seq is complete.  */
15627           break;
15628         }
15629
15630       seen_type_specifier = true;
15631       /* The standard says that a condition can be:
15632
15633             type-specifier-seq declarator = assignment-expression
15634
15635          However, given:
15636
15637            struct S {};
15638            if (int S = ...)
15639
15640          we should treat the "S" as a declarator, not as a
15641          type-specifier.  The standard doesn't say that explicitly for
15642          type-specifier-seq, but it does say that for
15643          decl-specifier-seq in an ordinary declaration.  Perhaps it
15644          would be clearer just to allow a decl-specifier-seq here, and
15645          then add a semantic restriction that if any decl-specifiers
15646          that are not type-specifiers appear, the program is invalid.  */
15647       if (is_declaration && !is_cv_qualifier)
15648         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15649     }
15650
15651   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15652 }
15653
15654 /* Parse a parameter-declaration-clause.
15655
15656    parameter-declaration-clause:
15657      parameter-declaration-list [opt] ... [opt]
15658      parameter-declaration-list , ...
15659
15660    Returns a representation for the parameter declarations.  A return
15661    value of NULL indicates a parameter-declaration-clause consisting
15662    only of an ellipsis.  */
15663
15664 static tree
15665 cp_parser_parameter_declaration_clause (cp_parser* parser)
15666 {
15667   tree parameters;
15668   cp_token *token;
15669   bool ellipsis_p;
15670   bool is_error;
15671
15672   /* Peek at the next token.  */
15673   token = cp_lexer_peek_token (parser->lexer);
15674   /* Check for trivial parameter-declaration-clauses.  */
15675   if (token->type == CPP_ELLIPSIS)
15676     {
15677       /* Consume the `...' token.  */
15678       cp_lexer_consume_token (parser->lexer);
15679       return NULL_TREE;
15680     }
15681   else if (token->type == CPP_CLOSE_PAREN)
15682     /* There are no parameters.  */
15683     {
15684 #ifndef NO_IMPLICIT_EXTERN_C
15685       if (in_system_header && current_class_type == NULL
15686           && current_lang_name == lang_name_c)
15687         return NULL_TREE;
15688       else
15689 #endif
15690         return void_list_node;
15691     }
15692   /* Check for `(void)', too, which is a special case.  */
15693   else if (token->keyword == RID_VOID
15694            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15695                == CPP_CLOSE_PAREN))
15696     {
15697       /* Consume the `void' token.  */
15698       cp_lexer_consume_token (parser->lexer);
15699       /* There are no parameters.  */
15700       return void_list_node;
15701     }
15702
15703   /* Parse the parameter-declaration-list.  */
15704   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15705   /* If a parse error occurred while parsing the
15706      parameter-declaration-list, then the entire
15707      parameter-declaration-clause is erroneous.  */
15708   if (is_error)
15709     return NULL;
15710
15711   /* Peek at the next token.  */
15712   token = cp_lexer_peek_token (parser->lexer);
15713   /* If it's a `,', the clause should terminate with an ellipsis.  */
15714   if (token->type == CPP_COMMA)
15715     {
15716       /* Consume the `,'.  */
15717       cp_lexer_consume_token (parser->lexer);
15718       /* Expect an ellipsis.  */
15719       ellipsis_p
15720         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15721     }
15722   /* It might also be `...' if the optional trailing `,' was
15723      omitted.  */
15724   else if (token->type == CPP_ELLIPSIS)
15725     {
15726       /* Consume the `...' token.  */
15727       cp_lexer_consume_token (parser->lexer);
15728       /* And remember that we saw it.  */
15729       ellipsis_p = true;
15730     }
15731   else
15732     ellipsis_p = false;
15733
15734   /* Finish the parameter list.  */
15735   if (!ellipsis_p)
15736     parameters = chainon (parameters, void_list_node);
15737
15738   return parameters;
15739 }
15740
15741 /* Parse a parameter-declaration-list.
15742
15743    parameter-declaration-list:
15744      parameter-declaration
15745      parameter-declaration-list , parameter-declaration
15746
15747    Returns a representation of the parameter-declaration-list, as for
15748    cp_parser_parameter_declaration_clause.  However, the
15749    `void_list_node' is never appended to the list.  Upon return,
15750    *IS_ERROR will be true iff an error occurred.  */
15751
15752 static tree
15753 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15754 {
15755   tree parameters = NULL_TREE;
15756   tree *tail = &parameters; 
15757   bool saved_in_unbraced_linkage_specification_p;
15758   int index = 0;
15759
15760   /* Assume all will go well.  */
15761   *is_error = false;
15762   /* The special considerations that apply to a function within an
15763      unbraced linkage specifications do not apply to the parameters
15764      to the function.  */
15765   saved_in_unbraced_linkage_specification_p 
15766     = parser->in_unbraced_linkage_specification_p;
15767   parser->in_unbraced_linkage_specification_p = false;
15768
15769   /* Look for more parameters.  */
15770   while (true)
15771     {
15772       cp_parameter_declarator *parameter;
15773       tree decl = error_mark_node;
15774       bool parenthesized_p;
15775       /* Parse the parameter.  */
15776       parameter
15777         = cp_parser_parameter_declaration (parser,
15778                                            /*template_parm_p=*/false,
15779                                            &parenthesized_p);
15780
15781       /* We don't know yet if the enclosing context is deprecated, so wait
15782          and warn in grokparms if appropriate.  */
15783       deprecated_state = DEPRECATED_SUPPRESS;
15784
15785       if (parameter)
15786         decl = grokdeclarator (parameter->declarator,
15787                                &parameter->decl_specifiers,
15788                                PARM,
15789                                parameter->default_argument != NULL_TREE,
15790                                &parameter->decl_specifiers.attributes);
15791
15792       deprecated_state = DEPRECATED_NORMAL;
15793
15794       /* If a parse error occurred parsing the parameter declaration,
15795          then the entire parameter-declaration-list is erroneous.  */
15796       if (decl == error_mark_node)
15797         {
15798           *is_error = true;
15799           parameters = error_mark_node;
15800           break;
15801         }
15802
15803       if (parameter->decl_specifiers.attributes)
15804         cplus_decl_attributes (&decl,
15805                                parameter->decl_specifiers.attributes,
15806                                0);
15807       if (DECL_NAME (decl))
15808         decl = pushdecl (decl);
15809
15810       if (decl != error_mark_node)
15811         {
15812           retrofit_lang_decl (decl);
15813           DECL_PARM_INDEX (decl) = ++index;
15814         }
15815
15816       /* Add the new parameter to the list.  */
15817       *tail = build_tree_list (parameter->default_argument, decl);
15818       tail = &TREE_CHAIN (*tail);
15819
15820       /* Peek at the next token.  */
15821       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15822           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15823           /* These are for Objective-C++ */
15824           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15825           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15826         /* The parameter-declaration-list is complete.  */
15827         break;
15828       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15829         {
15830           cp_token *token;
15831
15832           /* Peek at the next token.  */
15833           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15834           /* If it's an ellipsis, then the list is complete.  */
15835           if (token->type == CPP_ELLIPSIS)
15836             break;
15837           /* Otherwise, there must be more parameters.  Consume the
15838              `,'.  */
15839           cp_lexer_consume_token (parser->lexer);
15840           /* When parsing something like:
15841
15842                 int i(float f, double d)
15843
15844              we can tell after seeing the declaration for "f" that we
15845              are not looking at an initialization of a variable "i",
15846              but rather at the declaration of a function "i".
15847
15848              Due to the fact that the parsing of template arguments
15849              (as specified to a template-id) requires backtracking we
15850              cannot use this technique when inside a template argument
15851              list.  */
15852           if (!parser->in_template_argument_list_p
15853               && !parser->in_type_id_in_expr_p
15854               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15855               /* However, a parameter-declaration of the form
15856                  "foat(f)" (which is a valid declaration of a
15857                  parameter "f") can also be interpreted as an
15858                  expression (the conversion of "f" to "float").  */
15859               && !parenthesized_p)
15860             cp_parser_commit_to_tentative_parse (parser);
15861         }
15862       else
15863         {
15864           cp_parser_error (parser, "expected %<,%> or %<...%>");
15865           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15866             cp_parser_skip_to_closing_parenthesis (parser,
15867                                                    /*recovering=*/true,
15868                                                    /*or_comma=*/false,
15869                                                    /*consume_paren=*/false);
15870           break;
15871         }
15872     }
15873
15874   parser->in_unbraced_linkage_specification_p
15875     = saved_in_unbraced_linkage_specification_p;
15876
15877   return parameters;
15878 }
15879
15880 /* Parse a parameter declaration.
15881
15882    parameter-declaration:
15883      decl-specifier-seq ... [opt] declarator
15884      decl-specifier-seq declarator = assignment-expression
15885      decl-specifier-seq ... [opt] abstract-declarator [opt]
15886      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15887
15888    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15889    declares a template parameter.  (In that case, a non-nested `>'
15890    token encountered during the parsing of the assignment-expression
15891    is not interpreted as a greater-than operator.)
15892
15893    Returns a representation of the parameter, or NULL if an error
15894    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15895    true iff the declarator is of the form "(p)".  */
15896
15897 static cp_parameter_declarator *
15898 cp_parser_parameter_declaration (cp_parser *parser,
15899                                  bool template_parm_p,
15900                                  bool *parenthesized_p)
15901 {
15902   int declares_class_or_enum;
15903   cp_decl_specifier_seq decl_specifiers;
15904   cp_declarator *declarator;
15905   tree default_argument;
15906   cp_token *token = NULL, *declarator_token_start = NULL;
15907   const char *saved_message;
15908
15909   /* In a template parameter, `>' is not an operator.
15910
15911      [temp.param]
15912
15913      When parsing a default template-argument for a non-type
15914      template-parameter, the first non-nested `>' is taken as the end
15915      of the template parameter-list rather than a greater-than
15916      operator.  */
15917
15918   /* Type definitions may not appear in parameter types.  */
15919   saved_message = parser->type_definition_forbidden_message;
15920   parser->type_definition_forbidden_message
15921     = G_("types may not be defined in parameter types");
15922
15923   /* Parse the declaration-specifiers.  */
15924   cp_parser_decl_specifier_seq (parser,
15925                                 CP_PARSER_FLAGS_NONE,
15926                                 &decl_specifiers,
15927                                 &declares_class_or_enum);
15928
15929   /* Complain about missing 'typename' or other invalid type names.  */
15930   if (!decl_specifiers.any_type_specifiers_p)
15931     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15932
15933   /* If an error occurred, there's no reason to attempt to parse the
15934      rest of the declaration.  */
15935   if (cp_parser_error_occurred (parser))
15936     {
15937       parser->type_definition_forbidden_message = saved_message;
15938       return NULL;
15939     }
15940
15941   /* Peek at the next token.  */
15942   token = cp_lexer_peek_token (parser->lexer);
15943
15944   /* If the next token is a `)', `,', `=', `>', or `...', then there
15945      is no declarator. However, when variadic templates are enabled,
15946      there may be a declarator following `...'.  */
15947   if (token->type == CPP_CLOSE_PAREN
15948       || token->type == CPP_COMMA
15949       || token->type == CPP_EQ
15950       || token->type == CPP_GREATER)
15951     {
15952       declarator = NULL;
15953       if (parenthesized_p)
15954         *parenthesized_p = false;
15955     }
15956   /* Otherwise, there should be a declarator.  */
15957   else
15958     {
15959       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15960       parser->default_arg_ok_p = false;
15961
15962       /* After seeing a decl-specifier-seq, if the next token is not a
15963          "(", there is no possibility that the code is a valid
15964          expression.  Therefore, if parsing tentatively, we commit at
15965          this point.  */
15966       if (!parser->in_template_argument_list_p
15967           /* In an expression context, having seen:
15968
15969                (int((char ...
15970
15971              we cannot be sure whether we are looking at a
15972              function-type (taking a "char" as a parameter) or a cast
15973              of some object of type "char" to "int".  */
15974           && !parser->in_type_id_in_expr_p
15975           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15976           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15977         cp_parser_commit_to_tentative_parse (parser);
15978       /* Parse the declarator.  */
15979       declarator_token_start = token;
15980       declarator = cp_parser_declarator (parser,
15981                                          CP_PARSER_DECLARATOR_EITHER,
15982                                          /*ctor_dtor_or_conv_p=*/NULL,
15983                                          parenthesized_p,
15984                                          /*member_p=*/false);
15985       parser->default_arg_ok_p = saved_default_arg_ok_p;
15986       /* After the declarator, allow more attributes.  */
15987       decl_specifiers.attributes
15988         = chainon (decl_specifiers.attributes,
15989                    cp_parser_attributes_opt (parser));
15990     }
15991
15992   /* If the next token is an ellipsis, and we have not seen a
15993      declarator name, and the type of the declarator contains parameter
15994      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15995      a parameter pack expansion expression. Otherwise, leave the
15996      ellipsis for a C-style variadic function. */
15997   token = cp_lexer_peek_token (parser->lexer);
15998   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15999     {
16000       tree type = decl_specifiers.type;
16001
16002       if (type && DECL_P (type))
16003         type = TREE_TYPE (type);
16004
16005       if (type
16006           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16007           && declarator_can_be_parameter_pack (declarator)
16008           && (!declarator || !declarator->parameter_pack_p)
16009           && uses_parameter_packs (type))
16010         {
16011           /* Consume the `...'. */
16012           cp_lexer_consume_token (parser->lexer);
16013           maybe_warn_variadic_templates ();
16014           
16015           /* Build a pack expansion type */
16016           if (declarator)
16017             declarator->parameter_pack_p = true;
16018           else
16019             decl_specifiers.type = make_pack_expansion (type);
16020         }
16021     }
16022
16023   /* The restriction on defining new types applies only to the type
16024      of the parameter, not to the default argument.  */
16025   parser->type_definition_forbidden_message = saved_message;
16026
16027   /* If the next token is `=', then process a default argument.  */
16028   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16029     {
16030       /* Consume the `='.  */
16031       cp_lexer_consume_token (parser->lexer);
16032
16033       /* If we are defining a class, then the tokens that make up the
16034          default argument must be saved and processed later.  */
16035       if (!template_parm_p && at_class_scope_p ()
16036           && TYPE_BEING_DEFINED (current_class_type)
16037           && !LAMBDA_TYPE_P (current_class_type))
16038         {
16039           unsigned depth = 0;
16040           int maybe_template_id = 0;
16041           cp_token *first_token;
16042           cp_token *token;
16043
16044           /* Add tokens until we have processed the entire default
16045              argument.  We add the range [first_token, token).  */
16046           first_token = cp_lexer_peek_token (parser->lexer);
16047           while (true)
16048             {
16049               bool done = false;
16050
16051               /* Peek at the next token.  */
16052               token = cp_lexer_peek_token (parser->lexer);
16053               /* What we do depends on what token we have.  */
16054               switch (token->type)
16055                 {
16056                   /* In valid code, a default argument must be
16057                      immediately followed by a `,' `)', or `...'.  */
16058                 case CPP_COMMA:
16059                   if (depth == 0 && maybe_template_id)
16060                     {
16061                       /* If we've seen a '<', we might be in a
16062                          template-argument-list.  Until Core issue 325 is
16063                          resolved, we don't know how this situation ought
16064                          to be handled, so try to DTRT.  We check whether
16065                          what comes after the comma is a valid parameter
16066                          declaration list.  If it is, then the comma ends
16067                          the default argument; otherwise the default
16068                          argument continues.  */
16069                       bool error = false;
16070                       tree t;
16071
16072                       /* Set ITALP so cp_parser_parameter_declaration_list
16073                          doesn't decide to commit to this parse.  */
16074                       bool saved_italp = parser->in_template_argument_list_p;
16075                       parser->in_template_argument_list_p = true;
16076
16077                       cp_parser_parse_tentatively (parser);
16078                       cp_lexer_consume_token (parser->lexer);
16079                       begin_scope (sk_function_parms, NULL_TREE);
16080                       cp_parser_parameter_declaration_list (parser, &error);
16081                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16082                         pop_binding (DECL_NAME (t), t);
16083                       leave_scope ();
16084                       if (!cp_parser_error_occurred (parser) && !error)
16085                         done = true;
16086                       cp_parser_abort_tentative_parse (parser);
16087
16088                       parser->in_template_argument_list_p = saved_italp;
16089                       break;
16090                     }
16091                 case CPP_CLOSE_PAREN:
16092                 case CPP_ELLIPSIS:
16093                   /* If we run into a non-nested `;', `}', or `]',
16094                      then the code is invalid -- but the default
16095                      argument is certainly over.  */
16096                 case CPP_SEMICOLON:
16097                 case CPP_CLOSE_BRACE:
16098                 case CPP_CLOSE_SQUARE:
16099                   if (depth == 0)
16100                     done = true;
16101                   /* Update DEPTH, if necessary.  */
16102                   else if (token->type == CPP_CLOSE_PAREN
16103                            || token->type == CPP_CLOSE_BRACE
16104                            || token->type == CPP_CLOSE_SQUARE)
16105                     --depth;
16106                   break;
16107
16108                 case CPP_OPEN_PAREN:
16109                 case CPP_OPEN_SQUARE:
16110                 case CPP_OPEN_BRACE:
16111                   ++depth;
16112                   break;
16113
16114                 case CPP_LESS:
16115                   if (depth == 0)
16116                     /* This might be the comparison operator, or it might
16117                        start a template argument list.  */
16118                     ++maybe_template_id;
16119                   break;
16120
16121                 case CPP_RSHIFT:
16122                   if (cxx_dialect == cxx98)
16123                     break;
16124                   /* Fall through for C++0x, which treats the `>>'
16125                      operator like two `>' tokens in certain
16126                      cases.  */
16127
16128                 case CPP_GREATER:
16129                   if (depth == 0)
16130                     {
16131                       /* This might be an operator, or it might close a
16132                          template argument list.  But if a previous '<'
16133                          started a template argument list, this will have
16134                          closed it, so we can't be in one anymore.  */
16135                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16136                       if (maybe_template_id < 0)
16137                         maybe_template_id = 0;
16138                     }
16139                   break;
16140
16141                   /* If we run out of tokens, issue an error message.  */
16142                 case CPP_EOF:
16143                 case CPP_PRAGMA_EOL:
16144                   error_at (token->location, "file ends in default argument");
16145                   done = true;
16146                   break;
16147
16148                 case CPP_NAME:
16149                 case CPP_SCOPE:
16150                   /* In these cases, we should look for template-ids.
16151                      For example, if the default argument is
16152                      `X<int, double>()', we need to do name lookup to
16153                      figure out whether or not `X' is a template; if
16154                      so, the `,' does not end the default argument.
16155
16156                      That is not yet done.  */
16157                   break;
16158
16159                 default:
16160                   break;
16161                 }
16162
16163               /* If we've reached the end, stop.  */
16164               if (done)
16165                 break;
16166
16167               /* Add the token to the token block.  */
16168               token = cp_lexer_consume_token (parser->lexer);
16169             }
16170
16171           /* Create a DEFAULT_ARG to represent the unparsed default
16172              argument.  */
16173           default_argument = make_node (DEFAULT_ARG);
16174           DEFARG_TOKENS (default_argument)
16175             = cp_token_cache_new (first_token, token);
16176           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16177         }
16178       /* Outside of a class definition, we can just parse the
16179          assignment-expression.  */
16180       else
16181         {
16182           token = cp_lexer_peek_token (parser->lexer);
16183           default_argument 
16184             = cp_parser_default_argument (parser, template_parm_p);
16185         }
16186
16187       if (!parser->default_arg_ok_p)
16188         {
16189           if (flag_permissive)
16190             warning (0, "deprecated use of default argument for parameter of non-function");
16191           else
16192             {
16193               error_at (token->location,
16194                         "default arguments are only "
16195                         "permitted for function parameters");
16196               default_argument = NULL_TREE;
16197             }
16198         }
16199       else if ((declarator && declarator->parameter_pack_p)
16200                || (decl_specifiers.type
16201                    && PACK_EXPANSION_P (decl_specifiers.type)))
16202         {
16203           /* Find the name of the parameter pack.  */     
16204           cp_declarator *id_declarator = declarator;
16205           while (id_declarator && id_declarator->kind != cdk_id)
16206             id_declarator = id_declarator->declarator;
16207           
16208           if (id_declarator && id_declarator->kind == cdk_id)
16209             error_at (declarator_token_start->location,
16210                       template_parm_p 
16211                       ? "template parameter pack %qD"
16212                       " cannot have a default argument"
16213                       : "parameter pack %qD cannot have a default argument",
16214                       id_declarator->u.id.unqualified_name);
16215           else
16216             error_at (declarator_token_start->location,
16217                       template_parm_p 
16218                       ? "template parameter pack cannot have a default argument"
16219                       : "parameter pack cannot have a default argument");
16220           
16221           default_argument = NULL_TREE;
16222         }
16223     }
16224   else
16225     default_argument = NULL_TREE;
16226
16227   return make_parameter_declarator (&decl_specifiers,
16228                                     declarator,
16229                                     default_argument);
16230 }
16231
16232 /* Parse a default argument and return it.
16233
16234    TEMPLATE_PARM_P is true if this is a default argument for a
16235    non-type template parameter.  */
16236 static tree
16237 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16238 {
16239   tree default_argument = NULL_TREE;
16240   bool saved_greater_than_is_operator_p;
16241   bool saved_local_variables_forbidden_p;
16242
16243   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16244      set correctly.  */
16245   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16246   parser->greater_than_is_operator_p = !template_parm_p;
16247   /* Local variable names (and the `this' keyword) may not
16248      appear in a default argument.  */
16249   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16250   parser->local_variables_forbidden_p = true;
16251   /* Parse the assignment-expression.  */
16252   if (template_parm_p)
16253     push_deferring_access_checks (dk_no_deferred);
16254   default_argument
16255     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16256   if (template_parm_p)
16257     pop_deferring_access_checks ();
16258   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16259   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16260
16261   return default_argument;
16262 }
16263
16264 /* Parse a function-body.
16265
16266    function-body:
16267      compound_statement  */
16268
16269 static void
16270 cp_parser_function_body (cp_parser *parser)
16271 {
16272   cp_parser_compound_statement (parser, NULL, false);
16273 }
16274
16275 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16276    true if a ctor-initializer was present.  */
16277
16278 static bool
16279 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16280 {
16281   tree body, list;
16282   bool ctor_initializer_p;
16283   const bool check_body_p =
16284      DECL_CONSTRUCTOR_P (current_function_decl)
16285      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16286   tree last = NULL;
16287
16288   /* Begin the function body.  */
16289   body = begin_function_body ();
16290   /* Parse the optional ctor-initializer.  */
16291   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16292
16293   /* If we're parsing a constexpr constructor definition, we need
16294      to check that the constructor body is indeed empty.  However,
16295      before we get to cp_parser_function_body lot of junk has been
16296      generated, so we can't just check that we have an empty block.
16297      Rather we take a snapshot of the outermost block, and check whether
16298      cp_parser_function_body changed its state.  */
16299   if (check_body_p)
16300     {
16301       list = body;
16302       if (TREE_CODE (list) == BIND_EXPR)
16303         list = BIND_EXPR_BODY (list);
16304       if (TREE_CODE (list) == STATEMENT_LIST
16305           && STATEMENT_LIST_TAIL (list) != NULL)
16306         last = STATEMENT_LIST_TAIL (list)->stmt;
16307     }
16308   /* Parse the function-body.  */
16309   cp_parser_function_body (parser);
16310   if (check_body_p
16311       && (TREE_CODE (list) != STATEMENT_LIST
16312           || (last == NULL && STATEMENT_LIST_TAIL (list) != NULL)
16313           || (last != NULL && last != STATEMENT_LIST_TAIL (list)->stmt)))
16314     {
16315       error ("constexpr constructor does not have empty body");
16316       DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
16317     }
16318   /* Finish the function body.  */
16319   finish_function_body (body);
16320
16321   return ctor_initializer_p;
16322 }
16323
16324 /* Parse an initializer.
16325
16326    initializer:
16327      = initializer-clause
16328      ( expression-list )
16329
16330    Returns an expression representing the initializer.  If no
16331    initializer is present, NULL_TREE is returned.
16332
16333    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16334    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16335    set to TRUE if there is no initializer present.  If there is an
16336    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16337    is set to true; otherwise it is set to false.  */
16338
16339 static tree
16340 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16341                        bool* non_constant_p)
16342 {
16343   cp_token *token;
16344   tree init;
16345
16346   /* Peek at the next token.  */
16347   token = cp_lexer_peek_token (parser->lexer);
16348
16349   /* Let our caller know whether or not this initializer was
16350      parenthesized.  */
16351   *is_direct_init = (token->type != CPP_EQ);
16352   /* Assume that the initializer is constant.  */
16353   *non_constant_p = false;
16354
16355   if (token->type == CPP_EQ)
16356     {
16357       /* Consume the `='.  */
16358       cp_lexer_consume_token (parser->lexer);
16359       /* Parse the initializer-clause.  */
16360       init = cp_parser_initializer_clause (parser, non_constant_p);
16361     }
16362   else if (token->type == CPP_OPEN_PAREN)
16363     {
16364       VEC(tree,gc) *vec;
16365       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16366                                                      /*cast_p=*/false,
16367                                                      /*allow_expansion_p=*/true,
16368                                                      non_constant_p);
16369       if (vec == NULL)
16370         return error_mark_node;
16371       init = build_tree_list_vec (vec);
16372       release_tree_vector (vec);
16373     }
16374   else if (token->type == CPP_OPEN_BRACE)
16375     {
16376       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16377       init = cp_parser_braced_list (parser, non_constant_p);
16378       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16379     }
16380   else
16381     {
16382       /* Anything else is an error.  */
16383       cp_parser_error (parser, "expected initializer");
16384       init = error_mark_node;
16385     }
16386
16387   return init;
16388 }
16389
16390 /* Parse an initializer-clause.
16391
16392    initializer-clause:
16393      assignment-expression
16394      braced-init-list
16395
16396    Returns an expression representing the initializer.
16397
16398    If the `assignment-expression' production is used the value
16399    returned is simply a representation for the expression.
16400
16401    Otherwise, calls cp_parser_braced_list.  */
16402
16403 static tree
16404 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16405 {
16406   tree initializer;
16407
16408   /* Assume the expression is constant.  */
16409   *non_constant_p = false;
16410
16411   /* If it is not a `{', then we are looking at an
16412      assignment-expression.  */
16413   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16414     {
16415       initializer
16416         = cp_parser_constant_expression (parser,
16417                                         /*allow_non_constant_p=*/true,
16418                                         non_constant_p);
16419       if (!*non_constant_p)
16420         {
16421           /* We only want to fold if this is really a constant
16422              expression.  FIXME Actually, we don't want to fold here, but in
16423              cp_finish_decl.  */
16424           tree folded = fold_non_dependent_expr (initializer);
16425           folded = maybe_constant_value (folded);
16426           if (TREE_CONSTANT (folded))
16427             initializer = folded;
16428         }
16429     }
16430   else
16431     initializer = cp_parser_braced_list (parser, non_constant_p);
16432
16433   return initializer;
16434 }
16435
16436 /* Parse a brace-enclosed initializer list.
16437
16438    braced-init-list:
16439      { initializer-list , [opt] }
16440      { }
16441
16442    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16443    the elements of the initializer-list (or NULL, if the last
16444    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16445    NULL_TREE.  There is no way to detect whether or not the optional
16446    trailing `,' was provided.  NON_CONSTANT_P is as for
16447    cp_parser_initializer.  */     
16448
16449 static tree
16450 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16451 {
16452   tree initializer;
16453
16454   /* Consume the `{' token.  */
16455   cp_lexer_consume_token (parser->lexer);
16456   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16457   initializer = make_node (CONSTRUCTOR);
16458   /* If it's not a `}', then there is a non-trivial initializer.  */
16459   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16460     {
16461       /* Parse the initializer list.  */
16462       CONSTRUCTOR_ELTS (initializer)
16463         = cp_parser_initializer_list (parser, non_constant_p);
16464       /* A trailing `,' token is allowed.  */
16465       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16466         cp_lexer_consume_token (parser->lexer);
16467     }
16468   /* Now, there should be a trailing `}'.  */
16469   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16470   TREE_TYPE (initializer) = init_list_type_node;
16471   return initializer;
16472 }
16473
16474 /* Parse an initializer-list.
16475
16476    initializer-list:
16477      initializer-clause ... [opt]
16478      initializer-list , initializer-clause ... [opt]
16479
16480    GNU Extension:
16481
16482    initializer-list:
16483      identifier : initializer-clause
16484      initializer-list, identifier : initializer-clause
16485
16486    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16487    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16488    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16489    as for cp_parser_initializer.  */
16490
16491 static VEC(constructor_elt,gc) *
16492 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16493 {
16494   VEC(constructor_elt,gc) *v = NULL;
16495
16496   /* Assume all of the expressions are constant.  */
16497   *non_constant_p = false;
16498
16499   /* Parse the rest of the list.  */
16500   while (true)
16501     {
16502       cp_token *token;
16503       tree identifier;
16504       tree initializer;
16505       bool clause_non_constant_p;
16506
16507       /* If the next token is an identifier and the following one is a
16508          colon, we are looking at the GNU designated-initializer
16509          syntax.  */
16510       if (cp_parser_allow_gnu_extensions_p (parser)
16511           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16512           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16513         {
16514           /* Warn the user that they are using an extension.  */
16515           pedwarn (input_location, OPT_pedantic, 
16516                    "ISO C++ does not allow designated initializers");
16517           /* Consume the identifier.  */
16518           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16519           /* Consume the `:'.  */
16520           cp_lexer_consume_token (parser->lexer);
16521         }
16522       else
16523         identifier = NULL_TREE;
16524
16525       /* Parse the initializer.  */
16526       initializer = cp_parser_initializer_clause (parser,
16527                                                   &clause_non_constant_p);
16528       /* If any clause is non-constant, so is the entire initializer.  */
16529       if (clause_non_constant_p)
16530         *non_constant_p = true;
16531
16532       /* If we have an ellipsis, this is an initializer pack
16533          expansion.  */
16534       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16535         {
16536           /* Consume the `...'.  */
16537           cp_lexer_consume_token (parser->lexer);
16538
16539           /* Turn the initializer into an initializer expansion.  */
16540           initializer = make_pack_expansion (initializer);
16541         }
16542
16543       /* Add it to the vector.  */
16544       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16545
16546       /* If the next token is not a comma, we have reached the end of
16547          the list.  */
16548       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16549         break;
16550
16551       /* Peek at the next token.  */
16552       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16553       /* If the next token is a `}', then we're still done.  An
16554          initializer-clause can have a trailing `,' after the
16555          initializer-list and before the closing `}'.  */
16556       if (token->type == CPP_CLOSE_BRACE)
16557         break;
16558
16559       /* Consume the `,' token.  */
16560       cp_lexer_consume_token (parser->lexer);
16561     }
16562
16563   return v;
16564 }
16565
16566 /* Classes [gram.class] */
16567
16568 /* Parse a class-name.
16569
16570    class-name:
16571      identifier
16572      template-id
16573
16574    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16575    to indicate that names looked up in dependent types should be
16576    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16577    keyword has been used to indicate that the name that appears next
16578    is a template.  TAG_TYPE indicates the explicit tag given before
16579    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16580    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16581    is the class being defined in a class-head.
16582
16583    Returns the TYPE_DECL representing the class.  */
16584
16585 static tree
16586 cp_parser_class_name (cp_parser *parser,
16587                       bool typename_keyword_p,
16588                       bool template_keyword_p,
16589                       enum tag_types tag_type,
16590                       bool check_dependency_p,
16591                       bool class_head_p,
16592                       bool is_declaration)
16593 {
16594   tree decl;
16595   tree scope;
16596   bool typename_p;
16597   cp_token *token;
16598   tree identifier = NULL_TREE;
16599
16600   /* All class-names start with an identifier.  */
16601   token = cp_lexer_peek_token (parser->lexer);
16602   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16603     {
16604       cp_parser_error (parser, "expected class-name");
16605       return error_mark_node;
16606     }
16607
16608   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16609      to a template-id, so we save it here.  */
16610   scope = parser->scope;
16611   if (scope == error_mark_node)
16612     return error_mark_node;
16613
16614   /* Any name names a type if we're following the `typename' keyword
16615      in a qualified name where the enclosing scope is type-dependent.  */
16616   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16617                 && dependent_type_p (scope));
16618   /* Handle the common case (an identifier, but not a template-id)
16619      efficiently.  */
16620   if (token->type == CPP_NAME
16621       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16622     {
16623       cp_token *identifier_token;
16624       bool ambiguous_p;
16625
16626       /* Look for the identifier.  */
16627       identifier_token = cp_lexer_peek_token (parser->lexer);
16628       ambiguous_p = identifier_token->ambiguous_p;
16629       identifier = cp_parser_identifier (parser);
16630       /* If the next token isn't an identifier, we are certainly not
16631          looking at a class-name.  */
16632       if (identifier == error_mark_node)
16633         decl = error_mark_node;
16634       /* If we know this is a type-name, there's no need to look it
16635          up.  */
16636       else if (typename_p)
16637         decl = identifier;
16638       else
16639         {
16640           tree ambiguous_decls;
16641           /* If we already know that this lookup is ambiguous, then
16642              we've already issued an error message; there's no reason
16643              to check again.  */
16644           if (ambiguous_p)
16645             {
16646               cp_parser_simulate_error (parser);
16647               return error_mark_node;
16648             }
16649           /* If the next token is a `::', then the name must be a type
16650              name.
16651
16652              [basic.lookup.qual]
16653
16654              During the lookup for a name preceding the :: scope
16655              resolution operator, object, function, and enumerator
16656              names are ignored.  */
16657           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16658             tag_type = typename_type;
16659           /* Look up the name.  */
16660           decl = cp_parser_lookup_name (parser, identifier,
16661                                         tag_type,
16662                                         /*is_template=*/false,
16663                                         /*is_namespace=*/false,
16664                                         check_dependency_p,
16665                                         &ambiguous_decls,
16666                                         identifier_token->location);
16667           if (ambiguous_decls)
16668             {
16669               if (cp_parser_parsing_tentatively (parser))
16670                 cp_parser_simulate_error (parser);
16671               return error_mark_node;
16672             }
16673         }
16674     }
16675   else
16676     {
16677       /* Try a template-id.  */
16678       decl = cp_parser_template_id (parser, template_keyword_p,
16679                                     check_dependency_p,
16680                                     is_declaration);
16681       if (decl == error_mark_node)
16682         return error_mark_node;
16683     }
16684
16685   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16686
16687   /* If this is a typename, create a TYPENAME_TYPE.  */
16688   if (typename_p && decl != error_mark_node)
16689     {
16690       decl = make_typename_type (scope, decl, typename_type,
16691                                  /*complain=*/tf_error);
16692       if (decl != error_mark_node)
16693         decl = TYPE_NAME (decl);
16694     }
16695
16696   /* Check to see that it is really the name of a class.  */
16697   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16698       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16699       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16700     /* Situations like this:
16701
16702          template <typename T> struct A {
16703            typename T::template X<int>::I i;
16704          };
16705
16706        are problematic.  Is `T::template X<int>' a class-name?  The
16707        standard does not seem to be definitive, but there is no other
16708        valid interpretation of the following `::'.  Therefore, those
16709        names are considered class-names.  */
16710     {
16711       decl = make_typename_type (scope, decl, tag_type, tf_error);
16712       if (decl != error_mark_node)
16713         decl = TYPE_NAME (decl);
16714     }
16715   else if (TREE_CODE (decl) != TYPE_DECL
16716            || TREE_TYPE (decl) == error_mark_node
16717            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16718     decl = error_mark_node;
16719
16720   if (decl == error_mark_node)
16721     cp_parser_error (parser, "expected class-name");
16722   else if (identifier && !parser->scope)
16723     maybe_note_name_used_in_class (identifier, decl);
16724
16725   return decl;
16726 }
16727
16728 /* Parse a class-specifier.
16729
16730    class-specifier:
16731      class-head { member-specification [opt] }
16732
16733    Returns the TREE_TYPE representing the class.  */
16734
16735 static tree
16736 cp_parser_class_specifier (cp_parser* parser)
16737 {
16738   tree type;
16739   tree attributes = NULL_TREE;
16740   bool nested_name_specifier_p;
16741   unsigned saved_num_template_parameter_lists;
16742   bool saved_in_function_body;
16743   bool saved_in_unbraced_linkage_specification_p;
16744   tree old_scope = NULL_TREE;
16745   tree scope = NULL_TREE;
16746   tree bases;
16747
16748   push_deferring_access_checks (dk_no_deferred);
16749
16750   /* Parse the class-head.  */
16751   type = cp_parser_class_head (parser,
16752                                &nested_name_specifier_p,
16753                                &attributes,
16754                                &bases);
16755   /* If the class-head was a semantic disaster, skip the entire body
16756      of the class.  */
16757   if (!type)
16758     {
16759       cp_parser_skip_to_end_of_block_or_statement (parser);
16760       pop_deferring_access_checks ();
16761       return error_mark_node;
16762     }
16763
16764   /* Look for the `{'.  */
16765   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16766     {
16767       pop_deferring_access_checks ();
16768       return error_mark_node;
16769     }
16770
16771   /* Process the base classes. If they're invalid, skip the 
16772      entire class body.  */
16773   if (!xref_basetypes (type, bases))
16774     {
16775       /* Consuming the closing brace yields better error messages
16776          later on.  */
16777       if (cp_parser_skip_to_closing_brace (parser))
16778         cp_lexer_consume_token (parser->lexer);
16779       pop_deferring_access_checks ();
16780       return error_mark_node;
16781     }
16782
16783   /* Issue an error message if type-definitions are forbidden here.  */
16784   cp_parser_check_type_definition (parser);
16785   /* Remember that we are defining one more class.  */
16786   ++parser->num_classes_being_defined;
16787   /* Inside the class, surrounding template-parameter-lists do not
16788      apply.  */
16789   saved_num_template_parameter_lists
16790     = parser->num_template_parameter_lists;
16791   parser->num_template_parameter_lists = 0;
16792   /* We are not in a function body.  */
16793   saved_in_function_body = parser->in_function_body;
16794   parser->in_function_body = false;
16795   /* We are not immediately inside an extern "lang" block.  */
16796   saved_in_unbraced_linkage_specification_p
16797     = parser->in_unbraced_linkage_specification_p;
16798   parser->in_unbraced_linkage_specification_p = false;
16799
16800   /* Start the class.  */
16801   if (nested_name_specifier_p)
16802     {
16803       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16804       old_scope = push_inner_scope (scope);
16805     }
16806   type = begin_class_definition (type, attributes);
16807
16808   if (type == error_mark_node)
16809     /* If the type is erroneous, skip the entire body of the class.  */
16810     cp_parser_skip_to_closing_brace (parser);
16811   else
16812     /* Parse the member-specification.  */
16813     cp_parser_member_specification_opt (parser);
16814
16815   /* Look for the trailing `}'.  */
16816   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16817   /* Look for trailing attributes to apply to this class.  */
16818   if (cp_parser_allow_gnu_extensions_p (parser))
16819     attributes = cp_parser_attributes_opt (parser);
16820   if (type != error_mark_node)
16821     type = finish_struct (type, attributes);
16822   if (nested_name_specifier_p)
16823     pop_inner_scope (old_scope, scope);
16824   /* If this class is not itself within the scope of another class,
16825      then we need to parse the bodies of all of the queued function
16826      definitions.  Note that the queued functions defined in a class
16827      are not always processed immediately following the
16828      class-specifier for that class.  Consider:
16829
16830        struct A {
16831          struct B { void f() { sizeof (A); } };
16832        };
16833
16834      If `f' were processed before the processing of `A' were
16835      completed, there would be no way to compute the size of `A'.
16836      Note that the nesting we are interested in here is lexical --
16837      not the semantic nesting given by TYPE_CONTEXT.  In particular,
16838      for:
16839
16840        struct A { struct B; };
16841        struct A::B { void f() { } };
16842
16843      there is no need to delay the parsing of `A::B::f'.  */
16844   if (--parser->num_classes_being_defined == 0)
16845     {
16846       tree fn;
16847       tree class_type = NULL_TREE;
16848       tree pushed_scope = NULL_TREE;
16849       unsigned ix;
16850       cp_default_arg_entry *e;
16851
16852       /* In a first pass, parse default arguments to the functions.
16853          Then, in a second pass, parse the bodies of the functions.
16854          This two-phased approach handles cases like:
16855
16856             struct S {
16857               void f() { g(); }
16858               void g(int i = 3);
16859             };
16860
16861          */
16862       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16863                         ix, e)
16864         {
16865           fn = e->decl;
16866           /* If there are default arguments that have not yet been processed,
16867              take care of them now.  */
16868           if (class_type != e->class_type)
16869             {
16870               if (pushed_scope)
16871                 pop_scope (pushed_scope);
16872               class_type = e->class_type;
16873               pushed_scope = push_scope (class_type);
16874             }
16875           /* Make sure that any template parameters are in scope.  */
16876           maybe_begin_member_template_processing (fn);
16877           /* Parse the default argument expressions.  */
16878           cp_parser_late_parsing_default_args (parser, fn);
16879           /* Remove any template parameters from the symbol table.  */
16880           maybe_end_member_template_processing ();
16881         }
16882       if (pushed_scope)
16883         pop_scope (pushed_scope);
16884       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16885       /* Now parse the body of the functions.  */
16886       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16887         cp_parser_late_parsing_for_member (parser, fn);
16888       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16889     }
16890
16891   /* Put back any saved access checks.  */
16892   pop_deferring_access_checks ();
16893
16894   /* Restore saved state.  */
16895   parser->in_function_body = saved_in_function_body;
16896   parser->num_template_parameter_lists
16897     = saved_num_template_parameter_lists;
16898   parser->in_unbraced_linkage_specification_p
16899     = saved_in_unbraced_linkage_specification_p;
16900
16901   return type;
16902 }
16903
16904 /* Parse a class-head.
16905
16906    class-head:
16907      class-key identifier [opt] base-clause [opt]
16908      class-key nested-name-specifier identifier base-clause [opt]
16909      class-key nested-name-specifier [opt] template-id
16910        base-clause [opt]
16911
16912    GNU Extensions:
16913      class-key attributes identifier [opt] base-clause [opt]
16914      class-key attributes nested-name-specifier identifier base-clause [opt]
16915      class-key attributes nested-name-specifier [opt] template-id
16916        base-clause [opt]
16917
16918    Upon return BASES is initialized to the list of base classes (or
16919    NULL, if there are none) in the same form returned by
16920    cp_parser_base_clause.
16921
16922    Returns the TYPE of the indicated class.  Sets
16923    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16924    involving a nested-name-specifier was used, and FALSE otherwise.
16925
16926    Returns error_mark_node if this is not a class-head.
16927
16928    Returns NULL_TREE if the class-head is syntactically valid, but
16929    semantically invalid in a way that means we should skip the entire
16930    body of the class.  */
16931
16932 static tree
16933 cp_parser_class_head (cp_parser* parser,
16934                       bool* nested_name_specifier_p,
16935                       tree *attributes_p,
16936                       tree *bases)
16937 {
16938   tree nested_name_specifier;
16939   enum tag_types class_key;
16940   tree id = NULL_TREE;
16941   tree type = NULL_TREE;
16942   tree attributes;
16943   bool template_id_p = false;
16944   bool qualified_p = false;
16945   bool invalid_nested_name_p = false;
16946   bool invalid_explicit_specialization_p = false;
16947   tree pushed_scope = NULL_TREE;
16948   unsigned num_templates;
16949   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16950   /* Assume no nested-name-specifier will be present.  */
16951   *nested_name_specifier_p = false;
16952   /* Assume no template parameter lists will be used in defining the
16953      type.  */
16954   num_templates = 0;
16955
16956   *bases = NULL_TREE;
16957
16958   /* Look for the class-key.  */
16959   class_key = cp_parser_class_key (parser);
16960   if (class_key == none_type)
16961     return error_mark_node;
16962
16963   /* Parse the attributes.  */
16964   attributes = cp_parser_attributes_opt (parser);
16965
16966   /* If the next token is `::', that is invalid -- but sometimes
16967      people do try to write:
16968
16969        struct ::S {};
16970
16971      Handle this gracefully by accepting the extra qualifier, and then
16972      issuing an error about it later if this really is a
16973      class-head.  If it turns out just to be an elaborated type
16974      specifier, remain silent.  */
16975   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16976     qualified_p = true;
16977
16978   push_deferring_access_checks (dk_no_check);
16979
16980   /* Determine the name of the class.  Begin by looking for an
16981      optional nested-name-specifier.  */
16982   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16983   nested_name_specifier
16984     = cp_parser_nested_name_specifier_opt (parser,
16985                                            /*typename_keyword_p=*/false,
16986                                            /*check_dependency_p=*/false,
16987                                            /*type_p=*/false,
16988                                            /*is_declaration=*/false);
16989   /* If there was a nested-name-specifier, then there *must* be an
16990      identifier.  */
16991   if (nested_name_specifier)
16992     {
16993       type_start_token = cp_lexer_peek_token (parser->lexer);
16994       /* Although the grammar says `identifier', it really means
16995          `class-name' or `template-name'.  You are only allowed to
16996          define a class that has already been declared with this
16997          syntax.
16998
16999          The proposed resolution for Core Issue 180 says that wherever
17000          you see `class T::X' you should treat `X' as a type-name.
17001
17002          It is OK to define an inaccessible class; for example:
17003
17004            class A { class B; };
17005            class A::B {};
17006
17007          We do not know if we will see a class-name, or a
17008          template-name.  We look for a class-name first, in case the
17009          class-name is a template-id; if we looked for the
17010          template-name first we would stop after the template-name.  */
17011       cp_parser_parse_tentatively (parser);
17012       type = cp_parser_class_name (parser,
17013                                    /*typename_keyword_p=*/false,
17014                                    /*template_keyword_p=*/false,
17015                                    class_type,
17016                                    /*check_dependency_p=*/false,
17017                                    /*class_head_p=*/true,
17018                                    /*is_declaration=*/false);
17019       /* If that didn't work, ignore the nested-name-specifier.  */
17020       if (!cp_parser_parse_definitely (parser))
17021         {
17022           invalid_nested_name_p = true;
17023           type_start_token = cp_lexer_peek_token (parser->lexer);
17024           id = cp_parser_identifier (parser);
17025           if (id == error_mark_node)
17026             id = NULL_TREE;
17027         }
17028       /* If we could not find a corresponding TYPE, treat this
17029          declaration like an unqualified declaration.  */
17030       if (type == error_mark_node)
17031         nested_name_specifier = NULL_TREE;
17032       /* Otherwise, count the number of templates used in TYPE and its
17033          containing scopes.  */
17034       else
17035         {
17036           tree scope;
17037
17038           for (scope = TREE_TYPE (type);
17039                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17040                scope = (TYPE_P (scope)
17041                         ? TYPE_CONTEXT (scope)
17042                         : DECL_CONTEXT (scope)))
17043             if (TYPE_P (scope)
17044                 && CLASS_TYPE_P (scope)
17045                 && CLASSTYPE_TEMPLATE_INFO (scope)
17046                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17047                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17048               ++num_templates;
17049         }
17050     }
17051   /* Otherwise, the identifier is optional.  */
17052   else
17053     {
17054       /* We don't know whether what comes next is a template-id,
17055          an identifier, or nothing at all.  */
17056       cp_parser_parse_tentatively (parser);
17057       /* Check for a template-id.  */
17058       type_start_token = cp_lexer_peek_token (parser->lexer);
17059       id = cp_parser_template_id (parser,
17060                                   /*template_keyword_p=*/false,
17061                                   /*check_dependency_p=*/true,
17062                                   /*is_declaration=*/true);
17063       /* If that didn't work, it could still be an identifier.  */
17064       if (!cp_parser_parse_definitely (parser))
17065         {
17066           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17067             {
17068               type_start_token = cp_lexer_peek_token (parser->lexer);
17069               id = cp_parser_identifier (parser);
17070             }
17071           else
17072             id = NULL_TREE;
17073         }
17074       else
17075         {
17076           template_id_p = true;
17077           ++num_templates;
17078         }
17079     }
17080
17081   pop_deferring_access_checks ();
17082
17083   if (id)
17084     cp_parser_check_for_invalid_template_id (parser, id,
17085                                              type_start_token->location);
17086
17087   /* If it's not a `:' or a `{' then we can't really be looking at a
17088      class-head, since a class-head only appears as part of a
17089      class-specifier.  We have to detect this situation before calling
17090      xref_tag, since that has irreversible side-effects.  */
17091   if (!cp_parser_next_token_starts_class_definition_p (parser))
17092     {
17093       cp_parser_error (parser, "expected %<{%> or %<:%>");
17094       return error_mark_node;
17095     }
17096
17097   /* At this point, we're going ahead with the class-specifier, even
17098      if some other problem occurs.  */
17099   cp_parser_commit_to_tentative_parse (parser);
17100   /* Issue the error about the overly-qualified name now.  */
17101   if (qualified_p)
17102     {
17103       cp_parser_error (parser,
17104                        "global qualification of class name is invalid");
17105       return error_mark_node;
17106     }
17107   else if (invalid_nested_name_p)
17108     {
17109       cp_parser_error (parser,
17110                        "qualified name does not name a class");
17111       return error_mark_node;
17112     }
17113   else if (nested_name_specifier)
17114     {
17115       tree scope;
17116
17117       /* Reject typedef-names in class heads.  */
17118       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17119         {
17120           error_at (type_start_token->location,
17121                     "invalid class name in declaration of %qD",
17122                     type);
17123           type = NULL_TREE;
17124           goto done;
17125         }
17126
17127       /* Figure out in what scope the declaration is being placed.  */
17128       scope = current_scope ();
17129       /* If that scope does not contain the scope in which the
17130          class was originally declared, the program is invalid.  */
17131       if (scope && !is_ancestor (scope, nested_name_specifier))
17132         {
17133           if (at_namespace_scope_p ())
17134             error_at (type_start_token->location,
17135                       "declaration of %qD in namespace %qD which does not "
17136                       "enclose %qD",
17137                       type, scope, nested_name_specifier);
17138           else
17139             error_at (type_start_token->location,
17140                       "declaration of %qD in %qD which does not enclose %qD",
17141                       type, scope, nested_name_specifier);
17142           type = NULL_TREE;
17143           goto done;
17144         }
17145       /* [dcl.meaning]
17146
17147          A declarator-id shall not be qualified except for the
17148          definition of a ... nested class outside of its class
17149          ... [or] the definition or explicit instantiation of a
17150          class member of a namespace outside of its namespace.  */
17151       if (scope == nested_name_specifier)
17152         {
17153           permerror (nested_name_specifier_token_start->location,
17154                      "extra qualification not allowed");
17155           nested_name_specifier = NULL_TREE;
17156           num_templates = 0;
17157         }
17158     }
17159   /* An explicit-specialization must be preceded by "template <>".  If
17160      it is not, try to recover gracefully.  */
17161   if (at_namespace_scope_p ()
17162       && parser->num_template_parameter_lists == 0
17163       && template_id_p)
17164     {
17165       error_at (type_start_token->location,
17166                 "an explicit specialization must be preceded by %<template <>%>");
17167       invalid_explicit_specialization_p = true;
17168       /* Take the same action that would have been taken by
17169          cp_parser_explicit_specialization.  */
17170       ++parser->num_template_parameter_lists;
17171       begin_specialization ();
17172     }
17173   /* There must be no "return" statements between this point and the
17174      end of this function; set "type "to the correct return value and
17175      use "goto done;" to return.  */
17176   /* Make sure that the right number of template parameters were
17177      present.  */
17178   if (!cp_parser_check_template_parameters (parser, num_templates,
17179                                             type_start_token->location,
17180                                             /*declarator=*/NULL))
17181     {
17182       /* If something went wrong, there is no point in even trying to
17183          process the class-definition.  */
17184       type = NULL_TREE;
17185       goto done;
17186     }
17187
17188   /* Look up the type.  */
17189   if (template_id_p)
17190     {
17191       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17192           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17193               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17194         {
17195           error_at (type_start_token->location,
17196                     "function template %qD redeclared as a class template", id);
17197           type = error_mark_node;
17198         }
17199       else
17200         {
17201           type = TREE_TYPE (id);
17202           type = maybe_process_partial_specialization (type);
17203         }
17204       if (nested_name_specifier)
17205         pushed_scope = push_scope (nested_name_specifier);
17206     }
17207   else if (nested_name_specifier)
17208     {
17209       tree class_type;
17210
17211       /* Given:
17212
17213             template <typename T> struct S { struct T };
17214             template <typename T> struct S<T>::T { };
17215
17216          we will get a TYPENAME_TYPE when processing the definition of
17217          `S::T'.  We need to resolve it to the actual type before we
17218          try to define it.  */
17219       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17220         {
17221           class_type = resolve_typename_type (TREE_TYPE (type),
17222                                               /*only_current_p=*/false);
17223           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17224             type = TYPE_NAME (class_type);
17225           else
17226             {
17227               cp_parser_error (parser, "could not resolve typename type");
17228               type = error_mark_node;
17229             }
17230         }
17231
17232       if (maybe_process_partial_specialization (TREE_TYPE (type))
17233           == error_mark_node)
17234         {
17235           type = NULL_TREE;
17236           goto done;
17237         }
17238
17239       class_type = current_class_type;
17240       /* Enter the scope indicated by the nested-name-specifier.  */
17241       pushed_scope = push_scope (nested_name_specifier);
17242       /* Get the canonical version of this type.  */
17243       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17244       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17245           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17246         {
17247           type = push_template_decl (type);
17248           if (type == error_mark_node)
17249             {
17250               type = NULL_TREE;
17251               goto done;
17252             }
17253         }
17254
17255       type = TREE_TYPE (type);
17256       *nested_name_specifier_p = true;
17257     }
17258   else      /* The name is not a nested name.  */
17259     {
17260       /* If the class was unnamed, create a dummy name.  */
17261       if (!id)
17262         id = make_anon_name ();
17263       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17264                        parser->num_template_parameter_lists);
17265     }
17266
17267   /* Indicate whether this class was declared as a `class' or as a
17268      `struct'.  */
17269   if (TREE_CODE (type) == RECORD_TYPE)
17270     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17271   cp_parser_check_class_key (class_key, type);
17272
17273   /* If this type was already complete, and we see another definition,
17274      that's an error.  */
17275   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17276     {
17277       error_at (type_start_token->location, "redefinition of %q#T",
17278                 type);
17279       error_at (type_start_token->location, "previous definition of %q+#T",
17280                 type);
17281       type = NULL_TREE;
17282       goto done;
17283     }
17284   else if (type == error_mark_node)
17285     type = NULL_TREE;
17286
17287   /* We will have entered the scope containing the class; the names of
17288      base classes should be looked up in that context.  For example:
17289
17290        struct A { struct B {}; struct C; };
17291        struct A::C : B {};
17292
17293      is valid.  */
17294
17295   /* Get the list of base-classes, if there is one.  */
17296   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17297     *bases = cp_parser_base_clause (parser);
17298
17299  done:
17300   /* Leave the scope given by the nested-name-specifier.  We will
17301      enter the class scope itself while processing the members.  */
17302   if (pushed_scope)
17303     pop_scope (pushed_scope);
17304
17305   if (invalid_explicit_specialization_p)
17306     {
17307       end_specialization ();
17308       --parser->num_template_parameter_lists;
17309     }
17310
17311   if (type)
17312     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17313   *attributes_p = attributes;
17314   return type;
17315 }
17316
17317 /* Parse a class-key.
17318
17319    class-key:
17320      class
17321      struct
17322      union
17323
17324    Returns the kind of class-key specified, or none_type to indicate
17325    error.  */
17326
17327 static enum tag_types
17328 cp_parser_class_key (cp_parser* parser)
17329 {
17330   cp_token *token;
17331   enum tag_types tag_type;
17332
17333   /* Look for the class-key.  */
17334   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17335   if (!token)
17336     return none_type;
17337
17338   /* Check to see if the TOKEN is a class-key.  */
17339   tag_type = cp_parser_token_is_class_key (token);
17340   if (!tag_type)
17341     cp_parser_error (parser, "expected class-key");
17342   return tag_type;
17343 }
17344
17345 /* Parse an (optional) member-specification.
17346
17347    member-specification:
17348      member-declaration member-specification [opt]
17349      access-specifier : member-specification [opt]  */
17350
17351 static void
17352 cp_parser_member_specification_opt (cp_parser* parser)
17353 {
17354   while (true)
17355     {
17356       cp_token *token;
17357       enum rid keyword;
17358
17359       /* Peek at the next token.  */
17360       token = cp_lexer_peek_token (parser->lexer);
17361       /* If it's a `}', or EOF then we've seen all the members.  */
17362       if (token->type == CPP_CLOSE_BRACE
17363           || token->type == CPP_EOF
17364           || token->type == CPP_PRAGMA_EOL)
17365         break;
17366
17367       /* See if this token is a keyword.  */
17368       keyword = token->keyword;
17369       switch (keyword)
17370         {
17371         case RID_PUBLIC:
17372         case RID_PROTECTED:
17373         case RID_PRIVATE:
17374           /* Consume the access-specifier.  */
17375           cp_lexer_consume_token (parser->lexer);
17376           /* Remember which access-specifier is active.  */
17377           current_access_specifier = token->u.value;
17378           /* Look for the `:'.  */
17379           cp_parser_require (parser, CPP_COLON, RT_COLON);
17380           break;
17381
17382         default:
17383           /* Accept #pragmas at class scope.  */
17384           if (token->type == CPP_PRAGMA)
17385             {
17386               cp_parser_pragma (parser, pragma_external);
17387               break;
17388             }
17389
17390           /* Otherwise, the next construction must be a
17391              member-declaration.  */
17392           cp_parser_member_declaration (parser);
17393         }
17394     }
17395 }
17396
17397 /* Parse a member-declaration.
17398
17399    member-declaration:
17400      decl-specifier-seq [opt] member-declarator-list [opt] ;
17401      function-definition ; [opt]
17402      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17403      using-declaration
17404      template-declaration
17405
17406    member-declarator-list:
17407      member-declarator
17408      member-declarator-list , member-declarator
17409
17410    member-declarator:
17411      declarator pure-specifier [opt]
17412      declarator constant-initializer [opt]
17413      identifier [opt] : constant-expression
17414
17415    GNU Extensions:
17416
17417    member-declaration:
17418      __extension__ member-declaration
17419
17420    member-declarator:
17421      declarator attributes [opt] pure-specifier [opt]
17422      declarator attributes [opt] constant-initializer [opt]
17423      identifier [opt] attributes [opt] : constant-expression  
17424
17425    C++0x Extensions:
17426
17427    member-declaration:
17428      static_assert-declaration  */
17429
17430 static void
17431 cp_parser_member_declaration (cp_parser* parser)
17432 {
17433   cp_decl_specifier_seq decl_specifiers;
17434   tree prefix_attributes;
17435   tree decl;
17436   int declares_class_or_enum;
17437   bool friend_p;
17438   cp_token *token = NULL;
17439   cp_token *decl_spec_token_start = NULL;
17440   cp_token *initializer_token_start = NULL;
17441   int saved_pedantic;
17442
17443   /* Check for the `__extension__' keyword.  */
17444   if (cp_parser_extension_opt (parser, &saved_pedantic))
17445     {
17446       /* Recurse.  */
17447       cp_parser_member_declaration (parser);
17448       /* Restore the old value of the PEDANTIC flag.  */
17449       pedantic = saved_pedantic;
17450
17451       return;
17452     }
17453
17454   /* Check for a template-declaration.  */
17455   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17456     {
17457       /* An explicit specialization here is an error condition, and we
17458          expect the specialization handler to detect and report this.  */
17459       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17460           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17461         cp_parser_explicit_specialization (parser);
17462       else
17463         cp_parser_template_declaration (parser, /*member_p=*/true);
17464
17465       return;
17466     }
17467
17468   /* Check for a using-declaration.  */
17469   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17470     {
17471       /* Parse the using-declaration.  */
17472       cp_parser_using_declaration (parser,
17473                                    /*access_declaration_p=*/false);
17474       return;
17475     }
17476
17477   /* Check for @defs.  */
17478   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17479     {
17480       tree ivar, member;
17481       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17482       ivar = ivar_chains;
17483       while (ivar)
17484         {
17485           member = ivar;
17486           ivar = TREE_CHAIN (member);
17487           TREE_CHAIN (member) = NULL_TREE;
17488           finish_member_declaration (member);
17489         }
17490       return;
17491     }
17492
17493   /* If the next token is `static_assert' we have a static assertion.  */
17494   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17495     {
17496       cp_parser_static_assert (parser, /*member_p=*/true);
17497       return;
17498     }
17499
17500   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17501     return;
17502
17503   /* Parse the decl-specifier-seq.  */
17504   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17505   cp_parser_decl_specifier_seq (parser,
17506                                 CP_PARSER_FLAGS_OPTIONAL,
17507                                 &decl_specifiers,
17508                                 &declares_class_or_enum);
17509   prefix_attributes = decl_specifiers.attributes;
17510   decl_specifiers.attributes = NULL_TREE;
17511   /* Check for an invalid type-name.  */
17512   if (!decl_specifiers.any_type_specifiers_p
17513       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17514     return;
17515   /* If there is no declarator, then the decl-specifier-seq should
17516      specify a type.  */
17517   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17518     {
17519       /* If there was no decl-specifier-seq, and the next token is a
17520          `;', then we have something like:
17521
17522            struct S { ; };
17523
17524          [class.mem]
17525
17526          Each member-declaration shall declare at least one member
17527          name of the class.  */
17528       if (!decl_specifiers.any_specifiers_p)
17529         {
17530           cp_token *token = cp_lexer_peek_token (parser->lexer);
17531           if (!in_system_header_at (token->location))
17532             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17533         }
17534       else
17535         {
17536           tree type;
17537
17538           /* See if this declaration is a friend.  */
17539           friend_p = cp_parser_friend_p (&decl_specifiers);
17540           /* If there were decl-specifiers, check to see if there was
17541              a class-declaration.  */
17542           type = check_tag_decl (&decl_specifiers);
17543           /* Nested classes have already been added to the class, but
17544              a `friend' needs to be explicitly registered.  */
17545           if (friend_p)
17546             {
17547               /* If the `friend' keyword was present, the friend must
17548                  be introduced with a class-key.  */
17549                if (!declares_class_or_enum)
17550                  error_at (decl_spec_token_start->location,
17551                            "a class-key must be used when declaring a friend");
17552                /* In this case:
17553
17554                     template <typename T> struct A {
17555                       friend struct A<T>::B;
17556                     };
17557
17558                   A<T>::B will be represented by a TYPENAME_TYPE, and
17559                   therefore not recognized by check_tag_decl.  */
17560                if (!type
17561                    && decl_specifiers.type
17562                    && TYPE_P (decl_specifiers.type))
17563                  type = decl_specifiers.type;
17564                if (!type || !TYPE_P (type))
17565                  error_at (decl_spec_token_start->location,
17566                            "friend declaration does not name a class or "
17567                            "function");
17568                else
17569                  make_friend_class (current_class_type, type,
17570                                     /*complain=*/true);
17571             }
17572           /* If there is no TYPE, an error message will already have
17573              been issued.  */
17574           else if (!type || type == error_mark_node)
17575             ;
17576           /* An anonymous aggregate has to be handled specially; such
17577              a declaration really declares a data member (with a
17578              particular type), as opposed to a nested class.  */
17579           else if (ANON_AGGR_TYPE_P (type))
17580             {
17581               /* Remove constructors and such from TYPE, now that we
17582                  know it is an anonymous aggregate.  */
17583               fixup_anonymous_aggr (type);
17584               /* And make the corresponding data member.  */
17585               decl = build_decl (decl_spec_token_start->location,
17586                                  FIELD_DECL, NULL_TREE, type);
17587               /* Add it to the class.  */
17588               finish_member_declaration (decl);
17589             }
17590           else
17591             cp_parser_check_access_in_redeclaration
17592                                               (TYPE_NAME (type),
17593                                                decl_spec_token_start->location);
17594         }
17595     }
17596   else
17597     {
17598       /* See if these declarations will be friends.  */
17599       friend_p = cp_parser_friend_p (&decl_specifiers);
17600
17601       /* Keep going until we hit the `;' at the end of the
17602          declaration.  */
17603       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17604         {
17605           tree attributes = NULL_TREE;
17606           tree first_attribute;
17607
17608           /* Peek at the next token.  */
17609           token = cp_lexer_peek_token (parser->lexer);
17610
17611           /* Check for a bitfield declaration.  */
17612           if (token->type == CPP_COLON
17613               || (token->type == CPP_NAME
17614                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17615                   == CPP_COLON))
17616             {
17617               tree identifier;
17618               tree width;
17619
17620               /* Get the name of the bitfield.  Note that we cannot just
17621                  check TOKEN here because it may have been invalidated by
17622                  the call to cp_lexer_peek_nth_token above.  */
17623               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17624                 identifier = cp_parser_identifier (parser);
17625               else
17626                 identifier = NULL_TREE;
17627
17628               /* Consume the `:' token.  */
17629               cp_lexer_consume_token (parser->lexer);
17630               /* Get the width of the bitfield.  */
17631               width
17632                 = cp_parser_constant_expression (parser,
17633                                                  /*allow_non_constant=*/false,
17634                                                  NULL);
17635
17636               /* Look for attributes that apply to the bitfield.  */
17637               attributes = cp_parser_attributes_opt (parser);
17638               /* Remember which attributes are prefix attributes and
17639                  which are not.  */
17640               first_attribute = attributes;
17641               /* Combine the attributes.  */
17642               attributes = chainon (prefix_attributes, attributes);
17643
17644               /* Create the bitfield declaration.  */
17645               decl = grokbitfield (identifier
17646                                    ? make_id_declarator (NULL_TREE,
17647                                                          identifier,
17648                                                          sfk_none)
17649                                    : NULL,
17650                                    &decl_specifiers,
17651                                    width,
17652                                    attributes);
17653             }
17654           else
17655             {
17656               cp_declarator *declarator;
17657               tree initializer;
17658               tree asm_specification;
17659               int ctor_dtor_or_conv_p;
17660
17661               /* Parse the declarator.  */
17662               declarator
17663                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17664                                         &ctor_dtor_or_conv_p,
17665                                         /*parenthesized_p=*/NULL,
17666                                         /*member_p=*/true);
17667
17668               /* If something went wrong parsing the declarator, make sure
17669                  that we at least consume some tokens.  */
17670               if (declarator == cp_error_declarator)
17671                 {
17672                   /* Skip to the end of the statement.  */
17673                   cp_parser_skip_to_end_of_statement (parser);
17674                   /* If the next token is not a semicolon, that is
17675                      probably because we just skipped over the body of
17676                      a function.  So, we consume a semicolon if
17677                      present, but do not issue an error message if it
17678                      is not present.  */
17679                   if (cp_lexer_next_token_is (parser->lexer,
17680                                               CPP_SEMICOLON))
17681                     cp_lexer_consume_token (parser->lexer);
17682                   return;
17683                 }
17684
17685               if (declares_class_or_enum & 2)
17686                 cp_parser_check_for_definition_in_return_type
17687                                             (declarator, decl_specifiers.type,
17688                                              decl_specifiers.type_location);
17689
17690               /* Look for an asm-specification.  */
17691               asm_specification = cp_parser_asm_specification_opt (parser);
17692               /* Look for attributes that apply to the declaration.  */
17693               attributes = cp_parser_attributes_opt (parser);
17694               /* Remember which attributes are prefix attributes and
17695                  which are not.  */
17696               first_attribute = attributes;
17697               /* Combine the attributes.  */
17698               attributes = chainon (prefix_attributes, attributes);
17699
17700               /* If it's an `=', then we have a constant-initializer or a
17701                  pure-specifier.  It is not correct to parse the
17702                  initializer before registering the member declaration
17703                  since the member declaration should be in scope while
17704                  its initializer is processed.  However, the rest of the
17705                  front end does not yet provide an interface that allows
17706                  us to handle this correctly.  */
17707               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17708                 {
17709                   /* In [class.mem]:
17710
17711                      A pure-specifier shall be used only in the declaration of
17712                      a virtual function.
17713
17714                      A member-declarator can contain a constant-initializer
17715                      only if it declares a static member of integral or
17716                      enumeration type.
17717
17718                      Therefore, if the DECLARATOR is for a function, we look
17719                      for a pure-specifier; otherwise, we look for a
17720                      constant-initializer.  When we call `grokfield', it will
17721                      perform more stringent semantics checks.  */
17722                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17723                   if (function_declarator_p (declarator))
17724                     initializer = cp_parser_pure_specifier (parser);
17725                   else
17726                     /* Parse the initializer.  */
17727                     initializer = cp_parser_constant_initializer (parser);
17728                 }
17729               /* Otherwise, there is no initializer.  */
17730               else
17731                 initializer = NULL_TREE;
17732
17733               /* See if we are probably looking at a function
17734                  definition.  We are certainly not looking at a
17735                  member-declarator.  Calling `grokfield' has
17736                  side-effects, so we must not do it unless we are sure
17737                  that we are looking at a member-declarator.  */
17738               if (cp_parser_token_starts_function_definition_p
17739                   (cp_lexer_peek_token (parser->lexer)))
17740                 {
17741                   /* The grammar does not allow a pure-specifier to be
17742                      used when a member function is defined.  (It is
17743                      possible that this fact is an oversight in the
17744                      standard, since a pure function may be defined
17745                      outside of the class-specifier.  */
17746                   if (initializer)
17747                     error_at (initializer_token_start->location,
17748                               "pure-specifier on function-definition");
17749                   decl = cp_parser_save_member_function_body (parser,
17750                                                               &decl_specifiers,
17751                                                               declarator,
17752                                                               attributes);
17753                   /* If the member was not a friend, declare it here.  */
17754                   if (!friend_p)
17755                     finish_member_declaration (decl);
17756                   /* Peek at the next token.  */
17757                   token = cp_lexer_peek_token (parser->lexer);
17758                   /* If the next token is a semicolon, consume it.  */
17759                   if (token->type == CPP_SEMICOLON)
17760                     cp_lexer_consume_token (parser->lexer);
17761                   return;
17762                 }
17763               else
17764                 if (declarator->kind == cdk_function)
17765                   declarator->id_loc = token->location;
17766                 /* Create the declaration.  */
17767                 decl = grokfield (declarator, &decl_specifiers,
17768                                   initializer, /*init_const_expr_p=*/true,
17769                                   asm_specification,
17770                                   attributes);
17771             }
17772
17773           /* Reset PREFIX_ATTRIBUTES.  */
17774           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17775             attributes = TREE_CHAIN (attributes);
17776           if (attributes)
17777             TREE_CHAIN (attributes) = NULL_TREE;
17778
17779           /* If there is any qualification still in effect, clear it
17780              now; we will be starting fresh with the next declarator.  */
17781           parser->scope = NULL_TREE;
17782           parser->qualifying_scope = NULL_TREE;
17783           parser->object_scope = NULL_TREE;
17784           /* If it's a `,', then there are more declarators.  */
17785           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17786             cp_lexer_consume_token (parser->lexer);
17787           /* If the next token isn't a `;', then we have a parse error.  */
17788           else if (cp_lexer_next_token_is_not (parser->lexer,
17789                                                CPP_SEMICOLON))
17790             {
17791               cp_parser_error (parser, "expected %<;%>");
17792               /* Skip tokens until we find a `;'.  */
17793               cp_parser_skip_to_end_of_statement (parser);
17794
17795               break;
17796             }
17797
17798           if (decl)
17799             {
17800               /* Add DECL to the list of members.  */
17801               if (!friend_p)
17802                 finish_member_declaration (decl);
17803
17804               if (TREE_CODE (decl) == FUNCTION_DECL)
17805                 cp_parser_save_default_args (parser, decl);
17806             }
17807         }
17808     }
17809
17810   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17811 }
17812
17813 /* Parse a pure-specifier.
17814
17815    pure-specifier:
17816      = 0
17817
17818    Returns INTEGER_ZERO_NODE if a pure specifier is found.
17819    Otherwise, ERROR_MARK_NODE is returned.  */
17820
17821 static tree
17822 cp_parser_pure_specifier (cp_parser* parser)
17823 {
17824   cp_token *token;
17825
17826   /* Look for the `=' token.  */
17827   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17828     return error_mark_node;
17829   /* Look for the `0' token.  */
17830   token = cp_lexer_peek_token (parser->lexer);
17831
17832   if (token->type == CPP_EOF
17833       || token->type == CPP_PRAGMA_EOL)
17834     return error_mark_node;
17835
17836   cp_lexer_consume_token (parser->lexer);
17837
17838   /* Accept = default or = delete in c++0x mode.  */
17839   if (token->keyword == RID_DEFAULT
17840       || token->keyword == RID_DELETE)
17841     {
17842       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17843       return token->u.value;
17844     }
17845
17846   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
17847   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17848     {
17849       cp_parser_error (parser,
17850                        "invalid pure specifier (only %<= 0%> is allowed)");
17851       cp_parser_skip_to_end_of_statement (parser);
17852       return error_mark_node;
17853     }
17854   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17855     {
17856       error_at (token->location, "templates may not be %<virtual%>");
17857       return error_mark_node;
17858     }
17859
17860   return integer_zero_node;
17861 }
17862
17863 /* Parse a constant-initializer.
17864
17865    constant-initializer:
17866      = constant-expression
17867
17868    Returns a representation of the constant-expression.  */
17869
17870 static tree
17871 cp_parser_constant_initializer (cp_parser* parser)
17872 {
17873   /* Look for the `=' token.  */
17874   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17875     return error_mark_node;
17876
17877   /* It is invalid to write:
17878
17879        struct S { static const int i = { 7 }; };
17880
17881      */
17882   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17883     {
17884       cp_parser_error (parser,
17885                        "a brace-enclosed initializer is not allowed here");
17886       /* Consume the opening brace.  */
17887       cp_lexer_consume_token (parser->lexer);
17888       /* Skip the initializer.  */
17889       cp_parser_skip_to_closing_brace (parser);
17890       /* Look for the trailing `}'.  */
17891       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17892
17893       return error_mark_node;
17894     }
17895
17896   return cp_parser_constant_expression (parser,
17897                                         /*allow_non_constant=*/false,
17898                                         NULL);
17899 }
17900
17901 /* Derived classes [gram.class.derived] */
17902
17903 /* Parse a base-clause.
17904
17905    base-clause:
17906      : base-specifier-list
17907
17908    base-specifier-list:
17909      base-specifier ... [opt]
17910      base-specifier-list , base-specifier ... [opt]
17911
17912    Returns a TREE_LIST representing the base-classes, in the order in
17913    which they were declared.  The representation of each node is as
17914    described by cp_parser_base_specifier.
17915
17916    In the case that no bases are specified, this function will return
17917    NULL_TREE, not ERROR_MARK_NODE.  */
17918
17919 static tree
17920 cp_parser_base_clause (cp_parser* parser)
17921 {
17922   tree bases = NULL_TREE;
17923
17924   /* Look for the `:' that begins the list.  */
17925   cp_parser_require (parser, CPP_COLON, RT_COLON);
17926
17927   /* Scan the base-specifier-list.  */
17928   while (true)
17929     {
17930       cp_token *token;
17931       tree base;
17932       bool pack_expansion_p = false;
17933
17934       /* Look for the base-specifier.  */
17935       base = cp_parser_base_specifier (parser);
17936       /* Look for the (optional) ellipsis. */
17937       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17938         {
17939           /* Consume the `...'. */
17940           cp_lexer_consume_token (parser->lexer);
17941
17942           pack_expansion_p = true;
17943         }
17944
17945       /* Add BASE to the front of the list.  */
17946       if (base != error_mark_node)
17947         {
17948           if (pack_expansion_p)
17949             /* Make this a pack expansion type. */
17950             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17951           
17952
17953           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17954             {
17955               TREE_CHAIN (base) = bases;
17956               bases = base;
17957             }
17958         }
17959       /* Peek at the next token.  */
17960       token = cp_lexer_peek_token (parser->lexer);
17961       /* If it's not a comma, then the list is complete.  */
17962       if (token->type != CPP_COMMA)
17963         break;
17964       /* Consume the `,'.  */
17965       cp_lexer_consume_token (parser->lexer);
17966     }
17967
17968   /* PARSER->SCOPE may still be non-NULL at this point, if the last
17969      base class had a qualified name.  However, the next name that
17970      appears is certainly not qualified.  */
17971   parser->scope = NULL_TREE;
17972   parser->qualifying_scope = NULL_TREE;
17973   parser->object_scope = NULL_TREE;
17974
17975   return nreverse (bases);
17976 }
17977
17978 /* Parse a base-specifier.
17979
17980    base-specifier:
17981      :: [opt] nested-name-specifier [opt] class-name
17982      virtual access-specifier [opt] :: [opt] nested-name-specifier
17983        [opt] class-name
17984      access-specifier virtual [opt] :: [opt] nested-name-specifier
17985        [opt] class-name
17986
17987    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17988    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17989    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17990    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17991
17992 static tree
17993 cp_parser_base_specifier (cp_parser* parser)
17994 {
17995   cp_token *token;
17996   bool done = false;
17997   bool virtual_p = false;
17998   bool duplicate_virtual_error_issued_p = false;
17999   bool duplicate_access_error_issued_p = false;
18000   bool class_scope_p, template_p;
18001   tree access = access_default_node;
18002   tree type;
18003
18004   /* Process the optional `virtual' and `access-specifier'.  */
18005   while (!done)
18006     {
18007       /* Peek at the next token.  */
18008       token = cp_lexer_peek_token (parser->lexer);
18009       /* Process `virtual'.  */
18010       switch (token->keyword)
18011         {
18012         case RID_VIRTUAL:
18013           /* If `virtual' appears more than once, issue an error.  */
18014           if (virtual_p && !duplicate_virtual_error_issued_p)
18015             {
18016               cp_parser_error (parser,
18017                                "%<virtual%> specified more than once in base-specified");
18018               duplicate_virtual_error_issued_p = true;
18019             }
18020
18021           virtual_p = true;
18022
18023           /* Consume the `virtual' token.  */
18024           cp_lexer_consume_token (parser->lexer);
18025
18026           break;
18027
18028         case RID_PUBLIC:
18029         case RID_PROTECTED:
18030         case RID_PRIVATE:
18031           /* If more than one access specifier appears, issue an
18032              error.  */
18033           if (access != access_default_node
18034               && !duplicate_access_error_issued_p)
18035             {
18036               cp_parser_error (parser,
18037                                "more than one access specifier in base-specified");
18038               duplicate_access_error_issued_p = true;
18039             }
18040
18041           access = ridpointers[(int) token->keyword];
18042
18043           /* Consume the access-specifier.  */
18044           cp_lexer_consume_token (parser->lexer);
18045
18046           break;
18047
18048         default:
18049           done = true;
18050           break;
18051         }
18052     }
18053   /* It is not uncommon to see programs mechanically, erroneously, use
18054      the 'typename' keyword to denote (dependent) qualified types
18055      as base classes.  */
18056   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18057     {
18058       token = cp_lexer_peek_token (parser->lexer);
18059       if (!processing_template_decl)
18060         error_at (token->location,
18061                   "keyword %<typename%> not allowed outside of templates");
18062       else
18063         error_at (token->location,
18064                   "keyword %<typename%> not allowed in this context "
18065                   "(the base class is implicitly a type)");
18066       cp_lexer_consume_token (parser->lexer);
18067     }
18068
18069   /* Look for the optional `::' operator.  */
18070   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18071   /* Look for the nested-name-specifier.  The simplest way to
18072      implement:
18073
18074        [temp.res]
18075
18076        The keyword `typename' is not permitted in a base-specifier or
18077        mem-initializer; in these contexts a qualified name that
18078        depends on a template-parameter is implicitly assumed to be a
18079        type name.
18080
18081      is to pretend that we have seen the `typename' keyword at this
18082      point.  */
18083   cp_parser_nested_name_specifier_opt (parser,
18084                                        /*typename_keyword_p=*/true,
18085                                        /*check_dependency_p=*/true,
18086                                        typename_type,
18087                                        /*is_declaration=*/true);
18088   /* If the base class is given by a qualified name, assume that names
18089      we see are type names or templates, as appropriate.  */
18090   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18091   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18092
18093   /* Finally, look for the class-name.  */
18094   type = cp_parser_class_name (parser,
18095                                class_scope_p,
18096                                template_p,
18097                                typename_type,
18098                                /*check_dependency_p=*/true,
18099                                /*class_head_p=*/false,
18100                                /*is_declaration=*/true);
18101
18102   if (type == error_mark_node)
18103     return error_mark_node;
18104
18105   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18106 }
18107
18108 /* Exception handling [gram.exception] */
18109
18110 /* Parse an (optional) exception-specification.
18111
18112    exception-specification:
18113      throw ( type-id-list [opt] )
18114
18115    Returns a TREE_LIST representing the exception-specification.  The
18116    TREE_VALUE of each node is a type.  */
18117
18118 static tree
18119 cp_parser_exception_specification_opt (cp_parser* parser)
18120 {
18121   cp_token *token;
18122   tree type_id_list;
18123   const char *saved_message;
18124
18125   /* Peek at the next token.  */
18126   token = cp_lexer_peek_token (parser->lexer);
18127
18128   /* Is it a noexcept-specification?  */
18129   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18130     {
18131       tree expr;
18132       cp_lexer_consume_token (parser->lexer);
18133
18134       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18135         {
18136           cp_lexer_consume_token (parser->lexer);
18137
18138           /* Types may not be defined in an exception-specification.  */
18139           saved_message = parser->type_definition_forbidden_message;
18140           parser->type_definition_forbidden_message
18141             = G_("types may not be defined in an exception-specification");
18142
18143           expr = cp_parser_constant_expression (parser, false, NULL);
18144
18145           /* Restore the saved message.  */
18146           parser->type_definition_forbidden_message = saved_message;
18147
18148           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18149         }
18150       else
18151         expr = boolean_true_node;
18152
18153       return build_noexcept_spec (expr, tf_warning_or_error);
18154     }
18155
18156   /* If it's not `throw', then there's no exception-specification.  */
18157   if (!cp_parser_is_keyword (token, RID_THROW))
18158     return NULL_TREE;
18159
18160 #if 0
18161   /* Enable this once a lot of code has transitioned to noexcept?  */
18162   if (cxx_dialect == cxx0x && !in_system_header)
18163     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18164              "deprecated in C++0x; use %<noexcept%> instead.");
18165 #endif
18166
18167   /* Consume the `throw'.  */
18168   cp_lexer_consume_token (parser->lexer);
18169
18170   /* Look for the `('.  */
18171   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18172
18173   /* Peek at the next token.  */
18174   token = cp_lexer_peek_token (parser->lexer);
18175   /* If it's not a `)', then there is a type-id-list.  */
18176   if (token->type != CPP_CLOSE_PAREN)
18177     {
18178       /* Types may not be defined in an exception-specification.  */
18179       saved_message = parser->type_definition_forbidden_message;
18180       parser->type_definition_forbidden_message
18181         = G_("types may not be defined in an exception-specification");
18182       /* Parse the type-id-list.  */
18183       type_id_list = cp_parser_type_id_list (parser);
18184       /* Restore the saved message.  */
18185       parser->type_definition_forbidden_message = saved_message;
18186     }
18187   else
18188     type_id_list = empty_except_spec;
18189
18190   /* Look for the `)'.  */
18191   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18192
18193   return type_id_list;
18194 }
18195
18196 /* Parse an (optional) type-id-list.
18197
18198    type-id-list:
18199      type-id ... [opt]
18200      type-id-list , type-id ... [opt]
18201
18202    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18203    in the order that the types were presented.  */
18204
18205 static tree
18206 cp_parser_type_id_list (cp_parser* parser)
18207 {
18208   tree types = NULL_TREE;
18209
18210   while (true)
18211     {
18212       cp_token *token;
18213       tree type;
18214
18215       /* Get the next type-id.  */
18216       type = cp_parser_type_id (parser);
18217       /* Parse the optional ellipsis. */
18218       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18219         {
18220           /* Consume the `...'. */
18221           cp_lexer_consume_token (parser->lexer);
18222
18223           /* Turn the type into a pack expansion expression. */
18224           type = make_pack_expansion (type);
18225         }
18226       /* Add it to the list.  */
18227       types = add_exception_specifier (types, type, /*complain=*/1);
18228       /* Peek at the next token.  */
18229       token = cp_lexer_peek_token (parser->lexer);
18230       /* If it is not a `,', we are done.  */
18231       if (token->type != CPP_COMMA)
18232         break;
18233       /* Consume the `,'.  */
18234       cp_lexer_consume_token (parser->lexer);
18235     }
18236
18237   return nreverse (types);
18238 }
18239
18240 /* Parse a try-block.
18241
18242    try-block:
18243      try compound-statement handler-seq  */
18244
18245 static tree
18246 cp_parser_try_block (cp_parser* parser)
18247 {
18248   tree try_block;
18249
18250   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18251   try_block = begin_try_block ();
18252   cp_parser_compound_statement (parser, NULL, true);
18253   finish_try_block (try_block);
18254   cp_parser_handler_seq (parser);
18255   finish_handler_sequence (try_block);
18256
18257   return try_block;
18258 }
18259
18260 /* Parse a function-try-block.
18261
18262    function-try-block:
18263      try ctor-initializer [opt] function-body handler-seq  */
18264
18265 static bool
18266 cp_parser_function_try_block (cp_parser* parser)
18267 {
18268   tree compound_stmt;
18269   tree try_block;
18270   bool ctor_initializer_p;
18271
18272   /* Look for the `try' keyword.  */
18273   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18274     return false;
18275   /* Let the rest of the front end know where we are.  */
18276   try_block = begin_function_try_block (&compound_stmt);
18277   /* Parse the function-body.  */
18278   ctor_initializer_p
18279     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18280   /* We're done with the `try' part.  */
18281   finish_function_try_block (try_block);
18282   /* Parse the handlers.  */
18283   cp_parser_handler_seq (parser);
18284   /* We're done with the handlers.  */
18285   finish_function_handler_sequence (try_block, compound_stmt);
18286
18287   return ctor_initializer_p;
18288 }
18289
18290 /* Parse a handler-seq.
18291
18292    handler-seq:
18293      handler handler-seq [opt]  */
18294
18295 static void
18296 cp_parser_handler_seq (cp_parser* parser)
18297 {
18298   while (true)
18299     {
18300       cp_token *token;
18301
18302       /* Parse the handler.  */
18303       cp_parser_handler (parser);
18304       /* Peek at the next token.  */
18305       token = cp_lexer_peek_token (parser->lexer);
18306       /* If it's not `catch' then there are no more handlers.  */
18307       if (!cp_parser_is_keyword (token, RID_CATCH))
18308         break;
18309     }
18310 }
18311
18312 /* Parse a handler.
18313
18314    handler:
18315      catch ( exception-declaration ) compound-statement  */
18316
18317 static void
18318 cp_parser_handler (cp_parser* parser)
18319 {
18320   tree handler;
18321   tree declaration;
18322
18323   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18324   handler = begin_handler ();
18325   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18326   declaration = cp_parser_exception_declaration (parser);
18327   finish_handler_parms (declaration, handler);
18328   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18329   cp_parser_compound_statement (parser, NULL, false);
18330   finish_handler (handler);
18331 }
18332
18333 /* Parse an exception-declaration.
18334
18335    exception-declaration:
18336      type-specifier-seq declarator
18337      type-specifier-seq abstract-declarator
18338      type-specifier-seq
18339      ...
18340
18341    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18342    ellipsis variant is used.  */
18343
18344 static tree
18345 cp_parser_exception_declaration (cp_parser* parser)
18346 {
18347   cp_decl_specifier_seq type_specifiers;
18348   cp_declarator *declarator;
18349   const char *saved_message;
18350
18351   /* If it's an ellipsis, it's easy to handle.  */
18352   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18353     {
18354       /* Consume the `...' token.  */
18355       cp_lexer_consume_token (parser->lexer);
18356       return NULL_TREE;
18357     }
18358
18359   /* Types may not be defined in exception-declarations.  */
18360   saved_message = parser->type_definition_forbidden_message;
18361   parser->type_definition_forbidden_message
18362     = G_("types may not be defined in exception-declarations");
18363
18364   /* Parse the type-specifier-seq.  */
18365   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18366                                 /*is_trailing_return=*/false,
18367                                 &type_specifiers);
18368   /* If it's a `)', then there is no declarator.  */
18369   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18370     declarator = NULL;
18371   else
18372     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18373                                        /*ctor_dtor_or_conv_p=*/NULL,
18374                                        /*parenthesized_p=*/NULL,
18375                                        /*member_p=*/false);
18376
18377   /* Restore the saved message.  */
18378   parser->type_definition_forbidden_message = saved_message;
18379
18380   if (!type_specifiers.any_specifiers_p)
18381     return error_mark_node;
18382
18383   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18384 }
18385
18386 /* Parse a throw-expression.
18387
18388    throw-expression:
18389      throw assignment-expression [opt]
18390
18391    Returns a THROW_EXPR representing the throw-expression.  */
18392
18393 static tree
18394 cp_parser_throw_expression (cp_parser* parser)
18395 {
18396   tree expression;
18397   cp_token* token;
18398
18399   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18400   token = cp_lexer_peek_token (parser->lexer);
18401   /* Figure out whether or not there is an assignment-expression
18402      following the "throw" keyword.  */
18403   if (token->type == CPP_COMMA
18404       || token->type == CPP_SEMICOLON
18405       || token->type == CPP_CLOSE_PAREN
18406       || token->type == CPP_CLOSE_SQUARE
18407       || token->type == CPP_CLOSE_BRACE
18408       || token->type == CPP_COLON)
18409     expression = NULL_TREE;
18410   else
18411     expression = cp_parser_assignment_expression (parser,
18412                                                   /*cast_p=*/false, NULL);
18413
18414   return build_throw (expression);
18415 }
18416
18417 /* GNU Extensions */
18418
18419 /* Parse an (optional) asm-specification.
18420
18421    asm-specification:
18422      asm ( string-literal )
18423
18424    If the asm-specification is present, returns a STRING_CST
18425    corresponding to the string-literal.  Otherwise, returns
18426    NULL_TREE.  */
18427
18428 static tree
18429 cp_parser_asm_specification_opt (cp_parser* parser)
18430 {
18431   cp_token *token;
18432   tree asm_specification;
18433
18434   /* Peek at the next token.  */
18435   token = cp_lexer_peek_token (parser->lexer);
18436   /* If the next token isn't the `asm' keyword, then there's no
18437      asm-specification.  */
18438   if (!cp_parser_is_keyword (token, RID_ASM))
18439     return NULL_TREE;
18440
18441   /* Consume the `asm' token.  */
18442   cp_lexer_consume_token (parser->lexer);
18443   /* Look for the `('.  */
18444   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18445
18446   /* Look for the string-literal.  */
18447   asm_specification = cp_parser_string_literal (parser, false, false);
18448
18449   /* Look for the `)'.  */
18450   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18451
18452   return asm_specification;
18453 }
18454
18455 /* Parse an asm-operand-list.
18456
18457    asm-operand-list:
18458      asm-operand
18459      asm-operand-list , asm-operand
18460
18461    asm-operand:
18462      string-literal ( expression )
18463      [ string-literal ] string-literal ( expression )
18464
18465    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18466    each node is the expression.  The TREE_PURPOSE is itself a
18467    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18468    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18469    is a STRING_CST for the string literal before the parenthesis. Returns
18470    ERROR_MARK_NODE if any of the operands are invalid.  */
18471
18472 static tree
18473 cp_parser_asm_operand_list (cp_parser* parser)
18474 {
18475   tree asm_operands = NULL_TREE;
18476   bool invalid_operands = false;
18477
18478   while (true)
18479     {
18480       tree string_literal;
18481       tree expression;
18482       tree name;
18483
18484       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18485         {
18486           /* Consume the `[' token.  */
18487           cp_lexer_consume_token (parser->lexer);
18488           /* Read the operand name.  */
18489           name = cp_parser_identifier (parser);
18490           if (name != error_mark_node)
18491             name = build_string (IDENTIFIER_LENGTH (name),
18492                                  IDENTIFIER_POINTER (name));
18493           /* Look for the closing `]'.  */
18494           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18495         }
18496       else
18497         name = NULL_TREE;
18498       /* Look for the string-literal.  */
18499       string_literal = cp_parser_string_literal (parser, false, false);
18500
18501       /* Look for the `('.  */
18502       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18503       /* Parse the expression.  */
18504       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18505       /* Look for the `)'.  */
18506       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18507
18508       if (name == error_mark_node 
18509           || string_literal == error_mark_node 
18510           || expression == error_mark_node)
18511         invalid_operands = true;
18512
18513       /* Add this operand to the list.  */
18514       asm_operands = tree_cons (build_tree_list (name, string_literal),
18515                                 expression,
18516                                 asm_operands);
18517       /* If the next token is not a `,', there are no more
18518          operands.  */
18519       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18520         break;
18521       /* Consume the `,'.  */
18522       cp_lexer_consume_token (parser->lexer);
18523     }
18524
18525   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18526 }
18527
18528 /* Parse an asm-clobber-list.
18529
18530    asm-clobber-list:
18531      string-literal
18532      asm-clobber-list , string-literal
18533
18534    Returns a TREE_LIST, indicating the clobbers in the order that they
18535    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18536
18537 static tree
18538 cp_parser_asm_clobber_list (cp_parser* parser)
18539 {
18540   tree clobbers = NULL_TREE;
18541
18542   while (true)
18543     {
18544       tree string_literal;
18545
18546       /* Look for the string literal.  */
18547       string_literal = cp_parser_string_literal (parser, false, false);
18548       /* Add it to the list.  */
18549       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18550       /* If the next token is not a `,', then the list is
18551          complete.  */
18552       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18553         break;
18554       /* Consume the `,' token.  */
18555       cp_lexer_consume_token (parser->lexer);
18556     }
18557
18558   return clobbers;
18559 }
18560
18561 /* Parse an asm-label-list.
18562
18563    asm-label-list:
18564      identifier
18565      asm-label-list , identifier
18566
18567    Returns a TREE_LIST, indicating the labels in the order that they
18568    appeared.  The TREE_VALUE of each node is a label.  */
18569
18570 static tree
18571 cp_parser_asm_label_list (cp_parser* parser)
18572 {
18573   tree labels = NULL_TREE;
18574
18575   while (true)
18576     {
18577       tree identifier, label, name;
18578
18579       /* Look for the identifier.  */
18580       identifier = cp_parser_identifier (parser);
18581       if (!error_operand_p (identifier))
18582         {
18583           label = lookup_label (identifier);
18584           if (TREE_CODE (label) == LABEL_DECL)
18585             {
18586               TREE_USED (label) = 1;
18587               check_goto (label);
18588               name = build_string (IDENTIFIER_LENGTH (identifier),
18589                                    IDENTIFIER_POINTER (identifier));
18590               labels = tree_cons (name, label, labels);
18591             }
18592         }
18593       /* If the next token is not a `,', then the list is
18594          complete.  */
18595       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18596         break;
18597       /* Consume the `,' token.  */
18598       cp_lexer_consume_token (parser->lexer);
18599     }
18600
18601   return nreverse (labels);
18602 }
18603
18604 /* Parse an (optional) series of attributes.
18605
18606    attributes:
18607      attributes attribute
18608
18609    attribute:
18610      __attribute__ (( attribute-list [opt] ))
18611
18612    The return value is as for cp_parser_attribute_list.  */
18613
18614 static tree
18615 cp_parser_attributes_opt (cp_parser* parser)
18616 {
18617   tree attributes = NULL_TREE;
18618
18619   while (true)
18620     {
18621       cp_token *token;
18622       tree attribute_list;
18623
18624       /* Peek at the next token.  */
18625       token = cp_lexer_peek_token (parser->lexer);
18626       /* If it's not `__attribute__', then we're done.  */
18627       if (token->keyword != RID_ATTRIBUTE)
18628         break;
18629
18630       /* Consume the `__attribute__' keyword.  */
18631       cp_lexer_consume_token (parser->lexer);
18632       /* Look for the two `(' tokens.  */
18633       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18634       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18635
18636       /* Peek at the next token.  */
18637       token = cp_lexer_peek_token (parser->lexer);
18638       if (token->type != CPP_CLOSE_PAREN)
18639         /* Parse the attribute-list.  */
18640         attribute_list = cp_parser_attribute_list (parser);
18641       else
18642         /* If the next token is a `)', then there is no attribute
18643            list.  */
18644         attribute_list = NULL;
18645
18646       /* Look for the two `)' tokens.  */
18647       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18648       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18649
18650       /* Add these new attributes to the list.  */
18651       attributes = chainon (attributes, attribute_list);
18652     }
18653
18654   return attributes;
18655 }
18656
18657 /* Parse an attribute-list.
18658
18659    attribute-list:
18660      attribute
18661      attribute-list , attribute
18662
18663    attribute:
18664      identifier
18665      identifier ( identifier )
18666      identifier ( identifier , expression-list )
18667      identifier ( expression-list )
18668
18669    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18670    to an attribute.  The TREE_PURPOSE of each node is the identifier
18671    indicating which attribute is in use.  The TREE_VALUE represents
18672    the arguments, if any.  */
18673
18674 static tree
18675 cp_parser_attribute_list (cp_parser* parser)
18676 {
18677   tree attribute_list = NULL_TREE;
18678   bool save_translate_strings_p = parser->translate_strings_p;
18679
18680   parser->translate_strings_p = false;
18681   while (true)
18682     {
18683       cp_token *token;
18684       tree identifier;
18685       tree attribute;
18686
18687       /* Look for the identifier.  We also allow keywords here; for
18688          example `__attribute__ ((const))' is legal.  */
18689       token = cp_lexer_peek_token (parser->lexer);
18690       if (token->type == CPP_NAME
18691           || token->type == CPP_KEYWORD)
18692         {
18693           tree arguments = NULL_TREE;
18694
18695           /* Consume the token.  */
18696           token = cp_lexer_consume_token (parser->lexer);
18697
18698           /* Save away the identifier that indicates which attribute
18699              this is.  */
18700           identifier = (token->type == CPP_KEYWORD) 
18701             /* For keywords, use the canonical spelling, not the
18702                parsed identifier.  */
18703             ? ridpointers[(int) token->keyword]
18704             : token->u.value;
18705           
18706           attribute = build_tree_list (identifier, NULL_TREE);
18707
18708           /* Peek at the next token.  */
18709           token = cp_lexer_peek_token (parser->lexer);
18710           /* If it's an `(', then parse the attribute arguments.  */
18711           if (token->type == CPP_OPEN_PAREN)
18712             {
18713               VEC(tree,gc) *vec;
18714               int attr_flag = (attribute_takes_identifier_p (identifier)
18715                                ? id_attr : normal_attr);
18716               vec = cp_parser_parenthesized_expression_list
18717                     (parser, attr_flag, /*cast_p=*/false,
18718                      /*allow_expansion_p=*/false,
18719                      /*non_constant_p=*/NULL);
18720               if (vec == NULL)
18721                 arguments = error_mark_node;
18722               else
18723                 {
18724                   arguments = build_tree_list_vec (vec);
18725                   release_tree_vector (vec);
18726                 }
18727               /* Save the arguments away.  */
18728               TREE_VALUE (attribute) = arguments;
18729             }
18730
18731           if (arguments != error_mark_node)
18732             {
18733               /* Add this attribute to the list.  */
18734               TREE_CHAIN (attribute) = attribute_list;
18735               attribute_list = attribute;
18736             }
18737
18738           token = cp_lexer_peek_token (parser->lexer);
18739         }
18740       /* Now, look for more attributes.  If the next token isn't a
18741          `,', we're done.  */
18742       if (token->type != CPP_COMMA)
18743         break;
18744
18745       /* Consume the comma and keep going.  */
18746       cp_lexer_consume_token (parser->lexer);
18747     }
18748   parser->translate_strings_p = save_translate_strings_p;
18749
18750   /* We built up the list in reverse order.  */
18751   return nreverse (attribute_list);
18752 }
18753
18754 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
18755    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
18756    current value of the PEDANTIC flag, regardless of whether or not
18757    the `__extension__' keyword is present.  The caller is responsible
18758    for restoring the value of the PEDANTIC flag.  */
18759
18760 static bool
18761 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18762 {
18763   /* Save the old value of the PEDANTIC flag.  */
18764   *saved_pedantic = pedantic;
18765
18766   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18767     {
18768       /* Consume the `__extension__' token.  */
18769       cp_lexer_consume_token (parser->lexer);
18770       /* We're not being pedantic while the `__extension__' keyword is
18771          in effect.  */
18772       pedantic = 0;
18773
18774       return true;
18775     }
18776
18777   return false;
18778 }
18779
18780 /* Parse a label declaration.
18781
18782    label-declaration:
18783      __label__ label-declarator-seq ;
18784
18785    label-declarator-seq:
18786      identifier , label-declarator-seq
18787      identifier  */
18788
18789 static void
18790 cp_parser_label_declaration (cp_parser* parser)
18791 {
18792   /* Look for the `__label__' keyword.  */
18793   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18794
18795   while (true)
18796     {
18797       tree identifier;
18798
18799       /* Look for an identifier.  */
18800       identifier = cp_parser_identifier (parser);
18801       /* If we failed, stop.  */
18802       if (identifier == error_mark_node)
18803         break;
18804       /* Declare it as a label.  */
18805       finish_label_decl (identifier);
18806       /* If the next token is a `;', stop.  */
18807       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18808         break;
18809       /* Look for the `,' separating the label declarations.  */
18810       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18811     }
18812
18813   /* Look for the final `;'.  */
18814   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18815 }
18816
18817 /* Support Functions */
18818
18819 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18820    NAME should have one of the representations used for an
18821    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18822    is returned.  If PARSER->SCOPE is a dependent type, then a
18823    SCOPE_REF is returned.
18824
18825    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18826    returned; the name was already resolved when the TEMPLATE_ID_EXPR
18827    was formed.  Abstractly, such entities should not be passed to this
18828    function, because they do not need to be looked up, but it is
18829    simpler to check for this special case here, rather than at the
18830    call-sites.
18831
18832    In cases not explicitly covered above, this function returns a
18833    DECL, OVERLOAD, or baselink representing the result of the lookup.
18834    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18835    is returned.
18836
18837    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18838    (e.g., "struct") that was used.  In that case bindings that do not
18839    refer to types are ignored.
18840
18841    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18842    ignored.
18843
18844    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18845    are ignored.
18846
18847    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18848    types.
18849
18850    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18851    TREE_LIST of candidates if name-lookup results in an ambiguity, and
18852    NULL_TREE otherwise.  */
18853
18854 static tree
18855 cp_parser_lookup_name (cp_parser *parser, tree name,
18856                        enum tag_types tag_type,
18857                        bool is_template,
18858                        bool is_namespace,
18859                        bool check_dependency,
18860                        tree *ambiguous_decls,
18861                        location_t name_location)
18862 {
18863   int flags = 0;
18864   tree decl;
18865   tree object_type = parser->context->object_type;
18866
18867   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18868     flags |= LOOKUP_COMPLAIN;
18869
18870   /* Assume that the lookup will be unambiguous.  */
18871   if (ambiguous_decls)
18872     *ambiguous_decls = NULL_TREE;
18873
18874   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18875      no longer valid.  Note that if we are parsing tentatively, and
18876      the parse fails, OBJECT_TYPE will be automatically restored.  */
18877   parser->context->object_type = NULL_TREE;
18878
18879   if (name == error_mark_node)
18880     return error_mark_node;
18881
18882   /* A template-id has already been resolved; there is no lookup to
18883      do.  */
18884   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18885     return name;
18886   if (BASELINK_P (name))
18887     {
18888       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18889                   == TEMPLATE_ID_EXPR);
18890       return name;
18891     }
18892
18893   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
18894      it should already have been checked to make sure that the name
18895      used matches the type being destroyed.  */
18896   if (TREE_CODE (name) == BIT_NOT_EXPR)
18897     {
18898       tree type;
18899
18900       /* Figure out to which type this destructor applies.  */
18901       if (parser->scope)
18902         type = parser->scope;
18903       else if (object_type)
18904         type = object_type;
18905       else
18906         type = current_class_type;
18907       /* If that's not a class type, there is no destructor.  */
18908       if (!type || !CLASS_TYPE_P (type))
18909         return error_mark_node;
18910       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18911         lazily_declare_fn (sfk_destructor, type);
18912       if (!CLASSTYPE_DESTRUCTORS (type))
18913           return error_mark_node;
18914       /* If it was a class type, return the destructor.  */
18915       return CLASSTYPE_DESTRUCTORS (type);
18916     }
18917
18918   /* By this point, the NAME should be an ordinary identifier.  If
18919      the id-expression was a qualified name, the qualifying scope is
18920      stored in PARSER->SCOPE at this point.  */
18921   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18922
18923   /* Perform the lookup.  */
18924   if (parser->scope)
18925     {
18926       bool dependent_p;
18927
18928       if (parser->scope == error_mark_node)
18929         return error_mark_node;
18930
18931       /* If the SCOPE is dependent, the lookup must be deferred until
18932          the template is instantiated -- unless we are explicitly
18933          looking up names in uninstantiated templates.  Even then, we
18934          cannot look up the name if the scope is not a class type; it
18935          might, for example, be a template type parameter.  */
18936       dependent_p = (TYPE_P (parser->scope)
18937                      && dependent_scope_p (parser->scope));
18938       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18939           && dependent_p)
18940         /* Defer lookup.  */
18941         decl = error_mark_node;
18942       else
18943         {
18944           tree pushed_scope = NULL_TREE;
18945
18946           /* If PARSER->SCOPE is a dependent type, then it must be a
18947              class type, and we must not be checking dependencies;
18948              otherwise, we would have processed this lookup above.  So
18949              that PARSER->SCOPE is not considered a dependent base by
18950              lookup_member, we must enter the scope here.  */
18951           if (dependent_p)
18952             pushed_scope = push_scope (parser->scope);
18953
18954           /* If the PARSER->SCOPE is a template specialization, it
18955              may be instantiated during name lookup.  In that case,
18956              errors may be issued.  Even if we rollback the current
18957              tentative parse, those errors are valid.  */
18958           decl = lookup_qualified_name (parser->scope, name,
18959                                         tag_type != none_type,
18960                                         /*complain=*/true);
18961
18962           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18963              lookup result and the nested-name-specifier nominates a class C:
18964                * if the name specified after the nested-name-specifier, when
18965                looked up in C, is the injected-class-name of C (Clause 9), or
18966                * if the name specified after the nested-name-specifier is the
18967                same as the identifier or the simple-template-id's template-
18968                name in the last component of the nested-name-specifier,
18969              the name is instead considered to name the constructor of
18970              class C. [ Note: for example, the constructor is not an
18971              acceptable lookup result in an elaborated-type-specifier so
18972              the constructor would not be used in place of the
18973              injected-class-name. --end note ] Such a constructor name
18974              shall be used only in the declarator-id of a declaration that
18975              names a constructor or in a using-declaration.  */
18976           if (tag_type == none_type
18977               && DECL_SELF_REFERENCE_P (decl)
18978               && same_type_p (DECL_CONTEXT (decl), parser->scope))
18979             decl = lookup_qualified_name (parser->scope, ctor_identifier,
18980                                           tag_type != none_type,
18981                                           /*complain=*/true);
18982
18983           /* If we have a single function from a using decl, pull it out.  */
18984           if (TREE_CODE (decl) == OVERLOAD
18985               && !really_overloaded_fn (decl))
18986             decl = OVL_FUNCTION (decl);
18987
18988           if (pushed_scope)
18989             pop_scope (pushed_scope);
18990         }
18991
18992       /* If the scope is a dependent type and either we deferred lookup or
18993          we did lookup but didn't find the name, rememeber the name.  */
18994       if (decl == error_mark_node && TYPE_P (parser->scope)
18995           && dependent_type_p (parser->scope))
18996         {
18997           if (tag_type)
18998             {
18999               tree type;
19000
19001               /* The resolution to Core Issue 180 says that `struct
19002                  A::B' should be considered a type-name, even if `A'
19003                  is dependent.  */
19004               type = make_typename_type (parser->scope, name, tag_type,
19005                                          /*complain=*/tf_error);
19006               decl = TYPE_NAME (type);
19007             }
19008           else if (is_template
19009                    && (cp_parser_next_token_ends_template_argument_p (parser)
19010                        || cp_lexer_next_token_is (parser->lexer,
19011                                                   CPP_CLOSE_PAREN)))
19012             decl = make_unbound_class_template (parser->scope,
19013                                                 name, NULL_TREE,
19014                                                 /*complain=*/tf_error);
19015           else
19016             decl = build_qualified_name (/*type=*/NULL_TREE,
19017                                          parser->scope, name,
19018                                          is_template);
19019         }
19020       parser->qualifying_scope = parser->scope;
19021       parser->object_scope = NULL_TREE;
19022     }
19023   else if (object_type)
19024     {
19025       tree object_decl = NULL_TREE;
19026       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19027          OBJECT_TYPE is not a class.  */
19028       if (CLASS_TYPE_P (object_type))
19029         /* If the OBJECT_TYPE is a template specialization, it may
19030            be instantiated during name lookup.  In that case, errors
19031            may be issued.  Even if we rollback the current tentative
19032            parse, those errors are valid.  */
19033         object_decl = lookup_member (object_type,
19034                                      name,
19035                                      /*protect=*/0,
19036                                      tag_type != none_type);
19037       /* Look it up in the enclosing context, too.  */
19038       decl = lookup_name_real (name, tag_type != none_type,
19039                                /*nonclass=*/0,
19040                                /*block_p=*/true, is_namespace, flags);
19041       parser->object_scope = object_type;
19042       parser->qualifying_scope = NULL_TREE;
19043       if (object_decl)
19044         decl = object_decl;
19045     }
19046   else
19047     {
19048       decl = lookup_name_real (name, tag_type != none_type,
19049                                /*nonclass=*/0,
19050                                /*block_p=*/true, is_namespace, flags);
19051       parser->qualifying_scope = NULL_TREE;
19052       parser->object_scope = NULL_TREE;
19053     }
19054
19055   /* If the lookup failed, let our caller know.  */
19056   if (!decl || decl == error_mark_node)
19057     return error_mark_node;
19058
19059   /* Pull out the template from an injected-class-name (or multiple).  */
19060   if (is_template)
19061     decl = maybe_get_template_decl_from_type_decl (decl);
19062
19063   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19064   if (TREE_CODE (decl) == TREE_LIST)
19065     {
19066       if (ambiguous_decls)
19067         *ambiguous_decls = decl;
19068       /* The error message we have to print is too complicated for
19069          cp_parser_error, so we incorporate its actions directly.  */
19070       if (!cp_parser_simulate_error (parser))
19071         {
19072           error_at (name_location, "reference to %qD is ambiguous",
19073                     name);
19074           print_candidates (decl);
19075         }
19076       return error_mark_node;
19077     }
19078
19079   gcc_assert (DECL_P (decl)
19080               || TREE_CODE (decl) == OVERLOAD
19081               || TREE_CODE (decl) == SCOPE_REF
19082               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19083               || BASELINK_P (decl));
19084
19085   /* If we have resolved the name of a member declaration, check to
19086      see if the declaration is accessible.  When the name resolves to
19087      set of overloaded functions, accessibility is checked when
19088      overload resolution is done.
19089
19090      During an explicit instantiation, access is not checked at all,
19091      as per [temp.explicit].  */
19092   if (DECL_P (decl))
19093     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19094
19095   return decl;
19096 }
19097
19098 /* Like cp_parser_lookup_name, but for use in the typical case where
19099    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19100    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19101
19102 static tree
19103 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19104 {
19105   return cp_parser_lookup_name (parser, name,
19106                                 none_type,
19107                                 /*is_template=*/false,
19108                                 /*is_namespace=*/false,
19109                                 /*check_dependency=*/true,
19110                                 /*ambiguous_decls=*/NULL,
19111                                 location);
19112 }
19113
19114 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19115    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19116    true, the DECL indicates the class being defined in a class-head,
19117    or declared in an elaborated-type-specifier.
19118
19119    Otherwise, return DECL.  */
19120
19121 static tree
19122 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19123 {
19124   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19125      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19126
19127        struct A {
19128          template <typename T> struct B;
19129        };
19130
19131        template <typename T> struct A::B {};
19132
19133      Similarly, in an elaborated-type-specifier:
19134
19135        namespace N { struct X{}; }
19136
19137        struct A {
19138          template <typename T> friend struct N::X;
19139        };
19140
19141      However, if the DECL refers to a class type, and we are in
19142      the scope of the class, then the name lookup automatically
19143      finds the TYPE_DECL created by build_self_reference rather
19144      than a TEMPLATE_DECL.  For example, in:
19145
19146        template <class T> struct S {
19147          S s;
19148        };
19149
19150      there is no need to handle such case.  */
19151
19152   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19153     return DECL_TEMPLATE_RESULT (decl);
19154
19155   return decl;
19156 }
19157
19158 /* If too many, or too few, template-parameter lists apply to the
19159    declarator, issue an error message.  Returns TRUE if all went well,
19160    and FALSE otherwise.  */
19161
19162 static bool
19163 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19164                                                 cp_declarator *declarator,
19165                                                 location_t declarator_location)
19166 {
19167   unsigned num_templates;
19168
19169   /* We haven't seen any classes that involve template parameters yet.  */
19170   num_templates = 0;
19171
19172   switch (declarator->kind)
19173     {
19174     case cdk_id:
19175       if (declarator->u.id.qualifying_scope)
19176         {
19177           tree scope;
19178
19179           scope = declarator->u.id.qualifying_scope;
19180
19181           while (scope && CLASS_TYPE_P (scope))
19182             {
19183               /* You're supposed to have one `template <...>'
19184                  for every template class, but you don't need one
19185                  for a full specialization.  For example:
19186
19187                  template <class T> struct S{};
19188                  template <> struct S<int> { void f(); };
19189                  void S<int>::f () {}
19190
19191                  is correct; there shouldn't be a `template <>' for
19192                  the definition of `S<int>::f'.  */
19193               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19194                 /* If SCOPE does not have template information of any
19195                    kind, then it is not a template, nor is it nested
19196                    within a template.  */
19197                 break;
19198               if (explicit_class_specialization_p (scope))
19199                 break;
19200               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19201                 ++num_templates;
19202
19203               scope = TYPE_CONTEXT (scope);
19204             }
19205         }
19206       else if (TREE_CODE (declarator->u.id.unqualified_name)
19207                == TEMPLATE_ID_EXPR)
19208         /* If the DECLARATOR has the form `X<y>' then it uses one
19209            additional level of template parameters.  */
19210         ++num_templates;
19211
19212       return cp_parser_check_template_parameters 
19213         (parser, num_templates, declarator_location, declarator);
19214
19215
19216     case cdk_function:
19217     case cdk_array:
19218     case cdk_pointer:
19219     case cdk_reference:
19220     case cdk_ptrmem:
19221       return (cp_parser_check_declarator_template_parameters
19222               (parser, declarator->declarator, declarator_location));
19223
19224     case cdk_error:
19225       return true;
19226
19227     default:
19228       gcc_unreachable ();
19229     }
19230   return false;
19231 }
19232
19233 /* NUM_TEMPLATES were used in the current declaration.  If that is
19234    invalid, return FALSE and issue an error messages.  Otherwise,
19235    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19236    declarator and we can print more accurate diagnostics.  */
19237
19238 static bool
19239 cp_parser_check_template_parameters (cp_parser* parser,
19240                                      unsigned num_templates,
19241                                      location_t location,
19242                                      cp_declarator *declarator)
19243 {
19244   /* If there are the same number of template classes and parameter
19245      lists, that's OK.  */
19246   if (parser->num_template_parameter_lists == num_templates)
19247     return true;
19248   /* If there are more, but only one more, then we are referring to a
19249      member template.  That's OK too.  */
19250   if (parser->num_template_parameter_lists == num_templates + 1)
19251     return true;
19252   /* If there are more template classes than parameter lists, we have
19253      something like:
19254
19255        template <class T> void S<T>::R<T>::f ();  */
19256   if (parser->num_template_parameter_lists < num_templates)
19257     {
19258       if (declarator && !current_function_decl)
19259         error_at (location, "specializing member %<%T::%E%> "
19260                   "requires %<template<>%> syntax", 
19261                   declarator->u.id.qualifying_scope,
19262                   declarator->u.id.unqualified_name);
19263       else if (declarator)
19264         error_at (location, "invalid declaration of %<%T::%E%>",
19265                   declarator->u.id.qualifying_scope,
19266                   declarator->u.id.unqualified_name);
19267       else 
19268         error_at (location, "too few template-parameter-lists");
19269       return false;
19270     }
19271   /* Otherwise, there are too many template parameter lists.  We have
19272      something like:
19273
19274      template <class T> template <class U> void S::f();  */
19275   error_at (location, "too many template-parameter-lists");
19276   return false;
19277 }
19278
19279 /* Parse an optional `::' token indicating that the following name is
19280    from the global namespace.  If so, PARSER->SCOPE is set to the
19281    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19282    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19283    Returns the new value of PARSER->SCOPE, if the `::' token is
19284    present, and NULL_TREE otherwise.  */
19285
19286 static tree
19287 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19288 {
19289   cp_token *token;
19290
19291   /* Peek at the next token.  */
19292   token = cp_lexer_peek_token (parser->lexer);
19293   /* If we're looking at a `::' token then we're starting from the
19294      global namespace, not our current location.  */
19295   if (token->type == CPP_SCOPE)
19296     {
19297       /* Consume the `::' token.  */
19298       cp_lexer_consume_token (parser->lexer);
19299       /* Set the SCOPE so that we know where to start the lookup.  */
19300       parser->scope = global_namespace;
19301       parser->qualifying_scope = global_namespace;
19302       parser->object_scope = NULL_TREE;
19303
19304       return parser->scope;
19305     }
19306   else if (!current_scope_valid_p)
19307     {
19308       parser->scope = NULL_TREE;
19309       parser->qualifying_scope = NULL_TREE;
19310       parser->object_scope = NULL_TREE;
19311     }
19312
19313   return NULL_TREE;
19314 }
19315
19316 /* Returns TRUE if the upcoming token sequence is the start of a
19317    constructor declarator.  If FRIEND_P is true, the declarator is
19318    preceded by the `friend' specifier.  */
19319
19320 static bool
19321 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19322 {
19323   bool constructor_p;
19324   tree nested_name_specifier;
19325   cp_token *next_token;
19326
19327   /* The common case is that this is not a constructor declarator, so
19328      try to avoid doing lots of work if at all possible.  It's not
19329      valid declare a constructor at function scope.  */
19330   if (parser->in_function_body)
19331     return false;
19332   /* And only certain tokens can begin a constructor declarator.  */
19333   next_token = cp_lexer_peek_token (parser->lexer);
19334   if (next_token->type != CPP_NAME
19335       && next_token->type != CPP_SCOPE
19336       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19337       && next_token->type != CPP_TEMPLATE_ID)
19338     return false;
19339
19340   /* Parse tentatively; we are going to roll back all of the tokens
19341      consumed here.  */
19342   cp_parser_parse_tentatively (parser);
19343   /* Assume that we are looking at a constructor declarator.  */
19344   constructor_p = true;
19345
19346   /* Look for the optional `::' operator.  */
19347   cp_parser_global_scope_opt (parser,
19348                               /*current_scope_valid_p=*/false);
19349   /* Look for the nested-name-specifier.  */
19350   nested_name_specifier
19351     = (cp_parser_nested_name_specifier_opt (parser,
19352                                             /*typename_keyword_p=*/false,
19353                                             /*check_dependency_p=*/false,
19354                                             /*type_p=*/false,
19355                                             /*is_declaration=*/false));
19356   /* Outside of a class-specifier, there must be a
19357      nested-name-specifier.  */
19358   if (!nested_name_specifier &&
19359       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19360        || friend_p))
19361     constructor_p = false;
19362   else if (nested_name_specifier == error_mark_node)
19363     constructor_p = false;
19364
19365   /* If we have a class scope, this is easy; DR 147 says that S::S always
19366      names the constructor, and no other qualified name could.  */
19367   if (constructor_p && nested_name_specifier
19368       && TYPE_P (nested_name_specifier))
19369     {
19370       tree id = cp_parser_unqualified_id (parser,
19371                                           /*template_keyword_p=*/false,
19372                                           /*check_dependency_p=*/false,
19373                                           /*declarator_p=*/true,
19374                                           /*optional_p=*/false);
19375       if (is_overloaded_fn (id))
19376         id = DECL_NAME (get_first_fn (id));
19377       if (!constructor_name_p (id, nested_name_specifier))
19378         constructor_p = false;
19379     }
19380   /* If we still think that this might be a constructor-declarator,
19381      look for a class-name.  */
19382   else if (constructor_p)
19383     {
19384       /* If we have:
19385
19386            template <typename T> struct S {
19387              S();
19388            };
19389
19390          we must recognize that the nested `S' names a class.  */
19391       tree type_decl;
19392       type_decl = cp_parser_class_name (parser,
19393                                         /*typename_keyword_p=*/false,
19394                                         /*template_keyword_p=*/false,
19395                                         none_type,
19396                                         /*check_dependency_p=*/false,
19397                                         /*class_head_p=*/false,
19398                                         /*is_declaration=*/false);
19399       /* If there was no class-name, then this is not a constructor.  */
19400       constructor_p = !cp_parser_error_occurred (parser);
19401
19402       /* If we're still considering a constructor, we have to see a `(',
19403          to begin the parameter-declaration-clause, followed by either a
19404          `)', an `...', or a decl-specifier.  We need to check for a
19405          type-specifier to avoid being fooled into thinking that:
19406
19407            S (f) (int);
19408
19409          is a constructor.  (It is actually a function named `f' that
19410          takes one parameter (of type `int') and returns a value of type
19411          `S'.  */
19412       if (constructor_p
19413           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19414         constructor_p = false;
19415
19416       if (constructor_p
19417           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19418           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19419           /* A parameter declaration begins with a decl-specifier,
19420              which is either the "attribute" keyword, a storage class
19421              specifier, or (usually) a type-specifier.  */
19422           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19423         {
19424           tree type;
19425           tree pushed_scope = NULL_TREE;
19426           unsigned saved_num_template_parameter_lists;
19427
19428           /* Names appearing in the type-specifier should be looked up
19429              in the scope of the class.  */
19430           if (current_class_type)
19431             type = NULL_TREE;
19432           else
19433             {
19434               type = TREE_TYPE (type_decl);
19435               if (TREE_CODE (type) == TYPENAME_TYPE)
19436                 {
19437                   type = resolve_typename_type (type,
19438                                                 /*only_current_p=*/false);
19439                   if (TREE_CODE (type) == TYPENAME_TYPE)
19440                     {
19441                       cp_parser_abort_tentative_parse (parser);
19442                       return false;
19443                     }
19444                 }
19445               pushed_scope = push_scope (type);
19446             }
19447
19448           /* Inside the constructor parameter list, surrounding
19449              template-parameter-lists do not apply.  */
19450           saved_num_template_parameter_lists
19451             = parser->num_template_parameter_lists;
19452           parser->num_template_parameter_lists = 0;
19453
19454           /* Look for the type-specifier.  */
19455           cp_parser_type_specifier (parser,
19456                                     CP_PARSER_FLAGS_NONE,
19457                                     /*decl_specs=*/NULL,
19458                                     /*is_declarator=*/true,
19459                                     /*declares_class_or_enum=*/NULL,
19460                                     /*is_cv_qualifier=*/NULL);
19461
19462           parser->num_template_parameter_lists
19463             = saved_num_template_parameter_lists;
19464
19465           /* Leave the scope of the class.  */
19466           if (pushed_scope)
19467             pop_scope (pushed_scope);
19468
19469           constructor_p = !cp_parser_error_occurred (parser);
19470         }
19471     }
19472
19473   /* We did not really want to consume any tokens.  */
19474   cp_parser_abort_tentative_parse (parser);
19475
19476   return constructor_p;
19477 }
19478
19479 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19480    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19481    they must be performed once we are in the scope of the function.
19482
19483    Returns the function defined.  */
19484
19485 static tree
19486 cp_parser_function_definition_from_specifiers_and_declarator
19487   (cp_parser* parser,
19488    cp_decl_specifier_seq *decl_specifiers,
19489    tree attributes,
19490    const cp_declarator *declarator)
19491 {
19492   tree fn;
19493   bool success_p;
19494
19495   /* Begin the function-definition.  */
19496   success_p = start_function (decl_specifiers, declarator, attributes);
19497
19498   /* The things we're about to see are not directly qualified by any
19499      template headers we've seen thus far.  */
19500   reset_specialization ();
19501
19502   /* If there were names looked up in the decl-specifier-seq that we
19503      did not check, check them now.  We must wait until we are in the
19504      scope of the function to perform the checks, since the function
19505      might be a friend.  */
19506   perform_deferred_access_checks ();
19507
19508   if (!success_p)
19509     {
19510       /* Skip the entire function.  */
19511       cp_parser_skip_to_end_of_block_or_statement (parser);
19512       fn = error_mark_node;
19513     }
19514   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19515     {
19516       /* Seen already, skip it.  An error message has already been output.  */
19517       cp_parser_skip_to_end_of_block_or_statement (parser);
19518       fn = current_function_decl;
19519       current_function_decl = NULL_TREE;
19520       /* If this is a function from a class, pop the nested class.  */
19521       if (current_class_name)
19522         pop_nested_class ();
19523     }
19524   else
19525     fn = cp_parser_function_definition_after_declarator (parser,
19526                                                          /*inline_p=*/false);
19527
19528   return fn;
19529 }
19530
19531 /* Parse the part of a function-definition that follows the
19532    declarator.  INLINE_P is TRUE iff this function is an inline
19533    function defined within a class-specifier.
19534
19535    Returns the function defined.  */
19536
19537 static tree
19538 cp_parser_function_definition_after_declarator (cp_parser* parser,
19539                                                 bool inline_p)
19540 {
19541   tree fn;
19542   bool ctor_initializer_p = false;
19543   bool saved_in_unbraced_linkage_specification_p;
19544   bool saved_in_function_body;
19545   unsigned saved_num_template_parameter_lists;
19546   cp_token *token;
19547
19548   saved_in_function_body = parser->in_function_body;
19549   parser->in_function_body = true;
19550   /* If the next token is `return', then the code may be trying to
19551      make use of the "named return value" extension that G++ used to
19552      support.  */
19553   token = cp_lexer_peek_token (parser->lexer);
19554   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19555     {
19556       /* Consume the `return' keyword.  */
19557       cp_lexer_consume_token (parser->lexer);
19558       /* Look for the identifier that indicates what value is to be
19559          returned.  */
19560       cp_parser_identifier (parser);
19561       /* Issue an error message.  */
19562       error_at (token->location,
19563                 "named return values are no longer supported");
19564       /* Skip tokens until we reach the start of the function body.  */
19565       while (true)
19566         {
19567           cp_token *token = cp_lexer_peek_token (parser->lexer);
19568           if (token->type == CPP_OPEN_BRACE
19569               || token->type == CPP_EOF
19570               || token->type == CPP_PRAGMA_EOL)
19571             break;
19572           cp_lexer_consume_token (parser->lexer);
19573         }
19574     }
19575   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19576      anything declared inside `f'.  */
19577   saved_in_unbraced_linkage_specification_p
19578     = parser->in_unbraced_linkage_specification_p;
19579   parser->in_unbraced_linkage_specification_p = false;
19580   /* Inside the function, surrounding template-parameter-lists do not
19581      apply.  */
19582   saved_num_template_parameter_lists
19583     = parser->num_template_parameter_lists;
19584   parser->num_template_parameter_lists = 0;
19585
19586   start_lambda_scope (current_function_decl);
19587
19588   /* If the next token is `try', then we are looking at a
19589      function-try-block.  */
19590   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19591     ctor_initializer_p = cp_parser_function_try_block (parser);
19592   /* A function-try-block includes the function-body, so we only do
19593      this next part if we're not processing a function-try-block.  */
19594   else
19595     ctor_initializer_p
19596       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19597
19598   finish_lambda_scope ();
19599
19600   /* Finish the function.  */
19601   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19602                         (inline_p ? 2 : 0));
19603   /* Generate code for it, if necessary.  */
19604   expand_or_defer_fn (fn);
19605   /* Restore the saved values.  */
19606   parser->in_unbraced_linkage_specification_p
19607     = saved_in_unbraced_linkage_specification_p;
19608   parser->num_template_parameter_lists
19609     = saved_num_template_parameter_lists;
19610   parser->in_function_body = saved_in_function_body;
19611
19612   return fn;
19613 }
19614
19615 /* Parse a template-declaration, assuming that the `export' (and
19616    `extern') keywords, if present, has already been scanned.  MEMBER_P
19617    is as for cp_parser_template_declaration.  */
19618
19619 static void
19620 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19621 {
19622   tree decl = NULL_TREE;
19623   VEC (deferred_access_check,gc) *checks;
19624   tree parameter_list;
19625   bool friend_p = false;
19626   bool need_lang_pop;
19627   cp_token *token;
19628
19629   /* Look for the `template' keyword.  */
19630   token = cp_lexer_peek_token (parser->lexer);
19631   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19632     return;
19633
19634   /* And the `<'.  */
19635   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19636     return;
19637   if (at_class_scope_p () && current_function_decl)
19638     {
19639       /* 14.5.2.2 [temp.mem]
19640
19641          A local class shall not have member templates.  */
19642       error_at (token->location,
19643                 "invalid declaration of member template in local class");
19644       cp_parser_skip_to_end_of_block_or_statement (parser);
19645       return;
19646     }
19647   /* [temp]
19648
19649      A template ... shall not have C linkage.  */
19650   if (current_lang_name == lang_name_c)
19651     {
19652       error_at (token->location, "template with C linkage");
19653       /* Give it C++ linkage to avoid confusing other parts of the
19654          front end.  */
19655       push_lang_context (lang_name_cplusplus);
19656       need_lang_pop = true;
19657     }
19658   else
19659     need_lang_pop = false;
19660
19661   /* We cannot perform access checks on the template parameter
19662      declarations until we know what is being declared, just as we
19663      cannot check the decl-specifier list.  */
19664   push_deferring_access_checks (dk_deferred);
19665
19666   /* If the next token is `>', then we have an invalid
19667      specialization.  Rather than complain about an invalid template
19668      parameter, issue an error message here.  */
19669   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19670     {
19671       cp_parser_error (parser, "invalid explicit specialization");
19672       begin_specialization ();
19673       parameter_list = NULL_TREE;
19674     }
19675   else
19676     /* Parse the template parameters.  */
19677     parameter_list = cp_parser_template_parameter_list (parser);
19678
19679   /* Get the deferred access checks from the parameter list.  These
19680      will be checked once we know what is being declared, as for a
19681      member template the checks must be performed in the scope of the
19682      class containing the member.  */
19683   checks = get_deferred_access_checks ();
19684
19685   /* Look for the `>'.  */
19686   cp_parser_skip_to_end_of_template_parameter_list (parser);
19687   /* We just processed one more parameter list.  */
19688   ++parser->num_template_parameter_lists;
19689   /* If the next token is `template', there are more template
19690      parameters.  */
19691   if (cp_lexer_next_token_is_keyword (parser->lexer,
19692                                       RID_TEMPLATE))
19693     cp_parser_template_declaration_after_export (parser, member_p);
19694   else
19695     {
19696       /* There are no access checks when parsing a template, as we do not
19697          know if a specialization will be a friend.  */
19698       push_deferring_access_checks (dk_no_check);
19699       token = cp_lexer_peek_token (parser->lexer);
19700       decl = cp_parser_single_declaration (parser,
19701                                            checks,
19702                                            member_p,
19703                                            /*explicit_specialization_p=*/false,
19704                                            &friend_p);
19705       pop_deferring_access_checks ();
19706
19707       /* If this is a member template declaration, let the front
19708          end know.  */
19709       if (member_p && !friend_p && decl)
19710         {
19711           if (TREE_CODE (decl) == TYPE_DECL)
19712             cp_parser_check_access_in_redeclaration (decl, token->location);
19713
19714           decl = finish_member_template_decl (decl);
19715         }
19716       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19717         make_friend_class (current_class_type, TREE_TYPE (decl),
19718                            /*complain=*/true);
19719     }
19720   /* We are done with the current parameter list.  */
19721   --parser->num_template_parameter_lists;
19722
19723   pop_deferring_access_checks ();
19724
19725   /* Finish up.  */
19726   finish_template_decl (parameter_list);
19727
19728   /* Register member declarations.  */
19729   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19730     finish_member_declaration (decl);
19731   /* For the erroneous case of a template with C linkage, we pushed an
19732      implicit C++ linkage scope; exit that scope now.  */
19733   if (need_lang_pop)
19734     pop_lang_context ();
19735   /* If DECL is a function template, we must return to parse it later.
19736      (Even though there is no definition, there might be default
19737      arguments that need handling.)  */
19738   if (member_p && decl
19739       && (TREE_CODE (decl) == FUNCTION_DECL
19740           || DECL_FUNCTION_TEMPLATE_P (decl)))
19741     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19742 }
19743
19744 /* Perform the deferred access checks from a template-parameter-list.
19745    CHECKS is a TREE_LIST of access checks, as returned by
19746    get_deferred_access_checks.  */
19747
19748 static void
19749 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19750 {
19751   ++processing_template_parmlist;
19752   perform_access_checks (checks);
19753   --processing_template_parmlist;
19754 }
19755
19756 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19757    `function-definition' sequence.  MEMBER_P is true, this declaration
19758    appears in a class scope.
19759
19760    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
19761    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
19762
19763 static tree
19764 cp_parser_single_declaration (cp_parser* parser,
19765                               VEC (deferred_access_check,gc)* checks,
19766                               bool member_p,
19767                               bool explicit_specialization_p,
19768                               bool* friend_p)
19769 {
19770   int declares_class_or_enum;
19771   tree decl = NULL_TREE;
19772   cp_decl_specifier_seq decl_specifiers;
19773   bool function_definition_p = false;
19774   cp_token *decl_spec_token_start;
19775
19776   /* This function is only used when processing a template
19777      declaration.  */
19778   gcc_assert (innermost_scope_kind () == sk_template_parms
19779               || innermost_scope_kind () == sk_template_spec);
19780
19781   /* Defer access checks until we know what is being declared.  */
19782   push_deferring_access_checks (dk_deferred);
19783
19784   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19785      alternative.  */
19786   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19787   cp_parser_decl_specifier_seq (parser,
19788                                 CP_PARSER_FLAGS_OPTIONAL,
19789                                 &decl_specifiers,
19790                                 &declares_class_or_enum);
19791   if (friend_p)
19792     *friend_p = cp_parser_friend_p (&decl_specifiers);
19793
19794   /* There are no template typedefs.  */
19795   if (decl_specifiers.specs[(int) ds_typedef])
19796     {
19797       error_at (decl_spec_token_start->location,
19798                 "template declaration of %<typedef%>");
19799       decl = error_mark_node;
19800     }
19801
19802   /* Gather up the access checks that occurred the
19803      decl-specifier-seq.  */
19804   stop_deferring_access_checks ();
19805
19806   /* Check for the declaration of a template class.  */
19807   if (declares_class_or_enum)
19808     {
19809       if (cp_parser_declares_only_class_p (parser))
19810         {
19811           decl = shadow_tag (&decl_specifiers);
19812
19813           /* In this case:
19814
19815                struct C {
19816                  friend template <typename T> struct A<T>::B;
19817                };
19818
19819              A<T>::B will be represented by a TYPENAME_TYPE, and
19820              therefore not recognized by shadow_tag.  */
19821           if (friend_p && *friend_p
19822               && !decl
19823               && decl_specifiers.type
19824               && TYPE_P (decl_specifiers.type))
19825             decl = decl_specifiers.type;
19826
19827           if (decl && decl != error_mark_node)
19828             decl = TYPE_NAME (decl);
19829           else
19830             decl = error_mark_node;
19831
19832           /* Perform access checks for template parameters.  */
19833           cp_parser_perform_template_parameter_access_checks (checks);
19834         }
19835     }
19836
19837   /* Complain about missing 'typename' or other invalid type names.  */
19838   if (!decl_specifiers.any_type_specifiers_p)
19839     cp_parser_parse_and_diagnose_invalid_type_name (parser);
19840
19841   /* If it's not a template class, try for a template function.  If
19842      the next token is a `;', then this declaration does not declare
19843      anything.  But, if there were errors in the decl-specifiers, then
19844      the error might well have come from an attempted class-specifier.
19845      In that case, there's no need to warn about a missing declarator.  */
19846   if (!decl
19847       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19848           || decl_specifiers.type != error_mark_node))
19849     {
19850       decl = cp_parser_init_declarator (parser,
19851                                         &decl_specifiers,
19852                                         checks,
19853                                         /*function_definition_allowed_p=*/true,
19854                                         member_p,
19855                                         declares_class_or_enum,
19856                                         &function_definition_p);
19857
19858     /* 7.1.1-1 [dcl.stc]
19859
19860        A storage-class-specifier shall not be specified in an explicit
19861        specialization...  */
19862     if (decl
19863         && explicit_specialization_p
19864         && decl_specifiers.storage_class != sc_none)
19865       {
19866         error_at (decl_spec_token_start->location,
19867                   "explicit template specialization cannot have a storage class");
19868         decl = error_mark_node;
19869       }
19870     }
19871
19872   pop_deferring_access_checks ();
19873
19874   /* Clear any current qualification; whatever comes next is the start
19875      of something new.  */
19876   parser->scope = NULL_TREE;
19877   parser->qualifying_scope = NULL_TREE;
19878   parser->object_scope = NULL_TREE;
19879   /* Look for a trailing `;' after the declaration.  */
19880   if (!function_definition_p
19881       && (decl == error_mark_node
19882           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19883     cp_parser_skip_to_end_of_block_or_statement (parser);
19884
19885   return decl;
19886 }
19887
19888 /* Parse a cast-expression that is not the operand of a unary "&".  */
19889
19890 static tree
19891 cp_parser_simple_cast_expression (cp_parser *parser)
19892 {
19893   return cp_parser_cast_expression (parser, /*address_p=*/false,
19894                                     /*cast_p=*/false, NULL);
19895 }
19896
19897 /* Parse a functional cast to TYPE.  Returns an expression
19898    representing the cast.  */
19899
19900 static tree
19901 cp_parser_functional_cast (cp_parser* parser, tree type)
19902 {
19903   VEC(tree,gc) *vec;
19904   tree expression_list;
19905   tree cast;
19906   bool nonconst_p;
19907
19908   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19909     {
19910       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19911       expression_list = cp_parser_braced_list (parser, &nonconst_p);
19912       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19913       if (TREE_CODE (type) == TYPE_DECL)
19914         type = TREE_TYPE (type);
19915       return finish_compound_literal (type, expression_list);
19916     }
19917
19918
19919   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19920                                                  /*cast_p=*/true,
19921                                                  /*allow_expansion_p=*/true,
19922                                                  /*non_constant_p=*/NULL);
19923   if (vec == NULL)
19924     expression_list = error_mark_node;
19925   else
19926     {
19927       expression_list = build_tree_list_vec (vec);
19928       release_tree_vector (vec);
19929     }
19930
19931   cast = build_functional_cast (type, expression_list,
19932                                 tf_warning_or_error);
19933   /* [expr.const]/1: In an integral constant expression "only type
19934      conversions to integral or enumeration type can be used".  */
19935   if (TREE_CODE (type) == TYPE_DECL)
19936     type = TREE_TYPE (type);
19937   if (cast != error_mark_node
19938       && !cast_valid_in_integral_constant_expression_p (type)
19939       && cp_parser_non_integral_constant_expression (parser,
19940                                                      NIC_CONSTRUCTOR))
19941     return error_mark_node;
19942   return cast;
19943 }
19944
19945 /* Save the tokens that make up the body of a member function defined
19946    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
19947    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
19948    specifiers applied to the declaration.  Returns the FUNCTION_DECL
19949    for the member function.  */
19950
19951 static tree
19952 cp_parser_save_member_function_body (cp_parser* parser,
19953                                      cp_decl_specifier_seq *decl_specifiers,
19954                                      cp_declarator *declarator,
19955                                      tree attributes)
19956 {
19957   cp_token *first;
19958   cp_token *last;
19959   tree fn;
19960
19961   /* Create the FUNCTION_DECL.  */
19962   fn = grokmethod (decl_specifiers, declarator, attributes);
19963   /* If something went badly wrong, bail out now.  */
19964   if (fn == error_mark_node)
19965     {
19966       /* If there's a function-body, skip it.  */
19967       if (cp_parser_token_starts_function_definition_p
19968           (cp_lexer_peek_token (parser->lexer)))
19969         cp_parser_skip_to_end_of_block_or_statement (parser);
19970       return error_mark_node;
19971     }
19972
19973   /* Remember it, if there default args to post process.  */
19974   cp_parser_save_default_args (parser, fn);
19975
19976   /* Save away the tokens that make up the body of the
19977      function.  */
19978   first = parser->lexer->next_token;
19979   /* We can have braced-init-list mem-initializers before the fn body.  */
19980   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19981     {
19982       cp_lexer_consume_token (parser->lexer);
19983       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19984              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19985         {
19986           /* cache_group will stop after an un-nested { } pair, too.  */
19987           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19988             break;
19989
19990           /* variadic mem-inits have ... after the ')'.  */
19991           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19992             cp_lexer_consume_token (parser->lexer);
19993         }
19994     }
19995   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19996   /* Handle function try blocks.  */
19997   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19998     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19999   last = parser->lexer->next_token;
20000
20001   /* Save away the inline definition; we will process it when the
20002      class is complete.  */
20003   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20004   DECL_PENDING_INLINE_P (fn) = 1;
20005
20006   /* We need to know that this was defined in the class, so that
20007      friend templates are handled correctly.  */
20008   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20009
20010   /* Add FN to the queue of functions to be parsed later.  */
20011   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20012
20013   return fn;
20014 }
20015
20016 /* Parse a template-argument-list, as well as the trailing ">" (but
20017    not the opening ">").  See cp_parser_template_argument_list for the
20018    return value.  */
20019
20020 static tree
20021 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20022 {
20023   tree arguments;
20024   tree saved_scope;
20025   tree saved_qualifying_scope;
20026   tree saved_object_scope;
20027   bool saved_greater_than_is_operator_p;
20028   int saved_unevaluated_operand;
20029   int saved_inhibit_evaluation_warnings;
20030
20031   /* [temp.names]
20032
20033      When parsing a template-id, the first non-nested `>' is taken as
20034      the end of the template-argument-list rather than a greater-than
20035      operator.  */
20036   saved_greater_than_is_operator_p
20037     = parser->greater_than_is_operator_p;
20038   parser->greater_than_is_operator_p = false;
20039   /* Parsing the argument list may modify SCOPE, so we save it
20040      here.  */
20041   saved_scope = parser->scope;
20042   saved_qualifying_scope = parser->qualifying_scope;
20043   saved_object_scope = parser->object_scope;
20044   /* We need to evaluate the template arguments, even though this
20045      template-id may be nested within a "sizeof".  */
20046   saved_unevaluated_operand = cp_unevaluated_operand;
20047   cp_unevaluated_operand = 0;
20048   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20049   c_inhibit_evaluation_warnings = 0;
20050   /* Parse the template-argument-list itself.  */
20051   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20052       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20053     arguments = NULL_TREE;
20054   else
20055     arguments = cp_parser_template_argument_list (parser);
20056   /* Look for the `>' that ends the template-argument-list. If we find
20057      a '>>' instead, it's probably just a typo.  */
20058   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20059     {
20060       if (cxx_dialect != cxx98)
20061         {
20062           /* In C++0x, a `>>' in a template argument list or cast
20063              expression is considered to be two separate `>'
20064              tokens. So, change the current token to a `>', but don't
20065              consume it: it will be consumed later when the outer
20066              template argument list (or cast expression) is parsed.
20067              Note that this replacement of `>' for `>>' is necessary
20068              even if we are parsing tentatively: in the tentative
20069              case, after calling
20070              cp_parser_enclosed_template_argument_list we will always
20071              throw away all of the template arguments and the first
20072              closing `>', either because the template argument list
20073              was erroneous or because we are replacing those tokens
20074              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20075              not have been thrown away) is needed either to close an
20076              outer template argument list or to complete a new-style
20077              cast.  */
20078           cp_token *token = cp_lexer_peek_token (parser->lexer);
20079           token->type = CPP_GREATER;
20080         }
20081       else if (!saved_greater_than_is_operator_p)
20082         {
20083           /* If we're in a nested template argument list, the '>>' has
20084             to be a typo for '> >'. We emit the error message, but we
20085             continue parsing and we push a '>' as next token, so that
20086             the argument list will be parsed correctly.  Note that the
20087             global source location is still on the token before the
20088             '>>', so we need to say explicitly where we want it.  */
20089           cp_token *token = cp_lexer_peek_token (parser->lexer);
20090           error_at (token->location, "%<>>%> should be %<> >%> "
20091                     "within a nested template argument list");
20092
20093           token->type = CPP_GREATER;
20094         }
20095       else
20096         {
20097           /* If this is not a nested template argument list, the '>>'
20098             is a typo for '>'. Emit an error message and continue.
20099             Same deal about the token location, but here we can get it
20100             right by consuming the '>>' before issuing the diagnostic.  */
20101           cp_token *token = cp_lexer_consume_token (parser->lexer);
20102           error_at (token->location,
20103                     "spurious %<>>%>, use %<>%> to terminate "
20104                     "a template argument list");
20105         }
20106     }
20107   else
20108     cp_parser_skip_to_end_of_template_parameter_list (parser);
20109   /* The `>' token might be a greater-than operator again now.  */
20110   parser->greater_than_is_operator_p
20111     = saved_greater_than_is_operator_p;
20112   /* Restore the SAVED_SCOPE.  */
20113   parser->scope = saved_scope;
20114   parser->qualifying_scope = saved_qualifying_scope;
20115   parser->object_scope = saved_object_scope;
20116   cp_unevaluated_operand = saved_unevaluated_operand;
20117   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20118
20119   return arguments;
20120 }
20121
20122 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20123    arguments, or the body of the function have not yet been parsed,
20124    parse them now.  */
20125
20126 static void
20127 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20128 {
20129   /* If this member is a template, get the underlying
20130      FUNCTION_DECL.  */
20131   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20132     member_function = DECL_TEMPLATE_RESULT (member_function);
20133
20134   /* There should not be any class definitions in progress at this
20135      point; the bodies of members are only parsed outside of all class
20136      definitions.  */
20137   gcc_assert (parser->num_classes_being_defined == 0);
20138   /* While we're parsing the member functions we might encounter more
20139      classes.  We want to handle them right away, but we don't want
20140      them getting mixed up with functions that are currently in the
20141      queue.  */
20142   push_unparsed_function_queues (parser);
20143
20144   /* Make sure that any template parameters are in scope.  */
20145   maybe_begin_member_template_processing (member_function);
20146
20147   /* If the body of the function has not yet been parsed, parse it
20148      now.  */
20149   if (DECL_PENDING_INLINE_P (member_function))
20150     {
20151       tree function_scope;
20152       cp_token_cache *tokens;
20153
20154       /* The function is no longer pending; we are processing it.  */
20155       tokens = DECL_PENDING_INLINE_INFO (member_function);
20156       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20157       DECL_PENDING_INLINE_P (member_function) = 0;
20158
20159       /* If this is a local class, enter the scope of the containing
20160          function.  */
20161       function_scope = current_function_decl;
20162       if (function_scope)
20163         push_function_context ();
20164
20165       /* Push the body of the function onto the lexer stack.  */
20166       cp_parser_push_lexer_for_tokens (parser, tokens);
20167
20168       /* Let the front end know that we going to be defining this
20169          function.  */
20170       start_preparsed_function (member_function, NULL_TREE,
20171                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20172
20173       /* Don't do access checking if it is a templated function.  */
20174       if (processing_template_decl)
20175         push_deferring_access_checks (dk_no_check);
20176
20177       /* Now, parse the body of the function.  */
20178       cp_parser_function_definition_after_declarator (parser,
20179                                                       /*inline_p=*/true);
20180
20181       if (processing_template_decl)
20182         pop_deferring_access_checks ();
20183
20184       /* Leave the scope of the containing function.  */
20185       if (function_scope)
20186         pop_function_context ();
20187       cp_parser_pop_lexer (parser);
20188     }
20189
20190   /* Remove any template parameters from the symbol table.  */
20191   maybe_end_member_template_processing ();
20192
20193   /* Restore the queue.  */
20194   pop_unparsed_function_queues (parser);
20195 }
20196
20197 /* If DECL contains any default args, remember it on the unparsed
20198    functions queue.  */
20199
20200 static void
20201 cp_parser_save_default_args (cp_parser* parser, tree decl)
20202 {
20203   tree probe;
20204
20205   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20206        probe;
20207        probe = TREE_CHAIN (probe))
20208     if (TREE_PURPOSE (probe))
20209       {
20210         cp_default_arg_entry *entry
20211           = VEC_safe_push (cp_default_arg_entry, gc,
20212                            unparsed_funs_with_default_args, NULL);
20213         entry->class_type = current_class_type;
20214         entry->decl = decl;
20215         break;
20216       }
20217 }
20218
20219 /* FN is a FUNCTION_DECL which may contains a parameter with an
20220    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20221    assumes that the current scope is the scope in which the default
20222    argument should be processed.  */
20223
20224 static void
20225 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20226 {
20227   bool saved_local_variables_forbidden_p;
20228   tree parm, parmdecl;
20229
20230   /* While we're parsing the default args, we might (due to the
20231      statement expression extension) encounter more classes.  We want
20232      to handle them right away, but we don't want them getting mixed
20233      up with default args that are currently in the queue.  */
20234   push_unparsed_function_queues (parser);
20235
20236   /* Local variable names (and the `this' keyword) may not appear
20237      in a default argument.  */
20238   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20239   parser->local_variables_forbidden_p = true;
20240
20241   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20242          parmdecl = DECL_ARGUMENTS (fn);
20243        parm && parm != void_list_node;
20244        parm = TREE_CHAIN (parm),
20245          parmdecl = DECL_CHAIN (parmdecl))
20246     {
20247       cp_token_cache *tokens;
20248       tree default_arg = TREE_PURPOSE (parm);
20249       tree parsed_arg;
20250       VEC(tree,gc) *insts;
20251       tree copy;
20252       unsigned ix;
20253
20254       if (!default_arg)
20255         continue;
20256
20257       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20258         /* This can happen for a friend declaration for a function
20259            already declared with default arguments.  */
20260         continue;
20261
20262        /* Push the saved tokens for the default argument onto the parser's
20263           lexer stack.  */
20264       tokens = DEFARG_TOKENS (default_arg);
20265       cp_parser_push_lexer_for_tokens (parser, tokens);
20266
20267       start_lambda_scope (parmdecl);
20268
20269       /* Parse the assignment-expression.  */
20270       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20271       if (parsed_arg == error_mark_node)
20272         {
20273           cp_parser_pop_lexer (parser);
20274           continue;
20275         }
20276
20277       if (!processing_template_decl)
20278         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20279
20280       TREE_PURPOSE (parm) = parsed_arg;
20281
20282       /* Update any instantiations we've already created.  */
20283       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20284            VEC_iterate (tree, insts, ix, copy); ix++)
20285         TREE_PURPOSE (copy) = parsed_arg;
20286
20287       finish_lambda_scope ();
20288
20289       /* If the token stream has not been completely used up, then
20290          there was extra junk after the end of the default
20291          argument.  */
20292       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20293         cp_parser_error (parser, "expected %<,%>");
20294
20295       /* Revert to the main lexer.  */
20296       cp_parser_pop_lexer (parser);
20297     }
20298
20299   /* Make sure no default arg is missing.  */
20300   check_default_args (fn);
20301
20302   /* Restore the state of local_variables_forbidden_p.  */
20303   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20304
20305   /* Restore the queue.  */
20306   pop_unparsed_function_queues (parser);
20307 }
20308
20309 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20310    either a TYPE or an expression, depending on the form of the
20311    input.  The KEYWORD indicates which kind of expression we have
20312    encountered.  */
20313
20314 static tree
20315 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20316 {
20317   tree expr = NULL_TREE;
20318   const char *saved_message;
20319   char *tmp;
20320   bool saved_integral_constant_expression_p;
20321   bool saved_non_integral_constant_expression_p;
20322   bool pack_expansion_p = false;
20323
20324   /* Types cannot be defined in a `sizeof' expression.  Save away the
20325      old message.  */
20326   saved_message = parser->type_definition_forbidden_message;
20327   /* And create the new one.  */
20328   tmp = concat ("types may not be defined in %<",
20329                 IDENTIFIER_POINTER (ridpointers[keyword]),
20330                 "%> expressions", NULL);
20331   parser->type_definition_forbidden_message = tmp;
20332
20333   /* The restrictions on constant-expressions do not apply inside
20334      sizeof expressions.  */
20335   saved_integral_constant_expression_p
20336     = parser->integral_constant_expression_p;
20337   saved_non_integral_constant_expression_p
20338     = parser->non_integral_constant_expression_p;
20339   parser->integral_constant_expression_p = false;
20340
20341   /* If it's a `...', then we are computing the length of a parameter
20342      pack.  */
20343   if (keyword == RID_SIZEOF
20344       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20345     {
20346       /* Consume the `...'.  */
20347       cp_lexer_consume_token (parser->lexer);
20348       maybe_warn_variadic_templates ();
20349
20350       /* Note that this is an expansion.  */
20351       pack_expansion_p = true;
20352     }
20353
20354   /* Do not actually evaluate the expression.  */
20355   ++cp_unevaluated_operand;
20356   ++c_inhibit_evaluation_warnings;
20357   /* If it's a `(', then we might be looking at the type-id
20358      construction.  */
20359   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20360     {
20361       tree type;
20362       bool saved_in_type_id_in_expr_p;
20363
20364       /* We can't be sure yet whether we're looking at a type-id or an
20365          expression.  */
20366       cp_parser_parse_tentatively (parser);
20367       /* Consume the `('.  */
20368       cp_lexer_consume_token (parser->lexer);
20369       /* Parse the type-id.  */
20370       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20371       parser->in_type_id_in_expr_p = true;
20372       type = cp_parser_type_id (parser);
20373       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20374       /* Now, look for the trailing `)'.  */
20375       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20376       /* If all went well, then we're done.  */
20377       if (cp_parser_parse_definitely (parser))
20378         {
20379           cp_decl_specifier_seq decl_specs;
20380
20381           /* Build a trivial decl-specifier-seq.  */
20382           clear_decl_specs (&decl_specs);
20383           decl_specs.type = type;
20384
20385           /* Call grokdeclarator to figure out what type this is.  */
20386           expr = grokdeclarator (NULL,
20387                                  &decl_specs,
20388                                  TYPENAME,
20389                                  /*initialized=*/0,
20390                                  /*attrlist=*/NULL);
20391         }
20392     }
20393
20394   /* If the type-id production did not work out, then we must be
20395      looking at the unary-expression production.  */
20396   if (!expr)
20397     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20398                                        /*cast_p=*/false, NULL);
20399
20400   if (pack_expansion_p)
20401     /* Build a pack expansion. */
20402     expr = make_pack_expansion (expr);
20403
20404   /* Go back to evaluating expressions.  */
20405   --cp_unevaluated_operand;
20406   --c_inhibit_evaluation_warnings;
20407
20408   /* Free the message we created.  */
20409   free (tmp);
20410   /* And restore the old one.  */
20411   parser->type_definition_forbidden_message = saved_message;
20412   parser->integral_constant_expression_p
20413     = saved_integral_constant_expression_p;
20414   parser->non_integral_constant_expression_p
20415     = saved_non_integral_constant_expression_p;
20416
20417   return expr;
20418 }
20419
20420 /* If the current declaration has no declarator, return true.  */
20421
20422 static bool
20423 cp_parser_declares_only_class_p (cp_parser *parser)
20424 {
20425   /* If the next token is a `;' or a `,' then there is no
20426      declarator.  */
20427   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20428           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20429 }
20430
20431 /* Update the DECL_SPECS to reflect the storage class indicated by
20432    KEYWORD.  */
20433
20434 static void
20435 cp_parser_set_storage_class (cp_parser *parser,
20436                              cp_decl_specifier_seq *decl_specs,
20437                              enum rid keyword,
20438                              location_t location)
20439 {
20440   cp_storage_class storage_class;
20441
20442   if (parser->in_unbraced_linkage_specification_p)
20443     {
20444       error_at (location, "invalid use of %qD in linkage specification",
20445                 ridpointers[keyword]);
20446       return;
20447     }
20448   else if (decl_specs->storage_class != sc_none)
20449     {
20450       decl_specs->conflicting_specifiers_p = true;
20451       return;
20452     }
20453
20454   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20455       && decl_specs->specs[(int) ds_thread])
20456     {
20457       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20458       decl_specs->specs[(int) ds_thread] = 0;
20459     }
20460
20461   switch (keyword)
20462     {
20463     case RID_AUTO:
20464       storage_class = sc_auto;
20465       break;
20466     case RID_REGISTER:
20467       storage_class = sc_register;
20468       break;
20469     case RID_STATIC:
20470       storage_class = sc_static;
20471       break;
20472     case RID_EXTERN:
20473       storage_class = sc_extern;
20474       break;
20475     case RID_MUTABLE:
20476       storage_class = sc_mutable;
20477       break;
20478     default:
20479       gcc_unreachable ();
20480     }
20481   decl_specs->storage_class = storage_class;
20482
20483   /* A storage class specifier cannot be applied alongside a typedef 
20484      specifier. If there is a typedef specifier present then set 
20485      conflicting_specifiers_p which will trigger an error later
20486      on in grokdeclarator. */
20487   if (decl_specs->specs[(int)ds_typedef])
20488     decl_specs->conflicting_specifiers_p = true;
20489 }
20490
20491 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20492    is true, the type is a user-defined type; otherwise it is a
20493    built-in type specified by a keyword.  */
20494
20495 static void
20496 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20497                               tree type_spec,
20498                               location_t location,
20499                               bool user_defined_p)
20500 {
20501   decl_specs->any_specifiers_p = true;
20502
20503   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20504      (with, for example, in "typedef int wchar_t;") we remember that
20505      this is what happened.  In system headers, we ignore these
20506      declarations so that G++ can work with system headers that are not
20507      C++-safe.  */
20508   if (decl_specs->specs[(int) ds_typedef]
20509       && !user_defined_p
20510       && (type_spec == boolean_type_node
20511           || type_spec == char16_type_node
20512           || type_spec == char32_type_node
20513           || type_spec == wchar_type_node)
20514       && (decl_specs->type
20515           || decl_specs->specs[(int) ds_long]
20516           || decl_specs->specs[(int) ds_short]
20517           || decl_specs->specs[(int) ds_unsigned]
20518           || decl_specs->specs[(int) ds_signed]))
20519     {
20520       decl_specs->redefined_builtin_type = type_spec;
20521       if (!decl_specs->type)
20522         {
20523           decl_specs->type = type_spec;
20524           decl_specs->user_defined_type_p = false;
20525           decl_specs->type_location = location;
20526         }
20527     }
20528   else if (decl_specs->type)
20529     decl_specs->multiple_types_p = true;
20530   else
20531     {
20532       decl_specs->type = type_spec;
20533       decl_specs->user_defined_type_p = user_defined_p;
20534       decl_specs->redefined_builtin_type = NULL_TREE;
20535       decl_specs->type_location = location;
20536     }
20537 }
20538
20539 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20540    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20541
20542 static bool
20543 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20544 {
20545   return decl_specifiers->specs[(int) ds_friend] != 0;
20546 }
20547
20548 /* Issue an error message indicating that TOKEN_DESC was expected.
20549    If KEYWORD is true, it indicated this function is called by
20550    cp_parser_require_keword and the required token can only be
20551    a indicated keyword. */
20552
20553 static void
20554 cp_parser_required_error (cp_parser *parser,
20555                           required_token token_desc,
20556                           bool keyword)
20557 {
20558   switch (token_desc)
20559     {
20560       case RT_NEW:
20561         cp_parser_error (parser, "expected %<new%>");
20562         return;
20563       case RT_DELETE:
20564         cp_parser_error (parser, "expected %<delete%>");
20565         return;
20566       case RT_RETURN:
20567         cp_parser_error (parser, "expected %<return%>");
20568         return;
20569       case RT_WHILE:
20570         cp_parser_error (parser, "expected %<while%>");
20571         return;
20572       case RT_EXTERN:
20573         cp_parser_error (parser, "expected %<extern%>");
20574         return;
20575       case RT_STATIC_ASSERT:
20576         cp_parser_error (parser, "expected %<static_assert%>");
20577         return;
20578       case RT_DECLTYPE:
20579         cp_parser_error (parser, "expected %<decltype%>");
20580         return;
20581       case RT_OPERATOR:
20582         cp_parser_error (parser, "expected %<operator%>");
20583         return;
20584       case RT_CLASS:
20585         cp_parser_error (parser, "expected %<class%>");
20586         return;
20587       case RT_TEMPLATE:
20588         cp_parser_error (parser, "expected %<template%>");
20589         return;
20590       case RT_NAMESPACE:
20591         cp_parser_error (parser, "expected %<namespace%>");
20592         return;
20593       case RT_USING:
20594         cp_parser_error (parser, "expected %<using%>");
20595         return;
20596       case RT_ASM:
20597         cp_parser_error (parser, "expected %<asm%>");
20598         return;
20599       case RT_TRY:
20600         cp_parser_error (parser, "expected %<try%>");
20601         return;
20602       case RT_CATCH:
20603         cp_parser_error (parser, "expected %<catch%>");
20604         return;
20605       case RT_THROW:
20606         cp_parser_error (parser, "expected %<throw%>");
20607         return;
20608       case RT_LABEL:
20609         cp_parser_error (parser, "expected %<__label__%>");
20610         return;
20611       case RT_AT_TRY:
20612         cp_parser_error (parser, "expected %<@try%>");
20613         return;
20614       case RT_AT_SYNCHRONIZED:
20615         cp_parser_error (parser, "expected %<@synchronized%>");
20616         return;
20617       case RT_AT_THROW:
20618         cp_parser_error (parser, "expected %<@throw%>");
20619         return;
20620       default:
20621         break;
20622     }
20623   if (!keyword)
20624     {
20625       switch (token_desc)
20626         {
20627           case RT_SEMICOLON:
20628             cp_parser_error (parser, "expected %<;%>");
20629             return;
20630           case RT_OPEN_PAREN:
20631             cp_parser_error (parser, "expected %<(%>");
20632             return;
20633           case RT_CLOSE_BRACE:
20634             cp_parser_error (parser, "expected %<}%>");
20635             return;
20636           case RT_OPEN_BRACE:
20637             cp_parser_error (parser, "expected %<{%>");
20638             return;
20639           case RT_CLOSE_SQUARE:
20640             cp_parser_error (parser, "expected %<]%>");
20641             return;
20642           case RT_OPEN_SQUARE:
20643             cp_parser_error (parser, "expected %<[%>");
20644             return;
20645           case RT_COMMA:
20646             cp_parser_error (parser, "expected %<,%>");
20647             return;
20648           case RT_SCOPE:
20649             cp_parser_error (parser, "expected %<::%>");
20650             return;
20651           case RT_LESS:
20652             cp_parser_error (parser, "expected %<<%>");
20653             return;
20654           case RT_GREATER:
20655             cp_parser_error (parser, "expected %<>%>");
20656             return;
20657           case RT_EQ:
20658             cp_parser_error (parser, "expected %<=%>");
20659             return;
20660           case RT_ELLIPSIS:
20661             cp_parser_error (parser, "expected %<...%>");
20662             return;
20663           case RT_MULT:
20664             cp_parser_error (parser, "expected %<*%>");
20665             return;
20666           case RT_COMPL:
20667             cp_parser_error (parser, "expected %<~%>");
20668             return;
20669           case RT_COLON:
20670             cp_parser_error (parser, "expected %<:%>");
20671             return;
20672           case RT_COLON_SCOPE:
20673             cp_parser_error (parser, "expected %<:%> or %<::%>");
20674             return;
20675           case RT_CLOSE_PAREN:
20676             cp_parser_error (parser, "expected %<)%>");
20677             return;
20678           case RT_COMMA_CLOSE_PAREN:
20679             cp_parser_error (parser, "expected %<,%> or %<)%>");
20680             return;
20681           case RT_PRAGMA_EOL:
20682             cp_parser_error (parser, "expected end of line");
20683             return;
20684           case RT_NAME:
20685             cp_parser_error (parser, "expected identifier");
20686             return;
20687           case RT_SELECT:
20688             cp_parser_error (parser, "expected selection-statement");
20689             return;
20690           case RT_INTERATION:
20691             cp_parser_error (parser, "expected iteration-statement");
20692             return;
20693           case RT_JUMP:
20694             cp_parser_error (parser, "expected jump-statement");
20695             return;
20696           case RT_CLASS_KEY:
20697             cp_parser_error (parser, "expected class-key");
20698             return;
20699           case RT_CLASS_TYPENAME_TEMPLATE:
20700             cp_parser_error (parser,
20701                  "expected %<class%>, %<typename%>, or %<template%>");
20702             return;
20703           default:
20704             gcc_unreachable ();
20705         }
20706     }
20707   else
20708     gcc_unreachable ();
20709 }
20710
20711
20712
20713 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20714    issue an error message indicating that TOKEN_DESC was expected.
20715
20716    Returns the token consumed, if the token had the appropriate type.
20717    Otherwise, returns NULL.  */
20718
20719 static cp_token *
20720 cp_parser_require (cp_parser* parser,
20721                    enum cpp_ttype type,
20722                    required_token token_desc)
20723 {
20724   if (cp_lexer_next_token_is (parser->lexer, type))
20725     return cp_lexer_consume_token (parser->lexer);
20726   else
20727     {
20728       /* Output the MESSAGE -- unless we're parsing tentatively.  */
20729       if (!cp_parser_simulate_error (parser))
20730         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20731       return NULL;
20732     }
20733 }
20734
20735 /* An error message is produced if the next token is not '>'.
20736    All further tokens are skipped until the desired token is
20737    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
20738
20739 static void
20740 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20741 {
20742   /* Current level of '< ... >'.  */
20743   unsigned level = 0;
20744   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
20745   unsigned nesting_depth = 0;
20746
20747   /* Are we ready, yet?  If not, issue error message.  */
20748   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20749     return;
20750
20751   /* Skip tokens until the desired token is found.  */
20752   while (true)
20753     {
20754       /* Peek at the next token.  */
20755       switch (cp_lexer_peek_token (parser->lexer)->type)
20756         {
20757         case CPP_LESS:
20758           if (!nesting_depth)
20759             ++level;
20760           break;
20761
20762         case CPP_RSHIFT:
20763           if (cxx_dialect == cxx98)
20764             /* C++0x views the `>>' operator as two `>' tokens, but
20765                C++98 does not. */
20766             break;
20767           else if (!nesting_depth && level-- == 0)
20768             {
20769               /* We've hit a `>>' where the first `>' closes the
20770                  template argument list, and the second `>' is
20771                  spurious.  Just consume the `>>' and stop; we've
20772                  already produced at least one error.  */
20773               cp_lexer_consume_token (parser->lexer);
20774               return;
20775             }
20776           /* Fall through for C++0x, so we handle the second `>' in
20777              the `>>'.  */
20778
20779         case CPP_GREATER:
20780           if (!nesting_depth && level-- == 0)
20781             {
20782               /* We've reached the token we want, consume it and stop.  */
20783               cp_lexer_consume_token (parser->lexer);
20784               return;
20785             }
20786           break;
20787
20788         case CPP_OPEN_PAREN:
20789         case CPP_OPEN_SQUARE:
20790           ++nesting_depth;
20791           break;
20792
20793         case CPP_CLOSE_PAREN:
20794         case CPP_CLOSE_SQUARE:
20795           if (nesting_depth-- == 0)
20796             return;
20797           break;
20798
20799         case CPP_EOF:
20800         case CPP_PRAGMA_EOL:
20801         case CPP_SEMICOLON:
20802         case CPP_OPEN_BRACE:
20803         case CPP_CLOSE_BRACE:
20804           /* The '>' was probably forgotten, don't look further.  */
20805           return;
20806
20807         default:
20808           break;
20809         }
20810
20811       /* Consume this token.  */
20812       cp_lexer_consume_token (parser->lexer);
20813     }
20814 }
20815
20816 /* If the next token is the indicated keyword, consume it.  Otherwise,
20817    issue an error message indicating that TOKEN_DESC was expected.
20818
20819    Returns the token consumed, if the token had the appropriate type.
20820    Otherwise, returns NULL.  */
20821
20822 static cp_token *
20823 cp_parser_require_keyword (cp_parser* parser,
20824                            enum rid keyword,
20825                            required_token token_desc)
20826 {
20827   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20828
20829   if (token && token->keyword != keyword)
20830     {
20831       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
20832       return NULL;
20833     }
20834
20835   return token;
20836 }
20837
20838 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20839    function-definition.  */
20840
20841 static bool
20842 cp_parser_token_starts_function_definition_p (cp_token* token)
20843 {
20844   return (/* An ordinary function-body begins with an `{'.  */
20845           token->type == CPP_OPEN_BRACE
20846           /* A ctor-initializer begins with a `:'.  */
20847           || token->type == CPP_COLON
20848           /* A function-try-block begins with `try'.  */
20849           || token->keyword == RID_TRY
20850           /* The named return value extension begins with `return'.  */
20851           || token->keyword == RID_RETURN);
20852 }
20853
20854 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20855    definition.  */
20856
20857 static bool
20858 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20859 {
20860   cp_token *token;
20861
20862   token = cp_lexer_peek_token (parser->lexer);
20863   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20864 }
20865
20866 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20867    C++0x) ending a template-argument.  */
20868
20869 static bool
20870 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20871 {
20872   cp_token *token;
20873
20874   token = cp_lexer_peek_token (parser->lexer);
20875   return (token->type == CPP_COMMA 
20876           || token->type == CPP_GREATER
20877           || token->type == CPP_ELLIPSIS
20878           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20879 }
20880
20881 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20882    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
20883
20884 static bool
20885 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20886                                                      size_t n)
20887 {
20888   cp_token *token;
20889
20890   token = cp_lexer_peek_nth_token (parser->lexer, n);
20891   if (token->type == CPP_LESS)
20892     return true;
20893   /* Check for the sequence `<::' in the original code. It would be lexed as
20894      `[:', where `[' is a digraph, and there is no whitespace before
20895      `:'.  */
20896   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20897     {
20898       cp_token *token2;
20899       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20900       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20901         return true;
20902     }
20903   return false;
20904 }
20905
20906 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20907    or none_type otherwise.  */
20908
20909 static enum tag_types
20910 cp_parser_token_is_class_key (cp_token* token)
20911 {
20912   switch (token->keyword)
20913     {
20914     case RID_CLASS:
20915       return class_type;
20916     case RID_STRUCT:
20917       return record_type;
20918     case RID_UNION:
20919       return union_type;
20920
20921     default:
20922       return none_type;
20923     }
20924 }
20925
20926 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
20927
20928 static void
20929 cp_parser_check_class_key (enum tag_types class_key, tree type)
20930 {
20931   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20932     permerror (input_location, "%qs tag used in naming %q#T",
20933             class_key == union_type ? "union"
20934              : class_key == record_type ? "struct" : "class",
20935              type);
20936 }
20937
20938 /* Issue an error message if DECL is redeclared with different
20939    access than its original declaration [class.access.spec/3].
20940    This applies to nested classes and nested class templates.
20941    [class.mem/1].  */
20942
20943 static void
20944 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20945 {
20946   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20947     return;
20948
20949   if ((TREE_PRIVATE (decl)
20950        != (current_access_specifier == access_private_node))
20951       || (TREE_PROTECTED (decl)
20952           != (current_access_specifier == access_protected_node)))
20953     error_at (location, "%qD redeclared with different access", decl);
20954 }
20955
20956 /* Look for the `template' keyword, as a syntactic disambiguator.
20957    Return TRUE iff it is present, in which case it will be
20958    consumed.  */
20959
20960 static bool
20961 cp_parser_optional_template_keyword (cp_parser *parser)
20962 {
20963   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20964     {
20965       /* The `template' keyword can only be used within templates;
20966          outside templates the parser can always figure out what is a
20967          template and what is not.  */
20968       if (!processing_template_decl)
20969         {
20970           cp_token *token = cp_lexer_peek_token (parser->lexer);
20971           error_at (token->location,
20972                     "%<template%> (as a disambiguator) is only allowed "
20973                     "within templates");
20974           /* If this part of the token stream is rescanned, the same
20975              error message would be generated.  So, we purge the token
20976              from the stream.  */
20977           cp_lexer_purge_token (parser->lexer);
20978           return false;
20979         }
20980       else
20981         {
20982           /* Consume the `template' keyword.  */
20983           cp_lexer_consume_token (parser->lexer);
20984           return true;
20985         }
20986     }
20987
20988   return false;
20989 }
20990
20991 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
20992    set PARSER->SCOPE, and perform other related actions.  */
20993
20994 static void
20995 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20996 {
20997   int i;
20998   struct tree_check *check_value;
20999   deferred_access_check *chk;
21000   VEC (deferred_access_check,gc) *checks;
21001
21002   /* Get the stored value.  */
21003   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21004   /* Perform any access checks that were deferred.  */
21005   checks = check_value->checks;
21006   if (checks)
21007     {
21008       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21009         perform_or_defer_access_check (chk->binfo,
21010                                        chk->decl,
21011                                        chk->diag_decl);
21012     }
21013   /* Set the scope from the stored value.  */
21014   parser->scope = check_value->value;
21015   parser->qualifying_scope = check_value->qualifying_scope;
21016   parser->object_scope = NULL_TREE;
21017 }
21018
21019 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21020    encounter the end of a block before what we were looking for.  */
21021
21022 static bool
21023 cp_parser_cache_group (cp_parser *parser,
21024                        enum cpp_ttype end,
21025                        unsigned depth)
21026 {
21027   while (true)
21028     {
21029       cp_token *token = cp_lexer_peek_token (parser->lexer);
21030
21031       /* Abort a parenthesized expression if we encounter a semicolon.  */
21032       if ((end == CPP_CLOSE_PAREN || depth == 0)
21033           && token->type == CPP_SEMICOLON)
21034         return true;
21035       /* If we've reached the end of the file, stop.  */
21036       if (token->type == CPP_EOF
21037           || (end != CPP_PRAGMA_EOL
21038               && token->type == CPP_PRAGMA_EOL))
21039         return true;
21040       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21041         /* We've hit the end of an enclosing block, so there's been some
21042            kind of syntax error.  */
21043         return true;
21044
21045       /* Consume the token.  */
21046       cp_lexer_consume_token (parser->lexer);
21047       /* See if it starts a new group.  */
21048       if (token->type == CPP_OPEN_BRACE)
21049         {
21050           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21051           /* In theory this should probably check end == '}', but
21052              cp_parser_save_member_function_body needs it to exit
21053              after either '}' or ')' when called with ')'.  */
21054           if (depth == 0)
21055             return false;
21056         }
21057       else if (token->type == CPP_OPEN_PAREN)
21058         {
21059           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21060           if (depth == 0 && end == CPP_CLOSE_PAREN)
21061             return false;
21062         }
21063       else if (token->type == CPP_PRAGMA)
21064         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21065       else if (token->type == end)
21066         return false;
21067     }
21068 }
21069
21070 /* Begin parsing tentatively.  We always save tokens while parsing
21071    tentatively so that if the tentative parsing fails we can restore the
21072    tokens.  */
21073
21074 static void
21075 cp_parser_parse_tentatively (cp_parser* parser)
21076 {
21077   /* Enter a new parsing context.  */
21078   parser->context = cp_parser_context_new (parser->context);
21079   /* Begin saving tokens.  */
21080   cp_lexer_save_tokens (parser->lexer);
21081   /* In order to avoid repetitive access control error messages,
21082      access checks are queued up until we are no longer parsing
21083      tentatively.  */
21084   push_deferring_access_checks (dk_deferred);
21085 }
21086
21087 /* Commit to the currently active tentative parse.  */
21088
21089 static void
21090 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21091 {
21092   cp_parser_context *context;
21093   cp_lexer *lexer;
21094
21095   /* Mark all of the levels as committed.  */
21096   lexer = parser->lexer;
21097   for (context = parser->context; context->next; context = context->next)
21098     {
21099       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21100         break;
21101       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21102       while (!cp_lexer_saving_tokens (lexer))
21103         lexer = lexer->next;
21104       cp_lexer_commit_tokens (lexer);
21105     }
21106 }
21107
21108 /* Abort the currently active tentative parse.  All consumed tokens
21109    will be rolled back, and no diagnostics will be issued.  */
21110
21111 static void
21112 cp_parser_abort_tentative_parse (cp_parser* parser)
21113 {
21114   cp_parser_simulate_error (parser);
21115   /* Now, pretend that we want to see if the construct was
21116      successfully parsed.  */
21117   cp_parser_parse_definitely (parser);
21118 }
21119
21120 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21121    token stream.  Otherwise, commit to the tokens we have consumed.
21122    Returns true if no error occurred; false otherwise.  */
21123
21124 static bool
21125 cp_parser_parse_definitely (cp_parser* parser)
21126 {
21127   bool error_occurred;
21128   cp_parser_context *context;
21129
21130   /* Remember whether or not an error occurred, since we are about to
21131      destroy that information.  */
21132   error_occurred = cp_parser_error_occurred (parser);
21133   /* Remove the topmost context from the stack.  */
21134   context = parser->context;
21135   parser->context = context->next;
21136   /* If no parse errors occurred, commit to the tentative parse.  */
21137   if (!error_occurred)
21138     {
21139       /* Commit to the tokens read tentatively, unless that was
21140          already done.  */
21141       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21142         cp_lexer_commit_tokens (parser->lexer);
21143
21144       pop_to_parent_deferring_access_checks ();
21145     }
21146   /* Otherwise, if errors occurred, roll back our state so that things
21147      are just as they were before we began the tentative parse.  */
21148   else
21149     {
21150       cp_lexer_rollback_tokens (parser->lexer);
21151       pop_deferring_access_checks ();
21152     }
21153   /* Add the context to the front of the free list.  */
21154   context->next = cp_parser_context_free_list;
21155   cp_parser_context_free_list = context;
21156
21157   return !error_occurred;
21158 }
21159
21160 /* Returns true if we are parsing tentatively and are not committed to
21161    this tentative parse.  */
21162
21163 static bool
21164 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21165 {
21166   return (cp_parser_parsing_tentatively (parser)
21167           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21168 }
21169
21170 /* Returns nonzero iff an error has occurred during the most recent
21171    tentative parse.  */
21172
21173 static bool
21174 cp_parser_error_occurred (cp_parser* parser)
21175 {
21176   return (cp_parser_parsing_tentatively (parser)
21177           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21178 }
21179
21180 /* Returns nonzero if GNU extensions are allowed.  */
21181
21182 static bool
21183 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21184 {
21185   return parser->allow_gnu_extensions_p;
21186 }
21187 \f
21188 /* Objective-C++ Productions */
21189
21190
21191 /* Parse an Objective-C expression, which feeds into a primary-expression
21192    above.
21193
21194    objc-expression:
21195      objc-message-expression
21196      objc-string-literal
21197      objc-encode-expression
21198      objc-protocol-expression
21199      objc-selector-expression
21200
21201   Returns a tree representation of the expression.  */
21202
21203 static tree
21204 cp_parser_objc_expression (cp_parser* parser)
21205 {
21206   /* Try to figure out what kind of declaration is present.  */
21207   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21208
21209   switch (kwd->type)
21210     {
21211     case CPP_OPEN_SQUARE:
21212       return cp_parser_objc_message_expression (parser);
21213
21214     case CPP_OBJC_STRING:
21215       kwd = cp_lexer_consume_token (parser->lexer);
21216       return objc_build_string_object (kwd->u.value);
21217
21218     case CPP_KEYWORD:
21219       switch (kwd->keyword)
21220         {
21221         case RID_AT_ENCODE:
21222           return cp_parser_objc_encode_expression (parser);
21223
21224         case RID_AT_PROTOCOL:
21225           return cp_parser_objc_protocol_expression (parser);
21226
21227         case RID_AT_SELECTOR:
21228           return cp_parser_objc_selector_expression (parser);
21229
21230         default:
21231           break;
21232         }
21233     default:
21234       error_at (kwd->location,
21235                 "misplaced %<@%D%> Objective-C++ construct",
21236                 kwd->u.value);
21237       cp_parser_skip_to_end_of_block_or_statement (parser);
21238     }
21239
21240   return error_mark_node;
21241 }
21242
21243 /* Parse an Objective-C message expression.
21244
21245    objc-message-expression:
21246      [ objc-message-receiver objc-message-args ]
21247
21248    Returns a representation of an Objective-C message.  */
21249
21250 static tree
21251 cp_parser_objc_message_expression (cp_parser* parser)
21252 {
21253   tree receiver, messageargs;
21254
21255   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21256   receiver = cp_parser_objc_message_receiver (parser);
21257   messageargs = cp_parser_objc_message_args (parser);
21258   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21259
21260   return objc_build_message_expr (build_tree_list (receiver, messageargs));
21261 }
21262
21263 /* Parse an objc-message-receiver.
21264
21265    objc-message-receiver:
21266      expression
21267      simple-type-specifier
21268
21269   Returns a representation of the type or expression.  */
21270
21271 static tree
21272 cp_parser_objc_message_receiver (cp_parser* parser)
21273 {
21274   tree rcv;
21275
21276   /* An Objective-C message receiver may be either (1) a type
21277      or (2) an expression.  */
21278   cp_parser_parse_tentatively (parser);
21279   rcv = cp_parser_expression (parser, false, NULL);
21280
21281   if (cp_parser_parse_definitely (parser))
21282     return rcv;
21283
21284   rcv = cp_parser_simple_type_specifier (parser,
21285                                          /*decl_specs=*/NULL,
21286                                          CP_PARSER_FLAGS_NONE);
21287
21288   return objc_get_class_reference (rcv);
21289 }
21290
21291 /* Parse the arguments and selectors comprising an Objective-C message.
21292
21293    objc-message-args:
21294      objc-selector
21295      objc-selector-args
21296      objc-selector-args , objc-comma-args
21297
21298    objc-selector-args:
21299      objc-selector [opt] : assignment-expression
21300      objc-selector-args objc-selector [opt] : assignment-expression
21301
21302    objc-comma-args:
21303      assignment-expression
21304      objc-comma-args , assignment-expression
21305
21306    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21307    selector arguments and TREE_VALUE containing a list of comma
21308    arguments.  */
21309
21310 static tree
21311 cp_parser_objc_message_args (cp_parser* parser)
21312 {
21313   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21314   bool maybe_unary_selector_p = true;
21315   cp_token *token = cp_lexer_peek_token (parser->lexer);
21316
21317   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21318     {
21319       tree selector = NULL_TREE, arg;
21320
21321       if (token->type != CPP_COLON)
21322         selector = cp_parser_objc_selector (parser);
21323
21324       /* Detect if we have a unary selector.  */
21325       if (maybe_unary_selector_p
21326           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21327         return build_tree_list (selector, NULL_TREE);
21328
21329       maybe_unary_selector_p = false;
21330       cp_parser_require (parser, CPP_COLON, RT_COLON);
21331       arg = cp_parser_assignment_expression (parser, false, NULL);
21332
21333       sel_args
21334         = chainon (sel_args,
21335                    build_tree_list (selector, arg));
21336
21337       token = cp_lexer_peek_token (parser->lexer);
21338     }
21339
21340   /* Handle non-selector arguments, if any. */
21341   while (token->type == CPP_COMMA)
21342     {
21343       tree arg;
21344
21345       cp_lexer_consume_token (parser->lexer);
21346       arg = cp_parser_assignment_expression (parser, false, NULL);
21347
21348       addl_args
21349         = chainon (addl_args,
21350                    build_tree_list (NULL_TREE, arg));
21351
21352       token = cp_lexer_peek_token (parser->lexer);
21353     }
21354
21355   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21356     {
21357       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21358       return build_tree_list (error_mark_node, error_mark_node);
21359     }
21360
21361   return build_tree_list (sel_args, addl_args);
21362 }
21363
21364 /* Parse an Objective-C encode expression.
21365
21366    objc-encode-expression:
21367      @encode objc-typename
21368
21369    Returns an encoded representation of the type argument.  */
21370
21371 static tree
21372 cp_parser_objc_encode_expression (cp_parser* parser)
21373 {
21374   tree type;
21375   cp_token *token;
21376
21377   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21378   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21379   token = cp_lexer_peek_token (parser->lexer);
21380   type = complete_type (cp_parser_type_id (parser));
21381   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21382
21383   if (!type)
21384     {
21385       error_at (token->location, 
21386                 "%<@encode%> must specify a type as an argument");
21387       return error_mark_node;
21388     }
21389
21390   /* This happens if we find @encode(T) (where T is a template
21391      typename or something dependent on a template typename) when
21392      parsing a template.  In that case, we can't compile it
21393      immediately, but we rather create an AT_ENCODE_EXPR which will
21394      need to be instantiated when the template is used.
21395   */
21396   if (dependent_type_p (type))
21397     {
21398       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21399       TREE_READONLY (value) = 1;
21400       return value;
21401     }
21402
21403   return objc_build_encode_expr (type);
21404 }
21405
21406 /* Parse an Objective-C @defs expression.  */
21407
21408 static tree
21409 cp_parser_objc_defs_expression (cp_parser *parser)
21410 {
21411   tree name;
21412
21413   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21414   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21415   name = cp_parser_identifier (parser);
21416   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21417
21418   return objc_get_class_ivars (name);
21419 }
21420
21421 /* Parse an Objective-C protocol expression.
21422
21423   objc-protocol-expression:
21424     @protocol ( identifier )
21425
21426   Returns a representation of the protocol expression.  */
21427
21428 static tree
21429 cp_parser_objc_protocol_expression (cp_parser* parser)
21430 {
21431   tree proto;
21432
21433   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21434   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21435   proto = cp_parser_identifier (parser);
21436   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21437
21438   return objc_build_protocol_expr (proto);
21439 }
21440
21441 /* Parse an Objective-C selector expression.
21442
21443    objc-selector-expression:
21444      @selector ( objc-method-signature )
21445
21446    objc-method-signature:
21447      objc-selector
21448      objc-selector-seq
21449
21450    objc-selector-seq:
21451      objc-selector :
21452      objc-selector-seq objc-selector :
21453
21454   Returns a representation of the method selector.  */
21455
21456 static tree
21457 cp_parser_objc_selector_expression (cp_parser* parser)
21458 {
21459   tree sel_seq = NULL_TREE;
21460   bool maybe_unary_selector_p = true;
21461   cp_token *token;
21462   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21463
21464   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21465   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21466   token = cp_lexer_peek_token (parser->lexer);
21467
21468   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21469          || token->type == CPP_SCOPE)
21470     {
21471       tree selector = NULL_TREE;
21472
21473       if (token->type != CPP_COLON
21474           || token->type == CPP_SCOPE)
21475         selector = cp_parser_objc_selector (parser);
21476
21477       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21478           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21479         {
21480           /* Detect if we have a unary selector.  */
21481           if (maybe_unary_selector_p)
21482             {
21483               sel_seq = selector;
21484               goto finish_selector;
21485             }
21486           else
21487             {
21488               cp_parser_error (parser, "expected %<:%>");
21489             }
21490         }
21491       maybe_unary_selector_p = false;
21492       token = cp_lexer_consume_token (parser->lexer);
21493
21494       if (token->type == CPP_SCOPE)
21495         {
21496           sel_seq
21497             = chainon (sel_seq,
21498                        build_tree_list (selector, NULL_TREE));
21499           sel_seq
21500             = chainon (sel_seq,
21501                        build_tree_list (NULL_TREE, NULL_TREE));
21502         }
21503       else
21504         sel_seq
21505           = chainon (sel_seq,
21506                      build_tree_list (selector, NULL_TREE));
21507
21508       token = cp_lexer_peek_token (parser->lexer);
21509     }
21510
21511  finish_selector:
21512   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21513
21514   return objc_build_selector_expr (loc, sel_seq);
21515 }
21516
21517 /* Parse a list of identifiers.
21518
21519    objc-identifier-list:
21520      identifier
21521      objc-identifier-list , identifier
21522
21523    Returns a TREE_LIST of identifier nodes.  */
21524
21525 static tree
21526 cp_parser_objc_identifier_list (cp_parser* parser)
21527 {
21528   tree identifier;
21529   tree list;
21530   cp_token *sep;
21531
21532   identifier = cp_parser_identifier (parser);
21533   if (identifier == error_mark_node)
21534     return error_mark_node;      
21535
21536   list = build_tree_list (NULL_TREE, identifier);
21537   sep = cp_lexer_peek_token (parser->lexer);
21538
21539   while (sep->type == CPP_COMMA)
21540     {
21541       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21542       identifier = cp_parser_identifier (parser);
21543       if (identifier == error_mark_node)
21544         return list;
21545
21546       list = chainon (list, build_tree_list (NULL_TREE,
21547                                              identifier));
21548       sep = cp_lexer_peek_token (parser->lexer);
21549     }
21550   
21551   return list;
21552 }
21553
21554 /* Parse an Objective-C alias declaration.
21555
21556    objc-alias-declaration:
21557      @compatibility_alias identifier identifier ;
21558
21559    This function registers the alias mapping with the Objective-C front end.
21560    It returns nothing.  */
21561
21562 static void
21563 cp_parser_objc_alias_declaration (cp_parser* parser)
21564 {
21565   tree alias, orig;
21566
21567   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21568   alias = cp_parser_identifier (parser);
21569   orig = cp_parser_identifier (parser);
21570   objc_declare_alias (alias, orig);
21571   cp_parser_consume_semicolon_at_end_of_statement (parser);
21572 }
21573
21574 /* Parse an Objective-C class forward-declaration.
21575
21576    objc-class-declaration:
21577      @class objc-identifier-list ;
21578
21579    The function registers the forward declarations with the Objective-C
21580    front end.  It returns nothing.  */
21581
21582 static void
21583 cp_parser_objc_class_declaration (cp_parser* parser)
21584 {
21585   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21586   objc_declare_class (cp_parser_objc_identifier_list (parser));
21587   cp_parser_consume_semicolon_at_end_of_statement (parser);
21588 }
21589
21590 /* Parse a list of Objective-C protocol references.
21591
21592    objc-protocol-refs-opt:
21593      objc-protocol-refs [opt]
21594
21595    objc-protocol-refs:
21596      < objc-identifier-list >
21597
21598    Returns a TREE_LIST of identifiers, if any.  */
21599
21600 static tree
21601 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21602 {
21603   tree protorefs = NULL_TREE;
21604
21605   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21606     {
21607       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21608       protorefs = cp_parser_objc_identifier_list (parser);
21609       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21610     }
21611
21612   return protorefs;
21613 }
21614
21615 /* Parse a Objective-C visibility specification.  */
21616
21617 static void
21618 cp_parser_objc_visibility_spec (cp_parser* parser)
21619 {
21620   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21621
21622   switch (vis->keyword)
21623     {
21624     case RID_AT_PRIVATE:
21625       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21626       break;
21627     case RID_AT_PROTECTED:
21628       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21629       break;
21630     case RID_AT_PUBLIC:
21631       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21632       break;
21633     case RID_AT_PACKAGE:
21634       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21635       break;
21636     default:
21637       return;
21638     }
21639
21640   /* Eat '@private'/'@protected'/'@public'.  */
21641   cp_lexer_consume_token (parser->lexer);
21642 }
21643
21644 /* Parse an Objective-C method type.  Return 'true' if it is a class
21645    (+) method, and 'false' if it is an instance (-) method.  */
21646
21647 static inline bool
21648 cp_parser_objc_method_type (cp_parser* parser)
21649 {
21650   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21651     return true;
21652   else
21653     return false;
21654 }
21655
21656 /* Parse an Objective-C protocol qualifier.  */
21657
21658 static tree
21659 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21660 {
21661   tree quals = NULL_TREE, node;
21662   cp_token *token = cp_lexer_peek_token (parser->lexer);
21663
21664   node = token->u.value;
21665
21666   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21667          && (node == ridpointers [(int) RID_IN]
21668              || node == ridpointers [(int) RID_OUT]
21669              || node == ridpointers [(int) RID_INOUT]
21670              || node == ridpointers [(int) RID_BYCOPY]
21671              || node == ridpointers [(int) RID_BYREF]
21672              || node == ridpointers [(int) RID_ONEWAY]))
21673     {
21674       quals = tree_cons (NULL_TREE, node, quals);
21675       cp_lexer_consume_token (parser->lexer);
21676       token = cp_lexer_peek_token (parser->lexer);
21677       node = token->u.value;
21678     }
21679
21680   return quals;
21681 }
21682
21683 /* Parse an Objective-C typename.  */
21684
21685 static tree
21686 cp_parser_objc_typename (cp_parser* parser)
21687 {
21688   tree type_name = NULL_TREE;
21689
21690   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21691     {
21692       tree proto_quals, cp_type = NULL_TREE;
21693
21694       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21695       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21696
21697       /* An ObjC type name may consist of just protocol qualifiers, in which
21698          case the type shall default to 'id'.  */
21699       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21700         cp_type = cp_parser_type_id (parser);
21701
21702       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21703       type_name = build_tree_list (proto_quals, cp_type);
21704     }
21705
21706   return type_name;
21707 }
21708
21709 /* Check to see if TYPE refers to an Objective-C selector name.  */
21710
21711 static bool
21712 cp_parser_objc_selector_p (enum cpp_ttype type)
21713 {
21714   return (type == CPP_NAME || type == CPP_KEYWORD
21715           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21716           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21717           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21718           || type == CPP_XOR || type == CPP_XOR_EQ);
21719 }
21720
21721 /* Parse an Objective-C selector.  */
21722
21723 static tree
21724 cp_parser_objc_selector (cp_parser* parser)
21725 {
21726   cp_token *token = cp_lexer_consume_token (parser->lexer);
21727
21728   if (!cp_parser_objc_selector_p (token->type))
21729     {
21730       error_at (token->location, "invalid Objective-C++ selector name");
21731       return error_mark_node;
21732     }
21733
21734   /* C++ operator names are allowed to appear in ObjC selectors.  */
21735   switch (token->type)
21736     {
21737     case CPP_AND_AND: return get_identifier ("and");
21738     case CPP_AND_EQ: return get_identifier ("and_eq");
21739     case CPP_AND: return get_identifier ("bitand");
21740     case CPP_OR: return get_identifier ("bitor");
21741     case CPP_COMPL: return get_identifier ("compl");
21742     case CPP_NOT: return get_identifier ("not");
21743     case CPP_NOT_EQ: return get_identifier ("not_eq");
21744     case CPP_OR_OR: return get_identifier ("or");
21745     case CPP_OR_EQ: return get_identifier ("or_eq");
21746     case CPP_XOR: return get_identifier ("xor");
21747     case CPP_XOR_EQ: return get_identifier ("xor_eq");
21748     default: return token->u.value;
21749     }
21750 }
21751
21752 /* Parse an Objective-C params list.  */
21753
21754 static tree
21755 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21756 {
21757   tree params = NULL_TREE;
21758   bool maybe_unary_selector_p = true;
21759   cp_token *token = cp_lexer_peek_token (parser->lexer);
21760
21761   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21762     {
21763       tree selector = NULL_TREE, type_name, identifier;
21764       tree parm_attr = NULL_TREE;
21765
21766       if (token->keyword == RID_ATTRIBUTE)
21767         break;
21768
21769       if (token->type != CPP_COLON)
21770         selector = cp_parser_objc_selector (parser);
21771
21772       /* Detect if we have a unary selector.  */
21773       if (maybe_unary_selector_p
21774           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21775         {
21776           params = selector; /* Might be followed by attributes.  */
21777           break;
21778         }
21779
21780       maybe_unary_selector_p = false;
21781       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21782         {
21783           /* Something went quite wrong.  There should be a colon
21784              here, but there is not.  Stop parsing parameters.  */
21785           break;
21786         }
21787       type_name = cp_parser_objc_typename (parser);
21788       /* New ObjC allows attributes on parameters too.  */
21789       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21790         parm_attr = cp_parser_attributes_opt (parser);
21791       identifier = cp_parser_identifier (parser);
21792
21793       params
21794         = chainon (params,
21795                    objc_build_keyword_decl (selector,
21796                                             type_name,
21797                                             identifier,
21798                                             parm_attr));
21799
21800       token = cp_lexer_peek_token (parser->lexer);
21801     }
21802
21803   if (params == NULL_TREE)
21804     {
21805       cp_parser_error (parser, "objective-c++ method declaration is expected");
21806       return error_mark_node;
21807     }
21808
21809   /* We allow tail attributes for the method.  */
21810   if (token->keyword == RID_ATTRIBUTE)
21811     {
21812       *attributes = cp_parser_attributes_opt (parser);
21813       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21814           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21815         return params;
21816       cp_parser_error (parser, 
21817                        "method attributes must be specified at the end");
21818       return error_mark_node;
21819     }
21820
21821   if (params == NULL_TREE)
21822     {
21823       cp_parser_error (parser, "objective-c++ method declaration is expected");
21824       return error_mark_node;
21825     }
21826   return params;
21827 }
21828
21829 /* Parse the non-keyword Objective-C params.  */
21830
21831 static tree
21832 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
21833                                        tree* attributes)
21834 {
21835   tree params = make_node (TREE_LIST);
21836   cp_token *token = cp_lexer_peek_token (parser->lexer);
21837   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
21838
21839   while (token->type == CPP_COMMA)
21840     {
21841       cp_parameter_declarator *parmdecl;
21842       tree parm;
21843
21844       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21845       token = cp_lexer_peek_token (parser->lexer);
21846
21847       if (token->type == CPP_ELLIPSIS)
21848         {
21849           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
21850           *ellipsisp = true;
21851           token = cp_lexer_peek_token (parser->lexer);
21852           break;
21853         }
21854
21855       /* TODO: parse attributes for tail parameters.  */
21856       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21857       parm = grokdeclarator (parmdecl->declarator,
21858                              &parmdecl->decl_specifiers,
21859                              PARM, /*initialized=*/0,
21860                              /*attrlist=*/NULL);
21861
21862       chainon (params, build_tree_list (NULL_TREE, parm));
21863       token = cp_lexer_peek_token (parser->lexer);
21864     }
21865
21866   /* We allow tail attributes for the method.  */
21867   if (token->keyword == RID_ATTRIBUTE)
21868     {
21869       if (*attributes == NULL_TREE)
21870         {
21871           *attributes = cp_parser_attributes_opt (parser);
21872           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21873               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21874             return params;
21875         }
21876       else        
21877         /* We have an error, but parse the attributes, so that we can 
21878            carry on.  */
21879         *attributes = cp_parser_attributes_opt (parser);
21880
21881       cp_parser_error (parser, 
21882                        "method attributes must be specified at the end");
21883       return error_mark_node;
21884     }
21885
21886   return params;
21887 }
21888
21889 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
21890
21891 static void
21892 cp_parser_objc_interstitial_code (cp_parser* parser)
21893 {
21894   cp_token *token = cp_lexer_peek_token (parser->lexer);
21895
21896   /* If the next token is `extern' and the following token is a string
21897      literal, then we have a linkage specification.  */
21898   if (token->keyword == RID_EXTERN
21899       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21900     cp_parser_linkage_specification (parser);
21901   /* Handle #pragma, if any.  */
21902   else if (token->type == CPP_PRAGMA)
21903     cp_parser_pragma (parser, pragma_external);
21904   /* Allow stray semicolons.  */
21905   else if (token->type == CPP_SEMICOLON)
21906     cp_lexer_consume_token (parser->lexer);
21907   /* Mark methods as optional or required, when building protocols.  */
21908   else if (token->keyword == RID_AT_OPTIONAL)
21909     {
21910       cp_lexer_consume_token (parser->lexer);
21911       objc_set_method_opt (true);
21912     }
21913   else if (token->keyword == RID_AT_REQUIRED)
21914     {
21915       cp_lexer_consume_token (parser->lexer);
21916       objc_set_method_opt (false);
21917     }
21918   else if (token->keyword == RID_NAMESPACE)
21919     cp_parser_namespace_definition (parser);
21920   /* Other stray characters must generate errors.  */
21921   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
21922     {
21923       cp_lexer_consume_token (parser->lexer);
21924       error ("stray `%s' between Objective-C++ methods",
21925              token->type == CPP_OPEN_BRACE ? "{" : "}");
21926     }
21927   /* Finally, try to parse a block-declaration, or a function-definition.  */
21928   else
21929     cp_parser_block_declaration (parser, /*statement_p=*/false);
21930 }
21931
21932 /* Parse a method signature.  */
21933
21934 static tree
21935 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
21936 {
21937   tree rettype, kwdparms, optparms;
21938   bool ellipsis = false;
21939   bool is_class_method;
21940
21941   is_class_method = cp_parser_objc_method_type (parser);
21942   rettype = cp_parser_objc_typename (parser);
21943   *attributes = NULL_TREE;
21944   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
21945   if (kwdparms == error_mark_node)
21946     return error_mark_node;
21947   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
21948   if (optparms == error_mark_node)
21949     return error_mark_node;
21950
21951   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
21952 }
21953
21954 static bool
21955 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
21956 {
21957   tree tattr;  
21958   cp_lexer_save_tokens (parser->lexer);
21959   tattr = cp_parser_attributes_opt (parser);
21960   gcc_assert (tattr) ;
21961   
21962   /* If the attributes are followed by a method introducer, this is not allowed.
21963      Dump the attributes and flag the situation.  */
21964   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
21965       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
21966     return true;
21967
21968   /* Otherwise, the attributes introduce some interstitial code, possibly so
21969      rewind to allow that check.  */
21970   cp_lexer_rollback_tokens (parser->lexer);
21971   return false;  
21972 }
21973
21974 /* Parse an Objective-C method prototype list.  */
21975
21976 static void
21977 cp_parser_objc_method_prototype_list (cp_parser* parser)
21978 {
21979   cp_token *token = cp_lexer_peek_token (parser->lexer);
21980
21981   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
21982     {
21983       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21984         {
21985           tree attributes, sig;
21986           bool is_class_method;
21987           if (token->type == CPP_PLUS)
21988             is_class_method = true;
21989           else
21990             is_class_method = false;
21991           sig = cp_parser_objc_method_signature (parser, &attributes);
21992           if (sig == error_mark_node)
21993             {
21994               cp_parser_skip_to_end_of_block_or_statement (parser);
21995               token = cp_lexer_peek_token (parser->lexer);
21996               continue;
21997             }
21998           objc_add_method_declaration (is_class_method, sig, attributes);
21999           cp_parser_consume_semicolon_at_end_of_statement (parser);
22000         }
22001       else if (token->keyword == RID_AT_PROPERTY)
22002         cp_parser_objc_at_property_declaration (parser);
22003       else if (token->keyword == RID_ATTRIBUTE 
22004                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22005         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22006                     OPT_Wattributes, 
22007                     "prefix attributes are ignored for methods");
22008       else
22009         /* Allow for interspersed non-ObjC++ code.  */
22010         cp_parser_objc_interstitial_code (parser);
22011
22012       token = cp_lexer_peek_token (parser->lexer);
22013     }
22014
22015   if (token->type != CPP_EOF)
22016     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22017   else
22018     cp_parser_error (parser, "expected %<@end%>");
22019
22020   objc_finish_interface ();
22021 }
22022
22023 /* Parse an Objective-C method definition list.  */
22024
22025 static void
22026 cp_parser_objc_method_definition_list (cp_parser* parser)
22027 {
22028   cp_token *token = cp_lexer_peek_token (parser->lexer);
22029
22030   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22031     {
22032       tree meth;
22033
22034       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22035         {
22036           cp_token *ptk;
22037           tree sig, attribute;
22038           bool is_class_method;
22039           if (token->type == CPP_PLUS)
22040             is_class_method = true;
22041           else
22042             is_class_method = false;
22043           push_deferring_access_checks (dk_deferred);
22044           sig = cp_parser_objc_method_signature (parser, &attribute);
22045           if (sig == error_mark_node)
22046             {
22047               cp_parser_skip_to_end_of_block_or_statement (parser);
22048               token = cp_lexer_peek_token (parser->lexer);
22049               continue;
22050             }
22051           objc_start_method_definition (is_class_method, sig, attribute);
22052
22053           /* For historical reasons, we accept an optional semicolon.  */
22054           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22055             cp_lexer_consume_token (parser->lexer);
22056
22057           ptk = cp_lexer_peek_token (parser->lexer);
22058           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22059                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22060             {
22061               perform_deferred_access_checks ();
22062               stop_deferring_access_checks ();
22063               meth = cp_parser_function_definition_after_declarator (parser,
22064                                                                      false);
22065               pop_deferring_access_checks ();
22066               objc_finish_method_definition (meth);
22067             }
22068         }
22069       /* The following case will be removed once @synthesize is
22070          completely implemented.  */
22071       else if (token->keyword == RID_AT_PROPERTY)
22072         cp_parser_objc_at_property_declaration (parser);
22073       else if (token->keyword == RID_AT_SYNTHESIZE)
22074         cp_parser_objc_at_synthesize_declaration (parser);
22075       else if (token->keyword == RID_AT_DYNAMIC)
22076         cp_parser_objc_at_dynamic_declaration (parser);
22077       else if (token->keyword == RID_ATTRIBUTE 
22078                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22079         warning_at (token->location, OPT_Wattributes,
22080                     "prefix attributes are ignored for methods");
22081       else
22082         /* Allow for interspersed non-ObjC++ code.  */
22083         cp_parser_objc_interstitial_code (parser);
22084
22085       token = cp_lexer_peek_token (parser->lexer);
22086     }
22087
22088   if (token->type != CPP_EOF)
22089     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22090   else
22091     cp_parser_error (parser, "expected %<@end%>");
22092
22093   objc_finish_implementation ();
22094 }
22095
22096 /* Parse Objective-C ivars.  */
22097
22098 static void
22099 cp_parser_objc_class_ivars (cp_parser* parser)
22100 {
22101   cp_token *token = cp_lexer_peek_token (parser->lexer);
22102
22103   if (token->type != CPP_OPEN_BRACE)
22104     return;     /* No ivars specified.  */
22105
22106   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22107   token = cp_lexer_peek_token (parser->lexer);
22108
22109   while (token->type != CPP_CLOSE_BRACE 
22110         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22111     {
22112       cp_decl_specifier_seq declspecs;
22113       int decl_class_or_enum_p;
22114       tree prefix_attributes;
22115
22116       cp_parser_objc_visibility_spec (parser);
22117
22118       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22119         break;
22120
22121       cp_parser_decl_specifier_seq (parser,
22122                                     CP_PARSER_FLAGS_OPTIONAL,
22123                                     &declspecs,
22124                                     &decl_class_or_enum_p);
22125
22126       /* auto, register, static, extern, mutable.  */
22127       if (declspecs.storage_class != sc_none)
22128         {
22129           cp_parser_error (parser, "invalid type for instance variable");         
22130           declspecs.storage_class = sc_none;
22131         }
22132
22133       /* __thread.  */
22134       if (declspecs.specs[(int) ds_thread])
22135         {
22136           cp_parser_error (parser, "invalid type for instance variable");
22137           declspecs.specs[(int) ds_thread] = 0;
22138         }
22139       
22140       /* typedef.  */
22141       if (declspecs.specs[(int) ds_typedef])
22142         {
22143           cp_parser_error (parser, "invalid type for instance variable");
22144           declspecs.specs[(int) ds_typedef] = 0;
22145         }
22146
22147       prefix_attributes = declspecs.attributes;
22148       declspecs.attributes = NULL_TREE;
22149
22150       /* Keep going until we hit the `;' at the end of the
22151          declaration.  */
22152       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22153         {
22154           tree width = NULL_TREE, attributes, first_attribute, decl;
22155           cp_declarator *declarator = NULL;
22156           int ctor_dtor_or_conv_p;
22157
22158           /* Check for a (possibly unnamed) bitfield declaration.  */
22159           token = cp_lexer_peek_token (parser->lexer);
22160           if (token->type == CPP_COLON)
22161             goto eat_colon;
22162
22163           if (token->type == CPP_NAME
22164               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22165                   == CPP_COLON))
22166             {
22167               /* Get the name of the bitfield.  */
22168               declarator = make_id_declarator (NULL_TREE,
22169                                                cp_parser_identifier (parser),
22170                                                sfk_none);
22171
22172              eat_colon:
22173               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22174               /* Get the width of the bitfield.  */
22175               width
22176                 = cp_parser_constant_expression (parser,
22177                                                  /*allow_non_constant=*/false,
22178                                                  NULL);
22179             }
22180           else
22181             {
22182               /* Parse the declarator.  */
22183               declarator
22184                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22185                                         &ctor_dtor_or_conv_p,
22186                                         /*parenthesized_p=*/NULL,
22187                                         /*member_p=*/false);
22188             }
22189
22190           /* Look for attributes that apply to the ivar.  */
22191           attributes = cp_parser_attributes_opt (parser);
22192           /* Remember which attributes are prefix attributes and
22193              which are not.  */
22194           first_attribute = attributes;
22195           /* Combine the attributes.  */
22196           attributes = chainon (prefix_attributes, attributes);
22197
22198           if (width)
22199               /* Create the bitfield declaration.  */
22200               decl = grokbitfield (declarator, &declspecs,
22201                                    width,
22202                                    attributes);
22203           else
22204             decl = grokfield (declarator, &declspecs,
22205                               NULL_TREE, /*init_const_expr_p=*/false,
22206                               NULL_TREE, attributes);
22207
22208           /* Add the instance variable.  */
22209           objc_add_instance_variable (decl);
22210
22211           /* Reset PREFIX_ATTRIBUTES.  */
22212           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22213             attributes = TREE_CHAIN (attributes);
22214           if (attributes)
22215             TREE_CHAIN (attributes) = NULL_TREE;
22216
22217           token = cp_lexer_peek_token (parser->lexer);
22218
22219           if (token->type == CPP_COMMA)
22220             {
22221               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22222               continue;
22223             }
22224           break;
22225         }
22226
22227       cp_parser_consume_semicolon_at_end_of_statement (parser);
22228       token = cp_lexer_peek_token (parser->lexer);
22229     }
22230
22231   if (token->keyword == RID_AT_END)
22232     cp_parser_error (parser, "expected %<}%>");
22233
22234   /* Do not consume the RID_AT_END, so it will be read again as terminating
22235      the @interface of @implementation.  */ 
22236   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22237     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22238     
22239   /* For historical reasons, we accept an optional semicolon.  */
22240   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22241     cp_lexer_consume_token (parser->lexer);
22242 }
22243
22244 /* Parse an Objective-C protocol declaration.  */
22245
22246 static void
22247 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22248 {
22249   tree proto, protorefs;
22250   cp_token *tok;
22251
22252   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22253   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22254     {
22255       tok = cp_lexer_peek_token (parser->lexer);
22256       error_at (tok->location, "identifier expected after %<@protocol%>");
22257       goto finish;
22258     }
22259
22260   /* See if we have a forward declaration or a definition.  */
22261   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22262
22263   /* Try a forward declaration first.  */
22264   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22265     {
22266       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
22267      finish:
22268       cp_parser_consume_semicolon_at_end_of_statement (parser);
22269     }
22270
22271   /* Ok, we got a full-fledged definition (or at least should).  */
22272   else
22273     {
22274       proto = cp_parser_identifier (parser);
22275       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22276       objc_start_protocol (proto, protorefs, attributes);
22277       cp_parser_objc_method_prototype_list (parser);
22278     }
22279 }
22280
22281 /* Parse an Objective-C superclass or category.  */
22282
22283 static void
22284 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
22285                                                           tree *categ)
22286 {
22287   cp_token *next = cp_lexer_peek_token (parser->lexer);
22288
22289   *super = *categ = NULL_TREE;
22290   if (next->type == CPP_COLON)
22291     {
22292       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22293       *super = cp_parser_identifier (parser);
22294     }
22295   else if (next->type == CPP_OPEN_PAREN)
22296     {
22297       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22298       *categ = cp_parser_identifier (parser);
22299       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22300     }
22301 }
22302
22303 /* Parse an Objective-C class interface.  */
22304
22305 static void
22306 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22307 {
22308   tree name, super, categ, protos;
22309
22310   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22311   name = cp_parser_identifier (parser);
22312   if (name == error_mark_node)
22313     {
22314       /* It's hard to recover because even if valid @interface stuff
22315          is to follow, we can't compile it (or validate it) if we
22316          don't even know which class it refers to.  Let's assume this
22317          was a stray '@interface' token in the stream and skip it.
22318       */
22319       return;
22320     }
22321   cp_parser_objc_superclass_or_category (parser, &super, &categ);
22322   protos = cp_parser_objc_protocol_refs_opt (parser);
22323
22324   /* We have either a class or a category on our hands.  */
22325   if (categ)
22326     objc_start_category_interface (name, categ, protos, attributes);
22327   else
22328     {
22329       objc_start_class_interface (name, super, protos, attributes);
22330       /* Handle instance variable declarations, if any.  */
22331       cp_parser_objc_class_ivars (parser);
22332       objc_continue_interface ();
22333     }
22334
22335   cp_parser_objc_method_prototype_list (parser);
22336 }
22337
22338 /* Parse an Objective-C class implementation.  */
22339
22340 static void
22341 cp_parser_objc_class_implementation (cp_parser* parser)
22342 {
22343   tree name, super, categ;
22344
22345   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22346   name = cp_parser_identifier (parser);
22347   if (name == error_mark_node)
22348     {
22349       /* It's hard to recover because even if valid @implementation
22350          stuff is to follow, we can't compile it (or validate it) if
22351          we don't even know which class it refers to.  Let's assume
22352          this was a stray '@implementation' token in the stream and
22353          skip it.
22354       */
22355       return;
22356     }
22357   cp_parser_objc_superclass_or_category (parser, &super, &categ);
22358
22359   /* We have either a class or a category on our hands.  */
22360   if (categ)
22361     objc_start_category_implementation (name, categ);
22362   else
22363     {
22364       objc_start_class_implementation (name, super);
22365       /* Handle instance variable declarations, if any.  */
22366       cp_parser_objc_class_ivars (parser);
22367       objc_continue_implementation ();
22368     }
22369
22370   cp_parser_objc_method_definition_list (parser);
22371 }
22372
22373 /* Consume the @end token and finish off the implementation.  */
22374
22375 static void
22376 cp_parser_objc_end_implementation (cp_parser* parser)
22377 {
22378   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22379   objc_finish_implementation ();
22380 }
22381
22382 /* Parse an Objective-C declaration.  */
22383
22384 static void
22385 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22386 {
22387   /* Try to figure out what kind of declaration is present.  */
22388   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22389
22390   if (attributes)
22391     switch (kwd->keyword)
22392       {
22393         case RID_AT_ALIAS:
22394         case RID_AT_CLASS:
22395         case RID_AT_END:
22396           error_at (kwd->location, "attributes may not be specified before"
22397                     " the %<@%D%> Objective-C++ keyword",
22398                     kwd->u.value);
22399           attributes = NULL;
22400           break;
22401         case RID_AT_IMPLEMENTATION:
22402           warning_at (kwd->location, OPT_Wattributes,
22403                       "prefix attributes are ignored before %<@%D%>",
22404                       kwd->u.value);
22405           attributes = NULL;
22406         default:
22407           break;
22408       }
22409
22410   switch (kwd->keyword)
22411     {
22412     case RID_AT_ALIAS:
22413       cp_parser_objc_alias_declaration (parser);
22414       break;
22415     case RID_AT_CLASS:
22416       cp_parser_objc_class_declaration (parser);
22417       break;
22418     case RID_AT_PROTOCOL:
22419       cp_parser_objc_protocol_declaration (parser, attributes);
22420       break;
22421     case RID_AT_INTERFACE:
22422       cp_parser_objc_class_interface (parser, attributes);
22423       break;
22424     case RID_AT_IMPLEMENTATION:
22425       cp_parser_objc_class_implementation (parser);
22426       break;
22427     case RID_AT_END:
22428       cp_parser_objc_end_implementation (parser);
22429       break;
22430     default:
22431       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22432                 kwd->u.value);
22433       cp_parser_skip_to_end_of_block_or_statement (parser);
22434     }
22435 }
22436
22437 /* Parse an Objective-C try-catch-finally statement.
22438
22439    objc-try-catch-finally-stmt:
22440      @try compound-statement objc-catch-clause-seq [opt]
22441        objc-finally-clause [opt]
22442
22443    objc-catch-clause-seq:
22444      objc-catch-clause objc-catch-clause-seq [opt]
22445
22446    objc-catch-clause:
22447      @catch ( exception-declaration ) compound-statement
22448
22449    objc-finally-clause
22450      @finally compound-statement
22451
22452    Returns NULL_TREE.  */
22453
22454 static tree
22455 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22456   location_t location;
22457   tree stmt;
22458
22459   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22460   location = cp_lexer_peek_token (parser->lexer)->location;
22461   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22462      node, lest it get absorbed into the surrounding block.  */
22463   stmt = push_stmt_list ();
22464   cp_parser_compound_statement (parser, NULL, false);
22465   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22466
22467   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22468     {
22469       cp_parameter_declarator *parmdecl;
22470       tree parm;
22471
22472       cp_lexer_consume_token (parser->lexer);
22473       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22474       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22475       parm = grokdeclarator (parmdecl->declarator,
22476                              &parmdecl->decl_specifiers,
22477                              PARM, /*initialized=*/0,
22478                              /*attrlist=*/NULL);
22479       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22480       objc_begin_catch_clause (parm);
22481       cp_parser_compound_statement (parser, NULL, false);
22482       objc_finish_catch_clause ();
22483     }
22484
22485   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22486     {
22487       cp_lexer_consume_token (parser->lexer);
22488       location = cp_lexer_peek_token (parser->lexer)->location;
22489       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22490          node, lest it get absorbed into the surrounding block.  */
22491       stmt = push_stmt_list ();
22492       cp_parser_compound_statement (parser, NULL, false);
22493       objc_build_finally_clause (location, pop_stmt_list (stmt));
22494     }
22495
22496   return objc_finish_try_stmt ();
22497 }
22498
22499 /* Parse an Objective-C synchronized statement.
22500
22501    objc-synchronized-stmt:
22502      @synchronized ( expression ) compound-statement
22503
22504    Returns NULL_TREE.  */
22505
22506 static tree
22507 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22508   location_t location;
22509   tree lock, stmt;
22510
22511   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22512
22513   location = cp_lexer_peek_token (parser->lexer)->location;
22514   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22515   lock = cp_parser_expression (parser, false, NULL);
22516   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22517
22518   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22519      node, lest it get absorbed into the surrounding block.  */
22520   stmt = push_stmt_list ();
22521   cp_parser_compound_statement (parser, NULL, false);
22522
22523   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22524 }
22525
22526 /* Parse an Objective-C throw statement.
22527
22528    objc-throw-stmt:
22529      @throw assignment-expression [opt] ;
22530
22531    Returns a constructed '@throw' statement.  */
22532
22533 static tree
22534 cp_parser_objc_throw_statement (cp_parser *parser) {
22535   tree expr = NULL_TREE;
22536   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22537
22538   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22539
22540   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22541     expr = cp_parser_assignment_expression (parser, false, NULL);
22542
22543   cp_parser_consume_semicolon_at_end_of_statement (parser);
22544
22545   return objc_build_throw_stmt (loc, expr);
22546 }
22547
22548 /* Parse an Objective-C statement.  */
22549
22550 static tree
22551 cp_parser_objc_statement (cp_parser * parser) {
22552   /* Try to figure out what kind of declaration is present.  */
22553   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22554
22555   switch (kwd->keyword)
22556     {
22557     case RID_AT_TRY:
22558       return cp_parser_objc_try_catch_finally_statement (parser);
22559     case RID_AT_SYNCHRONIZED:
22560       return cp_parser_objc_synchronized_statement (parser);
22561     case RID_AT_THROW:
22562       return cp_parser_objc_throw_statement (parser);
22563     default:
22564       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22565                kwd->u.value);
22566       cp_parser_skip_to_end_of_block_or_statement (parser);
22567     }
22568
22569   return error_mark_node;
22570 }
22571
22572 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22573    look ahead to see if an objc keyword follows the attributes.  This
22574    is to detect the use of prefix attributes on ObjC @interface and 
22575    @protocol.  */
22576
22577 static bool
22578 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22579 {
22580   cp_lexer_save_tokens (parser->lexer);
22581   *attrib = cp_parser_attributes_opt (parser);
22582   gcc_assert (*attrib);
22583   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22584     {
22585       cp_lexer_commit_tokens (parser->lexer);
22586       return true;
22587     }
22588   cp_lexer_rollback_tokens (parser->lexer);
22589   return false;  
22590 }
22591
22592 /* This routine is a minimal replacement for
22593    c_parser_struct_declaration () used when parsing the list of
22594    types/names or ObjC++ properties.  For example, when parsing the
22595    code
22596
22597    @property (readonly) int a, b, c;
22598
22599    this function is responsible for parsing "int a, int b, int c" and
22600    returning the declarations as CHAIN of DECLs.
22601
22602    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
22603    similar parsing.  */
22604 static tree
22605 cp_parser_objc_struct_declaration (cp_parser *parser)
22606 {
22607   tree decls = NULL_TREE;
22608   cp_decl_specifier_seq declspecs;
22609   int decl_class_or_enum_p;
22610   tree prefix_attributes;
22611
22612   cp_parser_decl_specifier_seq (parser,
22613                                 CP_PARSER_FLAGS_NONE,
22614                                 &declspecs,
22615                                 &decl_class_or_enum_p);
22616
22617   if (declspecs.type == error_mark_node)
22618     return error_mark_node;
22619
22620   /* auto, register, static, extern, mutable.  */
22621   if (declspecs.storage_class != sc_none)
22622     {
22623       cp_parser_error (parser, "invalid type for property");
22624       declspecs.storage_class = sc_none;
22625     }
22626   
22627   /* __thread.  */
22628   if (declspecs.specs[(int) ds_thread])
22629     {
22630       cp_parser_error (parser, "invalid type for property");
22631       declspecs.specs[(int) ds_thread] = 0;
22632     }
22633   
22634   /* typedef.  */
22635   if (declspecs.specs[(int) ds_typedef])
22636     {
22637       cp_parser_error (parser, "invalid type for property");
22638       declspecs.specs[(int) ds_typedef] = 0;
22639     }
22640
22641   prefix_attributes = declspecs.attributes;
22642   declspecs.attributes = NULL_TREE;
22643
22644   /* Keep going until we hit the `;' at the end of the declaration. */
22645   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22646     {
22647       tree attributes, first_attribute, decl;
22648       cp_declarator *declarator;
22649       cp_token *token;
22650
22651       /* Parse the declarator.  */
22652       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22653                                          NULL, NULL, false);
22654
22655       /* Look for attributes that apply to the ivar.  */
22656       attributes = cp_parser_attributes_opt (parser);
22657       /* Remember which attributes are prefix attributes and
22658          which are not.  */
22659       first_attribute = attributes;
22660       /* Combine the attributes.  */
22661       attributes = chainon (prefix_attributes, attributes);
22662       
22663       decl = grokfield (declarator, &declspecs,
22664                         NULL_TREE, /*init_const_expr_p=*/false,
22665                         NULL_TREE, attributes);
22666
22667       if (decl == error_mark_node || decl == NULL_TREE)
22668         return error_mark_node;
22669       
22670       /* Reset PREFIX_ATTRIBUTES.  */
22671       while (attributes && TREE_CHAIN (attributes) != first_attribute)
22672         attributes = TREE_CHAIN (attributes);
22673       if (attributes)
22674         TREE_CHAIN (attributes) = NULL_TREE;
22675
22676       DECL_CHAIN (decl) = decls;
22677       decls = decl;
22678
22679       token = cp_lexer_peek_token (parser->lexer);
22680       if (token->type == CPP_COMMA)
22681         {
22682           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22683           continue;
22684         }
22685       else
22686         break;
22687     }
22688   return decls;
22689 }
22690
22691 /* Parse an Objective-C @property declaration.  The syntax is:
22692
22693    objc-property-declaration:
22694      '@property' objc-property-attributes[opt] struct-declaration ;
22695
22696    objc-property-attributes:
22697     '(' objc-property-attribute-list ')'
22698
22699    objc-property-attribute-list:
22700      objc-property-attribute
22701      objc-property-attribute-list, objc-property-attribute
22702
22703    objc-property-attribute
22704      'getter' = identifier
22705      'setter' = identifier
22706      'readonly'
22707      'readwrite'
22708      'assign'
22709      'retain'
22710      'copy'
22711      'nonatomic'
22712
22713   For example:
22714     @property NSString *name;
22715     @property (readonly) id object;
22716     @property (retain, nonatomic, getter=getTheName) id name;
22717     @property int a, b, c;
22718
22719    PS: This function is identical to
22720    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
22721 static void 
22722 cp_parser_objc_at_property_declaration (cp_parser *parser)
22723 {
22724   /* The following variables hold the attributes of the properties as
22725      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
22726      seen.  When we see an attribute, we set them to 'true' (if they
22727      are boolean properties) or to the identifier (if they have an
22728      argument, ie, for getter and setter).  Note that here we only
22729      parse the list of attributes, check the syntax and accumulate the
22730      attributes that we find.  objc_add_property_declaration() will
22731      then process the information.  */
22732   bool property_assign = false;
22733   bool property_copy = false;
22734   tree property_getter_ident = NULL_TREE;
22735   bool property_nonatomic = false;
22736   bool property_readonly = false;
22737   bool property_readwrite = false;
22738   bool property_retain = false;
22739   tree property_setter_ident = NULL_TREE;
22740
22741   /* 'properties' is the list of properties that we read.  Usually a
22742      single one, but maybe more (eg, in "@property int a, b, c;" there
22743      are three).  */
22744   tree properties;
22745   location_t loc;
22746
22747   loc = cp_lexer_peek_token (parser->lexer)->location;
22748
22749   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
22750
22751   /* Parse the optional attribute list...  */
22752   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22753     {
22754       /* Eat the '('.  */
22755       cp_lexer_consume_token (parser->lexer);
22756
22757       while (true)
22758         {
22759           bool syntax_error = false;
22760           cp_token *token = cp_lexer_peek_token (parser->lexer);
22761           enum rid keyword;
22762
22763           if (token->type != CPP_NAME)
22764             {
22765               cp_parser_error (parser, "expected identifier");
22766               break;
22767             }
22768           keyword = C_RID_CODE (token->u.value);
22769           cp_lexer_consume_token (parser->lexer);
22770           switch (keyword)
22771             {
22772             case RID_ASSIGN:    property_assign = true;    break;
22773             case RID_COPY:      property_copy = true;      break;
22774             case RID_NONATOMIC: property_nonatomic = true; break;
22775             case RID_READONLY:  property_readonly = true;  break;
22776             case RID_READWRITE: property_readwrite = true; break;
22777             case RID_RETAIN:    property_retain = true;    break;
22778
22779             case RID_GETTER:
22780             case RID_SETTER:
22781               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22782                 {
22783                   cp_parser_error (parser,
22784                                    "getter/setter/ivar attribute must be followed by %<=%>");
22785                   syntax_error = true;
22786                   break;
22787                 }
22788               cp_lexer_consume_token (parser->lexer); /* eat the = */
22789               if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22790                 {
22791                   cp_parser_error (parser, "expected identifier");
22792                   syntax_error = true;
22793                   break;
22794                 }
22795               if (keyword == RID_SETTER)
22796                 {
22797                   if (property_setter_ident != NULL_TREE)
22798                     cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
22799                   else
22800                     property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22801                   cp_lexer_consume_token (parser->lexer);
22802                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22803                     cp_parser_error (parser, "setter name must terminate with %<:%>");
22804                   else
22805                     cp_lexer_consume_token (parser->lexer);
22806                 }
22807               else
22808                 {
22809                   if (property_getter_ident != NULL_TREE)
22810                     cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
22811                   else
22812                     property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22813                   cp_lexer_consume_token (parser->lexer);
22814                 }
22815               break;
22816             default:
22817               cp_parser_error (parser, "unknown property attribute");
22818               syntax_error = true;
22819               break;
22820             }
22821
22822           if (syntax_error)
22823             break;
22824           
22825           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22826             cp_lexer_consume_token (parser->lexer);
22827           else
22828             break;
22829         }
22830
22831       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22832         {
22833           cp_parser_skip_to_closing_parenthesis (parser,
22834                                                  /*recovering=*/true,
22835                                                  /*or_comma=*/false,
22836                                                  /*consume_paren=*/true);
22837         }
22838     }
22839
22840   /* ... and the property declaration(s).  */
22841   properties = cp_parser_objc_struct_declaration (parser);
22842
22843   if (properties == error_mark_node)
22844     {
22845       cp_parser_skip_to_end_of_statement (parser);
22846       /* If the next token is now a `;', consume it.  */
22847       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22848         cp_lexer_consume_token (parser->lexer);
22849       return;
22850     }
22851
22852   if (properties == NULL_TREE)
22853     cp_parser_error (parser, "expected identifier");
22854   else
22855     {
22856       /* Comma-separated properties are chained together in
22857          reverse order; add them one by one.  */
22858       properties = nreverse (properties);
22859       
22860       for (; properties; properties = TREE_CHAIN (properties))
22861         objc_add_property_declaration (loc, copy_node (properties),
22862                                        property_readonly, property_readwrite,
22863                                        property_assign, property_retain,
22864                                        property_copy, property_nonatomic,
22865                                        property_getter_ident, property_setter_ident);
22866     }
22867   
22868   cp_parser_consume_semicolon_at_end_of_statement (parser);
22869 }
22870
22871 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
22872
22873    objc-synthesize-declaration:
22874      @synthesize objc-synthesize-identifier-list ;
22875
22876    objc-synthesize-identifier-list:
22877      objc-synthesize-identifier
22878      objc-synthesize-identifier-list, objc-synthesize-identifier
22879
22880    objc-synthesize-identifier
22881      identifier
22882      identifier = identifier
22883
22884   For example:
22885     @synthesize MyProperty;
22886     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
22887
22888   PS: This function is identical to c_parser_objc_at_synthesize_declaration
22889   for C.  Keep them in sync.
22890 */
22891 static void 
22892 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
22893 {
22894   tree list = NULL_TREE;
22895   location_t loc;
22896   loc = cp_lexer_peek_token (parser->lexer)->location;
22897
22898   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
22899   while (true)
22900     {
22901       tree property, ivar;
22902       property = cp_parser_identifier (parser);
22903       if (property == error_mark_node)
22904         {
22905           cp_parser_consume_semicolon_at_end_of_statement (parser);
22906           return;
22907         }
22908       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22909         {
22910           cp_lexer_consume_token (parser->lexer);
22911           ivar = cp_parser_identifier (parser);
22912           if (ivar == error_mark_node)
22913             {
22914               cp_parser_consume_semicolon_at_end_of_statement (parser);
22915               return;
22916             }
22917         }
22918       else
22919         ivar = NULL_TREE;
22920       list = chainon (list, build_tree_list (ivar, property));
22921       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22922         cp_lexer_consume_token (parser->lexer);
22923       else
22924         break;
22925     }
22926   cp_parser_consume_semicolon_at_end_of_statement (parser);
22927   objc_add_synthesize_declaration (loc, list);
22928 }
22929
22930 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
22931
22932    objc-dynamic-declaration:
22933      @dynamic identifier-list ;
22934
22935    For example:
22936      @dynamic MyProperty;
22937      @dynamic MyProperty, AnotherProperty;
22938
22939   PS: This function is identical to c_parser_objc_at_dynamic_declaration
22940   for C.  Keep them in sync.
22941 */
22942 static void 
22943 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
22944 {
22945   tree list = NULL_TREE;
22946   location_t loc;
22947   loc = cp_lexer_peek_token (parser->lexer)->location;
22948
22949   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
22950   while (true)
22951     {
22952       tree property;
22953       property = cp_parser_identifier (parser);
22954       if (property == error_mark_node)
22955         {
22956           cp_parser_consume_semicolon_at_end_of_statement (parser);
22957           return;
22958         }
22959       list = chainon (list, build_tree_list (NULL, property));
22960       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22961         cp_lexer_consume_token (parser->lexer);
22962       else
22963         break;
22964     }
22965   cp_parser_consume_semicolon_at_end_of_statement (parser);
22966   objc_add_dynamic_declaration (loc, list);
22967 }
22968
22969 \f
22970 /* OpenMP 2.5 parsing routines.  */
22971
22972 /* Returns name of the next clause.
22973    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
22974    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
22975    returned and the token is consumed.  */
22976
22977 static pragma_omp_clause
22978 cp_parser_omp_clause_name (cp_parser *parser)
22979 {
22980   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
22981
22982   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
22983     result = PRAGMA_OMP_CLAUSE_IF;
22984   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
22985     result = PRAGMA_OMP_CLAUSE_DEFAULT;
22986   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
22987     result = PRAGMA_OMP_CLAUSE_PRIVATE;
22988   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22989     {
22990       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22991       const char *p = IDENTIFIER_POINTER (id);
22992
22993       switch (p[0])
22994         {
22995         case 'c':
22996           if (!strcmp ("collapse", p))
22997             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
22998           else if (!strcmp ("copyin", p))
22999             result = PRAGMA_OMP_CLAUSE_COPYIN;
23000           else if (!strcmp ("copyprivate", p))
23001             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23002           break;
23003         case 'f':
23004           if (!strcmp ("firstprivate", p))
23005             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23006           break;
23007         case 'l':
23008           if (!strcmp ("lastprivate", p))
23009             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23010           break;
23011         case 'n':
23012           if (!strcmp ("nowait", p))
23013             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23014           else if (!strcmp ("num_threads", p))
23015             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23016           break;
23017         case 'o':
23018           if (!strcmp ("ordered", p))
23019             result = PRAGMA_OMP_CLAUSE_ORDERED;
23020           break;
23021         case 'r':
23022           if (!strcmp ("reduction", p))
23023             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23024           break;
23025         case 's':
23026           if (!strcmp ("schedule", p))
23027             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23028           else if (!strcmp ("shared", p))
23029             result = PRAGMA_OMP_CLAUSE_SHARED;
23030           break;
23031         case 'u':
23032           if (!strcmp ("untied", p))
23033             result = PRAGMA_OMP_CLAUSE_UNTIED;
23034           break;
23035         }
23036     }
23037
23038   if (result != PRAGMA_OMP_CLAUSE_NONE)
23039     cp_lexer_consume_token (parser->lexer);
23040
23041   return result;
23042 }
23043
23044 /* Validate that a clause of the given type does not already exist.  */
23045
23046 static void
23047 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23048                            const char *name, location_t location)
23049 {
23050   tree c;
23051
23052   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23053     if (OMP_CLAUSE_CODE (c) == code)
23054       {
23055         error_at (location, "too many %qs clauses", name);
23056         break;
23057       }
23058 }
23059
23060 /* OpenMP 2.5:
23061    variable-list:
23062      identifier
23063      variable-list , identifier
23064
23065    In addition, we match a closing parenthesis.  An opening parenthesis
23066    will have been consumed by the caller.
23067
23068    If KIND is nonzero, create the appropriate node and install the decl
23069    in OMP_CLAUSE_DECL and add the node to the head of the list.
23070
23071    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23072    return the list created.  */
23073
23074 static tree
23075 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23076                                 tree list)
23077 {
23078   cp_token *token;
23079   while (1)
23080     {
23081       tree name, decl;
23082
23083       token = cp_lexer_peek_token (parser->lexer);
23084       name = cp_parser_id_expression (parser, /*template_p=*/false,
23085                                       /*check_dependency_p=*/true,
23086                                       /*template_p=*/NULL,
23087                                       /*declarator_p=*/false,
23088                                       /*optional_p=*/false);
23089       if (name == error_mark_node)
23090         goto skip_comma;
23091
23092       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23093       if (decl == error_mark_node)
23094         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23095                                      token->location);
23096       else if (kind != 0)
23097         {
23098           tree u = build_omp_clause (token->location, kind);
23099           OMP_CLAUSE_DECL (u) = decl;
23100           OMP_CLAUSE_CHAIN (u) = list;
23101           list = u;
23102         }
23103       else
23104         list = tree_cons (decl, NULL_TREE, list);
23105
23106     get_comma:
23107       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23108         break;
23109       cp_lexer_consume_token (parser->lexer);
23110     }
23111
23112   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23113     {
23114       int ending;
23115
23116       /* Try to resync to an unnested comma.  Copied from
23117          cp_parser_parenthesized_expression_list.  */
23118     skip_comma:
23119       ending = cp_parser_skip_to_closing_parenthesis (parser,
23120                                                       /*recovering=*/true,
23121                                                       /*or_comma=*/true,
23122                                                       /*consume_paren=*/true);
23123       if (ending < 0)
23124         goto get_comma;
23125     }
23126
23127   return list;
23128 }
23129
23130 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23131    common case for omp clauses.  */
23132
23133 static tree
23134 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23135 {
23136   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23137     return cp_parser_omp_var_list_no_open (parser, kind, list);
23138   return list;
23139 }
23140
23141 /* OpenMP 3.0:
23142    collapse ( constant-expression ) */
23143
23144 static tree
23145 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23146 {
23147   tree c, num;
23148   location_t loc;
23149   HOST_WIDE_INT n;
23150
23151   loc = cp_lexer_peek_token (parser->lexer)->location;
23152   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23153     return list;
23154
23155   num = cp_parser_constant_expression (parser, false, NULL);
23156
23157   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23158     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23159                                            /*or_comma=*/false,
23160                                            /*consume_paren=*/true);
23161
23162   if (num == error_mark_node)
23163     return list;
23164   num = fold_non_dependent_expr (num);
23165   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23166       || !host_integerp (num, 0)
23167       || (n = tree_low_cst (num, 0)) <= 0
23168       || (int) n != n)
23169     {
23170       error_at (loc, "collapse argument needs positive constant integer expression");
23171       return list;
23172     }
23173
23174   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23175   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23176   OMP_CLAUSE_CHAIN (c) = list;
23177   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23178
23179   return c;
23180 }
23181
23182 /* OpenMP 2.5:
23183    default ( shared | none ) */
23184
23185 static tree
23186 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23187 {
23188   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23189   tree c;
23190
23191   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23192     return list;
23193   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23194     {
23195       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23196       const char *p = IDENTIFIER_POINTER (id);
23197
23198       switch (p[0])
23199         {
23200         case 'n':
23201           if (strcmp ("none", p) != 0)
23202             goto invalid_kind;
23203           kind = OMP_CLAUSE_DEFAULT_NONE;
23204           break;
23205
23206         case 's':
23207           if (strcmp ("shared", p) != 0)
23208             goto invalid_kind;
23209           kind = OMP_CLAUSE_DEFAULT_SHARED;
23210           break;
23211
23212         default:
23213           goto invalid_kind;
23214         }
23215
23216       cp_lexer_consume_token (parser->lexer);
23217     }
23218   else
23219     {
23220     invalid_kind:
23221       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23222     }
23223
23224   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23225     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23226                                            /*or_comma=*/false,
23227                                            /*consume_paren=*/true);
23228
23229   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23230     return list;
23231
23232   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23233   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23234   OMP_CLAUSE_CHAIN (c) = list;
23235   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23236
23237   return c;
23238 }
23239
23240 /* OpenMP 2.5:
23241    if ( expression ) */
23242
23243 static tree
23244 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23245 {
23246   tree t, c;
23247
23248   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23249     return list;
23250
23251   t = cp_parser_condition (parser);
23252
23253   if (t == error_mark_node
23254       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23255     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23256                                            /*or_comma=*/false,
23257                                            /*consume_paren=*/true);
23258
23259   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23260
23261   c = build_omp_clause (location, OMP_CLAUSE_IF);
23262   OMP_CLAUSE_IF_EXPR (c) = t;
23263   OMP_CLAUSE_CHAIN (c) = list;
23264
23265   return c;
23266 }
23267
23268 /* OpenMP 2.5:
23269    nowait */
23270
23271 static tree
23272 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23273                              tree list, location_t location)
23274 {
23275   tree c;
23276
23277   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23278
23279   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23280   OMP_CLAUSE_CHAIN (c) = list;
23281   return c;
23282 }
23283
23284 /* OpenMP 2.5:
23285    num_threads ( expression ) */
23286
23287 static tree
23288 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23289                                   location_t location)
23290 {
23291   tree t, c;
23292
23293   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23294     return list;
23295
23296   t = cp_parser_expression (parser, false, NULL);
23297
23298   if (t == error_mark_node
23299       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23300     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23301                                            /*or_comma=*/false,
23302                                            /*consume_paren=*/true);
23303
23304   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23305                              "num_threads", location);
23306
23307   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23308   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23309   OMP_CLAUSE_CHAIN (c) = list;
23310
23311   return c;
23312 }
23313
23314 /* OpenMP 2.5:
23315    ordered */
23316
23317 static tree
23318 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23319                               tree list, location_t location)
23320 {
23321   tree c;
23322
23323   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23324                              "ordered", location);
23325
23326   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23327   OMP_CLAUSE_CHAIN (c) = list;
23328   return c;
23329 }
23330
23331 /* OpenMP 2.5:
23332    reduction ( reduction-operator : variable-list )
23333
23334    reduction-operator:
23335      One of: + * - & ^ | && || */
23336
23337 static tree
23338 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23339 {
23340   enum tree_code code;
23341   tree nlist, c;
23342
23343   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23344     return list;
23345
23346   switch (cp_lexer_peek_token (parser->lexer)->type)
23347     {
23348     case CPP_PLUS:
23349       code = PLUS_EXPR;
23350       break;
23351     case CPP_MULT:
23352       code = MULT_EXPR;
23353       break;
23354     case CPP_MINUS:
23355       code = MINUS_EXPR;
23356       break;
23357     case CPP_AND:
23358       code = BIT_AND_EXPR;
23359       break;
23360     case CPP_XOR:
23361       code = BIT_XOR_EXPR;
23362       break;
23363     case CPP_OR:
23364       code = BIT_IOR_EXPR;
23365       break;
23366     case CPP_AND_AND:
23367       code = TRUTH_ANDIF_EXPR;
23368       break;
23369     case CPP_OR_OR:
23370       code = TRUTH_ORIF_EXPR;
23371       break;
23372     default:
23373       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23374                                "%<|%>, %<&&%>, or %<||%>");
23375     resync_fail:
23376       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23377                                              /*or_comma=*/false,
23378                                              /*consume_paren=*/true);
23379       return list;
23380     }
23381   cp_lexer_consume_token (parser->lexer);
23382
23383   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23384     goto resync_fail;
23385
23386   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23387   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23388     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23389
23390   return nlist;
23391 }
23392
23393 /* OpenMP 2.5:
23394    schedule ( schedule-kind )
23395    schedule ( schedule-kind , expression )
23396
23397    schedule-kind:
23398      static | dynamic | guided | runtime | auto  */
23399
23400 static tree
23401 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23402 {
23403   tree c, t;
23404
23405   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23406     return list;
23407
23408   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23409
23410   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23411     {
23412       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23413       const char *p = IDENTIFIER_POINTER (id);
23414
23415       switch (p[0])
23416         {
23417         case 'd':
23418           if (strcmp ("dynamic", p) != 0)
23419             goto invalid_kind;
23420           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23421           break;
23422
23423         case 'g':
23424           if (strcmp ("guided", p) != 0)
23425             goto invalid_kind;
23426           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23427           break;
23428
23429         case 'r':
23430           if (strcmp ("runtime", p) != 0)
23431             goto invalid_kind;
23432           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23433           break;
23434
23435         default:
23436           goto invalid_kind;
23437         }
23438     }
23439   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23440     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23441   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23442     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23443   else
23444     goto invalid_kind;
23445   cp_lexer_consume_token (parser->lexer);
23446
23447   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23448     {
23449       cp_token *token;
23450       cp_lexer_consume_token (parser->lexer);
23451
23452       token = cp_lexer_peek_token (parser->lexer);
23453       t = cp_parser_assignment_expression (parser, false, NULL);
23454
23455       if (t == error_mark_node)
23456         goto resync_fail;
23457       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23458         error_at (token->location, "schedule %<runtime%> does not take "
23459                   "a %<chunk_size%> parameter");
23460       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23461         error_at (token->location, "schedule %<auto%> does not take "
23462                   "a %<chunk_size%> parameter");
23463       else
23464         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23465
23466       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23467         goto resync_fail;
23468     }
23469   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23470     goto resync_fail;
23471
23472   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23473   OMP_CLAUSE_CHAIN (c) = list;
23474   return c;
23475
23476  invalid_kind:
23477   cp_parser_error (parser, "invalid schedule kind");
23478  resync_fail:
23479   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23480                                          /*or_comma=*/false,
23481                                          /*consume_paren=*/true);
23482   return list;
23483 }
23484
23485 /* OpenMP 3.0:
23486    untied */
23487
23488 static tree
23489 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23490                              tree list, location_t location)
23491 {
23492   tree c;
23493
23494   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23495
23496   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23497   OMP_CLAUSE_CHAIN (c) = list;
23498   return c;
23499 }
23500
23501 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23502    is a bitmask in MASK.  Return the list of clauses found; the result
23503    of clause default goes in *pdefault.  */
23504
23505 static tree
23506 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23507                            const char *where, cp_token *pragma_tok)
23508 {
23509   tree clauses = NULL;
23510   bool first = true;
23511   cp_token *token = NULL;
23512
23513   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23514     {
23515       pragma_omp_clause c_kind;
23516       const char *c_name;
23517       tree prev = clauses;
23518
23519       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23520         cp_lexer_consume_token (parser->lexer);
23521
23522       token = cp_lexer_peek_token (parser->lexer);
23523       c_kind = cp_parser_omp_clause_name (parser);
23524       first = false;
23525
23526       switch (c_kind)
23527         {
23528         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23529           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23530                                                    token->location);
23531           c_name = "collapse";
23532           break;
23533         case PRAGMA_OMP_CLAUSE_COPYIN:
23534           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23535           c_name = "copyin";
23536           break;
23537         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23538           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23539                                             clauses);
23540           c_name = "copyprivate";
23541           break;
23542         case PRAGMA_OMP_CLAUSE_DEFAULT:
23543           clauses = cp_parser_omp_clause_default (parser, clauses,
23544                                                   token->location);
23545           c_name = "default";
23546           break;
23547         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23548           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23549                                             clauses);
23550           c_name = "firstprivate";
23551           break;
23552         case PRAGMA_OMP_CLAUSE_IF:
23553           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23554           c_name = "if";
23555           break;
23556         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23557           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23558                                             clauses);
23559           c_name = "lastprivate";
23560           break;
23561         case PRAGMA_OMP_CLAUSE_NOWAIT:
23562           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23563           c_name = "nowait";
23564           break;
23565         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23566           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23567                                                       token->location);
23568           c_name = "num_threads";
23569           break;
23570         case PRAGMA_OMP_CLAUSE_ORDERED:
23571           clauses = cp_parser_omp_clause_ordered (parser, clauses,
23572                                                   token->location);
23573           c_name = "ordered";
23574           break;
23575         case PRAGMA_OMP_CLAUSE_PRIVATE:
23576           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23577                                             clauses);
23578           c_name = "private";
23579           break;
23580         case PRAGMA_OMP_CLAUSE_REDUCTION:
23581           clauses = cp_parser_omp_clause_reduction (parser, clauses);
23582           c_name = "reduction";
23583           break;
23584         case PRAGMA_OMP_CLAUSE_SCHEDULE:
23585           clauses = cp_parser_omp_clause_schedule (parser, clauses,
23586                                                    token->location);
23587           c_name = "schedule";
23588           break;
23589         case PRAGMA_OMP_CLAUSE_SHARED:
23590           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23591                                             clauses);
23592           c_name = "shared";
23593           break;
23594         case PRAGMA_OMP_CLAUSE_UNTIED:
23595           clauses = cp_parser_omp_clause_untied (parser, clauses,
23596                                                  token->location);
23597           c_name = "nowait";
23598           break;
23599         default:
23600           cp_parser_error (parser, "expected %<#pragma omp%> clause");
23601           goto saw_error;
23602         }
23603
23604       if (((mask >> c_kind) & 1) == 0)
23605         {
23606           /* Remove the invalid clause(s) from the list to avoid
23607              confusing the rest of the compiler.  */
23608           clauses = prev;
23609           error_at (token->location, "%qs is not valid for %qs", c_name, where);
23610         }
23611     }
23612  saw_error:
23613   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23614   return finish_omp_clauses (clauses);
23615 }
23616
23617 /* OpenMP 2.5:
23618    structured-block:
23619      statement
23620
23621    In practice, we're also interested in adding the statement to an
23622    outer node.  So it is convenient if we work around the fact that
23623    cp_parser_statement calls add_stmt.  */
23624
23625 static unsigned
23626 cp_parser_begin_omp_structured_block (cp_parser *parser)
23627 {
23628   unsigned save = parser->in_statement;
23629
23630   /* Only move the values to IN_OMP_BLOCK if they weren't false.
23631      This preserves the "not within loop or switch" style error messages
23632      for nonsense cases like
23633         void foo() {
23634         #pragma omp single
23635           break;
23636         }
23637   */
23638   if (parser->in_statement)
23639     parser->in_statement = IN_OMP_BLOCK;
23640
23641   return save;
23642 }
23643
23644 static void
23645 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23646 {
23647   parser->in_statement = save;
23648 }
23649
23650 static tree
23651 cp_parser_omp_structured_block (cp_parser *parser)
23652 {
23653   tree stmt = begin_omp_structured_block ();
23654   unsigned int save = cp_parser_begin_omp_structured_block (parser);
23655
23656   cp_parser_statement (parser, NULL_TREE, false, NULL);
23657
23658   cp_parser_end_omp_structured_block (parser, save);
23659   return finish_omp_structured_block (stmt);
23660 }
23661
23662 /* OpenMP 2.5:
23663    # pragma omp atomic new-line
23664      expression-stmt
23665
23666    expression-stmt:
23667      x binop= expr | x++ | ++x | x-- | --x
23668    binop:
23669      +, *, -, /, &, ^, |, <<, >>
23670
23671   where x is an lvalue expression with scalar type.  */
23672
23673 static void
23674 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23675 {
23676   tree lhs, rhs;
23677   enum tree_code code;
23678
23679   cp_parser_require_pragma_eol (parser, pragma_tok);
23680
23681   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23682                                     /*cast_p=*/false, NULL);
23683   switch (TREE_CODE (lhs))
23684     {
23685     case ERROR_MARK:
23686       goto saw_error;
23687
23688     case PREINCREMENT_EXPR:
23689     case POSTINCREMENT_EXPR:
23690       lhs = TREE_OPERAND (lhs, 0);
23691       code = PLUS_EXPR;
23692       rhs = integer_one_node;
23693       break;
23694
23695     case PREDECREMENT_EXPR:
23696     case POSTDECREMENT_EXPR:
23697       lhs = TREE_OPERAND (lhs, 0);
23698       code = MINUS_EXPR;
23699       rhs = integer_one_node;
23700       break;
23701
23702     case COMPOUND_EXPR:
23703       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23704          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23705          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23706          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23707          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23708                                              (TREE_OPERAND (lhs, 1), 0), 0)))
23709             == BOOLEAN_TYPE)
23710        /* Undo effects of boolean_increment for post {in,de}crement.  */
23711        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23712       /* FALLTHRU */
23713     case MODIFY_EXPR:
23714       if (TREE_CODE (lhs) == MODIFY_EXPR
23715          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23716        {
23717          /* Undo effects of boolean_increment.  */
23718          if (integer_onep (TREE_OPERAND (lhs, 1)))
23719            {
23720              /* This is pre or post increment.  */
23721              rhs = TREE_OPERAND (lhs, 1);
23722              lhs = TREE_OPERAND (lhs, 0);
23723              code = NOP_EXPR;
23724              break;
23725            }
23726        }
23727       /* FALLTHRU */
23728     default:
23729       switch (cp_lexer_peek_token (parser->lexer)->type)
23730         {
23731         case CPP_MULT_EQ:
23732           code = MULT_EXPR;
23733           break;
23734         case CPP_DIV_EQ:
23735           code = TRUNC_DIV_EXPR;
23736           break;
23737         case CPP_PLUS_EQ:
23738           code = PLUS_EXPR;
23739           break;
23740         case CPP_MINUS_EQ:
23741           code = MINUS_EXPR;
23742           break;
23743         case CPP_LSHIFT_EQ:
23744           code = LSHIFT_EXPR;
23745           break;
23746         case CPP_RSHIFT_EQ:
23747           code = RSHIFT_EXPR;
23748           break;
23749         case CPP_AND_EQ:
23750           code = BIT_AND_EXPR;
23751           break;
23752         case CPP_OR_EQ:
23753           code = BIT_IOR_EXPR;
23754           break;
23755         case CPP_XOR_EQ:
23756           code = BIT_XOR_EXPR;
23757           break;
23758         default:
23759           cp_parser_error (parser,
23760                            "invalid operator for %<#pragma omp atomic%>");
23761           goto saw_error;
23762         }
23763       cp_lexer_consume_token (parser->lexer);
23764
23765       rhs = cp_parser_expression (parser, false, NULL);
23766       if (rhs == error_mark_node)
23767         goto saw_error;
23768       break;
23769     }
23770   finish_omp_atomic (code, lhs, rhs);
23771   cp_parser_consume_semicolon_at_end_of_statement (parser);
23772   return;
23773
23774  saw_error:
23775   cp_parser_skip_to_end_of_block_or_statement (parser);
23776 }
23777
23778
23779 /* OpenMP 2.5:
23780    # pragma omp barrier new-line  */
23781
23782 static void
23783 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23784 {
23785   cp_parser_require_pragma_eol (parser, pragma_tok);
23786   finish_omp_barrier ();
23787 }
23788
23789 /* OpenMP 2.5:
23790    # pragma omp critical [(name)] new-line
23791      structured-block  */
23792
23793 static tree
23794 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23795 {
23796   tree stmt, name = NULL;
23797
23798   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23799     {
23800       cp_lexer_consume_token (parser->lexer);
23801
23802       name = cp_parser_identifier (parser);
23803
23804       if (name == error_mark_node
23805           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23806         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23807                                                /*or_comma=*/false,
23808                                                /*consume_paren=*/true);
23809       if (name == error_mark_node)
23810         name = NULL;
23811     }
23812   cp_parser_require_pragma_eol (parser, pragma_tok);
23813
23814   stmt = cp_parser_omp_structured_block (parser);
23815   return c_finish_omp_critical (input_location, stmt, name);
23816 }
23817
23818 /* OpenMP 2.5:
23819    # pragma omp flush flush-vars[opt] new-line
23820
23821    flush-vars:
23822      ( variable-list ) */
23823
23824 static void
23825 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23826 {
23827   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23828     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23829   cp_parser_require_pragma_eol (parser, pragma_tok);
23830
23831   finish_omp_flush ();
23832 }
23833
23834 /* Helper function, to parse omp for increment expression.  */
23835
23836 static tree
23837 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23838 {
23839   tree cond = cp_parser_binary_expression (parser, false, true,
23840                                            PREC_NOT_OPERATOR, NULL);
23841   bool overloaded_p;
23842
23843   if (cond == error_mark_node
23844       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23845     {
23846       cp_parser_skip_to_end_of_statement (parser);
23847       return error_mark_node;
23848     }
23849
23850   switch (TREE_CODE (cond))
23851     {
23852     case GT_EXPR:
23853     case GE_EXPR:
23854     case LT_EXPR:
23855     case LE_EXPR:
23856       break;
23857     default:
23858       return error_mark_node;
23859     }
23860
23861   /* If decl is an iterator, preserve LHS and RHS of the relational
23862      expr until finish_omp_for.  */
23863   if (decl
23864       && (type_dependent_expression_p (decl)
23865           || CLASS_TYPE_P (TREE_TYPE (decl))))
23866     return cond;
23867
23868   return build_x_binary_op (TREE_CODE (cond),
23869                             TREE_OPERAND (cond, 0), ERROR_MARK,
23870                             TREE_OPERAND (cond, 1), ERROR_MARK,
23871                             &overloaded_p, tf_warning_or_error);
23872 }
23873
23874 /* Helper function, to parse omp for increment expression.  */
23875
23876 static tree
23877 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
23878 {
23879   cp_token *token = cp_lexer_peek_token (parser->lexer);
23880   enum tree_code op;
23881   tree lhs, rhs;
23882   cp_id_kind idk;
23883   bool decl_first;
23884
23885   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23886     {
23887       op = (token->type == CPP_PLUS_PLUS
23888             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
23889       cp_lexer_consume_token (parser->lexer);
23890       lhs = cp_parser_cast_expression (parser, false, false, NULL);
23891       if (lhs != decl)
23892         return error_mark_node;
23893       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23894     }
23895
23896   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
23897   if (lhs != decl)
23898     return error_mark_node;
23899
23900   token = cp_lexer_peek_token (parser->lexer);
23901   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23902     {
23903       op = (token->type == CPP_PLUS_PLUS
23904             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
23905       cp_lexer_consume_token (parser->lexer);
23906       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23907     }
23908
23909   op = cp_parser_assignment_operator_opt (parser);
23910   if (op == ERROR_MARK)
23911     return error_mark_node;
23912
23913   if (op != NOP_EXPR)
23914     {
23915       rhs = cp_parser_assignment_expression (parser, false, NULL);
23916       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
23917       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23918     }
23919
23920   lhs = cp_parser_binary_expression (parser, false, false,
23921                                      PREC_ADDITIVE_EXPRESSION, NULL);
23922   token = cp_lexer_peek_token (parser->lexer);
23923   decl_first = lhs == decl;
23924   if (decl_first)
23925     lhs = NULL_TREE;
23926   if (token->type != CPP_PLUS
23927       && token->type != CPP_MINUS)
23928     return error_mark_node;
23929
23930   do
23931     {
23932       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
23933       cp_lexer_consume_token (parser->lexer);
23934       rhs = cp_parser_binary_expression (parser, false, false,
23935                                          PREC_ADDITIVE_EXPRESSION, NULL);
23936       token = cp_lexer_peek_token (parser->lexer);
23937       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
23938         {
23939           if (lhs == NULL_TREE)
23940             {
23941               if (op == PLUS_EXPR)
23942                 lhs = rhs;
23943               else
23944                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
23945             }
23946           else
23947             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
23948                                      NULL, tf_warning_or_error);
23949         }
23950     }
23951   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
23952
23953   if (!decl_first)
23954     {
23955       if (rhs != decl || op == MINUS_EXPR)
23956         return error_mark_node;
23957       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
23958     }
23959   else
23960     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
23961
23962   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23963 }
23964
23965 /* Parse the restricted form of the for statement allowed by OpenMP.  */
23966
23967 static tree
23968 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
23969 {
23970   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
23971   tree real_decl, initv, condv, incrv, declv;
23972   tree this_pre_body, cl;
23973   location_t loc_first;
23974   bool collapse_err = false;
23975   int i, collapse = 1, nbraces = 0;
23976   VEC(tree,gc) *for_block = make_tree_vector ();
23977
23978   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
23979     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
23980       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
23981
23982   gcc_assert (collapse >= 1);
23983
23984   declv = make_tree_vec (collapse);
23985   initv = make_tree_vec (collapse);
23986   condv = make_tree_vec (collapse);
23987   incrv = make_tree_vec (collapse);
23988
23989   loc_first = cp_lexer_peek_token (parser->lexer)->location;
23990
23991   for (i = 0; i < collapse; i++)
23992     {
23993       int bracecount = 0;
23994       bool add_private_clause = false;
23995       location_t loc;
23996
23997       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23998         {
23999           cp_parser_error (parser, "for statement expected");
24000           return NULL;
24001         }
24002       loc = cp_lexer_consume_token (parser->lexer)->location;
24003
24004       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24005         return NULL;
24006
24007       init = decl = real_decl = NULL;
24008       this_pre_body = push_stmt_list ();
24009       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24010         {
24011           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24012
24013              init-expr:
24014                        var = lb
24015                        integer-type var = lb
24016                        random-access-iterator-type var = lb
24017                        pointer-type var = lb
24018           */
24019           cp_decl_specifier_seq type_specifiers;
24020
24021           /* First, try to parse as an initialized declaration.  See
24022              cp_parser_condition, from whence the bulk of this is copied.  */
24023
24024           cp_parser_parse_tentatively (parser);
24025           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24026                                         /*is_trailing_return=*/false,
24027                                         &type_specifiers);
24028           if (cp_parser_parse_definitely (parser))
24029             {
24030               /* If parsing a type specifier seq succeeded, then this
24031                  MUST be a initialized declaration.  */
24032               tree asm_specification, attributes;
24033               cp_declarator *declarator;
24034
24035               declarator = cp_parser_declarator (parser,
24036                                                  CP_PARSER_DECLARATOR_NAMED,
24037                                                  /*ctor_dtor_or_conv_p=*/NULL,
24038                                                  /*parenthesized_p=*/NULL,
24039                                                  /*member_p=*/false);
24040               attributes = cp_parser_attributes_opt (parser);
24041               asm_specification = cp_parser_asm_specification_opt (parser);
24042
24043               if (declarator == cp_error_declarator) 
24044                 cp_parser_skip_to_end_of_statement (parser);
24045
24046               else 
24047                 {
24048                   tree pushed_scope, auto_node;
24049
24050                   decl = start_decl (declarator, &type_specifiers,
24051                                      SD_INITIALIZED, attributes,
24052                                      /*prefix_attributes=*/NULL_TREE,
24053                                      &pushed_scope);
24054
24055                   auto_node = type_uses_auto (TREE_TYPE (decl));
24056                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24057                     {
24058                       if (cp_lexer_next_token_is (parser->lexer, 
24059                                                   CPP_OPEN_PAREN))
24060                         error ("parenthesized initialization is not allowed in "
24061                                "OpenMP %<for%> loop");
24062                       else
24063                         /* Trigger an error.  */
24064                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24065
24066                       init = error_mark_node;
24067                       cp_parser_skip_to_end_of_statement (parser);
24068                     }
24069                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24070                            || type_dependent_expression_p (decl)
24071                            || auto_node)
24072                     {
24073                       bool is_direct_init, is_non_constant_init;
24074
24075                       init = cp_parser_initializer (parser,
24076                                                     &is_direct_init,
24077                                                     &is_non_constant_init);
24078
24079                       if (auto_node && describable_type (init))
24080                         {
24081                           TREE_TYPE (decl)
24082                             = do_auto_deduction (TREE_TYPE (decl), init,
24083                                                  auto_node);
24084
24085                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24086                               && !type_dependent_expression_p (decl))
24087                             goto non_class;
24088                         }
24089                       
24090                       cp_finish_decl (decl, init, !is_non_constant_init,
24091                                       asm_specification,
24092                                       LOOKUP_ONLYCONVERTING);
24093                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24094                         {
24095                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24096                           init = NULL_TREE;
24097                         }
24098                       else
24099                         init = pop_stmt_list (this_pre_body);
24100                       this_pre_body = NULL_TREE;
24101                     }
24102                   else
24103                     {
24104                       /* Consume '='.  */
24105                       cp_lexer_consume_token (parser->lexer);
24106                       init = cp_parser_assignment_expression (parser, false, NULL);
24107
24108                     non_class:
24109                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24110                         init = error_mark_node;
24111                       else
24112                         cp_finish_decl (decl, NULL_TREE,
24113                                         /*init_const_expr_p=*/false,
24114                                         asm_specification,
24115                                         LOOKUP_ONLYCONVERTING);
24116                     }
24117
24118                   if (pushed_scope)
24119                     pop_scope (pushed_scope);
24120                 }
24121             }
24122           else 
24123             {
24124               cp_id_kind idk;
24125               /* If parsing a type specifier sequence failed, then
24126                  this MUST be a simple expression.  */
24127               cp_parser_parse_tentatively (parser);
24128               decl = cp_parser_primary_expression (parser, false, false,
24129                                                    false, &idk);
24130               if (!cp_parser_error_occurred (parser)
24131                   && decl
24132                   && DECL_P (decl)
24133                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24134                 {
24135                   tree rhs;
24136
24137                   cp_parser_parse_definitely (parser);
24138                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24139                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24140                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24141                                                          rhs,
24142                                                          tf_warning_or_error));
24143                   add_private_clause = true;
24144                 }
24145               else
24146                 {
24147                   decl = NULL;
24148                   cp_parser_abort_tentative_parse (parser);
24149                   init = cp_parser_expression (parser, false, NULL);
24150                   if (init)
24151                     {
24152                       if (TREE_CODE (init) == MODIFY_EXPR
24153                           || TREE_CODE (init) == MODOP_EXPR)
24154                         real_decl = TREE_OPERAND (init, 0);
24155                     }
24156                 }
24157             }
24158         }
24159       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24160       if (this_pre_body)
24161         {
24162           this_pre_body = pop_stmt_list (this_pre_body);
24163           if (pre_body)
24164             {
24165               tree t = pre_body;
24166               pre_body = push_stmt_list ();
24167               add_stmt (t);
24168               add_stmt (this_pre_body);
24169               pre_body = pop_stmt_list (pre_body);
24170             }
24171           else
24172             pre_body = this_pre_body;
24173         }
24174
24175       if (decl)
24176         real_decl = decl;
24177       if (par_clauses != NULL && real_decl != NULL_TREE)
24178         {
24179           tree *c;
24180           for (c = par_clauses; *c ; )
24181             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24182                 && OMP_CLAUSE_DECL (*c) == real_decl)
24183               {
24184                 error_at (loc, "iteration variable %qD"
24185                           " should not be firstprivate", real_decl);
24186                 *c = OMP_CLAUSE_CHAIN (*c);
24187               }
24188             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24189                      && OMP_CLAUSE_DECL (*c) == real_decl)
24190               {
24191                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24192                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24193                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24194                 OMP_CLAUSE_DECL (l) = real_decl;
24195                 OMP_CLAUSE_CHAIN (l) = clauses;
24196                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24197                 clauses = l;
24198                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24199                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24200                 add_private_clause = false;
24201               }
24202             else
24203               {
24204                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24205                     && OMP_CLAUSE_DECL (*c) == real_decl)
24206                   add_private_clause = false;
24207                 c = &OMP_CLAUSE_CHAIN (*c);
24208               }
24209         }
24210
24211       if (add_private_clause)
24212         {
24213           tree c;
24214           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24215             {
24216               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24217                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24218                   && OMP_CLAUSE_DECL (c) == decl)
24219                 break;
24220               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24221                        && OMP_CLAUSE_DECL (c) == decl)
24222                 error_at (loc, "iteration variable %qD "
24223                           "should not be firstprivate",
24224                           decl);
24225               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24226                        && OMP_CLAUSE_DECL (c) == decl)
24227                 error_at (loc, "iteration variable %qD should not be reduction",
24228                           decl);
24229             }
24230           if (c == NULL)
24231             {
24232               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24233               OMP_CLAUSE_DECL (c) = decl;
24234               c = finish_omp_clauses (c);
24235               if (c)
24236                 {
24237                   OMP_CLAUSE_CHAIN (c) = clauses;
24238                   clauses = c;
24239                 }
24240             }
24241         }
24242
24243       cond = NULL;
24244       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24245         cond = cp_parser_omp_for_cond (parser, decl);
24246       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24247
24248       incr = NULL;
24249       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24250         {
24251           /* If decl is an iterator, preserve the operator on decl
24252              until finish_omp_for.  */
24253           if (decl
24254               && (type_dependent_expression_p (decl)
24255                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24256             incr = cp_parser_omp_for_incr (parser, decl);
24257           else
24258             incr = cp_parser_expression (parser, false, NULL);
24259         }
24260
24261       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24262         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24263                                                /*or_comma=*/false,
24264                                                /*consume_paren=*/true);
24265
24266       TREE_VEC_ELT (declv, i) = decl;
24267       TREE_VEC_ELT (initv, i) = init;
24268       TREE_VEC_ELT (condv, i) = cond;
24269       TREE_VEC_ELT (incrv, i) = incr;
24270
24271       if (i == collapse - 1)
24272         break;
24273
24274       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24275          in between the collapsed for loops to be still considered perfectly
24276          nested.  Hopefully the final version clarifies this.
24277          For now handle (multiple) {'s and empty statements.  */
24278       cp_parser_parse_tentatively (parser);
24279       do
24280         {
24281           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24282             break;
24283           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24284             {
24285               cp_lexer_consume_token (parser->lexer);
24286               bracecount++;
24287             }
24288           else if (bracecount
24289                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24290             cp_lexer_consume_token (parser->lexer);
24291           else
24292             {
24293               loc = cp_lexer_peek_token (parser->lexer)->location;
24294               error_at (loc, "not enough collapsed for loops");
24295               collapse_err = true;
24296               cp_parser_abort_tentative_parse (parser);
24297               declv = NULL_TREE;
24298               break;
24299             }
24300         }
24301       while (1);
24302
24303       if (declv)
24304         {
24305           cp_parser_parse_definitely (parser);
24306           nbraces += bracecount;
24307         }
24308     }
24309
24310   /* Note that we saved the original contents of this flag when we entered
24311      the structured block, and so we don't need to re-save it here.  */
24312   parser->in_statement = IN_OMP_FOR;
24313
24314   /* Note that the grammar doesn't call for a structured block here,
24315      though the loop as a whole is a structured block.  */
24316   body = push_stmt_list ();
24317   cp_parser_statement (parser, NULL_TREE, false, NULL);
24318   body = pop_stmt_list (body);
24319
24320   if (declv == NULL_TREE)
24321     ret = NULL_TREE;
24322   else
24323     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24324                           pre_body, clauses);
24325
24326   while (nbraces)
24327     {
24328       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24329         {
24330           cp_lexer_consume_token (parser->lexer);
24331           nbraces--;
24332         }
24333       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24334         cp_lexer_consume_token (parser->lexer);
24335       else
24336         {
24337           if (!collapse_err)
24338             {
24339               error_at (cp_lexer_peek_token (parser->lexer)->location,
24340                         "collapsed loops not perfectly nested");
24341             }
24342           collapse_err = true;
24343           cp_parser_statement_seq_opt (parser, NULL);
24344           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24345             break;
24346         }
24347     }
24348
24349   while (!VEC_empty (tree, for_block))
24350     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24351   release_tree_vector (for_block);
24352
24353   return ret;
24354 }
24355
24356 /* OpenMP 2.5:
24357    #pragma omp for for-clause[optseq] new-line
24358      for-loop  */
24359
24360 #define OMP_FOR_CLAUSE_MASK                             \
24361         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24362         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24363         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24364         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24365         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24366         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24367         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24368         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24369
24370 static tree
24371 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24372 {
24373   tree clauses, sb, ret;
24374   unsigned int save;
24375
24376   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24377                                        "#pragma omp for", pragma_tok);
24378
24379   sb = begin_omp_structured_block ();
24380   save = cp_parser_begin_omp_structured_block (parser);
24381
24382   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24383
24384   cp_parser_end_omp_structured_block (parser, save);
24385   add_stmt (finish_omp_structured_block (sb));
24386
24387   return ret;
24388 }
24389
24390 /* OpenMP 2.5:
24391    # pragma omp master new-line
24392      structured-block  */
24393
24394 static tree
24395 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24396 {
24397   cp_parser_require_pragma_eol (parser, pragma_tok);
24398   return c_finish_omp_master (input_location,
24399                               cp_parser_omp_structured_block (parser));
24400 }
24401
24402 /* OpenMP 2.5:
24403    # pragma omp ordered new-line
24404      structured-block  */
24405
24406 static tree
24407 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24408 {
24409   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24410   cp_parser_require_pragma_eol (parser, pragma_tok);
24411   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24412 }
24413
24414 /* OpenMP 2.5:
24415
24416    section-scope:
24417      { section-sequence }
24418
24419    section-sequence:
24420      section-directive[opt] structured-block
24421      section-sequence section-directive structured-block  */
24422
24423 static tree
24424 cp_parser_omp_sections_scope (cp_parser *parser)
24425 {
24426   tree stmt, substmt;
24427   bool error_suppress = false;
24428   cp_token *tok;
24429
24430   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24431     return NULL_TREE;
24432
24433   stmt = push_stmt_list ();
24434
24435   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24436     {
24437       unsigned save;
24438
24439       substmt = begin_omp_structured_block ();
24440       save = cp_parser_begin_omp_structured_block (parser);
24441
24442       while (1)
24443         {
24444           cp_parser_statement (parser, NULL_TREE, false, NULL);
24445
24446           tok = cp_lexer_peek_token (parser->lexer);
24447           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24448             break;
24449           if (tok->type == CPP_CLOSE_BRACE)
24450             break;
24451           if (tok->type == CPP_EOF)
24452             break;
24453         }
24454
24455       cp_parser_end_omp_structured_block (parser, save);
24456       substmt = finish_omp_structured_block (substmt);
24457       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24458       add_stmt (substmt);
24459     }
24460
24461   while (1)
24462     {
24463       tok = cp_lexer_peek_token (parser->lexer);
24464       if (tok->type == CPP_CLOSE_BRACE)
24465         break;
24466       if (tok->type == CPP_EOF)
24467         break;
24468
24469       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24470         {
24471           cp_lexer_consume_token (parser->lexer);
24472           cp_parser_require_pragma_eol (parser, tok);
24473           error_suppress = false;
24474         }
24475       else if (!error_suppress)
24476         {
24477           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24478           error_suppress = true;
24479         }
24480
24481       substmt = cp_parser_omp_structured_block (parser);
24482       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24483       add_stmt (substmt);
24484     }
24485   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24486
24487   substmt = pop_stmt_list (stmt);
24488
24489   stmt = make_node (OMP_SECTIONS);
24490   TREE_TYPE (stmt) = void_type_node;
24491   OMP_SECTIONS_BODY (stmt) = substmt;
24492
24493   add_stmt (stmt);
24494   return stmt;
24495 }
24496
24497 /* OpenMP 2.5:
24498    # pragma omp sections sections-clause[optseq] newline
24499      sections-scope  */
24500
24501 #define OMP_SECTIONS_CLAUSE_MASK                        \
24502         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24503         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24504         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24505         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24506         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24507
24508 static tree
24509 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24510 {
24511   tree clauses, ret;
24512
24513   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24514                                        "#pragma omp sections", pragma_tok);
24515
24516   ret = cp_parser_omp_sections_scope (parser);
24517   if (ret)
24518     OMP_SECTIONS_CLAUSES (ret) = clauses;
24519
24520   return ret;
24521 }
24522
24523 /* OpenMP 2.5:
24524    # pragma parallel parallel-clause new-line
24525    # pragma parallel for parallel-for-clause new-line
24526    # pragma parallel sections parallel-sections-clause new-line  */
24527
24528 #define OMP_PARALLEL_CLAUSE_MASK                        \
24529         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24530         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24531         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24532         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24533         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24534         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24535         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24536         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24537
24538 static tree
24539 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24540 {
24541   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24542   const char *p_name = "#pragma omp parallel";
24543   tree stmt, clauses, par_clause, ws_clause, block;
24544   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24545   unsigned int save;
24546   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24547
24548   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24549     {
24550       cp_lexer_consume_token (parser->lexer);
24551       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24552       p_name = "#pragma omp parallel for";
24553       mask |= OMP_FOR_CLAUSE_MASK;
24554       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24555     }
24556   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24557     {
24558       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24559       const char *p = IDENTIFIER_POINTER (id);
24560       if (strcmp (p, "sections") == 0)
24561         {
24562           cp_lexer_consume_token (parser->lexer);
24563           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24564           p_name = "#pragma omp parallel sections";
24565           mask |= OMP_SECTIONS_CLAUSE_MASK;
24566           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24567         }
24568     }
24569
24570   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24571   block = begin_omp_parallel ();
24572   save = cp_parser_begin_omp_structured_block (parser);
24573
24574   switch (p_kind)
24575     {
24576     case PRAGMA_OMP_PARALLEL:
24577       cp_parser_statement (parser, NULL_TREE, false, NULL);
24578       par_clause = clauses;
24579       break;
24580
24581     case PRAGMA_OMP_PARALLEL_FOR:
24582       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24583       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24584       break;
24585
24586     case PRAGMA_OMP_PARALLEL_SECTIONS:
24587       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24588       stmt = cp_parser_omp_sections_scope (parser);
24589       if (stmt)
24590         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24591       break;
24592
24593     default:
24594       gcc_unreachable ();
24595     }
24596
24597   cp_parser_end_omp_structured_block (parser, save);
24598   stmt = finish_omp_parallel (par_clause, block);
24599   if (p_kind != PRAGMA_OMP_PARALLEL)
24600     OMP_PARALLEL_COMBINED (stmt) = 1;
24601   return stmt;
24602 }
24603
24604 /* OpenMP 2.5:
24605    # pragma omp single single-clause[optseq] new-line
24606      structured-block  */
24607
24608 #define OMP_SINGLE_CLAUSE_MASK                          \
24609         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24610         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24611         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
24612         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24613
24614 static tree
24615 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24616 {
24617   tree stmt = make_node (OMP_SINGLE);
24618   TREE_TYPE (stmt) = void_type_node;
24619
24620   OMP_SINGLE_CLAUSES (stmt)
24621     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24622                                  "#pragma omp single", pragma_tok);
24623   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24624
24625   return add_stmt (stmt);
24626 }
24627
24628 /* OpenMP 3.0:
24629    # pragma omp task task-clause[optseq] new-line
24630      structured-block  */
24631
24632 #define OMP_TASK_CLAUSE_MASK                            \
24633         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24634         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
24635         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24636         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24637         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24638         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24639
24640 static tree
24641 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24642 {
24643   tree clauses, block;
24644   unsigned int save;
24645
24646   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24647                                        "#pragma omp task", pragma_tok);
24648   block = begin_omp_task ();
24649   save = cp_parser_begin_omp_structured_block (parser);
24650   cp_parser_statement (parser, NULL_TREE, false, NULL);
24651   cp_parser_end_omp_structured_block (parser, save);
24652   return finish_omp_task (clauses, block);
24653 }
24654
24655 /* OpenMP 3.0:
24656    # pragma omp taskwait new-line  */
24657
24658 static void
24659 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24660 {
24661   cp_parser_require_pragma_eol (parser, pragma_tok);
24662   finish_omp_taskwait ();
24663 }
24664
24665 /* OpenMP 2.5:
24666    # pragma omp threadprivate (variable-list) */
24667
24668 static void
24669 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24670 {
24671   tree vars;
24672
24673   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24674   cp_parser_require_pragma_eol (parser, pragma_tok);
24675
24676   finish_omp_threadprivate (vars);
24677 }
24678
24679 /* Main entry point to OpenMP statement pragmas.  */
24680
24681 static void
24682 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24683 {
24684   tree stmt;
24685
24686   switch (pragma_tok->pragma_kind)
24687     {
24688     case PRAGMA_OMP_ATOMIC:
24689       cp_parser_omp_atomic (parser, pragma_tok);
24690       return;
24691     case PRAGMA_OMP_CRITICAL:
24692       stmt = cp_parser_omp_critical (parser, pragma_tok);
24693       break;
24694     case PRAGMA_OMP_FOR:
24695       stmt = cp_parser_omp_for (parser, pragma_tok);
24696       break;
24697     case PRAGMA_OMP_MASTER:
24698       stmt = cp_parser_omp_master (parser, pragma_tok);
24699       break;
24700     case PRAGMA_OMP_ORDERED:
24701       stmt = cp_parser_omp_ordered (parser, pragma_tok);
24702       break;
24703     case PRAGMA_OMP_PARALLEL:
24704       stmt = cp_parser_omp_parallel (parser, pragma_tok);
24705       break;
24706     case PRAGMA_OMP_SECTIONS:
24707       stmt = cp_parser_omp_sections (parser, pragma_tok);
24708       break;
24709     case PRAGMA_OMP_SINGLE:
24710       stmt = cp_parser_omp_single (parser, pragma_tok);
24711       break;
24712     case PRAGMA_OMP_TASK:
24713       stmt = cp_parser_omp_task (parser, pragma_tok);
24714       break;
24715     default:
24716       gcc_unreachable ();
24717     }
24718
24719   if (stmt)
24720     SET_EXPR_LOCATION (stmt, pragma_tok->location);
24721 }
24722 \f
24723 /* The parser.  */
24724
24725 static GTY (()) cp_parser *the_parser;
24726
24727 \f
24728 /* Special handling for the first token or line in the file.  The first
24729    thing in the file might be #pragma GCC pch_preprocess, which loads a
24730    PCH file, which is a GC collection point.  So we need to handle this
24731    first pragma without benefit of an existing lexer structure.
24732
24733    Always returns one token to the caller in *FIRST_TOKEN.  This is
24734    either the true first token of the file, or the first token after
24735    the initial pragma.  */
24736
24737 static void
24738 cp_parser_initial_pragma (cp_token *first_token)
24739 {
24740   tree name = NULL;
24741
24742   cp_lexer_get_preprocessor_token (NULL, first_token);
24743   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24744     return;
24745
24746   cp_lexer_get_preprocessor_token (NULL, first_token);
24747   if (first_token->type == CPP_STRING)
24748     {
24749       name = first_token->u.value;
24750
24751       cp_lexer_get_preprocessor_token (NULL, first_token);
24752       if (first_token->type != CPP_PRAGMA_EOL)
24753         error_at (first_token->location,
24754                   "junk at end of %<#pragma GCC pch_preprocess%>");
24755     }
24756   else
24757     error_at (first_token->location, "expected string literal");
24758
24759   /* Skip to the end of the pragma.  */
24760   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24761     cp_lexer_get_preprocessor_token (NULL, first_token);
24762
24763   /* Now actually load the PCH file.  */
24764   if (name)
24765     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24766
24767   /* Read one more token to return to our caller.  We have to do this
24768      after reading the PCH file in, since its pointers have to be
24769      live.  */
24770   cp_lexer_get_preprocessor_token (NULL, first_token);
24771 }
24772
24773 /* Normal parsing of a pragma token.  Here we can (and must) use the
24774    regular lexer.  */
24775
24776 static bool
24777 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24778 {
24779   cp_token *pragma_tok;
24780   unsigned int id;
24781
24782   pragma_tok = cp_lexer_consume_token (parser->lexer);
24783   gcc_assert (pragma_tok->type == CPP_PRAGMA);
24784   parser->lexer->in_pragma = true;
24785
24786   id = pragma_tok->pragma_kind;
24787   switch (id)
24788     {
24789     case PRAGMA_GCC_PCH_PREPROCESS:
24790       error_at (pragma_tok->location,
24791                 "%<#pragma GCC pch_preprocess%> must be first");
24792       break;
24793
24794     case PRAGMA_OMP_BARRIER:
24795       switch (context)
24796         {
24797         case pragma_compound:
24798           cp_parser_omp_barrier (parser, pragma_tok);
24799           return false;
24800         case pragma_stmt:
24801           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24802                     "used in compound statements");
24803           break;
24804         default:
24805           goto bad_stmt;
24806         }
24807       break;
24808
24809     case PRAGMA_OMP_FLUSH:
24810       switch (context)
24811         {
24812         case pragma_compound:
24813           cp_parser_omp_flush (parser, pragma_tok);
24814           return false;
24815         case pragma_stmt:
24816           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24817                     "used in compound statements");
24818           break;
24819         default:
24820           goto bad_stmt;
24821         }
24822       break;
24823
24824     case PRAGMA_OMP_TASKWAIT:
24825       switch (context)
24826         {
24827         case pragma_compound:
24828           cp_parser_omp_taskwait (parser, pragma_tok);
24829           return false;
24830         case pragma_stmt:
24831           error_at (pragma_tok->location,
24832                     "%<#pragma omp taskwait%> may only be "
24833                     "used in compound statements");
24834           break;
24835         default:
24836           goto bad_stmt;
24837         }
24838       break;
24839
24840     case PRAGMA_OMP_THREADPRIVATE:
24841       cp_parser_omp_threadprivate (parser, pragma_tok);
24842       return false;
24843
24844     case PRAGMA_OMP_ATOMIC:
24845     case PRAGMA_OMP_CRITICAL:
24846     case PRAGMA_OMP_FOR:
24847     case PRAGMA_OMP_MASTER:
24848     case PRAGMA_OMP_ORDERED:
24849     case PRAGMA_OMP_PARALLEL:
24850     case PRAGMA_OMP_SECTIONS:
24851     case PRAGMA_OMP_SINGLE:
24852     case PRAGMA_OMP_TASK:
24853       if (context == pragma_external)
24854         goto bad_stmt;
24855       cp_parser_omp_construct (parser, pragma_tok);
24856       return true;
24857
24858     case PRAGMA_OMP_SECTION:
24859       error_at (pragma_tok->location, 
24860                 "%<#pragma omp section%> may only be used in "
24861                 "%<#pragma omp sections%> construct");
24862       break;
24863
24864     default:
24865       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
24866       c_invoke_pragma_handler (id);
24867       break;
24868
24869     bad_stmt:
24870       cp_parser_error (parser, "expected declaration specifiers");
24871       break;
24872     }
24873
24874   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24875   return false;
24876 }
24877
24878 /* The interface the pragma parsers have to the lexer.  */
24879
24880 enum cpp_ttype
24881 pragma_lex (tree *value)
24882 {
24883   cp_token *tok;
24884   enum cpp_ttype ret;
24885
24886   tok = cp_lexer_peek_token (the_parser->lexer);
24887
24888   ret = tok->type;
24889   *value = tok->u.value;
24890
24891   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
24892     ret = CPP_EOF;
24893   else if (ret == CPP_STRING)
24894     *value = cp_parser_string_literal (the_parser, false, false);
24895   else
24896     {
24897       cp_lexer_consume_token (the_parser->lexer);
24898       if (ret == CPP_KEYWORD)
24899         ret = CPP_NAME;
24900     }
24901
24902   return ret;
24903 }
24904
24905 \f
24906 /* External interface.  */
24907
24908 /* Parse one entire translation unit.  */
24909
24910 void
24911 c_parse_file (void)
24912 {
24913   static bool already_called = false;
24914
24915   if (already_called)
24916     {
24917       sorry ("inter-module optimizations not implemented for C++");
24918       return;
24919     }
24920   already_called = true;
24921
24922   the_parser = cp_parser_new ();
24923   push_deferring_access_checks (flag_access_control
24924                                 ? dk_no_deferred : dk_no_check);
24925   cp_parser_translation_unit (the_parser);
24926   the_parser = NULL;
24927 }
24928
24929 #include "gt-cp-parser.h"