1/* C++ Parser.
2   Copyright (C) 2000, 2001, 2002, 2003, 2004,
3   2005, 2007, 2008, 2009  Free Software Foundation, Inc.
4   Written by Mark Mitchell <mark@codesourcery.com>.
5
6   This file is part of GCC.
7
8   GCC is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 3, or (at your option)
11   any later version.
12
13   GCC is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3.  If not see
20<http://www.gnu.org/licenses/>.  */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "dyn-string.h"
27#include "varray.h"
28#include "cpplib.h"
29#include "tree.h"
30#include "cp-tree.h"
31#include "intl.h"
32#include "c-pragma.h"
33#include "decl.h"
34#include "flags.h"
35#include "diagnostic.h"
36#include "toplev.h"
37#include "output.h"
38#include "target.h"
39#include "cgraph.h"
40#include "c-common.h"
41#include "plugin.h"
42
43
44/* The lexer.  */
45
46/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47   and c-lex.c) and the C++ parser.  */
48
49/* A token's value and its associated deferred access checks and
50   qualifying scope.  */
51
52struct GTY(()) tree_check {
53  /* The value associated with the token.  */
54  tree value;
55  /* The checks that have been associated with value.  */
56  VEC (deferred_access_check, gc)* checks;
57  /* The token's qualifying scope (used when it is a
58     CPP_NESTED_NAME_SPECIFIER).  */
59  tree qualifying_scope;
60};
61
62/* A C++ token.  */
63
64typedef struct GTY (()) cp_token {
65  /* The kind of token.  */
66  ENUM_BITFIELD (cpp_ttype) type : 8;
67  /* If this token is a keyword, this value indicates which keyword.
68     Otherwise, this value is RID_MAX.  */
69  ENUM_BITFIELD (rid) keyword : 8;
70  /* Token flags.  */
71  unsigned char flags;
72  /* Identifier for the pragma.  */
73  ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74  /* True if this token is from a context where it is implicitly extern "C" */
75  BOOL_BITFIELD implicit_extern_c : 1;
76  /* True for a CPP_NAME token that is not a keyword (i.e., for which
77     KEYWORD is RID_MAX) iff this name was looked up and found to be
78     ambiguous.  An error has already been reported.  */
79  BOOL_BITFIELD ambiguous_p : 1;
80  /* The location at which this token was found.  */
81  location_t location;
82  /* The value associated with this token, if any.  */
83  union cp_token_value {
84    /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
85    struct tree_check* GTY((tag ("1"))) tree_check_value;
86    /* Use for all other tokens.  */
87    tree GTY((tag ("0"))) value;
88  } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89} cp_token;
90
91/* We use a stack of token pointer for saving token sets.  */
92typedef struct cp_token *cp_token_position;
93DEF_VEC_P (cp_token_position);
94DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96static cp_token eof_token =
97{
98  CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99};
100
101/* The cp_lexer structure represents the C++ lexer.  It is responsible
102   for managing the token stream from the preprocessor and supplying
103   it to the parser.  Tokens are never added to the cp_lexer after
104   it is created.  */
105
106typedef struct GTY (()) cp_lexer {
107  /* The memory allocated for the buffer.  NULL if this lexer does not
108     own the token buffer.  */
109  cp_token * GTY ((length ("%h.buffer_length"))) buffer;
110  /* If the lexer owns the buffer, this is the number of tokens in the
111     buffer.  */
112  size_t buffer_length;
113
114  /* A pointer just past the last available token.  The tokens
115     in this lexer are [buffer, last_token).  */
116  cp_token_position GTY ((skip)) last_token;
117
118  /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
119     no more available tokens.  */
120  cp_token_position GTY ((skip)) next_token;
121
122  /* A stack indicating positions at which cp_lexer_save_tokens was
123     called.  The top entry is the most recent position at which we
124     began saving tokens.  If the stack is non-empty, we are saving
125     tokens.  */
126  VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127
128  /* The next lexer in a linked list of lexers.  */
129  struct cp_lexer *next;
130
131  /* True if we should output debugging information.  */
132  bool debugging_p;
133
134  /* True if we're in the context of parsing a pragma, and should not
135     increment past the end-of-line marker.  */
136  bool in_pragma;
137} cp_lexer;
138
139/* cp_token_cache is a range of tokens.  There is no need to represent
140   allocate heap memory for it, since tokens are never removed from the
141   lexer's array.  There is also no need for the GC to walk through
142   a cp_token_cache, since everything in here is referenced through
143   a lexer.  */
144
145typedef struct GTY(()) cp_token_cache {
146  /* The beginning of the token range.  */
147  cp_token * GTY((skip)) first;
148
149  /* Points immediately after the last token in the range.  */
150  cp_token * GTY ((skip)) last;
151} cp_token_cache;
152
153/* Prototypes.  */
154
155static cp_lexer *cp_lexer_new_main
156  (void);
157static cp_lexer *cp_lexer_new_from_tokens
158  (cp_token_cache *tokens);
159static void cp_lexer_destroy
160  (cp_lexer *);
161static int cp_lexer_saving_tokens
162  (const cp_lexer *);
163static cp_token_position cp_lexer_token_position
164  (cp_lexer *, bool);
165static cp_token *cp_lexer_token_at
166  (cp_lexer *, cp_token_position);
167static void cp_lexer_get_preprocessor_token
168  (cp_lexer *, cp_token *);
169static inline cp_token *cp_lexer_peek_token
170  (cp_lexer *);
171static cp_token *cp_lexer_peek_nth_token
172  (cp_lexer *, size_t);
173static inline bool cp_lexer_next_token_is
174  (cp_lexer *, enum cpp_ttype);
175static bool cp_lexer_next_token_is_not
176  (cp_lexer *, enum cpp_ttype);
177static bool cp_lexer_next_token_is_keyword
178  (cp_lexer *, enum rid);
179static cp_token *cp_lexer_consume_token
180  (cp_lexer *);
181static void cp_lexer_purge_token
182  (cp_lexer *);
183static void cp_lexer_purge_tokens_after
184  (cp_lexer *, cp_token_position);
185static void cp_lexer_save_tokens
186  (cp_lexer *);
187static void cp_lexer_commit_tokens
188  (cp_lexer *);
189static void cp_lexer_rollback_tokens
190  (cp_lexer *);
191#ifdef ENABLE_CHECKING
192static void cp_lexer_print_token
193  (FILE *, cp_token *);
194static inline bool cp_lexer_debugging_p
195  (cp_lexer *);
196static void cp_lexer_start_debugging
197  (cp_lexer *) ATTRIBUTE_UNUSED;
198static void cp_lexer_stop_debugging
199  (cp_lexer *) ATTRIBUTE_UNUSED;
200#else
201/* If we define cp_lexer_debug_stream to NULL it will provoke warnings
202   about passing NULL to functions that require non-NULL arguments
203   (fputs, fprintf).  It will never be used, so all we need is a value
204   of the right type that's guaranteed not to be NULL.  */
205#define cp_lexer_debug_stream stdout
206#define cp_lexer_print_token(str, tok) (void) 0
207#define cp_lexer_debugging_p(lexer) 0
208#endif /* ENABLE_CHECKING */
209
210static cp_token_cache *cp_token_cache_new
211  (cp_token *, cp_token *);
212
213static void cp_parser_initial_pragma
214  (cp_token *);
215
216/* Manifest constants.  */
217#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
218#define CP_SAVED_TOKEN_STACK 5
219
220/* A token type for keywords, as opposed to ordinary identifiers.  */
221#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222
223/* A token type for template-ids.  If a template-id is processed while
224   parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
225   the value of the CPP_TEMPLATE_ID is whatever was returned by
226   cp_parser_template_id.  */
227#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228
229/* A token type for nested-name-specifiers.  If a
230   nested-name-specifier is processed while parsing tentatively, it is
231   replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
232   CPP_NESTED_NAME_SPECIFIER is whatever was returned by
233   cp_parser_nested_name_specifier_opt.  */
234#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235
236/* A token type for tokens that are not tokens at all; these are used
237   to represent slots in the array where there used to be a token
238   that has now been deleted.  */
239#define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240
241/* The number of token types, including C++-specific ones.  */
242#define N_CP_TTYPES ((int) (CPP_PURGED + 1))
243
244/* Variables.  */
245
246#ifdef ENABLE_CHECKING
247/* The stream to which debugging output should be written.  */
248static FILE *cp_lexer_debug_stream;
249#endif /* ENABLE_CHECKING */
250
251/* Nonzero if we are parsing an unevaluated operand: an operand to
252   sizeof, typeof, or alignof.  */
253int cp_unevaluated_operand;
254
255/* Create a new main C++ lexer, the lexer that gets tokens from the
256   preprocessor.  */
257
258static cp_lexer *
259cp_lexer_new_main (void)
260{
261  cp_token first_token;
262  cp_lexer *lexer;
263  cp_token *pos;
264  size_t alloc;
265  size_t space;
266  cp_token *buffer;
267
268  /* It's possible that parsing the first pragma will load a PCH file,
269     which is a GC collection point.  So we have to do that before
270     allocating any memory.  */
271  cp_parser_initial_pragma (&first_token);
272
273  c_common_no_more_pch ();
274
275  /* Allocate the memory.  */
276  lexer = GGC_CNEW (cp_lexer);
277
278#ifdef ENABLE_CHECKING
279  /* Initially we are not debugging.  */
280  lexer->debugging_p = false;
281#endif /* ENABLE_CHECKING */
282  lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
283				   CP_SAVED_TOKEN_STACK);
284
285  /* Create the buffer.  */
286  alloc = CP_LEXER_BUFFER_SIZE;
287  buffer = GGC_NEWVEC (cp_token, alloc);
288
289  /* Put the first token in the buffer.  */
290  space = alloc;
291  pos = buffer;
292  *pos = first_token;
293
294  /* Get the remaining tokens from the preprocessor.  */
295  while (pos->type != CPP_EOF)
296    {
297      pos++;
298      if (!--space)
299	{
300	  space = alloc;
301	  alloc *= 2;
302	  buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
303	  pos = buffer + space;
304	}
305      cp_lexer_get_preprocessor_token (lexer, pos);
306    }
307  lexer->buffer = buffer;
308  lexer->buffer_length = alloc - space;
309  lexer->last_token = pos;
310  lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311
312  /* Subsequent preprocessor diagnostics should use compiler
313     diagnostic functions to get the compiler source location.  */
314  done_lexing = true;
315
316  gcc_assert (lexer->next_token->type != CPP_PURGED);
317  return lexer;
318}
319
320/* Create a new lexer whose token stream is primed with the tokens in
321   CACHE.  When these tokens are exhausted, no new tokens will be read.  */
322
323static cp_lexer *
324cp_lexer_new_from_tokens (cp_token_cache *cache)
325{
326  cp_token *first = cache->first;
327  cp_token *last = cache->last;
328  cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
330  /* We do not own the buffer.  */
331  lexer->buffer = NULL;
332  lexer->buffer_length = 0;
333  lexer->next_token = first == last ? &eof_token : first;
334  lexer->last_token = last;
335
336  lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337				   CP_SAVED_TOKEN_STACK);
338
339#ifdef ENABLE_CHECKING
340  /* Initially we are not debugging.  */
341  lexer->debugging_p = false;
342#endif
343
344  gcc_assert (lexer->next_token->type != CPP_PURGED);
345  return lexer;
346}
347
348/* Frees all resources associated with LEXER.  */
349
350static void
351cp_lexer_destroy (cp_lexer *lexer)
352{
353  if (lexer->buffer)
354    ggc_free (lexer->buffer);
355  VEC_free (cp_token_position, heap, lexer->saved_tokens);
356  ggc_free (lexer);
357}
358
359/* Returns nonzero if debugging information should be output.  */
360
361#ifdef ENABLE_CHECKING
362
363static inline bool
364cp_lexer_debugging_p (cp_lexer *lexer)
365{
366  return lexer->debugging_p;
367}
368
369#endif /* ENABLE_CHECKING */
370
371static inline cp_token_position
372cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373{
374  gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
376  return lexer->next_token - previous_p;
377}
378
379static inline cp_token *
380cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381{
382  return pos;
383}
384
385/* nonzero if we are presently saving tokens.  */
386
387static inline int
388cp_lexer_saving_tokens (const cp_lexer* lexer)
389{
390  return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391}
392
393/* Store the next token from the preprocessor in *TOKEN.  Return true
394   if we reach EOF.  If LEXER is NULL, assume we are handling an
395   initial #pragma pch_preprocess, and thus want the lexer to return
396   processed strings.  */
397
398static void
399cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400{
401  static int is_extern_c = 0;
402
403   /* Get a new token from the preprocessor.  */
404  token->type
405    = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406			lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
407  token->keyword = RID_MAX;
408  token->pragma_kind = PRAGMA_NONE;
409
410  /* On some systems, some header files are surrounded by an
411     implicit extern "C" block.  Set a flag in the token if it
412     comes from such a header.  */
413  is_extern_c += pending_lang_change;
414  pending_lang_change = 0;
415  token->implicit_extern_c = is_extern_c > 0;
416
417  /* Check to see if this token is a keyword.  */
418  if (token->type == CPP_NAME)
419    {
420      if (C_IS_RESERVED_WORD (token->u.value))
421	{
422	  /* Mark this token as a keyword.  */
423	  token->type = CPP_KEYWORD;
424	  /* Record which keyword.  */
425	  token->keyword = C_RID_CODE (token->u.value);
426	}
427      else
428	{
429          if (warn_cxx0x_compat
430              && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
431              && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
432            {
433              /* Warn about the C++0x keyword (but still treat it as
434                 an identifier).  */
435              warning (OPT_Wc__0x_compat,
436                       "identifier %qE will become a keyword in C++0x",
437                       token->u.value);
438
439              /* Clear out the C_RID_CODE so we don't warn about this
440                 particular identifier-turned-keyword again.  */
441              C_SET_RID_CODE (token->u.value, RID_MAX);
442            }
443
444	  token->ambiguous_p = false;
445	  token->keyword = RID_MAX;
446	}
447    }
448  /* Handle Objective-C++ keywords.  */
449  else if (token->type == CPP_AT_NAME)
450    {
451      token->type = CPP_KEYWORD;
452      switch (C_RID_CODE (token->u.value))
453	{
454	/* Map 'class' to '@class', 'private' to '@private', etc.  */
455	case RID_CLASS: token->keyword = RID_AT_CLASS; break;
456	case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
457	case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
458	case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
459	case RID_THROW: token->keyword = RID_AT_THROW; break;
460	case RID_TRY: token->keyword = RID_AT_TRY; break;
461	case RID_CATCH: token->keyword = RID_AT_CATCH; break;
462	default: token->keyword = C_RID_CODE (token->u.value);
463	}
464    }
465  else if (token->type == CPP_PRAGMA)
466    {
467      /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
468      token->pragma_kind = ((enum pragma_kind)
469			    TREE_INT_CST_LOW (token->u.value));
470      token->u.value = NULL_TREE;
471    }
472}
473
474/* Update the globals input_location and the input file stack from TOKEN.  */
475static inline void
476cp_lexer_set_source_position_from_token (cp_token *token)
477{
478  if (token->type != CPP_EOF)
479    {
480      input_location = token->location;
481    }
482}
483
484/* Return a pointer to the next token in the token stream, but do not
485   consume it.  */
486
487static inline cp_token *
488cp_lexer_peek_token (cp_lexer *lexer)
489{
490  if (cp_lexer_debugging_p (lexer))
491    {
492      fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
493      cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
494      putc ('\n', cp_lexer_debug_stream);
495    }
496  return lexer->next_token;
497}
498
499/* Return true if the next token has the indicated TYPE.  */
500
501static inline bool
502cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
503{
504  return cp_lexer_peek_token (lexer)->type == type;
505}
506
507/* Return true if the next token does not have the indicated TYPE.  */
508
509static inline bool
510cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
511{
512  return !cp_lexer_next_token_is (lexer, type);
513}
514
515/* Return true if the next token is the indicated KEYWORD.  */
516
517static inline bool
518cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
519{
520  return cp_lexer_peek_token (lexer)->keyword == keyword;
521}
522
523/* Return true if the next token is not the indicated KEYWORD.  */
524
525static inline bool
526cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
527{
528  return cp_lexer_peek_token (lexer)->keyword != keyword;
529}
530
531/* Return true if the next token is a keyword for a decl-specifier.  */
532
533static bool
534cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
535{
536  cp_token *token;
537
538  token = cp_lexer_peek_token (lexer);
539  switch (token->keyword)
540    {
541      /* auto specifier: storage-class-specifier in C++,
542         simple-type-specifier in C++0x.  */
543    case RID_AUTO:
544      /* Storage classes.  */
545    case RID_REGISTER:
546    case RID_STATIC:
547    case RID_EXTERN:
548    case RID_MUTABLE:
549    case RID_THREAD:
550      /* Elaborated type specifiers.  */
551    case RID_ENUM:
552    case RID_CLASS:
553    case RID_STRUCT:
554    case RID_UNION:
555    case RID_TYPENAME:
556      /* Simple type specifiers.  */
557    case RID_CHAR:
558    case RID_CHAR16:
559    case RID_CHAR32:
560    case RID_WCHAR:
561    case RID_BOOL:
562    case RID_SHORT:
563    case RID_INT:
564    case RID_LONG:
565    case RID_SIGNED:
566    case RID_UNSIGNED:
567    case RID_FLOAT:
568    case RID_DOUBLE:
569    case RID_VOID:
570      /* GNU extensions.  */
571    case RID_ATTRIBUTE:
572    case RID_TYPEOF:
573      /* C++0x extensions.  */
574    case RID_DECLTYPE:
575      return true;
576
577    default:
578      return false;
579    }
580}
581
582/* Return a pointer to the Nth token in the token stream.  If N is 1,
583   then this is precisely equivalent to cp_lexer_peek_token (except
584   that it is not inline).  One would like to disallow that case, but
585   there is one case (cp_parser_nth_token_starts_template_id) where
586   the caller passes a variable for N and it might be 1.  */
587
588static cp_token *
589cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
590{
591  cp_token *token;
592
593  /* N is 1-based, not zero-based.  */
594  gcc_assert (n > 0);
595
596  if (cp_lexer_debugging_p (lexer))
597    fprintf (cp_lexer_debug_stream,
598	     "cp_lexer: peeking ahead %ld at token: ", (long)n);
599
600  --n;
601  token = lexer->next_token;
602  gcc_assert (!n || token != &eof_token);
603  while (n != 0)
604    {
605      ++token;
606      if (token == lexer->last_token)
607	{
608	  token = &eof_token;
609	  break;
610	}
611
612      if (token->type != CPP_PURGED)
613	--n;
614    }
615
616  if (cp_lexer_debugging_p (lexer))
617    {
618      cp_lexer_print_token (cp_lexer_debug_stream, token);
619      putc ('\n', cp_lexer_debug_stream);
620    }
621
622  return token;
623}
624
625/* Return the next token, and advance the lexer's next_token pointer
626   to point to the next non-purged token.  */
627
628static cp_token *
629cp_lexer_consume_token (cp_lexer* lexer)
630{
631  cp_token *token = lexer->next_token;
632
633  gcc_assert (token != &eof_token);
634  gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
635
636  do
637    {
638      lexer->next_token++;
639      if (lexer->next_token == lexer->last_token)
640	{
641	  lexer->next_token = &eof_token;
642	  break;
643	}
644
645    }
646  while (lexer->next_token->type == CPP_PURGED);
647
648  cp_lexer_set_source_position_from_token (token);
649
650  /* Provide debugging output.  */
651  if (cp_lexer_debugging_p (lexer))
652    {
653      fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
654      cp_lexer_print_token (cp_lexer_debug_stream, token);
655      putc ('\n', cp_lexer_debug_stream);
656    }
657
658  return token;
659}
660
661/* Permanently remove the next token from the token stream, and
662   advance the next_token pointer to refer to the next non-purged
663   token.  */
664
665static void
666cp_lexer_purge_token (cp_lexer *lexer)
667{
668  cp_token *tok = lexer->next_token;
669
670  gcc_assert (tok != &eof_token);
671  tok->type = CPP_PURGED;
672  tok->location = UNKNOWN_LOCATION;
673  tok->u.value = NULL_TREE;
674  tok->keyword = RID_MAX;
675
676  do
677    {
678      tok++;
679      if (tok == lexer->last_token)
680	{
681	  tok = &eof_token;
682	  break;
683	}
684    }
685  while (tok->type == CPP_PURGED);
686  lexer->next_token = tok;
687}
688
689/* Permanently remove all tokens after TOK, up to, but not
690   including, the token that will be returned next by
691   cp_lexer_peek_token.  */
692
693static void
694cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
695{
696  cp_token *peek = lexer->next_token;
697
698  if (peek == &eof_token)
699    peek = lexer->last_token;
700
701  gcc_assert (tok < peek);
702
703  for ( tok += 1; tok != peek; tok += 1)
704    {
705      tok->type = CPP_PURGED;
706      tok->location = UNKNOWN_LOCATION;
707      tok->u.value = NULL_TREE;
708      tok->keyword = RID_MAX;
709    }
710}
711
712/* Begin saving tokens.  All tokens consumed after this point will be
713   preserved.  */
714
715static void
716cp_lexer_save_tokens (cp_lexer* lexer)
717{
718  /* Provide debugging output.  */
719  if (cp_lexer_debugging_p (lexer))
720    fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
721
722  VEC_safe_push (cp_token_position, heap,
723		 lexer->saved_tokens, lexer->next_token);
724}
725
726/* Commit to the portion of the token stream most recently saved.  */
727
728static void
729cp_lexer_commit_tokens (cp_lexer* lexer)
730{
731  /* Provide debugging output.  */
732  if (cp_lexer_debugging_p (lexer))
733    fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
734
735  VEC_pop (cp_token_position, lexer->saved_tokens);
736}
737
738/* Return all tokens saved since the last call to cp_lexer_save_tokens
739   to the token stream.  Stop saving tokens.  */
740
741static void
742cp_lexer_rollback_tokens (cp_lexer* lexer)
743{
744  /* Provide debugging output.  */
745  if (cp_lexer_debugging_p (lexer))
746    fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
747
748  lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
749}
750
751/* Print a representation of the TOKEN on the STREAM.  */
752
753#ifdef ENABLE_CHECKING
754
755static void
756cp_lexer_print_token (FILE * stream, cp_token *token)
757{
758  /* We don't use cpp_type2name here because the parser defines
759     a few tokens of its own.  */
760  static const char *const token_names[] = {
761    /* cpplib-defined token types */
762#define OP(e, s) #e,
763#define TK(e, s) #e,
764    TTYPE_TABLE
765#undef OP
766#undef TK
767    /* C++ parser token types - see "Manifest constants", above.  */
768    "KEYWORD",
769    "TEMPLATE_ID",
770    "NESTED_NAME_SPECIFIER",
771    "PURGED"
772  };
773
774  /* If we have a name for the token, print it out.  Otherwise, we
775     simply give the numeric code.  */
776  gcc_assert (token->type < ARRAY_SIZE(token_names));
777  fputs (token_names[token->type], stream);
778
779  /* For some tokens, print the associated data.  */
780  switch (token->type)
781    {
782    case CPP_KEYWORD:
783      /* Some keywords have a value that is not an IDENTIFIER_NODE.
784	 For example, `struct' is mapped to an INTEGER_CST.  */
785      if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786	break;
787      /* else fall through */
788    case CPP_NAME:
789      fputs (IDENTIFIER_POINTER (token->u.value), stream);
790      break;
791
792    case CPP_STRING:
793    case CPP_STRING16:
794    case CPP_STRING32:
795    case CPP_WSTRING:
796    case CPP_UTF8STRING:
797      fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
798      break;
799
800    default:
801      break;
802    }
803}
804
805/* Start emitting debugging information.  */
806
807static void
808cp_lexer_start_debugging (cp_lexer* lexer)
809{
810  lexer->debugging_p = true;
811}
812
813/* Stop emitting debugging information.  */
814
815static void
816cp_lexer_stop_debugging (cp_lexer* lexer)
817{
818  lexer->debugging_p = false;
819}
820
821#endif /* ENABLE_CHECKING */
822
823/* Create a new cp_token_cache, representing a range of tokens.  */
824
825static cp_token_cache *
826cp_token_cache_new (cp_token *first, cp_token *last)
827{
828  cp_token_cache *cache = GGC_NEW (cp_token_cache);
829  cache->first = first;
830  cache->last = last;
831  return cache;
832}
833
834
835/* Decl-specifiers.  */
836
837/* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
838
839static void
840clear_decl_specs (cp_decl_specifier_seq *decl_specs)
841{
842  memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
843}
844
845/* Declarators.  */
846
847/* Nothing other than the parser should be creating declarators;
848   declarators are a semi-syntactic representation of C++ entities.
849   Other parts of the front end that need to create entities (like
850   VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
851
852static cp_declarator *make_call_declarator
853  (cp_declarator *, tree, cp_cv_quals, tree, tree);
854static cp_declarator *make_array_declarator
855  (cp_declarator *, tree);
856static cp_declarator *make_pointer_declarator
857  (cp_cv_quals, cp_declarator *);
858static cp_declarator *make_reference_declarator
859  (cp_cv_quals, cp_declarator *, bool);
860static cp_parameter_declarator *make_parameter_declarator
861  (cp_decl_specifier_seq *, cp_declarator *, tree);
862static cp_declarator *make_ptrmem_declarator
863  (cp_cv_quals, tree, cp_declarator *);
864
865/* An erroneous declarator.  */
866static cp_declarator *cp_error_declarator;
867
868/* The obstack on which declarators and related data structures are
869   allocated.  */
870static struct obstack declarator_obstack;
871
872/* Alloc BYTES from the declarator memory pool.  */
873
874static inline void *
875alloc_declarator (size_t bytes)
876{
877  return obstack_alloc (&declarator_obstack, bytes);
878}
879
880/* Allocate a declarator of the indicated KIND.  Clear fields that are
881   common to all declarators.  */
882
883static cp_declarator *
884make_declarator (cp_declarator_kind kind)
885{
886  cp_declarator *declarator;
887
888  declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889  declarator->kind = kind;
890  declarator->attributes = NULL_TREE;
891  declarator->declarator = NULL;
892  declarator->parameter_pack_p = false;
893  declarator->id_loc = UNKNOWN_LOCATION;
894
895  return declarator;
896}
897
898/* Make a declarator for a generalized identifier.  If
899   QUALIFYING_SCOPE is non-NULL, the identifier is
900   QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901   UNQUALIFIED_NAME.  SFK indicates the kind of special function this
902   is, if any.   */
903
904static cp_declarator *
905make_id_declarator (tree qualifying_scope, tree unqualified_name,
906		    special_function_kind sfk)
907{
908  cp_declarator *declarator;
909
910  /* It is valid to write:
911
912       class C { void f(); };
913       typedef C D;
914       void D::f();
915
916     The standard is not clear about whether `typedef const C D' is
917     legal; as of 2002-09-15 the committee is considering that
918     question.  EDG 3.0 allows that syntax.  Therefore, we do as
919     well.  */
920  if (qualifying_scope && TYPE_P (qualifying_scope))
921    qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922
923  gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924	      || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925	      || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926
927  declarator = make_declarator (cdk_id);
928  declarator->u.id.qualifying_scope = qualifying_scope;
929  declarator->u.id.unqualified_name = unqualified_name;
930  declarator->u.id.sfk = sfk;
931
932  return declarator;
933}
934
935/* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
936   of modifiers such as const or volatile to apply to the pointer
937   type, represented as identifiers.  */
938
939cp_declarator *
940make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941{
942  cp_declarator *declarator;
943
944  declarator = make_declarator (cdk_pointer);
945  declarator->declarator = target;
946  declarator->u.pointer.qualifiers = cv_qualifiers;
947  declarator->u.pointer.class_type = NULL_TREE;
948  if (target)
949    {
950      declarator->parameter_pack_p = target->parameter_pack_p;
951      target->parameter_pack_p = false;
952    }
953  else
954    declarator->parameter_pack_p = false;
955
956  return declarator;
957}
958
959/* Like make_pointer_declarator -- but for references.  */
960
961cp_declarator *
962make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963			   bool rvalue_ref)
964{
965  cp_declarator *declarator;
966
967  declarator = make_declarator (cdk_reference);
968  declarator->declarator = target;
969  declarator->u.reference.qualifiers = cv_qualifiers;
970  declarator->u.reference.rvalue_ref = rvalue_ref;
971  if (target)
972    {
973      declarator->parameter_pack_p = target->parameter_pack_p;
974      target->parameter_pack_p = false;
975    }
976  else
977    declarator->parameter_pack_p = false;
978
979  return declarator;
980}
981
982/* Like make_pointer_declarator -- but for a pointer to a non-static
983   member of CLASS_TYPE.  */
984
985cp_declarator *
986make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987			cp_declarator *pointee)
988{
989  cp_declarator *declarator;
990
991  declarator = make_declarator (cdk_ptrmem);
992  declarator->declarator = pointee;
993  declarator->u.pointer.qualifiers = cv_qualifiers;
994  declarator->u.pointer.class_type = class_type;
995
996  if (pointee)
997    {
998      declarator->parameter_pack_p = pointee->parameter_pack_p;
999      pointee->parameter_pack_p = false;
1000    }
1001  else
1002    declarator->parameter_pack_p = false;
1003
1004  return declarator;
1005}
1006
1007/* Make a declarator for the function given by TARGET, with the
1008   indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1009   "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1010   indicates what exceptions can be thrown.  */
1011
1012cp_declarator *
1013make_call_declarator (cp_declarator *target,
1014		      tree parms,
1015		      cp_cv_quals cv_qualifiers,
1016		      tree exception_specification,
1017		      tree late_return_type)
1018{
1019  cp_declarator *declarator;
1020
1021  declarator = make_declarator (cdk_function);
1022  declarator->declarator = target;
1023  declarator->u.function.parameters = parms;
1024  declarator->u.function.qualifiers = cv_qualifiers;
1025  declarator->u.function.exception_specification = exception_specification;
1026  declarator->u.function.late_return_type = late_return_type;
1027  if (target)
1028    {
1029      declarator->parameter_pack_p = target->parameter_pack_p;
1030      target->parameter_pack_p = false;
1031    }
1032  else
1033    declarator->parameter_pack_p = false;
1034
1035  return declarator;
1036}
1037
1038/* Make a declarator for an array of BOUNDS elements, each of which is
1039   defined by ELEMENT.  */
1040
1041cp_declarator *
1042make_array_declarator (cp_declarator *element, tree bounds)
1043{
1044  cp_declarator *declarator;
1045
1046  declarator = make_declarator (cdk_array);
1047  declarator->declarator = element;
1048  declarator->u.array.bounds = bounds;
1049  if (element)
1050    {
1051      declarator->parameter_pack_p = element->parameter_pack_p;
1052      element->parameter_pack_p = false;
1053    }
1054  else
1055    declarator->parameter_pack_p = false;
1056
1057  return declarator;
1058}
1059
1060/* Determine whether the declarator we've seen so far can be a
1061   parameter pack, when followed by an ellipsis.  */
1062static bool
1063declarator_can_be_parameter_pack (cp_declarator *declarator)
1064{
1065  /* Search for a declarator name, or any other declarator that goes
1066     after the point where the ellipsis could appear in a parameter
1067     pack. If we find any of these, then this declarator can not be
1068     made into a parameter pack.  */
1069  bool found = false;
1070  while (declarator && !found)
1071    {
1072      switch ((int)declarator->kind)
1073	{
1074	case cdk_id:
1075	case cdk_array:
1076	  found = true;
1077	  break;
1078
1079	case cdk_error:
1080	  return true;
1081
1082	default:
1083	  declarator = declarator->declarator;
1084	  break;
1085	}
1086    }
1087
1088  return !found;
1089}
1090
1091cp_parameter_declarator *no_parameters;
1092
1093/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094   DECLARATOR and DEFAULT_ARGUMENT.  */
1095
1096cp_parameter_declarator *
1097make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098			   cp_declarator *declarator,
1099			   tree default_argument)
1100{
1101  cp_parameter_declarator *parameter;
1102
1103  parameter = ((cp_parameter_declarator *)
1104	       alloc_declarator (sizeof (cp_parameter_declarator)));
1105  parameter->next = NULL;
1106  if (decl_specifiers)
1107    parameter->decl_specifiers = *decl_specifiers;
1108  else
1109    clear_decl_specs (&parameter->decl_specifiers);
1110  parameter->declarator = declarator;
1111  parameter->default_argument = default_argument;
1112  parameter->ellipsis_p = false;
1113
1114  return parameter;
1115}
1116
1117/* Returns true iff DECLARATOR  is a declaration for a function.  */
1118
1119static bool
1120function_declarator_p (const cp_declarator *declarator)
1121{
1122  while (declarator)
1123    {
1124      if (declarator->kind == cdk_function
1125	  && declarator->declarator->kind == cdk_id)
1126	return true;
1127      if (declarator->kind == cdk_id
1128	  || declarator->kind == cdk_error)
1129	return false;
1130      declarator = declarator->declarator;
1131    }
1132  return false;
1133}
1134
1135/* The parser.  */
1136
1137/* Overview
1138   --------
1139
1140   A cp_parser parses the token stream as specified by the C++
1141   grammar.  Its job is purely parsing, not semantic analysis.  For
1142   example, the parser breaks the token stream into declarators,
1143   expressions, statements, and other similar syntactic constructs.
1144   It does not check that the types of the expressions on either side
1145   of an assignment-statement are compatible, or that a function is
1146   not declared with a parameter of type `void'.
1147
1148   The parser invokes routines elsewhere in the compiler to perform
1149   semantic analysis and to build up the abstract syntax tree for the
1150   code processed.
1151
1152   The parser (and the template instantiation code, which is, in a
1153   way, a close relative of parsing) are the only parts of the
1154   compiler that should be calling push_scope and pop_scope, or
1155   related functions.  The parser (and template instantiation code)
1156   keeps track of what scope is presently active; everything else
1157   should simply honor that.  (The code that generates static
1158   initializers may also need to set the scope, in order to check
1159   access control correctly when emitting the initializers.)
1160
1161   Methodology
1162   -----------
1163
1164   The parser is of the standard recursive-descent variety.  Upcoming
1165   tokens in the token stream are examined in order to determine which
1166   production to use when parsing a non-terminal.  Some C++ constructs
1167   require arbitrary look ahead to disambiguate.  For example, it is
1168   impossible, in the general case, to tell whether a statement is an
1169   expression or declaration without scanning the entire statement.
1170   Therefore, the parser is capable of "parsing tentatively."  When the
1171   parser is not sure what construct comes next, it enters this mode.
1172   Then, while we attempt to parse the construct, the parser queues up
1173   error messages, rather than issuing them immediately, and saves the
1174   tokens it consumes.  If the construct is parsed successfully, the
1175   parser "commits", i.e., it issues any queued error messages and
1176   the tokens that were being preserved are permanently discarded.
1177   If, however, the construct is not parsed successfully, the parser
1178   rolls back its state completely so that it can resume parsing using
1179   a different alternative.
1180
1181   Future Improvements
1182   -------------------
1183
1184   The performance of the parser could probably be improved substantially.
1185   We could often eliminate the need to parse tentatively by looking ahead
1186   a little bit.  In some places, this approach might not entirely eliminate
1187   the need to parse tentatively, but it might still speed up the average
1188   case.  */
1189
1190/* Flags that are passed to some parsing functions.  These values can
1191   be bitwise-ored together.  */
1192
1193enum
1194{
1195  /* No flags.  */
1196  CP_PARSER_FLAGS_NONE = 0x0,
1197  /* The construct is optional.  If it is not present, then no error
1198     should be issued.  */
1199  CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200  /* When parsing a type-specifier, treat user-defined type-names
1201     as non-type identifiers.  */
1202  CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1203  /* When parsing a type-specifier, do not try to parse a class-specifier
1204     or enum-specifier.  */
1205  CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1206};
1207
1208/* This type is used for parameters and variables which hold
1209   combinations of the above flags.  */
1210typedef int cp_parser_flags;
1211
1212/* The different kinds of declarators we want to parse.  */
1213
1214typedef enum cp_parser_declarator_kind
1215{
1216  /* We want an abstract declarator.  */
1217  CP_PARSER_DECLARATOR_ABSTRACT,
1218  /* We want a named declarator.  */
1219  CP_PARSER_DECLARATOR_NAMED,
1220  /* We don't mind, but the name must be an unqualified-id.  */
1221  CP_PARSER_DECLARATOR_EITHER
1222} cp_parser_declarator_kind;
1223
1224/* The precedence values used to parse binary expressions.  The minimum value
1225   of PREC must be 1, because zero is reserved to quickly discriminate
1226   binary operators from other tokens.  */
1227
1228enum cp_parser_prec
1229{
1230  PREC_NOT_OPERATOR,
1231  PREC_LOGICAL_OR_EXPRESSION,
1232  PREC_LOGICAL_AND_EXPRESSION,
1233  PREC_INCLUSIVE_OR_EXPRESSION,
1234  PREC_EXCLUSIVE_OR_EXPRESSION,
1235  PREC_AND_EXPRESSION,
1236  PREC_EQUALITY_EXPRESSION,
1237  PREC_RELATIONAL_EXPRESSION,
1238  PREC_SHIFT_EXPRESSION,
1239  PREC_ADDITIVE_EXPRESSION,
1240  PREC_MULTIPLICATIVE_EXPRESSION,
1241  PREC_PM_EXPRESSION,
1242  NUM_PREC_VALUES = PREC_PM_EXPRESSION
1243};
1244
1245/* A mapping from a token type to a corresponding tree node type, with a
1246   precedence value.  */
1247
1248typedef struct cp_parser_binary_operations_map_node
1249{
1250  /* The token type.  */
1251  enum cpp_ttype token_type;
1252  /* The corresponding tree code.  */
1253  enum tree_code tree_type;
1254  /* The precedence of this operator.  */
1255  enum cp_parser_prec prec;
1256} cp_parser_binary_operations_map_node;
1257
1258/* The status of a tentative parse.  */
1259
1260typedef enum cp_parser_status_kind
1261{
1262  /* No errors have occurred.  */
1263  CP_PARSER_STATUS_KIND_NO_ERROR,
1264  /* An error has occurred.  */
1265  CP_PARSER_STATUS_KIND_ERROR,
1266  /* We are committed to this tentative parse, whether or not an error
1267     has occurred.  */
1268  CP_PARSER_STATUS_KIND_COMMITTED
1269} cp_parser_status_kind;
1270
1271typedef struct cp_parser_expression_stack_entry
1272{
1273  /* Left hand side of the binary operation we are currently
1274     parsing.  */
1275  tree lhs;
1276  /* Original tree code for left hand side, if it was a binary
1277     expression itself (used for -Wparentheses).  */
1278  enum tree_code lhs_type;
1279  /* Tree code for the binary operation we are parsing.  */
1280  enum tree_code tree_type;
1281  /* Precedence of the binary operation we are parsing.  */
1282  enum cp_parser_prec prec;
1283} cp_parser_expression_stack_entry;
1284
1285/* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1286   entries because precedence levels on the stack are monotonically
1287   increasing.  */
1288typedef struct cp_parser_expression_stack_entry
1289  cp_parser_expression_stack[NUM_PREC_VALUES];
1290
1291/* Context that is saved and restored when parsing tentatively.  */
1292typedef struct GTY (()) cp_parser_context {
1293  /* If this is a tentative parsing context, the status of the
1294     tentative parse.  */
1295  enum cp_parser_status_kind status;
1296  /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1297     that are looked up in this context must be looked up both in the
1298     scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1299     the context of the containing expression.  */
1300  tree object_type;
1301
1302  /* The next parsing context in the stack.  */
1303  struct cp_parser_context *next;
1304} cp_parser_context;
1305
1306/* Prototypes.  */
1307
1308/* Constructors and destructors.  */
1309
1310static cp_parser_context *cp_parser_context_new
1311  (cp_parser_context *);
1312
1313/* Class variables.  */
1314
1315static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1316
1317/* The operator-precedence table used by cp_parser_binary_expression.
1318   Transformed into an associative array (binops_by_token) by
1319   cp_parser_new.  */
1320
1321static const cp_parser_binary_operations_map_node binops[] = {
1322  { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1323  { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1324
1325  { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326  { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327  { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1328
1329  { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330  { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1331
1332  { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333  { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1334
1335  { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336  { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1337  { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338  { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1339
1340  { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1341  { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1342
1343  { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1344
1345  { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1346
1347  { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1348
1349  { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1350
1351  { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1352};
1353
1354/* The same as binops, but initialized by cp_parser_new so that
1355   binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1356   for speed.  */
1357static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1358
1359/* Constructors and destructors.  */
1360
1361/* Construct a new context.  The context below this one on the stack
1362   is given by NEXT.  */
1363
1364static cp_parser_context *
1365cp_parser_context_new (cp_parser_context* next)
1366{
1367  cp_parser_context *context;
1368
1369  /* Allocate the storage.  */
1370  if (cp_parser_context_free_list != NULL)
1371    {
1372      /* Pull the first entry from the free list.  */
1373      context = cp_parser_context_free_list;
1374      cp_parser_context_free_list = context->next;
1375      memset (context, 0, sizeof (*context));
1376    }
1377  else
1378    context = GGC_CNEW (cp_parser_context);
1379
1380  /* No errors have occurred yet in this context.  */
1381  context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1382  /* If this is not the bottommost context, copy information that we
1383     need from the previous context.  */
1384  if (next)
1385    {
1386      /* If, in the NEXT context, we are parsing an `x->' or `x.'
1387	 expression, then we are parsing one in this context, too.  */
1388      context->object_type = next->object_type;
1389      /* Thread the stack.  */
1390      context->next = next;
1391    }
1392
1393  return context;
1394}
1395
1396/* The cp_parser structure represents the C++ parser.  */
1397
1398typedef struct GTY(()) cp_parser {
1399  /* The lexer from which we are obtaining tokens.  */
1400  cp_lexer *lexer;
1401
1402  /* The scope in which names should be looked up.  If NULL_TREE, then
1403     we look up names in the scope that is currently open in the
1404     source program.  If non-NULL, this is either a TYPE or
1405     NAMESPACE_DECL for the scope in which we should look.  It can
1406     also be ERROR_MARK, when we've parsed a bogus scope.
1407
1408     This value is not cleared automatically after a name is looked
1409     up, so we must be careful to clear it before starting a new look
1410     up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1411     will look up `Z' in the scope of `X', rather than the current
1412     scope.)  Unfortunately, it is difficult to tell when name lookup
1413     is complete, because we sometimes peek at a token, look it up,
1414     and then decide not to consume it.   */
1415  tree scope;
1416
1417  /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1418     last lookup took place.  OBJECT_SCOPE is used if an expression
1419     like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1420     respectively.  QUALIFYING_SCOPE is used for an expression of the
1421     form "X::Y"; it refers to X.  */
1422  tree object_scope;
1423  tree qualifying_scope;
1424
1425  /* A stack of parsing contexts.  All but the bottom entry on the
1426     stack will be tentative contexts.
1427
1428     We parse tentatively in order to determine which construct is in
1429     use in some situations.  For example, in order to determine
1430     whether a statement is an expression-statement or a
1431     declaration-statement we parse it tentatively as a
1432     declaration-statement.  If that fails, we then reparse the same
1433     token stream as an expression-statement.  */
1434  cp_parser_context *context;
1435
1436  /* True if we are parsing GNU C++.  If this flag is not set, then
1437     GNU extensions are not recognized.  */
1438  bool allow_gnu_extensions_p;
1439
1440  /* TRUE if the `>' token should be interpreted as the greater-than
1441     operator.  FALSE if it is the end of a template-id or
1442     template-parameter-list. In C++0x mode, this flag also applies to
1443     `>>' tokens, which are viewed as two consecutive `>' tokens when
1444     this flag is FALSE.  */
1445  bool greater_than_is_operator_p;
1446
1447  /* TRUE if default arguments are allowed within a parameter list
1448     that starts at this point. FALSE if only a gnu extension makes
1449     them permissible.  */
1450  bool default_arg_ok_p;
1451
1452  /* TRUE if we are parsing an integral constant-expression.  See
1453     [expr.const] for a precise definition.  */
1454  bool integral_constant_expression_p;
1455
1456  /* TRUE if we are parsing an integral constant-expression -- but a
1457     non-constant expression should be permitted as well.  This flag
1458     is used when parsing an array bound so that GNU variable-length
1459     arrays are tolerated.  */
1460  bool allow_non_integral_constant_expression_p;
1461
1462  /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1463     been seen that makes the expression non-constant.  */
1464  bool non_integral_constant_expression_p;
1465
1466  /* TRUE if local variable names and `this' are forbidden in the
1467     current context.  */
1468  bool local_variables_forbidden_p;
1469
1470  /* TRUE if the declaration we are parsing is part of a
1471     linkage-specification of the form `extern string-literal
1472     declaration'.  */
1473  bool in_unbraced_linkage_specification_p;
1474
1475  /* TRUE if we are presently parsing a declarator, after the
1476     direct-declarator.  */
1477  bool in_declarator_p;
1478
1479  /* TRUE if we are presently parsing a template-argument-list.  */
1480  bool in_template_argument_list_p;
1481
1482  /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1483     to IN_OMP_BLOCK if parsing OpenMP structured block and
1484     IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1485     this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1486     iteration-statement, OpenMP block or loop within that switch.  */
1487#define IN_SWITCH_STMT		1
1488#define IN_ITERATION_STMT	2
1489#define IN_OMP_BLOCK		4
1490#define IN_OMP_FOR		8
1491#define IN_IF_STMT             16
1492  unsigned char in_statement;
1493
1494  /* TRUE if we are presently parsing the body of a switch statement.
1495     Note that this doesn't quite overlap with in_statement above.
1496     The difference relates to giving the right sets of error messages:
1497     "case not in switch" vs "break statement used with OpenMP...".  */
1498  bool in_switch_statement_p;
1499
1500  /* TRUE if we are parsing a type-id in an expression context.  In
1501     such a situation, both "type (expr)" and "type (type)" are valid
1502     alternatives.  */
1503  bool in_type_id_in_expr_p;
1504
1505  /* TRUE if we are currently in a header file where declarations are
1506     implicitly extern "C".  */
1507  bool implicit_extern_c;
1508
1509  /* TRUE if strings in expressions should be translated to the execution
1510     character set.  */
1511  bool translate_strings_p;
1512
1513  /* TRUE if we are presently parsing the body of a function, but not
1514     a local class.  */
1515  bool in_function_body;
1516
1517  /* If non-NULL, then we are parsing a construct where new type
1518     definitions are not permitted.  The string stored here will be
1519     issued as an error message if a type is defined.  */
1520  const char *type_definition_forbidden_message;
1521
1522  /* A list of lists. The outer list is a stack, used for member
1523     functions of local classes. At each level there are two sub-list,
1524     one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1525     sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1526     TREE_VALUE's. The functions are chained in reverse declaration
1527     order.
1528
1529     The TREE_PURPOSE sublist contains those functions with default
1530     arguments that need post processing, and the TREE_VALUE sublist
1531     contains those functions with definitions that need post
1532     processing.
1533
1534     These lists can only be processed once the outermost class being
1535     defined is complete.  */
1536  tree unparsed_functions_queues;
1537
1538  /* The number of classes whose definitions are currently in
1539     progress.  */
1540  unsigned num_classes_being_defined;
1541
1542  /* The number of template parameter lists that apply directly to the
1543     current declaration.  */
1544  unsigned num_template_parameter_lists;
1545} cp_parser;
1546
1547/* Prototypes.  */
1548
1549/* Constructors and destructors.  */
1550
1551static cp_parser *cp_parser_new
1552  (void);
1553
1554/* Routines to parse various constructs.
1555
1556   Those that return `tree' will return the error_mark_node (rather
1557   than NULL_TREE) if a parse error occurs, unless otherwise noted.
1558   Sometimes, they will return an ordinary node if error-recovery was
1559   attempted, even though a parse error occurred.  So, to check
1560   whether or not a parse error occurred, you should always use
1561   cp_parser_error_occurred.  If the construct is optional (indicated
1562   either by an `_opt' in the name of the function that does the
1563   parsing or via a FLAGS parameter), then NULL_TREE is returned if
1564   the construct is not present.  */
1565
1566/* Lexical conventions [gram.lex]  */
1567
1568static tree cp_parser_identifier
1569  (cp_parser *);
1570static tree cp_parser_string_literal
1571  (cp_parser *, bool, bool);
1572
1573/* Basic concepts [gram.basic]  */
1574
1575static bool cp_parser_translation_unit
1576  (cp_parser *);
1577
1578/* Expressions [gram.expr]  */
1579
1580static tree cp_parser_primary_expression
1581  (cp_parser *, bool, bool, bool, cp_id_kind *);
1582static tree cp_parser_id_expression
1583  (cp_parser *, bool, bool, bool *, bool, bool);
1584static tree cp_parser_unqualified_id
1585  (cp_parser *, bool, bool, bool, bool);
1586static tree cp_parser_nested_name_specifier_opt
1587  (cp_parser *, bool, bool, bool, bool);
1588static tree cp_parser_nested_name_specifier
1589  (cp_parser *, bool, bool, bool, bool);
1590static tree cp_parser_qualifying_entity
1591  (cp_parser *, bool, bool, bool, bool, bool);
1592static tree cp_parser_postfix_expression
1593  (cp_parser *, bool, bool, bool, cp_id_kind *);
1594static tree cp_parser_postfix_open_square_expression
1595  (cp_parser *, tree, bool);
1596static tree cp_parser_postfix_dot_deref_expression
1597  (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1598static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1599  (cp_parser *, bool, bool, bool, bool *);
1600static void cp_parser_pseudo_destructor_name
1601  (cp_parser *, tree *, tree *);
1602static tree cp_parser_unary_expression
1603  (cp_parser *, bool, bool, cp_id_kind *);
1604static enum tree_code cp_parser_unary_operator
1605  (cp_token *);
1606static tree cp_parser_new_expression
1607  (cp_parser *);
1608static VEC(tree,gc) *cp_parser_new_placement
1609  (cp_parser *);
1610static tree cp_parser_new_type_id
1611  (cp_parser *, tree *);
1612static cp_declarator *cp_parser_new_declarator_opt
1613  (cp_parser *);
1614static cp_declarator *cp_parser_direct_new_declarator
1615  (cp_parser *);
1616static VEC(tree,gc) *cp_parser_new_initializer
1617  (cp_parser *);
1618static tree cp_parser_delete_expression
1619  (cp_parser *);
1620static tree cp_parser_cast_expression
1621  (cp_parser *, bool, bool, cp_id_kind *);
1622static tree cp_parser_binary_expression
1623  (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1624static tree cp_parser_question_colon_clause
1625  (cp_parser *, tree);
1626static tree cp_parser_assignment_expression
1627  (cp_parser *, bool, cp_id_kind *);
1628static enum tree_code cp_parser_assignment_operator_opt
1629  (cp_parser *);
1630static tree cp_parser_expression
1631  (cp_parser *, bool, cp_id_kind *);
1632static tree cp_parser_constant_expression
1633  (cp_parser *, bool, bool *);
1634static tree cp_parser_builtin_offsetof
1635  (cp_parser *);
1636static tree cp_parser_lambda_expression
1637  (cp_parser *);
1638static void cp_parser_lambda_introducer
1639  (cp_parser *, tree);
1640static void cp_parser_lambda_declarator_opt
1641  (cp_parser *, tree);
1642static void cp_parser_lambda_body
1643  (cp_parser *, tree);
1644
1645/* Statements [gram.stmt.stmt]  */
1646
1647static void cp_parser_statement
1648  (cp_parser *, tree, bool, bool *);
1649static void cp_parser_label_for_labeled_statement
1650  (cp_parser *);
1651static tree cp_parser_expression_statement
1652  (cp_parser *, tree);
1653static tree cp_parser_compound_statement
1654  (cp_parser *, tree, bool);
1655static void cp_parser_statement_seq_opt
1656  (cp_parser *, tree);
1657static tree cp_parser_selection_statement
1658  (cp_parser *, bool *);
1659static tree cp_parser_condition
1660  (cp_parser *);
1661static tree cp_parser_iteration_statement
1662  (cp_parser *);
1663static void cp_parser_for_init_statement
1664  (cp_parser *);
1665static tree cp_parser_jump_statement
1666  (cp_parser *);
1667static void cp_parser_declaration_statement
1668  (cp_parser *);
1669
1670static tree cp_parser_implicitly_scoped_statement
1671  (cp_parser *, bool *);
1672static void cp_parser_already_scoped_statement
1673  (cp_parser *);
1674
1675/* Declarations [gram.dcl.dcl] */
1676
1677static void cp_parser_declaration_seq_opt
1678  (cp_parser *);
1679static void cp_parser_declaration
1680  (cp_parser *);
1681static void cp_parser_block_declaration
1682  (cp_parser *, bool);
1683static void cp_parser_simple_declaration
1684  (cp_parser *, bool);
1685static void cp_parser_decl_specifier_seq
1686  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1687static tree cp_parser_storage_class_specifier_opt
1688  (cp_parser *);
1689static tree cp_parser_function_specifier_opt
1690  (cp_parser *, cp_decl_specifier_seq *);
1691static tree cp_parser_type_specifier
1692  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1693   int *, bool *);
1694static tree cp_parser_simple_type_specifier
1695  (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1696static tree cp_parser_type_name
1697  (cp_parser *);
1698static tree cp_parser_nonclass_name
1699  (cp_parser* parser);
1700static tree cp_parser_elaborated_type_specifier
1701  (cp_parser *, bool, bool);
1702static tree cp_parser_enum_specifier
1703  (cp_parser *);
1704static void cp_parser_enumerator_list
1705  (cp_parser *, tree);
1706static void cp_parser_enumerator_definition
1707  (cp_parser *, tree);
1708static tree cp_parser_namespace_name
1709  (cp_parser *);
1710static void cp_parser_namespace_definition
1711  (cp_parser *);
1712static void cp_parser_namespace_body
1713  (cp_parser *);
1714static tree cp_parser_qualified_namespace_specifier
1715  (cp_parser *);
1716static void cp_parser_namespace_alias_definition
1717  (cp_parser *);
1718static bool cp_parser_using_declaration
1719  (cp_parser *, bool);
1720static void cp_parser_using_directive
1721  (cp_parser *);
1722static void cp_parser_asm_definition
1723  (cp_parser *);
1724static void cp_parser_linkage_specification
1725  (cp_parser *);
1726static void cp_parser_static_assert
1727  (cp_parser *, bool);
1728static tree cp_parser_decltype
1729  (cp_parser *);
1730
1731/* Declarators [gram.dcl.decl] */
1732
1733static tree cp_parser_init_declarator
1734  (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1735static cp_declarator *cp_parser_declarator
1736  (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1737static cp_declarator *cp_parser_direct_declarator
1738  (cp_parser *, cp_parser_declarator_kind, int *, bool);
1739static enum tree_code cp_parser_ptr_operator
1740  (cp_parser *, tree *, cp_cv_quals *);
1741static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1742  (cp_parser *);
1743static tree cp_parser_late_return_type_opt
1744  (cp_parser *);
1745static tree cp_parser_declarator_id
1746  (cp_parser *, bool);
1747static tree cp_parser_type_id
1748  (cp_parser *);
1749static tree cp_parser_template_type_arg
1750  (cp_parser *);
1751static tree cp_parser_trailing_type_id (cp_parser *);
1752static tree cp_parser_type_id_1
1753  (cp_parser *, bool, bool);
1754static void cp_parser_type_specifier_seq
1755  (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1756static tree cp_parser_parameter_declaration_clause
1757  (cp_parser *);
1758static tree cp_parser_parameter_declaration_list
1759  (cp_parser *, bool *);
1760static cp_parameter_declarator *cp_parser_parameter_declaration
1761  (cp_parser *, bool, bool *);
1762static tree cp_parser_default_argument
1763  (cp_parser *, bool);
1764static void cp_parser_function_body
1765  (cp_parser *);
1766static tree cp_parser_initializer
1767  (cp_parser *, bool *, bool *);
1768static tree cp_parser_initializer_clause
1769  (cp_parser *, bool *);
1770static tree cp_parser_braced_list
1771  (cp_parser*, bool*);
1772static VEC(constructor_elt,gc) *cp_parser_initializer_list
1773  (cp_parser *, bool *);
1774
1775static bool cp_parser_ctor_initializer_opt_and_function_body
1776  (cp_parser *);
1777
1778/* Classes [gram.class] */
1779
1780static tree cp_parser_class_name
1781  (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1782static tree cp_parser_class_specifier
1783  (cp_parser *);
1784static tree cp_parser_class_head
1785  (cp_parser *, bool *, tree *, tree *);
1786static enum tag_types cp_parser_class_key
1787  (cp_parser *);
1788static void cp_parser_member_specification_opt
1789  (cp_parser *);
1790static void cp_parser_member_declaration
1791  (cp_parser *);
1792static tree cp_parser_pure_specifier
1793  (cp_parser *);
1794static tree cp_parser_constant_initializer
1795  (cp_parser *);
1796
1797/* Derived classes [gram.class.derived] */
1798
1799static tree cp_parser_base_clause
1800  (cp_parser *);
1801static tree cp_parser_base_specifier
1802  (cp_parser *);
1803
1804/* Special member functions [gram.special] */
1805
1806static tree cp_parser_conversion_function_id
1807  (cp_parser *);
1808static tree cp_parser_conversion_type_id
1809  (cp_parser *);
1810static cp_declarator *cp_parser_conversion_declarator_opt
1811  (cp_parser *);
1812static bool cp_parser_ctor_initializer_opt
1813  (cp_parser *);
1814static void cp_parser_mem_initializer_list
1815  (cp_parser *);
1816static tree cp_parser_mem_initializer
1817  (cp_parser *);
1818static tree cp_parser_mem_initializer_id
1819  (cp_parser *);
1820
1821/* Overloading [gram.over] */
1822
1823static tree cp_parser_operator_function_id
1824  (cp_parser *);
1825static tree cp_parser_operator
1826  (cp_parser *);
1827
1828/* Templates [gram.temp] */
1829
1830static void cp_parser_template_declaration
1831  (cp_parser *, bool);
1832static tree cp_parser_template_parameter_list
1833  (cp_parser *);
1834static tree cp_parser_template_parameter
1835  (cp_parser *, bool *, bool *);
1836static tree cp_parser_type_parameter
1837  (cp_parser *, bool *);
1838static tree cp_parser_template_id
1839  (cp_parser *, bool, bool, bool);
1840static tree cp_parser_template_name
1841  (cp_parser *, bool, bool, bool, bool *);
1842static tree cp_parser_template_argument_list
1843  (cp_parser *);
1844static tree cp_parser_template_argument
1845  (cp_parser *);
1846static void cp_parser_explicit_instantiation
1847  (cp_parser *);
1848static void cp_parser_explicit_specialization
1849  (cp_parser *);
1850
1851/* Exception handling [gram.exception] */
1852
1853static tree cp_parser_try_block
1854  (cp_parser *);
1855static bool cp_parser_function_try_block
1856  (cp_parser *);
1857static void cp_parser_handler_seq
1858  (cp_parser *);
1859static void cp_parser_handler
1860  (cp_parser *);
1861static tree cp_parser_exception_declaration
1862  (cp_parser *);
1863static tree cp_parser_throw_expression
1864  (cp_parser *);
1865static tree cp_parser_exception_specification_opt
1866  (cp_parser *);
1867static tree cp_parser_type_id_list
1868  (cp_parser *);
1869
1870/* GNU Extensions */
1871
1872static tree cp_parser_asm_specification_opt
1873  (cp_parser *);
1874static tree cp_parser_asm_operand_list
1875  (cp_parser *);
1876static tree cp_parser_asm_clobber_list
1877  (cp_parser *);
1878static tree cp_parser_asm_label_list
1879  (cp_parser *);
1880static tree cp_parser_attributes_opt
1881  (cp_parser *);
1882static tree cp_parser_attribute_list
1883  (cp_parser *);
1884static bool cp_parser_extension_opt
1885  (cp_parser *, int *);
1886static void cp_parser_label_declaration
1887  (cp_parser *);
1888
1889enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1890static bool cp_parser_pragma
1891  (cp_parser *, enum pragma_context);
1892
1893/* Objective-C++ Productions */
1894
1895static tree cp_parser_objc_message_receiver
1896  (cp_parser *);
1897static tree cp_parser_objc_message_args
1898  (cp_parser *);
1899static tree cp_parser_objc_message_expression
1900  (cp_parser *);
1901static tree cp_parser_objc_encode_expression
1902  (cp_parser *);
1903static tree cp_parser_objc_defs_expression
1904  (cp_parser *);
1905static tree cp_parser_objc_protocol_expression
1906  (cp_parser *);
1907static tree cp_parser_objc_selector_expression
1908  (cp_parser *);
1909static tree cp_parser_objc_expression
1910  (cp_parser *);
1911static bool cp_parser_objc_selector_p
1912  (enum cpp_ttype);
1913static tree cp_parser_objc_selector
1914  (cp_parser *);
1915static tree cp_parser_objc_protocol_refs_opt
1916  (cp_parser *);
1917static void cp_parser_objc_declaration
1918  (cp_parser *);
1919static tree cp_parser_objc_statement
1920  (cp_parser *);
1921
1922/* Utility Routines */
1923
1924static tree cp_parser_lookup_name
1925  (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1926static tree cp_parser_lookup_name_simple
1927  (cp_parser *, tree, location_t);
1928static tree cp_parser_maybe_treat_template_as_class
1929  (tree, bool);
1930static bool cp_parser_check_declarator_template_parameters
1931  (cp_parser *, cp_declarator *, location_t);
1932static bool cp_parser_check_template_parameters
1933  (cp_parser *, unsigned, location_t, cp_declarator *);
1934static tree cp_parser_simple_cast_expression
1935  (cp_parser *);
1936static tree cp_parser_global_scope_opt
1937  (cp_parser *, bool);
1938static bool cp_parser_constructor_declarator_p
1939  (cp_parser *, bool);
1940static tree cp_parser_function_definition_from_specifiers_and_declarator
1941  (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1942static tree cp_parser_function_definition_after_declarator
1943  (cp_parser *, bool);
1944static void cp_parser_template_declaration_after_export
1945  (cp_parser *, bool);
1946static void cp_parser_perform_template_parameter_access_checks
1947  (VEC (deferred_access_check,gc)*);
1948static tree cp_parser_single_declaration
1949  (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1950static tree cp_parser_functional_cast
1951  (cp_parser *, tree);
1952static tree cp_parser_save_member_function_body
1953  (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1954static tree cp_parser_enclosed_template_argument_list
1955  (cp_parser *);
1956static void cp_parser_save_default_args
1957  (cp_parser *, tree);
1958static void cp_parser_late_parsing_for_member
1959  (cp_parser *, tree);
1960static void cp_parser_late_parsing_default_args
1961  (cp_parser *, tree);
1962static tree cp_parser_sizeof_operand
1963  (cp_parser *, enum rid);
1964static tree cp_parser_trait_expr
1965  (cp_parser *, enum rid);
1966static bool cp_parser_declares_only_class_p
1967  (cp_parser *);
1968static void cp_parser_set_storage_class
1969  (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1970static void cp_parser_set_decl_spec_type
1971  (cp_decl_specifier_seq *, tree, location_t, bool);
1972static bool cp_parser_friend_p
1973  (const cp_decl_specifier_seq *);
1974static cp_token *cp_parser_require
1975  (cp_parser *, enum cpp_ttype, const char *);
1976static cp_token *cp_parser_require_keyword
1977  (cp_parser *, enum rid, const char *);
1978static bool cp_parser_token_starts_function_definition_p
1979  (cp_token *);
1980static bool cp_parser_next_token_starts_class_definition_p
1981  (cp_parser *);
1982static bool cp_parser_next_token_ends_template_argument_p
1983  (cp_parser *);
1984static bool cp_parser_nth_token_starts_template_argument_list_p
1985  (cp_parser *, size_t);
1986static enum tag_types cp_parser_token_is_class_key
1987  (cp_token *);
1988static void cp_parser_check_class_key
1989  (enum tag_types, tree type);
1990static void cp_parser_check_access_in_redeclaration
1991  (tree type, location_t location);
1992static bool cp_parser_optional_template_keyword
1993  (cp_parser *);
1994static void cp_parser_pre_parsed_nested_name_specifier
1995  (cp_parser *);
1996static bool cp_parser_cache_group
1997  (cp_parser *, enum cpp_ttype, unsigned);
1998static void cp_parser_parse_tentatively
1999  (cp_parser *);
2000static void cp_parser_commit_to_tentative_parse
2001  (cp_parser *);
2002static void cp_parser_abort_tentative_parse
2003  (cp_parser *);
2004static bool cp_parser_parse_definitely
2005  (cp_parser *);
2006static inline bool cp_parser_parsing_tentatively
2007  (cp_parser *);
2008static bool cp_parser_uncommitted_to_tentative_parse_p
2009  (cp_parser *);
2010static void cp_parser_error
2011  (cp_parser *, const char *);
2012static void cp_parser_name_lookup_error
2013  (cp_parser *, tree, tree, const char *, location_t);
2014static bool cp_parser_simulate_error
2015  (cp_parser *);
2016static bool cp_parser_check_type_definition
2017  (cp_parser *);
2018static void cp_parser_check_for_definition_in_return_type
2019  (cp_declarator *, tree, location_t type_location);
2020static void cp_parser_check_for_invalid_template_id
2021  (cp_parser *, tree, location_t location);
2022static bool cp_parser_non_integral_constant_expression
2023  (cp_parser *, const char *);
2024static void cp_parser_diagnose_invalid_type_name
2025  (cp_parser *, tree, tree, location_t);
2026static bool cp_parser_parse_and_diagnose_invalid_type_name
2027  (cp_parser *);
2028static int cp_parser_skip_to_closing_parenthesis
2029  (cp_parser *, bool, bool, bool);
2030static void cp_parser_skip_to_end_of_statement
2031  (cp_parser *);
2032static void cp_parser_consume_semicolon_at_end_of_statement
2033  (cp_parser *);
2034static void cp_parser_skip_to_end_of_block_or_statement
2035  (cp_parser *);
2036static bool cp_parser_skip_to_closing_brace
2037  (cp_parser *);
2038static void cp_parser_skip_to_end_of_template_parameter_list
2039  (cp_parser *);
2040static void cp_parser_skip_to_pragma_eol
2041  (cp_parser*, cp_token *);
2042static bool cp_parser_error_occurred
2043  (cp_parser *);
2044static bool cp_parser_allow_gnu_extensions_p
2045  (cp_parser *);
2046static bool cp_parser_is_string_literal
2047  (cp_token *);
2048static bool cp_parser_is_keyword
2049  (cp_token *, enum rid);
2050static tree cp_parser_make_typename_type
2051  (cp_parser *, tree, tree, location_t location);
2052static cp_declarator * cp_parser_make_indirect_declarator
2053  (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2054
2055/* Returns nonzero if we are parsing tentatively.  */
2056
2057static inline bool
2058cp_parser_parsing_tentatively (cp_parser* parser)
2059{
2060  return parser->context->next != NULL;
2061}
2062
2063/* Returns nonzero if TOKEN is a string literal.  */
2064
2065static bool
2066cp_parser_is_string_literal (cp_token* token)
2067{
2068  return (token->type == CPP_STRING ||
2069	  token->type == CPP_STRING16 ||
2070	  token->type == CPP_STRING32 ||
2071	  token->type == CPP_WSTRING ||
2072	  token->type == CPP_UTF8STRING);
2073}
2074
2075/* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2076
2077static bool
2078cp_parser_is_keyword (cp_token* token, enum rid keyword)
2079{
2080  return token->keyword == keyword;
2081}
2082
2083/* If not parsing tentatively, issue a diagnostic of the form
2084      FILE:LINE: MESSAGE before TOKEN
2085   where TOKEN is the next token in the input stream.  MESSAGE
2086   (specified by the caller) is usually of the form "expected
2087   OTHER-TOKEN".  */
2088
2089static void
2090cp_parser_error (cp_parser* parser, const char* message)
2091{
2092  if (!cp_parser_simulate_error (parser))
2093    {
2094      cp_token *token = cp_lexer_peek_token (parser->lexer);
2095      /* This diagnostic makes more sense if it is tagged to the line
2096	 of the token we just peeked at.  */
2097      cp_lexer_set_source_position_from_token (token);
2098
2099      if (token->type == CPP_PRAGMA)
2100	{
2101	  error_at (token->location,
2102		    "%<#pragma%> is not allowed here");
2103	  cp_parser_skip_to_pragma_eol (parser, token);
2104	  return;
2105	}
2106
2107      c_parse_error (message,
2108		     /* Because c_parser_error does not understand
2109			CPP_KEYWORD, keywords are treated like
2110			identifiers.  */
2111		     (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2112		     token->u.value, token->flags);
2113    }
2114}
2115
2116/* Issue an error about name-lookup failing.  NAME is the
2117   IDENTIFIER_NODE DECL is the result of
2118   the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2119   the thing that we hoped to find.  */
2120
2121static void
2122cp_parser_name_lookup_error (cp_parser* parser,
2123			     tree name,
2124			     tree decl,
2125			     const char* desired,
2126			     location_t location)
2127{
2128  /* If name lookup completely failed, tell the user that NAME was not
2129     declared.  */
2130  if (decl == error_mark_node)
2131    {
2132      if (parser->scope && parser->scope != global_namespace)
2133	error_at (location, "%<%E::%E%> has not been declared",
2134		  parser->scope, name);
2135      else if (parser->scope == global_namespace)
2136	error_at (location, "%<::%E%> has not been declared", name);
2137      else if (parser->object_scope
2138	       && !CLASS_TYPE_P (parser->object_scope))
2139	error_at (location, "request for member %qE in non-class type %qT",
2140		  name, parser->object_scope);
2141      else if (parser->object_scope)
2142	error_at (location, "%<%T::%E%> has not been declared",
2143		  parser->object_scope, name);
2144      else
2145	error_at (location, "%qE has not been declared", name);
2146    }
2147  else if (parser->scope && parser->scope != global_namespace)
2148    error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2149  else if (parser->scope == global_namespace)
2150    error_at (location, "%<::%E%> %s", name, desired);
2151  else
2152    error_at (location, "%qE %s", name, desired);
2153}
2154
2155/* If we are parsing tentatively, remember that an error has occurred
2156   during this tentative parse.  Returns true if the error was
2157   simulated; false if a message should be issued by the caller.  */
2158
2159static bool
2160cp_parser_simulate_error (cp_parser* parser)
2161{
2162  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2163    {
2164      parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2165      return true;
2166    }
2167  return false;
2168}
2169
2170/* Check for repeated decl-specifiers.  */
2171
2172static void
2173cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2174			   location_t location)
2175{
2176  int ds;
2177
2178  for (ds = ds_first; ds != ds_last; ++ds)
2179    {
2180      unsigned count = decl_specs->specs[ds];
2181      if (count < 2)
2182	continue;
2183      /* The "long" specifier is a special case because of "long long".  */
2184      if (ds == ds_long)
2185	{
2186	  if (count > 2)
2187	    error_at (location, "%<long long long%> is too long for GCC");
2188	  else
2189	    pedwarn_cxx98 (location, OPT_Wlong_long,
2190			   "ISO C++ 1998 does not support %<long long%>");
2191	}
2192      else if (count > 1)
2193	{
2194	  static const char *const decl_spec_names[] = {
2195	    "signed",
2196	    "unsigned",
2197	    "short",
2198	    "long",
2199	    "const",
2200	    "volatile",
2201	    "restrict",
2202	    "inline",
2203	    "virtual",
2204	    "explicit",
2205	    "friend",
2206	    "typedef",
2207            "constexpr",
2208	    "__complex",
2209	    "__thread"
2210	  };
2211	  error_at (location, "duplicate %qs", decl_spec_names[ds]);
2212	}
2213    }
2214}
2215
2216/* This function is called when a type is defined.  If type
2217   definitions are forbidden at this point, an error message is
2218   issued.  */
2219
2220static bool
2221cp_parser_check_type_definition (cp_parser* parser)
2222{
2223  /* If types are forbidden here, issue a message.  */
2224  if (parser->type_definition_forbidden_message)
2225    {
2226      /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2227	 in the message need to be interpreted.  */
2228      error (parser->type_definition_forbidden_message);
2229      return false;
2230    }
2231  return true;
2232}
2233
2234/* This function is called when the DECLARATOR is processed.  The TYPE
2235   was a type defined in the decl-specifiers.  If it is invalid to
2236   define a type in the decl-specifiers for DECLARATOR, an error is
2237   issued. TYPE_LOCATION is the location of TYPE and is used
2238   for error reporting.  */
2239
2240static void
2241cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2242					       tree type, location_t type_location)
2243{
2244  /* [dcl.fct] forbids type definitions in return types.
2245     Unfortunately, it's not easy to know whether or not we are
2246     processing a return type until after the fact.  */
2247  while (declarator
2248	 && (declarator->kind == cdk_pointer
2249	     || declarator->kind == cdk_reference
2250	     || declarator->kind == cdk_ptrmem))
2251    declarator = declarator->declarator;
2252  if (declarator
2253      && declarator->kind == cdk_function)
2254    {
2255      error_at (type_location,
2256		"new types may not be defined in a return type");
2257      inform (type_location,
2258	      "(perhaps a semicolon is missing after the definition of %qT)",
2259	      type);
2260    }
2261}
2262
2263/* A type-specifier (TYPE) has been parsed which cannot be followed by
2264   "<" in any valid C++ program.  If the next token is indeed "<",
2265   issue a message warning the user about what appears to be an
2266   invalid attempt to form a template-id. LOCATION is the location
2267   of the type-specifier (TYPE) */
2268
2269static void
2270cp_parser_check_for_invalid_template_id (cp_parser* parser,
2271					 tree type, location_t location)
2272{
2273  cp_token_position start = 0;
2274
2275  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2276    {
2277      if (TYPE_P (type))
2278	error_at (location, "%qT is not a template", type);
2279      else if (TREE_CODE (type) == IDENTIFIER_NODE)
2280	error_at (location, "%qE is not a template", type);
2281      else
2282	error_at (location, "invalid template-id");
2283      /* Remember the location of the invalid "<".  */
2284      if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2285	start = cp_lexer_token_position (parser->lexer, true);
2286      /* Consume the "<".  */
2287      cp_lexer_consume_token (parser->lexer);
2288      /* Parse the template arguments.  */
2289      cp_parser_enclosed_template_argument_list (parser);
2290      /* Permanently remove the invalid template arguments so that
2291	 this error message is not issued again.  */
2292      if (start)
2293	cp_lexer_purge_tokens_after (parser->lexer, start);
2294    }
2295}
2296
2297/* If parsing an integral constant-expression, issue an error message
2298   about the fact that THING appeared and return true.  Otherwise,
2299   return false.  In either case, set
2300   PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2301
2302static bool
2303cp_parser_non_integral_constant_expression (cp_parser  *parser,
2304					    const char *thing)
2305{
2306  parser->non_integral_constant_expression_p = true;
2307  if (parser->integral_constant_expression_p)
2308    {
2309      if (!parser->allow_non_integral_constant_expression_p)
2310	{
2311	  /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2312	     in the message need to be interpreted.  */
2313	  char *message = concat (thing,
2314				  " cannot appear in a constant-expression",
2315				  NULL);
2316	  error (message);
2317	  free (message);
2318	  return true;
2319	}
2320    }
2321  return false;
2322}
2323
2324/* Emit a diagnostic for an invalid type name.  SCOPE is the
2325   qualifying scope (or NULL, if none) for ID.  This function commits
2326   to the current active tentative parse, if any.  (Otherwise, the
2327   problematic construct might be encountered again later, resulting
2328   in duplicate error messages.) LOCATION is the location of ID.  */
2329
2330static void
2331cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2332				      tree scope, tree id,
2333				      location_t location)
2334{
2335  tree decl, old_scope;
2336  /* Try to lookup the identifier.  */
2337  old_scope = parser->scope;
2338  parser->scope = scope;
2339  decl = cp_parser_lookup_name_simple (parser, id, location);
2340  parser->scope = old_scope;
2341  /* If the lookup found a template-name, it means that the user forgot
2342  to specify an argument list. Emit a useful error message.  */
2343  if (TREE_CODE (decl) == TEMPLATE_DECL)
2344    error_at (location,
2345	      "invalid use of template-name %qE without an argument list",
2346	      decl);
2347  else if (TREE_CODE (id) == BIT_NOT_EXPR)
2348    error_at (location, "invalid use of destructor %qD as a type", id);
2349  else if (TREE_CODE (decl) == TYPE_DECL)
2350    /* Something like 'unsigned A a;'  */
2351    error_at (location, "invalid combination of multiple type-specifiers");
2352  else if (!parser->scope)
2353    {
2354      /* Issue an error message.  */
2355      error_at (location, "%qE does not name a type", id);
2356      /* If we're in a template class, it's possible that the user was
2357	 referring to a type from a base class.  For example:
2358
2359	   template <typename T> struct A { typedef T X; };
2360	   template <typename T> struct B : public A<T> { X x; };
2361
2362	 The user should have said "typename A<T>::X".  */
2363      if (processing_template_decl && current_class_type
2364	  && TYPE_BINFO (current_class_type))
2365	{
2366	  tree b;
2367
2368	  for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2369	       b;
2370	       b = TREE_CHAIN (b))
2371	    {
2372	      tree base_type = BINFO_TYPE (b);
2373	      if (CLASS_TYPE_P (base_type)
2374		  && dependent_type_p (base_type))
2375		{
2376		  tree field;
2377		  /* Go from a particular instantiation of the
2378		     template (which will have an empty TYPE_FIELDs),
2379		     to the main version.  */
2380		  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2381		  for (field = TYPE_FIELDS (base_type);
2382		       field;
2383		       field = TREE_CHAIN (field))
2384		    if (TREE_CODE (field) == TYPE_DECL
2385			&& DECL_NAME (field) == id)
2386		      {
2387			inform (location,
2388				"(perhaps %<typename %T::%E%> was intended)",
2389				BINFO_TYPE (b), id);
2390			break;
2391		      }
2392		  if (field)
2393		    break;
2394		}
2395	    }
2396	}
2397    }
2398  /* Here we diagnose qualified-ids where the scope is actually correct,
2399     but the identifier does not resolve to a valid type name.  */
2400  else if (parser->scope != error_mark_node)
2401    {
2402      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2403	error_at (location, "%qE in namespace %qE does not name a type",
2404		  id, parser->scope);
2405      else if (CLASS_TYPE_P (parser->scope)
2406	       && constructor_name_p (id, parser->scope))
2407	{
2408	  /* A<T>::A<T>() */
2409	  error_at (location, "%<%T::%E%> names the constructor, not"
2410		    " the type", parser->scope, id);
2411	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2412	    error_at (location, "and %qT has no template constructors",
2413		      parser->scope);
2414	}
2415      else if (TYPE_P (parser->scope)
2416	       && dependent_scope_p (parser->scope))
2417	error_at (location, "need %<typename%> before %<%T::%E%> because "
2418		  "%qT is a dependent scope",
2419		  parser->scope, id, parser->scope);
2420      else if (TYPE_P (parser->scope))
2421	error_at (location, "%qE in class %qT does not name a type",
2422		  id, parser->scope);
2423      else
2424	gcc_unreachable ();
2425    }
2426  cp_parser_commit_to_tentative_parse (parser);
2427}
2428
2429/* Check for a common situation where a type-name should be present,
2430   but is not, and issue a sensible error message.  Returns true if an
2431   invalid type-name was detected.
2432
2433   The situation handled by this function are variable declarations of the
2434   form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2435   Usually, `ID' should name a type, but if we got here it means that it
2436   does not. We try to emit the best possible error message depending on
2437   how exactly the id-expression looks like.  */
2438
2439static bool
2440cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2441{
2442  tree id;
2443  cp_token *token = cp_lexer_peek_token (parser->lexer);
2444
2445  /* Avoid duplicate error about ambiguous lookup.  */
2446  if (token->type == CPP_NESTED_NAME_SPECIFIER)
2447    {
2448      cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2449      if (next->type == CPP_NAME && next->ambiguous_p)
2450	goto out;
2451    }
2452
2453  cp_parser_parse_tentatively (parser);
2454  id = cp_parser_id_expression (parser,
2455				/*template_keyword_p=*/false,
2456				/*check_dependency_p=*/true,
2457				/*template_p=*/NULL,
2458				/*declarator_p=*/true,
2459				/*optional_p=*/false);
2460  /* If the next token is a (, this is a function with no explicit return
2461     type, i.e. constructor, destructor or conversion op.  */
2462  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2463      || TREE_CODE (id) == TYPE_DECL)
2464    {
2465      cp_parser_abort_tentative_parse (parser);
2466      return false;
2467    }
2468  if (!cp_parser_parse_definitely (parser))
2469    return false;
2470
2471  /* Emit a diagnostic for the invalid type.  */
2472  cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2473					id, token->location);
2474 out:
2475  /* If we aren't in the middle of a declarator (i.e. in a
2476     parameter-declaration-clause), skip to the end of the declaration;
2477     there's no point in trying to process it.  */
2478  if (!parser->in_declarator_p)
2479    cp_parser_skip_to_end_of_block_or_statement (parser);
2480  return true;
2481}
2482
2483/* Consume tokens up to, and including, the next non-nested closing `)'.
2484   Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2485   are doing error recovery. Returns -1 if OR_COMMA is true and we
2486   found an unnested comma.  */
2487
2488static int
2489cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2490				       bool recovering,
2491				       bool or_comma,
2492				       bool consume_paren)
2493{
2494  unsigned paren_depth = 0;
2495  unsigned brace_depth = 0;
2496  unsigned square_depth = 0;
2497
2498  if (recovering && !or_comma
2499      && cp_parser_uncommitted_to_tentative_parse_p (parser))
2500    return 0;
2501
2502  while (true)
2503    {
2504      cp_token * token = cp_lexer_peek_token (parser->lexer);
2505
2506      switch (token->type)
2507	{
2508	case CPP_EOF:
2509	case CPP_PRAGMA_EOL:
2510	  /* If we've run out of tokens, then there is no closing `)'.  */
2511	  return 0;
2512
2513        /* This is good for lambda expression capture-lists.  */
2514        case CPP_OPEN_SQUARE:
2515          ++square_depth;
2516          break;
2517        case CPP_CLOSE_SQUARE:
2518          if (!square_depth--)
2519            return 0;
2520          break;
2521
2522	case CPP_SEMICOLON:
2523	  /* This matches the processing in skip_to_end_of_statement.  */
2524	  if (!brace_depth)
2525	    return 0;
2526	  break;
2527
2528	case CPP_OPEN_BRACE:
2529	  ++brace_depth;
2530	  break;
2531	case CPP_CLOSE_BRACE:
2532	  if (!brace_depth--)
2533	    return 0;
2534	  break;
2535
2536	case CPP_COMMA:
2537	  if (recovering && or_comma && !brace_depth && !paren_depth
2538	      && !square_depth)
2539	    return -1;
2540	  break;
2541
2542	case CPP_OPEN_PAREN:
2543	  if (!brace_depth)
2544	    ++paren_depth;
2545	  break;
2546
2547	case CPP_CLOSE_PAREN:
2548	  if (!brace_depth && !paren_depth--)
2549	    {
2550	      if (consume_paren)
2551		cp_lexer_consume_token (parser->lexer);
2552	      return 1;
2553	    }
2554	  break;
2555
2556	default:
2557	  break;
2558	}
2559
2560      /* Consume the token.  */
2561      cp_lexer_consume_token (parser->lexer);
2562    }
2563}
2564
2565/* Consume tokens until we reach the end of the current statement.
2566   Normally, that will be just before consuming a `;'.  However, if a
2567   non-nested `}' comes first, then we stop before consuming that.  */
2568
2569static void
2570cp_parser_skip_to_end_of_statement (cp_parser* parser)
2571{
2572  unsigned nesting_depth = 0;
2573
2574  while (true)
2575    {
2576      cp_token *token = cp_lexer_peek_token (parser->lexer);
2577
2578      switch (token->type)
2579	{
2580	case CPP_EOF:
2581	case CPP_PRAGMA_EOL:
2582	  /* If we've run out of tokens, stop.  */
2583	  return;
2584
2585	case CPP_SEMICOLON:
2586	  /* If the next token is a `;', we have reached the end of the
2587	     statement.  */
2588	  if (!nesting_depth)
2589	    return;
2590	  break;
2591
2592	case CPP_CLOSE_BRACE:
2593	  /* If this is a non-nested '}', stop before consuming it.
2594	     That way, when confronted with something like:
2595
2596	       { 3 + }
2597
2598	     we stop before consuming the closing '}', even though we
2599	     have not yet reached a `;'.  */
2600	  if (nesting_depth == 0)
2601	    return;
2602
2603	  /* If it is the closing '}' for a block that we have
2604	     scanned, stop -- but only after consuming the token.
2605	     That way given:
2606
2607		void f g () { ... }
2608		typedef int I;
2609
2610	     we will stop after the body of the erroneously declared
2611	     function, but before consuming the following `typedef'
2612	     declaration.  */
2613	  if (--nesting_depth == 0)
2614	    {
2615	      cp_lexer_consume_token (parser->lexer);
2616	      return;
2617	    }
2618
2619	case CPP_OPEN_BRACE:
2620	  ++nesting_depth;
2621	  break;
2622
2623	default:
2624	  break;
2625	}
2626
2627      /* Consume the token.  */
2628      cp_lexer_consume_token (parser->lexer);
2629    }
2630}
2631
2632/* This function is called at the end of a statement or declaration.
2633   If the next token is a semicolon, it is consumed; otherwise, error
2634   recovery is attempted.  */
2635
2636static void
2637cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2638{
2639  /* Look for the trailing `;'.  */
2640  if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2641    {
2642      /* If there is additional (erroneous) input, skip to the end of
2643	 the statement.  */
2644      cp_parser_skip_to_end_of_statement (parser);
2645      /* If the next token is now a `;', consume it.  */
2646      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2647	cp_lexer_consume_token (parser->lexer);
2648    }
2649}
2650
2651/* Skip tokens until we have consumed an entire block, or until we
2652   have consumed a non-nested `;'.  */
2653
2654static void
2655cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2656{
2657  int nesting_depth = 0;
2658
2659  while (nesting_depth >= 0)
2660    {
2661      cp_token *token = cp_lexer_peek_token (parser->lexer);
2662
2663      switch (token->type)
2664	{
2665	case CPP_EOF:
2666	case CPP_PRAGMA_EOL:
2667	  /* If we've run out of tokens, stop.  */
2668	  return;
2669
2670	case CPP_SEMICOLON:
2671	  /* Stop if this is an unnested ';'. */
2672	  if (!nesting_depth)
2673	    nesting_depth = -1;
2674	  break;
2675
2676	case CPP_CLOSE_BRACE:
2677	  /* Stop if this is an unnested '}', or closes the outermost
2678	     nesting level.  */
2679	  nesting_depth--;
2680	  if (nesting_depth < 0)
2681	    return;
2682	  if (!nesting_depth)
2683	    nesting_depth = -1;
2684	  break;
2685
2686	case CPP_OPEN_BRACE:
2687	  /* Nest. */
2688	  nesting_depth++;
2689	  break;
2690
2691	default:
2692	  break;
2693	}
2694
2695      /* Consume the token.  */
2696      cp_lexer_consume_token (parser->lexer);
2697    }
2698}
2699
2700/* Skip tokens until a non-nested closing curly brace is the next
2701   token, or there are no more tokens. Return true in the first case,
2702   false otherwise.  */
2703
2704static bool
2705cp_parser_skip_to_closing_brace (cp_parser *parser)
2706{
2707  unsigned nesting_depth = 0;
2708
2709  while (true)
2710    {
2711      cp_token *token = cp_lexer_peek_token (parser->lexer);
2712
2713      switch (token->type)
2714	{
2715	case CPP_EOF:
2716	case CPP_PRAGMA_EOL:
2717	  /* If we've run out of tokens, stop.  */
2718	  return false;
2719
2720	case CPP_CLOSE_BRACE:
2721	  /* If the next token is a non-nested `}', then we have reached
2722	     the end of the current block.  */
2723	  if (nesting_depth-- == 0)
2724	    return true;
2725	  break;
2726
2727	case CPP_OPEN_BRACE:
2728	  /* If it the next token is a `{', then we are entering a new
2729	     block.  Consume the entire block.  */
2730	  ++nesting_depth;
2731	  break;
2732
2733	default:
2734	  break;
2735	}
2736
2737      /* Consume the token.  */
2738      cp_lexer_consume_token (parser->lexer);
2739    }
2740}
2741
2742/* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2743   parameter is the PRAGMA token, allowing us to purge the entire pragma
2744   sequence.  */
2745
2746static void
2747cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2748{
2749  cp_token *token;
2750
2751  parser->lexer->in_pragma = false;
2752
2753  do
2754    token = cp_lexer_consume_token (parser->lexer);
2755  while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2756
2757  /* Ensure that the pragma is not parsed again.  */
2758  cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2759}
2760
2761/* Require pragma end of line, resyncing with it as necessary.  The
2762   arguments are as for cp_parser_skip_to_pragma_eol.  */
2763
2764static void
2765cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2766{
2767  parser->lexer->in_pragma = false;
2768  if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2769    cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2770}
2771
2772/* This is a simple wrapper around make_typename_type. When the id is
2773   an unresolved identifier node, we can provide a superior diagnostic
2774   using cp_parser_diagnose_invalid_type_name.  */
2775
2776static tree
2777cp_parser_make_typename_type (cp_parser *parser, tree scope,
2778			      tree id, location_t id_location)
2779{
2780  tree result;
2781  if (TREE_CODE (id) == IDENTIFIER_NODE)
2782    {
2783      result = make_typename_type (scope, id, typename_type,
2784				   /*complain=*/tf_none);
2785      if (result == error_mark_node)
2786	cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2787      return result;
2788    }
2789  return make_typename_type (scope, id, typename_type, tf_error);
2790}
2791
2792/* This is a wrapper around the
2793   make_{pointer,ptrmem,reference}_declarator functions that decides
2794   which one to call based on the CODE and CLASS_TYPE arguments. The
2795   CODE argument should be one of the values returned by
2796   cp_parser_ptr_operator. */
2797static cp_declarator *
2798cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2799				    cp_cv_quals cv_qualifiers,
2800				    cp_declarator *target)
2801{
2802  if (code == ERROR_MARK)
2803    return cp_error_declarator;
2804
2805  if (code == INDIRECT_REF)
2806    if (class_type == NULL_TREE)
2807      return make_pointer_declarator (cv_qualifiers, target);
2808    else
2809      return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2810  else if (code == ADDR_EXPR && class_type == NULL_TREE)
2811    return make_reference_declarator (cv_qualifiers, target, false);
2812  else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2813    return make_reference_declarator (cv_qualifiers, target, true);
2814  gcc_unreachable ();
2815}
2816
2817/* Create a new C++ parser.  */
2818
2819static cp_parser *
2820cp_parser_new (void)
2821{
2822  cp_parser *parser;
2823  cp_lexer *lexer;
2824  unsigned i;
2825
2826  /* cp_lexer_new_main is called before calling ggc_alloc because
2827     cp_lexer_new_main might load a PCH file.  */
2828  lexer = cp_lexer_new_main ();
2829
2830  /* Initialize the binops_by_token so that we can get the tree
2831     directly from the token.  */
2832  for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2833    binops_by_token[binops[i].token_type] = binops[i];
2834
2835  parser = GGC_CNEW (cp_parser);
2836  parser->lexer = lexer;
2837  parser->context = cp_parser_context_new (NULL);
2838
2839  /* For now, we always accept GNU extensions.  */
2840  parser->allow_gnu_extensions_p = 1;
2841
2842  /* The `>' token is a greater-than operator, not the end of a
2843     template-id.  */
2844  parser->greater_than_is_operator_p = true;
2845
2846  parser->default_arg_ok_p = true;
2847
2848  /* We are not parsing a constant-expression.  */
2849  parser->integral_constant_expression_p = false;
2850  parser->allow_non_integral_constant_expression_p = false;
2851  parser->non_integral_constant_expression_p = false;
2852
2853  /* Local variable names are not forbidden.  */
2854  parser->local_variables_forbidden_p = false;
2855
2856  /* We are not processing an `extern "C"' declaration.  */
2857  parser->in_unbraced_linkage_specification_p = false;
2858
2859  /* We are not processing a declarator.  */
2860  parser->in_declarator_p = false;
2861
2862  /* We are not processing a template-argument-list.  */
2863  parser->in_template_argument_list_p = false;
2864
2865  /* We are not in an iteration statement.  */
2866  parser->in_statement = 0;
2867
2868  /* We are not in a switch statement.  */
2869  parser->in_switch_statement_p = false;
2870
2871  /* We are not parsing a type-id inside an expression.  */
2872  parser->in_type_id_in_expr_p = false;
2873
2874  /* Declarations aren't implicitly extern "C".  */
2875  parser->implicit_extern_c = false;
2876
2877  /* String literals should be translated to the execution character set.  */
2878  parser->translate_strings_p = true;
2879
2880  /* We are not parsing a function body.  */
2881  parser->in_function_body = false;
2882
2883  /* The unparsed function queue is empty.  */
2884  parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2885
2886  /* There are no classes being defined.  */
2887  parser->num_classes_being_defined = 0;
2888
2889  /* No template parameters apply.  */
2890  parser->num_template_parameter_lists = 0;
2891
2892  return parser;
2893}
2894
2895/* Create a cp_lexer structure which will emit the tokens in CACHE
2896   and push it onto the parser's lexer stack.  This is used for delayed
2897   parsing of in-class method bodies and default arguments, and should
2898   not be confused with tentative parsing.  */
2899static void
2900cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2901{
2902  cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2903  lexer->next = parser->lexer;
2904  parser->lexer = lexer;
2905
2906  /* Move the current source position to that of the first token in the
2907     new lexer.  */
2908  cp_lexer_set_source_position_from_token (lexer->next_token);
2909}
2910
2911/* Pop the top lexer off the parser stack.  This is never used for the
2912   "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2913static void
2914cp_parser_pop_lexer (cp_parser *parser)
2915{
2916  cp_lexer *lexer = parser->lexer;
2917  parser->lexer = lexer->next;
2918  cp_lexer_destroy (lexer);
2919
2920  /* Put the current source position back where it was before this
2921     lexer was pushed.  */
2922  cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2923}
2924
2925/* Lexical conventions [gram.lex]  */
2926
2927/* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2928   identifier.  */
2929
2930static tree
2931cp_parser_identifier (cp_parser* parser)
2932{
2933  cp_token *token;
2934
2935  /* Look for the identifier.  */
2936  token = cp_parser_require (parser, CPP_NAME, "identifier");
2937  /* Return the value.  */
2938  return token ? token->u.value : error_mark_node;
2939}
2940
2941/* Parse a sequence of adjacent string constants.  Returns a
2942   TREE_STRING representing the combined, nul-terminated string
2943   constant.  If TRANSLATE is true, translate the string to the
2944   execution character set.  If WIDE_OK is true, a wide string is
2945   invalid here.
2946
2947   C++98 [lex.string] says that if a narrow string literal token is
2948   adjacent to a wide string literal token, the behavior is undefined.
2949   However, C99 6.4.5p4 says that this results in a wide string literal.
2950   We follow C99 here, for consistency with the C front end.
2951
2952   This code is largely lifted from lex_string() in c-lex.c.
2953
2954   FUTURE: ObjC++ will need to handle @-strings here.  */
2955static tree
2956cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2957{
2958  tree value;
2959  size_t count;
2960  struct obstack str_ob;
2961  cpp_string str, istr, *strs;
2962  cp_token *tok;
2963  enum cpp_ttype type;
2964
2965  tok = cp_lexer_peek_token (parser->lexer);
2966  if (!cp_parser_is_string_literal (tok))
2967    {
2968      cp_parser_error (parser, "expected string-literal");
2969      return error_mark_node;
2970    }
2971
2972  type = tok->type;
2973
2974  /* Try to avoid the overhead of creating and destroying an obstack
2975     for the common case of just one string.  */
2976  if (!cp_parser_is_string_literal
2977      (cp_lexer_peek_nth_token (parser->lexer, 2)))
2978    {
2979      cp_lexer_consume_token (parser->lexer);
2980
2981      str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2982      str.len = TREE_STRING_LENGTH (tok->u.value);
2983      count = 1;
2984
2985      strs = &str;
2986    }
2987  else
2988    {
2989      gcc_obstack_init (&str_ob);
2990      count = 0;
2991
2992      do
2993	{
2994	  cp_lexer_consume_token (parser->lexer);
2995	  count++;
2996	  str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2997	  str.len = TREE_STRING_LENGTH (tok->u.value);
2998
2999	  if (type != tok->type)
3000	    {
3001	      if (type == CPP_STRING)
3002		type = tok->type;
3003	      else if (tok->type != CPP_STRING)
3004		error_at (tok->location,
3005			  "unsupported non-standard concatenation "
3006			  "of string literals");
3007	    }
3008
3009	  obstack_grow (&str_ob, &str, sizeof (cpp_string));
3010
3011	  tok = cp_lexer_peek_token (parser->lexer);
3012	}
3013      while (cp_parser_is_string_literal (tok));
3014
3015      strs = (cpp_string *) obstack_finish (&str_ob);
3016    }
3017
3018  if (type != CPP_STRING && !wide_ok)
3019    {
3020      cp_parser_error (parser, "a wide string is invalid in this context");
3021      type = CPP_STRING;
3022    }
3023
3024  if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3025      (parse_in, strs, count, &istr, type))
3026    {
3027      value = build_string (istr.len, (const char *)istr.text);
3028      free (CONST_CAST (unsigned char *, istr.text));
3029
3030      switch (type)
3031	{
3032	default:
3033	case CPP_STRING:
3034	case CPP_UTF8STRING:
3035	  TREE_TYPE (value) = char_array_type_node;
3036	  break;
3037	case CPP_STRING16:
3038	  TREE_TYPE (value) = char16_array_type_node;
3039	  break;
3040	case CPP_STRING32:
3041	  TREE_TYPE (value) = char32_array_type_node;
3042	  break;
3043	case CPP_WSTRING:
3044	  TREE_TYPE (value) = wchar_array_type_node;
3045	  break;
3046	}
3047
3048      value = fix_string_type (value);
3049    }
3050  else
3051    /* cpp_interpret_string has issued an error.  */
3052    value = error_mark_node;
3053
3054  if (count > 1)
3055    obstack_free (&str_ob, 0);
3056
3057  return value;
3058}
3059
3060
3061/* Basic concepts [gram.basic]  */
3062
3063/* Parse a translation-unit.
3064
3065   translation-unit:
3066     declaration-seq [opt]
3067
3068   Returns TRUE if all went well.  */
3069
3070static bool
3071cp_parser_translation_unit (cp_parser* parser)
3072{
3073  /* The address of the first non-permanent object on the declarator
3074     obstack.  */
3075  static void *declarator_obstack_base;
3076
3077  bool success;
3078
3079  /* Create the declarator obstack, if necessary.  */
3080  if (!cp_error_declarator)
3081    {
3082      gcc_obstack_init (&declarator_obstack);
3083      /* Create the error declarator.  */
3084      cp_error_declarator = make_declarator (cdk_error);
3085      /* Create the empty parameter list.  */
3086      no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3087      /* Remember where the base of the declarator obstack lies.  */
3088      declarator_obstack_base = obstack_next_free (&declarator_obstack);
3089    }
3090
3091  cp_parser_declaration_seq_opt (parser);
3092
3093  /* If there are no tokens left then all went well.  */
3094  if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3095    {
3096      /* Get rid of the token array; we don't need it any more.  */
3097      cp_lexer_destroy (parser->lexer);
3098      parser->lexer = NULL;
3099
3100      /* This file might have been a context that's implicitly extern
3101	 "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3102      if (parser->implicit_extern_c)
3103	{
3104	  pop_lang_context ();
3105	  parser->implicit_extern_c = false;
3106	}
3107
3108      /* Finish up.  */
3109      finish_translation_unit ();
3110
3111      success = true;
3112    }
3113  else
3114    {
3115      cp_parser_error (parser, "expected declaration");
3116      success = false;
3117    }
3118
3119  /* Make sure the declarator obstack was fully cleaned up.  */
3120  gcc_assert (obstack_next_free (&declarator_obstack)
3121	      == declarator_obstack_base);
3122
3123  /* All went well.  */
3124  return success;
3125}
3126
3127/* Expressions [gram.expr] */
3128
3129/* Parse a primary-expression.
3130
3131   primary-expression:
3132     literal
3133     this
3134     ( expression )
3135     id-expression
3136
3137   GNU Extensions:
3138
3139   primary-expression:
3140     ( compound-statement )
3141     __builtin_va_arg ( assignment-expression , type-id )
3142     __builtin_offsetof ( type-id , offsetof-expression )
3143
3144   C++ Extensions:
3145     __has_nothrow_assign ( type-id )
3146     __has_nothrow_constructor ( type-id )
3147     __has_nothrow_copy ( type-id )
3148     __has_trivial_assign ( type-id )
3149     __has_trivial_constructor ( type-id )
3150     __has_trivial_copy ( type-id )
3151     __has_trivial_destructor ( type-id )
3152     __has_virtual_destructor ( type-id )
3153     __is_abstract ( type-id )
3154     __is_base_of ( type-id , type-id )
3155     __is_class ( type-id )
3156     __is_convertible_to ( type-id , type-id )
3157     __is_empty ( type-id )
3158     __is_enum ( type-id )
3159     __is_pod ( type-id )
3160     __is_polymorphic ( type-id )
3161     __is_union ( type-id )
3162
3163   Objective-C++ Extension:
3164
3165   primary-expression:
3166     objc-expression
3167
3168   literal:
3169     __null
3170
3171   ADDRESS_P is true iff this expression was immediately preceded by
3172   "&" and therefore might denote a pointer-to-member.  CAST_P is true
3173   iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3174   true iff this expression is a template argument.
3175
3176   Returns a representation of the expression.  Upon return, *IDK
3177   indicates what kind of id-expression (if any) was present.  */
3178
3179static tree
3180cp_parser_primary_expression (cp_parser *parser,
3181			      bool address_p,
3182			      bool cast_p,
3183			      bool template_arg_p,
3184			      cp_id_kind *idk)
3185{
3186  cp_token *token = NULL;
3187
3188  /* Assume the primary expression is not an id-expression.  */
3189  *idk = CP_ID_KIND_NONE;
3190
3191  /* Peek at the next token.  */
3192  token = cp_lexer_peek_token (parser->lexer);
3193  switch (token->type)
3194    {
3195      /* literal:
3196	   integer-literal
3197	   character-literal
3198	   floating-literal
3199	   string-literal
3200	   boolean-literal  */
3201    case CPP_CHAR:
3202    case CPP_CHAR16:
3203    case CPP_CHAR32:
3204    case CPP_WCHAR:
3205    case CPP_NUMBER:
3206      token = cp_lexer_consume_token (parser->lexer);
3207      if (TREE_CODE (token->u.value) == FIXED_CST)
3208	{
3209	  error_at (token->location,
3210		    "fixed-point types not supported in C++");
3211	  return error_mark_node;
3212	}
3213      /* Floating-point literals are only allowed in an integral
3214	 constant expression if they are cast to an integral or
3215	 enumeration type.  */
3216      if (TREE_CODE (token->u.value) == REAL_CST
3217	  && parser->integral_constant_expression_p
3218	  && pedantic)
3219	{
3220	  /* CAST_P will be set even in invalid code like "int(2.7 +
3221	     ...)".   Therefore, we have to check that the next token
3222	     is sure to end the cast.  */
3223	  if (cast_p)
3224	    {
3225	      cp_token *next_token;
3226
3227	      next_token = cp_lexer_peek_token (parser->lexer);
3228	      if (/* The comma at the end of an
3229		     enumerator-definition.  */
3230		  next_token->type != CPP_COMMA
3231		  /* The curly brace at the end of an enum-specifier.  */
3232		  && next_token->type != CPP_CLOSE_BRACE
3233		  /* The end of a statement.  */
3234		  && next_token->type != CPP_SEMICOLON
3235		  /* The end of the cast-expression.  */
3236		  && next_token->type != CPP_CLOSE_PAREN
3237		  /* The end of an array bound.  */
3238		  && next_token->type != CPP_CLOSE_SQUARE
3239		  /* The closing ">" in a template-argument-list.  */
3240		  && (next_token->type != CPP_GREATER
3241		      || parser->greater_than_is_operator_p)
3242		  /* C++0x only: A ">>" treated like two ">" tokens,
3243                     in a template-argument-list.  */
3244		  && (next_token->type != CPP_RSHIFT
3245                      || (cxx_dialect == cxx98)
3246		      || parser->greater_than_is_operator_p))
3247		cast_p = false;
3248	    }
3249
3250	  /* If we are within a cast, then the constraint that the
3251	     cast is to an integral or enumeration type will be
3252	     checked at that point.  If we are not within a cast, then
3253	     this code is invalid.  */
3254	  if (!cast_p)
3255	    cp_parser_non_integral_constant_expression
3256	      (parser, "floating-point literal");
3257	}
3258      return token->u.value;
3259
3260    case CPP_STRING:
3261    case CPP_STRING16:
3262    case CPP_STRING32:
3263    case CPP_WSTRING:
3264    case CPP_UTF8STRING:
3265      /* ??? Should wide strings be allowed when parser->translate_strings_p
3266	 is false (i.e. in attributes)?  If not, we can kill the third
3267	 argument to cp_parser_string_literal.  */
3268      return cp_parser_string_literal (parser,
3269				       parser->translate_strings_p,
3270				       true);
3271
3272    case CPP_OPEN_PAREN:
3273      {
3274	tree expr;
3275	bool saved_greater_than_is_operator_p;
3276
3277	/* Consume the `('.  */
3278	cp_lexer_consume_token (parser->lexer);
3279	/* Within a parenthesized expression, a `>' token is always
3280	   the greater-than operator.  */
3281	saved_greater_than_is_operator_p
3282	  = parser->greater_than_is_operator_p;
3283	parser->greater_than_is_operator_p = true;
3284	/* If we see `( { ' then we are looking at the beginning of
3285	   a GNU statement-expression.  */
3286	if (cp_parser_allow_gnu_extensions_p (parser)
3287	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3288	  {
3289	    /* Statement-expressions are not allowed by the standard.  */
3290	    pedwarn (token->location, OPT_pedantic,
3291		     "ISO C++ forbids braced-groups within expressions");
3292
3293	    /* And they're not allowed outside of a function-body; you
3294	       cannot, for example, write:
3295
3296		 int i = ({ int j = 3; j + 1; });
3297
3298	       at class or namespace scope.  */
3299	    if (!parser->in_function_body
3300		|| parser->in_template_argument_list_p)
3301	      {
3302		error_at (token->location,
3303			  "statement-expressions are not allowed outside "
3304			  "functions nor in template-argument lists");
3305		cp_parser_skip_to_end_of_block_or_statement (parser);
3306		expr = error_mark_node;
3307	      }
3308	    else
3309	      {
3310		/* Start the statement-expression.  */
3311		expr = begin_stmt_expr ();
3312		/* Parse the compound-statement.  */
3313		cp_parser_compound_statement (parser, expr, false);
3314		/* Finish up.  */
3315		expr = finish_stmt_expr (expr, false);
3316	      }
3317	  }
3318	else
3319	  {
3320	    /* Parse the parenthesized expression.  */
3321	    expr = cp_parser_expression (parser, cast_p, idk);
3322	    /* Let the front end know that this expression was
3323	       enclosed in parentheses. This matters in case, for
3324	       example, the expression is of the form `A::B', since
3325	       `&A::B' might be a pointer-to-member, but `&(A::B)' is
3326	       not.  */
3327	    finish_parenthesized_expr (expr);
3328	    /* DR 705: Wrapping an unqualified name in parentheses
3329	       suppresses arg-dependent lookup.  We want to pass back
3330	       CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3331	       (c++/37862), but none of the others.  */
3332	    if (*idk != CP_ID_KIND_QUALIFIED)
3333	      *idk = CP_ID_KIND_NONE;
3334	  }
3335	/* The `>' token might be the end of a template-id or
3336	   template-parameter-list now.  */
3337	parser->greater_than_is_operator_p
3338	  = saved_greater_than_is_operator_p;
3339	/* Consume the `)'.  */
3340	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3341	  cp_parser_skip_to_end_of_statement (parser);
3342
3343	return expr;
3344      }
3345
3346    case CPP_OPEN_SQUARE:
3347      if (c_dialect_objc ())
3348        /* We have an Objective-C++ message. */
3349        return cp_parser_objc_expression (parser);
3350      {
3351	tree lam = cp_parser_lambda_expression (parser);
3352	/* Don't warn about a failed tentative parse.  */
3353	if (cp_parser_error_occurred (parser))
3354	  return error_mark_node;
3355	maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3356	return lam;
3357      }
3358
3359    case CPP_OBJC_STRING:
3360      if (c_dialect_objc ())
3361	/* We have an Objective-C++ string literal. */
3362        return cp_parser_objc_expression (parser);
3363      cp_parser_error (parser, "expected primary-expression");
3364      return error_mark_node;
3365
3366    case CPP_KEYWORD:
3367      switch (token->keyword)
3368	{
3369	  /* These two are the boolean literals.  */
3370	case RID_TRUE:
3371	  cp_lexer_consume_token (parser->lexer);
3372	  return boolean_true_node;
3373	case RID_FALSE:
3374	  cp_lexer_consume_token (parser->lexer);
3375	  return boolean_false_node;
3376
3377	  /* The `__null' literal.  */
3378	case RID_NULL:
3379	  cp_lexer_consume_token (parser->lexer);
3380	  return null_node;
3381
3382	  /* Recognize the `this' keyword.  */
3383	case RID_THIS:
3384	  cp_lexer_consume_token (parser->lexer);
3385	  if (parser->local_variables_forbidden_p)
3386	    {
3387	      error_at (token->location,
3388			"%<this%> may not be used in this context");
3389	      return error_mark_node;
3390	    }
3391	  /* Pointers cannot appear in constant-expressions.  */
3392	  if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3393	    return error_mark_node;
3394	  return finish_this_expr ();
3395
3396	  /* The `operator' keyword can be the beginning of an
3397	     id-expression.  */
3398	case RID_OPERATOR:
3399	  goto id_expression;
3400
3401	case RID_FUNCTION_NAME:
3402	case RID_PRETTY_FUNCTION_NAME:
3403	case RID_C99_FUNCTION_NAME:
3404	  {
3405	    const char *name;
3406
3407	    /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3408	       __func__ are the names of variables -- but they are
3409	       treated specially.  Therefore, they are handled here,
3410	       rather than relying on the generic id-expression logic
3411	       below.  Grammatically, these names are id-expressions.
3412
3413	       Consume the token.  */
3414	    token = cp_lexer_consume_token (parser->lexer);
3415
3416	    switch (token->keyword)
3417	      {
3418	      case RID_FUNCTION_NAME:
3419		name = "%<__FUNCTION__%>";
3420		break;
3421	      case RID_PRETTY_FUNCTION_NAME:
3422		name = "%<__PRETTY_FUNCTION__%>";
3423		break;
3424	      case RID_C99_FUNCTION_NAME:
3425		name = "%<__func__%>";
3426		break;
3427	      default:
3428		gcc_unreachable ();
3429	      }
3430
3431	    if (cp_parser_non_integral_constant_expression (parser, name))
3432	      return error_mark_node;
3433
3434	    /* Look up the name.  */
3435	    return finish_fname (token->u.value);
3436	  }
3437
3438	case RID_VA_ARG:
3439	  {
3440	    tree expression;
3441	    tree type;
3442
3443	    /* The `__builtin_va_arg' construct is used to handle
3444	       `va_arg'.  Consume the `__builtin_va_arg' token.  */
3445	    cp_lexer_consume_token (parser->lexer);
3446	    /* Look for the opening `('.  */
3447	    cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3448	    /* Now, parse the assignment-expression.  */
3449	    expression = cp_parser_assignment_expression (parser,
3450							  /*cast_p=*/false, NULL);
3451	    /* Look for the `,'.  */
3452	    cp_parser_require (parser, CPP_COMMA, "%<,%>");
3453	    /* Parse the type-id.  */
3454	    type = cp_parser_type_id (parser);
3455	    /* Look for the closing `)'.  */
3456	    cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3457	    /* Using `va_arg' in a constant-expression is not
3458	       allowed.  */
3459	    if (cp_parser_non_integral_constant_expression (parser,
3460							    "%<va_arg%>"))
3461	      return error_mark_node;
3462	    return build_x_va_arg (expression, type);
3463	  }
3464
3465	case RID_OFFSETOF:
3466	  return cp_parser_builtin_offsetof (parser);
3467
3468	case RID_HAS_NOTHROW_ASSIGN:
3469	case RID_HAS_NOTHROW_CONSTRUCTOR:
3470	case RID_HAS_NOTHROW_COPY:
3471	case RID_HAS_TRIVIAL_ASSIGN:
3472	case RID_HAS_TRIVIAL_CONSTRUCTOR:
3473	case RID_HAS_TRIVIAL_COPY:
3474	case RID_HAS_TRIVIAL_DESTRUCTOR:
3475	case RID_HAS_VIRTUAL_DESTRUCTOR:
3476	case RID_IS_ABSTRACT:
3477	case RID_IS_BASE_OF:
3478	case RID_IS_CLASS:
3479	case RID_IS_CONVERTIBLE_TO:
3480	case RID_IS_EMPTY:
3481	case RID_IS_ENUM:
3482	case RID_IS_POD:
3483	case RID_IS_POLYMORPHIC:
3484	case RID_IS_STD_LAYOUT:
3485	case RID_IS_TRIVIAL:
3486	case RID_IS_UNION:
3487	  return cp_parser_trait_expr (parser, token->keyword);
3488
3489	/* Objective-C++ expressions.  */
3490	case RID_AT_ENCODE:
3491	case RID_AT_PROTOCOL:
3492	case RID_AT_SELECTOR:
3493	  return cp_parser_objc_expression (parser);
3494
3495	default:
3496	  cp_parser_error (parser, "expected primary-expression");
3497	  return error_mark_node;
3498	}
3499
3500      /* An id-expression can start with either an identifier, a
3501	 `::' as the beginning of a qualified-id, or the "operator"
3502	 keyword.  */
3503    case CPP_NAME:
3504    case CPP_SCOPE:
3505    case CPP_TEMPLATE_ID:
3506    case CPP_NESTED_NAME_SPECIFIER:
3507      {
3508	tree id_expression;
3509	tree decl;
3510	const char *error_msg;
3511	bool template_p;
3512	bool done;
3513	cp_token *id_expr_token;
3514
3515      id_expression:
3516	/* Parse the id-expression.  */
3517	id_expression
3518	  = cp_parser_id_expression (parser,
3519				     /*template_keyword_p=*/false,
3520				     /*check_dependency_p=*/true,
3521				     &template_p,
3522				     /*declarator_p=*/false,
3523				     /*optional_p=*/false);
3524	if (id_expression == error_mark_node)
3525	  return error_mark_node;
3526	id_expr_token = token;
3527	token = cp_lexer_peek_token (parser->lexer);
3528	done = (token->type != CPP_OPEN_SQUARE
3529		&& token->type != CPP_OPEN_PAREN
3530		&& token->type != CPP_DOT
3531		&& token->type != CPP_DEREF
3532		&& token->type != CPP_PLUS_PLUS
3533		&& token->type != CPP_MINUS_MINUS);
3534	/* If we have a template-id, then no further lookup is
3535	   required.  If the template-id was for a template-class, we
3536	   will sometimes have a TYPE_DECL at this point.  */
3537	if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3538		 || TREE_CODE (id_expression) == TYPE_DECL)
3539	  decl = id_expression;
3540	/* Look up the name.  */
3541	else
3542	  {
3543	    tree ambiguous_decls;
3544
3545	    /* If we already know that this lookup is ambiguous, then
3546	       we've already issued an error message; there's no reason
3547	       to check again.  */
3548	    if (id_expr_token->type == CPP_NAME
3549		&& id_expr_token->ambiguous_p)
3550	      {
3551		cp_parser_simulate_error (parser);
3552		return error_mark_node;
3553	      }
3554
3555	    decl = cp_parser_lookup_name (parser, id_expression,
3556					  none_type,
3557					  template_p,
3558					  /*is_namespace=*/false,
3559					  /*check_dependency=*/true,
3560					  &ambiguous_decls,
3561					  id_expr_token->location);
3562	    /* If the lookup was ambiguous, an error will already have
3563	       been issued.  */
3564	    if (ambiguous_decls)
3565	      return error_mark_node;
3566
3567	    /* In Objective-C++, an instance variable (ivar) may be preferred
3568	       to whatever cp_parser_lookup_name() found.  */
3569	    decl = objc_lookup_ivar (decl, id_expression);
3570
3571	    /* If name lookup gives us a SCOPE_REF, then the
3572	       qualifying scope was dependent.  */
3573	    if (TREE_CODE (decl) == SCOPE_REF)
3574	      {
3575		/* At this point, we do not know if DECL is a valid
3576		   integral constant expression.  We assume that it is
3577		   in fact such an expression, so that code like:
3578
3579		      template <int N> struct A {
3580			int a[B<N>::i];
3581		      };
3582
3583		   is accepted.  At template-instantiation time, we
3584		   will check that B<N>::i is actually a constant.  */
3585		return decl;
3586	      }
3587	    /* Check to see if DECL is a local variable in a context
3588	       where that is forbidden.  */
3589	    if (parser->local_variables_forbidden_p
3590		&& local_variable_p (decl))
3591	      {
3592		/* It might be that we only found DECL because we are
3593		   trying to be generous with pre-ISO scoping rules.
3594		   For example, consider:
3595
3596		     int i;
3597		     void g() {
3598		       for (int i = 0; i < 10; ++i) {}
3599		       extern void f(int j = i);
3600		     }
3601
3602		   Here, name look up will originally find the out
3603		   of scope `i'.  We need to issue a warning message,
3604		   but then use the global `i'.  */
3605		decl = check_for_out_of_scope_variable (decl);
3606		if (local_variable_p (decl))
3607		  {
3608		    error_at (id_expr_token->location,
3609			      "local variable %qD may not appear in this context",
3610			      decl);
3611		    return error_mark_node;
3612		  }
3613	      }
3614	  }
3615
3616	decl = (finish_id_expression
3617		(id_expression, decl, parser->scope,
3618		 idk,
3619		 parser->integral_constant_expression_p,
3620		 parser->allow_non_integral_constant_expression_p,
3621		 &parser->non_integral_constant_expression_p,
3622		 template_p, done, address_p,
3623		 template_arg_p,
3624		 &error_msg,
3625                 id_expr_token->location));
3626	if (error_msg)
3627	  cp_parser_error (parser, error_msg);
3628	return decl;
3629      }
3630
3631      /* Anything else is an error.  */
3632    default:
3633      cp_parser_error (parser, "expected primary-expression");
3634      return error_mark_node;
3635    }
3636}
3637
3638/* Parse an id-expression.
3639
3640   id-expression:
3641     unqualified-id
3642     qualified-id
3643
3644   qualified-id:
3645     :: [opt] nested-name-specifier template [opt] unqualified-id
3646     :: identifier
3647     :: operator-function-id
3648     :: template-id
3649
3650   Return a representation of the unqualified portion of the
3651   identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3652   a `::' or nested-name-specifier.
3653
3654   Often, if the id-expression was a qualified-id, the caller will
3655   want to make a SCOPE_REF to represent the qualified-id.  This
3656   function does not do this in order to avoid wastefully creating
3657   SCOPE_REFs when they are not required.
3658
3659   If TEMPLATE_KEYWORD_P is true, then we have just seen the
3660   `template' keyword.
3661
3662   If CHECK_DEPENDENCY_P is false, then names are looked up inside
3663   uninstantiated templates.
3664
3665   If *TEMPLATE_P is non-NULL, it is set to true iff the
3666   `template' keyword is used to explicitly indicate that the entity
3667   named is a template.
3668
3669   If DECLARATOR_P is true, the id-expression is appearing as part of
3670   a declarator, rather than as part of an expression.  */
3671
3672static tree
3673cp_parser_id_expression (cp_parser *parser,
3674			 bool template_keyword_p,
3675			 bool check_dependency_p,
3676			 bool *template_p,
3677			 bool declarator_p,
3678			 bool optional_p)
3679{
3680  bool global_scope_p;
3681  bool nested_name_specifier_p;
3682
3683  /* Assume the `template' keyword was not used.  */
3684  if (template_p)
3685    *template_p = template_keyword_p;
3686
3687  /* Look for the optional `::' operator.  */
3688  global_scope_p
3689    = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3690       != NULL_TREE);
3691  /* Look for the optional nested-name-specifier.  */
3692  nested_name_specifier_p
3693    = (cp_parser_nested_name_specifier_opt (parser,
3694					    /*typename_keyword_p=*/false,
3695					    check_dependency_p,
3696					    /*type_p=*/false,
3697					    declarator_p)
3698       != NULL_TREE);
3699  /* If there is a nested-name-specifier, then we are looking at
3700     the first qualified-id production.  */
3701  if (nested_name_specifier_p)
3702    {
3703      tree saved_scope;
3704      tree saved_object_scope;
3705      tree saved_qualifying_scope;
3706      tree unqualified_id;
3707      bool is_template;
3708
3709      /* See if the next token is the `template' keyword.  */
3710      if (!template_p)
3711	template_p = &is_template;
3712      *template_p = cp_parser_optional_template_keyword (parser);
3713      /* Name lookup we do during the processing of the
3714	 unqualified-id might obliterate SCOPE.  */
3715      saved_scope = parser->scope;
3716      saved_object_scope = parser->object_scope;
3717      saved_qualifying_scope = parser->qualifying_scope;
3718      /* Process the final unqualified-id.  */
3719      unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3720						 check_dependency_p,
3721						 declarator_p,
3722						 /*optional_p=*/false);
3723      /* Restore the SAVED_SCOPE for our caller.  */
3724      parser->scope = saved_scope;
3725      parser->object_scope = saved_object_scope;
3726      parser->qualifying_scope = saved_qualifying_scope;
3727
3728      return unqualified_id;
3729    }
3730  /* Otherwise, if we are in global scope, then we are looking at one
3731     of the other qualified-id productions.  */
3732  else if (global_scope_p)
3733    {
3734      cp_token *token;
3735      tree id;
3736
3737      /* Peek at the next token.  */
3738      token = cp_lexer_peek_token (parser->lexer);
3739
3740      /* If it's an identifier, and the next token is not a "<", then
3741	 we can avoid the template-id case.  This is an optimization
3742	 for this common case.  */
3743      if (token->type == CPP_NAME
3744	  && !cp_parser_nth_token_starts_template_argument_list_p
3745	       (parser, 2))
3746	return cp_parser_identifier (parser);
3747
3748      cp_parser_parse_tentatively (parser);
3749      /* Try a template-id.  */
3750      id = cp_parser_template_id (parser,
3751				  /*template_keyword_p=*/false,
3752				  /*check_dependency_p=*/true,
3753				  declarator_p);
3754      /* If that worked, we're done.  */
3755      if (cp_parser_parse_definitely (parser))
3756	return id;
3757
3758      /* Peek at the next token.  (Changes in the token buffer may
3759	 have invalidated the pointer obtained above.)  */
3760      token = cp_lexer_peek_token (parser->lexer);
3761
3762      switch (token->type)
3763	{
3764	case CPP_NAME:
3765	  return cp_parser_identifier (parser);
3766
3767	case CPP_KEYWORD:
3768	  if (token->keyword == RID_OPERATOR)
3769	    return cp_parser_operator_function_id (parser);
3770	  /* Fall through.  */
3771
3772	default:
3773	  cp_parser_error (parser, "expected id-expression");
3774	  return error_mark_node;
3775	}
3776    }
3777  else
3778    return cp_parser_unqualified_id (parser, template_keyword_p,
3779				     /*check_dependency_p=*/true,
3780				     declarator_p,
3781				     optional_p);
3782}
3783
3784/* Parse an unqualified-id.
3785
3786   unqualified-id:
3787     identifier
3788     operator-function-id
3789     conversion-function-id
3790     ~ class-name
3791     template-id
3792
3793   If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3794   keyword, in a construct like `A::template ...'.
3795
3796   Returns a representation of unqualified-id.  For the `identifier'
3797   production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3798   production a BIT_NOT_EXPR is returned; the operand of the
3799   BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3800   other productions, see the documentation accompanying the
3801   corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3802   names are looked up in uninstantiated templates.  If DECLARATOR_P
3803   is true, the unqualified-id is appearing as part of a declarator,
3804   rather than as part of an expression.  */
3805
3806static tree
3807cp_parser_unqualified_id (cp_parser* parser,
3808			  bool template_keyword_p,
3809			  bool check_dependency_p,
3810			  bool declarator_p,
3811			  bool optional_p)
3812{
3813  cp_token *token;
3814
3815  /* Peek at the next token.  */
3816  token = cp_lexer_peek_token (parser->lexer);
3817
3818  switch (token->type)
3819    {
3820    case CPP_NAME:
3821      {
3822	tree id;
3823
3824	/* We don't know yet whether or not this will be a
3825	   template-id.  */
3826	cp_parser_parse_tentatively (parser);
3827	/* Try a template-id.  */
3828	id = cp_parser_template_id (parser, template_keyword_p,
3829				    check_dependency_p,
3830				    declarator_p);
3831	/* If it worked, we're done.  */
3832	if (cp_parser_parse_definitely (parser))
3833	  return id;
3834	/* Otherwise, it's an ordinary identifier.  */
3835	return cp_parser_identifier (parser);
3836      }
3837
3838    case CPP_TEMPLATE_ID:
3839      return cp_parser_template_id (parser, template_keyword_p,
3840				    check_dependency_p,
3841				    declarator_p);
3842
3843    case CPP_COMPL:
3844      {
3845	tree type_decl;
3846	tree qualifying_scope;
3847	tree object_scope;
3848	tree scope;
3849	bool done;
3850
3851	/* Consume the `~' token.  */
3852	cp_lexer_consume_token (parser->lexer);
3853	/* Parse the class-name.  The standard, as written, seems to
3854	   say that:
3855
3856	     template <typename T> struct S { ~S (); };
3857	     template <typename T> S<T>::~S() {}
3858
3859	   is invalid, since `~' must be followed by a class-name, but
3860	   `S<T>' is dependent, and so not known to be a class.
3861	   That's not right; we need to look in uninstantiated
3862	   templates.  A further complication arises from:
3863
3864	     template <typename T> void f(T t) {
3865	       t.T::~T();
3866	     }
3867
3868	   Here, it is not possible to look up `T' in the scope of `T'
3869	   itself.  We must look in both the current scope, and the
3870	   scope of the containing complete expression.
3871
3872	   Yet another issue is:
3873
3874	     struct S {
3875	       int S;
3876	       ~S();
3877	     };
3878
3879	     S::~S() {}
3880
3881	   The standard does not seem to say that the `S' in `~S'
3882	   should refer to the type `S' and not the data member
3883	   `S::S'.  */
3884
3885	/* DR 244 says that we look up the name after the "~" in the
3886	   same scope as we looked up the qualifying name.  That idea
3887	   isn't fully worked out; it's more complicated than that.  */
3888	scope = parser->scope;
3889	object_scope = parser->object_scope;
3890	qualifying_scope = parser->qualifying_scope;
3891
3892	/* Check for invalid scopes.  */
3893	if (scope == error_mark_node)
3894	  {
3895	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3896	      cp_lexer_consume_token (parser->lexer);
3897	    return error_mark_node;
3898	  }
3899	if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3900	  {
3901	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3902	      error_at (token->location,
3903			"scope %qT before %<~%> is not a class-name",
3904			scope);
3905	    cp_parser_simulate_error (parser);
3906	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3907	      cp_lexer_consume_token (parser->lexer);
3908	    return error_mark_node;
3909	  }
3910	gcc_assert (!scope || TYPE_P (scope));
3911
3912	/* If the name is of the form "X::~X" it's OK even if X is a
3913	   typedef.  */
3914	token = cp_lexer_peek_token (parser->lexer);
3915	if (scope
3916	    && token->type == CPP_NAME
3917	    && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3918		!= CPP_LESS)
3919	    && (token->u.value == TYPE_IDENTIFIER (scope)
3920		|| constructor_name_p (token->u.value, scope)))
3921	  {
3922	    cp_lexer_consume_token (parser->lexer);
3923	    return build_nt (BIT_NOT_EXPR, scope);
3924	  }
3925
3926	/* If there was an explicit qualification (S::~T), first look
3927	   in the scope given by the qualification (i.e., S).
3928
3929	   Note: in the calls to cp_parser_class_name below we pass
3930	   typename_type so that lookup finds the injected-class-name
3931	   rather than the constructor.  */
3932	done = false;
3933	type_decl = NULL_TREE;
3934	if (scope)
3935	  {
3936	    cp_parser_parse_tentatively (parser);
3937	    type_decl = cp_parser_class_name (parser,
3938					      /*typename_keyword_p=*/false,
3939					      /*template_keyword_p=*/false,
3940					      typename_type,
3941					      /*check_dependency=*/false,
3942					      /*class_head_p=*/false,
3943					      declarator_p);
3944	    if (cp_parser_parse_definitely (parser))
3945	      done = true;
3946	  }
3947	/* In "N::S::~S", look in "N" as well.  */
3948	if (!done && scope && qualifying_scope)
3949	  {
3950	    cp_parser_parse_tentatively (parser);
3951	    parser->scope = qualifying_scope;
3952	    parser->object_scope = NULL_TREE;
3953	    parser->qualifying_scope = NULL_TREE;
3954	    type_decl
3955	      = cp_parser_class_name (parser,
3956				      /*typename_keyword_p=*/false,
3957				      /*template_keyword_p=*/false,
3958				      typename_type,
3959				      /*check_dependency=*/false,
3960				      /*class_head_p=*/false,
3961				      declarator_p);
3962	    if (cp_parser_parse_definitely (parser))
3963	      done = true;
3964	  }
3965	/* In "p->S::~T", look in the scope given by "*p" as well.  */
3966	else if (!done && object_scope)
3967	  {
3968	    cp_parser_parse_tentatively (parser);
3969	    parser->scope = object_scope;
3970	    parser->object_scope = NULL_TREE;
3971	    parser->qualifying_scope = NULL_TREE;
3972	    type_decl
3973	      = cp_parser_class_name (parser,
3974				      /*typename_keyword_p=*/false,
3975				      /*template_keyword_p=*/false,
3976				      typename_type,
3977				      /*check_dependency=*/false,
3978				      /*class_head_p=*/false,
3979				      declarator_p);
3980	    if (cp_parser_parse_definitely (parser))
3981	      done = true;
3982	  }
3983	/* Look in the surrounding context.  */
3984	if (!done)
3985	  {
3986	    parser->scope = NULL_TREE;
3987	    parser->object_scope = NULL_TREE;
3988	    parser->qualifying_scope = NULL_TREE;
3989	    if (processing_template_decl)
3990	      cp_parser_parse_tentatively (parser);
3991	    type_decl
3992	      = cp_parser_class_name (parser,
3993				      /*typename_keyword_p=*/false,
3994				      /*template_keyword_p=*/false,
3995				      typename_type,
3996				      /*check_dependency=*/false,
3997				      /*class_head_p=*/false,
3998				      declarator_p);
3999	    if (processing_template_decl
4000		&& ! cp_parser_parse_definitely (parser))
4001	      {
4002		/* We couldn't find a type with this name, so just accept
4003		   it and check for a match at instantiation time.  */
4004		type_decl = cp_parser_identifier (parser);
4005		if (type_decl != error_mark_node)
4006		  type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4007		return type_decl;
4008	      }
4009	  }
4010	/* If an error occurred, assume that the name of the
4011	   destructor is the same as the name of the qualifying
4012	   class.  That allows us to keep parsing after running
4013	   into ill-formed destructor names.  */
4014	if (type_decl == error_mark_node && scope)
4015	  return build_nt (BIT_NOT_EXPR, scope);
4016	else if (type_decl == error_mark_node)
4017	  return error_mark_node;
4018
4019	/* Check that destructor name and scope match.  */
4020	if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4021	  {
4022	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4023	      error_at (token->location,
4024			"declaration of %<~%T%> as member of %qT",
4025			type_decl, scope);
4026	    cp_parser_simulate_error (parser);
4027	    return error_mark_node;
4028	  }
4029
4030	/* [class.dtor]
4031
4032	   A typedef-name that names a class shall not be used as the
4033	   identifier in the declarator for a destructor declaration.  */
4034	if (declarator_p
4035	    && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4036	    && !DECL_SELF_REFERENCE_P (type_decl)
4037	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4038	  error_at (token->location,
4039		    "typedef-name %qD used as destructor declarator",
4040		    type_decl);
4041
4042	return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4043      }
4044
4045    case CPP_KEYWORD:
4046      if (token->keyword == RID_OPERATOR)
4047	{
4048	  tree id;
4049
4050	  /* This could be a template-id, so we try that first.  */
4051	  cp_parser_parse_tentatively (parser);
4052	  /* Try a template-id.  */
4053	  id = cp_parser_template_id (parser, template_keyword_p,
4054				      /*check_dependency_p=*/true,
4055				      declarator_p);
4056	  /* If that worked, we're done.  */
4057	  if (cp_parser_parse_definitely (parser))
4058	    return id;
4059	  /* We still don't know whether we're looking at an
4060	     operator-function-id or a conversion-function-id.  */
4061	  cp_parser_parse_tentatively (parser);
4062	  /* Try an operator-function-id.  */
4063	  id = cp_parser_operator_function_id (parser);
4064	  /* If that didn't work, try a conversion-function-id.  */
4065	  if (!cp_parser_parse_definitely (parser))
4066	    id = cp_parser_conversion_function_id (parser);
4067
4068	  return id;
4069	}
4070      /* Fall through.  */
4071
4072    default:
4073      if (optional_p)
4074	return NULL_TREE;
4075      cp_parser_error (parser, "expected unqualified-id");
4076      return error_mark_node;
4077    }
4078}
4079
4080/* Parse an (optional) nested-name-specifier.
4081
4082   nested-name-specifier: [C++98]
4083     class-or-namespace-name :: nested-name-specifier [opt]
4084     class-or-namespace-name :: template nested-name-specifier [opt]
4085
4086   nested-name-specifier: [C++0x]
4087     type-name ::
4088     namespace-name ::
4089     nested-name-specifier identifier ::
4090     nested-name-specifier template [opt] simple-template-id ::
4091
4092   PARSER->SCOPE should be set appropriately before this function is
4093   called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4094   effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4095   in name lookups.
4096
4097   Sets PARSER->SCOPE to the class (TYPE) or namespace
4098   (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4099   it unchanged if there is no nested-name-specifier.  Returns the new
4100   scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4101
4102   If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4103   part of a declaration and/or decl-specifier.  */
4104
4105static tree
4106cp_parser_nested_name_specifier_opt (cp_parser *parser,
4107				     bool typename_keyword_p,
4108				     bool check_dependency_p,
4109				     bool type_p,
4110				     bool is_declaration)
4111{
4112  bool success = false;
4113  cp_token_position start = 0;
4114  cp_token *token;
4115
4116  /* Remember where the nested-name-specifier starts.  */
4117  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4118    {
4119      start = cp_lexer_token_position (parser->lexer, false);
4120      push_deferring_access_checks (dk_deferred);
4121    }
4122
4123  while (true)
4124    {
4125      tree new_scope;
4126      tree old_scope;
4127      tree saved_qualifying_scope;
4128      bool template_keyword_p;
4129
4130      /* Spot cases that cannot be the beginning of a
4131	 nested-name-specifier.  */
4132      token = cp_lexer_peek_token (parser->lexer);
4133
4134      /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4135	 the already parsed nested-name-specifier.  */
4136      if (token->type == CPP_NESTED_NAME_SPECIFIER)
4137	{
4138	  /* Grab the nested-name-specifier and continue the loop.  */
4139	  cp_parser_pre_parsed_nested_name_specifier (parser);
4140	  /* If we originally encountered this nested-name-specifier
4141	     with IS_DECLARATION set to false, we will not have
4142	     resolved TYPENAME_TYPEs, so we must do so here.  */
4143	  if (is_declaration
4144	      && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4145	    {
4146	      new_scope = resolve_typename_type (parser->scope,
4147						 /*only_current_p=*/false);
4148	      if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4149		parser->scope = new_scope;
4150	    }
4151	  success = true;
4152	  continue;
4153	}
4154
4155      /* Spot cases that cannot be the beginning of a
4156	 nested-name-specifier.  On the second and subsequent times
4157	 through the loop, we look for the `template' keyword.  */
4158      if (success && token->keyword == RID_TEMPLATE)
4159	;
4160      /* A template-id can start a nested-name-specifier.  */
4161      else if (token->type == CPP_TEMPLATE_ID)
4162	;
4163      else
4164	{
4165	  /* If the next token is not an identifier, then it is
4166	     definitely not a type-name or namespace-name.  */
4167	  if (token->type != CPP_NAME)
4168	    break;
4169	  /* If the following token is neither a `<' (to begin a
4170	     template-id), nor a `::', then we are not looking at a
4171	     nested-name-specifier.  */
4172	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
4173	  if (token->type != CPP_SCOPE
4174	      && !cp_parser_nth_token_starts_template_argument_list_p
4175		  (parser, 2))
4176	    break;
4177	}
4178
4179      /* The nested-name-specifier is optional, so we parse
4180	 tentatively.  */
4181      cp_parser_parse_tentatively (parser);
4182
4183      /* Look for the optional `template' keyword, if this isn't the
4184	 first time through the loop.  */
4185      if (success)
4186	template_keyword_p = cp_parser_optional_template_keyword (parser);
4187      else
4188	template_keyword_p = false;
4189
4190      /* Save the old scope since the name lookup we are about to do
4191	 might destroy it.  */
4192      old_scope = parser->scope;
4193      saved_qualifying_scope = parser->qualifying_scope;
4194      /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4195	 look up names in "X<T>::I" in order to determine that "Y" is
4196	 a template.  So, if we have a typename at this point, we make
4197	 an effort to look through it.  */
4198      if (is_declaration
4199	  && !typename_keyword_p
4200	  && parser->scope
4201	  && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4202	parser->scope = resolve_typename_type (parser->scope,
4203					       /*only_current_p=*/false);
4204      /* Parse the qualifying entity.  */
4205      new_scope
4206	= cp_parser_qualifying_entity (parser,
4207                                       typename_keyword_p,
4208                                       template_keyword_p,
4209                                       check_dependency_p,
4210                                       type_p,
4211                                       is_declaration);
4212      /* Look for the `::' token.  */
4213      cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4214
4215      /* If we found what we wanted, we keep going; otherwise, we're
4216	 done.  */
4217      if (!cp_parser_parse_definitely (parser))
4218	{
4219	  bool error_p = false;
4220
4221	  /* Restore the OLD_SCOPE since it was valid before the
4222	     failed attempt at finding the last
4223	     class-or-namespace-name.  */
4224	  parser->scope = old_scope;
4225	  parser->qualifying_scope = saved_qualifying_scope;
4226	  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4227	    break;
4228	  /* If the next token is an identifier, and the one after
4229	     that is a `::', then any valid interpretation would have
4230	     found a class-or-namespace-name.  */
4231	  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4232		 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4233		     == CPP_SCOPE)
4234		 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4235		     != CPP_COMPL))
4236	    {
4237	      token = cp_lexer_consume_token (parser->lexer);
4238	      if (!error_p)
4239		{
4240		  if (!token->ambiguous_p)
4241		    {
4242		      tree decl;
4243		      tree ambiguous_decls;
4244
4245		      decl = cp_parser_lookup_name (parser, token->u.value,
4246						    none_type,
4247						    /*is_template=*/false,
4248						    /*is_namespace=*/false,
4249						    /*check_dependency=*/true,
4250						    &ambiguous_decls,
4251						    token->location);
4252		      if (TREE_CODE (decl) == TEMPLATE_DECL)
4253			error_at (token->location,
4254				  "%qD used without template parameters",
4255				  decl);
4256		      else if (ambiguous_decls)
4257			{
4258			  error_at (token->location,
4259				    "reference to %qD is ambiguous",
4260				    token->u.value);
4261			  print_candidates (ambiguous_decls);
4262			  decl = error_mark_node;
4263			}
4264		      else
4265                        {
4266                          const char* msg = "is not a class or namespace";
4267                          if (cxx_dialect != cxx98)
4268                            msg = "is not a class, namespace, or enumeration";
4269                          cp_parser_name_lookup_error
4270                            (parser, token->u.value, decl, msg,
4271	  		     token->location);
4272                        }
4273		    }
4274		  parser->scope = error_mark_node;
4275		  error_p = true;
4276		  /* Treat this as a successful nested-name-specifier
4277		     due to:
4278
4279		     [basic.lookup.qual]
4280
4281		     If the name found is not a class-name (clause
4282		     _class_) or namespace-name (_namespace.def_), the
4283		     program is ill-formed.  */
4284		  success = true;
4285		}
4286	      cp_lexer_consume_token (parser->lexer);
4287	    }
4288	  break;
4289	}
4290      /* We've found one valid nested-name-specifier.  */
4291      success = true;
4292      /* Name lookup always gives us a DECL.  */
4293      if (TREE_CODE (new_scope) == TYPE_DECL)
4294	new_scope = TREE_TYPE (new_scope);
4295      /* Uses of "template" must be followed by actual templates.  */
4296      if (template_keyword_p
4297	  && !(CLASS_TYPE_P (new_scope)
4298	       && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4299		    && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4300		   || CLASSTYPE_IS_TEMPLATE (new_scope)))
4301	  && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4302	       && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4303		   == TEMPLATE_ID_EXPR)))
4304	permerror (input_location, TYPE_P (new_scope)
4305		   ? "%qT is not a template"
4306		   : "%qD is not a template",
4307		   new_scope);
4308      /* If it is a class scope, try to complete it; we are about to
4309	 be looking up names inside the class.  */
4310      if (TYPE_P (new_scope)
4311	  /* Since checking types for dependency can be expensive,
4312	     avoid doing it if the type is already complete.  */
4313	  && !COMPLETE_TYPE_P (new_scope)
4314	  /* Do not try to complete dependent types.  */
4315	  && !dependent_type_p (new_scope))
4316	{
4317	  new_scope = complete_type (new_scope);
4318	  /* If it is a typedef to current class, use the current
4319	     class instead, as the typedef won't have any names inside
4320	     it yet.  */
4321	  if (!COMPLETE_TYPE_P (new_scope)
4322	      && currently_open_class (new_scope))
4323	    new_scope = TYPE_MAIN_VARIANT (new_scope);
4324	}
4325      /* Make sure we look in the right scope the next time through
4326	 the loop.  */
4327      parser->scope = new_scope;
4328    }
4329
4330  /* If parsing tentatively, replace the sequence of tokens that makes
4331     up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4332     token.  That way, should we re-parse the token stream, we will
4333     not have to repeat the effort required to do the parse, nor will
4334     we issue duplicate error messages.  */
4335  if (success && start)
4336    {
4337      cp_token *token;
4338
4339      token = cp_lexer_token_at (parser->lexer, start);
4340      /* Reset the contents of the START token.  */
4341      token->type = CPP_NESTED_NAME_SPECIFIER;
4342      /* Retrieve any deferred checks.  Do not pop this access checks yet
4343	 so the memory will not be reclaimed during token replacing below.  */
4344      token->u.tree_check_value = GGC_CNEW (struct tree_check);
4345      token->u.tree_check_value->value = parser->scope;
4346      token->u.tree_check_value->checks = get_deferred_access_checks ();
4347      token->u.tree_check_value->qualifying_scope =
4348	parser->qualifying_scope;
4349      token->keyword = RID_MAX;
4350
4351      /* Purge all subsequent tokens.  */
4352      cp_lexer_purge_tokens_after (parser->lexer, start);
4353    }
4354
4355  if (start)
4356    pop_to_parent_deferring_access_checks ();
4357
4358  return success ? parser->scope : NULL_TREE;
4359}
4360
4361/* Parse a nested-name-specifier.  See
4362   cp_parser_nested_name_specifier_opt for details.  This function
4363   behaves identically, except that it will an issue an error if no
4364   nested-name-specifier is present.  */
4365
4366static tree
4367cp_parser_nested_name_specifier (cp_parser *parser,
4368				 bool typename_keyword_p,
4369				 bool check_dependency_p,
4370				 bool type_p,
4371				 bool is_declaration)
4372{
4373  tree scope;
4374
4375  /* Look for the nested-name-specifier.  */
4376  scope = cp_parser_nested_name_specifier_opt (parser,
4377					       typename_keyword_p,
4378					       check_dependency_p,
4379					       type_p,
4380					       is_declaration);
4381  /* If it was not present, issue an error message.  */
4382  if (!scope)
4383    {
4384      cp_parser_error (parser, "expected nested-name-specifier");
4385      parser->scope = NULL_TREE;
4386    }
4387
4388  return scope;
4389}
4390
4391/* Parse the qualifying entity in a nested-name-specifier. For C++98,
4392   this is either a class-name or a namespace-name (which corresponds
4393   to the class-or-namespace-name production in the grammar). For
4394   C++0x, it can also be a type-name that refers to an enumeration
4395   type.
4396
4397   TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4398   TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4399   CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4400   TYPE_P is TRUE iff the next name should be taken as a class-name,
4401   even the same name is declared to be another entity in the same
4402   scope.
4403
4404   Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4405   specified by the class-or-namespace-name.  If neither is found the
4406   ERROR_MARK_NODE is returned.  */
4407
4408static tree
4409cp_parser_qualifying_entity (cp_parser *parser,
4410			     bool typename_keyword_p,
4411			     bool template_keyword_p,
4412			     bool check_dependency_p,
4413			     bool type_p,
4414			     bool is_declaration)
4415{
4416  tree saved_scope;
4417  tree saved_qualifying_scope;
4418  tree saved_object_scope;
4419  tree scope;
4420  bool only_class_p;
4421  bool successful_parse_p;
4422
4423  /* Before we try to parse the class-name, we must save away the
4424     current PARSER->SCOPE since cp_parser_class_name will destroy
4425     it.  */
4426  saved_scope = parser->scope;
4427  saved_qualifying_scope = parser->qualifying_scope;
4428  saved_object_scope = parser->object_scope;
4429  /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4430     there is no need to look for a namespace-name.  */
4431  only_class_p = template_keyword_p
4432    || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4433  if (!only_class_p)
4434    cp_parser_parse_tentatively (parser);
4435  scope = cp_parser_class_name (parser,
4436				typename_keyword_p,
4437				template_keyword_p,
4438				type_p ? class_type : none_type,
4439				check_dependency_p,
4440				/*class_head_p=*/false,
4441				is_declaration);
4442  successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4443  /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4444  if (!only_class_p
4445      && cxx_dialect != cxx98
4446      && !successful_parse_p)
4447    {
4448      /* Restore the saved scope.  */
4449      parser->scope = saved_scope;
4450      parser->qualifying_scope = saved_qualifying_scope;
4451      parser->object_scope = saved_object_scope;
4452
4453      /* Parse tentatively.  */
4454      cp_parser_parse_tentatively (parser);
4455
4456      /* Parse a typedef-name or enum-name.  */
4457      scope = cp_parser_nonclass_name (parser);
4458
4459      /* "If the name found does not designate a namespace or a class,
4460	 enumeration, or dependent type, the program is ill-formed."
4461
4462         We cover classes and dependent types above and namespaces below,
4463         so this code is only looking for enums.  */
4464      if (!scope || TREE_CODE (scope) != TYPE_DECL
4465	  || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4466	cp_parser_simulate_error (parser);
4467
4468      successful_parse_p = cp_parser_parse_definitely (parser);
4469    }
4470  /* If that didn't work, try for a namespace-name.  */
4471  if (!only_class_p && !successful_parse_p)
4472    {
4473      /* Restore the saved scope.  */
4474      parser->scope = saved_scope;
4475      parser->qualifying_scope = saved_qualifying_scope;
4476      parser->object_scope = saved_object_scope;
4477      /* If we are not looking at an identifier followed by the scope
4478	 resolution operator, then this is not part of a
4479	 nested-name-specifier.  (Note that this function is only used
4480	 to parse the components of a nested-name-specifier.)  */
4481      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4482	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4483	return error_mark_node;
4484      scope = cp_parser_namespace_name (parser);
4485    }
4486
4487  return scope;
4488}
4489
4490/* Parse a postfix-expression.
4491
4492   postfix-expression:
4493     primary-expression
4494     postfix-expression [ expression ]
4495     postfix-expression ( expression-list [opt] )
4496     simple-type-specifier ( expression-list [opt] )
4497     typename :: [opt] nested-name-specifier identifier
4498       ( expression-list [opt] )
4499     typename :: [opt] nested-name-specifier template [opt] template-id
4500       ( expression-list [opt] )
4501     postfix-expression . template [opt] id-expression
4502     postfix-expression -> template [opt] id-expression
4503     postfix-expression . pseudo-destructor-name
4504     postfix-expression -> pseudo-destructor-name
4505     postfix-expression ++
4506     postfix-expression --
4507     dynamic_cast < type-id > ( expression )
4508     static_cast < type-id > ( expression )
4509     reinterpret_cast < type-id > ( expression )
4510     const_cast < type-id > ( expression )
4511     typeid ( expression )
4512     typeid ( type-id )
4513
4514   GNU Extension:
4515
4516   postfix-expression:
4517     ( type-id ) { initializer-list , [opt] }
4518
4519   This extension is a GNU version of the C99 compound-literal
4520   construct.  (The C99 grammar uses `type-name' instead of `type-id',
4521   but they are essentially the same concept.)
4522
4523   If ADDRESS_P is true, the postfix expression is the operand of the
4524   `&' operator.  CAST_P is true if this expression is the target of a
4525   cast.
4526
4527   If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4528   class member access expressions [expr.ref].
4529
4530   Returns a representation of the expression.  */
4531
4532static tree
4533cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4534                              bool member_access_only_p,
4535			      cp_id_kind * pidk_return)
4536{
4537  cp_token *token;
4538  enum rid keyword;
4539  cp_id_kind idk = CP_ID_KIND_NONE;
4540  tree postfix_expression = NULL_TREE;
4541  bool is_member_access = false;
4542
4543  /* Peek at the next token.  */
4544  token = cp_lexer_peek_token (parser->lexer);
4545  /* Some of the productions are determined by keywords.  */
4546  keyword = token->keyword;
4547  switch (keyword)
4548    {
4549    case RID_DYNCAST:
4550    case RID_STATCAST:
4551    case RID_REINTCAST:
4552    case RID_CONSTCAST:
4553      {
4554	tree type;
4555	tree expression;
4556	const char *saved_message;
4557
4558	/* All of these can be handled in the same way from the point
4559	   of view of parsing.  Begin by consuming the token
4560	   identifying the cast.  */
4561	cp_lexer_consume_token (parser->lexer);
4562
4563	/* New types cannot be defined in the cast.  */
4564	saved_message = parser->type_definition_forbidden_message;
4565	parser->type_definition_forbidden_message
4566	  = G_("types may not be defined in casts");
4567
4568	/* Look for the opening `<'.  */
4569	cp_parser_require (parser, CPP_LESS, "%<<%>");
4570	/* Parse the type to which we are casting.  */
4571	type = cp_parser_type_id (parser);
4572	/* Look for the closing `>'.  */
4573	cp_parser_require (parser, CPP_GREATER, "%<>%>");
4574	/* Restore the old message.  */
4575	parser->type_definition_forbidden_message = saved_message;
4576
4577	/* And the expression which is being cast.  */
4578	cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4579	expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4580	cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4581
4582	/* Only type conversions to integral or enumeration types
4583	   can be used in constant-expressions.  */
4584	if (!cast_valid_in_integral_constant_expression_p (type)
4585	    && (cp_parser_non_integral_constant_expression
4586		(parser,
4587		 "a cast to a type other than an integral or "
4588		 "enumeration type")))
4589	  return error_mark_node;
4590
4591	switch (keyword)
4592	  {
4593	  case RID_DYNCAST:
4594	    postfix_expression
4595	      = build_dynamic_cast (type, expression, tf_warning_or_error);
4596	    break;
4597	  case RID_STATCAST:
4598	    postfix_expression
4599	      = build_static_cast (type, expression, tf_warning_or_error);
4600	    break;
4601	  case RID_REINTCAST:
4602	    postfix_expression
4603	      = build_reinterpret_cast (type, expression,
4604                                        tf_warning_or_error);
4605	    break;
4606	  case RID_CONSTCAST:
4607	    postfix_expression
4608	      = build_const_cast (type, expression, tf_warning_or_error);
4609	    break;
4610	  default:
4611	    gcc_unreachable ();
4612	  }
4613      }
4614      break;
4615
4616    case RID_TYPEID:
4617      {
4618	tree type;
4619	const char *saved_message;
4620	bool saved_in_type_id_in_expr_p;
4621
4622	/* Consume the `typeid' token.  */
4623	cp_lexer_consume_token (parser->lexer);
4624	/* Look for the `(' token.  */
4625	cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4626	/* Types cannot be defined in a `typeid' expression.  */
4627	saved_message = parser->type_definition_forbidden_message;
4628	parser->type_definition_forbidden_message
4629	  = G_("types may not be defined in a %<typeid%> expression");
4630	/* We can't be sure yet whether we're looking at a type-id or an
4631	   expression.  */
4632	cp_parser_parse_tentatively (parser);
4633	/* Try a type-id first.  */
4634	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4635	parser->in_type_id_in_expr_p = true;
4636	type = cp_parser_type_id (parser);
4637	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4638	/* Look for the `)' token.  Otherwise, we can't be sure that
4639	   we're not looking at an expression: consider `typeid (int
4640	   (3))', for example.  */
4641	cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4642	/* If all went well, simply lookup the type-id.  */
4643	if (cp_parser_parse_definitely (parser))
4644	  postfix_expression = get_typeid (type);
4645	/* Otherwise, fall back to the expression variant.  */
4646	else
4647	  {
4648	    tree expression;
4649
4650	    /* Look for an expression.  */
4651	    expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4652	    /* Compute its typeid.  */
4653	    postfix_expression = build_typeid (expression);
4654	    /* Look for the `)' token.  */
4655	    cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4656	  }
4657	/* Restore the saved message.  */
4658	parser->type_definition_forbidden_message = saved_message;
4659	/* `typeid' may not appear in an integral constant expression.  */
4660	if (cp_parser_non_integral_constant_expression(parser,
4661						       "%<typeid%> operator"))
4662	  return error_mark_node;
4663      }
4664      break;
4665
4666    case RID_TYPENAME:
4667      {
4668	tree type;
4669	/* The syntax permitted here is the same permitted for an
4670	   elaborated-type-specifier.  */
4671	type = cp_parser_elaborated_type_specifier (parser,
4672						    /*is_friend=*/false,
4673						    /*is_declaration=*/false);
4674	postfix_expression = cp_parser_functional_cast (parser, type);
4675      }
4676      break;
4677
4678    default:
4679      {
4680	tree type;
4681
4682	/* If the next thing is a simple-type-specifier, we may be
4683	   looking at a functional cast.  We could also be looking at
4684	   an id-expression.  So, we try the functional cast, and if
4685	   that doesn't work we fall back to the primary-expression.  */
4686	cp_parser_parse_tentatively (parser);
4687	/* Look for the simple-type-specifier.  */
4688	type = cp_parser_simple_type_specifier (parser,
4689						/*decl_specs=*/NULL,
4690						CP_PARSER_FLAGS_NONE);
4691	/* Parse the cast itself.  */
4692	if (!cp_parser_error_occurred (parser))
4693	  postfix_expression
4694	    = cp_parser_functional_cast (parser, type);
4695	/* If that worked, we're done.  */
4696	if (cp_parser_parse_definitely (parser))
4697	  break;
4698
4699	/* If the functional-cast didn't work out, try a
4700	   compound-literal.  */
4701	if (cp_parser_allow_gnu_extensions_p (parser)
4702	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4703	  {
4704	    VEC(constructor_elt,gc) *initializer_list = NULL;
4705	    bool saved_in_type_id_in_expr_p;
4706
4707	    cp_parser_parse_tentatively (parser);
4708	    /* Consume the `('.  */
4709	    cp_lexer_consume_token (parser->lexer);
4710	    /* Parse the type.  */
4711	    saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4712	    parser->in_type_id_in_expr_p = true;
4713	    type = cp_parser_type_id (parser);
4714	    parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4715	    /* Look for the `)'.  */
4716	    cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4717	    /* Look for the `{'.  */
4718	    cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4719	    /* If things aren't going well, there's no need to
4720	       keep going.  */
4721	    if (!cp_parser_error_occurred (parser))
4722	      {
4723		bool non_constant_p;
4724		/* Parse the initializer-list.  */
4725		initializer_list
4726		  = cp_parser_initializer_list (parser, &non_constant_p);
4727		/* Allow a trailing `,'.  */
4728		if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4729		  cp_lexer_consume_token (parser->lexer);
4730		/* Look for the final `}'.  */
4731		cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4732	      }
4733	    /* If that worked, we're definitely looking at a
4734	       compound-literal expression.  */
4735	    if (cp_parser_parse_definitely (parser))
4736	      {
4737		/* Warn the user that a compound literal is not
4738		   allowed in standard C++.  */
4739		pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4740		/* For simplicity, we disallow compound literals in
4741		   constant-expressions.  We could
4742		   allow compound literals of integer type, whose
4743		   initializer was a constant, in constant
4744		   expressions.  Permitting that usage, as a further
4745		   extension, would not change the meaning of any
4746		   currently accepted programs.  (Of course, as
4747		   compound literals are not part of ISO C++, the
4748		   standard has nothing to say.)  */
4749		if (cp_parser_non_integral_constant_expression
4750		    (parser, "non-constant compound literals"))
4751		  {
4752		    postfix_expression = error_mark_node;
4753		    break;
4754		  }
4755		/* Form the representation of the compound-literal.  */
4756		postfix_expression
4757		  = (finish_compound_literal
4758		     (type, build_constructor (init_list_type_node,
4759					       initializer_list)));
4760		break;
4761	      }
4762	  }
4763
4764	/* It must be a primary-expression.  */
4765	postfix_expression
4766	  = cp_parser_primary_expression (parser, address_p, cast_p,
4767					  /*template_arg_p=*/false,
4768					  &idk);
4769      }
4770      break;
4771    }
4772
4773  /* Keep looping until the postfix-expression is complete.  */
4774  while (true)
4775    {
4776      if (idk == CP_ID_KIND_UNQUALIFIED
4777	  && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4778	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4779	/* It is not a Koenig lookup function call.  */
4780	postfix_expression
4781	  = unqualified_name_lookup_error (postfix_expression);
4782
4783      /* Peek at the next token.  */
4784      token = cp_lexer_peek_token (parser->lexer);
4785
4786      switch (token->type)
4787	{
4788	case CPP_OPEN_SQUARE:
4789	  postfix_expression
4790	    = cp_parser_postfix_open_square_expression (parser,
4791							postfix_expression,
4792							false);
4793	  idk = CP_ID_KIND_NONE;
4794          is_member_access = false;
4795	  break;
4796
4797	case CPP_OPEN_PAREN:
4798	  /* postfix-expression ( expression-list [opt] ) */
4799	  {
4800	    bool koenig_p;
4801	    bool is_builtin_constant_p;
4802	    bool saved_integral_constant_expression_p = false;
4803	    bool saved_non_integral_constant_expression_p = false;
4804	    VEC(tree,gc) *args;
4805
4806            is_member_access = false;
4807
4808	    is_builtin_constant_p
4809	      = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4810	    if (is_builtin_constant_p)
4811	      {
4812		/* The whole point of __builtin_constant_p is to allow
4813		   non-constant expressions to appear as arguments.  */
4814		saved_integral_constant_expression_p
4815		  = parser->integral_constant_expression_p;
4816		saved_non_integral_constant_expression_p
4817		  = parser->non_integral_constant_expression_p;
4818		parser->integral_constant_expression_p = false;
4819	      }
4820	    args = (cp_parser_parenthesized_expression_list
4821		    (parser, /*is_attribute_list=*/false,
4822		     /*cast_p=*/false, /*allow_expansion_p=*/true,
4823		     /*non_constant_p=*/NULL));
4824	    if (is_builtin_constant_p)
4825	      {
4826		parser->integral_constant_expression_p
4827		  = saved_integral_constant_expression_p;
4828		parser->non_integral_constant_expression_p
4829		  = saved_non_integral_constant_expression_p;
4830	      }
4831
4832	    if (args == NULL)
4833	      {
4834		postfix_expression = error_mark_node;
4835		break;
4836	      }
4837
4838	    /* Function calls are not permitted in
4839	       constant-expressions.  */
4840	    if (! builtin_valid_in_constant_expr_p (postfix_expression)
4841		&& cp_parser_non_integral_constant_expression (parser,
4842							       "a function call"))
4843	      {
4844		postfix_expression = error_mark_node;
4845		release_tree_vector (args);
4846		break;
4847	      }
4848
4849	    koenig_p = false;
4850	    if (idk == CP_ID_KIND_UNQUALIFIED
4851		|| idk == CP_ID_KIND_TEMPLATE_ID)
4852	      {
4853		if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4854		  {
4855		    if (!VEC_empty (tree, args))
4856		      {
4857			koenig_p = true;
4858			if (!any_type_dependent_arguments_p (args))
4859			  postfix_expression
4860			    = perform_koenig_lookup (postfix_expression, args);
4861		      }
4862		    else
4863		      postfix_expression
4864			= unqualified_fn_lookup_error (postfix_expression);
4865		  }
4866		/* We do not perform argument-dependent lookup if
4867		   normal lookup finds a non-function, in accordance
4868		   with the expected resolution of DR 218.  */
4869		else if (!VEC_empty (tree, args)
4870			 && is_overloaded_fn (postfix_expression))
4871		  {
4872		    tree fn = get_first_fn (postfix_expression);
4873		    fn = STRIP_TEMPLATE (fn);
4874
4875		    /* Do not do argument dependent lookup if regular
4876		       lookup finds a member function or a block-scope
4877		       function declaration.  [basic.lookup.argdep]/3  */
4878		    if (!DECL_FUNCTION_MEMBER_P (fn)
4879			&& !DECL_LOCAL_FUNCTION_P (fn))
4880		      {
4881			koenig_p = true;
4882			if (!any_type_dependent_arguments_p (args))
4883			  postfix_expression
4884			    = perform_koenig_lookup (postfix_expression, args);
4885		      }
4886		  }
4887	      }
4888
4889	    if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4890	      {
4891		tree instance = TREE_OPERAND (postfix_expression, 0);
4892		tree fn = TREE_OPERAND (postfix_expression, 1);
4893
4894		if (processing_template_decl
4895		    && (type_dependent_expression_p (instance)
4896			|| (!BASELINK_P (fn)
4897			    && TREE_CODE (fn) != FIELD_DECL)
4898			|| type_dependent_expression_p (fn)
4899			|| any_type_dependent_arguments_p (args)))
4900		  {
4901		    postfix_expression
4902		      = build_nt_call_vec (postfix_expression, args);
4903		    release_tree_vector (args);
4904		    break;
4905		  }
4906
4907		if (BASELINK_P (fn))
4908		  {
4909		  postfix_expression
4910		    = (build_new_method_call
4911		       (instance, fn, &args, NULL_TREE,
4912			(idk == CP_ID_KIND_QUALIFIED
4913			 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4914			/*fn_p=*/NULL,
4915			tf_warning_or_error));
4916		  }
4917		else
4918		  postfix_expression
4919		    = finish_call_expr (postfix_expression, &args,
4920					/*disallow_virtual=*/false,
4921					/*koenig_p=*/false,
4922					tf_warning_or_error);
4923	      }
4924	    else if (TREE_CODE (postfix_expression) == OFFSET_REF
4925		     || TREE_CODE (postfix_expression) == MEMBER_REF
4926		     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4927	      postfix_expression = (build_offset_ref_call_from_tree
4928				    (postfix_expression, &args));
4929	    else if (idk == CP_ID_KIND_QUALIFIED)
4930	      /* A call to a static class member, or a namespace-scope
4931		 function.  */
4932	      postfix_expression
4933		= finish_call_expr (postfix_expression, &args,
4934				    /*disallow_virtual=*/true,
4935				    koenig_p,
4936				    tf_warning_or_error);
4937	    else
4938	      /* All other function calls.  */
4939	      postfix_expression
4940		= finish_call_expr (postfix_expression, &args,
4941				    /*disallow_virtual=*/false,
4942				    koenig_p,
4943				    tf_warning_or_error);
4944
4945	    /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4946	    idk = CP_ID_KIND_NONE;
4947
4948	    release_tree_vector (args);
4949	  }
4950	  break;
4951
4952	case CPP_DOT:
4953	case CPP_DEREF:
4954	  /* postfix-expression . template [opt] id-expression
4955	     postfix-expression . pseudo-destructor-name
4956	     postfix-expression -> template [opt] id-expression
4957	     postfix-expression -> pseudo-destructor-name */
4958
4959	  /* Consume the `.' or `->' operator.  */
4960	  cp_lexer_consume_token (parser->lexer);
4961
4962	  postfix_expression
4963	    = cp_parser_postfix_dot_deref_expression (parser, token->type,
4964						      postfix_expression,
4965						      false, &idk,
4966						      token->location);
4967
4968          is_member_access = true;
4969	  break;
4970
4971	case CPP_PLUS_PLUS:
4972	  /* postfix-expression ++  */
4973	  /* Consume the `++' token.  */
4974	  cp_lexer_consume_token (parser->lexer);
4975	  /* Generate a representation for the complete expression.  */
4976	  postfix_expression
4977	    = finish_increment_expr (postfix_expression,
4978				     POSTINCREMENT_EXPR);
4979	  /* Increments may not appear in constant-expressions.  */
4980	  if (cp_parser_non_integral_constant_expression (parser,
4981							  "an increment"))
4982	    postfix_expression = error_mark_node;
4983	  idk = CP_ID_KIND_NONE;
4984          is_member_access = false;
4985	  break;
4986
4987	case CPP_MINUS_MINUS:
4988	  /* postfix-expression -- */
4989	  /* Consume the `--' token.  */
4990	  cp_lexer_consume_token (parser->lexer);
4991	  /* Generate a representation for the complete expression.  */
4992	  postfix_expression
4993	    = finish_increment_expr (postfix_expression,
4994				     POSTDECREMENT_EXPR);
4995	  /* Decrements may not appear in constant-expressions.  */
4996	  if (cp_parser_non_integral_constant_expression (parser,
4997							  "a decrement"))
4998	    postfix_expression = error_mark_node;
4999	  idk = CP_ID_KIND_NONE;
5000          is_member_access = false;
5001	  break;
5002
5003	default:
5004	  if (pidk_return != NULL)
5005	    * pidk_return = idk;
5006          if (member_access_only_p)
5007            return is_member_access? postfix_expression : error_mark_node;
5008          else
5009            return postfix_expression;
5010	}
5011    }
5012
5013  /* We should never get here.  */
5014  gcc_unreachable ();
5015  return error_mark_node;
5016}
5017
5018/* A subroutine of cp_parser_postfix_expression that also gets hijacked
5019   by cp_parser_builtin_offsetof.  We're looking for
5020
5021     postfix-expression [ expression ]
5022
5023   FOR_OFFSETOF is set if we're being called in that context, which
5024   changes how we deal with integer constant expressions.  */
5025
5026static tree
5027cp_parser_postfix_open_square_expression (cp_parser *parser,
5028					  tree postfix_expression,
5029					  bool for_offsetof)
5030{
5031  tree index;
5032
5033  /* Consume the `[' token.  */
5034  cp_lexer_consume_token (parser->lexer);
5035
5036  /* Parse the index expression.  */
5037  /* ??? For offsetof, there is a question of what to allow here.  If
5038     offsetof is not being used in an integral constant expression context,
5039     then we *could* get the right answer by computing the value at runtime.
5040     If we are in an integral constant expression context, then we might
5041     could accept any constant expression; hard to say without analysis.
5042     Rather than open the barn door too wide right away, allow only integer
5043     constant expressions here.  */
5044  if (for_offsetof)
5045    index = cp_parser_constant_expression (parser, false, NULL);
5046  else
5047    index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5048
5049  /* Look for the closing `]'.  */
5050  cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5051
5052  /* Build the ARRAY_REF.  */
5053  postfix_expression = grok_array_decl (postfix_expression, index);
5054
5055  /* When not doing offsetof, array references are not permitted in
5056     constant-expressions.  */
5057  if (!for_offsetof
5058      && (cp_parser_non_integral_constant_expression
5059	  (parser, "an array reference")))
5060    postfix_expression = error_mark_node;
5061
5062  return postfix_expression;
5063}
5064
5065/* A subroutine of cp_parser_postfix_expression that also gets hijacked
5066   by cp_parser_builtin_offsetof.  We're looking for
5067
5068     postfix-expression . template [opt] id-expression
5069     postfix-expression . pseudo-destructor-name
5070     postfix-expression -> template [opt] id-expression
5071     postfix-expression -> pseudo-destructor-name
5072
5073   FOR_OFFSETOF is set if we're being called in that context.  That sorta
5074   limits what of the above we'll actually accept, but nevermind.
5075   TOKEN_TYPE is the "." or "->" token, which will already have been
5076   removed from the stream.  */
5077
5078static tree
5079cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5080					enum cpp_ttype token_type,
5081					tree postfix_expression,
5082					bool for_offsetof, cp_id_kind *idk,
5083					location_t location)
5084{
5085  tree name;
5086  bool dependent_p;
5087  bool pseudo_destructor_p;
5088  tree scope = NULL_TREE;
5089
5090  /* If this is a `->' operator, dereference the pointer.  */
5091  if (token_type == CPP_DEREF)
5092    postfix_expression = build_x_arrow (postfix_expression);
5093  /* Check to see whether or not the expression is type-dependent.  */
5094  dependent_p = type_dependent_expression_p (postfix_expression);
5095  /* The identifier following the `->' or `.' is not qualified.  */
5096  parser->scope = NULL_TREE;
5097  parser->qualifying_scope = NULL_TREE;
5098  parser->object_scope = NULL_TREE;
5099  *idk = CP_ID_KIND_NONE;
5100
5101  /* Enter the scope corresponding to the type of the object
5102     given by the POSTFIX_EXPRESSION.  */
5103  if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5104    {
5105      scope = TREE_TYPE (postfix_expression);
5106      /* According to the standard, no expression should ever have
5107	 reference type.  Unfortunately, we do not currently match
5108	 the standard in this respect in that our internal representation
5109	 of an expression may have reference type even when the standard
5110	 says it does not.  Therefore, we have to manually obtain the
5111	 underlying type here.  */
5112      scope = non_reference (scope);
5113      /* The type of the POSTFIX_EXPRESSION must be complete.  */
5114      if (scope == unknown_type_node)
5115	{
5116	  error_at (location, "%qE does not have class type",
5117		    postfix_expression);
5118	  scope = NULL_TREE;
5119	}
5120      else
5121	scope = complete_type_or_else (scope, NULL_TREE);
5122      /* Let the name lookup machinery know that we are processing a
5123	 class member access expression.  */
5124      parser->context->object_type = scope;
5125      /* If something went wrong, we want to be able to discern that case,
5126	 as opposed to the case where there was no SCOPE due to the type
5127	 of expression being dependent.  */
5128      if (!scope)
5129	scope = error_mark_node;
5130      /* If the SCOPE was erroneous, make the various semantic analysis
5131	 functions exit quickly -- and without issuing additional error
5132	 messages.  */
5133      if (scope == error_mark_node)
5134	postfix_expression = error_mark_node;
5135    }
5136
5137  /* Assume this expression is not a pseudo-destructor access.  */
5138  pseudo_destructor_p = false;
5139
5140  /* If the SCOPE is a scalar type, then, if this is a valid program,
5141     we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5142     is type dependent, it can be pseudo-destructor-name or something else.
5143     Try to parse it as pseudo-destructor-name first.  */
5144  if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5145    {
5146      tree s;
5147      tree type;
5148
5149      cp_parser_parse_tentatively (parser);
5150      /* Parse the pseudo-destructor-name.  */
5151      s = NULL_TREE;
5152      cp_parser_pseudo_destructor_name (parser, &s, &type);
5153      if (dependent_p
5154	  && (cp_parser_error_occurred (parser)
5155	      || TREE_CODE (type) != TYPE_DECL
5156	      || !SCALAR_TYPE_P (TREE_TYPE (type))))
5157	cp_parser_abort_tentative_parse (parser);
5158      else if (cp_parser_parse_definitely (parser))
5159	{
5160	  pseudo_destructor_p = true;
5161	  postfix_expression
5162	    = finish_pseudo_destructor_expr (postfix_expression,
5163					     s, TREE_TYPE (type));
5164	}
5165    }
5166
5167  if (!pseudo_destructor_p)
5168    {
5169      /* If the SCOPE is not a scalar type, we are looking at an
5170	 ordinary class member access expression, rather than a
5171	 pseudo-destructor-name.  */
5172      bool template_p;
5173      cp_token *token = cp_lexer_peek_token (parser->lexer);
5174      /* Parse the id-expression.  */
5175      name = (cp_parser_id_expression
5176	      (parser,
5177	       cp_parser_optional_template_keyword (parser),
5178	       /*check_dependency_p=*/true,
5179	       &template_p,
5180	       /*declarator_p=*/false,
5181	       /*optional_p=*/false));
5182      /* In general, build a SCOPE_REF if the member name is qualified.
5183	 However, if the name was not dependent and has already been
5184	 resolved; there is no need to build the SCOPE_REF.  For example;
5185
5186	     struct X { void f(); };
5187	     template <typename T> void f(T* t) { t->X::f(); }
5188
5189	 Even though "t" is dependent, "X::f" is not and has been resolved
5190	 to a BASELINK; there is no need to include scope information.  */
5191
5192      /* But we do need to remember that there was an explicit scope for
5193	 virtual function calls.  */
5194      if (parser->scope)
5195	*idk = CP_ID_KIND_QUALIFIED;
5196
5197      /* If the name is a template-id that names a type, we will get a
5198	 TYPE_DECL here.  That is invalid code.  */
5199      if (TREE_CODE (name) == TYPE_DECL)
5200	{
5201	  error_at (token->location, "invalid use of %qD", name);
5202	  postfix_expression = error_mark_node;
5203	}
5204      else
5205	{
5206	  if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5207	    {
5208	      name = build_qualified_name (/*type=*/NULL_TREE,
5209					   parser->scope,
5210					   name,
5211					   template_p);
5212	      parser->scope = NULL_TREE;
5213	      parser->qualifying_scope = NULL_TREE;
5214	      parser->object_scope = NULL_TREE;
5215	    }
5216	  if (scope && name && BASELINK_P (name))
5217	    adjust_result_of_qualified_name_lookup
5218	      (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5219	  postfix_expression
5220	    = finish_class_member_access_expr (postfix_expression, name,
5221					       template_p,
5222					       tf_warning_or_error);
5223	}
5224    }
5225
5226  /* We no longer need to look up names in the scope of the object on
5227     the left-hand side of the `.' or `->' operator.  */
5228  parser->context->object_type = NULL_TREE;
5229
5230  /* Outside of offsetof, these operators may not appear in
5231     constant-expressions.  */
5232  if (!for_offsetof
5233      && (cp_parser_non_integral_constant_expression
5234	  (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5235    postfix_expression = error_mark_node;
5236
5237  return postfix_expression;
5238}
5239
5240/* Parse a parenthesized expression-list.
5241
5242   expression-list:
5243     assignment-expression
5244     expression-list, assignment-expression
5245
5246   attribute-list:
5247     expression-list
5248     identifier
5249     identifier, expression-list
5250
5251   CAST_P is true if this expression is the target of a cast.
5252
5253   ALLOW_EXPANSION_P is true if this expression allows expansion of an
5254   argument pack.
5255
5256   Returns a vector of trees.  Each element is a representation of an
5257   assignment-expression.  NULL is returned if the ( and or ) are
5258   missing.  An empty, but allocated, vector is returned on no
5259   expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5260   if this is really an attribute list being parsed.  If
5261   NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5262   not all of the expressions in the list were constant.  */
5263
5264static VEC(tree,gc) *
5265cp_parser_parenthesized_expression_list (cp_parser* parser,
5266					 bool is_attribute_list,
5267					 bool cast_p,
5268                                         bool allow_expansion_p,
5269					 bool *non_constant_p)
5270{
5271  VEC(tree,gc) *expression_list;
5272  bool fold_expr_p = is_attribute_list;
5273  tree identifier = NULL_TREE;
5274  bool saved_greater_than_is_operator_p;
5275
5276  /* Assume all the expressions will be constant.  */
5277  if (non_constant_p)
5278    *non_constant_p = false;
5279
5280  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5281    return NULL;
5282
5283  expression_list = make_tree_vector ();
5284
5285  /* Within a parenthesized expression, a `>' token is always
5286     the greater-than operator.  */
5287  saved_greater_than_is_operator_p
5288    = parser->greater_than_is_operator_p;
5289  parser->greater_than_is_operator_p = true;
5290
5291  /* Consume expressions until there are no more.  */
5292  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5293    while (true)
5294      {
5295	tree expr;
5296
5297	/* At the beginning of attribute lists, check to see if the
5298	   next token is an identifier.  */
5299	if (is_attribute_list
5300	    && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5301	  {
5302	    cp_token *token;
5303
5304	    /* Consume the identifier.  */
5305	    token = cp_lexer_consume_token (parser->lexer);
5306	    /* Save the identifier.  */
5307	    identifier = token->u.value;
5308	  }
5309	else
5310	  {
5311	    bool expr_non_constant_p;
5312
5313	    /* Parse the next assignment-expression.  */
5314	    if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5315	      {
5316		/* A braced-init-list.  */
5317		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5318		expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5319		if (non_constant_p && expr_non_constant_p)
5320		  *non_constant_p = true;
5321	      }
5322	    else if (non_constant_p)
5323	      {
5324		expr = (cp_parser_constant_expression
5325			(parser, /*allow_non_constant_p=*/true,
5326			 &expr_non_constant_p));
5327		if (expr_non_constant_p)
5328		  *non_constant_p = true;
5329	      }
5330	    else
5331	      expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5332
5333	    if (fold_expr_p)
5334	      expr = fold_non_dependent_expr (expr);
5335
5336            /* If we have an ellipsis, then this is an expression
5337	       expansion.  */
5338            if (allow_expansion_p
5339                && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5340              {
5341                /* Consume the `...'.  */
5342                cp_lexer_consume_token (parser->lexer);
5343
5344                /* Build the argument pack.  */
5345                expr = make_pack_expansion (expr);
5346              }
5347
5348	     /* Add it to the list.  We add error_mark_node
5349		expressions to the list, so that we can still tell if
5350		the correct form for a parenthesized expression-list
5351		is found. That gives better errors.  */
5352	    VEC_safe_push (tree, gc, expression_list, expr);
5353
5354	    if (expr == error_mark_node)
5355	      goto skip_comma;
5356	  }
5357
5358	/* After the first item, attribute lists look the same as
5359	   expression lists.  */
5360	is_attribute_list = false;
5361
5362      get_comma:;
5363	/* If the next token isn't a `,', then we are done.  */
5364	if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5365	  break;
5366
5367	/* Otherwise, consume the `,' and keep going.  */
5368	cp_lexer_consume_token (parser->lexer);
5369      }
5370
5371  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5372    {
5373      int ending;
5374
5375    skip_comma:;
5376      /* We try and resync to an unnested comma, as that will give the
5377	 user better diagnostics.  */
5378      ending = cp_parser_skip_to_closing_parenthesis (parser,
5379						      /*recovering=*/true,
5380						      /*or_comma=*/true,
5381						      /*consume_paren=*/true);
5382      if (ending < 0)
5383	goto get_comma;
5384      if (!ending)
5385	{
5386	  parser->greater_than_is_operator_p
5387	    = saved_greater_than_is_operator_p;
5388	  return NULL;
5389	}
5390    }
5391
5392  parser->greater_than_is_operator_p
5393    = saved_greater_than_is_operator_p;
5394
5395  if (identifier)
5396    VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5397
5398  return expression_list;
5399}
5400
5401/* Parse a pseudo-destructor-name.
5402
5403   pseudo-destructor-name:
5404     :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5405     :: [opt] nested-name-specifier template template-id :: ~ type-name
5406     :: [opt] nested-name-specifier [opt] ~ type-name
5407
5408   If either of the first two productions is used, sets *SCOPE to the
5409   TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5410   NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5411   or ERROR_MARK_NODE if the parse fails.  */
5412
5413static void
5414cp_parser_pseudo_destructor_name (cp_parser* parser,
5415				  tree* scope,
5416				  tree* type)
5417{
5418  bool nested_name_specifier_p;
5419
5420  /* Assume that things will not work out.  */
5421  *type = error_mark_node;
5422
5423  /* Look for the optional `::' operator.  */
5424  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5425  /* Look for the optional nested-name-specifier.  */
5426  nested_name_specifier_p
5427    = (cp_parser_nested_name_specifier_opt (parser,
5428					    /*typename_keyword_p=*/false,
5429					    /*check_dependency_p=*/true,
5430					    /*type_p=*/false,
5431					    /*is_declaration=*/false)
5432       != NULL_TREE);
5433  /* Now, if we saw a nested-name-specifier, we might be doing the
5434     second production.  */
5435  if (nested_name_specifier_p
5436      && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5437    {
5438      /* Consume the `template' keyword.  */
5439      cp_lexer_consume_token (parser->lexer);
5440      /* Parse the template-id.  */
5441      cp_parser_template_id (parser,
5442			     /*template_keyword_p=*/true,
5443			     /*check_dependency_p=*/false,
5444			     /*is_declaration=*/true);
5445      /* Look for the `::' token.  */
5446      cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5447    }
5448  /* If the next token is not a `~', then there might be some
5449     additional qualification.  */
5450  else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5451    {
5452      /* At this point, we're looking for "type-name :: ~".  The type-name
5453	 must not be a class-name, since this is a pseudo-destructor.  So,
5454	 it must be either an enum-name, or a typedef-name -- both of which
5455	 are just identifiers.  So, we peek ahead to check that the "::"
5456	 and "~" tokens are present; if they are not, then we can avoid
5457	 calling type_name.  */
5458      if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5459	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5460	  || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5461	{
5462	  cp_parser_error (parser, "non-scalar type");
5463	  return;
5464	}
5465
5466      /* Look for the type-name.  */
5467      *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5468      if (*scope == error_mark_node)
5469	return;
5470
5471      /* Look for the `::' token.  */
5472      cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5473    }
5474  else
5475    *scope = NULL_TREE;
5476
5477  /* Look for the `~'.  */
5478  cp_parser_require (parser, CPP_COMPL, "%<~%>");
5479  /* Look for the type-name again.  We are not responsible for
5480     checking that it matches the first type-name.  */
5481  *type = cp_parser_nonclass_name (parser);
5482}
5483
5484/* Parse a unary-expression.
5485
5486   unary-expression:
5487     postfix-expression
5488     ++ cast-expression
5489     -- cast-expression
5490     unary-operator cast-expression
5491     sizeof unary-expression
5492     sizeof ( type-id )
5493     new-expression
5494     delete-expression
5495
5496   GNU Extensions:
5497
5498   unary-expression:
5499     __extension__ cast-expression
5500     __alignof__ unary-expression
5501     __alignof__ ( type-id )
5502     __real__ cast-expression
5503     __imag__ cast-expression
5504     && identifier
5505
5506   ADDRESS_P is true iff the unary-expression is appearing as the
5507   operand of the `&' operator.   CAST_P is true if this expression is
5508   the target of a cast.
5509
5510   Returns a representation of the expression.  */
5511
5512static tree
5513cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5514			    cp_id_kind * pidk)
5515{
5516  cp_token *token;
5517  enum tree_code unary_operator;
5518
5519  /* Peek at the next token.  */
5520  token = cp_lexer_peek_token (parser->lexer);
5521  /* Some keywords give away the kind of expression.  */
5522  if (token->type == CPP_KEYWORD)
5523    {
5524      enum rid keyword = token->keyword;
5525
5526      switch (keyword)
5527	{
5528	case RID_ALIGNOF:
5529	case RID_SIZEOF:
5530	  {
5531	    tree operand;
5532	    enum tree_code op;
5533
5534	    op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5535	    /* Consume the token.  */
5536	    cp_lexer_consume_token (parser->lexer);
5537	    /* Parse the operand.  */
5538	    operand = cp_parser_sizeof_operand (parser, keyword);
5539
5540	    if (TYPE_P (operand))
5541	      return cxx_sizeof_or_alignof_type (operand, op, true);
5542	    else
5543	      return cxx_sizeof_or_alignof_expr (operand, op, true);
5544	  }
5545
5546	case RID_NEW:
5547	  return cp_parser_new_expression (parser);
5548
5549	case RID_DELETE:
5550	  return cp_parser_delete_expression (parser);
5551
5552	case RID_EXTENSION:
5553	  {
5554	    /* The saved value of the PEDANTIC flag.  */
5555	    int saved_pedantic;
5556	    tree expr;
5557
5558	    /* Save away the PEDANTIC flag.  */
5559	    cp_parser_extension_opt (parser, &saved_pedantic);
5560	    /* Parse the cast-expression.  */
5561	    expr = cp_parser_simple_cast_expression (parser);
5562	    /* Restore the PEDANTIC flag.  */
5563	    pedantic = saved_pedantic;
5564
5565	    return expr;
5566	  }
5567
5568	case RID_REALPART:
5569	case RID_IMAGPART:
5570	  {
5571	    tree expression;
5572
5573	    /* Consume the `__real__' or `__imag__' token.  */
5574	    cp_lexer_consume_token (parser->lexer);
5575	    /* Parse the cast-expression.  */
5576	    expression = cp_parser_simple_cast_expression (parser);
5577	    /* Create the complete representation.  */
5578	    return build_x_unary_op ((keyword == RID_REALPART
5579				      ? REALPART_EXPR : IMAGPART_EXPR),
5580				     expression,
5581                                     tf_warning_or_error);
5582	  }
5583	  break;
5584
5585	default:
5586	  break;
5587	}
5588    }
5589
5590  /* Look for the `:: new' and `:: delete', which also signal the
5591     beginning of a new-expression, or delete-expression,
5592     respectively.  If the next token is `::', then it might be one of
5593     these.  */
5594  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5595    {
5596      enum rid keyword;
5597
5598      /* See if the token after the `::' is one of the keywords in
5599	 which we're interested.  */
5600      keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5601      /* If it's `new', we have a new-expression.  */
5602      if (keyword == RID_NEW)
5603	return cp_parser_new_expression (parser);
5604      /* Similarly, for `delete'.  */
5605      else if (keyword == RID_DELETE)
5606	return cp_parser_delete_expression (parser);
5607    }
5608
5609  /* Look for a unary operator.  */
5610  unary_operator = cp_parser_unary_operator (token);
5611  /* The `++' and `--' operators can be handled similarly, even though
5612     they are not technically unary-operators in the grammar.  */
5613  if (unary_operator == ERROR_MARK)
5614    {
5615      if (token->type == CPP_PLUS_PLUS)
5616	unary_operator = PREINCREMENT_EXPR;
5617      else if (token->type == CPP_MINUS_MINUS)
5618	unary_operator = PREDECREMENT_EXPR;
5619      /* Handle the GNU address-of-label extension.  */
5620      else if (cp_parser_allow_gnu_extensions_p (parser)
5621	       && token->type == CPP_AND_AND)
5622	{
5623	  tree identifier;
5624	  tree expression;
5625	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5626
5627	  /* Consume the '&&' token.  */
5628	  cp_lexer_consume_token (parser->lexer);
5629	  /* Look for the identifier.  */
5630	  identifier = cp_parser_identifier (parser);
5631	  /* Create an expression representing the address.  */
5632	  expression = finish_label_address_expr (identifier, loc);
5633	  if (cp_parser_non_integral_constant_expression (parser,
5634						"the address of a label"))
5635	    expression = error_mark_node;
5636	  return expression;
5637	}
5638    }
5639  if (unary_operator != ERROR_MARK)
5640    {
5641      tree cast_expression;
5642      tree expression = error_mark_node;
5643      const char *non_constant_p = NULL;
5644
5645      /* Consume the operator token.  */
5646      token = cp_lexer_consume_token (parser->lexer);
5647      /* Parse the cast-expression.  */
5648      cast_expression
5649	= cp_parser_cast_expression (parser,
5650				     unary_operator == ADDR_EXPR,
5651				     /*cast_p=*/false, pidk);
5652      /* Now, build an appropriate representation.  */
5653      switch (unary_operator)
5654	{
5655	case INDIRECT_REF:
5656	  non_constant_p = "%<*%>";
5657	  expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5658                                             tf_warning_or_error);
5659	  break;
5660
5661	case ADDR_EXPR:
5662	  non_constant_p = "%<&%>";
5663	  /* Fall through.  */
5664	case BIT_NOT_EXPR:
5665	  expression = build_x_unary_op (unary_operator, cast_expression,
5666                                         tf_warning_or_error);
5667	  break;
5668
5669	case PREINCREMENT_EXPR:
5670	case PREDECREMENT_EXPR:
5671	  non_constant_p = (unary_operator == PREINCREMENT_EXPR
5672			    ? "%<++%>" : "%<--%>");
5673	  /* Fall through.  */
5674	case UNARY_PLUS_EXPR:
5675	case NEGATE_EXPR:
5676	case TRUTH_NOT_EXPR:
5677	  expression = finish_unary_op_expr (unary_operator, cast_expression);
5678	  break;
5679
5680	default:
5681	  gcc_unreachable ();
5682	}
5683
5684      if (non_constant_p
5685	  && cp_parser_non_integral_constant_expression (parser,
5686							 non_constant_p))
5687	expression = error_mark_node;
5688
5689      return expression;
5690    }
5691
5692  return cp_parser_postfix_expression (parser, address_p, cast_p,
5693                                       /*member_access_only_p=*/false,
5694				       pidk);
5695}
5696
5697/* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5698   unary-operator, the corresponding tree code is returned.  */
5699
5700static enum tree_code
5701cp_parser_unary_operator (cp_token* token)
5702{
5703  switch (token->type)
5704    {
5705    case CPP_MULT:
5706      return INDIRECT_REF;
5707
5708    case CPP_AND:
5709      return ADDR_EXPR;
5710
5711    case CPP_PLUS:
5712      return UNARY_PLUS_EXPR;
5713
5714    case CPP_MINUS:
5715      return NEGATE_EXPR;
5716
5717    case CPP_NOT:
5718      return TRUTH_NOT_EXPR;
5719
5720    case CPP_COMPL:
5721      return BIT_NOT_EXPR;
5722
5723    default:
5724      return ERROR_MARK;
5725    }
5726}
5727
5728/* Parse a new-expression.
5729
5730   new-expression:
5731     :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5732     :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5733
5734   Returns a representation of the expression.  */
5735
5736static tree
5737cp_parser_new_expression (cp_parser* parser)
5738{
5739  bool global_scope_p;
5740  VEC(tree,gc) *placement;
5741  tree type;
5742  VEC(tree,gc) *initializer;
5743  tree nelts;
5744  tree ret;
5745
5746  /* Look for the optional `::' operator.  */
5747  global_scope_p
5748    = (cp_parser_global_scope_opt (parser,
5749				   /*current_scope_valid_p=*/false)
5750       != NULL_TREE);
5751  /* Look for the `new' operator.  */
5752  cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5753  /* There's no easy way to tell a new-placement from the
5754     `( type-id )' construct.  */
5755  cp_parser_parse_tentatively (parser);
5756  /* Look for a new-placement.  */
5757  placement = cp_parser_new_placement (parser);
5758  /* If that didn't work out, there's no new-placement.  */
5759  if (!cp_parser_parse_definitely (parser))
5760    {
5761      if (placement != NULL)
5762	release_tree_vector (placement);
5763      placement = NULL;
5764    }
5765
5766  /* If the next token is a `(', then we have a parenthesized
5767     type-id.  */
5768  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5769    {
5770      cp_token *token;
5771      /* Consume the `('.  */
5772      cp_lexer_consume_token (parser->lexer);
5773      /* Parse the type-id.  */
5774      type = cp_parser_type_id (parser);
5775      /* Look for the closing `)'.  */
5776      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5777      token = cp_lexer_peek_token (parser->lexer);
5778      /* There should not be a direct-new-declarator in this production,
5779	 but GCC used to allowed this, so we check and emit a sensible error
5780	 message for this case.  */
5781      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5782	{
5783	  error_at (token->location,
5784		    "array bound forbidden after parenthesized type-id");
5785	  inform (token->location,
5786		  "try removing the parentheses around the type-id");
5787	  cp_parser_direct_new_declarator (parser);
5788	}
5789      nelts = NULL_TREE;
5790    }
5791  /* Otherwise, there must be a new-type-id.  */
5792  else
5793    type = cp_parser_new_type_id (parser, &nelts);
5794
5795  /* If the next token is a `(' or '{', then we have a new-initializer.  */
5796  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5797      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5798    initializer = cp_parser_new_initializer (parser);
5799  else
5800    initializer = NULL;
5801
5802  /* A new-expression may not appear in an integral constant
5803     expression.  */
5804  if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5805    ret = error_mark_node;
5806  else
5807    {
5808      /* Create a representation of the new-expression.  */
5809      ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5810		       tf_warning_or_error);
5811    }
5812
5813  if (placement != NULL)
5814    release_tree_vector (placement);
5815  if (initializer != NULL)
5816    release_tree_vector (initializer);
5817
5818  return ret;
5819}
5820
5821/* Parse a new-placement.
5822
5823   new-placement:
5824     ( expression-list )
5825
5826   Returns the same representation as for an expression-list.  */
5827
5828static VEC(tree,gc) *
5829cp_parser_new_placement (cp_parser* parser)
5830{
5831  VEC(tree,gc) *expression_list;
5832
5833  /* Parse the expression-list.  */
5834  expression_list = (cp_parser_parenthesized_expression_list
5835		     (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5836		      /*non_constant_p=*/NULL));
5837
5838  return expression_list;
5839}
5840
5841/* Parse a new-type-id.
5842
5843   new-type-id:
5844     type-specifier-seq new-declarator [opt]
5845
5846   Returns the TYPE allocated.  If the new-type-id indicates an array
5847   type, *NELTS is set to the number of elements in the last array
5848   bound; the TYPE will not include the last array bound.  */
5849
5850static tree
5851cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5852{
5853  cp_decl_specifier_seq type_specifier_seq;
5854  cp_declarator *new_declarator;
5855  cp_declarator *declarator;
5856  cp_declarator *outer_declarator;
5857  const char *saved_message;
5858  tree type;
5859
5860  /* The type-specifier sequence must not contain type definitions.
5861     (It cannot contain declarations of new types either, but if they
5862     are not definitions we will catch that because they are not
5863     complete.)  */
5864  saved_message = parser->type_definition_forbidden_message;
5865  parser->type_definition_forbidden_message
5866    = G_("types may not be defined in a new-type-id");
5867  /* Parse the type-specifier-seq.  */
5868  cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5869				/*is_trailing_return=*/false,
5870				&type_specifier_seq);
5871  /* Restore the old message.  */
5872  parser->type_definition_forbidden_message = saved_message;
5873  /* Parse the new-declarator.  */
5874  new_declarator = cp_parser_new_declarator_opt (parser);
5875
5876  /* Determine the number of elements in the last array dimension, if
5877     any.  */
5878  *nelts = NULL_TREE;
5879  /* Skip down to the last array dimension.  */
5880  declarator = new_declarator;
5881  outer_declarator = NULL;
5882  while (declarator && (declarator->kind == cdk_pointer
5883			|| declarator->kind == cdk_ptrmem))
5884    {
5885      outer_declarator = declarator;
5886      declarator = declarator->declarator;
5887    }
5888  while (declarator
5889	 && declarator->kind == cdk_array
5890	 && declarator->declarator
5891	 && declarator->declarator->kind == cdk_array)
5892    {
5893      outer_declarator = declarator;
5894      declarator = declarator->declarator;
5895    }
5896
5897  if (declarator && declarator->kind == cdk_array)
5898    {
5899      *nelts = declarator->u.array.bounds;
5900      if (*nelts == error_mark_node)
5901	*nelts = integer_one_node;
5902
5903      if (outer_declarator)
5904	outer_declarator->declarator = declarator->declarator;
5905      else
5906	new_declarator = NULL;
5907    }
5908
5909  type = groktypename (&type_specifier_seq, new_declarator, false);
5910  return type;
5911}
5912
5913/* Parse an (optional) new-declarator.
5914
5915   new-declarator:
5916     ptr-operator new-declarator [opt]
5917     direct-new-declarator
5918
5919   Returns the declarator.  */
5920
5921static cp_declarator *
5922cp_parser_new_declarator_opt (cp_parser* parser)
5923{
5924  enum tree_code code;
5925  tree type;
5926  cp_cv_quals cv_quals;
5927
5928  /* We don't know if there's a ptr-operator next, or not.  */
5929  cp_parser_parse_tentatively (parser);
5930  /* Look for a ptr-operator.  */
5931  code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5932  /* If that worked, look for more new-declarators.  */
5933  if (cp_parser_parse_definitely (parser))
5934    {
5935      cp_declarator *declarator;
5936
5937      /* Parse another optional declarator.  */
5938      declarator = cp_parser_new_declarator_opt (parser);
5939
5940      return cp_parser_make_indirect_declarator
5941	(code, type, cv_quals, declarator);
5942    }
5943
5944  /* If the next token is a `[', there is a direct-new-declarator.  */
5945  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5946    return cp_parser_direct_new_declarator (parser);
5947
5948  return NULL;
5949}
5950
5951/* Parse a direct-new-declarator.
5952
5953   direct-new-declarator:
5954     [ expression ]
5955     direct-new-declarator [constant-expression]
5956
5957   */
5958
5959static cp_declarator *
5960cp_parser_direct_new_declarator (cp_parser* parser)
5961{
5962  cp_declarator *declarator = NULL;
5963
5964  while (true)
5965    {
5966      tree expression;
5967
5968      /* Look for the opening `['.  */
5969      cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5970      /* The first expression is not required to be constant.  */
5971      if (!declarator)
5972	{
5973	  cp_token *token = cp_lexer_peek_token (parser->lexer);
5974	  expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5975	  /* The standard requires that the expression have integral
5976	     type.  DR 74 adds enumeration types.  We believe that the
5977	     real intent is that these expressions be handled like the
5978	     expression in a `switch' condition, which also allows
5979	     classes with a single conversion to integral or
5980	     enumeration type.  */
5981	  if (!processing_template_decl)
5982	    {
5983	      expression
5984		= build_expr_type_conversion (WANT_INT | WANT_ENUM,
5985					      expression,
5986					      /*complain=*/true);
5987	      if (!expression)
5988		{
5989		  error_at (token->location,
5990			    "expression in new-declarator must have integral "
5991			    "or enumeration type");
5992		  expression = error_mark_node;
5993		}
5994	    }
5995	}
5996      /* But all the other expressions must be.  */
5997      else
5998	expression
5999	  = cp_parser_constant_expression (parser,
6000					   /*allow_non_constant=*/false,
6001					   NULL);
6002      /* Look for the closing `]'.  */
6003      cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6004
6005      /* Add this bound to the declarator.  */
6006      declarator = make_array_declarator (declarator, expression);
6007
6008      /* If the next token is not a `[', then there are no more
6009	 bounds.  */
6010      if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6011	break;
6012    }
6013
6014  return declarator;
6015}
6016
6017/* Parse a new-initializer.
6018
6019   new-initializer:
6020     ( expression-list [opt] )
6021     braced-init-list
6022
6023   Returns a representation of the expression-list.  */
6024
6025static VEC(tree,gc) *
6026cp_parser_new_initializer (cp_parser* parser)
6027{
6028  VEC(tree,gc) *expression_list;
6029
6030  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6031    {
6032      tree t;
6033      bool expr_non_constant_p;
6034      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6035      t = cp_parser_braced_list (parser, &expr_non_constant_p);
6036      CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6037      expression_list = make_tree_vector_single (t);
6038    }
6039  else
6040    expression_list = (cp_parser_parenthesized_expression_list
6041		       (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6042			/*non_constant_p=*/NULL));
6043
6044  return expression_list;
6045}
6046
6047/* Parse a delete-expression.
6048
6049   delete-expression:
6050     :: [opt] delete cast-expression
6051     :: [opt] delete [ ] cast-expression
6052
6053   Returns a representation of the expression.  */
6054
6055static tree
6056cp_parser_delete_expression (cp_parser* parser)
6057{
6058  bool global_scope_p;
6059  bool array_p;
6060  tree expression;
6061
6062  /* Look for the optional `::' operator.  */
6063  global_scope_p
6064    = (cp_parser_global_scope_opt (parser,
6065				   /*current_scope_valid_p=*/false)
6066       != NULL_TREE);
6067  /* Look for the `delete' keyword.  */
6068  cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6069  /* See if the array syntax is in use.  */
6070  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6071    {
6072      /* Consume the `[' token.  */
6073      cp_lexer_consume_token (parser->lexer);
6074      /* Look for the `]' token.  */
6075      cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6076      /* Remember that this is the `[]' construct.  */
6077      array_p = true;
6078    }
6079  else
6080    array_p = false;
6081
6082  /* Parse the cast-expression.  */
6083  expression = cp_parser_simple_cast_expression (parser);
6084
6085  /* A delete-expression may not appear in an integral constant
6086     expression.  */
6087  if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6088    return error_mark_node;
6089
6090  return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6091}
6092
6093/* Returns true if TOKEN may start a cast-expression and false
6094   otherwise.  */
6095
6096static bool
6097cp_parser_token_starts_cast_expression (cp_token *token)
6098{
6099  switch (token->type)
6100    {
6101    case CPP_COMMA:
6102    case CPP_SEMICOLON:
6103    case CPP_QUERY:
6104    case CPP_COLON:
6105    case CPP_CLOSE_SQUARE:
6106    case CPP_CLOSE_PAREN:
6107    case CPP_CLOSE_BRACE:
6108    case CPP_DOT:
6109    case CPP_DOT_STAR:
6110    case CPP_DEREF:
6111    case CPP_DEREF_STAR:
6112    case CPP_DIV:
6113    case CPP_MOD:
6114    case CPP_LSHIFT:
6115    case CPP_RSHIFT:
6116    case CPP_LESS:
6117    case CPP_GREATER:
6118    case CPP_LESS_EQ:
6119    case CPP_GREATER_EQ:
6120    case CPP_EQ_EQ:
6121    case CPP_NOT_EQ:
6122    case CPP_EQ:
6123    case CPP_MULT_EQ:
6124    case CPP_DIV_EQ:
6125    case CPP_MOD_EQ:
6126    case CPP_PLUS_EQ:
6127    case CPP_MINUS_EQ:
6128    case CPP_RSHIFT_EQ:
6129    case CPP_LSHIFT_EQ:
6130    case CPP_AND_EQ:
6131    case CPP_XOR_EQ:
6132    case CPP_OR_EQ:
6133    case CPP_XOR:
6134    case CPP_OR:
6135    case CPP_OR_OR:
6136    case CPP_EOF:
6137      return false;
6138
6139      /* '[' may start a primary-expression in obj-c++.  */
6140    case CPP_OPEN_SQUARE:
6141      return c_dialect_objc ();
6142
6143    default:
6144      return true;
6145    }
6146}
6147
6148/* Parse a cast-expression.
6149
6150   cast-expression:
6151     unary-expression
6152     ( type-id ) cast-expression
6153
6154   ADDRESS_P is true iff the unary-expression is appearing as the
6155   operand of the `&' operator.   CAST_P is true if this expression is
6156   the target of a cast.
6157
6158   Returns a representation of the expression.  */
6159
6160static tree
6161cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6162			   cp_id_kind * pidk)
6163{
6164  /* If it's a `(', then we might be looking at a cast.  */
6165  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6166    {
6167      tree type = NULL_TREE;
6168      tree expr = NULL_TREE;
6169      bool compound_literal_p;
6170      const char *saved_message;
6171
6172      /* There's no way to know yet whether or not this is a cast.
6173	 For example, `(int (3))' is a unary-expression, while `(int)
6174	 3' is a cast.  So, we resort to parsing tentatively.  */
6175      cp_parser_parse_tentatively (parser);
6176      /* Types may not be defined in a cast.  */
6177      saved_message = parser->type_definition_forbidden_message;
6178      parser->type_definition_forbidden_message
6179	= G_("types may not be defined in casts");
6180      /* Consume the `('.  */
6181      cp_lexer_consume_token (parser->lexer);
6182      /* A very tricky bit is that `(struct S) { 3 }' is a
6183	 compound-literal (which we permit in C++ as an extension).
6184	 But, that construct is not a cast-expression -- it is a
6185	 postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6186	 is legal; if the compound-literal were a cast-expression,
6187	 you'd need an extra set of parentheses.)  But, if we parse
6188	 the type-id, and it happens to be a class-specifier, then we
6189	 will commit to the parse at that point, because we cannot
6190	 undo the action that is done when creating a new class.  So,
6191	 then we cannot back up and do a postfix-expression.
6192
6193	 Therefore, we scan ahead to the closing `)', and check to see
6194	 if the token after the `)' is a `{'.  If so, we are not
6195	 looking at a cast-expression.
6196
6197	 Save tokens so that we can put them back.  */
6198      cp_lexer_save_tokens (parser->lexer);
6199      /* Skip tokens until the next token is a closing parenthesis.
6200	 If we find the closing `)', and the next token is a `{', then
6201	 we are looking at a compound-literal.  */
6202      compound_literal_p
6203	= (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6204						  /*consume_paren=*/true)
6205	   && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6206      /* Roll back the tokens we skipped.  */
6207      cp_lexer_rollback_tokens (parser->lexer);
6208      /* If we were looking at a compound-literal, simulate an error
6209	 so that the call to cp_parser_parse_definitely below will
6210	 fail.  */
6211      if (compound_literal_p)
6212	cp_parser_simulate_error (parser);
6213      else
6214	{
6215	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6216	  parser->in_type_id_in_expr_p = true;
6217	  /* Look for the type-id.  */
6218	  type = cp_parser_type_id (parser);
6219	  /* Look for the closing `)'.  */
6220	  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6221	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6222	}
6223
6224      /* Restore the saved message.  */
6225      parser->type_definition_forbidden_message = saved_message;
6226
6227      /* At this point this can only be either a cast or a
6228	 parenthesized ctor such as `(T ())' that looks like a cast to
6229	 function returning T.  */
6230      if (!cp_parser_error_occurred (parser)
6231	  && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6232						     (parser->lexer)))
6233	{
6234	  cp_parser_parse_definitely (parser);
6235	  expr = cp_parser_cast_expression (parser,
6236					    /*address_p=*/false,
6237					    /*cast_p=*/true, pidk);
6238
6239	  /* Warn about old-style casts, if so requested.  */
6240	  if (warn_old_style_cast
6241	      && !in_system_header
6242	      && !VOID_TYPE_P (type)
6243	      && current_lang_name != lang_name_c)
6244	    warning (OPT_Wold_style_cast, "use of old-style cast");
6245
6246	  /* Only type conversions to integral or enumeration types
6247	     can be used in constant-expressions.  */
6248	  if (!cast_valid_in_integral_constant_expression_p (type)
6249	      && (cp_parser_non_integral_constant_expression
6250		  (parser,
6251		   "a cast to a type other than an integral or "
6252		   "enumeration type")))
6253	    return error_mark_node;
6254
6255	  /* Perform the cast.  */
6256	  expr = build_c_cast (input_location, type, expr);
6257	  return expr;
6258	}
6259      else
6260        cp_parser_abort_tentative_parse (parser);
6261    }
6262
6263  /* If we get here, then it's not a cast, so it must be a
6264     unary-expression.  */
6265  return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6266}
6267
6268/* Parse a binary expression of the general form:
6269
6270   pm-expression:
6271     cast-expression
6272     pm-expression .* cast-expression
6273     pm-expression ->* cast-expression
6274
6275   multiplicative-expression:
6276     pm-expression
6277     multiplicative-expression * pm-expression
6278     multiplicative-expression / pm-expression
6279     multiplicative-expression % pm-expression
6280
6281   additive-expression:
6282     multiplicative-expression
6283     additive-expression + multiplicative-expression
6284     additive-expression - multiplicative-expression
6285
6286   shift-expression:
6287     additive-expression
6288     shift-expression << additive-expression
6289     shift-expression >> additive-expression
6290
6291   relational-expression:
6292     shift-expression
6293     relational-expression < shift-expression
6294     relational-expression > shift-expression
6295     relational-expression <= shift-expression
6296     relational-expression >= shift-expression
6297
6298  GNU Extension:
6299
6300   relational-expression:
6301     relational-expression <? shift-expression
6302     relational-expression >? shift-expression
6303
6304   equality-expression:
6305     relational-expression
6306     equality-expression == relational-expression
6307     equality-expression != relational-expression
6308
6309   and-expression:
6310     equality-expression
6311     and-expression & equality-expression
6312
6313   exclusive-or-expression:
6314     and-expression
6315     exclusive-or-expression ^ and-expression
6316
6317   inclusive-or-expression:
6318     exclusive-or-expression
6319     inclusive-or-expression | exclusive-or-expression
6320
6321   logical-and-expression:
6322     inclusive-or-expression
6323     logical-and-expression && inclusive-or-expression
6324
6325   logical-or-expression:
6326     logical-and-expression
6327     logical-or-expression || logical-and-expression
6328
6329   All these are implemented with a single function like:
6330
6331   binary-expression:
6332     simple-cast-expression
6333     binary-expression <token> binary-expression
6334
6335   CAST_P is true if this expression is the target of a cast.
6336
6337   The binops_by_token map is used to get the tree codes for each <token> type.
6338   binary-expressions are associated according to a precedence table.  */
6339
6340#define TOKEN_PRECEDENCE(token)				     \
6341(((token->type == CPP_GREATER				     \
6342   || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6343  && !parser->greater_than_is_operator_p)		     \
6344 ? PREC_NOT_OPERATOR					     \
6345 : binops_by_token[token->type].prec)
6346
6347static tree
6348cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6349			     bool no_toplevel_fold_p,
6350			     enum cp_parser_prec prec,
6351			     cp_id_kind * pidk)
6352{
6353  cp_parser_expression_stack stack;
6354  cp_parser_expression_stack_entry *sp = &stack[0];
6355  tree lhs, rhs;
6356  cp_token *token;
6357  enum tree_code tree_type, lhs_type, rhs_type;
6358  enum cp_parser_prec new_prec, lookahead_prec;
6359  bool overloaded_p;
6360
6361  /* Parse the first expression.  */
6362  lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6363  lhs_type = ERROR_MARK;
6364
6365  for (;;)
6366    {
6367      /* Get an operator token.  */
6368      token = cp_lexer_peek_token (parser->lexer);
6369
6370      if (warn_cxx0x_compat
6371          && token->type == CPP_RSHIFT
6372          && !parser->greater_than_is_operator_p)
6373        {
6374          if (warning_at (token->location, OPT_Wc__0x_compat,
6375			  "%<>>%> operator will be treated as"
6376			  " two right angle brackets in C++0x"))
6377	    inform (token->location,
6378		    "suggest parentheses around %<>>%> expression");
6379        }
6380
6381      new_prec = TOKEN_PRECEDENCE (token);
6382
6383      /* Popping an entry off the stack means we completed a subexpression:
6384	 - either we found a token which is not an operator (`>' where it is not
6385	   an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6386	   will happen repeatedly;
6387	 - or, we found an operator which has lower priority.  This is the case
6388	   where the recursive descent *ascends*, as in `3 * 4 + 5' after
6389	   parsing `3 * 4'.  */
6390      if (new_prec <= prec)
6391	{
6392	  if (sp == stack)
6393	    break;
6394	  else
6395	    goto pop;
6396	}
6397
6398     get_rhs:
6399      tree_type = binops_by_token[token->type].tree_type;
6400
6401      /* We used the operator token.  */
6402      cp_lexer_consume_token (parser->lexer);
6403
6404      /* For "false && x" or "true || x", x will never be executed;
6405	 disable warnings while evaluating it.  */
6406      if (tree_type == TRUTH_ANDIF_EXPR)
6407	c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6408      else if (tree_type == TRUTH_ORIF_EXPR)
6409	c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6410
6411      /* Extract another operand.  It may be the RHS of this expression
6412	 or the LHS of a new, higher priority expression.  */
6413      rhs = cp_parser_simple_cast_expression (parser);
6414      rhs_type = ERROR_MARK;
6415
6416      /* Get another operator token.  Look up its precedence to avoid
6417	 building a useless (immediately popped) stack entry for common
6418	 cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6419      token = cp_lexer_peek_token (parser->lexer);
6420      lookahead_prec = TOKEN_PRECEDENCE (token);
6421      if (lookahead_prec > new_prec)
6422	{
6423	  /* ... and prepare to parse the RHS of the new, higher priority
6424	     expression.  Since precedence levels on the stack are
6425	     monotonically increasing, we do not have to care about
6426	     stack overflows.  */
6427	  sp->prec = prec;
6428	  sp->tree_type = tree_type;
6429	  sp->lhs = lhs;
6430	  sp->lhs_type = lhs_type;
6431	  sp++;
6432	  lhs = rhs;
6433	  lhs_type = rhs_type;
6434	  prec = new_prec;
6435	  new_prec = lookahead_prec;
6436	  goto get_rhs;
6437
6438	 pop:
6439	  lookahead_prec = new_prec;
6440	  /* If the stack is not empty, we have parsed into LHS the right side
6441	     (`4' in the example above) of an expression we had suspended.
6442	     We can use the information on the stack to recover the LHS (`3')
6443	     from the stack together with the tree code (`MULT_EXPR'), and
6444	     the precedence of the higher level subexpression
6445	     (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6446	     which will be used to actually build the additive expression.  */
6447	  --sp;
6448	  prec = sp->prec;
6449	  tree_type = sp->tree_type;
6450	  rhs = lhs;
6451	  rhs_type = lhs_type;
6452	  lhs = sp->lhs;
6453	  lhs_type = sp->lhs_type;
6454	}
6455
6456      /* Undo the disabling of warnings done above.  */
6457      if (tree_type == TRUTH_ANDIF_EXPR)
6458	c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6459      else if (tree_type == TRUTH_ORIF_EXPR)
6460	c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6461
6462      overloaded_p = false;
6463      /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6464	 ERROR_MARK for everything that is not a binary expression.
6465	 This makes warn_about_parentheses miss some warnings that
6466	 involve unary operators.  For unary expressions we should
6467	 pass the correct tree_code unless the unary expression was
6468	 surrounded by parentheses.
6469      */
6470      if (no_toplevel_fold_p
6471	  && lookahead_prec <= prec
6472	  && sp == stack
6473	  && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6474	lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6475      else
6476	lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6477				 &overloaded_p, tf_warning_or_error);
6478      lhs_type = tree_type;
6479
6480      /* If the binary operator required the use of an overloaded operator,
6481	 then this expression cannot be an integral constant-expression.
6482	 An overloaded operator can be used even if both operands are
6483	 otherwise permissible in an integral constant-expression if at
6484	 least one of the operands is of enumeration type.  */
6485
6486      if (overloaded_p
6487	  && (cp_parser_non_integral_constant_expression
6488	      (parser, "calls to overloaded operators")))
6489	return error_mark_node;
6490    }
6491
6492  return lhs;
6493}
6494
6495
6496/* Parse the `? expression : assignment-expression' part of a
6497   conditional-expression.  The LOGICAL_OR_EXPR is the
6498   logical-or-expression that started the conditional-expression.
6499   Returns a representation of the entire conditional-expression.
6500
6501   This routine is used by cp_parser_assignment_expression.
6502
6503     ? expression : assignment-expression
6504
6505   GNU Extensions:
6506
6507     ? : assignment-expression */
6508
6509static tree
6510cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6511{
6512  tree expr;
6513  tree assignment_expr;
6514
6515  /* Consume the `?' token.  */
6516  cp_lexer_consume_token (parser->lexer);
6517  if (cp_parser_allow_gnu_extensions_p (parser)
6518      && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6519    {
6520      /* Implicit true clause.  */
6521      expr = NULL_TREE;
6522      c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6523    }
6524  else
6525    {
6526      /* Parse the expression.  */
6527      c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6528      expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6529      c_inhibit_evaluation_warnings +=
6530	((logical_or_expr == truthvalue_true_node)
6531	 - (logical_or_expr == truthvalue_false_node));
6532    }
6533
6534  /* The next token should be a `:'.  */
6535  cp_parser_require (parser, CPP_COLON, "%<:%>");
6536  /* Parse the assignment-expression.  */
6537  assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6538  c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6539
6540  /* Build the conditional-expression.  */
6541  return build_x_conditional_expr (logical_or_expr,
6542				   expr,
6543				   assignment_expr,
6544                                   tf_warning_or_error);
6545}
6546
6547/* Parse an assignment-expression.
6548
6549   assignment-expression:
6550     conditional-expression
6551     logical-or-expression assignment-operator assignment_expression
6552     throw-expression
6553
6554   CAST_P is true if this expression is the target of a cast.
6555
6556   Returns a representation for the expression.  */
6557
6558static tree
6559cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6560				 cp_id_kind * pidk)
6561{
6562  tree expr;
6563
6564  /* If the next token is the `throw' keyword, then we're looking at
6565     a throw-expression.  */
6566  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6567    expr = cp_parser_throw_expression (parser);
6568  /* Otherwise, it must be that we are looking at a
6569     logical-or-expression.  */
6570  else
6571    {
6572      /* Parse the binary expressions (logical-or-expression).  */
6573      expr = cp_parser_binary_expression (parser, cast_p, false,
6574					  PREC_NOT_OPERATOR, pidk);
6575      /* If the next token is a `?' then we're actually looking at a
6576	 conditional-expression.  */
6577      if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6578	return cp_parser_question_colon_clause (parser, expr);
6579      else
6580	{
6581	  enum tree_code assignment_operator;
6582
6583	  /* If it's an assignment-operator, we're using the second
6584	     production.  */
6585	  assignment_operator
6586	    = cp_parser_assignment_operator_opt (parser);
6587	  if (assignment_operator != ERROR_MARK)
6588	    {
6589	      bool non_constant_p;
6590
6591	      /* Parse the right-hand side of the assignment.  */
6592	      tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6593
6594	      if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6595		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6596
6597	      /* An assignment may not appear in a
6598		 constant-expression.  */
6599	      if (cp_parser_non_integral_constant_expression (parser,
6600							      "an assignment"))
6601		return error_mark_node;
6602	      /* Build the assignment expression.  */
6603	      expr = build_x_modify_expr (expr,
6604					  assignment_operator,
6605					  rhs,
6606					  tf_warning_or_error);
6607	    }
6608	}
6609    }
6610
6611  return expr;
6612}
6613
6614/* Parse an (optional) assignment-operator.
6615
6616   assignment-operator: one of
6617     = *= /= %= += -= >>= <<= &= ^= |=
6618
6619   GNU Extension:
6620
6621   assignment-operator: one of
6622     <?= >?=
6623
6624   If the next token is an assignment operator, the corresponding tree
6625   code is returned, and the token is consumed.  For example, for
6626   `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6627   NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6628   TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6629   operator, ERROR_MARK is returned.  */
6630
6631static enum tree_code
6632cp_parser_assignment_operator_opt (cp_parser* parser)
6633{
6634  enum tree_code op;
6635  cp_token *token;
6636
6637  /* Peek at the next token.  */
6638  token = cp_lexer_peek_token (parser->lexer);
6639
6640  switch (token->type)
6641    {
6642    case CPP_EQ:
6643      op = NOP_EXPR;
6644      break;
6645
6646    case CPP_MULT_EQ:
6647      op = MULT_EXPR;
6648      break;
6649
6650    case CPP_DIV_EQ:
6651      op = TRUNC_DIV_EXPR;
6652      break;
6653
6654    case CPP_MOD_EQ:
6655      op = TRUNC_MOD_EXPR;
6656      break;
6657
6658    case CPP_PLUS_EQ:
6659      op = PLUS_EXPR;
6660      break;
6661
6662    case CPP_MINUS_EQ:
6663      op = MINUS_EXPR;
6664      break;
6665
6666    case CPP_RSHIFT_EQ:
6667      op = RSHIFT_EXPR;
6668      break;
6669
6670    case CPP_LSHIFT_EQ:
6671      op = LSHIFT_EXPR;
6672      break;
6673
6674    case CPP_AND_EQ:
6675      op = BIT_AND_EXPR;
6676      break;
6677
6678    case CPP_XOR_EQ:
6679      op = BIT_XOR_EXPR;
6680      break;
6681
6682    case CPP_OR_EQ:
6683      op = BIT_IOR_EXPR;
6684      break;
6685
6686    default:
6687      /* Nothing else is an assignment operator.  */
6688      op = ERROR_MARK;
6689    }
6690
6691  /* If it was an assignment operator, consume it.  */
6692  if (op != ERROR_MARK)
6693    cp_lexer_consume_token (parser->lexer);
6694
6695  return op;
6696}
6697
6698/* Parse an expression.
6699
6700   expression:
6701     assignment-expression
6702     expression , assignment-expression
6703
6704   CAST_P is true if this expression is the target of a cast.
6705
6706   Returns a representation of the expression.  */
6707
6708static tree
6709cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6710{
6711  tree expression = NULL_TREE;
6712
6713  while (true)
6714    {
6715      tree assignment_expression;
6716
6717      /* Parse the next assignment-expression.  */
6718      assignment_expression
6719	= cp_parser_assignment_expression (parser, cast_p, pidk);
6720      /* If this is the first assignment-expression, we can just
6721	 save it away.  */
6722      if (!expression)
6723	expression = assignment_expression;
6724      else
6725	expression = build_x_compound_expr (expression,
6726					    assignment_expression,
6727                                            tf_warning_or_error);
6728      /* If the next token is not a comma, then we are done with the
6729	 expression.  */
6730      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6731	break;
6732      /* Consume the `,'.  */
6733      cp_lexer_consume_token (parser->lexer);
6734      /* A comma operator cannot appear in a constant-expression.  */
6735      if (cp_parser_non_integral_constant_expression (parser,
6736						      "a comma operator"))
6737	expression = error_mark_node;
6738    }
6739
6740  return expression;
6741}
6742
6743/* Parse a constant-expression.
6744
6745   constant-expression:
6746     conditional-expression
6747
6748  If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6749  accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6750  constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6751  is false, NON_CONSTANT_P should be NULL.  */
6752
6753static tree
6754cp_parser_constant_expression (cp_parser* parser,
6755			       bool allow_non_constant_p,
6756			       bool *non_constant_p)
6757{
6758  bool saved_integral_constant_expression_p;
6759  bool saved_allow_non_integral_constant_expression_p;
6760  bool saved_non_integral_constant_expression_p;
6761  tree expression;
6762
6763  /* It might seem that we could simply parse the
6764     conditional-expression, and then check to see if it were
6765     TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6766     one that the compiler can figure out is constant, possibly after
6767     doing some simplifications or optimizations.  The standard has a
6768     precise definition of constant-expression, and we must honor
6769     that, even though it is somewhat more restrictive.
6770
6771     For example:
6772
6773       int i[(2, 3)];
6774
6775     is not a legal declaration, because `(2, 3)' is not a
6776     constant-expression.  The `,' operator is forbidden in a
6777     constant-expression.  However, GCC's constant-folding machinery
6778     will fold this operation to an INTEGER_CST for `3'.  */
6779
6780  /* Save the old settings.  */
6781  saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6782  saved_allow_non_integral_constant_expression_p
6783    = parser->allow_non_integral_constant_expression_p;
6784  saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6785  /* We are now parsing a constant-expression.  */
6786  parser->integral_constant_expression_p = true;
6787  parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6788  parser->non_integral_constant_expression_p = false;
6789  /* Although the grammar says "conditional-expression", we parse an
6790     "assignment-expression", which also permits "throw-expression"
6791     and the use of assignment operators.  In the case that
6792     ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6793     otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6794     actually essential that we look for an assignment-expression.
6795     For example, cp_parser_initializer_clauses uses this function to
6796     determine whether a particular assignment-expression is in fact
6797     constant.  */
6798  expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6799  /* Restore the old settings.  */
6800  parser->integral_constant_expression_p
6801    = saved_integral_constant_expression_p;
6802  parser->allow_non_integral_constant_expression_p
6803    = saved_allow_non_integral_constant_expression_p;
6804  if (allow_non_constant_p)
6805    *non_constant_p = parser->non_integral_constant_expression_p;
6806  else if (parser->non_integral_constant_expression_p)
6807    expression = error_mark_node;
6808  parser->non_integral_constant_expression_p
6809    = saved_non_integral_constant_expression_p;
6810
6811  return expression;
6812}
6813
6814/* Parse __builtin_offsetof.
6815
6816   offsetof-expression:
6817     "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6818
6819   offsetof-member-designator:
6820     id-expression
6821     | offsetof-member-designator "." id-expression
6822     | offsetof-member-designator "[" expression "]"
6823     | offsetof-member-designator "->" id-expression  */
6824
6825static tree
6826cp_parser_builtin_offsetof (cp_parser *parser)
6827{
6828  int save_ice_p, save_non_ice_p;
6829  tree type, expr;
6830  cp_id_kind dummy;
6831  cp_token *token;
6832
6833  /* We're about to accept non-integral-constant things, but will
6834     definitely yield an integral constant expression.  Save and
6835     restore these values around our local parsing.  */
6836  save_ice_p = parser->integral_constant_expression_p;
6837  save_non_ice_p = parser->non_integral_constant_expression_p;
6838
6839  /* Consume the "__builtin_offsetof" token.  */
6840  cp_lexer_consume_token (parser->lexer);
6841  /* Consume the opening `('.  */
6842  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6843  /* Parse the type-id.  */
6844  type = cp_parser_type_id (parser);
6845  /* Look for the `,'.  */
6846  cp_parser_require (parser, CPP_COMMA, "%<,%>");
6847  token = cp_lexer_peek_token (parser->lexer);
6848
6849  /* Build the (type *)null that begins the traditional offsetof macro.  */
6850  expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6851                            tf_warning_or_error);
6852
6853  /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6854  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6855						 true, &dummy, token->location);
6856  while (true)
6857    {
6858      token = cp_lexer_peek_token (parser->lexer);
6859      switch (token->type)
6860	{
6861	case CPP_OPEN_SQUARE:
6862	  /* offsetof-member-designator "[" expression "]" */
6863	  expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6864	  break;
6865
6866	case CPP_DEREF:
6867	  /* offsetof-member-designator "->" identifier */
6868	  expr = grok_array_decl (expr, integer_zero_node);
6869	  /* FALLTHRU */
6870
6871	case CPP_DOT:
6872	  /* offsetof-member-designator "." identifier */
6873	  cp_lexer_consume_token (parser->lexer);
6874	  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6875							 expr, true, &dummy,
6876							 token->location);
6877	  break;
6878
6879	case CPP_CLOSE_PAREN:
6880	  /* Consume the ")" token.  */
6881	  cp_lexer_consume_token (parser->lexer);
6882	  goto success;
6883
6884	default:
6885	  /* Error.  We know the following require will fail, but
6886	     that gives the proper error message.  */
6887	  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6888	  cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6889	  expr = error_mark_node;
6890	  goto failure;
6891	}
6892    }
6893
6894 success:
6895  /* If we're processing a template, we can't finish the semantics yet.
6896     Otherwise we can fold the entire expression now.  */
6897  if (processing_template_decl)
6898    expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6899  else
6900    expr = finish_offsetof (expr);
6901
6902 failure:
6903  parser->integral_constant_expression_p = save_ice_p;
6904  parser->non_integral_constant_expression_p = save_non_ice_p;
6905
6906  return expr;
6907}
6908
6909/* Parse a trait expression.  */
6910
6911static tree
6912cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6913{
6914  cp_trait_kind kind;
6915  tree type1, type2 = NULL_TREE;
6916  bool binary = false;
6917  cp_decl_specifier_seq decl_specs;
6918
6919  switch (keyword)
6920    {
6921    case RID_HAS_NOTHROW_ASSIGN:
6922      kind = CPTK_HAS_NOTHROW_ASSIGN;
6923      break;
6924    case RID_HAS_NOTHROW_CONSTRUCTOR:
6925      kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6926      break;
6927    case RID_HAS_NOTHROW_COPY:
6928      kind = CPTK_HAS_NOTHROW_COPY;
6929      break;
6930    case RID_HAS_TRIVIAL_ASSIGN:
6931      kind = CPTK_HAS_TRIVIAL_ASSIGN;
6932      break;
6933    case RID_HAS_TRIVIAL_CONSTRUCTOR:
6934      kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6935      break;
6936    case RID_HAS_TRIVIAL_COPY:
6937      kind = CPTK_HAS_TRIVIAL_COPY;
6938      break;
6939    case RID_HAS_TRIVIAL_DESTRUCTOR:
6940      kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6941      break;
6942    case RID_HAS_VIRTUAL_DESTRUCTOR:
6943      kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6944      break;
6945    case RID_IS_ABSTRACT:
6946      kind = CPTK_IS_ABSTRACT;
6947      break;
6948    case RID_IS_BASE_OF:
6949      kind = CPTK_IS_BASE_OF;
6950      binary = true;
6951      break;
6952    case RID_IS_CLASS:
6953      kind = CPTK_IS_CLASS;
6954      break;
6955    case RID_IS_CONVERTIBLE_TO:
6956      kind = CPTK_IS_CONVERTIBLE_TO;
6957      binary = true;
6958      break;
6959    case RID_IS_EMPTY:
6960      kind = CPTK_IS_EMPTY;
6961      break;
6962    case RID_IS_ENUM:
6963      kind = CPTK_IS_ENUM;
6964      break;
6965    case RID_IS_POD:
6966      kind = CPTK_IS_POD;
6967      break;
6968    case RID_IS_POLYMORPHIC:
6969      kind = CPTK_IS_POLYMORPHIC;
6970      break;
6971    case RID_IS_STD_LAYOUT:
6972      kind = CPTK_IS_STD_LAYOUT;
6973      break;
6974    case RID_IS_TRIVIAL:
6975      kind = CPTK_IS_TRIVIAL;
6976      break;
6977    case RID_IS_UNION:
6978      kind = CPTK_IS_UNION;
6979      break;
6980    default:
6981      gcc_unreachable ();
6982    }
6983
6984  /* Consume the token.  */
6985  cp_lexer_consume_token (parser->lexer);
6986
6987  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6988
6989  type1 = cp_parser_type_id (parser);
6990
6991  if (type1 == error_mark_node)
6992    return error_mark_node;
6993
6994  /* Build a trivial decl-specifier-seq.  */
6995  clear_decl_specs (&decl_specs);
6996  decl_specs.type = type1;
6997
6998  /* Call grokdeclarator to figure out what type this is.  */
6999  type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7000			  /*initialized=*/0, /*attrlist=*/NULL);
7001
7002  if (binary)
7003    {
7004      cp_parser_require (parser, CPP_COMMA, "%<,%>");
7005
7006      type2 = cp_parser_type_id (parser);
7007
7008      if (type2 == error_mark_node)
7009	return error_mark_node;
7010
7011      /* Build a trivial decl-specifier-seq.  */
7012      clear_decl_specs (&decl_specs);
7013      decl_specs.type = type2;
7014
7015      /* Call grokdeclarator to figure out what type this is.  */
7016      type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7017			      /*initialized=*/0, /*attrlist=*/NULL);
7018    }
7019
7020  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7021
7022  /* Complete the trait expression, which may mean either processing
7023     the trait expr now or saving it for template instantiation.  */
7024  return finish_trait_expr (kind, type1, type2);
7025}
7026
7027/* Lambdas that appear in variable initializer or default argument scope
7028   get that in their mangling, so we need to record it.  We might as well
7029   use the count for function and namespace scopes as well.  */
7030static GTY(()) tree lambda_scope;
7031static GTY(()) int lambda_count;
7032typedef struct GTY(()) tree_int
7033{
7034  tree t;
7035  int i;
7036} tree_int;
7037DEF_VEC_O(tree_int);
7038DEF_VEC_ALLOC_O(tree_int,gc);
7039static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7040
7041static void
7042start_lambda_scope (tree decl)
7043{
7044  tree_int ti;
7045  gcc_assert (decl);
7046  /* Once we're inside a function, we ignore other scopes and just push
7047     the function again so that popping works properly.  */
7048  if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7049    decl = current_function_decl;
7050  ti.t = lambda_scope;
7051  ti.i = lambda_count;
7052  VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7053  if (lambda_scope != decl)
7054    {
7055      /* Don't reset the count if we're still in the same function.  */
7056      lambda_scope = decl;
7057      lambda_count = 0;
7058    }
7059}
7060
7061static void
7062record_lambda_scope (tree lambda)
7063{
7064  LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7065  LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7066}
7067
7068static void
7069finish_lambda_scope (void)
7070{
7071  tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7072  if (lambda_scope != p->t)
7073    {
7074      lambda_scope = p->t;
7075      lambda_count = p->i;
7076    }
7077  VEC_pop (tree_int, lambda_scope_stack);
7078}
7079
7080/* Parse a lambda expression.
7081
7082   lambda-expression:
7083     lambda-introducer lambda-declarator [opt] compound-statement
7084
7085   Returns a representation of the expression.  */
7086
7087static tree
7088cp_parser_lambda_expression (cp_parser* parser)
7089{
7090  tree lambda_expr = build_lambda_expr ();
7091  tree type;
7092
7093  LAMBDA_EXPR_LOCATION (lambda_expr)
7094    = cp_lexer_peek_token (parser->lexer)->location;
7095
7096  if (cp_unevaluated_operand)
7097    error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7098	      "lambda-expression in unevaluated context");
7099
7100  /* We may be in the middle of deferred access check.  Disable
7101     it now.  */
7102  push_deferring_access_checks (dk_no_deferred);
7103
7104  cp_parser_lambda_introducer (parser, lambda_expr);
7105
7106  type = begin_lambda_type (lambda_expr);
7107
7108  record_lambda_scope (lambda_expr);
7109
7110  /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7111  determine_visibility (TYPE_NAME (type));
7112
7113  /* Now that we've started the type, add the capture fields for any
7114     explicit captures.  */
7115  register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7116
7117  {
7118    /* Inside the class, surrounding template-parameter-lists do not apply.  */
7119    unsigned int saved_num_template_parameter_lists
7120        = parser->num_template_parameter_lists;
7121
7122    parser->num_template_parameter_lists = 0;
7123
7124    /* By virtue of defining a local class, a lambda expression has access to
7125       the private variables of enclosing classes.  */
7126
7127    cp_parser_lambda_declarator_opt (parser, lambda_expr);
7128
7129    cp_parser_lambda_body (parser, lambda_expr);
7130
7131    /* The capture list was built up in reverse order; fix that now.  */
7132    {
7133      tree newlist = NULL_TREE;
7134      tree elt, next;
7135
7136      for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7137	   elt; elt = next)
7138	{
7139	  tree field = TREE_PURPOSE (elt);
7140	  char *buf;
7141
7142	  next = TREE_CHAIN (elt);
7143	  TREE_CHAIN (elt) = newlist;
7144	  newlist = elt;
7145
7146	  /* Also add __ to the beginning of the field name so that code
7147	     outside the lambda body can't see the captured name.  We could
7148	     just remove the name entirely, but this is more useful for
7149	     debugging.  */
7150	  if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7151	    /* The 'this' capture already starts with __.  */
7152	    continue;
7153
7154	  buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7155	  buf[1] = buf[0] = '_';
7156	  memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7157		  IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7158	  DECL_NAME (field) = get_identifier (buf);
7159	}
7160      LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7161    }
7162
7163    maybe_add_lambda_conv_op (type);
7164
7165    type = finish_struct (type, /*attributes=*/NULL_TREE);
7166
7167    parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7168  }
7169
7170  pop_deferring_access_checks ();
7171
7172  return build_lambda_object (lambda_expr);
7173}
7174
7175/* Parse the beginning of a lambda expression.
7176
7177   lambda-introducer:
7178     [ lambda-capture [opt] ]
7179
7180   LAMBDA_EXPR is the current representation of the lambda expression.  */
7181
7182static void
7183cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7184{
7185  /* Need commas after the first capture.  */
7186  bool first = true;
7187
7188  /* Eat the leading `['.  */
7189  cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7190
7191  /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7192  if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7193      && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7194    LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7195  else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7196    LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7197
7198  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7199    {
7200      cp_lexer_consume_token (parser->lexer);
7201      first = false;
7202    }
7203
7204  while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7205    {
7206      cp_token* capture_token;
7207      tree capture_id;
7208      tree capture_init_expr;
7209      cp_id_kind idk = CP_ID_KIND_NONE;
7210      bool explicit_init_p = false;
7211
7212      enum capture_kind_type
7213      {
7214	BY_COPY,
7215	BY_REFERENCE
7216      };
7217      enum capture_kind_type capture_kind = BY_COPY;
7218
7219      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7220	{
7221	  error ("expected end of capture-list");
7222	  return;
7223	}
7224
7225      if (first)
7226	first = false;
7227      else
7228	cp_parser_require (parser, CPP_COMMA, "%<,%>");
7229
7230      /* Possibly capture `this'.  */
7231      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7232	{
7233	  cp_lexer_consume_token (parser->lexer);
7234	  add_capture (lambda_expr,
7235		       /*id=*/get_identifier ("__this"),
7236		       /*initializer=*/finish_this_expr(),
7237		       /*by_reference_p=*/false,
7238		       explicit_init_p);
7239	  continue;
7240	}
7241
7242      /* Remember whether we want to capture as a reference or not.  */
7243      if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7244	{
7245	  capture_kind = BY_REFERENCE;
7246	  cp_lexer_consume_token (parser->lexer);
7247	}
7248
7249      /* Get the identifier.  */
7250      capture_token = cp_lexer_peek_token (parser->lexer);
7251      capture_id = cp_parser_identifier (parser);
7252
7253      if (capture_id == error_mark_node)
7254	/* Would be nice to have a cp_parser_skip_to_closing_x for general
7255           delimiters, but I modified this to stop on unnested ']' as well.  It
7256           was already changed to stop on unnested '}', so the
7257           "closing_parenthesis" name is no more misleading with my change.  */
7258	{
7259	  cp_parser_skip_to_closing_parenthesis (parser,
7260						 /*recovering=*/true,
7261						 /*or_comma=*/true,
7262						 /*consume_paren=*/true);
7263	  break;
7264	}
7265
7266      /* Find the initializer for this capture.  */
7267      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7268	{
7269	  /* An explicit expression exists.  */
7270	  cp_lexer_consume_token (parser->lexer);
7271          pedwarn (input_location, OPT_pedantic,
7272                   "ISO C++ does not allow initializers "
7273                   "in lambda expression capture lists");
7274	  capture_init_expr = cp_parser_assignment_expression (parser,
7275							       /*cast_p=*/true,
7276							       &idk);
7277	  explicit_init_p = true;
7278	}
7279      else
7280	{
7281	  const char* error_msg;
7282
7283	  /* Turn the identifier into an id-expression.  */
7284	  capture_init_expr
7285            = cp_parser_lookup_name
7286                (parser,
7287		 capture_id,
7288                 none_type,
7289                 /*is_template=*/false,
7290                 /*is_namespace=*/false,
7291                 /*check_dependency=*/true,
7292                 /*ambiguous_decls=*/NULL,
7293                 capture_token->location);
7294
7295	  capture_init_expr
7296            = finish_id_expression
7297                (capture_id,
7298		 capture_init_expr,
7299                 parser->scope,
7300                 &idk,
7301                 /*integral_constant_expression_p=*/false,
7302                 /*allow_non_integral_constant_expression_p=*/false,
7303                 /*non_integral_constant_expression_p=*/NULL,
7304                 /*template_p=*/false,
7305                 /*done=*/true,
7306                 /*address_p=*/false,
7307                 /*template_arg_p=*/false,
7308                 &error_msg,
7309                 capture_token->location);
7310	}
7311
7312      if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7313	capture_init_expr
7314	  = unqualified_name_lookup_error (capture_init_expr);
7315
7316      add_capture (lambda_expr,
7317		   capture_id,
7318		   capture_init_expr,
7319		   /*by_reference_p=*/capture_kind == BY_REFERENCE,
7320		   explicit_init_p);
7321    }
7322
7323  cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7324}
7325
7326/* Parse the (optional) middle of a lambda expression.
7327
7328   lambda-declarator:
7329     ( parameter-declaration-clause [opt] )
7330       attribute-specifier [opt]
7331       mutable [opt]
7332       exception-specification [opt]
7333       lambda-return-type-clause [opt]
7334
7335   LAMBDA_EXPR is the current representation of the lambda expression.  */
7336
7337static void
7338cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7339{
7340  /* 5.1.1.4 of the standard says:
7341       If a lambda-expression does not include a lambda-declarator, it is as if
7342       the lambda-declarator were ().
7343     This means an empty parameter list, no attributes, and no exception
7344     specification.  */
7345  tree param_list = void_list_node;
7346  tree attributes = NULL_TREE;
7347  tree exception_spec = NULL_TREE;
7348  tree t;
7349
7350  /* The lambda-declarator is optional, but must begin with an opening
7351     parenthesis if present.  */
7352  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7353    {
7354      cp_lexer_consume_token (parser->lexer);
7355
7356      begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7357
7358      /* Parse parameters.  */
7359      param_list = cp_parser_parameter_declaration_clause (parser);
7360
7361      /* Default arguments shall not be specified in the
7362	 parameter-declaration-clause of a lambda-declarator.  */
7363      for (t = param_list; t; t = TREE_CHAIN (t))
7364	if (TREE_PURPOSE (t))
7365	  pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7366		   "default argument specified for lambda parameter");
7367
7368      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7369
7370      attributes = cp_parser_attributes_opt (parser);
7371
7372      /* Parse optional `mutable' keyword.  */
7373      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7374        {
7375          cp_lexer_consume_token (parser->lexer);
7376          LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7377        }
7378
7379      /* Parse optional exception specification.  */
7380      exception_spec = cp_parser_exception_specification_opt (parser);
7381
7382      /* Parse optional trailing return type.  */
7383      if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7384        {
7385          cp_lexer_consume_token (parser->lexer);
7386          LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7387        }
7388
7389      /* The function parameters must be in scope all the way until after the
7390         trailing-return-type in case of decltype.  */
7391      for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7392	pop_binding (DECL_NAME (t), t);
7393
7394      leave_scope ();
7395    }
7396
7397  /* Create the function call operator.
7398
7399     Messing with declarators like this is no uglier than building up the
7400     FUNCTION_DECL by hand, and this is less likely to get out of sync with
7401     other code.  */
7402  {
7403    cp_decl_specifier_seq return_type_specs;
7404    cp_declarator* declarator;
7405    tree fco;
7406    int quals;
7407    void *p;
7408
7409    clear_decl_specs (&return_type_specs);
7410    if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7411      return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7412    else
7413      /* Maybe we will deduce the return type later, but we can use void
7414	 as a placeholder return type anyways.  */
7415      return_type_specs.type = void_type_node;
7416
7417    p = obstack_alloc (&declarator_obstack, 0);
7418
7419    declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7420				     sfk_none);
7421
7422    quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7423	     ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7424    declarator = make_call_declarator (declarator, param_list, quals,
7425				       exception_spec,
7426                                       /*late_return_type=*/NULL_TREE);
7427    declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7428
7429    fco = grokmethod (&return_type_specs,
7430		      declarator,
7431		      attributes);
7432    DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7433    DECL_ARTIFICIAL (fco) = 1;
7434
7435    finish_member_declaration (fco);
7436
7437    obstack_free (&declarator_obstack, p);
7438  }
7439}
7440
7441/* Parse the body of a lambda expression, which is simply
7442
7443   compound-statement
7444
7445   but which requires special handling.
7446   LAMBDA_EXPR is the current representation of the lambda expression.  */
7447
7448static void
7449cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7450{
7451  bool nested = (current_function_decl != NULL_TREE);
7452  if (nested)
7453    push_function_context ();
7454
7455  /* Finish the function call operator
7456     - class_specifier
7457     + late_parsing_for_member
7458     + function_definition_after_declarator
7459     + ctor_initializer_opt_and_function_body  */
7460  {
7461    tree fco = lambda_function (lambda_expr);
7462    tree body;
7463    bool done = false;
7464
7465    /* Let the front end know that we are going to be defining this
7466       function.  */
7467    start_preparsed_function (fco,
7468			      NULL_TREE,
7469			      SF_PRE_PARSED | SF_INCLASS_INLINE);
7470
7471    start_lambda_scope (fco);
7472    body = begin_function_body ();
7473
7474    /* 5.1.1.4 of the standard says:
7475         If a lambda-expression does not include a trailing-return-type, it
7476         is as if the trailing-return-type denotes the following type:
7477	  * if the compound-statement is of the form
7478               { return attribute-specifier [opt] expression ; }
7479             the type of the returned expression after lvalue-to-rvalue
7480             conversion (_conv.lval_ 4.1), array-to-pointer conversion
7481             (_conv.array_ 4.2), and function-to-pointer conversion
7482             (_conv.func_ 4.3);
7483          * otherwise, void.  */
7484
7485    /* In a lambda that has neither a lambda-return-type-clause
7486       nor a deducible form, errors should be reported for return statements
7487       in the body.  Since we used void as the placeholder return type, parsing
7488       the body as usual will give such desired behavior.  */
7489    if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7490        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7491        && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7492        && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7493      {
7494	tree compound_stmt;
7495	tree expr = NULL_TREE;
7496	cp_id_kind idk = CP_ID_KIND_NONE;
7497
7498	/* Parse tentatively in case there's more after the initial return
7499	   statement.  */
7500	cp_parser_parse_tentatively (parser);
7501
7502	cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7503	cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7504
7505	expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7506
7507	cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7508	cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7509
7510	if (cp_parser_parse_definitely (parser))
7511	  {
7512	    apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7513
7514	    compound_stmt = begin_compound_stmt (0);
7515	    /* Will get error here if type not deduced yet.  */
7516	    finish_return_stmt (expr);
7517	    finish_compound_stmt (compound_stmt);
7518
7519	    done = true;
7520	  }
7521      }
7522
7523    if (!done)
7524      {
7525	if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7526	  LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7527	/* TODO: does begin_compound_stmt want BCS_FN_BODY?
7528	   cp_parser_compound_stmt does not pass it.  */
7529	cp_parser_function_body (parser);
7530	LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7531      }
7532
7533    finish_function_body (body);
7534    finish_lambda_scope ();
7535
7536    /* Finish the function and generate code for it if necessary.  */
7537    expand_or_defer_fn (finish_function (/*inline*/2));
7538  }
7539
7540  if (nested)
7541    pop_function_context();
7542}
7543
7544/* Statements [gram.stmt.stmt]  */
7545
7546/* Parse a statement.
7547
7548   statement:
7549     labeled-statement
7550     expression-statement
7551     compound-statement
7552     selection-statement
7553     iteration-statement
7554     jump-statement
7555     declaration-statement
7556     try-block
7557
7558  IN_COMPOUND is true when the statement is nested inside a
7559  cp_parser_compound_statement; this matters for certain pragmas.
7560
7561  If IF_P is not NULL, *IF_P is set to indicate whether the statement
7562  is a (possibly labeled) if statement which is not enclosed in braces
7563  and has an else clause.  This is used to implement -Wparentheses.  */
7564
7565static void
7566cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7567		     bool in_compound, bool *if_p)
7568{
7569  tree statement;
7570  cp_token *token;
7571  location_t statement_location;
7572
7573 restart:
7574  if (if_p != NULL)
7575    *if_p = false;
7576  /* There is no statement yet.  */
7577  statement = NULL_TREE;
7578  /* Peek at the next token.  */
7579  token = cp_lexer_peek_token (parser->lexer);
7580  /* Remember the location of the first token in the statement.  */
7581  statement_location = token->location;
7582  /* If this is a keyword, then that will often determine what kind of
7583     statement we have.  */
7584  if (token->type == CPP_KEYWORD)
7585    {
7586      enum rid keyword = token->keyword;
7587
7588      switch (keyword)
7589	{
7590	case RID_CASE:
7591	case RID_DEFAULT:
7592	  /* Looks like a labeled-statement with a case label.
7593	     Parse the label, and then use tail recursion to parse
7594	     the statement.  */
7595	  cp_parser_label_for_labeled_statement (parser);
7596	  goto restart;
7597
7598	case RID_IF:
7599	case RID_SWITCH:
7600	  statement = cp_parser_selection_statement (parser, if_p);
7601	  break;
7602
7603	case RID_WHILE:
7604	case RID_DO:
7605	case RID_FOR:
7606	  statement = cp_parser_iteration_statement (parser);
7607	  break;
7608
7609	case RID_BREAK:
7610	case RID_CONTINUE:
7611	case RID_RETURN:
7612	case RID_GOTO:
7613	  statement = cp_parser_jump_statement (parser);
7614	  break;
7615
7616	  /* Objective-C++ exception-handling constructs.  */
7617	case RID_AT_TRY:
7618	case RID_AT_CATCH:
7619	case RID_AT_FINALLY:
7620	case RID_AT_SYNCHRONIZED:
7621	case RID_AT_THROW:
7622	  statement = cp_parser_objc_statement (parser);
7623	  break;
7624
7625	case RID_TRY:
7626	  statement = cp_parser_try_block (parser);
7627	  break;
7628
7629	case RID_NAMESPACE:
7630	  /* This must be a namespace alias definition.  */
7631	  cp_parser_declaration_statement (parser);
7632	  return;
7633
7634	default:
7635	  /* It might be a keyword like `int' that can start a
7636	     declaration-statement.  */
7637	  break;
7638	}
7639    }
7640  else if (token->type == CPP_NAME)
7641    {
7642      /* If the next token is a `:', then we are looking at a
7643	 labeled-statement.  */
7644      token = cp_lexer_peek_nth_token (parser->lexer, 2);
7645      if (token->type == CPP_COLON)
7646	{
7647	  /* Looks like a labeled-statement with an ordinary label.
7648	     Parse the label, and then use tail recursion to parse
7649	     the statement.  */
7650	  cp_parser_label_for_labeled_statement (parser);
7651	  goto restart;
7652	}
7653    }
7654  /* Anything that starts with a `{' must be a compound-statement.  */
7655  else if (token->type == CPP_OPEN_BRACE)
7656    statement = cp_parser_compound_statement (parser, NULL, false);
7657  /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7658     a statement all its own.  */
7659  else if (token->type == CPP_PRAGMA)
7660    {
7661      /* Only certain OpenMP pragmas are attached to statements, and thus
7662	 are considered statements themselves.  All others are not.  In
7663	 the context of a compound, accept the pragma as a "statement" and
7664	 return so that we can check for a close brace.  Otherwise we
7665	 require a real statement and must go back and read one.  */
7666      if (in_compound)
7667	cp_parser_pragma (parser, pragma_compound);
7668      else if (!cp_parser_pragma (parser, pragma_stmt))
7669	goto restart;
7670      return;
7671    }
7672  else if (token->type == CPP_EOF)
7673    {
7674      cp_parser_error (parser, "expected statement");
7675      return;
7676    }
7677
7678  /* Everything else must be a declaration-statement or an
7679     expression-statement.  Try for the declaration-statement
7680     first, unless we are looking at a `;', in which case we know that
7681     we have an expression-statement.  */
7682  if (!statement)
7683    {
7684      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7685	{
7686	  cp_parser_parse_tentatively (parser);
7687	  /* Try to parse the declaration-statement.  */
7688	  cp_parser_declaration_statement (parser);
7689	  /* If that worked, we're done.  */
7690	  if (cp_parser_parse_definitely (parser))
7691	    return;
7692	}
7693      /* Look for an expression-statement instead.  */
7694      statement = cp_parser_expression_statement (parser, in_statement_expr);
7695    }
7696
7697  /* Set the line number for the statement.  */
7698  if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7699    SET_EXPR_LOCATION (statement, statement_location);
7700}
7701
7702/* Parse the label for a labeled-statement, i.e.
7703
7704   identifier :
7705   case constant-expression :
7706   default :
7707
7708   GNU Extension:
7709   case constant-expression ... constant-expression : statement
7710
7711   When a label is parsed without errors, the label is added to the
7712   parse tree by the finish_* functions, so this function doesn't
7713   have to return the label.  */
7714
7715static void
7716cp_parser_label_for_labeled_statement (cp_parser* parser)
7717{
7718  cp_token *token;
7719  tree label = NULL_TREE;
7720
7721  /* The next token should be an identifier.  */
7722  token = cp_lexer_peek_token (parser->lexer);
7723  if (token->type != CPP_NAME
7724      && token->type != CPP_KEYWORD)
7725    {
7726      cp_parser_error (parser, "expected labeled-statement");
7727      return;
7728    }
7729
7730  switch (token->keyword)
7731    {
7732    case RID_CASE:
7733      {
7734	tree expr, expr_hi;
7735	cp_token *ellipsis;
7736
7737	/* Consume the `case' token.  */
7738	cp_lexer_consume_token (parser->lexer);
7739	/* Parse the constant-expression.  */
7740	expr = cp_parser_constant_expression (parser,
7741					      /*allow_non_constant_p=*/false,
7742					      NULL);
7743
7744	ellipsis = cp_lexer_peek_token (parser->lexer);
7745	if (ellipsis->type == CPP_ELLIPSIS)
7746	  {
7747	    /* Consume the `...' token.  */
7748	    cp_lexer_consume_token (parser->lexer);
7749	    expr_hi =
7750	      cp_parser_constant_expression (parser,
7751					     /*allow_non_constant_p=*/false,
7752					     NULL);
7753	    /* We don't need to emit warnings here, as the common code
7754	       will do this for us.  */
7755	  }
7756	else
7757	  expr_hi = NULL_TREE;
7758
7759	if (parser->in_switch_statement_p)
7760	  finish_case_label (token->location, expr, expr_hi);
7761	else
7762	  error_at (token->location,
7763		    "case label %qE not within a switch statement",
7764		    expr);
7765      }
7766      break;
7767
7768    case RID_DEFAULT:
7769      /* Consume the `default' token.  */
7770      cp_lexer_consume_token (parser->lexer);
7771
7772      if (parser->in_switch_statement_p)
7773	finish_case_label (token->location, NULL_TREE, NULL_TREE);
7774      else
7775	error_at (token->location, "case label not within a switch statement");
7776      break;
7777
7778    default:
7779      /* Anything else must be an ordinary label.  */
7780      label = finish_label_stmt (cp_parser_identifier (parser));
7781      break;
7782    }
7783
7784  /* Require the `:' token.  */
7785  cp_parser_require (parser, CPP_COLON, "%<:%>");
7786
7787  /* An ordinary label may optionally be followed by attributes.
7788     However, this is only permitted if the attributes are then
7789     followed by a semicolon.  This is because, for backward
7790     compatibility, when parsing
7791       lab: __attribute__ ((unused)) int i;
7792     we want the attribute to attach to "i", not "lab".  */
7793  if (label != NULL_TREE
7794      && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7795    {
7796      tree attrs;
7797
7798      cp_parser_parse_tentatively (parser);
7799      attrs = cp_parser_attributes_opt (parser);
7800      if (attrs == NULL_TREE
7801	  || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7802	cp_parser_abort_tentative_parse (parser);
7803      else if (!cp_parser_parse_definitely (parser))
7804	;
7805      else
7806	cplus_decl_attributes (&label, attrs, 0);
7807    }
7808}
7809
7810/* Parse an expression-statement.
7811
7812   expression-statement:
7813     expression [opt] ;
7814
7815   Returns the new EXPR_STMT -- or NULL_TREE if the expression
7816   statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7817   indicates whether this expression-statement is part of an
7818   expression statement.  */
7819
7820static tree
7821cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7822{
7823  tree statement = NULL_TREE;
7824  cp_token *token = cp_lexer_peek_token (parser->lexer);
7825
7826  /* If the next token is a ';', then there is no expression
7827     statement.  */
7828  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7829    statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7830
7831  /* Give a helpful message for "A<T>::type t;" and the like.  */
7832  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7833      && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7834    {
7835      if (TREE_CODE (statement) == SCOPE_REF)
7836	error_at (token->location, "need %<typename%> before %qE because "
7837		  "%qT is a dependent scope",
7838		  statement, TREE_OPERAND (statement, 0));
7839      else if (is_overloaded_fn (statement)
7840	       && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7841	{
7842	  /* A::A a; */
7843	  tree fn = get_first_fn (statement);
7844	  error_at (token->location,
7845		    "%<%T::%D%> names the constructor, not the type",
7846		    DECL_CONTEXT (fn), DECL_NAME (fn));
7847	}
7848    }
7849
7850  /* Consume the final `;'.  */
7851  cp_parser_consume_semicolon_at_end_of_statement (parser);
7852
7853  if (in_statement_expr
7854      && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7855    /* This is the final expression statement of a statement
7856       expression.  */
7857    statement = finish_stmt_expr_expr (statement, in_statement_expr);
7858  else if (statement)
7859    statement = finish_expr_stmt (statement);
7860  else
7861    finish_stmt ();
7862
7863  return statement;
7864}
7865
7866/* Parse a compound-statement.
7867
7868   compound-statement:
7869     { statement-seq [opt] }
7870
7871   GNU extension:
7872
7873   compound-statement:
7874     { label-declaration-seq [opt] statement-seq [opt] }
7875
7876   label-declaration-seq:
7877     label-declaration
7878     label-declaration-seq label-declaration
7879
7880   Returns a tree representing the statement.  */
7881
7882static tree
7883cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7884			      bool in_try)
7885{
7886  tree compound_stmt;
7887
7888  /* Consume the `{'.  */
7889  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7890    return error_mark_node;
7891  /* Begin the compound-statement.  */
7892  compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7893  /* If the next keyword is `__label__' we have a label declaration.  */
7894  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7895    cp_parser_label_declaration (parser);
7896  /* Parse an (optional) statement-seq.  */
7897  cp_parser_statement_seq_opt (parser, in_statement_expr);
7898  /* Finish the compound-statement.  */
7899  finish_compound_stmt (compound_stmt);
7900  /* Consume the `}'.  */
7901  cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7902
7903  return compound_stmt;
7904}
7905
7906/* Parse an (optional) statement-seq.
7907
7908   statement-seq:
7909     statement
7910     statement-seq [opt] statement  */
7911
7912static void
7913cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7914{
7915  /* Scan statements until there aren't any more.  */
7916  while (true)
7917    {
7918      cp_token *token = cp_lexer_peek_token (parser->lexer);
7919
7920      /* If we're looking at a `}', then we've run out of statements.  */
7921      if (token->type == CPP_CLOSE_BRACE
7922	  || token->type == CPP_EOF
7923	  || token->type == CPP_PRAGMA_EOL)
7924	break;
7925
7926      /* If we are in a compound statement and find 'else' then
7927	 something went wrong.  */
7928      else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7929	{
7930	  if (parser->in_statement & IN_IF_STMT)
7931	    break;
7932	  else
7933	    {
7934	      token = cp_lexer_consume_token (parser->lexer);
7935	      error_at (token->location, "%<else%> without a previous %<if%>");
7936	    }
7937	}
7938
7939      /* Parse the statement.  */
7940      cp_parser_statement (parser, in_statement_expr, true, NULL);
7941    }
7942}
7943
7944/* Parse a selection-statement.
7945
7946   selection-statement:
7947     if ( condition ) statement
7948     if ( condition ) statement else statement
7949     switch ( condition ) statement
7950
7951   Returns the new IF_STMT or SWITCH_STMT.
7952
7953   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7954   is a (possibly labeled) if statement which is not enclosed in
7955   braces and has an else clause.  This is used to implement
7956   -Wparentheses.  */
7957
7958static tree
7959cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7960{
7961  cp_token *token;
7962  enum rid keyword;
7963
7964  if (if_p != NULL)
7965    *if_p = false;
7966
7967  /* Peek at the next token.  */
7968  token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7969
7970  /* See what kind of keyword it is.  */
7971  keyword = token->keyword;
7972  switch (keyword)
7973    {
7974    case RID_IF:
7975    case RID_SWITCH:
7976      {
7977	tree statement;
7978	tree condition;
7979
7980	/* Look for the `('.  */
7981	if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7982	  {
7983	    cp_parser_skip_to_end_of_statement (parser);
7984	    return error_mark_node;
7985	  }
7986
7987	/* Begin the selection-statement.  */
7988	if (keyword == RID_IF)
7989	  statement = begin_if_stmt ();
7990	else
7991	  statement = begin_switch_stmt ();
7992
7993	/* Parse the condition.  */
7994	condition = cp_parser_condition (parser);
7995	/* Look for the `)'.  */
7996	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7997	  cp_parser_skip_to_closing_parenthesis (parser, true, false,
7998						 /*consume_paren=*/true);
7999
8000	if (keyword == RID_IF)
8001	  {
8002	    bool nested_if;
8003	    unsigned char in_statement;
8004
8005	    /* Add the condition.  */
8006	    finish_if_stmt_cond (condition, statement);
8007
8008	    /* Parse the then-clause.  */
8009	    in_statement = parser->in_statement;
8010	    parser->in_statement |= IN_IF_STMT;
8011	    if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8012	      {
8013	        location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8014		add_stmt (build_empty_stmt (loc));
8015		cp_lexer_consume_token (parser->lexer);
8016	        if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8017		  warning_at (loc, OPT_Wempty_body, "suggest braces around "
8018			      "empty body in an %<if%> statement");
8019		nested_if = false;
8020	      }
8021	    else
8022	      cp_parser_implicitly_scoped_statement (parser, &nested_if);
8023	    parser->in_statement = in_statement;
8024
8025	    finish_then_clause (statement);
8026
8027	    /* If the next token is `else', parse the else-clause.  */
8028	    if (cp_lexer_next_token_is_keyword (parser->lexer,
8029						RID_ELSE))
8030	      {
8031		/* Consume the `else' keyword.  */
8032		cp_lexer_consume_token (parser->lexer);
8033		begin_else_clause (statement);
8034		/* Parse the else-clause.  */
8035	        if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8036	          {
8037		    location_t loc;
8038		    loc = cp_lexer_peek_token (parser->lexer)->location;
8039		    warning_at (loc,
8040				OPT_Wempty_body, "suggest braces around "
8041			        "empty body in an %<else%> statement");
8042		    add_stmt (build_empty_stmt (loc));
8043		    cp_lexer_consume_token (parser->lexer);
8044		  }
8045		else
8046		  cp_parser_implicitly_scoped_statement (parser, NULL);
8047
8048		finish_else_clause (statement);
8049
8050		/* If we are currently parsing a then-clause, then
8051		   IF_P will not be NULL.  We set it to true to
8052		   indicate that this if statement has an else clause.
8053		   This may trigger the Wparentheses warning below
8054		   when we get back up to the parent if statement.  */
8055		if (if_p != NULL)
8056		  *if_p = true;
8057	      }
8058	    else
8059	      {
8060		/* This if statement does not have an else clause.  If
8061		   NESTED_IF is true, then the then-clause is an if
8062		   statement which does have an else clause.  We warn
8063		   about the potential ambiguity.  */
8064		if (nested_if)
8065		  warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8066			      "suggest explicit braces to avoid ambiguous"
8067			      " %<else%>");
8068	      }
8069
8070	    /* Now we're all done with the if-statement.  */
8071	    finish_if_stmt (statement);
8072	  }
8073	else
8074	  {
8075	    bool in_switch_statement_p;
8076	    unsigned char in_statement;
8077
8078	    /* Add the condition.  */
8079	    finish_switch_cond (condition, statement);
8080
8081	    /* Parse the body of the switch-statement.  */
8082	    in_switch_statement_p = parser->in_switch_statement_p;
8083	    in_statement = parser->in_statement;
8084	    parser->in_switch_statement_p = true;
8085	    parser->in_statement |= IN_SWITCH_STMT;
8086	    cp_parser_implicitly_scoped_statement (parser, NULL);
8087	    parser->in_switch_statement_p = in_switch_statement_p;
8088	    parser->in_statement = in_statement;
8089
8090	    /* Now we're all done with the switch-statement.  */
8091	    finish_switch_stmt (statement);
8092	  }
8093
8094	return statement;
8095      }
8096      break;
8097
8098    default:
8099      cp_parser_error (parser, "expected selection-statement");
8100      return error_mark_node;
8101    }
8102}
8103
8104/* Parse a condition.
8105
8106   condition:
8107     expression
8108     type-specifier-seq declarator = initializer-clause
8109     type-specifier-seq declarator braced-init-list
8110
8111   GNU Extension:
8112
8113   condition:
8114     type-specifier-seq declarator asm-specification [opt]
8115       attributes [opt] = assignment-expression
8116
8117   Returns the expression that should be tested.  */
8118
8119static tree
8120cp_parser_condition (cp_parser* parser)
8121{
8122  cp_decl_specifier_seq type_specifiers;
8123  const char *saved_message;
8124
8125  /* Try the declaration first.  */
8126  cp_parser_parse_tentatively (parser);
8127  /* New types are not allowed in the type-specifier-seq for a
8128     condition.  */
8129  saved_message = parser->type_definition_forbidden_message;
8130  parser->type_definition_forbidden_message
8131    = G_("types may not be defined in conditions");
8132  /* Parse the type-specifier-seq.  */
8133  cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8134				/*is_trailing_return=*/false,
8135				&type_specifiers);
8136  /* Restore the saved message.  */
8137  parser->type_definition_forbidden_message = saved_message;
8138  /* If all is well, we might be looking at a declaration.  */
8139  if (!cp_parser_error_occurred (parser))
8140    {
8141      tree decl;
8142      tree asm_specification;
8143      tree attributes;
8144      cp_declarator *declarator;
8145      tree initializer = NULL_TREE;
8146
8147      /* Parse the declarator.  */
8148      declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8149					 /*ctor_dtor_or_conv_p=*/NULL,
8150					 /*parenthesized_p=*/NULL,
8151					 /*member_p=*/false);
8152      /* Parse the attributes.  */
8153      attributes = cp_parser_attributes_opt (parser);
8154      /* Parse the asm-specification.  */
8155      asm_specification = cp_parser_asm_specification_opt (parser);
8156      /* If the next token is not an `=' or '{', then we might still be
8157	 looking at an expression.  For example:
8158
8159	   if (A(a).x)
8160
8161	 looks like a decl-specifier-seq and a declarator -- but then
8162	 there is no `=', so this is an expression.  */
8163      if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8164	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8165	cp_parser_simulate_error (parser);
8166
8167      /* If we did see an `=' or '{', then we are looking at a declaration
8168	 for sure.  */
8169      if (cp_parser_parse_definitely (parser))
8170	{
8171	  tree pushed_scope;
8172	  bool non_constant_p;
8173	  bool flags = LOOKUP_ONLYCONVERTING;
8174
8175	  /* Create the declaration.  */
8176	  decl = start_decl (declarator, &type_specifiers,
8177			     /*initialized_p=*/true,
8178			     attributes, /*prefix_attributes=*/NULL_TREE,
8179			     &pushed_scope);
8180
8181	  /* Parse the initializer.  */
8182	  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8183	    {
8184	      initializer = cp_parser_braced_list (parser, &non_constant_p);
8185	      CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8186	      flags = 0;
8187	    }
8188	  else
8189	    {
8190	      /* Consume the `='.  */
8191	      cp_parser_require (parser, CPP_EQ, "%<=%>");
8192	      initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8193	    }
8194	  if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8195	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8196
8197	  if (!non_constant_p)
8198	    initializer = fold_non_dependent_expr (initializer);
8199
8200	  /* Process the initializer.  */
8201	  cp_finish_decl (decl,
8202			  initializer, !non_constant_p,
8203			  asm_specification,
8204			  flags);
8205
8206	  if (pushed_scope)
8207	    pop_scope (pushed_scope);
8208
8209	  return convert_from_reference (decl);
8210	}
8211    }
8212  /* If we didn't even get past the declarator successfully, we are
8213     definitely not looking at a declaration.  */
8214  else
8215    cp_parser_abort_tentative_parse (parser);
8216
8217  /* Otherwise, we are looking at an expression.  */
8218  return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8219}
8220
8221/* Parse an iteration-statement.
8222
8223   iteration-statement:
8224     while ( condition ) statement
8225     do statement while ( expression ) ;
8226     for ( for-init-statement condition [opt] ; expression [opt] )
8227       statement
8228
8229   Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8230
8231static tree
8232cp_parser_iteration_statement (cp_parser* parser)
8233{
8234  cp_token *token;
8235  enum rid keyword;
8236  tree statement;
8237  unsigned char in_statement;
8238
8239  /* Peek at the next token.  */
8240  token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8241  if (!token)
8242    return error_mark_node;
8243
8244  /* Remember whether or not we are already within an iteration
8245     statement.  */
8246  in_statement = parser->in_statement;
8247
8248  /* See what kind of keyword it is.  */
8249  keyword = token->keyword;
8250  switch (keyword)
8251    {
8252    case RID_WHILE:
8253      {
8254	tree condition;
8255
8256	/* Begin the while-statement.  */
8257	statement = begin_while_stmt ();
8258	/* Look for the `('.  */
8259	cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8260	/* Parse the condition.  */
8261	condition = cp_parser_condition (parser);
8262	finish_while_stmt_cond (condition, statement);
8263	/* Look for the `)'.  */
8264	cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8265	/* Parse the dependent statement.  */
8266	parser->in_statement = IN_ITERATION_STMT;
8267	cp_parser_already_scoped_statement (parser);
8268	parser->in_statement = in_statement;
8269	/* We're done with the while-statement.  */
8270	finish_while_stmt (statement);
8271      }
8272      break;
8273
8274    case RID_DO:
8275      {
8276	tree expression;
8277
8278	/* Begin the do-statement.  */
8279	statement = begin_do_stmt ();
8280	/* Parse the body of the do-statement.  */
8281	parser->in_statement = IN_ITERATION_STMT;
8282	cp_parser_implicitly_scoped_statement (parser, NULL);
8283	parser->in_statement = in_statement;
8284	finish_do_body (statement);
8285	/* Look for the `while' keyword.  */
8286	cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8287	/* Look for the `('.  */
8288	cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8289	/* Parse the expression.  */
8290	expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8291	/* We're done with the do-statement.  */
8292	finish_do_stmt (expression, statement);
8293	/* Look for the `)'.  */
8294	cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8295	/* Look for the `;'.  */
8296	cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8297      }
8298      break;
8299
8300    case RID_FOR:
8301      {
8302	tree condition = NULL_TREE;
8303	tree expression = NULL_TREE;
8304
8305	/* Begin the for-statement.  */
8306	statement = begin_for_stmt ();
8307	/* Look for the `('.  */
8308	cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8309	/* Parse the initialization.  */
8310	cp_parser_for_init_statement (parser);
8311	finish_for_init_stmt (statement);
8312
8313	/* If there's a condition, process it.  */
8314	if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8315	  condition = cp_parser_condition (parser);
8316	finish_for_cond (condition, statement);
8317	/* Look for the `;'.  */
8318	cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8319
8320	/* If there's an expression, process it.  */
8321	if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8322	  expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8323	finish_for_expr (expression, statement);
8324	/* Look for the `)'.  */
8325	cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8326
8327	/* Parse the body of the for-statement.  */
8328	parser->in_statement = IN_ITERATION_STMT;
8329	cp_parser_already_scoped_statement (parser);
8330	parser->in_statement = in_statement;
8331
8332	/* We're done with the for-statement.  */
8333	finish_for_stmt (statement);
8334      }
8335      break;
8336
8337    default:
8338      cp_parser_error (parser, "expected iteration-statement");
8339      statement = error_mark_node;
8340      break;
8341    }
8342
8343  return statement;
8344}
8345
8346/* Parse a for-init-statement.
8347
8348   for-init-statement:
8349     expression-statement
8350     simple-declaration  */
8351
8352static void
8353cp_parser_for_init_statement (cp_parser* parser)
8354{
8355  /* If the next token is a `;', then we have an empty
8356     expression-statement.  Grammatically, this is also a
8357     simple-declaration, but an invalid one, because it does not
8358     declare anything.  Therefore, if we did not handle this case
8359     specially, we would issue an error message about an invalid
8360     declaration.  */
8361  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8362    {
8363      /* We're going to speculatively look for a declaration, falling back
8364	 to an expression, if necessary.  */
8365      cp_parser_parse_tentatively (parser);
8366      /* Parse the declaration.  */
8367      cp_parser_simple_declaration (parser,
8368				    /*function_definition_allowed_p=*/false);
8369      /* If the tentative parse failed, then we shall need to look for an
8370	 expression-statement.  */
8371      if (cp_parser_parse_definitely (parser))
8372	return;
8373    }
8374
8375  cp_parser_expression_statement (parser, NULL_TREE);
8376}
8377
8378/* Parse a jump-statement.
8379
8380   jump-statement:
8381     break ;
8382     continue ;
8383     return expression [opt] ;
8384     return braced-init-list ;
8385     goto identifier ;
8386
8387   GNU extension:
8388
8389   jump-statement:
8390     goto * expression ;
8391
8392   Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8393
8394static tree
8395cp_parser_jump_statement (cp_parser* parser)
8396{
8397  tree statement = error_mark_node;
8398  cp_token *token;
8399  enum rid keyword;
8400  unsigned char in_statement;
8401
8402  /* Peek at the next token.  */
8403  token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8404  if (!token)
8405    return error_mark_node;
8406
8407  /* See what kind of keyword it is.  */
8408  keyword = token->keyword;
8409  switch (keyword)
8410    {
8411    case RID_BREAK:
8412      in_statement = parser->in_statement & ~IN_IF_STMT;
8413      switch (in_statement)
8414	{
8415	case 0:
8416	  error_at (token->location, "break statement not within loop or switch");
8417	  break;
8418	default:
8419	  gcc_assert ((in_statement & IN_SWITCH_STMT)
8420		      || in_statement == IN_ITERATION_STMT);
8421	  statement = finish_break_stmt ();
8422	  break;
8423	case IN_OMP_BLOCK:
8424	  error_at (token->location, "invalid exit from OpenMP structured block");
8425	  break;
8426	case IN_OMP_FOR:
8427	  error_at (token->location, "break statement used with OpenMP for loop");
8428	  break;
8429	}
8430      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8431      break;
8432
8433    case RID_CONTINUE:
8434      switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8435	{
8436	case 0:
8437	  error_at (token->location, "continue statement not within a loop");
8438	  break;
8439	case IN_ITERATION_STMT:
8440	case IN_OMP_FOR:
8441	  statement = finish_continue_stmt ();
8442	  break;
8443	case IN_OMP_BLOCK:
8444	  error_at (token->location, "invalid exit from OpenMP structured block");
8445	  break;
8446	default:
8447	  gcc_unreachable ();
8448	}
8449      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8450      break;
8451
8452    case RID_RETURN:
8453      {
8454	tree expr;
8455	bool expr_non_constant_p;
8456
8457	if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8458	  {
8459	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8460	    expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8461	  }
8462	else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8463	  expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8464	else
8465	  /* If the next token is a `;', then there is no
8466	     expression.  */
8467	  expr = NULL_TREE;
8468	/* Build the return-statement.  */
8469	statement = finish_return_stmt (expr);
8470	/* Look for the final `;'.  */
8471	cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8472      }
8473      break;
8474
8475    case RID_GOTO:
8476      /* Create the goto-statement.  */
8477      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8478	{
8479	  /* Issue a warning about this use of a GNU extension.  */
8480	  pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8481	  /* Consume the '*' token.  */
8482	  cp_lexer_consume_token (parser->lexer);
8483	  /* Parse the dependent expression.  */
8484	  finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8485	}
8486      else
8487	finish_goto_stmt (cp_parser_identifier (parser));
8488      /* Look for the final `;'.  */
8489      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8490      break;
8491
8492    default:
8493      cp_parser_error (parser, "expected jump-statement");
8494      break;
8495    }
8496
8497  return statement;
8498}
8499
8500/* Parse a declaration-statement.
8501
8502   declaration-statement:
8503     block-declaration  */
8504
8505static void
8506cp_parser_declaration_statement (cp_parser* parser)
8507{
8508  void *p;
8509
8510  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8511  p = obstack_alloc (&declarator_obstack, 0);
8512
8513 /* Parse the block-declaration.  */
8514  cp_parser_block_declaration (parser, /*statement_p=*/true);
8515
8516  /* Free any declarators allocated.  */
8517  obstack_free (&declarator_obstack, p);
8518
8519  /* Finish off the statement.  */
8520  finish_stmt ();
8521}
8522
8523/* Some dependent statements (like `if (cond) statement'), are
8524   implicitly in their own scope.  In other words, if the statement is
8525   a single statement (as opposed to a compound-statement), it is
8526   none-the-less treated as if it were enclosed in braces.  Any
8527   declarations appearing in the dependent statement are out of scope
8528   after control passes that point.  This function parses a statement,
8529   but ensures that is in its own scope, even if it is not a
8530   compound-statement.
8531
8532   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8533   is a (possibly labeled) if statement which is not enclosed in
8534   braces and has an else clause.  This is used to implement
8535   -Wparentheses.
8536
8537   Returns the new statement.  */
8538
8539static tree
8540cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8541{
8542  tree statement;
8543
8544  if (if_p != NULL)
8545    *if_p = false;
8546
8547  /* Mark if () ; with a special NOP_EXPR.  */
8548  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8549    {
8550      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8551      cp_lexer_consume_token (parser->lexer);
8552      statement = add_stmt (build_empty_stmt (loc));
8553    }
8554  /* if a compound is opened, we simply parse the statement directly.  */
8555  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8556    statement = cp_parser_compound_statement (parser, NULL, false);
8557  /* If the token is not a `{', then we must take special action.  */
8558  else
8559    {
8560      /* Create a compound-statement.  */
8561      statement = begin_compound_stmt (0);
8562      /* Parse the dependent-statement.  */
8563      cp_parser_statement (parser, NULL_TREE, false, if_p);
8564      /* Finish the dummy compound-statement.  */
8565      finish_compound_stmt (statement);
8566    }
8567
8568  /* Return the statement.  */
8569  return statement;
8570}
8571
8572/* For some dependent statements (like `while (cond) statement'), we
8573   have already created a scope.  Therefore, even if the dependent
8574   statement is a compound-statement, we do not want to create another
8575   scope.  */
8576
8577static void
8578cp_parser_already_scoped_statement (cp_parser* parser)
8579{
8580  /* If the token is a `{', then we must take special action.  */
8581  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8582    cp_parser_statement (parser, NULL_TREE, false, NULL);
8583  else
8584    {
8585      /* Avoid calling cp_parser_compound_statement, so that we
8586	 don't create a new scope.  Do everything else by hand.  */
8587      cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8588      /* If the next keyword is `__label__' we have a label declaration.  */
8589      while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8590	cp_parser_label_declaration (parser);
8591      /* Parse an (optional) statement-seq.  */
8592      cp_parser_statement_seq_opt (parser, NULL_TREE);
8593      cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8594    }
8595}
8596
8597/* Declarations [gram.dcl.dcl] */
8598
8599/* Parse an optional declaration-sequence.
8600
8601   declaration-seq:
8602     declaration
8603     declaration-seq declaration  */
8604
8605static void
8606cp_parser_declaration_seq_opt (cp_parser* parser)
8607{
8608  while (true)
8609    {
8610      cp_token *token;
8611
8612      token = cp_lexer_peek_token (parser->lexer);
8613
8614      if (token->type == CPP_CLOSE_BRACE
8615	  || token->type == CPP_EOF
8616	  || token->type == CPP_PRAGMA_EOL)
8617	break;
8618
8619      if (token->type == CPP_SEMICOLON)
8620	{
8621	  /* A declaration consisting of a single semicolon is
8622	     invalid.  Allow it unless we're being pedantic.  */
8623	  cp_lexer_consume_token (parser->lexer);
8624	  if (!in_system_header)
8625	    pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8626	  continue;
8627	}
8628
8629      /* If we're entering or exiting a region that's implicitly
8630	 extern "C", modify the lang context appropriately.  */
8631      if (!parser->implicit_extern_c && token->implicit_extern_c)
8632	{
8633	  push_lang_context (lang_name_c);
8634	  parser->implicit_extern_c = true;
8635	}
8636      else if (parser->implicit_extern_c && !token->implicit_extern_c)
8637	{
8638	  pop_lang_context ();
8639	  parser->implicit_extern_c = false;
8640	}
8641
8642      if (token->type == CPP_PRAGMA)
8643	{
8644	  /* A top-level declaration can consist solely of a #pragma.
8645	     A nested declaration cannot, so this is done here and not
8646	     in cp_parser_declaration.  (A #pragma at block scope is
8647	     handled in cp_parser_statement.)  */
8648	  cp_parser_pragma (parser, pragma_external);
8649	  continue;
8650	}
8651
8652      /* Parse the declaration itself.  */
8653      cp_parser_declaration (parser);
8654    }
8655}
8656
8657/* Parse a declaration.
8658
8659   declaration:
8660     block-declaration
8661     function-definition
8662     template-declaration
8663     explicit-instantiation
8664     explicit-specialization
8665     linkage-specification
8666     namespace-definition
8667
8668   GNU extension:
8669
8670   declaration:
8671      __extension__ declaration */
8672
8673static void
8674cp_parser_declaration (cp_parser* parser)
8675{
8676  cp_token token1;
8677  cp_token token2;
8678  int saved_pedantic;
8679  void *p;
8680
8681  /* Check for the `__extension__' keyword.  */
8682  if (cp_parser_extension_opt (parser, &saved_pedantic))
8683    {
8684      /* Parse the qualified declaration.  */
8685      cp_parser_declaration (parser);
8686      /* Restore the PEDANTIC flag.  */
8687      pedantic = saved_pedantic;
8688
8689      return;
8690    }
8691
8692  /* Try to figure out what kind of declaration is present.  */
8693  token1 = *cp_lexer_peek_token (parser->lexer);
8694
8695  if (token1.type != CPP_EOF)
8696    token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8697  else
8698    {
8699      token2.type = CPP_EOF;
8700      token2.keyword = RID_MAX;
8701    }
8702
8703  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8704  p = obstack_alloc (&declarator_obstack, 0);
8705
8706  /* If the next token is `extern' and the following token is a string
8707     literal, then we have a linkage specification.  */
8708  if (token1.keyword == RID_EXTERN
8709      && cp_parser_is_string_literal (&token2))
8710    cp_parser_linkage_specification (parser);
8711  /* If the next token is `template', then we have either a template
8712     declaration, an explicit instantiation, or an explicit
8713     specialization.  */
8714  else if (token1.keyword == RID_TEMPLATE)
8715    {
8716      /* `template <>' indicates a template specialization.  */
8717      if (token2.type == CPP_LESS
8718	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8719	cp_parser_explicit_specialization (parser);
8720      /* `template <' indicates a template declaration.  */
8721      else if (token2.type == CPP_LESS)
8722	cp_parser_template_declaration (parser, /*member_p=*/false);
8723      /* Anything else must be an explicit instantiation.  */
8724      else
8725	cp_parser_explicit_instantiation (parser);
8726    }
8727  /* If the next token is `export', then we have a template
8728     declaration.  */
8729  else if (token1.keyword == RID_EXPORT)
8730    cp_parser_template_declaration (parser, /*member_p=*/false);
8731  /* If the next token is `extern', 'static' or 'inline' and the one
8732     after that is `template', we have a GNU extended explicit
8733     instantiation directive.  */
8734  else if (cp_parser_allow_gnu_extensions_p (parser)
8735	   && (token1.keyword == RID_EXTERN
8736	       || token1.keyword == RID_STATIC
8737	       || token1.keyword == RID_INLINE)
8738	   && token2.keyword == RID_TEMPLATE)
8739    cp_parser_explicit_instantiation (parser);
8740  /* If the next token is `namespace', check for a named or unnamed
8741     namespace definition.  */
8742  else if (token1.keyword == RID_NAMESPACE
8743	   && (/* A named namespace definition.  */
8744	       (token2.type == CPP_NAME
8745		&& (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8746		    != CPP_EQ))
8747	       /* An unnamed namespace definition.  */
8748	       || token2.type == CPP_OPEN_BRACE
8749	       || token2.keyword == RID_ATTRIBUTE))
8750    cp_parser_namespace_definition (parser);
8751  /* An inline (associated) namespace definition.  */
8752  else if (token1.keyword == RID_INLINE
8753	   && token2.keyword == RID_NAMESPACE)
8754    cp_parser_namespace_definition (parser);
8755  /* Objective-C++ declaration/definition.  */
8756  else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8757    cp_parser_objc_declaration (parser);
8758  /* We must have either a block declaration or a function
8759     definition.  */
8760  else
8761    /* Try to parse a block-declaration, or a function-definition.  */
8762    cp_parser_block_declaration (parser, /*statement_p=*/false);
8763
8764  /* Free any declarators allocated.  */
8765  obstack_free (&declarator_obstack, p);
8766}
8767
8768/* Parse a block-declaration.
8769
8770   block-declaration:
8771     simple-declaration
8772     asm-definition
8773     namespace-alias-definition
8774     using-declaration
8775     using-directive
8776
8777   GNU Extension:
8778
8779   block-declaration:
8780     __extension__ block-declaration
8781
8782   C++0x Extension:
8783
8784   block-declaration:
8785     static_assert-declaration
8786
8787   If STATEMENT_P is TRUE, then this block-declaration is occurring as
8788   part of a declaration-statement.  */
8789
8790static void
8791cp_parser_block_declaration (cp_parser *parser,
8792			     bool      statement_p)
8793{
8794  cp_token *token1;
8795  int saved_pedantic;
8796
8797  /* Check for the `__extension__' keyword.  */
8798  if (cp_parser_extension_opt (parser, &saved_pedantic))
8799    {
8800      /* Parse the qualified declaration.  */
8801      cp_parser_block_declaration (parser, statement_p);
8802      /* Restore the PEDANTIC flag.  */
8803      pedantic = saved_pedantic;
8804
8805      return;
8806    }
8807
8808  /* Peek at the next token to figure out which kind of declaration is
8809     present.  */
8810  token1 = cp_lexer_peek_token (parser->lexer);
8811
8812  /* If the next keyword is `asm', we have an asm-definition.  */
8813  if (token1->keyword == RID_ASM)
8814    {
8815      if (statement_p)
8816	cp_parser_commit_to_tentative_parse (parser);
8817      cp_parser_asm_definition (parser);
8818    }
8819  /* If the next keyword is `namespace', we have a
8820     namespace-alias-definition.  */
8821  else if (token1->keyword == RID_NAMESPACE)
8822    cp_parser_namespace_alias_definition (parser);
8823  /* If the next keyword is `using', we have either a
8824     using-declaration or a using-directive.  */
8825  else if (token1->keyword == RID_USING)
8826    {
8827      cp_token *token2;
8828
8829      if (statement_p)
8830	cp_parser_commit_to_tentative_parse (parser);
8831      /* If the token after `using' is `namespace', then we have a
8832	 using-directive.  */
8833      token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8834      if (token2->keyword == RID_NAMESPACE)
8835	cp_parser_using_directive (parser);
8836      /* Otherwise, it's a using-declaration.  */
8837      else
8838	cp_parser_using_declaration (parser,
8839				     /*access_declaration_p=*/false);
8840    }
8841  /* If the next keyword is `__label__' we have a misplaced label
8842     declaration.  */
8843  else if (token1->keyword == RID_LABEL)
8844    {
8845      cp_lexer_consume_token (parser->lexer);
8846      error_at (token1->location, "%<__label__%> not at the beginning of a block");
8847      cp_parser_skip_to_end_of_statement (parser);
8848      /* If the next token is now a `;', consume it.  */
8849      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8850	cp_lexer_consume_token (parser->lexer);
8851    }
8852  /* If the next token is `static_assert' we have a static assertion.  */
8853  else if (token1->keyword == RID_STATIC_ASSERT)
8854    cp_parser_static_assert (parser, /*member_p=*/false);
8855  /* Anything else must be a simple-declaration.  */
8856  else
8857    cp_parser_simple_declaration (parser, !statement_p);
8858}
8859
8860/* Parse a simple-declaration.
8861
8862   simple-declaration:
8863     decl-specifier-seq [opt] init-declarator-list [opt] ;
8864
8865   init-declarator-list:
8866     init-declarator
8867     init-declarator-list , init-declarator
8868
8869   If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8870   function-definition as a simple-declaration.  */
8871
8872static void
8873cp_parser_simple_declaration (cp_parser* parser,
8874			      bool function_definition_allowed_p)
8875{
8876  cp_decl_specifier_seq decl_specifiers;
8877  int declares_class_or_enum;
8878  bool saw_declarator;
8879
8880  /* Defer access checks until we know what is being declared; the
8881     checks for names appearing in the decl-specifier-seq should be
8882     done as if we were in the scope of the thing being declared.  */
8883  push_deferring_access_checks (dk_deferred);
8884
8885  /* Parse the decl-specifier-seq.  We have to keep track of whether
8886     or not the decl-specifier-seq declares a named class or
8887     enumeration type, since that is the only case in which the
8888     init-declarator-list is allowed to be empty.
8889
8890     [dcl.dcl]
8891
8892     In a simple-declaration, the optional init-declarator-list can be
8893     omitted only when declaring a class or enumeration, that is when
8894     the decl-specifier-seq contains either a class-specifier, an
8895     elaborated-type-specifier, or an enum-specifier.  */
8896  cp_parser_decl_specifier_seq (parser,
8897				CP_PARSER_FLAGS_OPTIONAL,
8898				&decl_specifiers,
8899				&declares_class_or_enum);
8900  /* We no longer need to defer access checks.  */
8901  stop_deferring_access_checks ();
8902
8903  /* In a block scope, a valid declaration must always have a
8904     decl-specifier-seq.  By not trying to parse declarators, we can
8905     resolve the declaration/expression ambiguity more quickly.  */
8906  if (!function_definition_allowed_p
8907      && !decl_specifiers.any_specifiers_p)
8908    {
8909      cp_parser_error (parser, "expected declaration");
8910      goto done;
8911    }
8912
8913  /* If the next two tokens are both identifiers, the code is
8914     erroneous. The usual cause of this situation is code like:
8915
8916       T t;
8917
8918     where "T" should name a type -- but does not.  */
8919  if (!decl_specifiers.any_type_specifiers_p
8920      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8921    {
8922      /* If parsing tentatively, we should commit; we really are
8923	 looking at a declaration.  */
8924      cp_parser_commit_to_tentative_parse (parser);
8925      /* Give up.  */
8926      goto done;
8927    }
8928
8929  /* If we have seen at least one decl-specifier, and the next token
8930     is not a parenthesis, then we must be looking at a declaration.
8931     (After "int (" we might be looking at a functional cast.)  */
8932  if (decl_specifiers.any_specifiers_p
8933      && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8934      && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8935      && !cp_parser_error_occurred (parser))
8936    cp_parser_commit_to_tentative_parse (parser);
8937
8938  /* Keep going until we hit the `;' at the end of the simple
8939     declaration.  */
8940  saw_declarator = false;
8941  while (cp_lexer_next_token_is_not (parser->lexer,
8942				     CPP_SEMICOLON))
8943    {
8944      cp_token *token;
8945      bool function_definition_p;
8946      tree decl;
8947
8948      if (saw_declarator)
8949	{
8950	  /* If we are processing next declarator, coma is expected */
8951	  token = cp_lexer_peek_token (parser->lexer);
8952	  gcc_assert (token->type == CPP_COMMA);
8953	  cp_lexer_consume_token (parser->lexer);
8954	}
8955      else
8956	saw_declarator = true;
8957
8958      /* Parse the init-declarator.  */
8959      decl = cp_parser_init_declarator (parser, &decl_specifiers,
8960					/*checks=*/NULL,
8961					function_definition_allowed_p,
8962					/*member_p=*/false,
8963					declares_class_or_enum,
8964					&function_definition_p);
8965      /* If an error occurred while parsing tentatively, exit quickly.
8966	 (That usually happens when in the body of a function; each
8967	 statement is treated as a declaration-statement until proven
8968	 otherwise.)  */
8969      if (cp_parser_error_occurred (parser))
8970	goto done;
8971      /* Handle function definitions specially.  */
8972      if (function_definition_p)
8973	{
8974	  /* If the next token is a `,', then we are probably
8975	     processing something like:
8976
8977	       void f() {}, *p;
8978
8979	     which is erroneous.  */
8980	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8981	    {
8982	      cp_token *token = cp_lexer_peek_token (parser->lexer);
8983	      error_at (token->location,
8984			"mixing"
8985			" declarations and function-definitions is forbidden");
8986	    }
8987	  /* Otherwise, we're done with the list of declarators.  */
8988	  else
8989	    {
8990	      pop_deferring_access_checks ();
8991	      return;
8992	    }
8993	}
8994      /* The next token should be either a `,' or a `;'.  */
8995      token = cp_lexer_peek_token (parser->lexer);
8996      /* If it's a `,', there are more declarators to come.  */
8997      if (token->type == CPP_COMMA)
8998	/* will be consumed next time around */;
8999      /* If it's a `;', we are done.  */
9000      else if (token->type == CPP_SEMICOLON)
9001	break;
9002      /* Anything else is an error.  */
9003      else
9004	{
9005	  /* If we have already issued an error message we don't need
9006	     to issue another one.  */
9007	  if (decl != error_mark_node
9008	      || cp_parser_uncommitted_to_tentative_parse_p (parser))
9009	    cp_parser_error (parser, "expected %<,%> or %<;%>");
9010	  /* Skip tokens until we reach the end of the statement.  */
9011	  cp_parser_skip_to_end_of_statement (parser);
9012	  /* If the next token is now a `;', consume it.  */
9013	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9014	    cp_lexer_consume_token (parser->lexer);
9015	  goto done;
9016	}
9017      /* After the first time around, a function-definition is not
9018	 allowed -- even if it was OK at first.  For example:
9019
9020	   int i, f() {}
9021
9022	 is not valid.  */
9023      function_definition_allowed_p = false;
9024    }
9025
9026  /* Issue an error message if no declarators are present, and the
9027     decl-specifier-seq does not itself declare a class or
9028     enumeration.  */
9029  if (!saw_declarator)
9030    {
9031      if (cp_parser_declares_only_class_p (parser))
9032	shadow_tag (&decl_specifiers);
9033      /* Perform any deferred access checks.  */
9034      perform_deferred_access_checks ();
9035    }
9036
9037  /* Consume the `;'.  */
9038  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9039
9040 done:
9041  pop_deferring_access_checks ();
9042}
9043
9044/* Parse a decl-specifier-seq.
9045
9046   decl-specifier-seq:
9047     decl-specifier-seq [opt] decl-specifier
9048
9049   decl-specifier:
9050     storage-class-specifier
9051     type-specifier
9052     function-specifier
9053     friend
9054     typedef
9055
9056   GNU Extension:
9057
9058   decl-specifier:
9059     attributes
9060
9061   Set *DECL_SPECS to a representation of the decl-specifier-seq.
9062
9063   The parser flags FLAGS is used to control type-specifier parsing.
9064
9065   *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9066   flags:
9067
9068     1: one of the decl-specifiers is an elaborated-type-specifier
9069	(i.e., a type declaration)
9070     2: one of the decl-specifiers is an enum-specifier or a
9071	class-specifier (i.e., a type definition)
9072
9073   */
9074
9075static void
9076cp_parser_decl_specifier_seq (cp_parser* parser,
9077			      cp_parser_flags flags,
9078			      cp_decl_specifier_seq *decl_specs,
9079			      int* declares_class_or_enum)
9080{
9081  bool constructor_possible_p = !parser->in_declarator_p;
9082  cp_token *start_token = NULL;
9083
9084  /* Clear DECL_SPECS.  */
9085  clear_decl_specs (decl_specs);
9086
9087  /* Assume no class or enumeration type is declared.  */
9088  *declares_class_or_enum = 0;
9089
9090  /* Keep reading specifiers until there are no more to read.  */
9091  while (true)
9092    {
9093      bool constructor_p;
9094      bool found_decl_spec;
9095      cp_token *token;
9096
9097      /* Peek at the next token.  */
9098      token = cp_lexer_peek_token (parser->lexer);
9099
9100      /* Save the first token of the decl spec list for error
9101         reporting.  */
9102      if (!start_token)
9103	start_token = token;
9104      /* Handle attributes.  */
9105      if (token->keyword == RID_ATTRIBUTE)
9106	{
9107	  /* Parse the attributes.  */
9108	  decl_specs->attributes
9109	    = chainon (decl_specs->attributes,
9110		       cp_parser_attributes_opt (parser));
9111	  continue;
9112	}
9113      /* Assume we will find a decl-specifier keyword.  */
9114      found_decl_spec = true;
9115      /* If the next token is an appropriate keyword, we can simply
9116	 add it to the list.  */
9117      switch (token->keyword)
9118	{
9119	  /* decl-specifier:
9120	       friend
9121               constexpr */
9122	case RID_FRIEND:
9123	  if (!at_class_scope_p ())
9124	    {
9125	      error_at (token->location, "%<friend%> used outside of class");
9126	      cp_lexer_purge_token (parser->lexer);
9127	    }
9128	  else
9129	    {
9130	      ++decl_specs->specs[(int) ds_friend];
9131	      /* Consume the token.  */
9132	      cp_lexer_consume_token (parser->lexer);
9133	    }
9134	  break;
9135
9136        case RID_CONSTEXPR:
9137          ++decl_specs->specs[(int) ds_constexpr];
9138          cp_lexer_consume_token (parser->lexer);
9139          break;
9140
9141	  /* function-specifier:
9142	       inline
9143	       virtual
9144	       explicit  */
9145	case RID_INLINE:
9146	case RID_VIRTUAL:
9147	case RID_EXPLICIT:
9148	  cp_parser_function_specifier_opt (parser, decl_specs);
9149	  break;
9150
9151	  /* decl-specifier:
9152	       typedef  */
9153	case RID_TYPEDEF:
9154	  ++decl_specs->specs[(int) ds_typedef];
9155	  /* Consume the token.  */
9156	  cp_lexer_consume_token (parser->lexer);
9157	  /* A constructor declarator cannot appear in a typedef.  */
9158	  constructor_possible_p = false;
9159	  /* The "typedef" keyword can only occur in a declaration; we
9160	     may as well commit at this point.  */
9161	  cp_parser_commit_to_tentative_parse (parser);
9162
9163          if (decl_specs->storage_class != sc_none)
9164            decl_specs->conflicting_specifiers_p = true;
9165	  break;
9166
9167	  /* storage-class-specifier:
9168	       auto
9169	       register
9170	       static
9171	       extern
9172	       mutable
9173
9174	     GNU Extension:
9175	       thread  */
9176	case RID_AUTO:
9177          if (cxx_dialect == cxx98)
9178            {
9179	      /* Consume the token.  */
9180	      cp_lexer_consume_token (parser->lexer);
9181
9182              /* Complain about `auto' as a storage specifier, if
9183                 we're complaining about C++0x compatibility.  */
9184              warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9185			  " will change meaning in C++0x; please remove it");
9186
9187              /* Set the storage class anyway.  */
9188              cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9189					   token->location);
9190            }
9191          else
9192	    /* C++0x auto type-specifier.  */
9193	    found_decl_spec = false;
9194          break;
9195
9196	case RID_REGISTER:
9197	case RID_STATIC:
9198	case RID_EXTERN:
9199	case RID_MUTABLE:
9200	  /* Consume the token.  */
9201	  cp_lexer_consume_token (parser->lexer);
9202          cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9203				       token->location);
9204	  break;
9205	case RID_THREAD:
9206	  /* Consume the token.  */
9207	  cp_lexer_consume_token (parser->lexer);
9208	  ++decl_specs->specs[(int) ds_thread];
9209	  break;
9210
9211	default:
9212	  /* We did not yet find a decl-specifier yet.  */
9213	  found_decl_spec = false;
9214	  break;
9215	}
9216
9217      /* Constructors are a special case.  The `S' in `S()' is not a
9218	 decl-specifier; it is the beginning of the declarator.  */
9219      constructor_p
9220	= (!found_decl_spec
9221	   && constructor_possible_p
9222	   && (cp_parser_constructor_declarator_p
9223	       (parser, decl_specs->specs[(int) ds_friend] != 0)));
9224
9225      /* If we don't have a DECL_SPEC yet, then we must be looking at
9226	 a type-specifier.  */
9227      if (!found_decl_spec && !constructor_p)
9228	{
9229	  int decl_spec_declares_class_or_enum;
9230	  bool is_cv_qualifier;
9231	  tree type_spec;
9232
9233	  type_spec
9234	    = cp_parser_type_specifier (parser, flags,
9235					decl_specs,
9236					/*is_declaration=*/true,
9237					&decl_spec_declares_class_or_enum,
9238					&is_cv_qualifier);
9239	  *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9240
9241	  /* If this type-specifier referenced a user-defined type
9242	     (a typedef, class-name, etc.), then we can't allow any
9243	     more such type-specifiers henceforth.
9244
9245	     [dcl.spec]
9246
9247	     The longest sequence of decl-specifiers that could
9248	     possibly be a type name is taken as the
9249	     decl-specifier-seq of a declaration.  The sequence shall
9250	     be self-consistent as described below.
9251
9252	     [dcl.type]
9253
9254	     As a general rule, at most one type-specifier is allowed
9255	     in the complete decl-specifier-seq of a declaration.  The
9256	     only exceptions are the following:
9257
9258	     -- const or volatile can be combined with any other
9259		type-specifier.
9260
9261	     -- signed or unsigned can be combined with char, long,
9262		short, or int.
9263
9264	     -- ..
9265
9266	     Example:
9267
9268	       typedef char* Pc;
9269	       void g (const int Pc);
9270
9271	     Here, Pc is *not* part of the decl-specifier seq; it's
9272	     the declarator.  Therefore, once we see a type-specifier
9273	     (other than a cv-qualifier), we forbid any additional
9274	     user-defined types.  We *do* still allow things like `int
9275	     int' to be considered a decl-specifier-seq, and issue the
9276	     error message later.  */
9277	  if (type_spec && !is_cv_qualifier)
9278	    flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9279	  /* A constructor declarator cannot follow a type-specifier.  */
9280	  if (type_spec)
9281	    {
9282	      constructor_possible_p = false;
9283	      found_decl_spec = true;
9284	      if (!is_cv_qualifier)
9285		decl_specs->any_type_specifiers_p = true;
9286	    }
9287	}
9288
9289      /* If we still do not have a DECL_SPEC, then there are no more
9290	 decl-specifiers.  */
9291      if (!found_decl_spec)
9292	break;
9293
9294      decl_specs->any_specifiers_p = true;
9295      /* After we see one decl-specifier, further decl-specifiers are
9296	 always optional.  */
9297      flags |= CP_PARSER_FLAGS_OPTIONAL;
9298    }
9299
9300  cp_parser_check_decl_spec (decl_specs, start_token->location);
9301
9302  /* Don't allow a friend specifier with a class definition.  */
9303  if (decl_specs->specs[(int) ds_friend] != 0
9304      && (*declares_class_or_enum & 2))
9305    error_at (start_token->location,
9306	      "class definition may not be declared a friend");
9307}
9308
9309/* Parse an (optional) storage-class-specifier.
9310
9311   storage-class-specifier:
9312     auto
9313     register
9314     static
9315     extern
9316     mutable
9317
9318   GNU Extension:
9319
9320   storage-class-specifier:
9321     thread
9322
9323   Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9324
9325static tree
9326cp_parser_storage_class_specifier_opt (cp_parser* parser)
9327{
9328  switch (cp_lexer_peek_token (parser->lexer)->keyword)
9329    {
9330    case RID_AUTO:
9331      if (cxx_dialect != cxx98)
9332        return NULL_TREE;
9333      /* Fall through for C++98.  */
9334
9335    case RID_REGISTER:
9336    case RID_STATIC:
9337    case RID_EXTERN:
9338    case RID_MUTABLE:
9339    case RID_THREAD:
9340      /* Consume the token.  */
9341      return cp_lexer_consume_token (parser->lexer)->u.value;
9342
9343    default:
9344      return NULL_TREE;
9345    }
9346}
9347
9348/* Parse an (optional) function-specifier.
9349
9350   function-specifier:
9351     inline
9352     virtual
9353     explicit
9354
9355   Returns an IDENTIFIER_NODE corresponding to the keyword used.
9356   Updates DECL_SPECS, if it is non-NULL.  */
9357
9358static tree
9359cp_parser_function_specifier_opt (cp_parser* parser,
9360				  cp_decl_specifier_seq *decl_specs)
9361{
9362  cp_token *token = cp_lexer_peek_token (parser->lexer);
9363  switch (token->keyword)
9364    {
9365    case RID_INLINE:
9366      if (decl_specs)
9367	++decl_specs->specs[(int) ds_inline];
9368      break;
9369
9370    case RID_VIRTUAL:
9371      /* 14.5.2.3 [temp.mem]
9372
9373	 A member function template shall not be virtual.  */
9374      if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9375	error_at (token->location, "templates may not be %<virtual%>");
9376      else if (decl_specs)
9377	++decl_specs->specs[(int) ds_virtual];
9378      break;
9379
9380    case RID_EXPLICIT:
9381      if (decl_specs)
9382	++decl_specs->specs[(int) ds_explicit];
9383      break;
9384
9385    default:
9386      return NULL_TREE;
9387    }
9388
9389  /* Consume the token.  */
9390  return cp_lexer_consume_token (parser->lexer)->u.value;
9391}
9392
9393/* Parse a linkage-specification.
9394
9395   linkage-specification:
9396     extern string-literal { declaration-seq [opt] }
9397     extern string-literal declaration  */
9398
9399static void
9400cp_parser_linkage_specification (cp_parser* parser)
9401{
9402  tree linkage;
9403
9404  /* Look for the `extern' keyword.  */
9405  cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9406
9407  /* Look for the string-literal.  */
9408  linkage = cp_parser_string_literal (parser, false, false);
9409
9410  /* Transform the literal into an identifier.  If the literal is a
9411     wide-character string, or contains embedded NULs, then we can't
9412     handle it as the user wants.  */
9413  if (strlen (TREE_STRING_POINTER (linkage))
9414      != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9415    {
9416      cp_parser_error (parser, "invalid linkage-specification");
9417      /* Assume C++ linkage.  */
9418      linkage = lang_name_cplusplus;
9419    }
9420  else
9421    linkage = get_identifier (TREE_STRING_POINTER (linkage));
9422
9423  /* We're now using the new linkage.  */
9424  push_lang_context (linkage);
9425
9426  /* If the next token is a `{', then we're using the first
9427     production.  */
9428  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9429    {
9430      /* Consume the `{' token.  */
9431      cp_lexer_consume_token (parser->lexer);
9432      /* Parse the declarations.  */
9433      cp_parser_declaration_seq_opt (parser);
9434      /* Look for the closing `}'.  */
9435      cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9436    }
9437  /* Otherwise, there's just one declaration.  */
9438  else
9439    {
9440      bool saved_in_unbraced_linkage_specification_p;
9441
9442      saved_in_unbraced_linkage_specification_p
9443	= parser->in_unbraced_linkage_specification_p;
9444      parser->in_unbraced_linkage_specification_p = true;
9445      cp_parser_declaration (parser);
9446      parser->in_unbraced_linkage_specification_p
9447	= saved_in_unbraced_linkage_specification_p;
9448    }
9449
9450  /* We're done with the linkage-specification.  */
9451  pop_lang_context ();
9452}
9453
9454/* Parse a static_assert-declaration.
9455
9456   static_assert-declaration:
9457     static_assert ( constant-expression , string-literal ) ;
9458
9459   If MEMBER_P, this static_assert is a class member.  */
9460
9461static void
9462cp_parser_static_assert(cp_parser *parser, bool member_p)
9463{
9464  tree condition;
9465  tree message;
9466  cp_token *token;
9467  location_t saved_loc;
9468
9469  /* Peek at the `static_assert' token so we can keep track of exactly
9470     where the static assertion started.  */
9471  token = cp_lexer_peek_token (parser->lexer);
9472  saved_loc = token->location;
9473
9474  /* Look for the `static_assert' keyword.  */
9475  if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9476                                  "%<static_assert%>"))
9477    return;
9478
9479  /*  We know we are in a static assertion; commit to any tentative
9480      parse.  */
9481  if (cp_parser_parsing_tentatively (parser))
9482    cp_parser_commit_to_tentative_parse (parser);
9483
9484  /* Parse the `(' starting the static assertion condition.  */
9485  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9486
9487  /* Parse the constant-expression.  */
9488  condition =
9489    cp_parser_constant_expression (parser,
9490                                   /*allow_non_constant_p=*/false,
9491                                   /*non_constant_p=*/NULL);
9492
9493  /* Parse the separating `,'.  */
9494  cp_parser_require (parser, CPP_COMMA, "%<,%>");
9495
9496  /* Parse the string-literal message.  */
9497  message = cp_parser_string_literal (parser,
9498                                      /*translate=*/false,
9499                                      /*wide_ok=*/true);
9500
9501  /* A `)' completes the static assertion.  */
9502  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9503    cp_parser_skip_to_closing_parenthesis (parser,
9504                                           /*recovering=*/true,
9505                                           /*or_comma=*/false,
9506					   /*consume_paren=*/true);
9507
9508  /* A semicolon terminates the declaration.  */
9509  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9510
9511  /* Complete the static assertion, which may mean either processing
9512     the static assert now or saving it for template instantiation.  */
9513  finish_static_assert (condition, message, saved_loc, member_p);
9514}
9515
9516/* Parse a `decltype' type. Returns the type.
9517
9518   simple-type-specifier:
9519     decltype ( expression )  */
9520
9521static tree
9522cp_parser_decltype (cp_parser *parser)
9523{
9524  tree expr;
9525  bool id_expression_or_member_access_p = false;
9526  const char *saved_message;
9527  bool saved_integral_constant_expression_p;
9528  bool saved_non_integral_constant_expression_p;
9529  cp_token *id_expr_start_token;
9530
9531  /* Look for the `decltype' token.  */
9532  if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9533    return error_mark_node;
9534
9535  /* Types cannot be defined in a `decltype' expression.  Save away the
9536     old message.  */
9537  saved_message = parser->type_definition_forbidden_message;
9538
9539  /* And create the new one.  */
9540  parser->type_definition_forbidden_message
9541    = G_("types may not be defined in %<decltype%> expressions");
9542
9543  /* The restrictions on constant-expressions do not apply inside
9544     decltype expressions.  */
9545  saved_integral_constant_expression_p
9546    = parser->integral_constant_expression_p;
9547  saved_non_integral_constant_expression_p
9548    = parser->non_integral_constant_expression_p;
9549  parser->integral_constant_expression_p = false;
9550
9551  /* Do not actually evaluate the expression.  */
9552  ++cp_unevaluated_operand;
9553
9554  /* Do not warn about problems with the expression.  */
9555  ++c_inhibit_evaluation_warnings;
9556
9557  /* Parse the opening `('.  */
9558  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9559    return error_mark_node;
9560
9561  /* First, try parsing an id-expression.  */
9562  id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9563  cp_parser_parse_tentatively (parser);
9564  expr = cp_parser_id_expression (parser,
9565                                  /*template_keyword_p=*/false,
9566                                  /*check_dependency_p=*/true,
9567                                  /*template_p=*/NULL,
9568                                  /*declarator_p=*/false,
9569                                  /*optional_p=*/false);
9570
9571  if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9572    {
9573      bool non_integral_constant_expression_p = false;
9574      tree id_expression = expr;
9575      cp_id_kind idk;
9576      const char *error_msg;
9577
9578      if (TREE_CODE (expr) == IDENTIFIER_NODE)
9579	/* Lookup the name we got back from the id-expression.  */
9580	expr = cp_parser_lookup_name (parser, expr,
9581				      none_type,
9582				      /*is_template=*/false,
9583				      /*is_namespace=*/false,
9584				      /*check_dependency=*/true,
9585				      /*ambiguous_decls=*/NULL,
9586				      id_expr_start_token->location);
9587
9588      if (expr
9589          && expr != error_mark_node
9590          && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9591          && TREE_CODE (expr) != TYPE_DECL
9592	  && (TREE_CODE (expr) != BIT_NOT_EXPR
9593	      || !TYPE_P (TREE_OPERAND (expr, 0)))
9594          && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9595        {
9596          /* Complete lookup of the id-expression.  */
9597          expr = (finish_id_expression
9598                  (id_expression, expr, parser->scope, &idk,
9599                   /*integral_constant_expression_p=*/false,
9600                   /*allow_non_integral_constant_expression_p=*/true,
9601                   &non_integral_constant_expression_p,
9602                   /*template_p=*/false,
9603                   /*done=*/true,
9604                   /*address_p=*/false,
9605                   /*template_arg_p=*/false,
9606                   &error_msg,
9607		   id_expr_start_token->location));
9608
9609          if (expr == error_mark_node)
9610            /* We found an id-expression, but it was something that we
9611               should not have found. This is an error, not something
9612               we can recover from, so note that we found an
9613               id-expression and we'll recover as gracefully as
9614               possible.  */
9615            id_expression_or_member_access_p = true;
9616        }
9617
9618      if (expr
9619          && expr != error_mark_node
9620          && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9621        /* We have an id-expression.  */
9622        id_expression_or_member_access_p = true;
9623    }
9624
9625  if (!id_expression_or_member_access_p)
9626    {
9627      /* Abort the id-expression parse.  */
9628      cp_parser_abort_tentative_parse (parser);
9629
9630      /* Parsing tentatively, again.  */
9631      cp_parser_parse_tentatively (parser);
9632
9633      /* Parse a class member access.  */
9634      expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9635                                           /*cast_p=*/false,
9636                                           /*member_access_only_p=*/true, NULL);
9637
9638      if (expr
9639          && expr != error_mark_node
9640          && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9641        /* We have an id-expression.  */
9642        id_expression_or_member_access_p = true;
9643    }
9644
9645  if (id_expression_or_member_access_p)
9646    /* We have parsed the complete id-expression or member access.  */
9647    cp_parser_parse_definitely (parser);
9648  else
9649    {
9650      bool saved_greater_than_is_operator_p;
9651
9652      /* Abort our attempt to parse an id-expression or member access
9653         expression.  */
9654      cp_parser_abort_tentative_parse (parser);
9655
9656      /* Within a parenthesized expression, a `>' token is always
9657	 the greater-than operator.  */
9658      saved_greater_than_is_operator_p
9659	= parser->greater_than_is_operator_p;
9660      parser->greater_than_is_operator_p = true;
9661
9662      /* Parse a full expression.  */
9663      expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9664
9665      /* The `>' token might be the end of a template-id or
9666	 template-parameter-list now.  */
9667      parser->greater_than_is_operator_p
9668	= saved_greater_than_is_operator_p;
9669    }
9670
9671  /* Go back to evaluating expressions.  */
9672  --cp_unevaluated_operand;
9673  --c_inhibit_evaluation_warnings;
9674
9675  /* Restore the old message and the integral constant expression
9676     flags.  */
9677  parser->type_definition_forbidden_message = saved_message;
9678  parser->integral_constant_expression_p
9679    = saved_integral_constant_expression_p;
9680  parser->non_integral_constant_expression_p
9681    = saved_non_integral_constant_expression_p;
9682
9683  if (expr == error_mark_node)
9684    {
9685      /* Skip everything up to the closing `)'.  */
9686      cp_parser_skip_to_closing_parenthesis (parser, true, false,
9687                                             /*consume_paren=*/true);
9688      return error_mark_node;
9689    }
9690
9691  /* Parse to the closing `)'.  */
9692  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9693    {
9694      cp_parser_skip_to_closing_parenthesis (parser, true, false,
9695					     /*consume_paren=*/true);
9696      return error_mark_node;
9697    }
9698
9699  return finish_decltype_type (expr, id_expression_or_member_access_p);
9700}
9701
9702/* Special member functions [gram.special] */
9703
9704/* Parse a conversion-function-id.
9705
9706   conversion-function-id:
9707     operator conversion-type-id
9708
9709   Returns an IDENTIFIER_NODE representing the operator.  */
9710
9711static tree
9712cp_parser_conversion_function_id (cp_parser* parser)
9713{
9714  tree type;
9715  tree saved_scope;
9716  tree saved_qualifying_scope;
9717  tree saved_object_scope;
9718  tree pushed_scope = NULL_TREE;
9719
9720  /* Look for the `operator' token.  */
9721  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9722    return error_mark_node;
9723  /* When we parse the conversion-type-id, the current scope will be
9724     reset.  However, we need that information in able to look up the
9725     conversion function later, so we save it here.  */
9726  saved_scope = parser->scope;
9727  saved_qualifying_scope = parser->qualifying_scope;
9728  saved_object_scope = parser->object_scope;
9729  /* We must enter the scope of the class so that the names of
9730     entities declared within the class are available in the
9731     conversion-type-id.  For example, consider:
9732
9733       struct S {
9734	 typedef int I;
9735	 operator I();
9736       };
9737
9738       S::operator I() { ... }
9739
9740     In order to see that `I' is a type-name in the definition, we
9741     must be in the scope of `S'.  */
9742  if (saved_scope)
9743    pushed_scope = push_scope (saved_scope);
9744  /* Parse the conversion-type-id.  */
9745  type = cp_parser_conversion_type_id (parser);
9746  /* Leave the scope of the class, if any.  */
9747  if (pushed_scope)
9748    pop_scope (pushed_scope);
9749  /* Restore the saved scope.  */
9750  parser->scope = saved_scope;
9751  parser->qualifying_scope = saved_qualifying_scope;
9752  parser->object_scope = saved_object_scope;
9753  /* If the TYPE is invalid, indicate failure.  */
9754  if (type == error_mark_node)
9755    return error_mark_node;
9756  return mangle_conv_op_name_for_type (type);
9757}
9758
9759/* Parse a conversion-type-id:
9760
9761   conversion-type-id:
9762     type-specifier-seq conversion-declarator [opt]
9763
9764   Returns the TYPE specified.  */
9765
9766static tree
9767cp_parser_conversion_type_id (cp_parser* parser)
9768{
9769  tree attributes;
9770  cp_decl_specifier_seq type_specifiers;
9771  cp_declarator *declarator;
9772  tree type_specified;
9773
9774  /* Parse the attributes.  */
9775  attributes = cp_parser_attributes_opt (parser);
9776  /* Parse the type-specifiers.  */
9777  cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9778				/*is_trailing_return=*/false,
9779				&type_specifiers);
9780  /* If that didn't work, stop.  */
9781  if (type_specifiers.type == error_mark_node)
9782    return error_mark_node;
9783  /* Parse the conversion-declarator.  */
9784  declarator = cp_parser_conversion_declarator_opt (parser);
9785
9786  type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9787				    /*initialized=*/0, &attributes);
9788  if (attributes)
9789    cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9790
9791  /* Don't give this error when parsing tentatively.  This happens to
9792     work because we always parse this definitively once.  */
9793  if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9794      && type_uses_auto (type_specified))
9795    {
9796      error ("invalid use of %<auto%> in conversion operator");
9797      return error_mark_node;
9798    }
9799
9800  return type_specified;
9801}
9802
9803/* Parse an (optional) conversion-declarator.
9804
9805   conversion-declarator:
9806     ptr-operator conversion-declarator [opt]
9807
9808   */
9809
9810static cp_declarator *
9811cp_parser_conversion_declarator_opt (cp_parser* parser)
9812{
9813  enum tree_code code;
9814  tree class_type;
9815  cp_cv_quals cv_quals;
9816
9817  /* We don't know if there's a ptr-operator next, or not.  */
9818  cp_parser_parse_tentatively (parser);
9819  /* Try the ptr-operator.  */
9820  code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9821  /* If it worked, look for more conversion-declarators.  */
9822  if (cp_parser_parse_definitely (parser))
9823    {
9824      cp_declarator *declarator;
9825
9826      /* Parse another optional declarator.  */
9827      declarator = cp_parser_conversion_declarator_opt (parser);
9828
9829      return cp_parser_make_indirect_declarator
9830	(code, class_type, cv_quals, declarator);
9831   }
9832
9833  return NULL;
9834}
9835
9836/* Parse an (optional) ctor-initializer.
9837
9838   ctor-initializer:
9839     : mem-initializer-list
9840
9841   Returns TRUE iff the ctor-initializer was actually present.  */
9842
9843static bool
9844cp_parser_ctor_initializer_opt (cp_parser* parser)
9845{
9846  /* If the next token is not a `:', then there is no
9847     ctor-initializer.  */
9848  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9849    {
9850      /* Do default initialization of any bases and members.  */
9851      if (DECL_CONSTRUCTOR_P (current_function_decl))
9852	finish_mem_initializers (NULL_TREE);
9853
9854      return false;
9855    }
9856
9857  /* Consume the `:' token.  */
9858  cp_lexer_consume_token (parser->lexer);
9859  /* And the mem-initializer-list.  */
9860  cp_parser_mem_initializer_list (parser);
9861
9862  return true;
9863}
9864
9865/* Parse a mem-initializer-list.
9866
9867   mem-initializer-list:
9868     mem-initializer ... [opt]
9869     mem-initializer ... [opt] , mem-initializer-list  */
9870
9871static void
9872cp_parser_mem_initializer_list (cp_parser* parser)
9873{
9874  tree mem_initializer_list = NULL_TREE;
9875  cp_token *token = cp_lexer_peek_token (parser->lexer);
9876
9877  /* Let the semantic analysis code know that we are starting the
9878     mem-initializer-list.  */
9879  if (!DECL_CONSTRUCTOR_P (current_function_decl))
9880    error_at (token->location,
9881	      "only constructors take base initializers");
9882
9883  /* Loop through the list.  */
9884  while (true)
9885    {
9886      tree mem_initializer;
9887
9888      token = cp_lexer_peek_token (parser->lexer);
9889      /* Parse the mem-initializer.  */
9890      mem_initializer = cp_parser_mem_initializer (parser);
9891      /* If the next token is a `...', we're expanding member initializers. */
9892      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9893        {
9894          /* Consume the `...'. */
9895          cp_lexer_consume_token (parser->lexer);
9896
9897          /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9898             can be expanded but members cannot. */
9899          if (mem_initializer != error_mark_node
9900              && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9901            {
9902              error_at (token->location,
9903			"cannot expand initializer for member %<%D%>",
9904			TREE_PURPOSE (mem_initializer));
9905              mem_initializer = error_mark_node;
9906            }
9907
9908          /* Construct the pack expansion type. */
9909          if (mem_initializer != error_mark_node)
9910            mem_initializer = make_pack_expansion (mem_initializer);
9911        }
9912      /* Add it to the list, unless it was erroneous.  */
9913      if (mem_initializer != error_mark_node)
9914	{
9915	  TREE_CHAIN (mem_initializer) = mem_initializer_list;
9916	  mem_initializer_list = mem_initializer;
9917	}
9918      /* If the next token is not a `,', we're done.  */
9919      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9920	break;
9921      /* Consume the `,' token.  */
9922      cp_lexer_consume_token (parser->lexer);
9923    }
9924
9925  /* Perform semantic analysis.  */
9926  if (DECL_CONSTRUCTOR_P (current_function_decl))
9927    finish_mem_initializers (mem_initializer_list);
9928}
9929
9930/* Parse a mem-initializer.
9931
9932   mem-initializer:
9933     mem-initializer-id ( expression-list [opt] )
9934     mem-initializer-id braced-init-list
9935
9936   GNU extension:
9937
9938   mem-initializer:
9939     ( expression-list [opt] )
9940
9941   Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9942   class) or FIELD_DECL (for a non-static data member) to initialize;
9943   the TREE_VALUE is the expression-list.  An empty initialization
9944   list is represented by void_list_node.  */
9945
9946static tree
9947cp_parser_mem_initializer (cp_parser* parser)
9948{
9949  tree mem_initializer_id;
9950  tree expression_list;
9951  tree member;
9952  cp_token *token = cp_lexer_peek_token (parser->lexer);
9953
9954  /* Find out what is being initialized.  */
9955  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9956    {
9957      permerror (token->location,
9958		 "anachronistic old-style base class initializer");
9959      mem_initializer_id = NULL_TREE;
9960    }
9961  else
9962    {
9963      mem_initializer_id = cp_parser_mem_initializer_id (parser);
9964      if (mem_initializer_id == error_mark_node)
9965	return mem_initializer_id;
9966    }
9967  member = expand_member_init (mem_initializer_id);
9968  if (member && !DECL_P (member))
9969    in_base_initializer = 1;
9970
9971  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9972    {
9973      bool expr_non_constant_p;
9974      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9975      expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9976      CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9977      expression_list = build_tree_list (NULL_TREE, expression_list);
9978    }
9979  else
9980    {
9981      VEC(tree,gc)* vec;
9982      vec = cp_parser_parenthesized_expression_list (parser, false,
9983						     /*cast_p=*/false,
9984						     /*allow_expansion_p=*/true,
9985						     /*non_constant_p=*/NULL);
9986      if (vec == NULL)
9987	return error_mark_node;
9988      expression_list = build_tree_list_vec (vec);
9989      release_tree_vector (vec);
9990    }
9991
9992  if (expression_list == error_mark_node)
9993    return error_mark_node;
9994  if (!expression_list)
9995    expression_list = void_type_node;
9996
9997  in_base_initializer = 0;
9998
9999  return member ? build_tree_list (member, expression_list) : error_mark_node;
10000}
10001
10002/* Parse a mem-initializer-id.
10003
10004   mem-initializer-id:
10005     :: [opt] nested-name-specifier [opt] class-name
10006     identifier
10007
10008   Returns a TYPE indicating the class to be initializer for the first
10009   production.  Returns an IDENTIFIER_NODE indicating the data member
10010   to be initialized for the second production.  */
10011
10012static tree
10013cp_parser_mem_initializer_id (cp_parser* parser)
10014{
10015  bool global_scope_p;
10016  bool nested_name_specifier_p;
10017  bool template_p = false;
10018  tree id;
10019
10020  cp_token *token = cp_lexer_peek_token (parser->lexer);
10021
10022  /* `typename' is not allowed in this context ([temp.res]).  */
10023  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10024    {
10025      error_at (token->location,
10026		"keyword %<typename%> not allowed in this context (a qualified "
10027		"member initializer is implicitly a type)");
10028      cp_lexer_consume_token (parser->lexer);
10029    }
10030  /* Look for the optional `::' operator.  */
10031  global_scope_p
10032    = (cp_parser_global_scope_opt (parser,
10033				   /*current_scope_valid_p=*/false)
10034       != NULL_TREE);
10035  /* Look for the optional nested-name-specifier.  The simplest way to
10036     implement:
10037
10038       [temp.res]
10039
10040       The keyword `typename' is not permitted in a base-specifier or
10041       mem-initializer; in these contexts a qualified name that
10042       depends on a template-parameter is implicitly assumed to be a
10043       type name.
10044
10045     is to assume that we have seen the `typename' keyword at this
10046     point.  */
10047  nested_name_specifier_p
10048    = (cp_parser_nested_name_specifier_opt (parser,
10049					    /*typename_keyword_p=*/true,
10050					    /*check_dependency_p=*/true,
10051					    /*type_p=*/true,
10052					    /*is_declaration=*/true)
10053       != NULL_TREE);
10054  if (nested_name_specifier_p)
10055    template_p = cp_parser_optional_template_keyword (parser);
10056  /* If there is a `::' operator or a nested-name-specifier, then we
10057     are definitely looking for a class-name.  */
10058  if (global_scope_p || nested_name_specifier_p)
10059    return cp_parser_class_name (parser,
10060				 /*typename_keyword_p=*/true,
10061				 /*template_keyword_p=*/template_p,
10062				 typename_type,
10063				 /*check_dependency_p=*/true,
10064				 /*class_head_p=*/false,
10065				 /*is_declaration=*/true);
10066  /* Otherwise, we could also be looking for an ordinary identifier.  */
10067  cp_parser_parse_tentatively (parser);
10068  /* Try a class-name.  */
10069  id = cp_parser_class_name (parser,
10070			     /*typename_keyword_p=*/true,
10071			     /*template_keyword_p=*/false,
10072			     none_type,
10073			     /*check_dependency_p=*/true,
10074			     /*class_head_p=*/false,
10075			     /*is_declaration=*/true);
10076  /* If we found one, we're done.  */
10077  if (cp_parser_parse_definitely (parser))
10078    return id;
10079  /* Otherwise, look for an ordinary identifier.  */
10080  return cp_parser_identifier (parser);
10081}
10082
10083/* Overloading [gram.over] */
10084
10085/* Parse an operator-function-id.
10086
10087   operator-function-id:
10088     operator operator
10089
10090   Returns an IDENTIFIER_NODE for the operator which is a
10091   human-readable spelling of the identifier, e.g., `operator +'.  */
10092
10093static tree
10094cp_parser_operator_function_id (cp_parser* parser)
10095{
10096  /* Look for the `operator' keyword.  */
10097  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10098    return error_mark_node;
10099  /* And then the name of the operator itself.  */
10100  return cp_parser_operator (parser);
10101}
10102
10103/* Parse an operator.
10104
10105   operator:
10106     new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10107     += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10108     || ++ -- , ->* -> () []
10109
10110   GNU Extensions:
10111
10112   operator:
10113     <? >? <?= >?=
10114
10115   Returns an IDENTIFIER_NODE for the operator which is a
10116   human-readable spelling of the identifier, e.g., `operator +'.  */
10117
10118static tree
10119cp_parser_operator (cp_parser* parser)
10120{
10121  tree id = NULL_TREE;
10122  cp_token *token;
10123
10124  /* Peek at the next token.  */
10125  token = cp_lexer_peek_token (parser->lexer);
10126  /* Figure out which operator we have.  */
10127  switch (token->type)
10128    {
10129    case CPP_KEYWORD:
10130      {
10131	enum tree_code op;
10132
10133	/* The keyword should be either `new' or `delete'.  */
10134	if (token->keyword == RID_NEW)
10135	  op = NEW_EXPR;
10136	else if (token->keyword == RID_DELETE)
10137	  op = DELETE_EXPR;
10138	else
10139	  break;
10140
10141	/* Consume the `new' or `delete' token.  */
10142	cp_lexer_consume_token (parser->lexer);
10143
10144	/* Peek at the next token.  */
10145	token = cp_lexer_peek_token (parser->lexer);
10146	/* If it's a `[' token then this is the array variant of the
10147	   operator.  */
10148	if (token->type == CPP_OPEN_SQUARE)
10149	  {
10150	    /* Consume the `[' token.  */
10151	    cp_lexer_consume_token (parser->lexer);
10152	    /* Look for the `]' token.  */
10153	    cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10154	    id = ansi_opname (op == NEW_EXPR
10155			      ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10156	  }
10157	/* Otherwise, we have the non-array variant.  */
10158	else
10159	  id = ansi_opname (op);
10160
10161	return id;
10162      }
10163
10164    case CPP_PLUS:
10165      id = ansi_opname (PLUS_EXPR);
10166      break;
10167
10168    case CPP_MINUS:
10169      id = ansi_opname (MINUS_EXPR);
10170      break;
10171
10172    case CPP_MULT:
10173      id = ansi_opname (MULT_EXPR);
10174      break;
10175
10176    case CPP_DIV:
10177      id = ansi_opname (TRUNC_DIV_EXPR);
10178      break;
10179
10180    case CPP_MOD:
10181      id = ansi_opname (TRUNC_MOD_EXPR);
10182      break;
10183
10184    case CPP_XOR:
10185      id = ansi_opname (BIT_XOR_EXPR);
10186      break;
10187
10188    case CPP_AND:
10189      id = ansi_opname (BIT_AND_EXPR);
10190      break;
10191
10192    case CPP_OR:
10193      id = ansi_opname (BIT_IOR_EXPR);
10194      break;
10195
10196    case CPP_COMPL:
10197      id = ansi_opname (BIT_NOT_EXPR);
10198      break;
10199
10200    case CPP_NOT:
10201      id = ansi_opname (TRUTH_NOT_EXPR);
10202      break;
10203
10204    case CPP_EQ:
10205      id = ansi_assopname (NOP_EXPR);
10206      break;
10207
10208    case CPP_LESS:
10209      id = ansi_opname (LT_EXPR);
10210      break;
10211
10212    case CPP_GREATER:
10213      id = ansi_opname (GT_EXPR);
10214      break;
10215
10216    case CPP_PLUS_EQ:
10217      id = ansi_assopname (PLUS_EXPR);
10218      break;
10219
10220    case CPP_MINUS_EQ:
10221      id = ansi_assopname (MINUS_EXPR);
10222      break;
10223
10224    case CPP_MULT_EQ:
10225      id = ansi_assopname (MULT_EXPR);
10226      break;
10227
10228    case CPP_DIV_EQ:
10229      id = ansi_assopname (TRUNC_DIV_EXPR);
10230      break;
10231
10232    case CPP_MOD_EQ:
10233      id = ansi_assopname (TRUNC_MOD_EXPR);
10234      break;
10235
10236    case CPP_XOR_EQ:
10237      id = ansi_assopname (BIT_XOR_EXPR);
10238      break;
10239
10240    case CPP_AND_EQ:
10241      id = ansi_assopname (BIT_AND_EXPR);
10242      break;
10243
10244    case CPP_OR_EQ:
10245      id = ansi_assopname (BIT_IOR_EXPR);
10246      break;
10247
10248    case CPP_LSHIFT:
10249      id = ansi_opname (LSHIFT_EXPR);
10250      break;
10251
10252    case CPP_RSHIFT:
10253      id = ansi_opname (RSHIFT_EXPR);
10254      break;
10255
10256    case CPP_LSHIFT_EQ:
10257      id = ansi_assopname (LSHIFT_EXPR);
10258      break;
10259
10260    case CPP_RSHIFT_EQ:
10261      id = ansi_assopname (RSHIFT_EXPR);
10262      break;
10263
10264    case CPP_EQ_EQ:
10265      id = ansi_opname (EQ_EXPR);
10266      break;
10267
10268    case CPP_NOT_EQ:
10269      id = ansi_opname (NE_EXPR);
10270      break;
10271
10272    case CPP_LESS_EQ:
10273      id = ansi_opname (LE_EXPR);
10274      break;
10275
10276    case CPP_GREATER_EQ:
10277      id = ansi_opname (GE_EXPR);
10278      break;
10279
10280    case CPP_AND_AND:
10281      id = ansi_opname (TRUTH_ANDIF_EXPR);
10282      break;
10283
10284    case CPP_OR_OR:
10285      id = ansi_opname (TRUTH_ORIF_EXPR);
10286      break;
10287
10288    case CPP_PLUS_PLUS:
10289      id = ansi_opname (POSTINCREMENT_EXPR);
10290      break;
10291
10292    case CPP_MINUS_MINUS:
10293      id = ansi_opname (PREDECREMENT_EXPR);
10294      break;
10295
10296    case CPP_COMMA:
10297      id = ansi_opname (COMPOUND_EXPR);
10298      break;
10299
10300    case CPP_DEREF_STAR:
10301      id = ansi_opname (MEMBER_REF);
10302      break;
10303
10304    case CPP_DEREF:
10305      id = ansi_opname (COMPONENT_REF);
10306      break;
10307
10308    case CPP_OPEN_PAREN:
10309      /* Consume the `('.  */
10310      cp_lexer_consume_token (parser->lexer);
10311      /* Look for the matching `)'.  */
10312      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10313      return ansi_opname (CALL_EXPR);
10314
10315    case CPP_OPEN_SQUARE:
10316      /* Consume the `['.  */
10317      cp_lexer_consume_token (parser->lexer);
10318      /* Look for the matching `]'.  */
10319      cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10320      return ansi_opname (ARRAY_REF);
10321
10322    default:
10323      /* Anything else is an error.  */
10324      break;
10325    }
10326
10327  /* If we have selected an identifier, we need to consume the
10328     operator token.  */
10329  if (id)
10330    cp_lexer_consume_token (parser->lexer);
10331  /* Otherwise, no valid operator name was present.  */
10332  else
10333    {
10334      cp_parser_error (parser, "expected operator");
10335      id = error_mark_node;
10336    }
10337
10338  return id;
10339}
10340
10341/* Parse a template-declaration.
10342
10343   template-declaration:
10344     export [opt] template < template-parameter-list > declaration
10345
10346   If MEMBER_P is TRUE, this template-declaration occurs within a
10347   class-specifier.
10348
10349   The grammar rule given by the standard isn't correct.  What
10350   is really meant is:
10351
10352   template-declaration:
10353     export [opt] template-parameter-list-seq
10354       decl-specifier-seq [opt] init-declarator [opt] ;
10355     export [opt] template-parameter-list-seq
10356       function-definition
10357
10358   template-parameter-list-seq:
10359     template-parameter-list-seq [opt]
10360     template < template-parameter-list >  */
10361
10362static void
10363cp_parser_template_declaration (cp_parser* parser, bool member_p)
10364{
10365  /* Check for `export'.  */
10366  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10367    {
10368      /* Consume the `export' token.  */
10369      cp_lexer_consume_token (parser->lexer);
10370      /* Warn that we do not support `export'.  */
10371      warning (0, "keyword %<export%> not implemented, and will be ignored");
10372    }
10373
10374  cp_parser_template_declaration_after_export (parser, member_p);
10375}
10376
10377/* Parse a template-parameter-list.
10378
10379   template-parameter-list:
10380     template-parameter
10381     template-parameter-list , template-parameter
10382
10383   Returns a TREE_LIST.  Each node represents a template parameter.
10384   The nodes are connected via their TREE_CHAINs.  */
10385
10386static tree
10387cp_parser_template_parameter_list (cp_parser* parser)
10388{
10389  tree parameter_list = NULL_TREE;
10390
10391  begin_template_parm_list ();
10392  while (true)
10393    {
10394      tree parameter;
10395      bool is_non_type;
10396      bool is_parameter_pack;
10397      location_t parm_loc;
10398
10399      /* Parse the template-parameter.  */
10400      parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10401      parameter = cp_parser_template_parameter (parser,
10402                                                &is_non_type,
10403                                                &is_parameter_pack);
10404      /* Add it to the list.  */
10405      if (parameter != error_mark_node)
10406	parameter_list = process_template_parm (parameter_list,
10407						parm_loc,
10408						parameter,
10409						is_non_type,
10410                                                is_parameter_pack);
10411      else
10412       {
10413         tree err_parm = build_tree_list (parameter, parameter);
10414         TREE_VALUE (err_parm) = error_mark_node;
10415         parameter_list = chainon (parameter_list, err_parm);
10416       }
10417
10418      /* If the next token is not a `,', we're done.  */
10419      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10420	break;
10421      /* Otherwise, consume the `,' token.  */
10422      cp_lexer_consume_token (parser->lexer);
10423    }
10424
10425  return end_template_parm_list (parameter_list);
10426}
10427
10428/* Parse a template-parameter.
10429
10430   template-parameter:
10431     type-parameter
10432     parameter-declaration
10433
10434   If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10435   the parameter.  The TREE_PURPOSE is the default value, if any.
10436   Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10437   iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10438   set to true iff this parameter is a parameter pack. */
10439
10440static tree
10441cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10442                              bool *is_parameter_pack)
10443{
10444  cp_token *token;
10445  cp_parameter_declarator *parameter_declarator;
10446  cp_declarator *id_declarator;
10447  tree parm;
10448
10449  /* Assume it is a type parameter or a template parameter.  */
10450  *is_non_type = false;
10451  /* Assume it not a parameter pack. */
10452  *is_parameter_pack = false;
10453  /* Peek at the next token.  */
10454  token = cp_lexer_peek_token (parser->lexer);
10455  /* If it is `class' or `template', we have a type-parameter.  */
10456  if (token->keyword == RID_TEMPLATE)
10457    return cp_parser_type_parameter (parser, is_parameter_pack);
10458  /* If it is `class' or `typename' we do not know yet whether it is a
10459     type parameter or a non-type parameter.  Consider:
10460
10461       template <typename T, typename T::X X> ...
10462
10463     or:
10464
10465       template <class C, class D*> ...
10466
10467     Here, the first parameter is a type parameter, and the second is
10468     a non-type parameter.  We can tell by looking at the token after
10469     the identifier -- if it is a `,', `=', or `>' then we have a type
10470     parameter.  */
10471  if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10472    {
10473      /* Peek at the token after `class' or `typename'.  */
10474      token = cp_lexer_peek_nth_token (parser->lexer, 2);
10475      /* If it's an ellipsis, we have a template type parameter
10476         pack. */
10477      if (token->type == CPP_ELLIPSIS)
10478        return cp_parser_type_parameter (parser, is_parameter_pack);
10479      /* If it's an identifier, skip it.  */
10480      if (token->type == CPP_NAME)
10481	token = cp_lexer_peek_nth_token (parser->lexer, 3);
10482      /* Now, see if the token looks like the end of a template
10483	 parameter.  */
10484      if (token->type == CPP_COMMA
10485	  || token->type == CPP_EQ
10486	  || token->type == CPP_GREATER)
10487	return cp_parser_type_parameter (parser, is_parameter_pack);
10488    }
10489
10490  /* Otherwise, it is a non-type parameter.
10491
10492     [temp.param]
10493
10494     When parsing a default template-argument for a non-type
10495     template-parameter, the first non-nested `>' is taken as the end
10496     of the template parameter-list rather than a greater-than
10497     operator.  */
10498  *is_non_type = true;
10499  parameter_declarator
10500     = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10501					/*parenthesized_p=*/NULL);
10502
10503  /* If the parameter declaration is marked as a parameter pack, set
10504     *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10505     declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10506     grokdeclarator. */
10507  if (parameter_declarator
10508      && parameter_declarator->declarator
10509      && parameter_declarator->declarator->parameter_pack_p)
10510    {
10511      *is_parameter_pack = true;
10512      parameter_declarator->declarator->parameter_pack_p = false;
10513    }
10514
10515  /* If the next token is an ellipsis, and we don't already have it
10516     marked as a parameter pack, then we have a parameter pack (that
10517     has no declarator).  */
10518  if (!*is_parameter_pack
10519      && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10520      && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10521    {
10522      /* Consume the `...'.  */
10523      cp_lexer_consume_token (parser->lexer);
10524      maybe_warn_variadic_templates ();
10525
10526      *is_parameter_pack = true;
10527    }
10528  /* We might end up with a pack expansion as the type of the non-type
10529     template parameter, in which case this is a non-type template
10530     parameter pack.  */
10531  else if (parameter_declarator
10532	   && parameter_declarator->decl_specifiers.type
10533	   && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10534    {
10535      *is_parameter_pack = true;
10536      parameter_declarator->decl_specifiers.type =
10537	PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10538    }
10539
10540  if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10541    {
10542      /* Parameter packs cannot have default arguments.  However, a
10543	 user may try to do so, so we'll parse them and give an
10544	 appropriate diagnostic here.  */
10545
10546      /* Consume the `='.  */
10547      cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10548      cp_lexer_consume_token (parser->lexer);
10549
10550      /* Find the name of the parameter pack.  */
10551      id_declarator = parameter_declarator->declarator;
10552      while (id_declarator && id_declarator->kind != cdk_id)
10553	id_declarator = id_declarator->declarator;
10554
10555      if (id_declarator && id_declarator->kind == cdk_id)
10556	error_at (start_token->location,
10557		  "template parameter pack %qD cannot have a default argument",
10558		  id_declarator->u.id.unqualified_name);
10559      else
10560	error_at (start_token->location,
10561		  "template parameter pack cannot have a default argument");
10562
10563      /* Parse the default argument, but throw away the result.  */
10564      cp_parser_default_argument (parser, /*template_parm_p=*/true);
10565    }
10566
10567  parm = grokdeclarator (parameter_declarator->declarator,
10568			 &parameter_declarator->decl_specifiers,
10569			 TPARM, /*initialized=*/0,
10570			 /*attrlist=*/NULL);
10571  if (parm == error_mark_node)
10572    return error_mark_node;
10573
10574  return build_tree_list (parameter_declarator->default_argument, parm);
10575}
10576
10577/* Parse a type-parameter.
10578
10579   type-parameter:
10580     class identifier [opt]
10581     class identifier [opt] = type-id
10582     typename identifier [opt]
10583     typename identifier [opt] = type-id
10584     template < template-parameter-list > class identifier [opt]
10585     template < template-parameter-list > class identifier [opt]
10586       = id-expression
10587
10588   GNU Extension (variadic templates):
10589
10590   type-parameter:
10591     class ... identifier [opt]
10592     typename ... identifier [opt]
10593
10594   Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10595   TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10596   the declaration of the parameter.
10597
10598   Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10599
10600static tree
10601cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10602{
10603  cp_token *token;
10604  tree parameter;
10605
10606  /* Look for a keyword to tell us what kind of parameter this is.  */
10607  token = cp_parser_require (parser, CPP_KEYWORD,
10608			     "%<class%>, %<typename%>, or %<template%>");
10609  if (!token)
10610    return error_mark_node;
10611
10612  switch (token->keyword)
10613    {
10614    case RID_CLASS:
10615    case RID_TYPENAME:
10616      {
10617	tree identifier;
10618	tree default_argument;
10619
10620        /* If the next token is an ellipsis, we have a template
10621           argument pack. */
10622        if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10623          {
10624            /* Consume the `...' token. */
10625            cp_lexer_consume_token (parser->lexer);
10626            maybe_warn_variadic_templates ();
10627
10628            *is_parameter_pack = true;
10629          }
10630
10631	/* If the next token is an identifier, then it names the
10632	   parameter.  */
10633	if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10634	  identifier = cp_parser_identifier (parser);
10635	else
10636	  identifier = NULL_TREE;
10637
10638	/* Create the parameter.  */
10639	parameter = finish_template_type_parm (class_type_node, identifier);
10640
10641	/* If the next token is an `=', we have a default argument.  */
10642	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10643	  {
10644	    /* Consume the `=' token.  */
10645	    cp_lexer_consume_token (parser->lexer);
10646	    /* Parse the default-argument.  */
10647	    push_deferring_access_checks (dk_no_deferred);
10648	    default_argument = cp_parser_type_id (parser);
10649
10650            /* Template parameter packs cannot have default
10651               arguments. */
10652            if (*is_parameter_pack)
10653              {
10654                if (identifier)
10655                  error_at (token->location,
10656			    "template parameter pack %qD cannot have a "
10657			    "default argument", identifier);
10658                else
10659                  error_at (token->location,
10660			    "template parameter packs cannot have "
10661			    "default arguments");
10662                default_argument = NULL_TREE;
10663              }
10664	    pop_deferring_access_checks ();
10665	  }
10666	else
10667	  default_argument = NULL_TREE;
10668
10669	/* Create the combined representation of the parameter and the
10670	   default argument.  */
10671	parameter = build_tree_list (default_argument, parameter);
10672      }
10673      break;
10674
10675    case RID_TEMPLATE:
10676      {
10677	tree identifier;
10678	tree default_argument;
10679
10680	/* Look for the `<'.  */
10681	cp_parser_require (parser, CPP_LESS, "%<<%>");
10682	/* Parse the template-parameter-list.  */
10683	cp_parser_template_parameter_list (parser);
10684	/* Look for the `>'.  */
10685	cp_parser_require (parser, CPP_GREATER, "%<>%>");
10686	/* Look for the `class' keyword.  */
10687	cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10688        /* If the next token is an ellipsis, we have a template
10689           argument pack. */
10690        if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10691          {
10692            /* Consume the `...' token. */
10693            cp_lexer_consume_token (parser->lexer);
10694            maybe_warn_variadic_templates ();
10695
10696            *is_parameter_pack = true;
10697          }
10698	/* If the next token is an `=', then there is a
10699	   default-argument.  If the next token is a `>', we are at
10700	   the end of the parameter-list.  If the next token is a `,',
10701	   then we are at the end of this parameter.  */
10702	if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10703	    && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10704	    && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10705	  {
10706	    identifier = cp_parser_identifier (parser);
10707	    /* Treat invalid names as if the parameter were nameless.  */
10708	    if (identifier == error_mark_node)
10709	      identifier = NULL_TREE;
10710	  }
10711	else
10712	  identifier = NULL_TREE;
10713
10714	/* Create the template parameter.  */
10715	parameter = finish_template_template_parm (class_type_node,
10716						   identifier);
10717
10718	/* If the next token is an `=', then there is a
10719	   default-argument.  */
10720	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10721	  {
10722	    bool is_template;
10723
10724	    /* Consume the `='.  */
10725	    cp_lexer_consume_token (parser->lexer);
10726	    /* Parse the id-expression.  */
10727	    push_deferring_access_checks (dk_no_deferred);
10728	    /* save token before parsing the id-expression, for error
10729	       reporting */
10730	    token = cp_lexer_peek_token (parser->lexer);
10731	    default_argument
10732	      = cp_parser_id_expression (parser,
10733					 /*template_keyword_p=*/false,
10734					 /*check_dependency_p=*/true,
10735					 /*template_p=*/&is_template,
10736					 /*declarator_p=*/false,
10737					 /*optional_p=*/false);
10738	    if (TREE_CODE (default_argument) == TYPE_DECL)
10739	      /* If the id-expression was a template-id that refers to
10740		 a template-class, we already have the declaration here,
10741		 so no further lookup is needed.  */
10742		 ;
10743	    else
10744	      /* Look up the name.  */
10745	      default_argument
10746		= cp_parser_lookup_name (parser, default_argument,
10747					 none_type,
10748					 /*is_template=*/is_template,
10749					 /*is_namespace=*/false,
10750					 /*check_dependency=*/true,
10751					 /*ambiguous_decls=*/NULL,
10752					 token->location);
10753	    /* See if the default argument is valid.  */
10754	    default_argument
10755	      = check_template_template_default_arg (default_argument);
10756
10757            /* Template parameter packs cannot have default
10758               arguments. */
10759            if (*is_parameter_pack)
10760              {
10761                if (identifier)
10762                  error_at (token->location,
10763			    "template parameter pack %qD cannot "
10764			    "have a default argument",
10765			    identifier);
10766                else
10767                  error_at (token->location, "template parameter packs cannot "
10768			    "have default arguments");
10769                default_argument = NULL_TREE;
10770              }
10771	    pop_deferring_access_checks ();
10772	  }
10773	else
10774	  default_argument = NULL_TREE;
10775
10776	/* Create the combined representation of the parameter and the
10777	   default argument.  */
10778	parameter = build_tree_list (default_argument, parameter);
10779      }
10780      break;
10781
10782    default:
10783      gcc_unreachable ();
10784      break;
10785    }
10786
10787  return parameter;
10788}
10789
10790/* Parse a template-id.
10791
10792   template-id:
10793     template-name < template-argument-list [opt] >
10794
10795   If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10796   `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10797   returned.  Otherwise, if the template-name names a function, or set
10798   of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10799   names a class, returns a TYPE_DECL for the specialization.
10800
10801   If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10802   uninstantiated templates.  */
10803
10804static tree
10805cp_parser_template_id (cp_parser *parser,
10806		       bool template_keyword_p,
10807		       bool check_dependency_p,
10808		       bool is_declaration)
10809{
10810  int i;
10811  tree templ;
10812  tree arguments;
10813  tree template_id;
10814  cp_token_position start_of_id = 0;
10815  deferred_access_check *chk;
10816  VEC (deferred_access_check,gc) *access_check;
10817  cp_token *next_token = NULL, *next_token_2 = NULL;
10818  bool is_identifier;
10819
10820  /* If the next token corresponds to a template-id, there is no need
10821     to reparse it.  */
10822  next_token = cp_lexer_peek_token (parser->lexer);
10823  if (next_token->type == CPP_TEMPLATE_ID)
10824    {
10825      struct tree_check *check_value;
10826
10827      /* Get the stored value.  */
10828      check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10829      /* Perform any access checks that were deferred.  */
10830      access_check = check_value->checks;
10831      if (access_check)
10832	{
10833	  for (i = 0 ;
10834	       VEC_iterate (deferred_access_check, access_check, i, chk) ;
10835	       ++i)
10836	    {
10837	      perform_or_defer_access_check (chk->binfo,
10838					     chk->decl,
10839					     chk->diag_decl);
10840	    }
10841	}
10842      /* Return the stored value.  */
10843      return check_value->value;
10844    }
10845
10846  /* Avoid performing name lookup if there is no possibility of
10847     finding a template-id.  */
10848  if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10849      || (next_token->type == CPP_NAME
10850	  && !cp_parser_nth_token_starts_template_argument_list_p
10851	       (parser, 2)))
10852    {
10853      cp_parser_error (parser, "expected template-id");
10854      return error_mark_node;
10855    }
10856
10857  /* Remember where the template-id starts.  */
10858  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10859    start_of_id = cp_lexer_token_position (parser->lexer, false);
10860
10861  push_deferring_access_checks (dk_deferred);
10862
10863  /* Parse the template-name.  */
10864  is_identifier = false;
10865  templ = cp_parser_template_name (parser, template_keyword_p,
10866				   check_dependency_p,
10867				   is_declaration,
10868				   &is_identifier);
10869  if (templ == error_mark_node || is_identifier)
10870    {
10871      pop_deferring_access_checks ();
10872      return templ;
10873    }
10874
10875  /* If we find the sequence `[:' after a template-name, it's probably
10876     a digraph-typo for `< ::'. Substitute the tokens and check if we can
10877     parse correctly the argument list.  */
10878  next_token = cp_lexer_peek_token (parser->lexer);
10879  next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10880  if (next_token->type == CPP_OPEN_SQUARE
10881      && next_token->flags & DIGRAPH
10882      && next_token_2->type == CPP_COLON
10883      && !(next_token_2->flags & PREV_WHITE))
10884    {
10885      cp_parser_parse_tentatively (parser);
10886      /* Change `:' into `::'.  */
10887      next_token_2->type = CPP_SCOPE;
10888      /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10889	 CPP_LESS.  */
10890      cp_lexer_consume_token (parser->lexer);
10891
10892      /* Parse the arguments.  */
10893      arguments = cp_parser_enclosed_template_argument_list (parser);
10894      if (!cp_parser_parse_definitely (parser))
10895	{
10896	  /* If we couldn't parse an argument list, then we revert our changes
10897	     and return simply an error. Maybe this is not a template-id
10898	     after all.  */
10899	  next_token_2->type = CPP_COLON;
10900	  cp_parser_error (parser, "expected %<<%>");
10901	  pop_deferring_access_checks ();
10902	  return error_mark_node;
10903	}
10904      /* Otherwise, emit an error about the invalid digraph, but continue
10905	 parsing because we got our argument list.  */
10906      if (permerror (next_token->location,
10907		     "%<<::%> cannot begin a template-argument list"))
10908	{
10909	  static bool hint = false;
10910	  inform (next_token->location,
10911		  "%<<:%> is an alternate spelling for %<[%>."
10912		  " Insert whitespace between %<<%> and %<::%>");
10913	  if (!hint && !flag_permissive)
10914	    {
10915	      inform (next_token->location, "(if you use %<-fpermissive%>"
10916		      " G++ will accept your code)");
10917	      hint = true;
10918	    }
10919	}
10920    }
10921  else
10922    {
10923      /* Look for the `<' that starts the template-argument-list.  */
10924      if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10925	{
10926	  pop_deferring_access_checks ();
10927	  return error_mark_node;
10928	}
10929      /* Parse the arguments.  */
10930      arguments = cp_parser_enclosed_template_argument_list (parser);
10931    }
10932
10933  /* Build a representation of the specialization.  */
10934  if (TREE_CODE (templ) == IDENTIFIER_NODE)
10935    template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10936  else if (DECL_CLASS_TEMPLATE_P (templ)
10937	   || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10938    {
10939      bool entering_scope;
10940      /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10941	 template (rather than some instantiation thereof) only if
10942	 is not nested within some other construct.  For example, in
10943	 "template <typename T> void f(T) { A<T>::", A<T> is just an
10944	 instantiation of A.  */
10945      entering_scope = (template_parm_scope_p ()
10946			&& cp_lexer_next_token_is (parser->lexer,
10947						   CPP_SCOPE));
10948      template_id
10949	= finish_template_type (templ, arguments, entering_scope);
10950    }
10951  else
10952    {
10953      /* If it's not a class-template or a template-template, it should be
10954	 a function-template.  */
10955      gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10956		   || TREE_CODE (templ) == OVERLOAD
10957		   || BASELINK_P (templ)));
10958
10959      template_id = lookup_template_function (templ, arguments);
10960    }
10961
10962  /* If parsing tentatively, replace the sequence of tokens that makes
10963     up the template-id with a CPP_TEMPLATE_ID token.  That way,
10964     should we re-parse the token stream, we will not have to repeat
10965     the effort required to do the parse, nor will we issue duplicate
10966     error messages about problems during instantiation of the
10967     template.  */
10968  if (start_of_id)
10969    {
10970      cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10971
10972      /* Reset the contents of the START_OF_ID token.  */
10973      token->type = CPP_TEMPLATE_ID;
10974      /* Retrieve any deferred checks.  Do not pop this access checks yet
10975	 so the memory will not be reclaimed during token replacing below.  */
10976      token->u.tree_check_value = GGC_CNEW (struct tree_check);
10977      token->u.tree_check_value->value = template_id;
10978      token->u.tree_check_value->checks = get_deferred_access_checks ();
10979      token->keyword = RID_MAX;
10980
10981      /* Purge all subsequent tokens.  */
10982      cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10983
10984      /* ??? Can we actually assume that, if template_id ==
10985	 error_mark_node, we will have issued a diagnostic to the
10986	 user, as opposed to simply marking the tentative parse as
10987	 failed?  */
10988      if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10989	error_at (token->location, "parse error in template argument list");
10990    }
10991
10992  pop_deferring_access_checks ();
10993  return template_id;
10994}
10995
10996/* Parse a template-name.
10997
10998   template-name:
10999     identifier
11000
11001   The standard should actually say:
11002
11003   template-name:
11004     identifier
11005     operator-function-id
11006
11007   A defect report has been filed about this issue.
11008
11009   A conversion-function-id cannot be a template name because they cannot
11010   be part of a template-id. In fact, looking at this code:
11011
11012   a.operator K<int>()
11013
11014   the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11015   It is impossible to call a templated conversion-function-id with an
11016   explicit argument list, since the only allowed template parameter is
11017   the type to which it is converting.
11018
11019   If TEMPLATE_KEYWORD_P is true, then we have just seen the
11020   `template' keyword, in a construction like:
11021
11022     T::template f<3>()
11023
11024   In that case `f' is taken to be a template-name, even though there
11025   is no way of knowing for sure.
11026
11027   Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11028   name refers to a set of overloaded functions, at least one of which
11029   is a template, or an IDENTIFIER_NODE with the name of the template,
11030   if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11031   names are looked up inside uninstantiated templates.  */
11032
11033static tree
11034cp_parser_template_name (cp_parser* parser,
11035			 bool template_keyword_p,
11036			 bool check_dependency_p,
11037			 bool is_declaration,
11038			 bool *is_identifier)
11039{
11040  tree identifier;
11041  tree decl;
11042  tree fns;
11043  cp_token *token = cp_lexer_peek_token (parser->lexer);
11044
11045  /* If the next token is `operator', then we have either an
11046     operator-function-id or a conversion-function-id.  */
11047  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11048    {
11049      /* We don't know whether we're looking at an
11050	 operator-function-id or a conversion-function-id.  */
11051      cp_parser_parse_tentatively (parser);
11052      /* Try an operator-function-id.  */
11053      identifier = cp_parser_operator_function_id (parser);
11054      /* If that didn't work, try a conversion-function-id.  */
11055      if (!cp_parser_parse_definitely (parser))
11056	{
11057	  cp_parser_error (parser, "expected template-name");
11058	  return error_mark_node;
11059	}
11060    }
11061  /* Look for the identifier.  */
11062  else
11063    identifier = cp_parser_identifier (parser);
11064
11065  /* If we didn't find an identifier, we don't have a template-id.  */
11066  if (identifier == error_mark_node)
11067    return error_mark_node;
11068
11069  /* If the name immediately followed the `template' keyword, then it
11070     is a template-name.  However, if the next token is not `<', then
11071     we do not treat it as a template-name, since it is not being used
11072     as part of a template-id.  This enables us to handle constructs
11073     like:
11074
11075       template <typename T> struct S { S(); };
11076       template <typename T> S<T>::S();
11077
11078     correctly.  We would treat `S' as a template -- if it were `S<T>'
11079     -- but we do not if there is no `<'.  */
11080
11081  if (processing_template_decl
11082      && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11083    {
11084      /* In a declaration, in a dependent context, we pretend that the
11085	 "template" keyword was present in order to improve error
11086	 recovery.  For example, given:
11087
11088	   template <typename T> void f(T::X<int>);
11089
11090	 we want to treat "X<int>" as a template-id.  */
11091      if (is_declaration
11092	  && !template_keyword_p
11093	  && parser->scope && TYPE_P (parser->scope)
11094	  && check_dependency_p
11095	  && dependent_scope_p (parser->scope)
11096	  /* Do not do this for dtors (or ctors), since they never
11097	     need the template keyword before their name.  */
11098	  && !constructor_name_p (identifier, parser->scope))
11099	{
11100	  cp_token_position start = 0;
11101
11102	  /* Explain what went wrong.  */
11103	  error_at (token->location, "non-template %qD used as template",
11104		    identifier);
11105	  inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11106		  parser->scope, identifier);
11107	  /* If parsing tentatively, find the location of the "<" token.  */
11108	  if (cp_parser_simulate_error (parser))
11109	    start = cp_lexer_token_position (parser->lexer, true);
11110	  /* Parse the template arguments so that we can issue error
11111	     messages about them.  */
11112	  cp_lexer_consume_token (parser->lexer);
11113	  cp_parser_enclosed_template_argument_list (parser);
11114	  /* Skip tokens until we find a good place from which to
11115	     continue parsing.  */
11116	  cp_parser_skip_to_closing_parenthesis (parser,
11117						 /*recovering=*/true,
11118						 /*or_comma=*/true,
11119						 /*consume_paren=*/false);
11120	  /* If parsing tentatively, permanently remove the
11121	     template argument list.  That will prevent duplicate
11122	     error messages from being issued about the missing
11123	     "template" keyword.  */
11124	  if (start)
11125	    cp_lexer_purge_tokens_after (parser->lexer, start);
11126	  if (is_identifier)
11127	    *is_identifier = true;
11128	  return identifier;
11129	}
11130
11131      /* If the "template" keyword is present, then there is generally
11132	 no point in doing name-lookup, so we just return IDENTIFIER.
11133	 But, if the qualifying scope is non-dependent then we can
11134	 (and must) do name-lookup normally.  */
11135      if (template_keyword_p
11136	  && (!parser->scope
11137	      || (TYPE_P (parser->scope)
11138		  && dependent_type_p (parser->scope))))
11139	return identifier;
11140    }
11141
11142  /* Look up the name.  */
11143  decl = cp_parser_lookup_name (parser, identifier,
11144				none_type,
11145				/*is_template=*/true,
11146				/*is_namespace=*/false,
11147				check_dependency_p,
11148				/*ambiguous_decls=*/NULL,
11149				token->location);
11150
11151  /* If DECL is a template, then the name was a template-name.  */
11152  if (TREE_CODE (decl) == TEMPLATE_DECL)
11153    ;
11154  else
11155    {
11156      tree fn = NULL_TREE;
11157
11158      /* The standard does not explicitly indicate whether a name that
11159	 names a set of overloaded declarations, some of which are
11160	 templates, is a template-name.  However, such a name should
11161	 be a template-name; otherwise, there is no way to form a
11162	 template-id for the overloaded templates.  */
11163      fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11164      if (TREE_CODE (fns) == OVERLOAD)
11165	for (fn = fns; fn; fn = OVL_NEXT (fn))
11166	  if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11167	    break;
11168
11169      if (!fn)
11170	{
11171	  /* The name does not name a template.  */
11172	  cp_parser_error (parser, "expected template-name");
11173	  return error_mark_node;
11174	}
11175    }
11176
11177  /* If DECL is dependent, and refers to a function, then just return
11178     its name; we will look it up again during template instantiation.  */
11179  if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11180    {
11181      tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11182      if (TYPE_P (scope) && dependent_type_p (scope))
11183	return identifier;
11184    }
11185
11186  return decl;
11187}
11188
11189/* Parse a template-argument-list.
11190
11191   template-argument-list:
11192     template-argument ... [opt]
11193     template-argument-list , template-argument ... [opt]
11194
11195   Returns a TREE_VEC containing the arguments.  */
11196
11197static tree
11198cp_parser_template_argument_list (cp_parser* parser)
11199{
11200  tree fixed_args[10];
11201  unsigned n_args = 0;
11202  unsigned alloced = 10;
11203  tree *arg_ary = fixed_args;
11204  tree vec;
11205  bool saved_in_template_argument_list_p;
11206  bool saved_ice_p;
11207  bool saved_non_ice_p;
11208
11209  saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11210  parser->in_template_argument_list_p = true;
11211  /* Even if the template-id appears in an integral
11212     constant-expression, the contents of the argument list do
11213     not.  */
11214  saved_ice_p = parser->integral_constant_expression_p;
11215  parser->integral_constant_expression_p = false;
11216  saved_non_ice_p = parser->non_integral_constant_expression_p;
11217  parser->non_integral_constant_expression_p = false;
11218  /* Parse the arguments.  */
11219  do
11220    {
11221      tree argument;
11222
11223      if (n_args)
11224	/* Consume the comma.  */
11225	cp_lexer_consume_token (parser->lexer);
11226
11227      /* Parse the template-argument.  */
11228      argument = cp_parser_template_argument (parser);
11229
11230      /* If the next token is an ellipsis, we're expanding a template
11231         argument pack. */
11232      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11233        {
11234	  if (argument == error_mark_node)
11235	    {
11236	      cp_token *token = cp_lexer_peek_token (parser->lexer);
11237	      error_at (token->location,
11238			"expected parameter pack before %<...%>");
11239	    }
11240          /* Consume the `...' token. */
11241          cp_lexer_consume_token (parser->lexer);
11242
11243          /* Make the argument into a TYPE_PACK_EXPANSION or
11244             EXPR_PACK_EXPANSION. */
11245          argument = make_pack_expansion (argument);
11246        }
11247
11248      if (n_args == alloced)
11249	{
11250	  alloced *= 2;
11251
11252	  if (arg_ary == fixed_args)
11253	    {
11254	      arg_ary = XNEWVEC (tree, alloced);
11255	      memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11256	    }
11257	  else
11258	    arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11259	}
11260      arg_ary[n_args++] = argument;
11261    }
11262  while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11263
11264  vec = make_tree_vec (n_args);
11265
11266  while (n_args--)
11267    TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11268
11269  if (arg_ary != fixed_args)
11270    free (arg_ary);
11271  parser->non_integral_constant_expression_p = saved_non_ice_p;
11272  parser->integral_constant_expression_p = saved_ice_p;
11273  parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11274#ifdef ENABLE_CHECKING
11275  SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11276#endif
11277  return vec;
11278}
11279
11280/* Parse a template-argument.
11281
11282   template-argument:
11283     assignment-expression
11284     type-id
11285     id-expression
11286
11287   The representation is that of an assignment-expression, type-id, or
11288   id-expression -- except that the qualified id-expression is
11289   evaluated, so that the value returned is either a DECL or an
11290   OVERLOAD.
11291
11292   Although the standard says "assignment-expression", it forbids
11293   throw-expressions or assignments in the template argument.
11294   Therefore, we use "conditional-expression" instead.  */
11295
11296static tree
11297cp_parser_template_argument (cp_parser* parser)
11298{
11299  tree argument;
11300  bool template_p;
11301  bool address_p;
11302  bool maybe_type_id = false;
11303  cp_token *token = NULL, *argument_start_token = NULL;
11304  cp_id_kind idk;
11305
11306  /* There's really no way to know what we're looking at, so we just
11307     try each alternative in order.
11308
11309       [temp.arg]
11310
11311       In a template-argument, an ambiguity between a type-id and an
11312       expression is resolved to a type-id, regardless of the form of
11313       the corresponding template-parameter.
11314
11315     Therefore, we try a type-id first.  */
11316  cp_parser_parse_tentatively (parser);
11317  argument = cp_parser_template_type_arg (parser);
11318  /* If there was no error parsing the type-id but the next token is a
11319     '>>', our behavior depends on which dialect of C++ we're
11320     parsing. In C++98, we probably found a typo for '> >'. But there
11321     are type-id which are also valid expressions. For instance:
11322
11323     struct X { int operator >> (int); };
11324     template <int V> struct Foo {};
11325     Foo<X () >> 5> r;
11326
11327     Here 'X()' is a valid type-id of a function type, but the user just
11328     wanted to write the expression "X() >> 5". Thus, we remember that we
11329     found a valid type-id, but we still try to parse the argument as an
11330     expression to see what happens.
11331
11332     In C++0x, the '>>' will be considered two separate '>'
11333     tokens.  */
11334  if (!cp_parser_error_occurred (parser)
11335      && cxx_dialect == cxx98
11336      && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11337    {
11338      maybe_type_id = true;
11339      cp_parser_abort_tentative_parse (parser);
11340    }
11341  else
11342    {
11343      /* If the next token isn't a `,' or a `>', then this argument wasn't
11344      really finished. This means that the argument is not a valid
11345      type-id.  */
11346      if (!cp_parser_next_token_ends_template_argument_p (parser))
11347	cp_parser_error (parser, "expected template-argument");
11348      /* If that worked, we're done.  */
11349      if (cp_parser_parse_definitely (parser))
11350	return argument;
11351    }
11352  /* We're still not sure what the argument will be.  */
11353  cp_parser_parse_tentatively (parser);
11354  /* Try a template.  */
11355  argument_start_token = cp_lexer_peek_token (parser->lexer);
11356  argument = cp_parser_id_expression (parser,
11357				      /*template_keyword_p=*/false,
11358				      /*check_dependency_p=*/true,
11359				      &template_p,
11360				      /*declarator_p=*/false,
11361				      /*optional_p=*/false);
11362  /* If the next token isn't a `,' or a `>', then this argument wasn't
11363     really finished.  */
11364  if (!cp_parser_next_token_ends_template_argument_p (parser))
11365    cp_parser_error (parser, "expected template-argument");
11366  if (!cp_parser_error_occurred (parser))
11367    {
11368      /* Figure out what is being referred to.  If the id-expression
11369	 was for a class template specialization, then we will have a
11370	 TYPE_DECL at this point.  There is no need to do name lookup
11371	 at this point in that case.  */
11372      if (TREE_CODE (argument) != TYPE_DECL)
11373	argument = cp_parser_lookup_name (parser, argument,
11374					  none_type,
11375					  /*is_template=*/template_p,
11376					  /*is_namespace=*/false,
11377					  /*check_dependency=*/true,
11378					  /*ambiguous_decls=*/NULL,
11379					  argument_start_token->location);
11380      if (TREE_CODE (argument) != TEMPLATE_DECL
11381	  && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11382	cp_parser_error (parser, "expected template-name");
11383    }
11384  if (cp_parser_parse_definitely (parser))
11385    return argument;
11386  /* It must be a non-type argument.  There permitted cases are given
11387     in [temp.arg.nontype]:
11388
11389     -- an integral constant-expression of integral or enumeration
11390	type; or
11391
11392     -- the name of a non-type template-parameter; or
11393
11394     -- the name of an object or function with external linkage...
11395
11396     -- the address of an object or function with external linkage...
11397
11398     -- a pointer to member...  */
11399  /* Look for a non-type template parameter.  */
11400  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11401    {
11402      cp_parser_parse_tentatively (parser);
11403      argument = cp_parser_primary_expression (parser,
11404					       /*address_p=*/false,
11405					       /*cast_p=*/false,
11406					       /*template_arg_p=*/true,
11407					       &idk);
11408      if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11409	  || !cp_parser_next_token_ends_template_argument_p (parser))
11410	cp_parser_simulate_error (parser);
11411      if (cp_parser_parse_definitely (parser))
11412	return argument;
11413    }
11414
11415  /* If the next token is "&", the argument must be the address of an
11416     object or function with external linkage.  */
11417  address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11418  if (address_p)
11419    cp_lexer_consume_token (parser->lexer);
11420  /* See if we might have an id-expression.  */
11421  token = cp_lexer_peek_token (parser->lexer);
11422  if (token->type == CPP_NAME
11423      || token->keyword == RID_OPERATOR
11424      || token->type == CPP_SCOPE
11425      || token->type == CPP_TEMPLATE_ID
11426      || token->type == CPP_NESTED_NAME_SPECIFIER)
11427    {
11428      cp_parser_parse_tentatively (parser);
11429      argument = cp_parser_primary_expression (parser,
11430					       address_p,
11431					       /*cast_p=*/false,
11432					       /*template_arg_p=*/true,
11433					       &idk);
11434      if (cp_parser_error_occurred (parser)
11435	  || !cp_parser_next_token_ends_template_argument_p (parser))
11436	cp_parser_abort_tentative_parse (parser);
11437      else
11438	{
11439	  tree probe;
11440
11441	  if (TREE_CODE (argument) == INDIRECT_REF)
11442	    {
11443	      gcc_assert (REFERENCE_REF_P (argument));
11444	      argument = TREE_OPERAND (argument, 0);
11445	    }
11446
11447	  /* If we're in a template, we represent a qualified-id referring
11448	     to a static data member as a SCOPE_REF even if the scope isn't
11449	     dependent so that we can check access control later.  */
11450	  probe = argument;
11451	  if (TREE_CODE (probe) == SCOPE_REF)
11452	    probe = TREE_OPERAND (probe, 1);
11453	  if (TREE_CODE (probe) == VAR_DECL)
11454	    {
11455	      /* A variable without external linkage might still be a
11456		 valid constant-expression, so no error is issued here
11457		 if the external-linkage check fails.  */
11458	      if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11459		cp_parser_simulate_error (parser);
11460	    }
11461	  else if (is_overloaded_fn (argument))
11462	    /* All overloaded functions are allowed; if the external
11463	       linkage test does not pass, an error will be issued
11464	       later.  */
11465	    ;
11466	  else if (address_p
11467		   && (TREE_CODE (argument) == OFFSET_REF
11468		       || TREE_CODE (argument) == SCOPE_REF))
11469	    /* A pointer-to-member.  */
11470	    ;
11471	  else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11472	    ;
11473	  else
11474	    cp_parser_simulate_error (parser);
11475
11476	  if (cp_parser_parse_definitely (parser))
11477	    {
11478	      if (address_p)
11479		argument = build_x_unary_op (ADDR_EXPR, argument,
11480                                             tf_warning_or_error);
11481	      return argument;
11482	    }
11483	}
11484    }
11485  /* If the argument started with "&", there are no other valid
11486     alternatives at this point.  */
11487  if (address_p)
11488    {
11489      cp_parser_error (parser, "invalid non-type template argument");
11490      return error_mark_node;
11491    }
11492
11493  /* If the argument wasn't successfully parsed as a type-id followed
11494     by '>>', the argument can only be a constant expression now.
11495     Otherwise, we try parsing the constant-expression tentatively,
11496     because the argument could really be a type-id.  */
11497  if (maybe_type_id)
11498    cp_parser_parse_tentatively (parser);
11499  argument = cp_parser_constant_expression (parser,
11500					    /*allow_non_constant_p=*/false,
11501					    /*non_constant_p=*/NULL);
11502  argument = fold_non_dependent_expr (argument);
11503  if (!maybe_type_id)
11504    return argument;
11505  if (!cp_parser_next_token_ends_template_argument_p (parser))
11506    cp_parser_error (parser, "expected template-argument");
11507  if (cp_parser_parse_definitely (parser))
11508    return argument;
11509  /* We did our best to parse the argument as a non type-id, but that
11510     was the only alternative that matched (albeit with a '>' after
11511     it). We can assume it's just a typo from the user, and a
11512     diagnostic will then be issued.  */
11513  return cp_parser_template_type_arg (parser);
11514}
11515
11516/* Parse an explicit-instantiation.
11517
11518   explicit-instantiation:
11519     template declaration
11520
11521   Although the standard says `declaration', what it really means is:
11522
11523   explicit-instantiation:
11524     template decl-specifier-seq [opt] declarator [opt] ;
11525
11526   Things like `template int S<int>::i = 5, int S<double>::j;' are not
11527   supposed to be allowed.  A defect report has been filed about this
11528   issue.
11529
11530   GNU Extension:
11531
11532   explicit-instantiation:
11533     storage-class-specifier template
11534       decl-specifier-seq [opt] declarator [opt] ;
11535     function-specifier template
11536       decl-specifier-seq [opt] declarator [opt] ;  */
11537
11538static void
11539cp_parser_explicit_instantiation (cp_parser* parser)
11540{
11541  int declares_class_or_enum;
11542  cp_decl_specifier_seq decl_specifiers;
11543  tree extension_specifier = NULL_TREE;
11544
11545  /* Look for an (optional) storage-class-specifier or
11546     function-specifier.  */
11547  if (cp_parser_allow_gnu_extensions_p (parser))
11548    {
11549      extension_specifier
11550	= cp_parser_storage_class_specifier_opt (parser);
11551      if (!extension_specifier)
11552	extension_specifier
11553	  = cp_parser_function_specifier_opt (parser,
11554					      /*decl_specs=*/NULL);
11555    }
11556
11557  /* Look for the `template' keyword.  */
11558  cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11559  /* Let the front end know that we are processing an explicit
11560     instantiation.  */
11561  begin_explicit_instantiation ();
11562  /* [temp.explicit] says that we are supposed to ignore access
11563     control while processing explicit instantiation directives.  */
11564  push_deferring_access_checks (dk_no_check);
11565  /* Parse a decl-specifier-seq.  */
11566  cp_parser_decl_specifier_seq (parser,
11567				CP_PARSER_FLAGS_OPTIONAL,
11568				&decl_specifiers,
11569				&declares_class_or_enum);
11570  /* If there was exactly one decl-specifier, and it declared a class,
11571     and there's no declarator, then we have an explicit type
11572     instantiation.  */
11573  if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11574    {
11575      tree type;
11576
11577      type = check_tag_decl (&decl_specifiers);
11578      /* Turn access control back on for names used during
11579	 template instantiation.  */
11580      pop_deferring_access_checks ();
11581      if (type)
11582	do_type_instantiation (type, extension_specifier,
11583			       /*complain=*/tf_error);
11584    }
11585  else
11586    {
11587      cp_declarator *declarator;
11588      tree decl;
11589
11590      /* Parse the declarator.  */
11591      declarator
11592	= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11593				/*ctor_dtor_or_conv_p=*/NULL,
11594				/*parenthesized_p=*/NULL,
11595				/*member_p=*/false);
11596      if (declares_class_or_enum & 2)
11597	cp_parser_check_for_definition_in_return_type (declarator,
11598						       decl_specifiers.type,
11599						       decl_specifiers.type_location);
11600      if (declarator != cp_error_declarator)
11601	{
11602	  decl = grokdeclarator (declarator, &decl_specifiers,
11603				 NORMAL, 0, &decl_specifiers.attributes);
11604	  /* Turn access control back on for names used during
11605	     template instantiation.  */
11606	  pop_deferring_access_checks ();
11607	  /* Do the explicit instantiation.  */
11608	  do_decl_instantiation (decl, extension_specifier);
11609	}
11610      else
11611	{
11612	  pop_deferring_access_checks ();
11613	  /* Skip the body of the explicit instantiation.  */
11614	  cp_parser_skip_to_end_of_statement (parser);
11615	}
11616    }
11617  /* We're done with the instantiation.  */
11618  end_explicit_instantiation ();
11619
11620  cp_parser_consume_semicolon_at_end_of_statement (parser);
11621}
11622
11623/* Parse an explicit-specialization.
11624
11625   explicit-specialization:
11626     template < > declaration
11627
11628   Although the standard says `declaration', what it really means is:
11629
11630   explicit-specialization:
11631     template <> decl-specifier [opt] init-declarator [opt] ;
11632     template <> function-definition
11633     template <> explicit-specialization
11634     template <> template-declaration  */
11635
11636static void
11637cp_parser_explicit_specialization (cp_parser* parser)
11638{
11639  bool need_lang_pop;
11640  cp_token *token = cp_lexer_peek_token (parser->lexer);
11641
11642  /* Look for the `template' keyword.  */
11643  cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11644  /* Look for the `<'.  */
11645  cp_parser_require (parser, CPP_LESS, "%<<%>");
11646  /* Look for the `>'.  */
11647  cp_parser_require (parser, CPP_GREATER, "%<>%>");
11648  /* We have processed another parameter list.  */
11649  ++parser->num_template_parameter_lists;
11650  /* [temp]
11651
11652     A template ... explicit specialization ... shall not have C
11653     linkage.  */
11654  if (current_lang_name == lang_name_c)
11655    {
11656      error_at (token->location, "template specialization with C linkage");
11657      /* Give it C++ linkage to avoid confusing other parts of the
11658	 front end.  */
11659      push_lang_context (lang_name_cplusplus);
11660      need_lang_pop = true;
11661    }
11662  else
11663    need_lang_pop = false;
11664  /* Let the front end know that we are beginning a specialization.  */
11665  if (!begin_specialization ())
11666    {
11667      end_specialization ();
11668      return;
11669    }
11670
11671  /* If the next keyword is `template', we need to figure out whether
11672     or not we're looking a template-declaration.  */
11673  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11674    {
11675      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11676	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11677	cp_parser_template_declaration_after_export (parser,
11678						     /*member_p=*/false);
11679      else
11680	cp_parser_explicit_specialization (parser);
11681    }
11682  else
11683    /* Parse the dependent declaration.  */
11684    cp_parser_single_declaration (parser,
11685				  /*checks=*/NULL,
11686				  /*member_p=*/false,
11687                                  /*explicit_specialization_p=*/true,
11688				  /*friend_p=*/NULL);
11689  /* We're done with the specialization.  */
11690  end_specialization ();
11691  /* For the erroneous case of a template with C linkage, we pushed an
11692     implicit C++ linkage scope; exit that scope now.  */
11693  if (need_lang_pop)
11694    pop_lang_context ();
11695  /* We're done with this parameter list.  */
11696  --parser->num_template_parameter_lists;
11697}
11698
11699/* Parse a type-specifier.
11700
11701   type-specifier:
11702     simple-type-specifier
11703     class-specifier
11704     enum-specifier
11705     elaborated-type-specifier
11706     cv-qualifier
11707
11708   GNU Extension:
11709
11710   type-specifier:
11711     __complex__
11712
11713   Returns a representation of the type-specifier.  For a
11714   class-specifier, enum-specifier, or elaborated-type-specifier, a
11715   TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11716
11717   The parser flags FLAGS is used to control type-specifier parsing.
11718
11719   If IS_DECLARATION is TRUE, then this type-specifier is appearing
11720   in a decl-specifier-seq.
11721
11722   If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11723   class-specifier, enum-specifier, or elaborated-type-specifier, then
11724   *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11725   if a type is declared; 2 if it is defined.  Otherwise, it is set to
11726   zero.
11727
11728   If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11729   cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11730   is set to FALSE.  */
11731
11732static tree
11733cp_parser_type_specifier (cp_parser* parser,
11734			  cp_parser_flags flags,
11735			  cp_decl_specifier_seq *decl_specs,
11736			  bool is_declaration,
11737			  int* declares_class_or_enum,
11738			  bool* is_cv_qualifier)
11739{
11740  tree type_spec = NULL_TREE;
11741  cp_token *token;
11742  enum rid keyword;
11743  cp_decl_spec ds = ds_last;
11744
11745  /* Assume this type-specifier does not declare a new type.  */
11746  if (declares_class_or_enum)
11747    *declares_class_or_enum = 0;
11748  /* And that it does not specify a cv-qualifier.  */
11749  if (is_cv_qualifier)
11750    *is_cv_qualifier = false;
11751  /* Peek at the next token.  */
11752  token = cp_lexer_peek_token (parser->lexer);
11753
11754  /* If we're looking at a keyword, we can use that to guide the
11755     production we choose.  */
11756  keyword = token->keyword;
11757  switch (keyword)
11758    {
11759    case RID_ENUM:
11760      if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11761	goto elaborated_type_specifier;
11762
11763      /* Look for the enum-specifier.  */
11764      type_spec = cp_parser_enum_specifier (parser);
11765      /* If that worked, we're done.  */
11766      if (type_spec)
11767	{
11768	  if (declares_class_or_enum)
11769	    *declares_class_or_enum = 2;
11770	  if (decl_specs)
11771	    cp_parser_set_decl_spec_type (decl_specs,
11772					  type_spec,
11773					  token->location,
11774					  /*user_defined_p=*/true);
11775	  return type_spec;
11776	}
11777      else
11778	goto elaborated_type_specifier;
11779
11780      /* Any of these indicate either a class-specifier, or an
11781	 elaborated-type-specifier.  */
11782    case RID_CLASS:
11783    case RID_STRUCT:
11784    case RID_UNION:
11785      if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11786	goto elaborated_type_specifier;
11787
11788      /* Parse tentatively so that we can back up if we don't find a
11789	 class-specifier.  */
11790      cp_parser_parse_tentatively (parser);
11791      /* Look for the class-specifier.  */
11792      type_spec = cp_parser_class_specifier (parser);
11793      invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11794      /* If that worked, we're done.  */
11795      if (cp_parser_parse_definitely (parser))
11796	{
11797	  if (declares_class_or_enum)
11798	    *declares_class_or_enum = 2;
11799	  if (decl_specs)
11800	    cp_parser_set_decl_spec_type (decl_specs,
11801					  type_spec,
11802					  token->location,
11803					  /*user_defined_p=*/true);
11804	  return type_spec;
11805	}
11806
11807      /* Fall through.  */
11808    elaborated_type_specifier:
11809      /* We're declaring (not defining) a class or enum.  */
11810      if (declares_class_or_enum)
11811	*declares_class_or_enum = 1;
11812
11813      /* Fall through.  */
11814    case RID_TYPENAME:
11815      /* Look for an elaborated-type-specifier.  */
11816      type_spec
11817	= (cp_parser_elaborated_type_specifier
11818	   (parser,
11819	    decl_specs && decl_specs->specs[(int) ds_friend],
11820	    is_declaration));
11821      if (decl_specs)
11822	cp_parser_set_decl_spec_type (decl_specs,
11823				      type_spec,
11824				      token->location,
11825				      /*user_defined_p=*/true);
11826      return type_spec;
11827
11828    case RID_CONST:
11829      ds = ds_const;
11830      if (is_cv_qualifier)
11831	*is_cv_qualifier = true;
11832      break;
11833
11834    case RID_VOLATILE:
11835      ds = ds_volatile;
11836      if (is_cv_qualifier)
11837	*is_cv_qualifier = true;
11838      break;
11839
11840    case RID_RESTRICT:
11841      ds = ds_restrict;
11842      if (is_cv_qualifier)
11843	*is_cv_qualifier = true;
11844      break;
11845
11846    case RID_COMPLEX:
11847      /* The `__complex__' keyword is a GNU extension.  */
11848      ds = ds_complex;
11849      break;
11850
11851    default:
11852      break;
11853    }
11854
11855  /* Handle simple keywords.  */
11856  if (ds != ds_last)
11857    {
11858      if (decl_specs)
11859	{
11860	  ++decl_specs->specs[(int)ds];
11861	  decl_specs->any_specifiers_p = true;
11862	}
11863      return cp_lexer_consume_token (parser->lexer)->u.value;
11864    }
11865
11866  /* If we do not already have a type-specifier, assume we are looking
11867     at a simple-type-specifier.  */
11868  type_spec = cp_parser_simple_type_specifier (parser,
11869					       decl_specs,
11870					       flags);
11871
11872  /* If we didn't find a type-specifier, and a type-specifier was not
11873     optional in this context, issue an error message.  */
11874  if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11875    {
11876      cp_parser_error (parser, "expected type specifier");
11877      return error_mark_node;
11878    }
11879
11880  return type_spec;
11881}
11882
11883/* Parse a simple-type-specifier.
11884
11885   simple-type-specifier:
11886     :: [opt] nested-name-specifier [opt] type-name
11887     :: [opt] nested-name-specifier template template-id
11888     char
11889     wchar_t
11890     bool
11891     short
11892     int
11893     long
11894     signed
11895     unsigned
11896     float
11897     double
11898     void
11899
11900   C++0x Extension:
11901
11902   simple-type-specifier:
11903     auto
11904     decltype ( expression )
11905     char16_t
11906     char32_t
11907
11908   GNU Extension:
11909
11910   simple-type-specifier:
11911     __typeof__ unary-expression
11912     __typeof__ ( type-id )
11913
11914   Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11915   appropriately updated.  */
11916
11917static tree
11918cp_parser_simple_type_specifier (cp_parser* parser,
11919				 cp_decl_specifier_seq *decl_specs,
11920				 cp_parser_flags flags)
11921{
11922  tree type = NULL_TREE;
11923  cp_token *token;
11924
11925  /* Peek at the next token.  */
11926  token = cp_lexer_peek_token (parser->lexer);
11927
11928  /* If we're looking at a keyword, things are easy.  */
11929  switch (token->keyword)
11930    {
11931    case RID_CHAR:
11932      if (decl_specs)
11933	decl_specs->explicit_char_p = true;
11934      type = char_type_node;
11935      break;
11936    case RID_CHAR16:
11937      type = char16_type_node;
11938      break;
11939    case RID_CHAR32:
11940      type = char32_type_node;
11941      break;
11942    case RID_WCHAR:
11943      type = wchar_type_node;
11944      break;
11945    case RID_BOOL:
11946      type = boolean_type_node;
11947      break;
11948    case RID_SHORT:
11949      if (decl_specs)
11950	++decl_specs->specs[(int) ds_short];
11951      type = short_integer_type_node;
11952      break;
11953    case RID_INT:
11954      if (decl_specs)
11955	decl_specs->explicit_int_p = true;
11956      type = integer_type_node;
11957      break;
11958    case RID_LONG:
11959      if (decl_specs)
11960	++decl_specs->specs[(int) ds_long];
11961      type = long_integer_type_node;
11962      break;
11963    case RID_SIGNED:
11964      if (decl_specs)
11965	++decl_specs->specs[(int) ds_signed];
11966      type = integer_type_node;
11967      break;
11968    case RID_UNSIGNED:
11969      if (decl_specs)
11970	++decl_specs->specs[(int) ds_unsigned];
11971      type = unsigned_type_node;
11972      break;
11973    case RID_FLOAT:
11974      type = float_type_node;
11975      break;
11976    case RID_DOUBLE:
11977      type = double_type_node;
11978      break;
11979    case RID_VOID:
11980      type = void_type_node;
11981      break;
11982
11983    case RID_AUTO:
11984      maybe_warn_cpp0x (CPP0X_AUTO);
11985      type = make_auto ();
11986      break;
11987
11988    case RID_DECLTYPE:
11989      /* Parse the `decltype' type.  */
11990      type = cp_parser_decltype (parser);
11991
11992      if (decl_specs)
11993	cp_parser_set_decl_spec_type (decl_specs, type,
11994				      token->location,
11995				      /*user_defined_p=*/true);
11996
11997      return type;
11998
11999    case RID_TYPEOF:
12000      /* Consume the `typeof' token.  */
12001      cp_lexer_consume_token (parser->lexer);
12002      /* Parse the operand to `typeof'.  */
12003      type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12004      /* If it is not already a TYPE, take its type.  */
12005      if (!TYPE_P (type))
12006	type = finish_typeof (type);
12007
12008      if (decl_specs)
12009	cp_parser_set_decl_spec_type (decl_specs, type,
12010				      token->location,
12011				      /*user_defined_p=*/true);
12012
12013      return type;
12014
12015    default:
12016      break;
12017    }
12018
12019  /* If the type-specifier was for a built-in type, we're done.  */
12020  if (type)
12021    {
12022      /* Record the type.  */
12023      if (decl_specs
12024	  && (token->keyword != RID_SIGNED
12025	      && token->keyword != RID_UNSIGNED
12026	      && token->keyword != RID_SHORT
12027	      && token->keyword != RID_LONG))
12028	cp_parser_set_decl_spec_type (decl_specs,
12029				      type,
12030				      token->location,
12031				      /*user_defined=*/false);
12032      if (decl_specs)
12033	decl_specs->any_specifiers_p = true;
12034
12035      /* Consume the token.  */
12036      cp_lexer_consume_token (parser->lexer);
12037
12038      /* There is no valid C++ program where a non-template type is
12039	 followed by a "<".  That usually indicates that the user thought
12040	 that the type was a template.  */
12041      cp_parser_check_for_invalid_template_id (parser, type, token->location);
12042
12043      return TYPE_NAME (type);
12044    }
12045
12046  /* The type-specifier must be a user-defined type.  */
12047  if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12048    {
12049      bool qualified_p;
12050      bool global_p;
12051
12052      /* Don't gobble tokens or issue error messages if this is an
12053	 optional type-specifier.  */
12054      if (flags & CP_PARSER_FLAGS_OPTIONAL)
12055	cp_parser_parse_tentatively (parser);
12056
12057      /* Look for the optional `::' operator.  */
12058      global_p
12059	= (cp_parser_global_scope_opt (parser,
12060				       /*current_scope_valid_p=*/false)
12061	   != NULL_TREE);
12062      /* Look for the nested-name specifier.  */
12063      qualified_p
12064	= (cp_parser_nested_name_specifier_opt (parser,
12065						/*typename_keyword_p=*/false,
12066						/*check_dependency_p=*/true,
12067						/*type_p=*/false,
12068						/*is_declaration=*/false)
12069	   != NULL_TREE);
12070      token = cp_lexer_peek_token (parser->lexer);
12071      /* If we have seen a nested-name-specifier, and the next token
12072	 is `template', then we are using the template-id production.  */
12073      if (parser->scope
12074	  && cp_parser_optional_template_keyword (parser))
12075	{
12076	  /* Look for the template-id.  */
12077	  type = cp_parser_template_id (parser,
12078					/*template_keyword_p=*/true,
12079					/*check_dependency_p=*/true,
12080					/*is_declaration=*/false);
12081	  /* If the template-id did not name a type, we are out of
12082	     luck.  */
12083	  if (TREE_CODE (type) != TYPE_DECL)
12084	    {
12085	      cp_parser_error (parser, "expected template-id for type");
12086	      type = NULL_TREE;
12087	    }
12088	}
12089      /* Otherwise, look for a type-name.  */
12090      else
12091	type = cp_parser_type_name (parser);
12092      /* Keep track of all name-lookups performed in class scopes.  */
12093      if (type
12094	  && !global_p
12095	  && !qualified_p
12096	  && TREE_CODE (type) == TYPE_DECL
12097	  && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12098	maybe_note_name_used_in_class (DECL_NAME (type), type);
12099      /* If it didn't work out, we don't have a TYPE.  */
12100      if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12101	  && !cp_parser_parse_definitely (parser))
12102	type = NULL_TREE;
12103      if (type && decl_specs)
12104	cp_parser_set_decl_spec_type (decl_specs, type,
12105				      token->location,
12106				      /*user_defined=*/true);
12107    }
12108
12109  /* If we didn't get a type-name, issue an error message.  */
12110  if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12111    {
12112      cp_parser_error (parser, "expected type-name");
12113      return error_mark_node;
12114    }
12115
12116  /* There is no valid C++ program where a non-template type is
12117     followed by a "<".  That usually indicates that the user thought
12118     that the type was a template.  */
12119  if (type && type != error_mark_node)
12120    {
12121      /* As a last-ditch effort, see if TYPE is an Objective-C type.
12122	 If it is, then the '<'...'>' enclose protocol names rather than
12123	 template arguments, and so everything is fine.  */
12124      if (c_dialect_objc ()
12125	  && (objc_is_id (type) || objc_is_class_name (type)))
12126	{
12127	  tree protos = cp_parser_objc_protocol_refs_opt (parser);
12128	  tree qual_type = objc_get_protocol_qualified_type (type, protos);
12129
12130	  /* Clobber the "unqualified" type previously entered into
12131	     DECL_SPECS with the new, improved protocol-qualified version.  */
12132	  if (decl_specs)
12133	    decl_specs->type = qual_type;
12134
12135	  return qual_type;
12136	}
12137
12138      cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12139					       token->location);
12140    }
12141
12142  return type;
12143}
12144
12145/* Parse a type-name.
12146
12147   type-name:
12148     class-name
12149     enum-name
12150     typedef-name
12151
12152   enum-name:
12153     identifier
12154
12155   typedef-name:
12156     identifier
12157
12158   Returns a TYPE_DECL for the type.  */
12159
12160static tree
12161cp_parser_type_name (cp_parser* parser)
12162{
12163  tree type_decl;
12164
12165  /* We can't know yet whether it is a class-name or not.  */
12166  cp_parser_parse_tentatively (parser);
12167  /* Try a class-name.  */
12168  type_decl = cp_parser_class_name (parser,
12169				    /*typename_keyword_p=*/false,
12170				    /*template_keyword_p=*/false,
12171				    none_type,
12172				    /*check_dependency_p=*/true,
12173				    /*class_head_p=*/false,
12174				    /*is_declaration=*/false);
12175  /* If it's not a class-name, keep looking.  */
12176  if (!cp_parser_parse_definitely (parser))
12177    {
12178      /* It must be a typedef-name or an enum-name.  */
12179      return cp_parser_nonclass_name (parser);
12180    }
12181
12182  return type_decl;
12183}
12184
12185/* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12186
12187   enum-name:
12188     identifier
12189
12190   typedef-name:
12191     identifier
12192
12193   Returns a TYPE_DECL for the type.  */
12194
12195static tree
12196cp_parser_nonclass_name (cp_parser* parser)
12197{
12198  tree type_decl;
12199  tree identifier;
12200
12201  cp_token *token = cp_lexer_peek_token (parser->lexer);
12202  identifier = cp_parser_identifier (parser);
12203  if (identifier == error_mark_node)
12204    return error_mark_node;
12205
12206  /* Look up the type-name.  */
12207  type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12208
12209  if (TREE_CODE (type_decl) != TYPE_DECL
12210      && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12211    {
12212      /* See if this is an Objective-C type.  */
12213      tree protos = cp_parser_objc_protocol_refs_opt (parser);
12214      tree type = objc_get_protocol_qualified_type (identifier, protos);
12215      if (type)
12216	type_decl = TYPE_NAME (type);
12217    }
12218
12219  /* Issue an error if we did not find a type-name.  */
12220  if (TREE_CODE (type_decl) != TYPE_DECL)
12221    {
12222      if (!cp_parser_simulate_error (parser))
12223	cp_parser_name_lookup_error (parser, identifier, type_decl,
12224				     "is not a type", token->location);
12225      return error_mark_node;
12226    }
12227  /* Remember that the name was used in the definition of the
12228     current class so that we can check later to see if the
12229     meaning would have been different after the class was
12230     entirely defined.  */
12231  else if (type_decl != error_mark_node
12232	   && !parser->scope)
12233    maybe_note_name_used_in_class (identifier, type_decl);
12234
12235  return type_decl;
12236}
12237
12238/* Parse an elaborated-type-specifier.  Note that the grammar given
12239   here incorporates the resolution to DR68.
12240
12241   elaborated-type-specifier:
12242     class-key :: [opt] nested-name-specifier [opt] identifier
12243     class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12244     enum-key :: [opt] nested-name-specifier [opt] identifier
12245     typename :: [opt] nested-name-specifier identifier
12246     typename :: [opt] nested-name-specifier template [opt]
12247       template-id
12248
12249   GNU extension:
12250
12251   elaborated-type-specifier:
12252     class-key attributes :: [opt] nested-name-specifier [opt] identifier
12253     class-key attributes :: [opt] nested-name-specifier [opt]
12254	       template [opt] template-id
12255     enum attributes :: [opt] nested-name-specifier [opt] identifier
12256
12257   If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12258   declared `friend'.  If IS_DECLARATION is TRUE, then this
12259   elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12260   something is being declared.
12261
12262   Returns the TYPE specified.  */
12263
12264static tree
12265cp_parser_elaborated_type_specifier (cp_parser* parser,
12266				     bool is_friend,
12267				     bool is_declaration)
12268{
12269  enum tag_types tag_type;
12270  tree identifier;
12271  tree type = NULL_TREE;
12272  tree attributes = NULL_TREE;
12273  tree globalscope;
12274  cp_token *token = NULL;
12275
12276  /* See if we're looking at the `enum' keyword.  */
12277  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12278    {
12279      /* Consume the `enum' token.  */
12280      cp_lexer_consume_token (parser->lexer);
12281      /* Remember that it's an enumeration type.  */
12282      tag_type = enum_type;
12283      /* Parse the optional `struct' or `class' key (for C++0x scoped
12284         enums).  */
12285      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12286          || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12287        {
12288          if (cxx_dialect == cxx98)
12289            maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12290
12291          /* Consume the `struct' or `class'.  */
12292          cp_lexer_consume_token (parser->lexer);
12293        }
12294      /* Parse the attributes.  */
12295      attributes = cp_parser_attributes_opt (parser);
12296    }
12297  /* Or, it might be `typename'.  */
12298  else if (cp_lexer_next_token_is_keyword (parser->lexer,
12299					   RID_TYPENAME))
12300    {
12301      /* Consume the `typename' token.  */
12302      cp_lexer_consume_token (parser->lexer);
12303      /* Remember that it's a `typename' type.  */
12304      tag_type = typename_type;
12305    }
12306  /* Otherwise it must be a class-key.  */
12307  else
12308    {
12309      tag_type = cp_parser_class_key (parser);
12310      if (tag_type == none_type)
12311	return error_mark_node;
12312      /* Parse the attributes.  */
12313      attributes = cp_parser_attributes_opt (parser);
12314    }
12315
12316  /* Look for the `::' operator.  */
12317  globalscope =  cp_parser_global_scope_opt (parser,
12318					     /*current_scope_valid_p=*/false);
12319  /* Look for the nested-name-specifier.  */
12320  if (tag_type == typename_type && !globalscope)
12321    {
12322      if (!cp_parser_nested_name_specifier (parser,
12323					   /*typename_keyword_p=*/true,
12324					   /*check_dependency_p=*/true,
12325					   /*type_p=*/true,
12326					    is_declaration))
12327	return error_mark_node;
12328    }
12329  else
12330    /* Even though `typename' is not present, the proposed resolution
12331       to Core Issue 180 says that in `class A<T>::B', `B' should be
12332       considered a type-name, even if `A<T>' is dependent.  */
12333    cp_parser_nested_name_specifier_opt (parser,
12334					 /*typename_keyword_p=*/true,
12335					 /*check_dependency_p=*/true,
12336					 /*type_p=*/true,
12337					 is_declaration);
12338 /* For everything but enumeration types, consider a template-id.
12339    For an enumeration type, consider only a plain identifier.  */
12340  if (tag_type != enum_type)
12341    {
12342      bool template_p = false;
12343      tree decl;
12344
12345      /* Allow the `template' keyword.  */
12346      template_p = cp_parser_optional_template_keyword (parser);
12347      /* If we didn't see `template', we don't know if there's a
12348	 template-id or not.  */
12349      if (!template_p)
12350	cp_parser_parse_tentatively (parser);
12351      /* Parse the template-id.  */
12352      token = cp_lexer_peek_token (parser->lexer);
12353      decl = cp_parser_template_id (parser, template_p,
12354				    /*check_dependency_p=*/true,
12355				    is_declaration);
12356      /* If we didn't find a template-id, look for an ordinary
12357	 identifier.  */
12358      if (!template_p && !cp_parser_parse_definitely (parser))
12359	;
12360      /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12361	 in effect, then we must assume that, upon instantiation, the
12362	 template will correspond to a class.  */
12363      else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12364	       && tag_type == typename_type)
12365	type = make_typename_type (parser->scope, decl,
12366				   typename_type,
12367				   /*complain=*/tf_error);
12368      /* If the `typename' keyword is in effect and DECL is not a type
12369	 decl. Then type is non existant.   */
12370      else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12371        type = NULL_TREE;
12372      else
12373	type = TREE_TYPE (decl);
12374    }
12375
12376  if (!type)
12377    {
12378      token = cp_lexer_peek_token (parser->lexer);
12379      identifier = cp_parser_identifier (parser);
12380
12381      if (identifier == error_mark_node)
12382	{
12383	  parser->scope = NULL_TREE;
12384	  return error_mark_node;
12385	}
12386
12387      /* For a `typename', we needn't call xref_tag.  */
12388      if (tag_type == typename_type
12389	  && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12390	return cp_parser_make_typename_type (parser, parser->scope,
12391					     identifier,
12392					     token->location);
12393      /* Look up a qualified name in the usual way.  */
12394      if (parser->scope)
12395	{
12396	  tree decl;
12397	  tree ambiguous_decls;
12398
12399	  decl = cp_parser_lookup_name (parser, identifier,
12400					tag_type,
12401					/*is_template=*/false,
12402					/*is_namespace=*/false,
12403					/*check_dependency=*/true,
12404					&ambiguous_decls,
12405					token->location);
12406
12407	  /* If the lookup was ambiguous, an error will already have been
12408	     issued.  */
12409	  if (ambiguous_decls)
12410	    return error_mark_node;
12411
12412	  /* If we are parsing friend declaration, DECL may be a
12413	     TEMPLATE_DECL tree node here.  However, we need to check
12414	     whether this TEMPLATE_DECL results in valid code.  Consider
12415	     the following example:
12416
12417	       namespace N {
12418		 template <class T> class C {};
12419	       }
12420	       class X {
12421		 template <class T> friend class N::C; // #1, valid code
12422	       };
12423	       template <class T> class Y {
12424		 friend class N::C;		       // #2, invalid code
12425	       };
12426
12427	     For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12428	     name lookup of `N::C'.  We see that friend declaration must
12429	     be template for the code to be valid.  Note that
12430	     processing_template_decl does not work here since it is
12431	     always 1 for the above two cases.  */
12432
12433	  decl = (cp_parser_maybe_treat_template_as_class
12434		  (decl, /*tag_name_p=*/is_friend
12435			 && parser->num_template_parameter_lists));
12436
12437	  if (TREE_CODE (decl) != TYPE_DECL)
12438	    {
12439	      cp_parser_diagnose_invalid_type_name (parser,
12440						    parser->scope,
12441						    identifier,
12442						    token->location);
12443	      return error_mark_node;
12444	    }
12445
12446	  if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12447            {
12448              bool allow_template = (parser->num_template_parameter_lists
12449		                      || DECL_SELF_REFERENCE_P (decl));
12450              type = check_elaborated_type_specifier (tag_type, decl,
12451                                                      allow_template);
12452
12453              if (type == error_mark_node)
12454                return error_mark_node;
12455            }
12456
12457          /* Forward declarations of nested types, such as
12458
12459               class C1::C2;
12460               class C1::C2::C3;
12461
12462             are invalid unless all components preceding the final '::'
12463             are complete.  If all enclosing types are complete, these
12464             declarations become merely pointless.
12465
12466             Invalid forward declarations of nested types are errors
12467             caught elsewhere in parsing.  Those that are pointless arrive
12468             here.  */
12469
12470          if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12471              && !is_friend && !processing_explicit_instantiation)
12472            warning (0, "declaration %qD does not declare anything", decl);
12473
12474	  type = TREE_TYPE (decl);
12475	}
12476      else
12477	{
12478	  /* An elaborated-type-specifier sometimes introduces a new type and
12479	     sometimes names an existing type.  Normally, the rule is that it
12480	     introduces a new type only if there is not an existing type of
12481	     the same name already in scope.  For example, given:
12482
12483	       struct S {};
12484	       void f() { struct S s; }
12485
12486	     the `struct S' in the body of `f' is the same `struct S' as in
12487	     the global scope; the existing definition is used.  However, if
12488	     there were no global declaration, this would introduce a new
12489	     local class named `S'.
12490
12491	     An exception to this rule applies to the following code:
12492
12493	       namespace N { struct S; }
12494
12495	     Here, the elaborated-type-specifier names a new type
12496	     unconditionally; even if there is already an `S' in the
12497	     containing scope this declaration names a new type.
12498	     This exception only applies if the elaborated-type-specifier
12499	     forms the complete declaration:
12500
12501	       [class.name]
12502
12503	       A declaration consisting solely of `class-key identifier ;' is
12504	       either a redeclaration of the name in the current scope or a
12505	       forward declaration of the identifier as a class name.  It
12506	       introduces the name into the current scope.
12507
12508	     We are in this situation precisely when the next token is a `;'.
12509
12510	     An exception to the exception is that a `friend' declaration does
12511	     *not* name a new type; i.e., given:
12512
12513	       struct S { friend struct T; };
12514
12515	     `T' is not a new type in the scope of `S'.
12516
12517	     Also, `new struct S' or `sizeof (struct S)' never results in the
12518	     definition of a new type; a new type can only be declared in a
12519	     declaration context.  */
12520
12521	  tag_scope ts;
12522	  bool template_p;
12523
12524	  if (is_friend)
12525	    /* Friends have special name lookup rules.  */
12526	    ts = ts_within_enclosing_non_class;
12527	  else if (is_declaration
12528		   && cp_lexer_next_token_is (parser->lexer,
12529					      CPP_SEMICOLON))
12530	    /* This is a `class-key identifier ;' */
12531	    ts = ts_current;
12532	  else
12533	    ts = ts_global;
12534
12535	  template_p =
12536	    (parser->num_template_parameter_lists
12537	     && (cp_parser_next_token_starts_class_definition_p (parser)
12538		 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12539	  /* An unqualified name was used to reference this type, so
12540	     there were no qualifying templates.  */
12541	  if (!cp_parser_check_template_parameters (parser,
12542						    /*num_templates=*/0,
12543						    token->location,
12544						    /*declarator=*/NULL))
12545	    return error_mark_node;
12546	  type = xref_tag (tag_type, identifier, ts, template_p);
12547	}
12548    }
12549
12550  if (type == error_mark_node)
12551    return error_mark_node;
12552
12553  /* Allow attributes on forward declarations of classes.  */
12554  if (attributes)
12555    {
12556      if (TREE_CODE (type) == TYPENAME_TYPE)
12557	warning (OPT_Wattributes,
12558		 "attributes ignored on uninstantiated type");
12559      else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12560	       && ! processing_explicit_instantiation)
12561	warning (OPT_Wattributes,
12562		 "attributes ignored on template instantiation");
12563      else if (is_declaration && cp_parser_declares_only_class_p (parser))
12564	cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12565      else
12566	warning (OPT_Wattributes,
12567		 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12568    }
12569
12570  if (tag_type != enum_type)
12571    cp_parser_check_class_key (tag_type, type);
12572
12573  /* A "<" cannot follow an elaborated type specifier.  If that
12574     happens, the user was probably trying to form a template-id.  */
12575  cp_parser_check_for_invalid_template_id (parser, type, token->location);
12576
12577  return type;
12578}
12579
12580/* Parse an enum-specifier.
12581
12582   enum-specifier:
12583     enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12584
12585   enum-key:
12586     enum
12587     enum class   [C++0x]
12588     enum struct  [C++0x]
12589
12590   enum-base:   [C++0x]
12591     : type-specifier-seq
12592
12593   GNU Extensions:
12594     enum-key attributes[opt] identifier [opt] enum-base [opt]
12595       { enumerator-list [opt] }attributes[opt]
12596
12597   Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12598   if the token stream isn't an enum-specifier after all.  */
12599
12600static tree
12601cp_parser_enum_specifier (cp_parser* parser)
12602{
12603  tree identifier;
12604  tree type;
12605  tree attributes;
12606  bool scoped_enum_p = false;
12607  bool has_underlying_type = false;
12608  tree underlying_type = NULL_TREE;
12609
12610  /* Parse tentatively so that we can back up if we don't find a
12611     enum-specifier.  */
12612  cp_parser_parse_tentatively (parser);
12613
12614  /* Caller guarantees that the current token is 'enum', an identifier
12615     possibly follows, and the token after that is an opening brace.
12616     If we don't have an identifier, fabricate an anonymous name for
12617     the enumeration being defined.  */
12618  cp_lexer_consume_token (parser->lexer);
12619
12620  /* Parse the "class" or "struct", which indicates a scoped
12621     enumeration type in C++0x.  */
12622  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12623      || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12624    {
12625      if (cxx_dialect == cxx98)
12626        maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12627
12628      /* Consume the `struct' or `class' token.  */
12629      cp_lexer_consume_token (parser->lexer);
12630
12631      scoped_enum_p = true;
12632    }
12633
12634  attributes = cp_parser_attributes_opt (parser);
12635
12636  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12637    identifier = cp_parser_identifier (parser);
12638  else
12639    identifier = make_anon_name ();
12640
12641  /* Check for the `:' that denotes a specified underlying type in C++0x.
12642     Note that a ':' could also indicate a bitfield width, however.  */
12643  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12644    {
12645      cp_decl_specifier_seq type_specifiers;
12646
12647      /* Consume the `:'.  */
12648      cp_lexer_consume_token (parser->lexer);
12649
12650      /* Parse the type-specifier-seq.  */
12651      cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12652				    /*is_trailing_return=*/false,
12653                                    &type_specifiers);
12654
12655      /* At this point this is surely not elaborated type specifier.  */
12656      if (!cp_parser_parse_definitely (parser))
12657	return NULL_TREE;
12658
12659      if (cxx_dialect == cxx98)
12660        maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12661
12662      has_underlying_type = true;
12663
12664      /* If that didn't work, stop.  */
12665      if (type_specifiers.type != error_mark_node)
12666        {
12667          underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12668                                            /*initialized=*/0, NULL);
12669          if (underlying_type == error_mark_node)
12670            underlying_type = NULL_TREE;
12671        }
12672    }
12673
12674  /* Look for the `{' but don't consume it yet.  */
12675  if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12676    {
12677      cp_parser_error (parser, "expected %<{%>");
12678      if (has_underlying_type)
12679	return NULL_TREE;
12680    }
12681
12682  if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12683    return NULL_TREE;
12684
12685  /* Issue an error message if type-definitions are forbidden here.  */
12686  if (!cp_parser_check_type_definition (parser))
12687    type = error_mark_node;
12688  else
12689    /* Create the new type.  We do this before consuming the opening
12690       brace so the enum will be recorded as being on the line of its
12691       tag (or the 'enum' keyword, if there is no tag).  */
12692    type = start_enum (identifier, underlying_type, scoped_enum_p);
12693
12694  /* Consume the opening brace.  */
12695  cp_lexer_consume_token (parser->lexer);
12696
12697  if (type == error_mark_node)
12698    {
12699      cp_parser_skip_to_end_of_block_or_statement (parser);
12700      return error_mark_node;
12701    }
12702
12703  /* If the next token is not '}', then there are some enumerators.  */
12704  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12705    cp_parser_enumerator_list (parser, type);
12706
12707  /* Consume the final '}'.  */
12708  cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12709
12710  /* Look for trailing attributes to apply to this enumeration, and
12711     apply them if appropriate.  */
12712  if (cp_parser_allow_gnu_extensions_p (parser))
12713    {
12714      tree trailing_attr = cp_parser_attributes_opt (parser);
12715      trailing_attr = chainon (trailing_attr, attributes);
12716      cplus_decl_attributes (&type,
12717			     trailing_attr,
12718			     (int) ATTR_FLAG_TYPE_IN_PLACE);
12719    }
12720
12721  /* Finish up the enumeration.  */
12722  finish_enum (type);
12723
12724  return type;
12725}
12726
12727/* Parse an enumerator-list.  The enumerators all have the indicated
12728   TYPE.
12729
12730   enumerator-list:
12731     enumerator-definition
12732     enumerator-list , enumerator-definition  */
12733
12734static void
12735cp_parser_enumerator_list (cp_parser* parser, tree type)
12736{
12737  while (true)
12738    {
12739      /* Parse an enumerator-definition.  */
12740      cp_parser_enumerator_definition (parser, type);
12741
12742      /* If the next token is not a ',', we've reached the end of
12743	 the list.  */
12744      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12745	break;
12746      /* Otherwise, consume the `,' and keep going.  */
12747      cp_lexer_consume_token (parser->lexer);
12748      /* If the next token is a `}', there is a trailing comma.  */
12749      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12750	{
12751	  if (!in_system_header)
12752	    pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12753	  break;
12754	}
12755    }
12756}
12757
12758/* Parse an enumerator-definition.  The enumerator has the indicated
12759   TYPE.
12760
12761   enumerator-definition:
12762     enumerator
12763     enumerator = constant-expression
12764
12765   enumerator:
12766     identifier  */
12767
12768static void
12769cp_parser_enumerator_definition (cp_parser* parser, tree type)
12770{
12771  tree identifier;
12772  tree value;
12773
12774  /* Look for the identifier.  */
12775  identifier = cp_parser_identifier (parser);
12776  if (identifier == error_mark_node)
12777    return;
12778
12779  /* If the next token is an '=', then there is an explicit value.  */
12780  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12781    {
12782      /* Consume the `=' token.  */
12783      cp_lexer_consume_token (parser->lexer);
12784      /* Parse the value.  */
12785      value = cp_parser_constant_expression (parser,
12786					     /*allow_non_constant_p=*/false,
12787					     NULL);
12788    }
12789  else
12790    value = NULL_TREE;
12791
12792  /* If we are processing a template, make sure the initializer of the
12793     enumerator doesn't contain any bare template parameter pack.  */
12794  if (check_for_bare_parameter_packs (value))
12795    value = error_mark_node;
12796
12797  /* Create the enumerator.  */
12798  build_enumerator (identifier, value, type);
12799}
12800
12801/* Parse a namespace-name.
12802
12803   namespace-name:
12804     original-namespace-name
12805     namespace-alias
12806
12807   Returns the NAMESPACE_DECL for the namespace.  */
12808
12809static tree
12810cp_parser_namespace_name (cp_parser* parser)
12811{
12812  tree identifier;
12813  tree namespace_decl;
12814
12815  cp_token *token = cp_lexer_peek_token (parser->lexer);
12816
12817  /* Get the name of the namespace.  */
12818  identifier = cp_parser_identifier (parser);
12819  if (identifier == error_mark_node)
12820    return error_mark_node;
12821
12822  /* Look up the identifier in the currently active scope.  Look only
12823     for namespaces, due to:
12824
12825       [basic.lookup.udir]
12826
12827       When looking up a namespace-name in a using-directive or alias
12828       definition, only namespace names are considered.
12829
12830     And:
12831
12832       [basic.lookup.qual]
12833
12834       During the lookup of a name preceding the :: scope resolution
12835       operator, object, function, and enumerator names are ignored.
12836
12837     (Note that cp_parser_qualifying_entity only calls this
12838     function if the token after the name is the scope resolution
12839     operator.)  */
12840  namespace_decl = cp_parser_lookup_name (parser, identifier,
12841					  none_type,
12842					  /*is_template=*/false,
12843					  /*is_namespace=*/true,
12844					  /*check_dependency=*/true,
12845					  /*ambiguous_decls=*/NULL,
12846					  token->location);
12847  /* If it's not a namespace, issue an error.  */
12848  if (namespace_decl == error_mark_node
12849      || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12850    {
12851      if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12852	error_at (token->location, "%qD is not a namespace-name", identifier);
12853      cp_parser_error (parser, "expected namespace-name");
12854      namespace_decl = error_mark_node;
12855    }
12856
12857  return namespace_decl;
12858}
12859
12860/* Parse a namespace-definition.
12861
12862   namespace-definition:
12863     named-namespace-definition
12864     unnamed-namespace-definition
12865
12866   named-namespace-definition:
12867     original-namespace-definition
12868     extension-namespace-definition
12869
12870   original-namespace-definition:
12871     namespace identifier { namespace-body }
12872
12873   extension-namespace-definition:
12874     namespace original-namespace-name { namespace-body }
12875
12876   unnamed-namespace-definition:
12877     namespace { namespace-body } */
12878
12879static void
12880cp_parser_namespace_definition (cp_parser* parser)
12881{
12882  tree identifier, attribs;
12883  bool has_visibility;
12884  bool is_inline;
12885
12886  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12887    {
12888      is_inline = true;
12889      cp_lexer_consume_token (parser->lexer);
12890    }
12891  else
12892    is_inline = false;
12893
12894  /* Look for the `namespace' keyword.  */
12895  cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12896
12897  /* Get the name of the namespace.  We do not attempt to distinguish
12898     between an original-namespace-definition and an
12899     extension-namespace-definition at this point.  The semantic
12900     analysis routines are responsible for that.  */
12901  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12902    identifier = cp_parser_identifier (parser);
12903  else
12904    identifier = NULL_TREE;
12905
12906  /* Parse any specified attributes.  */
12907  attribs = cp_parser_attributes_opt (parser);
12908
12909  /* Look for the `{' to start the namespace.  */
12910  cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12911  /* Start the namespace.  */
12912  push_namespace (identifier);
12913
12914  /* "inline namespace" is equivalent to a stub namespace definition
12915     followed by a strong using directive.  */
12916  if (is_inline)
12917    {
12918      tree name_space = current_namespace;
12919      /* Set up namespace association.  */
12920      DECL_NAMESPACE_ASSOCIATIONS (name_space)
12921	= tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12922		     DECL_NAMESPACE_ASSOCIATIONS (name_space));
12923      /* Import the contents of the inline namespace.  */
12924      pop_namespace ();
12925      do_using_directive (name_space);
12926      push_namespace (identifier);
12927    }
12928
12929  has_visibility = handle_namespace_attrs (current_namespace, attribs);
12930
12931  /* Parse the body of the namespace.  */
12932  cp_parser_namespace_body (parser);
12933
12934#ifdef HANDLE_PRAGMA_VISIBILITY
12935  if (has_visibility)
12936    pop_visibility (1);
12937#endif
12938
12939  /* Finish the namespace.  */
12940  pop_namespace ();
12941  /* Look for the final `}'.  */
12942  cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12943}
12944
12945/* Parse a namespace-body.
12946
12947   namespace-body:
12948     declaration-seq [opt]  */
12949
12950static void
12951cp_parser_namespace_body (cp_parser* parser)
12952{
12953  cp_parser_declaration_seq_opt (parser);
12954}
12955
12956/* Parse a namespace-alias-definition.
12957
12958   namespace-alias-definition:
12959     namespace identifier = qualified-namespace-specifier ;  */
12960
12961static void
12962cp_parser_namespace_alias_definition (cp_parser* parser)
12963{
12964  tree identifier;
12965  tree namespace_specifier;
12966
12967  cp_token *token = cp_lexer_peek_token (parser->lexer);
12968
12969  /* Look for the `namespace' keyword.  */
12970  cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12971  /* Look for the identifier.  */
12972  identifier = cp_parser_identifier (parser);
12973  if (identifier == error_mark_node)
12974    return;
12975  /* Look for the `=' token.  */
12976  if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12977      && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12978    {
12979      error_at (token->location, "%<namespace%> definition is not allowed here");
12980      /* Skip the definition.  */
12981      cp_lexer_consume_token (parser->lexer);
12982      if (cp_parser_skip_to_closing_brace (parser))
12983	cp_lexer_consume_token (parser->lexer);
12984      return;
12985    }
12986  cp_parser_require (parser, CPP_EQ, "%<=%>");
12987  /* Look for the qualified-namespace-specifier.  */
12988  namespace_specifier
12989    = cp_parser_qualified_namespace_specifier (parser);
12990  /* Look for the `;' token.  */
12991  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12992
12993  /* Register the alias in the symbol table.  */
12994  do_namespace_alias (identifier, namespace_specifier);
12995}
12996
12997/* Parse a qualified-namespace-specifier.
12998
12999   qualified-namespace-specifier:
13000     :: [opt] nested-name-specifier [opt] namespace-name
13001
13002   Returns a NAMESPACE_DECL corresponding to the specified
13003   namespace.  */
13004
13005static tree
13006cp_parser_qualified_namespace_specifier (cp_parser* parser)
13007{
13008  /* Look for the optional `::'.  */
13009  cp_parser_global_scope_opt (parser,
13010			      /*current_scope_valid_p=*/false);
13011
13012  /* Look for the optional nested-name-specifier.  */
13013  cp_parser_nested_name_specifier_opt (parser,
13014				       /*typename_keyword_p=*/false,
13015				       /*check_dependency_p=*/true,
13016				       /*type_p=*/false,
13017				       /*is_declaration=*/true);
13018
13019  return cp_parser_namespace_name (parser);
13020}
13021
13022/* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13023   access declaration.
13024
13025   using-declaration:
13026     using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13027     using :: unqualified-id ;
13028
13029   access-declaration:
13030     qualified-id ;
13031
13032   */
13033
13034static bool
13035cp_parser_using_declaration (cp_parser* parser,
13036			     bool access_declaration_p)
13037{
13038  cp_token *token;
13039  bool typename_p = false;
13040  bool global_scope_p;
13041  tree decl;
13042  tree identifier;
13043  tree qscope;
13044
13045  if (access_declaration_p)
13046    cp_parser_parse_tentatively (parser);
13047  else
13048    {
13049      /* Look for the `using' keyword.  */
13050      cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13051
13052      /* Peek at the next token.  */
13053      token = cp_lexer_peek_token (parser->lexer);
13054      /* See if it's `typename'.  */
13055      if (token->keyword == RID_TYPENAME)
13056	{
13057	  /* Remember that we've seen it.  */
13058	  typename_p = true;
13059	  /* Consume the `typename' token.  */
13060	  cp_lexer_consume_token (parser->lexer);
13061	}
13062    }
13063
13064  /* Look for the optional global scope qualification.  */
13065  global_scope_p
13066    = (cp_parser_global_scope_opt (parser,
13067				   /*current_scope_valid_p=*/false)
13068       != NULL_TREE);
13069
13070  /* If we saw `typename', or didn't see `::', then there must be a
13071     nested-name-specifier present.  */
13072  if (typename_p || !global_scope_p)
13073    qscope = cp_parser_nested_name_specifier (parser, typename_p,
13074					      /*check_dependency_p=*/true,
13075					      /*type_p=*/false,
13076					      /*is_declaration=*/true);
13077  /* Otherwise, we could be in either of the two productions.  In that
13078     case, treat the nested-name-specifier as optional.  */
13079  else
13080    qscope = cp_parser_nested_name_specifier_opt (parser,
13081						  /*typename_keyword_p=*/false,
13082						  /*check_dependency_p=*/true,
13083						  /*type_p=*/false,
13084						  /*is_declaration=*/true);
13085  if (!qscope)
13086    qscope = global_namespace;
13087
13088  if (access_declaration_p && cp_parser_error_occurred (parser))
13089    /* Something has already gone wrong; there's no need to parse
13090       further.  Since an error has occurred, the return value of
13091       cp_parser_parse_definitely will be false, as required.  */
13092    return cp_parser_parse_definitely (parser);
13093
13094  token = cp_lexer_peek_token (parser->lexer);
13095  /* Parse the unqualified-id.  */
13096  identifier = cp_parser_unqualified_id (parser,
13097					 /*template_keyword_p=*/false,
13098					 /*check_dependency_p=*/true,
13099					 /*declarator_p=*/true,
13100					 /*optional_p=*/false);
13101
13102  if (access_declaration_p)
13103    {
13104      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13105	cp_parser_simulate_error (parser);
13106      if (!cp_parser_parse_definitely (parser))
13107	return false;
13108    }
13109
13110  /* The function we call to handle a using-declaration is different
13111     depending on what scope we are in.  */
13112  if (qscope == error_mark_node || identifier == error_mark_node)
13113    ;
13114  else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13115	   && TREE_CODE (identifier) != BIT_NOT_EXPR)
13116    /* [namespace.udecl]
13117
13118       A using declaration shall not name a template-id.  */
13119    error_at (token->location,
13120	      "a template-id may not appear in a using-declaration");
13121  else
13122    {
13123      if (at_class_scope_p ())
13124	{
13125	  /* Create the USING_DECL.  */
13126	  decl = do_class_using_decl (parser->scope, identifier);
13127
13128	  if (check_for_bare_parameter_packs (decl))
13129            return false;
13130          else
13131	    /* Add it to the list of members in this class.  */
13132	    finish_member_declaration (decl);
13133	}
13134      else
13135	{
13136	  decl = cp_parser_lookup_name_simple (parser,
13137					       identifier,
13138					       token->location);
13139	  if (decl == error_mark_node)
13140	    cp_parser_name_lookup_error (parser, identifier,
13141					 decl, NULL,
13142					 token->location);
13143	  else if (check_for_bare_parameter_packs (decl))
13144	    return false;
13145	  else if (!at_namespace_scope_p ())
13146	    do_local_using_decl (decl, qscope, identifier);
13147	  else
13148	    do_toplevel_using_decl (decl, qscope, identifier);
13149	}
13150    }
13151
13152  /* Look for the final `;'.  */
13153  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13154
13155  return true;
13156}
13157
13158/* Parse a using-directive.
13159
13160   using-directive:
13161     using namespace :: [opt] nested-name-specifier [opt]
13162       namespace-name ;  */
13163
13164static void
13165cp_parser_using_directive (cp_parser* parser)
13166{
13167  tree namespace_decl;
13168  tree attribs;
13169
13170  /* Look for the `using' keyword.  */
13171  cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13172  /* And the `namespace' keyword.  */
13173  cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13174  /* Look for the optional `::' operator.  */
13175  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13176  /* And the optional nested-name-specifier.  */
13177  cp_parser_nested_name_specifier_opt (parser,
13178				       /*typename_keyword_p=*/false,
13179				       /*check_dependency_p=*/true,
13180				       /*type_p=*/false,
13181				       /*is_declaration=*/true);
13182  /* Get the namespace being used.  */
13183  namespace_decl = cp_parser_namespace_name (parser);
13184  /* And any specified attributes.  */
13185  attribs = cp_parser_attributes_opt (parser);
13186  /* Update the symbol table.  */
13187  parse_using_directive (namespace_decl, attribs);
13188  /* Look for the final `;'.  */
13189  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13190}
13191
13192/* Parse an asm-definition.
13193
13194   asm-definition:
13195     asm ( string-literal ) ;
13196
13197   GNU Extension:
13198
13199   asm-definition:
13200     asm volatile [opt] ( string-literal ) ;
13201     asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13202     asm volatile [opt] ( string-literal : asm-operand-list [opt]
13203			  : asm-operand-list [opt] ) ;
13204     asm volatile [opt] ( string-literal : asm-operand-list [opt]
13205			  : asm-operand-list [opt]
13206			  : asm-clobber-list [opt] ) ;
13207     asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13208			       : asm-clobber-list [opt]
13209			       : asm-goto-list ) ;  */
13210
13211static void
13212cp_parser_asm_definition (cp_parser* parser)
13213{
13214  tree string;
13215  tree outputs = NULL_TREE;
13216  tree inputs = NULL_TREE;
13217  tree clobbers = NULL_TREE;
13218  tree labels = NULL_TREE;
13219  tree asm_stmt;
13220  bool volatile_p = false;
13221  bool extended_p = false;
13222  bool invalid_inputs_p = false;
13223  bool invalid_outputs_p = false;
13224  bool goto_p = false;
13225  const char *missing = NULL;
13226
13227  /* Look for the `asm' keyword.  */
13228  cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13229  /* See if the next token is `volatile'.  */
13230  if (cp_parser_allow_gnu_extensions_p (parser)
13231      && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13232    {
13233      /* Remember that we saw the `volatile' keyword.  */
13234      volatile_p = true;
13235      /* Consume the token.  */
13236      cp_lexer_consume_token (parser->lexer);
13237    }
13238  if (cp_parser_allow_gnu_extensions_p (parser)
13239      && parser->in_function_body
13240      && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13241    {
13242      /* Remember that we saw the `goto' keyword.  */
13243      goto_p = true;
13244      /* Consume the token.  */
13245      cp_lexer_consume_token (parser->lexer);
13246    }
13247  /* Look for the opening `('.  */
13248  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13249    return;
13250  /* Look for the string.  */
13251  string = cp_parser_string_literal (parser, false, false);
13252  if (string == error_mark_node)
13253    {
13254      cp_parser_skip_to_closing_parenthesis (parser, true, false,
13255					     /*consume_paren=*/true);
13256      return;
13257    }
13258
13259  /* If we're allowing GNU extensions, check for the extended assembly
13260     syntax.  Unfortunately, the `:' tokens need not be separated by
13261     a space in C, and so, for compatibility, we tolerate that here
13262     too.  Doing that means that we have to treat the `::' operator as
13263     two `:' tokens.  */
13264  if (cp_parser_allow_gnu_extensions_p (parser)
13265      && parser->in_function_body
13266      && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13267	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13268    {
13269      bool inputs_p = false;
13270      bool clobbers_p = false;
13271      bool labels_p = false;
13272
13273      /* The extended syntax was used.  */
13274      extended_p = true;
13275
13276      /* Look for outputs.  */
13277      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13278	{
13279	  /* Consume the `:'.  */
13280	  cp_lexer_consume_token (parser->lexer);
13281	  /* Parse the output-operands.  */
13282	  if (cp_lexer_next_token_is_not (parser->lexer,
13283					  CPP_COLON)
13284	      && cp_lexer_next_token_is_not (parser->lexer,
13285					     CPP_SCOPE)
13286	      && cp_lexer_next_token_is_not (parser->lexer,
13287					     CPP_CLOSE_PAREN)
13288	      && !goto_p)
13289	    outputs = cp_parser_asm_operand_list (parser);
13290
13291	    if (outputs == error_mark_node)
13292	      invalid_outputs_p = true;
13293	}
13294      /* If the next token is `::', there are no outputs, and the
13295	 next token is the beginning of the inputs.  */
13296      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13297	/* The inputs are coming next.  */
13298	inputs_p = true;
13299
13300      /* Look for inputs.  */
13301      if (inputs_p
13302	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13303	{
13304	  /* Consume the `:' or `::'.  */
13305	  cp_lexer_consume_token (parser->lexer);
13306	  /* Parse the output-operands.  */
13307	  if (cp_lexer_next_token_is_not (parser->lexer,
13308					  CPP_COLON)
13309	      && cp_lexer_next_token_is_not (parser->lexer,
13310					     CPP_SCOPE)
13311	      && cp_lexer_next_token_is_not (parser->lexer,
13312					     CPP_CLOSE_PAREN))
13313	    inputs = cp_parser_asm_operand_list (parser);
13314
13315	    if (inputs == error_mark_node)
13316	      invalid_inputs_p = true;
13317	}
13318      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13319	/* The clobbers are coming next.  */
13320	clobbers_p = true;
13321
13322      /* Look for clobbers.  */
13323      if (clobbers_p
13324	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13325	{
13326	  clobbers_p = true;
13327	  /* Consume the `:' or `::'.  */
13328	  cp_lexer_consume_token (parser->lexer);
13329	  /* Parse the clobbers.  */
13330	  if (cp_lexer_next_token_is_not (parser->lexer,
13331					  CPP_COLON)
13332	      && cp_lexer_next_token_is_not (parser->lexer,
13333					     CPP_CLOSE_PAREN))
13334	    clobbers = cp_parser_asm_clobber_list (parser);
13335	}
13336      else if (goto_p
13337	       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13338	/* The labels are coming next.  */
13339	labels_p = true;
13340
13341      /* Look for labels.  */
13342      if (labels_p
13343	  || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13344	{
13345	  labels_p = true;
13346	  /* Consume the `:' or `::'.  */
13347	  cp_lexer_consume_token (parser->lexer);
13348	  /* Parse the labels.  */
13349	  labels = cp_parser_asm_label_list (parser);
13350	}
13351
13352      if (goto_p && !labels_p)
13353	missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13354    }
13355  else if (goto_p)
13356    missing = "%<:%> or %<::%>";
13357
13358  /* Look for the closing `)'.  */
13359  if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13360			  missing ? missing : "%<)%>"))
13361    cp_parser_skip_to_closing_parenthesis (parser, true, false,
13362					   /*consume_paren=*/true);
13363  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13364
13365  if (!invalid_inputs_p && !invalid_outputs_p)
13366    {
13367      /* Create the ASM_EXPR.  */
13368      if (parser->in_function_body)
13369	{
13370	  asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13371				      inputs, clobbers, labels);
13372	  /* If the extended syntax was not used, mark the ASM_EXPR.  */
13373	  if (!extended_p)
13374	    {
13375	      tree temp = asm_stmt;
13376	      if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13377		temp = TREE_OPERAND (temp, 0);
13378
13379	      ASM_INPUT_P (temp) = 1;
13380	    }
13381	}
13382      else
13383	cgraph_add_asm_node (string);
13384    }
13385}
13386
13387/* Declarators [gram.dcl.decl] */
13388
13389/* Parse an init-declarator.
13390
13391   init-declarator:
13392     declarator initializer [opt]
13393
13394   GNU Extension:
13395
13396   init-declarator:
13397     declarator asm-specification [opt] attributes [opt] initializer [opt]
13398
13399   function-definition:
13400     decl-specifier-seq [opt] declarator ctor-initializer [opt]
13401       function-body
13402     decl-specifier-seq [opt] declarator function-try-block
13403
13404   GNU Extension:
13405
13406   function-definition:
13407     __extension__ function-definition
13408
13409   The DECL_SPECIFIERS apply to this declarator.  Returns a
13410   representation of the entity declared.  If MEMBER_P is TRUE, then
13411   this declarator appears in a class scope.  The new DECL created by
13412   this declarator is returned.
13413
13414   The CHECKS are access checks that should be performed once we know
13415   what entity is being declared (and, therefore, what classes have
13416   befriended it).
13417
13418   If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13419   for a function-definition here as well.  If the declarator is a
13420   declarator for a function-definition, *FUNCTION_DEFINITION_P will
13421   be TRUE upon return.  By that point, the function-definition will
13422   have been completely parsed.
13423
13424   FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13425   is FALSE.  */
13426
13427static tree
13428cp_parser_init_declarator (cp_parser* parser,
13429			   cp_decl_specifier_seq *decl_specifiers,
13430			   VEC (deferred_access_check,gc)* checks,
13431			   bool function_definition_allowed_p,
13432			   bool member_p,
13433			   int declares_class_or_enum,
13434			   bool* function_definition_p)
13435{
13436  cp_token *token = NULL, *asm_spec_start_token = NULL,
13437           *attributes_start_token = NULL;
13438  cp_declarator *declarator;
13439  tree prefix_attributes;
13440  tree attributes;
13441  tree asm_specification;
13442  tree initializer;
13443  tree decl = NULL_TREE;
13444  tree scope;
13445  int is_initialized;
13446  /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13447     initialized with "= ..", CPP_OPEN_PAREN if initialized with
13448     "(...)".  */
13449  enum cpp_ttype initialization_kind;
13450  bool is_direct_init = false;
13451  bool is_non_constant_init;
13452  int ctor_dtor_or_conv_p;
13453  bool friend_p;
13454  tree pushed_scope = NULL;
13455
13456  /* Gather the attributes that were provided with the
13457     decl-specifiers.  */
13458  prefix_attributes = decl_specifiers->attributes;
13459
13460  /* Assume that this is not the declarator for a function
13461     definition.  */
13462  if (function_definition_p)
13463    *function_definition_p = false;
13464
13465  /* Defer access checks while parsing the declarator; we cannot know
13466     what names are accessible until we know what is being
13467     declared.  */
13468  resume_deferring_access_checks ();
13469
13470  /* Parse the declarator.  */
13471  token = cp_lexer_peek_token (parser->lexer);
13472  declarator
13473    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13474			    &ctor_dtor_or_conv_p,
13475			    /*parenthesized_p=*/NULL,
13476			    /*member_p=*/false);
13477  /* Gather up the deferred checks.  */
13478  stop_deferring_access_checks ();
13479
13480  /* If the DECLARATOR was erroneous, there's no need to go
13481     further.  */
13482  if (declarator == cp_error_declarator)
13483    return error_mark_node;
13484
13485  /* Check that the number of template-parameter-lists is OK.  */
13486  if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13487						       token->location))
13488    return error_mark_node;
13489
13490  if (declares_class_or_enum & 2)
13491    cp_parser_check_for_definition_in_return_type (declarator,
13492						   decl_specifiers->type,
13493						   decl_specifiers->type_location);
13494
13495  /* Figure out what scope the entity declared by the DECLARATOR is
13496     located in.  `grokdeclarator' sometimes changes the scope, so
13497     we compute it now.  */
13498  scope = get_scope_of_declarator (declarator);
13499
13500  /* Perform any lookups in the declared type which were thought to be
13501     dependent, but are not in the scope of the declarator.  */
13502  decl_specifiers->type
13503    = maybe_update_decl_type (decl_specifiers->type, scope);
13504
13505  /* If we're allowing GNU extensions, look for an asm-specification
13506     and attributes.  */
13507  if (cp_parser_allow_gnu_extensions_p (parser))
13508    {
13509      /* Look for an asm-specification.  */
13510      asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13511      asm_specification = cp_parser_asm_specification_opt (parser);
13512      /* And attributes.  */
13513      attributes_start_token = cp_lexer_peek_token (parser->lexer);
13514      attributes = cp_parser_attributes_opt (parser);
13515    }
13516  else
13517    {
13518      asm_specification = NULL_TREE;
13519      attributes = NULL_TREE;
13520    }
13521
13522  /* Peek at the next token.  */
13523  token = cp_lexer_peek_token (parser->lexer);
13524  /* Check to see if the token indicates the start of a
13525     function-definition.  */
13526  if (function_declarator_p (declarator)
13527      && cp_parser_token_starts_function_definition_p (token))
13528    {
13529      if (!function_definition_allowed_p)
13530	{
13531	  /* If a function-definition should not appear here, issue an
13532	     error message.  */
13533	  cp_parser_error (parser,
13534			   "a function-definition is not allowed here");
13535	  return error_mark_node;
13536	}
13537      else
13538	{
13539	  location_t func_brace_location
13540	    = cp_lexer_peek_token (parser->lexer)->location;
13541
13542	  /* Neither attributes nor an asm-specification are allowed
13543	     on a function-definition.  */
13544	  if (asm_specification)
13545	    error_at (asm_spec_start_token->location,
13546		      "an asm-specification is not allowed "
13547		      "on a function-definition");
13548	  if (attributes)
13549	    error_at (attributes_start_token->location,
13550		      "attributes are not allowed on a function-definition");
13551	  /* This is a function-definition.  */
13552	  *function_definition_p = true;
13553
13554	  /* Parse the function definition.  */
13555	  if (member_p)
13556	    decl = cp_parser_save_member_function_body (parser,
13557							decl_specifiers,
13558							declarator,
13559							prefix_attributes);
13560	  else
13561	    decl
13562	      = (cp_parser_function_definition_from_specifiers_and_declarator
13563		 (parser, decl_specifiers, prefix_attributes, declarator));
13564
13565	  if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13566	    {
13567	      /* This is where the prologue starts...  */
13568	      DECL_STRUCT_FUNCTION (decl)->function_start_locus
13569		= func_brace_location;
13570	    }
13571
13572	  return decl;
13573	}
13574    }
13575
13576  /* [dcl.dcl]
13577
13578     Only in function declarations for constructors, destructors, and
13579     type conversions can the decl-specifier-seq be omitted.
13580
13581     We explicitly postpone this check past the point where we handle
13582     function-definitions because we tolerate function-definitions
13583     that are missing their return types in some modes.  */
13584  if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13585    {
13586      cp_parser_error (parser,
13587		       "expected constructor, destructor, or type conversion");
13588      return error_mark_node;
13589    }
13590
13591  /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13592  if (token->type == CPP_EQ
13593      || token->type == CPP_OPEN_PAREN
13594      || token->type == CPP_OPEN_BRACE)
13595    {
13596      is_initialized = SD_INITIALIZED;
13597      initialization_kind = token->type;
13598
13599      if (token->type == CPP_EQ
13600	  && function_declarator_p (declarator))
13601	{
13602	  cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13603	  if (t2->keyword == RID_DEFAULT)
13604	    is_initialized = SD_DEFAULTED;
13605	  else if (t2->keyword == RID_DELETE)
13606	    is_initialized = SD_DELETED;
13607	}
13608    }
13609  else
13610    {
13611      /* If the init-declarator isn't initialized and isn't followed by a
13612	 `,' or `;', it's not a valid init-declarator.  */
13613      if (token->type != CPP_COMMA
13614	  && token->type != CPP_SEMICOLON)
13615	{
13616	  cp_parser_error (parser, "expected initializer");
13617	  return error_mark_node;
13618	}
13619      is_initialized = SD_UNINITIALIZED;
13620      initialization_kind = CPP_EOF;
13621    }
13622
13623  /* Because start_decl has side-effects, we should only call it if we
13624     know we're going ahead.  By this point, we know that we cannot
13625     possibly be looking at any other construct.  */
13626  cp_parser_commit_to_tentative_parse (parser);
13627
13628  /* If the decl specifiers were bad, issue an error now that we're
13629     sure this was intended to be a declarator.  Then continue
13630     declaring the variable(s), as int, to try to cut down on further
13631     errors.  */
13632  if (decl_specifiers->any_specifiers_p
13633      && decl_specifiers->type == error_mark_node)
13634    {
13635      cp_parser_error (parser, "invalid type in declaration");
13636      decl_specifiers->type = integer_type_node;
13637    }
13638
13639  /* Check to see whether or not this declaration is a friend.  */
13640  friend_p = cp_parser_friend_p (decl_specifiers);
13641
13642  /* Enter the newly declared entry in the symbol table.  If we're
13643     processing a declaration in a class-specifier, we wait until
13644     after processing the initializer.  */
13645  if (!member_p)
13646    {
13647      if (parser->in_unbraced_linkage_specification_p)
13648	decl_specifiers->storage_class = sc_extern;
13649      decl = start_decl (declarator, decl_specifiers,
13650			 is_initialized, attributes, prefix_attributes,
13651			 &pushed_scope);
13652    }
13653  else if (scope)
13654    /* Enter the SCOPE.  That way unqualified names appearing in the
13655       initializer will be looked up in SCOPE.  */
13656    pushed_scope = push_scope (scope);
13657
13658  /* Perform deferred access control checks, now that we know in which
13659     SCOPE the declared entity resides.  */
13660  if (!member_p && decl)
13661    {
13662      tree saved_current_function_decl = NULL_TREE;
13663
13664      /* If the entity being declared is a function, pretend that we
13665	 are in its scope.  If it is a `friend', it may have access to
13666	 things that would not otherwise be accessible.  */
13667      if (TREE_CODE (decl) == FUNCTION_DECL)
13668	{
13669	  saved_current_function_decl = current_function_decl;
13670	  current_function_decl = decl;
13671	}
13672
13673      /* Perform access checks for template parameters.  */
13674      cp_parser_perform_template_parameter_access_checks (checks);
13675
13676      /* Perform the access control checks for the declarator and the
13677	 decl-specifiers.  */
13678      perform_deferred_access_checks ();
13679
13680      /* Restore the saved value.  */
13681      if (TREE_CODE (decl) == FUNCTION_DECL)
13682	current_function_decl = saved_current_function_decl;
13683    }
13684
13685  /* Parse the initializer.  */
13686  initializer = NULL_TREE;
13687  is_direct_init = false;
13688  is_non_constant_init = true;
13689  if (is_initialized)
13690    {
13691      if (function_declarator_p (declarator))
13692	{
13693	  cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13694	   if (initialization_kind == CPP_EQ)
13695	     initializer = cp_parser_pure_specifier (parser);
13696	   else
13697	     {
13698	       /* If the declaration was erroneous, we don't really
13699		  know what the user intended, so just silently
13700		  consume the initializer.  */
13701	       if (decl != error_mark_node)
13702		 error_at (initializer_start_token->location,
13703			   "initializer provided for function");
13704	       cp_parser_skip_to_closing_parenthesis (parser,
13705						      /*recovering=*/true,
13706						      /*or_comma=*/false,
13707						      /*consume_paren=*/true);
13708	     }
13709	}
13710      else
13711	{
13712	  /* We want to record the extra mangling scope for in-class
13713	     initializers of class members and initializers of static data
13714	     member templates.  The former is a C++0x feature which isn't
13715	     implemented yet, and I expect it will involve deferring
13716	     parsing of the initializer until end of class as with default
13717	     arguments.  So right here we only handle the latter.  */
13718	  if (!member_p && processing_template_decl)
13719	    start_lambda_scope (decl);
13720	  initializer = cp_parser_initializer (parser,
13721					       &is_direct_init,
13722					       &is_non_constant_init);
13723	  if (!member_p && processing_template_decl)
13724	    finish_lambda_scope ();
13725	}
13726    }
13727
13728  /* The old parser allows attributes to appear after a parenthesized
13729     initializer.  Mark Mitchell proposed removing this functionality
13730     on the GCC mailing lists on 2002-08-13.  This parser accepts the
13731     attributes -- but ignores them.  */
13732  if (cp_parser_allow_gnu_extensions_p (parser)
13733      && initialization_kind == CPP_OPEN_PAREN)
13734    if (cp_parser_attributes_opt (parser))
13735      warning (OPT_Wattributes,
13736	       "attributes after parenthesized initializer ignored");
13737
13738  /* For an in-class declaration, use `grokfield' to create the
13739     declaration.  */
13740  if (member_p)
13741    {
13742      if (pushed_scope)
13743	{
13744	  pop_scope (pushed_scope);
13745	  pushed_scope = false;
13746	}
13747      decl = grokfield (declarator, decl_specifiers,
13748			initializer, !is_non_constant_init,
13749			/*asmspec=*/NULL_TREE,
13750			prefix_attributes);
13751      if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13752	cp_parser_save_default_args (parser, decl);
13753    }
13754
13755  /* Finish processing the declaration.  But, skip friend
13756     declarations.  */
13757  if (!friend_p && decl && decl != error_mark_node)
13758    {
13759      cp_finish_decl (decl,
13760		      initializer, !is_non_constant_init,
13761		      asm_specification,
13762		      /* If the initializer is in parentheses, then this is
13763			 a direct-initialization, which means that an
13764			 `explicit' constructor is OK.  Otherwise, an
13765			 `explicit' constructor cannot be used.  */
13766		      ((is_direct_init || !is_initialized)
13767		       ? 0 : LOOKUP_ONLYCONVERTING));
13768    }
13769  else if ((cxx_dialect != cxx98) && friend_p
13770	   && decl && TREE_CODE (decl) == FUNCTION_DECL)
13771    /* Core issue #226 (C++0x only): A default template-argument
13772       shall not be specified in a friend class template
13773       declaration. */
13774    check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13775                             /*is_partial=*/0, /*is_friend_decl=*/1);
13776
13777  if (!friend_p && pushed_scope)
13778    pop_scope (pushed_scope);
13779
13780  return decl;
13781}
13782
13783/* Parse a declarator.
13784
13785   declarator:
13786     direct-declarator
13787     ptr-operator declarator
13788
13789   abstract-declarator:
13790     ptr-operator abstract-declarator [opt]
13791     direct-abstract-declarator
13792
13793   GNU Extensions:
13794
13795   declarator:
13796     attributes [opt] direct-declarator
13797     attributes [opt] ptr-operator declarator
13798
13799   abstract-declarator:
13800     attributes [opt] ptr-operator abstract-declarator [opt]
13801     attributes [opt] direct-abstract-declarator
13802
13803   If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13804   detect constructor, destructor or conversion operators. It is set
13805   to -1 if the declarator is a name, and +1 if it is a
13806   function. Otherwise it is set to zero. Usually you just want to
13807   test for >0, but internally the negative value is used.
13808
13809   (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13810   a decl-specifier-seq unless it declares a constructor, destructor,
13811   or conversion.  It might seem that we could check this condition in
13812   semantic analysis, rather than parsing, but that makes it difficult
13813   to handle something like `f()'.  We want to notice that there are
13814   no decl-specifiers, and therefore realize that this is an
13815   expression, not a declaration.)
13816
13817   If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13818   the declarator is a direct-declarator of the form "(...)".
13819
13820   MEMBER_P is true iff this declarator is a member-declarator.  */
13821
13822static cp_declarator *
13823cp_parser_declarator (cp_parser* parser,
13824		      cp_parser_declarator_kind dcl_kind,
13825		      int* ctor_dtor_or_conv_p,
13826		      bool* parenthesized_p,
13827		      bool member_p)
13828{
13829  cp_declarator *declarator;
13830  enum tree_code code;
13831  cp_cv_quals cv_quals;
13832  tree class_type;
13833  tree attributes = NULL_TREE;
13834
13835  /* Assume this is not a constructor, destructor, or type-conversion
13836     operator.  */
13837  if (ctor_dtor_or_conv_p)
13838    *ctor_dtor_or_conv_p = 0;
13839
13840  if (cp_parser_allow_gnu_extensions_p (parser))
13841    attributes = cp_parser_attributes_opt (parser);
13842
13843  /* Check for the ptr-operator production.  */
13844  cp_parser_parse_tentatively (parser);
13845  /* Parse the ptr-operator.  */
13846  code = cp_parser_ptr_operator (parser,
13847				 &class_type,
13848				 &cv_quals);
13849  /* If that worked, then we have a ptr-operator.  */
13850  if (cp_parser_parse_definitely (parser))
13851    {
13852      /* If a ptr-operator was found, then this declarator was not
13853	 parenthesized.  */
13854      if (parenthesized_p)
13855	*parenthesized_p = true;
13856      /* The dependent declarator is optional if we are parsing an
13857	 abstract-declarator.  */
13858      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13859	cp_parser_parse_tentatively (parser);
13860
13861      /* Parse the dependent declarator.  */
13862      declarator = cp_parser_declarator (parser, dcl_kind,
13863					 /*ctor_dtor_or_conv_p=*/NULL,
13864					 /*parenthesized_p=*/NULL,
13865					 /*member_p=*/false);
13866
13867      /* If we are parsing an abstract-declarator, we must handle the
13868	 case where the dependent declarator is absent.  */
13869      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13870	  && !cp_parser_parse_definitely (parser))
13871	declarator = NULL;
13872
13873      declarator = cp_parser_make_indirect_declarator
13874	(code, class_type, cv_quals, declarator);
13875    }
13876  /* Everything else is a direct-declarator.  */
13877  else
13878    {
13879      if (parenthesized_p)
13880	*parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13881						   CPP_OPEN_PAREN);
13882      declarator = cp_parser_direct_declarator (parser, dcl_kind,
13883						ctor_dtor_or_conv_p,
13884						member_p);
13885    }
13886
13887  if (attributes && declarator && declarator != cp_error_declarator)
13888    declarator->attributes = attributes;
13889
13890  return declarator;
13891}
13892
13893/* Parse a direct-declarator or direct-abstract-declarator.
13894
13895   direct-declarator:
13896     declarator-id
13897     direct-declarator ( parameter-declaration-clause )
13898       cv-qualifier-seq [opt]
13899       exception-specification [opt]
13900     direct-declarator [ constant-expression [opt] ]
13901     ( declarator )
13902
13903   direct-abstract-declarator:
13904     direct-abstract-declarator [opt]
13905       ( parameter-declaration-clause )
13906       cv-qualifier-seq [opt]
13907       exception-specification [opt]
13908     direct-abstract-declarator [opt] [ constant-expression [opt] ]
13909     ( abstract-declarator )
13910
13911   Returns a representation of the declarator.  DCL_KIND is
13912   CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13913   direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13914   we are parsing a direct-declarator.  It is
13915   CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13916   of ambiguity we prefer an abstract declarator, as per
13917   [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13918   cp_parser_declarator.  */
13919
13920static cp_declarator *
13921cp_parser_direct_declarator (cp_parser* parser,
13922			     cp_parser_declarator_kind dcl_kind,
13923			     int* ctor_dtor_or_conv_p,
13924			     bool member_p)
13925{
13926  cp_token *token;
13927  cp_declarator *declarator = NULL;
13928  tree scope = NULL_TREE;
13929  bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13930  bool saved_in_declarator_p = parser->in_declarator_p;
13931  bool first = true;
13932  tree pushed_scope = NULL_TREE;
13933
13934  while (true)
13935    {
13936      /* Peek at the next token.  */
13937      token = cp_lexer_peek_token (parser->lexer);
13938      if (token->type == CPP_OPEN_PAREN)
13939	{
13940	  /* This is either a parameter-declaration-clause, or a
13941	     parenthesized declarator. When we know we are parsing a
13942	     named declarator, it must be a parenthesized declarator
13943	     if FIRST is true. For instance, `(int)' is a
13944	     parameter-declaration-clause, with an omitted
13945	     direct-abstract-declarator. But `((*))', is a
13946	     parenthesized abstract declarator. Finally, when T is a
13947	     template parameter `(T)' is a
13948	     parameter-declaration-clause, and not a parenthesized
13949	     named declarator.
13950
13951	     We first try and parse a parameter-declaration-clause,
13952	     and then try a nested declarator (if FIRST is true).
13953
13954	     It is not an error for it not to be a
13955	     parameter-declaration-clause, even when FIRST is
13956	     false. Consider,
13957
13958	       int i (int);
13959	       int i (3);
13960
13961	     The first is the declaration of a function while the
13962	     second is the definition of a variable, including its
13963	     initializer.
13964
13965	     Having seen only the parenthesis, we cannot know which of
13966	     these two alternatives should be selected.  Even more
13967	     complex are examples like:
13968
13969	       int i (int (a));
13970	       int i (int (3));
13971
13972	     The former is a function-declaration; the latter is a
13973	     variable initialization.
13974
13975	     Thus again, we try a parameter-declaration-clause, and if
13976	     that fails, we back out and return.  */
13977
13978	  if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13979	    {
13980	      tree params;
13981	      unsigned saved_num_template_parameter_lists;
13982	      bool is_declarator = false;
13983	      tree t;
13984
13985	      /* In a member-declarator, the only valid interpretation
13986		 of a parenthesis is the start of a
13987		 parameter-declaration-clause.  (It is invalid to
13988		 initialize a static data member with a parenthesized
13989		 initializer; only the "=" form of initialization is
13990		 permitted.)  */
13991	      if (!member_p)
13992		cp_parser_parse_tentatively (parser);
13993
13994	      /* Consume the `('.  */
13995	      cp_lexer_consume_token (parser->lexer);
13996	      if (first)
13997		{
13998		  /* If this is going to be an abstract declarator, we're
13999		     in a declarator and we can't have default args.  */
14000		  parser->default_arg_ok_p = false;
14001		  parser->in_declarator_p = true;
14002		}
14003
14004	      /* Inside the function parameter list, surrounding
14005		 template-parameter-lists do not apply.  */
14006	      saved_num_template_parameter_lists
14007		= parser->num_template_parameter_lists;
14008	      parser->num_template_parameter_lists = 0;
14009
14010	      begin_scope (sk_function_parms, NULL_TREE);
14011
14012	      /* Parse the parameter-declaration-clause.  */
14013	      params = cp_parser_parameter_declaration_clause (parser);
14014
14015	      parser->num_template_parameter_lists
14016		= saved_num_template_parameter_lists;
14017
14018	      /* If all went well, parse the cv-qualifier-seq and the
14019		 exception-specification.  */
14020	      if (member_p || cp_parser_parse_definitely (parser))
14021		{
14022		  cp_cv_quals cv_quals;
14023		  tree exception_specification;
14024		  tree late_return;
14025
14026		  is_declarator = true;
14027
14028		  if (ctor_dtor_or_conv_p)
14029		    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14030		  first = false;
14031		  /* Consume the `)'.  */
14032		  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14033
14034		  /* Parse the cv-qualifier-seq.  */
14035		  cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14036		  /* And the exception-specification.  */
14037		  exception_specification
14038		    = cp_parser_exception_specification_opt (parser);
14039
14040		  late_return
14041		    = cp_parser_late_return_type_opt (parser);
14042
14043		  /* Create the function-declarator.  */
14044		  declarator = make_call_declarator (declarator,
14045						     params,
14046						     cv_quals,
14047						     exception_specification,
14048						     late_return);
14049		  /* Any subsequent parameter lists are to do with
14050		     return type, so are not those of the declared
14051		     function.  */
14052		  parser->default_arg_ok_p = false;
14053		}
14054
14055	      /* Remove the function parms from scope.  */
14056	      for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14057		pop_binding (DECL_NAME (t), t);
14058	      leave_scope();
14059
14060	      if (is_declarator)
14061		/* Repeat the main loop.  */
14062		continue;
14063	    }
14064
14065	  /* If this is the first, we can try a parenthesized
14066	     declarator.  */
14067	  if (first)
14068	    {
14069	      bool saved_in_type_id_in_expr_p;
14070
14071	      parser->default_arg_ok_p = saved_default_arg_ok_p;
14072	      parser->in_declarator_p = saved_in_declarator_p;
14073
14074	      /* Consume the `('.  */
14075	      cp_lexer_consume_token (parser->lexer);
14076	      /* Parse the nested declarator.  */
14077	      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14078	      parser->in_type_id_in_expr_p = true;
14079	      declarator
14080		= cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14081					/*parenthesized_p=*/NULL,
14082					member_p);
14083	      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14084	      first = false;
14085	      /* Expect a `)'.  */
14086	      if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14087		declarator = cp_error_declarator;
14088	      if (declarator == cp_error_declarator)
14089		break;
14090
14091	      goto handle_declarator;
14092	    }
14093	  /* Otherwise, we must be done.  */
14094	  else
14095	    break;
14096	}
14097      else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14098	       && token->type == CPP_OPEN_SQUARE)
14099	{
14100	  /* Parse an array-declarator.  */
14101	  tree bounds;
14102
14103	  if (ctor_dtor_or_conv_p)
14104	    *ctor_dtor_or_conv_p = 0;
14105
14106	  first = false;
14107	  parser->default_arg_ok_p = false;
14108	  parser->in_declarator_p = true;
14109	  /* Consume the `['.  */
14110	  cp_lexer_consume_token (parser->lexer);
14111	  /* Peek at the next token.  */
14112	  token = cp_lexer_peek_token (parser->lexer);
14113	  /* If the next token is `]', then there is no
14114	     constant-expression.  */
14115	  if (token->type != CPP_CLOSE_SQUARE)
14116	    {
14117	      bool non_constant_p;
14118
14119	      bounds
14120		= cp_parser_constant_expression (parser,
14121						 /*allow_non_constant=*/true,
14122						 &non_constant_p);
14123	      if (!non_constant_p)
14124		bounds = fold_non_dependent_expr (bounds);
14125	      /* Normally, the array bound must be an integral constant
14126		 expression.  However, as an extension, we allow VLAs
14127		 in function scopes as long as they aren't part of a
14128		 parameter declaration.  */
14129	      else if (!parser->in_function_body
14130		       || current_binding_level->kind == sk_function_parms)
14131		{
14132		  cp_parser_error (parser,
14133				   "array bound is not an integer constant");
14134		  bounds = error_mark_node;
14135		}
14136	      else if (processing_template_decl && !error_operand_p (bounds))
14137		{
14138		  /* Remember this wasn't a constant-expression.  */
14139		  bounds = build_nop (TREE_TYPE (bounds), bounds);
14140		  TREE_SIDE_EFFECTS (bounds) = 1;
14141		}
14142	    }
14143	  else
14144	    bounds = NULL_TREE;
14145	  /* Look for the closing `]'.  */
14146	  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14147	    {
14148	      declarator = cp_error_declarator;
14149	      break;
14150	    }
14151
14152	  declarator = make_array_declarator (declarator, bounds);
14153	}
14154      else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14155	{
14156	  {
14157	    tree qualifying_scope;
14158	    tree unqualified_name;
14159	    special_function_kind sfk;
14160	    bool abstract_ok;
14161	    bool pack_expansion_p = false;
14162	    cp_token *declarator_id_start_token;
14163
14164	    /* Parse a declarator-id */
14165	    abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14166	    if (abstract_ok)
14167	      {
14168		cp_parser_parse_tentatively (parser);
14169
14170		/* If we see an ellipsis, we should be looking at a
14171		   parameter pack. */
14172		if (token->type == CPP_ELLIPSIS)
14173		  {
14174		    /* Consume the `...' */
14175		    cp_lexer_consume_token (parser->lexer);
14176
14177		    pack_expansion_p = true;
14178		  }
14179	      }
14180
14181	    declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14182	    unqualified_name
14183	      = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14184	    qualifying_scope = parser->scope;
14185	    if (abstract_ok)
14186	      {
14187		bool okay = false;
14188
14189		if (!unqualified_name && pack_expansion_p)
14190		  {
14191		    /* Check whether an error occurred. */
14192		    okay = !cp_parser_error_occurred (parser);
14193
14194		    /* We already consumed the ellipsis to mark a
14195		       parameter pack, but we have no way to report it,
14196		       so abort the tentative parse. We will be exiting
14197		       immediately anyway. */
14198		    cp_parser_abort_tentative_parse (parser);
14199		  }
14200		else
14201		  okay = cp_parser_parse_definitely (parser);
14202
14203		if (!okay)
14204		  unqualified_name = error_mark_node;
14205		else if (unqualified_name
14206			 && (qualifying_scope
14207			     || (TREE_CODE (unqualified_name)
14208				 != IDENTIFIER_NODE)))
14209		  {
14210		    cp_parser_error (parser, "expected unqualified-id");
14211		    unqualified_name = error_mark_node;
14212		  }
14213	      }
14214
14215	    if (!unqualified_name)
14216	      return NULL;
14217	    if (unqualified_name == error_mark_node)
14218	      {
14219		declarator = cp_error_declarator;
14220		pack_expansion_p = false;
14221		declarator->parameter_pack_p = false;
14222		break;
14223	      }
14224
14225	    if (qualifying_scope && at_namespace_scope_p ()
14226		&& TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14227	      {
14228		/* In the declaration of a member of a template class
14229		   outside of the class itself, the SCOPE will sometimes
14230		   be a TYPENAME_TYPE.  For example, given:
14231
14232		   template <typename T>
14233		   int S<T>::R::i = 3;
14234
14235		   the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14236		   this context, we must resolve S<T>::R to an ordinary
14237		   type, rather than a typename type.
14238
14239		   The reason we normally avoid resolving TYPENAME_TYPEs
14240		   is that a specialization of `S' might render
14241		   `S<T>::R' not a type.  However, if `S' is
14242		   specialized, then this `i' will not be used, so there
14243		   is no harm in resolving the types here.  */
14244		tree type;
14245
14246		/* Resolve the TYPENAME_TYPE.  */
14247		type = resolve_typename_type (qualifying_scope,
14248					      /*only_current_p=*/false);
14249		/* If that failed, the declarator is invalid.  */
14250		if (TREE_CODE (type) == TYPENAME_TYPE)
14251		  {
14252		    if (typedef_variant_p (type))
14253		      error_at (declarator_id_start_token->location,
14254				"cannot define member of dependent typedef "
14255				"%qT", type);
14256		    else
14257		      error_at (declarator_id_start_token->location,
14258				"%<%T::%E%> is not a type",
14259				TYPE_CONTEXT (qualifying_scope),
14260				TYPE_IDENTIFIER (qualifying_scope));
14261		  }
14262		qualifying_scope = type;
14263	      }
14264
14265	    sfk = sfk_none;
14266
14267	    if (unqualified_name)
14268	      {
14269		tree class_type;
14270
14271		if (qualifying_scope
14272		    && CLASS_TYPE_P (qualifying_scope))
14273		  class_type = qualifying_scope;
14274		else
14275		  class_type = current_class_type;
14276
14277		if (TREE_CODE (unqualified_name) == TYPE_DECL)
14278		  {
14279		    tree name_type = TREE_TYPE (unqualified_name);
14280		    if (class_type && same_type_p (name_type, class_type))
14281		      {
14282			if (qualifying_scope
14283			    && CLASSTYPE_USE_TEMPLATE (name_type))
14284			  {
14285			    error_at (declarator_id_start_token->location,
14286				      "invalid use of constructor as a template");
14287			    inform (declarator_id_start_token->location,
14288				    "use %<%T::%D%> instead of %<%T::%D%> to "
14289				    "name the constructor in a qualified name",
14290				    class_type,
14291				    DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14292				    class_type, name_type);
14293			    declarator = cp_error_declarator;
14294			    break;
14295			  }
14296			else
14297			  unqualified_name = constructor_name (class_type);
14298		      }
14299		    else
14300		      {
14301			/* We do not attempt to print the declarator
14302			   here because we do not have enough
14303			   information about its original syntactic
14304			   form.  */
14305			cp_parser_error (parser, "invalid declarator");
14306			declarator = cp_error_declarator;
14307			break;
14308		      }
14309		  }
14310
14311		if (class_type)
14312		  {
14313		    if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14314		      sfk = sfk_destructor;
14315		    else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14316		      sfk = sfk_conversion;
14317		    else if (/* There's no way to declare a constructor
14318				for an anonymous type, even if the type
14319				got a name for linkage purposes.  */
14320			     !TYPE_WAS_ANONYMOUS (class_type)
14321			     && constructor_name_p (unqualified_name,
14322						    class_type))
14323		      {
14324			unqualified_name = constructor_name (class_type);
14325			sfk = sfk_constructor;
14326		      }
14327		    else if (is_overloaded_fn (unqualified_name)
14328			     && DECL_CONSTRUCTOR_P (get_first_fn
14329						    (unqualified_name)))
14330		      sfk = sfk_constructor;
14331
14332		    if (ctor_dtor_or_conv_p && sfk != sfk_none)
14333		      *ctor_dtor_or_conv_p = -1;
14334		  }
14335	      }
14336	    declarator = make_id_declarator (qualifying_scope,
14337					     unqualified_name,
14338					     sfk);
14339	    declarator->id_loc = token->location;
14340	    declarator->parameter_pack_p = pack_expansion_p;
14341
14342	    if (pack_expansion_p)
14343	      maybe_warn_variadic_templates ();
14344	  }
14345
14346	handle_declarator:;
14347	  scope = get_scope_of_declarator (declarator);
14348	  if (scope)
14349	    /* Any names that appear after the declarator-id for a
14350	       member are looked up in the containing scope.  */
14351	    pushed_scope = push_scope (scope);
14352	  parser->in_declarator_p = true;
14353	  if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14354	      || (declarator && declarator->kind == cdk_id))
14355	    /* Default args are only allowed on function
14356	       declarations.  */
14357	    parser->default_arg_ok_p = saved_default_arg_ok_p;
14358	  else
14359	    parser->default_arg_ok_p = false;
14360
14361	  first = false;
14362	}
14363      /* We're done.  */
14364      else
14365	break;
14366    }
14367
14368  /* For an abstract declarator, we might wind up with nothing at this
14369     point.  That's an error; the declarator is not optional.  */
14370  if (!declarator)
14371    cp_parser_error (parser, "expected declarator");
14372
14373  /* If we entered a scope, we must exit it now.  */
14374  if (pushed_scope)
14375    pop_scope (pushed_scope);
14376
14377  parser->default_arg_ok_p = saved_default_arg_ok_p;
14378  parser->in_declarator_p = saved_in_declarator_p;
14379
14380  return declarator;
14381}
14382
14383/* Parse a ptr-operator.
14384
14385   ptr-operator:
14386     * cv-qualifier-seq [opt]
14387     &
14388     :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14389
14390   GNU Extension:
14391
14392   ptr-operator:
14393     & cv-qualifier-seq [opt]
14394
14395   Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14396   Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14397   an rvalue reference. In the case of a pointer-to-member, *TYPE is
14398   filled in with the TYPE containing the member.  *CV_QUALS is
14399   filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14400   are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14401   Note that the tree codes returned by this function have nothing
14402   to do with the types of trees that will be eventually be created
14403   to represent the pointer or reference type being parsed. They are
14404   just constants with suggestive names. */
14405static enum tree_code
14406cp_parser_ptr_operator (cp_parser* parser,
14407			tree* type,
14408			cp_cv_quals *cv_quals)
14409{
14410  enum tree_code code = ERROR_MARK;
14411  cp_token *token;
14412
14413  /* Assume that it's not a pointer-to-member.  */
14414  *type = NULL_TREE;
14415  /* And that there are no cv-qualifiers.  */
14416  *cv_quals = TYPE_UNQUALIFIED;
14417
14418  /* Peek at the next token.  */
14419  token = cp_lexer_peek_token (parser->lexer);
14420
14421  /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14422  if (token->type == CPP_MULT)
14423    code = INDIRECT_REF;
14424  else if (token->type == CPP_AND)
14425    code = ADDR_EXPR;
14426  else if ((cxx_dialect != cxx98) &&
14427	   token->type == CPP_AND_AND) /* C++0x only */
14428    code = NON_LVALUE_EXPR;
14429
14430  if (code != ERROR_MARK)
14431    {
14432      /* Consume the `*', `&' or `&&'.  */
14433      cp_lexer_consume_token (parser->lexer);
14434
14435      /* A `*' can be followed by a cv-qualifier-seq, and so can a
14436	 `&', if we are allowing GNU extensions.  (The only qualifier
14437	 that can legally appear after `&' is `restrict', but that is
14438	 enforced during semantic analysis.  */
14439      if (code == INDIRECT_REF
14440	  || cp_parser_allow_gnu_extensions_p (parser))
14441	*cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14442    }
14443  else
14444    {
14445      /* Try the pointer-to-member case.  */
14446      cp_parser_parse_tentatively (parser);
14447      /* Look for the optional `::' operator.  */
14448      cp_parser_global_scope_opt (parser,
14449				  /*current_scope_valid_p=*/false);
14450      /* Look for the nested-name specifier.  */
14451      token = cp_lexer_peek_token (parser->lexer);
14452      cp_parser_nested_name_specifier (parser,
14453				       /*typename_keyword_p=*/false,
14454				       /*check_dependency_p=*/true,
14455				       /*type_p=*/false,
14456				       /*is_declaration=*/false);
14457      /* If we found it, and the next token is a `*', then we are
14458	 indeed looking at a pointer-to-member operator.  */
14459      if (!cp_parser_error_occurred (parser)
14460	  && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14461	{
14462	  /* Indicate that the `*' operator was used.  */
14463	  code = INDIRECT_REF;
14464
14465	  if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14466	    error_at (token->location, "%qD is a namespace", parser->scope);
14467	  else
14468	    {
14469	      /* The type of which the member is a member is given by the
14470		 current SCOPE.  */
14471	      *type = parser->scope;
14472	      /* The next name will not be qualified.  */
14473	      parser->scope = NULL_TREE;
14474	      parser->qualifying_scope = NULL_TREE;
14475	      parser->object_scope = NULL_TREE;
14476	      /* Look for the optional cv-qualifier-seq.  */
14477	      *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14478	    }
14479	}
14480      /* If that didn't work we don't have a ptr-operator.  */
14481      if (!cp_parser_parse_definitely (parser))
14482	cp_parser_error (parser, "expected ptr-operator");
14483    }
14484
14485  return code;
14486}
14487
14488/* Parse an (optional) cv-qualifier-seq.
14489
14490   cv-qualifier-seq:
14491     cv-qualifier cv-qualifier-seq [opt]
14492
14493   cv-qualifier:
14494     const
14495     volatile
14496
14497   GNU Extension:
14498
14499   cv-qualifier:
14500     __restrict__
14501
14502   Returns a bitmask representing the cv-qualifiers.  */
14503
14504static cp_cv_quals
14505cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14506{
14507  cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14508
14509  while (true)
14510    {
14511      cp_token *token;
14512      cp_cv_quals cv_qualifier;
14513
14514      /* Peek at the next token.  */
14515      token = cp_lexer_peek_token (parser->lexer);
14516      /* See if it's a cv-qualifier.  */
14517      switch (token->keyword)
14518	{
14519	case RID_CONST:
14520	  cv_qualifier = TYPE_QUAL_CONST;
14521	  break;
14522
14523	case RID_VOLATILE:
14524	  cv_qualifier = TYPE_QUAL_VOLATILE;
14525	  break;
14526
14527	case RID_RESTRICT:
14528	  cv_qualifier = TYPE_QUAL_RESTRICT;
14529	  break;
14530
14531	default:
14532	  cv_qualifier = TYPE_UNQUALIFIED;
14533	  break;
14534	}
14535
14536      if (!cv_qualifier)
14537	break;
14538
14539      if (cv_quals & cv_qualifier)
14540	{
14541	  error_at (token->location, "duplicate cv-qualifier");
14542	  cp_lexer_purge_token (parser->lexer);
14543	}
14544      else
14545	{
14546	  cp_lexer_consume_token (parser->lexer);
14547	  cv_quals |= cv_qualifier;
14548	}
14549    }
14550
14551  return cv_quals;
14552}
14553
14554/* Parse a late-specified return type, if any.  This is not a separate
14555   non-terminal, but part of a function declarator, which looks like
14556
14557   -> trailing-type-specifier-seq abstract-declarator(opt)
14558
14559   Returns the type indicated by the type-id.  */
14560
14561static tree
14562cp_parser_late_return_type_opt (cp_parser* parser)
14563{
14564  cp_token *token;
14565
14566  /* Peek at the next token.  */
14567  token = cp_lexer_peek_token (parser->lexer);
14568  /* A late-specified return type is indicated by an initial '->'. */
14569  if (token->type != CPP_DEREF)
14570    return NULL_TREE;
14571
14572  /* Consume the ->.  */
14573  cp_lexer_consume_token (parser->lexer);
14574
14575  return cp_parser_trailing_type_id (parser);
14576}
14577
14578/* Parse a declarator-id.
14579
14580   declarator-id:
14581     id-expression
14582     :: [opt] nested-name-specifier [opt] type-name
14583
14584   In the `id-expression' case, the value returned is as for
14585   cp_parser_id_expression if the id-expression was an unqualified-id.
14586   If the id-expression was a qualified-id, then a SCOPE_REF is
14587   returned.  The first operand is the scope (either a NAMESPACE_DECL
14588   or TREE_TYPE), but the second is still just a representation of an
14589   unqualified-id.  */
14590
14591static tree
14592cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14593{
14594  tree id;
14595  /* The expression must be an id-expression.  Assume that qualified
14596     names are the names of types so that:
14597
14598       template <class T>
14599       int S<T>::R::i = 3;
14600
14601     will work; we must treat `S<T>::R' as the name of a type.
14602     Similarly, assume that qualified names are templates, where
14603     required, so that:
14604
14605       template <class T>
14606       int S<T>::R<T>::i = 3;
14607
14608     will work, too.  */
14609  id = cp_parser_id_expression (parser,
14610				/*template_keyword_p=*/false,
14611				/*check_dependency_p=*/false,
14612				/*template_p=*/NULL,
14613				/*declarator_p=*/true,
14614				optional_p);
14615  if (id && BASELINK_P (id))
14616    id = BASELINK_FUNCTIONS (id);
14617  return id;
14618}
14619
14620/* Parse a type-id.
14621
14622   type-id:
14623     type-specifier-seq abstract-declarator [opt]
14624
14625   Returns the TYPE specified.  */
14626
14627static tree
14628cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14629		     bool is_trailing_return)
14630{
14631  cp_decl_specifier_seq type_specifier_seq;
14632  cp_declarator *abstract_declarator;
14633
14634  /* Parse the type-specifier-seq.  */
14635  cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14636				is_trailing_return,
14637				&type_specifier_seq);
14638  if (type_specifier_seq.type == error_mark_node)
14639    return error_mark_node;
14640
14641  /* There might or might not be an abstract declarator.  */
14642  cp_parser_parse_tentatively (parser);
14643  /* Look for the declarator.  */
14644  abstract_declarator
14645    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14646			    /*parenthesized_p=*/NULL,
14647			    /*member_p=*/false);
14648  /* Check to see if there really was a declarator.  */
14649  if (!cp_parser_parse_definitely (parser))
14650    abstract_declarator = NULL;
14651
14652  if (type_specifier_seq.type
14653      && type_uses_auto (type_specifier_seq.type))
14654    {
14655      /* A type-id with type 'auto' is only ok if the abstract declarator
14656	 is a function declarator with a late-specified return type.  */
14657      if (abstract_declarator
14658	  && abstract_declarator->kind == cdk_function
14659	  && abstract_declarator->u.function.late_return_type)
14660	/* OK */;
14661      else
14662	{
14663	  error ("invalid use of %<auto%>");
14664	  return error_mark_node;
14665	}
14666    }
14667
14668  return groktypename (&type_specifier_seq, abstract_declarator,
14669		       is_template_arg);
14670}
14671
14672static tree cp_parser_type_id (cp_parser *parser)
14673{
14674  return cp_parser_type_id_1 (parser, false, false);
14675}
14676
14677static tree cp_parser_template_type_arg (cp_parser *parser)
14678{
14679  return cp_parser_type_id_1 (parser, true, false);
14680}
14681
14682static tree cp_parser_trailing_type_id (cp_parser *parser)
14683{
14684  return cp_parser_type_id_1 (parser, false, true);
14685}
14686
14687/* Parse a type-specifier-seq.
14688
14689   type-specifier-seq:
14690     type-specifier type-specifier-seq [opt]
14691
14692   GNU extension:
14693
14694   type-specifier-seq:
14695     attributes type-specifier-seq [opt]
14696
14697   If IS_DECLARATION is true, we are at the start of a "condition" or
14698   exception-declaration, so we might be followed by a declarator-id.
14699
14700   If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14701   i.e. we've just seen "->".
14702
14703   Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14704
14705static void
14706cp_parser_type_specifier_seq (cp_parser* parser,
14707			      bool is_declaration,
14708			      bool is_trailing_return,
14709			      cp_decl_specifier_seq *type_specifier_seq)
14710{
14711  bool seen_type_specifier = false;
14712  cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14713  cp_token *start_token = NULL;
14714
14715  /* Clear the TYPE_SPECIFIER_SEQ.  */
14716  clear_decl_specs (type_specifier_seq);
14717
14718  /* In the context of a trailing return type, enum E { } is an
14719     elaborated-type-specifier followed by a function-body, not an
14720     enum-specifier.  */
14721  if (is_trailing_return)
14722    flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14723
14724  /* Parse the type-specifiers and attributes.  */
14725  while (true)
14726    {
14727      tree type_specifier;
14728      bool is_cv_qualifier;
14729
14730      /* Check for attributes first.  */
14731      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14732	{
14733	  type_specifier_seq->attributes =
14734	    chainon (type_specifier_seq->attributes,
14735		     cp_parser_attributes_opt (parser));
14736	  continue;
14737	}
14738
14739      /* record the token of the beginning of the type specifier seq,
14740         for error reporting purposes*/
14741     if (!start_token)
14742       start_token = cp_lexer_peek_token (parser->lexer);
14743
14744      /* Look for the type-specifier.  */
14745      type_specifier = cp_parser_type_specifier (parser,
14746						 flags,
14747						 type_specifier_seq,
14748						 /*is_declaration=*/false,
14749						 NULL,
14750						 &is_cv_qualifier);
14751      if (!type_specifier)
14752	{
14753	  /* If the first type-specifier could not be found, this is not a
14754	     type-specifier-seq at all.  */
14755	  if (!seen_type_specifier)
14756	    {
14757	      cp_parser_error (parser, "expected type-specifier");
14758	      type_specifier_seq->type = error_mark_node;
14759	      return;
14760	    }
14761	  /* If subsequent type-specifiers could not be found, the
14762	     type-specifier-seq is complete.  */
14763	  break;
14764	}
14765
14766      seen_type_specifier = true;
14767      /* The standard says that a condition can be:
14768
14769	    type-specifier-seq declarator = assignment-expression
14770
14771	 However, given:
14772
14773	   struct S {};
14774	   if (int S = ...)
14775
14776	 we should treat the "S" as a declarator, not as a
14777	 type-specifier.  The standard doesn't say that explicitly for
14778	 type-specifier-seq, but it does say that for
14779	 decl-specifier-seq in an ordinary declaration.  Perhaps it
14780	 would be clearer just to allow a decl-specifier-seq here, and
14781	 then add a semantic restriction that if any decl-specifiers
14782	 that are not type-specifiers appear, the program is invalid.  */
14783      if (is_declaration && !is_cv_qualifier)
14784	flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14785    }
14786
14787  cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14788}
14789
14790/* Parse a parameter-declaration-clause.
14791
14792   parameter-declaration-clause:
14793     parameter-declaration-list [opt] ... [opt]
14794     parameter-declaration-list , ...
14795
14796   Returns a representation for the parameter declarations.  A return
14797   value of NULL indicates a parameter-declaration-clause consisting
14798   only of an ellipsis.  */
14799
14800static tree
14801cp_parser_parameter_declaration_clause (cp_parser* parser)
14802{
14803  tree parameters;
14804  cp_token *token;
14805  bool ellipsis_p;
14806  bool is_error;
14807
14808  /* Peek at the next token.  */
14809  token = cp_lexer_peek_token (parser->lexer);
14810  /* Check for trivial parameter-declaration-clauses.  */
14811  if (token->type == CPP_ELLIPSIS)
14812    {
14813      /* Consume the `...' token.  */
14814      cp_lexer_consume_token (parser->lexer);
14815      return NULL_TREE;
14816    }
14817  else if (token->type == CPP_CLOSE_PAREN)
14818    /* There are no parameters.  */
14819    {
14820#ifndef NO_IMPLICIT_EXTERN_C
14821      if (in_system_header && current_class_type == NULL
14822	  && current_lang_name == lang_name_c)
14823	return NULL_TREE;
14824      else
14825#endif
14826	return void_list_node;
14827    }
14828  /* Check for `(void)', too, which is a special case.  */
14829  else if (token->keyword == RID_VOID
14830	   && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14831	       == CPP_CLOSE_PAREN))
14832    {
14833      /* Consume the `void' token.  */
14834      cp_lexer_consume_token (parser->lexer);
14835      /* There are no parameters.  */
14836      return void_list_node;
14837    }
14838
14839  /* Parse the parameter-declaration-list.  */
14840  parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14841  /* If a parse error occurred while parsing the
14842     parameter-declaration-list, then the entire
14843     parameter-declaration-clause is erroneous.  */
14844  if (is_error)
14845    return NULL;
14846
14847  /* Peek at the next token.  */
14848  token = cp_lexer_peek_token (parser->lexer);
14849  /* If it's a `,', the clause should terminate with an ellipsis.  */
14850  if (token->type == CPP_COMMA)
14851    {
14852      /* Consume the `,'.  */
14853      cp_lexer_consume_token (parser->lexer);
14854      /* Expect an ellipsis.  */
14855      ellipsis_p
14856	= (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14857    }
14858  /* It might also be `...' if the optional trailing `,' was
14859     omitted.  */
14860  else if (token->type == CPP_ELLIPSIS)
14861    {
14862      /* Consume the `...' token.  */
14863      cp_lexer_consume_token (parser->lexer);
14864      /* And remember that we saw it.  */
14865      ellipsis_p = true;
14866    }
14867  else
14868    ellipsis_p = false;
14869
14870  /* Finish the parameter list.  */
14871  if (!ellipsis_p)
14872    parameters = chainon (parameters, void_list_node);
14873
14874  return parameters;
14875}
14876
14877/* Parse a parameter-declaration-list.
14878
14879   parameter-declaration-list:
14880     parameter-declaration
14881     parameter-declaration-list , parameter-declaration
14882
14883   Returns a representation of the parameter-declaration-list, as for
14884   cp_parser_parameter_declaration_clause.  However, the
14885   `void_list_node' is never appended to the list.  Upon return,
14886   *IS_ERROR will be true iff an error occurred.  */
14887
14888static tree
14889cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14890{
14891  tree parameters = NULL_TREE;
14892  tree *tail = &parameters;
14893  bool saved_in_unbraced_linkage_specification_p;
14894  int index = 0;
14895
14896  /* Assume all will go well.  */
14897  *is_error = false;
14898  /* The special considerations that apply to a function within an
14899     unbraced linkage specifications do not apply to the parameters
14900     to the function.  */
14901  saved_in_unbraced_linkage_specification_p
14902    = parser->in_unbraced_linkage_specification_p;
14903  parser->in_unbraced_linkage_specification_p = false;
14904
14905  /* Look for more parameters.  */
14906  while (true)
14907    {
14908      cp_parameter_declarator *parameter;
14909      tree decl = error_mark_node;
14910      bool parenthesized_p;
14911      /* Parse the parameter.  */
14912      parameter
14913	= cp_parser_parameter_declaration (parser,
14914					   /*template_parm_p=*/false,
14915					   &parenthesized_p);
14916
14917      /* We don't know yet if the enclosing context is deprecated, so wait
14918	 and warn in grokparms if appropriate.  */
14919      deprecated_state = DEPRECATED_SUPPRESS;
14920
14921      if (parameter)
14922	decl = grokdeclarator (parameter->declarator,
14923			       &parameter->decl_specifiers,
14924			       PARM,
14925			       parameter->default_argument != NULL_TREE,
14926			       &parameter->decl_specifiers.attributes);
14927
14928      deprecated_state = DEPRECATED_NORMAL;
14929
14930      /* If a parse error occurred parsing the parameter declaration,
14931	 then the entire parameter-declaration-list is erroneous.  */
14932      if (decl == error_mark_node)
14933	{
14934	  *is_error = true;
14935	  parameters = error_mark_node;
14936	  break;
14937	}
14938
14939      if (parameter->decl_specifiers.attributes)
14940	cplus_decl_attributes (&decl,
14941			       parameter->decl_specifiers.attributes,
14942			       0);
14943      if (DECL_NAME (decl))
14944	decl = pushdecl (decl);
14945
14946      if (decl != error_mark_node)
14947	{
14948	  retrofit_lang_decl (decl);
14949	  DECL_PARM_INDEX (decl) = ++index;
14950	}
14951
14952      /* Add the new parameter to the list.  */
14953      *tail = build_tree_list (parameter->default_argument, decl);
14954      tail = &TREE_CHAIN (*tail);
14955
14956      /* Peek at the next token.  */
14957      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14958	  || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14959	  /* These are for Objective-C++ */
14960	  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14961	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14962	/* The parameter-declaration-list is complete.  */
14963	break;
14964      else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14965	{
14966	  cp_token *token;
14967
14968	  /* Peek at the next token.  */
14969	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
14970	  /* If it's an ellipsis, then the list is complete.  */
14971	  if (token->type == CPP_ELLIPSIS)
14972	    break;
14973	  /* Otherwise, there must be more parameters.  Consume the
14974	     `,'.  */
14975	  cp_lexer_consume_token (parser->lexer);
14976	  /* When parsing something like:
14977
14978		int i(float f, double d)
14979
14980	     we can tell after seeing the declaration for "f" that we
14981	     are not looking at an initialization of a variable "i",
14982	     but rather at the declaration of a function "i".
14983
14984	     Due to the fact that the parsing of template arguments
14985	     (as specified to a template-id) requires backtracking we
14986	     cannot use this technique when inside a template argument
14987	     list.  */
14988	  if (!parser->in_template_argument_list_p
14989	      && !parser->in_type_id_in_expr_p
14990	      && cp_parser_uncommitted_to_tentative_parse_p (parser)
14991	      /* However, a parameter-declaration of the form
14992		 "foat(f)" (which is a valid declaration of a
14993		 parameter "f") can also be interpreted as an
14994		 expression (the conversion of "f" to "float").  */
14995	      && !parenthesized_p)
14996	    cp_parser_commit_to_tentative_parse (parser);
14997	}
14998      else
14999	{
15000	  cp_parser_error (parser, "expected %<,%> or %<...%>");
15001	  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15002	    cp_parser_skip_to_closing_parenthesis (parser,
15003						   /*recovering=*/true,
15004						   /*or_comma=*/false,
15005						   /*consume_paren=*/false);
15006	  break;
15007	}
15008    }
15009
15010  parser->in_unbraced_linkage_specification_p
15011    = saved_in_unbraced_linkage_specification_p;
15012
15013  return parameters;
15014}
15015
15016/* Parse a parameter declaration.
15017
15018   parameter-declaration:
15019     decl-specifier-seq ... [opt] declarator
15020     decl-specifier-seq declarator = assignment-expression
15021     decl-specifier-seq ... [opt] abstract-declarator [opt]
15022     decl-specifier-seq abstract-declarator [opt] = assignment-expression
15023
15024   If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15025   declares a template parameter.  (In that case, a non-nested `>'
15026   token encountered during the parsing of the assignment-expression
15027   is not interpreted as a greater-than operator.)
15028
15029   Returns a representation of the parameter, or NULL if an error
15030   occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15031   true iff the declarator is of the form "(p)".  */
15032
15033static cp_parameter_declarator *
15034cp_parser_parameter_declaration (cp_parser *parser,
15035				 bool template_parm_p,
15036				 bool *parenthesized_p)
15037{
15038  int declares_class_or_enum;
15039  cp_decl_specifier_seq decl_specifiers;
15040  cp_declarator *declarator;
15041  tree default_argument;
15042  cp_token *token = NULL, *declarator_token_start = NULL;
15043  const char *saved_message;
15044
15045  /* In a template parameter, `>' is not an operator.
15046
15047     [temp.param]
15048
15049     When parsing a default template-argument for a non-type
15050     template-parameter, the first non-nested `>' is taken as the end
15051     of the template parameter-list rather than a greater-than
15052     operator.  */
15053
15054  /* Type definitions may not appear in parameter types.  */
15055  saved_message = parser->type_definition_forbidden_message;
15056  parser->type_definition_forbidden_message
15057    = G_("types may not be defined in parameter types");
15058
15059  /* Parse the declaration-specifiers.  */
15060  cp_parser_decl_specifier_seq (parser,
15061				CP_PARSER_FLAGS_NONE,
15062				&decl_specifiers,
15063				&declares_class_or_enum);
15064
15065  /* Complain about missing 'typename' or other invalid type names.  */
15066  if (!decl_specifiers.any_type_specifiers_p)
15067    cp_parser_parse_and_diagnose_invalid_type_name (parser);
15068
15069  /* If an error occurred, there's no reason to attempt to parse the
15070     rest of the declaration.  */
15071  if (cp_parser_error_occurred (parser))
15072    {
15073      parser->type_definition_forbidden_message = saved_message;
15074      return NULL;
15075    }
15076
15077  /* Peek at the next token.  */
15078  token = cp_lexer_peek_token (parser->lexer);
15079
15080  /* If the next token is a `)', `,', `=', `>', or `...', then there
15081     is no declarator. However, when variadic templates are enabled,
15082     there may be a declarator following `...'.  */
15083  if (token->type == CPP_CLOSE_PAREN
15084      || token->type == CPP_COMMA
15085      || token->type == CPP_EQ
15086      || token->type == CPP_GREATER)
15087    {
15088      declarator = NULL;
15089      if (parenthesized_p)
15090	*parenthesized_p = false;
15091    }
15092  /* Otherwise, there should be a declarator.  */
15093  else
15094    {
15095      bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15096      parser->default_arg_ok_p = false;
15097
15098      /* After seeing a decl-specifier-seq, if the next token is not a
15099	 "(", there is no possibility that the code is a valid
15100	 expression.  Therefore, if parsing tentatively, we commit at
15101	 this point.  */
15102      if (!parser->in_template_argument_list_p
15103	  /* In an expression context, having seen:
15104
15105	       (int((char ...
15106
15107	     we cannot be sure whether we are looking at a
15108	     function-type (taking a "char" as a parameter) or a cast
15109	     of some object of type "char" to "int".  */
15110	  && !parser->in_type_id_in_expr_p
15111	  && cp_parser_uncommitted_to_tentative_parse_p (parser)
15112	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15113	cp_parser_commit_to_tentative_parse (parser);
15114      /* Parse the declarator.  */
15115      declarator_token_start = token;
15116      declarator = cp_parser_declarator (parser,
15117					 CP_PARSER_DECLARATOR_EITHER,
15118					 /*ctor_dtor_or_conv_p=*/NULL,
15119					 parenthesized_p,
15120					 /*member_p=*/false);
15121      parser->default_arg_ok_p = saved_default_arg_ok_p;
15122      /* After the declarator, allow more attributes.  */
15123      decl_specifiers.attributes
15124	= chainon (decl_specifiers.attributes,
15125		   cp_parser_attributes_opt (parser));
15126    }
15127
15128  /* If the next token is an ellipsis, and we have not seen a
15129     declarator name, and the type of the declarator contains parameter
15130     packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15131     a parameter pack expansion expression. Otherwise, leave the
15132     ellipsis for a C-style variadic function. */
15133  token = cp_lexer_peek_token (parser->lexer);
15134  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15135    {
15136      tree type = decl_specifiers.type;
15137
15138      if (type && DECL_P (type))
15139        type = TREE_TYPE (type);
15140
15141      if (type
15142	  && TREE_CODE (type) != TYPE_PACK_EXPANSION
15143	  && declarator_can_be_parameter_pack (declarator)
15144          && (!declarator || !declarator->parameter_pack_p)
15145          && uses_parameter_packs (type))
15146        {
15147	  /* Consume the `...'. */
15148	  cp_lexer_consume_token (parser->lexer);
15149	  maybe_warn_variadic_templates ();
15150
15151	  /* Build a pack expansion type */
15152	  if (declarator)
15153	    declarator->parameter_pack_p = true;
15154	  else
15155	    decl_specifiers.type = make_pack_expansion (type);
15156	}
15157    }
15158
15159  /* The restriction on defining new types applies only to the type
15160     of the parameter, not to the default argument.  */
15161  parser->type_definition_forbidden_message = saved_message;
15162
15163  /* If the next token is `=', then process a default argument.  */
15164  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15165    {
15166      /* Consume the `='.  */
15167      cp_lexer_consume_token (parser->lexer);
15168
15169      /* If we are defining a class, then the tokens that make up the
15170	 default argument must be saved and processed later.  */
15171      if (!template_parm_p && at_class_scope_p ()
15172	  && TYPE_BEING_DEFINED (current_class_type)
15173	  && !LAMBDA_TYPE_P (current_class_type))
15174	{
15175	  unsigned depth = 0;
15176	  int maybe_template_id = 0;
15177	  cp_token *first_token;
15178	  cp_token *token;
15179
15180	  /* Add tokens until we have processed the entire default
15181	     argument.  We add the range [first_token, token).  */
15182	  first_token = cp_lexer_peek_token (parser->lexer);
15183	  while (true)
15184	    {
15185	      bool done = false;
15186
15187	      /* Peek at the next token.  */
15188	      token = cp_lexer_peek_token (parser->lexer);
15189	      /* What we do depends on what token we have.  */
15190	      switch (token->type)
15191		{
15192		  /* In valid code, a default argument must be
15193		     immediately followed by a `,' `)', or `...'.  */
15194		case CPP_COMMA:
15195		  if (depth == 0 && maybe_template_id)
15196		    {
15197		      /* If we've seen a '<', we might be in a
15198			 template-argument-list.  Until Core issue 325 is
15199			 resolved, we don't know how this situation ought
15200			 to be handled, so try to DTRT.  We check whether
15201			 what comes after the comma is a valid parameter
15202			 declaration list.  If it is, then the comma ends
15203			 the default argument; otherwise the default
15204			 argument continues.  */
15205		      bool error = false;
15206		      tree t;
15207
15208		      /* Set ITALP so cp_parser_parameter_declaration_list
15209			 doesn't decide to commit to this parse.  */
15210		      bool saved_italp = parser->in_template_argument_list_p;
15211		      parser->in_template_argument_list_p = true;
15212
15213		      cp_parser_parse_tentatively (parser);
15214		      cp_lexer_consume_token (parser->lexer);
15215		      begin_scope (sk_function_parms, NULL_TREE);
15216		      cp_parser_parameter_declaration_list (parser, &error);
15217		      for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
15218			pop_binding (DECL_NAME (t), t);
15219		      leave_scope ();
15220		      if (!cp_parser_error_occurred (parser) && !error)
15221			done = true;
15222		      cp_parser_abort_tentative_parse (parser);
15223
15224		      parser->in_template_argument_list_p = saved_italp;
15225		      break;
15226		    }
15227		case CPP_CLOSE_PAREN:
15228		case CPP_ELLIPSIS:
15229		  /* If we run into a non-nested `;', `}', or `]',
15230		     then the code is invalid -- but the default
15231		     argument is certainly over.  */
15232		case CPP_SEMICOLON:
15233		case CPP_CLOSE_BRACE:
15234		case CPP_CLOSE_SQUARE:
15235		  if (depth == 0)
15236		    done = true;
15237		  /* Update DEPTH, if necessary.  */
15238		  else if (token->type == CPP_CLOSE_PAREN
15239			   || token->type == CPP_CLOSE_BRACE
15240			   || token->type == CPP_CLOSE_SQUARE)
15241		    --depth;
15242		  break;
15243
15244		case CPP_OPEN_PAREN:
15245		case CPP_OPEN_SQUARE:
15246		case CPP_OPEN_BRACE:
15247		  ++depth;
15248		  break;
15249
15250		case CPP_LESS:
15251		  if (depth == 0)
15252		    /* This might be the comparison operator, or it might
15253		       start a template argument list.  */
15254		    ++maybe_template_id;
15255		  break;
15256
15257                case CPP_RSHIFT:
15258                  if (cxx_dialect == cxx98)
15259                    break;
15260                  /* Fall through for C++0x, which treats the `>>'
15261                     operator like two `>' tokens in certain
15262                     cases.  */
15263
15264		case CPP_GREATER:
15265		  if (depth == 0)
15266		    {
15267		      /* This might be an operator, or it might close a
15268			 template argument list.  But if a previous '<'
15269			 started a template argument list, this will have
15270			 closed it, so we can't be in one anymore.  */
15271		      maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15272		      if (maybe_template_id < 0)
15273			maybe_template_id = 0;
15274		    }
15275		  break;
15276
15277		  /* If we run out of tokens, issue an error message.  */
15278		case CPP_EOF:
15279		case CPP_PRAGMA_EOL:
15280		  error_at (token->location, "file ends in default argument");
15281		  done = true;
15282		  break;
15283
15284		case CPP_NAME:
15285		case CPP_SCOPE:
15286		  /* In these cases, we should look for template-ids.
15287		     For example, if the default argument is
15288		     `X<int, double>()', we need to do name lookup to
15289		     figure out whether or not `X' is a template; if
15290		     so, the `,' does not end the default argument.
15291
15292		     That is not yet done.  */
15293		  break;
15294
15295		default:
15296		  break;
15297		}
15298
15299	      /* If we've reached the end, stop.  */
15300	      if (done)
15301		break;
15302
15303	      /* Add the token to the token block.  */
15304	      token = cp_lexer_consume_token (parser->lexer);
15305	    }
15306
15307	  /* Create a DEFAULT_ARG to represent the unparsed default
15308	     argument.  */
15309	  default_argument = make_node (DEFAULT_ARG);
15310	  DEFARG_TOKENS (default_argument)
15311	    = cp_token_cache_new (first_token, token);
15312	  DEFARG_INSTANTIATIONS (default_argument) = NULL;
15313	}
15314      /* Outside of a class definition, we can just parse the
15315	 assignment-expression.  */
15316      else
15317	{
15318	  token = cp_lexer_peek_token (parser->lexer);
15319	  default_argument
15320	    = cp_parser_default_argument (parser, template_parm_p);
15321	}
15322
15323      if (!parser->default_arg_ok_p)
15324	{
15325	  if (flag_permissive)
15326	    warning (0, "deprecated use of default argument for parameter of non-function");
15327	  else
15328	    {
15329	      error_at (token->location,
15330			"default arguments are only "
15331			"permitted for function parameters");
15332	      default_argument = NULL_TREE;
15333	    }
15334	}
15335      else if ((declarator && declarator->parameter_pack_p)
15336	       || (decl_specifiers.type
15337		   && PACK_EXPANSION_P (decl_specifiers.type)))
15338	{
15339	  /* Find the name of the parameter pack.  */
15340	  cp_declarator *id_declarator = declarator;
15341	  while (id_declarator && id_declarator->kind != cdk_id)
15342	    id_declarator = id_declarator->declarator;
15343
15344	  if (id_declarator && id_declarator->kind == cdk_id)
15345	    error_at (declarator_token_start->location,
15346		      template_parm_p
15347		      ? "template parameter pack %qD"
15348		      " cannot have a default argument"
15349		      : "parameter pack %qD cannot have a default argument",
15350		      id_declarator->u.id.unqualified_name);
15351	  else
15352	    error_at (declarator_token_start->location,
15353		      template_parm_p
15354		      ? "template parameter pack cannot have a default argument"
15355		      : "parameter pack cannot have a default argument");
15356
15357	  default_argument = NULL_TREE;
15358	}
15359    }
15360  else
15361    default_argument = NULL_TREE;
15362
15363  return make_parameter_declarator (&decl_specifiers,
15364				    declarator,
15365				    default_argument);
15366}
15367
15368/* Parse a default argument and return it.
15369
15370   TEMPLATE_PARM_P is true if this is a default argument for a
15371   non-type template parameter.  */
15372static tree
15373cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15374{
15375  tree default_argument = NULL_TREE;
15376  bool saved_greater_than_is_operator_p;
15377  bool saved_local_variables_forbidden_p;
15378
15379  /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15380     set correctly.  */
15381  saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15382  parser->greater_than_is_operator_p = !template_parm_p;
15383  /* Local variable names (and the `this' keyword) may not
15384     appear in a default argument.  */
15385  saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15386  parser->local_variables_forbidden_p = true;
15387  /* Parse the assignment-expression.  */
15388  if (template_parm_p)
15389    push_deferring_access_checks (dk_no_deferred);
15390  default_argument
15391    = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15392  if (template_parm_p)
15393    pop_deferring_access_checks ();
15394  parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15395  parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15396
15397  return default_argument;
15398}
15399
15400/* Parse a function-body.
15401
15402   function-body:
15403     compound_statement  */
15404
15405static void
15406cp_parser_function_body (cp_parser *parser)
15407{
15408  cp_parser_compound_statement (parser, NULL, false);
15409}
15410
15411/* Parse a ctor-initializer-opt followed by a function-body.  Return
15412   true if a ctor-initializer was present.  */
15413
15414static bool
15415cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15416{
15417  tree body;
15418  bool ctor_initializer_p;
15419
15420  /* Begin the function body.  */
15421  body = begin_function_body ();
15422  /* Parse the optional ctor-initializer.  */
15423  ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15424  /* Parse the function-body.  */
15425  cp_parser_function_body (parser);
15426  /* Finish the function body.  */
15427  finish_function_body (body);
15428
15429  return ctor_initializer_p;
15430}
15431
15432/* Parse an initializer.
15433
15434   initializer:
15435     = initializer-clause
15436     ( expression-list )
15437
15438   Returns an expression representing the initializer.  If no
15439   initializer is present, NULL_TREE is returned.
15440
15441   *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15442   production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15443   set to TRUE if there is no initializer present.  If there is an
15444   initializer, and it is not a constant-expression, *NON_CONSTANT_P
15445   is set to true; otherwise it is set to false.  */
15446
15447static tree
15448cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15449		       bool* non_constant_p)
15450{
15451  cp_token *token;
15452  tree init;
15453
15454  /* Peek at the next token.  */
15455  token = cp_lexer_peek_token (parser->lexer);
15456
15457  /* Let our caller know whether or not this initializer was
15458     parenthesized.  */
15459  *is_direct_init = (token->type != CPP_EQ);
15460  /* Assume that the initializer is constant.  */
15461  *non_constant_p = false;
15462
15463  if (token->type == CPP_EQ)
15464    {
15465      /* Consume the `='.  */
15466      cp_lexer_consume_token (parser->lexer);
15467      /* Parse the initializer-clause.  */
15468      init = cp_parser_initializer_clause (parser, non_constant_p);
15469    }
15470  else if (token->type == CPP_OPEN_PAREN)
15471    {
15472      VEC(tree,gc) *vec;
15473      vec = cp_parser_parenthesized_expression_list (parser, false,
15474						     /*cast_p=*/false,
15475						     /*allow_expansion_p=*/true,
15476						     non_constant_p);
15477      if (vec == NULL)
15478	return error_mark_node;
15479      init = build_tree_list_vec (vec);
15480      release_tree_vector (vec);
15481    }
15482  else if (token->type == CPP_OPEN_BRACE)
15483    {
15484      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15485      init = cp_parser_braced_list (parser, non_constant_p);
15486      CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15487    }
15488  else
15489    {
15490      /* Anything else is an error.  */
15491      cp_parser_error (parser, "expected initializer");
15492      init = error_mark_node;
15493    }
15494
15495  return init;
15496}
15497
15498/* Parse an initializer-clause.
15499
15500   initializer-clause:
15501     assignment-expression
15502     braced-init-list
15503
15504   Returns an expression representing the initializer.
15505
15506   If the `assignment-expression' production is used the value
15507   returned is simply a representation for the expression.
15508
15509   Otherwise, calls cp_parser_braced_list.  */
15510
15511static tree
15512cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15513{
15514  tree initializer;
15515
15516  /* Assume the expression is constant.  */
15517  *non_constant_p = false;
15518
15519  /* If it is not a `{', then we are looking at an
15520     assignment-expression.  */
15521  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15522    {
15523      initializer
15524	= cp_parser_constant_expression (parser,
15525					/*allow_non_constant_p=*/true,
15526					non_constant_p);
15527      if (!*non_constant_p)
15528	initializer = fold_non_dependent_expr (initializer);
15529    }
15530  else
15531    initializer = cp_parser_braced_list (parser, non_constant_p);
15532
15533  return initializer;
15534}
15535
15536/* Parse a brace-enclosed initializer list.
15537
15538   braced-init-list:
15539     { initializer-list , [opt] }
15540     { }
15541
15542   Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15543   the elements of the initializer-list (or NULL, if the last
15544   production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15545   NULL_TREE.  There is no way to detect whether or not the optional
15546   trailing `,' was provided.  NON_CONSTANT_P is as for
15547   cp_parser_initializer.  */
15548
15549static tree
15550cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15551{
15552  tree initializer;
15553
15554  /* Consume the `{' token.  */
15555  cp_lexer_consume_token (parser->lexer);
15556  /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15557  initializer = make_node (CONSTRUCTOR);
15558  /* If it's not a `}', then there is a non-trivial initializer.  */
15559  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15560    {
15561      /* Parse the initializer list.  */
15562      CONSTRUCTOR_ELTS (initializer)
15563	= cp_parser_initializer_list (parser, non_constant_p);
15564      /* A trailing `,' token is allowed.  */
15565      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15566	cp_lexer_consume_token (parser->lexer);
15567    }
15568  /* Now, there should be a trailing `}'.  */
15569  cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15570  TREE_TYPE (initializer) = init_list_type_node;
15571  return initializer;
15572}
15573
15574/* Parse an initializer-list.
15575
15576   initializer-list:
15577     initializer-clause ... [opt]
15578     initializer-list , initializer-clause ... [opt]
15579
15580   GNU Extension:
15581
15582   initializer-list:
15583     identifier : initializer-clause
15584     initializer-list, identifier : initializer-clause
15585
15586   Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15587   for the initializer.  If the INDEX of the elt is non-NULL, it is the
15588   IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15589   as for cp_parser_initializer.  */
15590
15591static VEC(constructor_elt,gc) *
15592cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15593{
15594  VEC(constructor_elt,gc) *v = NULL;
15595
15596  /* Assume all of the expressions are constant.  */
15597  *non_constant_p = false;
15598
15599  /* Parse the rest of the list.  */
15600  while (true)
15601    {
15602      cp_token *token;
15603      tree identifier;
15604      tree initializer;
15605      bool clause_non_constant_p;
15606
15607      /* If the next token is an identifier and the following one is a
15608	 colon, we are looking at the GNU designated-initializer
15609	 syntax.  */
15610      if (cp_parser_allow_gnu_extensions_p (parser)
15611	  && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15612	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15613	{
15614	  /* Warn the user that they are using an extension.  */
15615	  pedwarn (input_location, OPT_pedantic,
15616		   "ISO C++ does not allow designated initializers");
15617	  /* Consume the identifier.  */
15618	  identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15619	  /* Consume the `:'.  */
15620	  cp_lexer_consume_token (parser->lexer);
15621	}
15622      else
15623	identifier = NULL_TREE;
15624
15625      /* Parse the initializer.  */
15626      initializer = cp_parser_initializer_clause (parser,
15627						  &clause_non_constant_p);
15628      /* If any clause is non-constant, so is the entire initializer.  */
15629      if (clause_non_constant_p)
15630	*non_constant_p = true;
15631
15632      /* If we have an ellipsis, this is an initializer pack
15633	 expansion.  */
15634      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15635        {
15636          /* Consume the `...'.  */
15637          cp_lexer_consume_token (parser->lexer);
15638
15639          /* Turn the initializer into an initializer expansion.  */
15640          initializer = make_pack_expansion (initializer);
15641        }
15642
15643      /* Add it to the vector.  */
15644      CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15645
15646      /* If the next token is not a comma, we have reached the end of
15647	 the list.  */
15648      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15649	break;
15650
15651      /* Peek at the next token.  */
15652      token = cp_lexer_peek_nth_token (parser->lexer, 2);
15653      /* If the next token is a `}', then we're still done.  An
15654	 initializer-clause can have a trailing `,' after the
15655	 initializer-list and before the closing `}'.  */
15656      if (token->type == CPP_CLOSE_BRACE)
15657	break;
15658
15659      /* Consume the `,' token.  */
15660      cp_lexer_consume_token (parser->lexer);
15661    }
15662
15663  return v;
15664}
15665
15666/* Classes [gram.class] */
15667
15668/* Parse a class-name.
15669
15670   class-name:
15671     identifier
15672     template-id
15673
15674   TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15675   to indicate that names looked up in dependent types should be
15676   assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15677   keyword has been used to indicate that the name that appears next
15678   is a template.  TAG_TYPE indicates the explicit tag given before
15679   the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15680   looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15681   is the class being defined in a class-head.
15682
15683   Returns the TYPE_DECL representing the class.  */
15684
15685static tree
15686cp_parser_class_name (cp_parser *parser,
15687		      bool typename_keyword_p,
15688		      bool template_keyword_p,
15689		      enum tag_types tag_type,
15690		      bool check_dependency_p,
15691		      bool class_head_p,
15692		      bool is_declaration)
15693{
15694  tree decl;
15695  tree scope;
15696  bool typename_p;
15697  cp_token *token;
15698  tree identifier = NULL_TREE;
15699
15700  /* All class-names start with an identifier.  */
15701  token = cp_lexer_peek_token (parser->lexer);
15702  if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15703    {
15704      cp_parser_error (parser, "expected class-name");
15705      return error_mark_node;
15706    }
15707
15708  /* PARSER->SCOPE can be cleared when parsing the template-arguments
15709     to a template-id, so we save it here.  */
15710  scope = parser->scope;
15711  if (scope == error_mark_node)
15712    return error_mark_node;
15713
15714  /* Any name names a type if we're following the `typename' keyword
15715     in a qualified name where the enclosing scope is type-dependent.  */
15716  typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15717		&& dependent_type_p (scope));
15718  /* Handle the common case (an identifier, but not a template-id)
15719     efficiently.  */
15720  if (token->type == CPP_NAME
15721      && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15722    {
15723      cp_token *identifier_token;
15724      bool ambiguous_p;
15725
15726      /* Look for the identifier.  */
15727      identifier_token = cp_lexer_peek_token (parser->lexer);
15728      ambiguous_p = identifier_token->ambiguous_p;
15729      identifier = cp_parser_identifier (parser);
15730      /* If the next token isn't an identifier, we are certainly not
15731	 looking at a class-name.  */
15732      if (identifier == error_mark_node)
15733	decl = error_mark_node;
15734      /* If we know this is a type-name, there's no need to look it
15735	 up.  */
15736      else if (typename_p)
15737	decl = identifier;
15738      else
15739	{
15740	  tree ambiguous_decls;
15741	  /* If we already know that this lookup is ambiguous, then
15742	     we've already issued an error message; there's no reason
15743	     to check again.  */
15744	  if (ambiguous_p)
15745	    {
15746	      cp_parser_simulate_error (parser);
15747	      return error_mark_node;
15748	    }
15749	  /* If the next token is a `::', then the name must be a type
15750	     name.
15751
15752	     [basic.lookup.qual]
15753
15754	     During the lookup for a name preceding the :: scope
15755	     resolution operator, object, function, and enumerator
15756	     names are ignored.  */
15757	  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15758	    tag_type = typename_type;
15759	  /* Look up the name.  */
15760	  decl = cp_parser_lookup_name (parser, identifier,
15761					tag_type,
15762					/*is_template=*/false,
15763					/*is_namespace=*/false,
15764					check_dependency_p,
15765					&ambiguous_decls,
15766					identifier_token->location);
15767	  if (ambiguous_decls)
15768	    {
15769	      if (cp_parser_parsing_tentatively (parser))
15770		cp_parser_simulate_error (parser);
15771	      return error_mark_node;
15772	    }
15773	}
15774    }
15775  else
15776    {
15777      /* Try a template-id.  */
15778      decl = cp_parser_template_id (parser, template_keyword_p,
15779				    check_dependency_p,
15780				    is_declaration);
15781      if (decl == error_mark_node)
15782	return error_mark_node;
15783    }
15784
15785  decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15786
15787  /* If this is a typename, create a TYPENAME_TYPE.  */
15788  if (typename_p && decl != error_mark_node)
15789    {
15790      decl = make_typename_type (scope, decl, typename_type,
15791				 /*complain=*/tf_error);
15792      if (decl != error_mark_node)
15793	decl = TYPE_NAME (decl);
15794    }
15795
15796  /* Check to see that it is really the name of a class.  */
15797  if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15798      && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15799      && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15800    /* Situations like this:
15801
15802	 template <typename T> struct A {
15803	   typename T::template X<int>::I i;
15804	 };
15805
15806       are problematic.  Is `T::template X<int>' a class-name?  The
15807       standard does not seem to be definitive, but there is no other
15808       valid interpretation of the following `::'.  Therefore, those
15809       names are considered class-names.  */
15810    {
15811      decl = make_typename_type (scope, decl, tag_type, tf_error);
15812      if (decl != error_mark_node)
15813	decl = TYPE_NAME (decl);
15814    }
15815  else if (TREE_CODE (decl) != TYPE_DECL
15816	   || TREE_TYPE (decl) == error_mark_node
15817	   || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15818    decl = error_mark_node;
15819
15820  if (decl == error_mark_node)
15821    cp_parser_error (parser, "expected class-name");
15822  else if (identifier && !parser->scope)
15823    maybe_note_name_used_in_class (identifier, decl);
15824
15825  return decl;
15826}
15827
15828/* Parse a class-specifier.
15829
15830   class-specifier:
15831     class-head { member-specification [opt] }
15832
15833   Returns the TREE_TYPE representing the class.  */
15834
15835static tree
15836cp_parser_class_specifier (cp_parser* parser)
15837{
15838  tree type;
15839  tree attributes = NULL_TREE;
15840  bool nested_name_specifier_p;
15841  unsigned saved_num_template_parameter_lists;
15842  bool saved_in_function_body;
15843  bool saved_in_unbraced_linkage_specification_p;
15844  tree old_scope = NULL_TREE;
15845  tree scope = NULL_TREE;
15846  tree bases;
15847
15848  push_deferring_access_checks (dk_no_deferred);
15849
15850  /* Parse the class-head.  */
15851  type = cp_parser_class_head (parser,
15852			       &nested_name_specifier_p,
15853			       &attributes,
15854			       &bases);
15855  /* If the class-head was a semantic disaster, skip the entire body
15856     of the class.  */
15857  if (!type)
15858    {
15859      cp_parser_skip_to_end_of_block_or_statement (parser);
15860      pop_deferring_access_checks ();
15861      return error_mark_node;
15862    }
15863
15864  /* Look for the `{'.  */
15865  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15866    {
15867      pop_deferring_access_checks ();
15868      return error_mark_node;
15869    }
15870
15871  /* Process the base classes. If they're invalid, skip the
15872     entire class body.  */
15873  if (!xref_basetypes (type, bases))
15874    {
15875      /* Consuming the closing brace yields better error messages
15876         later on.  */
15877      if (cp_parser_skip_to_closing_brace (parser))
15878	cp_lexer_consume_token (parser->lexer);
15879      pop_deferring_access_checks ();
15880      return error_mark_node;
15881    }
15882
15883  /* Issue an error message if type-definitions are forbidden here.  */
15884  cp_parser_check_type_definition (parser);
15885  /* Remember that we are defining one more class.  */
15886  ++parser->num_classes_being_defined;
15887  /* Inside the class, surrounding template-parameter-lists do not
15888     apply.  */
15889  saved_num_template_parameter_lists
15890    = parser->num_template_parameter_lists;
15891  parser->num_template_parameter_lists = 0;
15892  /* We are not in a function body.  */
15893  saved_in_function_body = parser->in_function_body;
15894  parser->in_function_body = false;
15895  /* We are not immediately inside an extern "lang" block.  */
15896  saved_in_unbraced_linkage_specification_p
15897    = parser->in_unbraced_linkage_specification_p;
15898  parser->in_unbraced_linkage_specification_p = false;
15899
15900  /* Start the class.  */
15901  if (nested_name_specifier_p)
15902    {
15903      scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15904      old_scope = push_inner_scope (scope);
15905    }
15906  type = begin_class_definition (type, attributes);
15907
15908  if (type == error_mark_node)
15909    /* If the type is erroneous, skip the entire body of the class.  */
15910    cp_parser_skip_to_closing_brace (parser);
15911  else
15912    /* Parse the member-specification.  */
15913    cp_parser_member_specification_opt (parser);
15914
15915  /* Look for the trailing `}'.  */
15916  cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15917  /* Look for trailing attributes to apply to this class.  */
15918  if (cp_parser_allow_gnu_extensions_p (parser))
15919    attributes = cp_parser_attributes_opt (parser);
15920  if (type != error_mark_node)
15921    type = finish_struct (type, attributes);
15922  if (nested_name_specifier_p)
15923    pop_inner_scope (old_scope, scope);
15924  /* If this class is not itself within the scope of another class,
15925     then we need to parse the bodies of all of the queued function
15926     definitions.  Note that the queued functions defined in a class
15927     are not always processed immediately following the
15928     class-specifier for that class.  Consider:
15929
15930       struct A {
15931	 struct B { void f() { sizeof (A); } };
15932       };
15933
15934     If `f' were processed before the processing of `A' were
15935     completed, there would be no way to compute the size of `A'.
15936     Note that the nesting we are interested in here is lexical --
15937     not the semantic nesting given by TYPE_CONTEXT.  In particular,
15938     for:
15939
15940       struct A { struct B; };
15941       struct A::B { void f() { } };
15942
15943     there is no need to delay the parsing of `A::B::f'.  */
15944  if (--parser->num_classes_being_defined == 0)
15945    {
15946      tree queue_entry;
15947      tree fn;
15948      tree class_type = NULL_TREE;
15949      tree pushed_scope = NULL_TREE;
15950
15951      /* In a first pass, parse default arguments to the functions.
15952	 Then, in a second pass, parse the bodies of the functions.
15953	 This two-phased approach handles cases like:
15954
15955	    struct S {
15956	      void f() { g(); }
15957	      void g(int i = 3);
15958	    };
15959
15960	 */
15961      for (TREE_PURPOSE (parser->unparsed_functions_queues)
15962	     = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15963	   (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15964	   TREE_PURPOSE (parser->unparsed_functions_queues)
15965	     = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15966	{
15967	  fn = TREE_VALUE (queue_entry);
15968	  /* If there are default arguments that have not yet been processed,
15969	     take care of them now.  */
15970	  if (class_type != TREE_PURPOSE (queue_entry))
15971	    {
15972	      if (pushed_scope)
15973		pop_scope (pushed_scope);
15974	      class_type = TREE_PURPOSE (queue_entry);
15975	      pushed_scope = push_scope (class_type);
15976	    }
15977	  /* Make sure that any template parameters are in scope.  */
15978	  maybe_begin_member_template_processing (fn);
15979	  /* Parse the default argument expressions.  */
15980	  cp_parser_late_parsing_default_args (parser, fn);
15981	  /* Remove any template parameters from the symbol table.  */
15982	  maybe_end_member_template_processing ();
15983	}
15984      if (pushed_scope)
15985	pop_scope (pushed_scope);
15986      /* Now parse the body of the functions.  */
15987      for (TREE_VALUE (parser->unparsed_functions_queues)
15988	     = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15989	   (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15990	   TREE_VALUE (parser->unparsed_functions_queues)
15991	     = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15992	{
15993	  /* Figure out which function we need to process.  */
15994	  fn = TREE_VALUE (queue_entry);
15995	  /* Parse the function.  */
15996	  cp_parser_late_parsing_for_member (parser, fn);
15997	}
15998    }
15999
16000  /* Put back any saved access checks.  */
16001  pop_deferring_access_checks ();
16002
16003  /* Restore saved state.  */
16004  parser->in_function_body = saved_in_function_body;
16005  parser->num_template_parameter_lists
16006    = saved_num_template_parameter_lists;
16007  parser->in_unbraced_linkage_specification_p
16008    = saved_in_unbraced_linkage_specification_p;
16009
16010  return type;
16011}
16012
16013/* Parse a class-head.
16014
16015   class-head:
16016     class-key identifier [opt] base-clause [opt]
16017     class-key nested-name-specifier identifier base-clause [opt]
16018     class-key nested-name-specifier [opt] template-id
16019       base-clause [opt]
16020
16021   GNU Extensions:
16022     class-key attributes identifier [opt] base-clause [opt]
16023     class-key attributes nested-name-specifier identifier base-clause [opt]
16024     class-key attributes nested-name-specifier [opt] template-id
16025       base-clause [opt]
16026
16027   Upon return BASES is initialized to the list of base classes (or
16028   NULL, if there are none) in the same form returned by
16029   cp_parser_base_clause.
16030
16031   Returns the TYPE of the indicated class.  Sets
16032   *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16033   involving a nested-name-specifier was used, and FALSE otherwise.
16034
16035   Returns error_mark_node if this is not a class-head.
16036
16037   Returns NULL_TREE if the class-head is syntactically valid, but
16038   semantically invalid in a way that means we should skip the entire
16039   body of the class.  */
16040
16041static tree
16042cp_parser_class_head (cp_parser* parser,
16043		      bool* nested_name_specifier_p,
16044		      tree *attributes_p,
16045		      tree *bases)
16046{
16047  tree nested_name_specifier;
16048  enum tag_types class_key;
16049  tree id = NULL_TREE;
16050  tree type = NULL_TREE;
16051  tree attributes;
16052  bool template_id_p = false;
16053  bool qualified_p = false;
16054  bool invalid_nested_name_p = false;
16055  bool invalid_explicit_specialization_p = false;
16056  tree pushed_scope = NULL_TREE;
16057  unsigned num_templates;
16058  cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16059  /* Assume no nested-name-specifier will be present.  */
16060  *nested_name_specifier_p = false;
16061  /* Assume no template parameter lists will be used in defining the
16062     type.  */
16063  num_templates = 0;
16064
16065  *bases = NULL_TREE;
16066
16067  /* Look for the class-key.  */
16068  class_key = cp_parser_class_key (parser);
16069  if (class_key == none_type)
16070    return error_mark_node;
16071
16072  /* Parse the attributes.  */
16073  attributes = cp_parser_attributes_opt (parser);
16074
16075  /* If the next token is `::', that is invalid -- but sometimes
16076     people do try to write:
16077
16078       struct ::S {};
16079
16080     Handle this gracefully by accepting the extra qualifier, and then
16081     issuing an error about it later if this really is a
16082     class-head.  If it turns out just to be an elaborated type
16083     specifier, remain silent.  */
16084  if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16085    qualified_p = true;
16086
16087  push_deferring_access_checks (dk_no_check);
16088
16089  /* Determine the name of the class.  Begin by looking for an
16090     optional nested-name-specifier.  */
16091  nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16092  nested_name_specifier
16093    = cp_parser_nested_name_specifier_opt (parser,
16094					   /*typename_keyword_p=*/false,
16095					   /*check_dependency_p=*/false,
16096					   /*type_p=*/false,
16097					   /*is_declaration=*/false);
16098  /* If there was a nested-name-specifier, then there *must* be an
16099     identifier.  */
16100  if (nested_name_specifier)
16101    {
16102      type_start_token = cp_lexer_peek_token (parser->lexer);
16103      /* Although the grammar says `identifier', it really means
16104	 `class-name' or `template-name'.  You are only allowed to
16105	 define a class that has already been declared with this
16106	 syntax.
16107
16108	 The proposed resolution for Core Issue 180 says that wherever
16109	 you see `class T::X' you should treat `X' as a type-name.
16110
16111	 It is OK to define an inaccessible class; for example:
16112
16113	   class A { class B; };
16114	   class A::B {};
16115
16116	 We do not know if we will see a class-name, or a
16117	 template-name.  We look for a class-name first, in case the
16118	 class-name is a template-id; if we looked for the
16119	 template-name first we would stop after the template-name.  */
16120      cp_parser_parse_tentatively (parser);
16121      type = cp_parser_class_name (parser,
16122				   /*typename_keyword_p=*/false,
16123				   /*template_keyword_p=*/false,
16124				   class_type,
16125				   /*check_dependency_p=*/false,
16126				   /*class_head_p=*/true,
16127				   /*is_declaration=*/false);
16128      /* If that didn't work, ignore the nested-name-specifier.  */
16129      if (!cp_parser_parse_definitely (parser))
16130	{
16131	  invalid_nested_name_p = true;
16132	  type_start_token = cp_lexer_peek_token (parser->lexer);
16133	  id = cp_parser_identifier (parser);
16134	  if (id == error_mark_node)
16135	    id = NULL_TREE;
16136	}
16137      /* If we could not find a corresponding TYPE, treat this
16138	 declaration like an unqualified declaration.  */
16139      if (type == error_mark_node)
16140	nested_name_specifier = NULL_TREE;
16141      /* Otherwise, count the number of templates used in TYPE and its
16142	 containing scopes.  */
16143      else
16144	{
16145	  tree scope;
16146
16147	  for (scope = TREE_TYPE (type);
16148	       scope && TREE_CODE (scope) != NAMESPACE_DECL;
16149	       scope = (TYPE_P (scope)
16150			? TYPE_CONTEXT (scope)
16151			: DECL_CONTEXT (scope)))
16152	    if (TYPE_P (scope)
16153		&& CLASS_TYPE_P (scope)
16154		&& CLASSTYPE_TEMPLATE_INFO (scope)
16155		&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16156		&& !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16157	      ++num_templates;
16158	}
16159    }
16160  /* Otherwise, the identifier is optional.  */
16161  else
16162    {
16163      /* We don't know whether what comes next is a template-id,
16164	 an identifier, or nothing at all.  */
16165      cp_parser_parse_tentatively (parser);
16166      /* Check for a template-id.  */
16167      type_start_token = cp_lexer_peek_token (parser->lexer);
16168      id = cp_parser_template_id (parser,
16169				  /*template_keyword_p=*/false,
16170				  /*check_dependency_p=*/true,
16171				  /*is_declaration=*/true);
16172      /* If that didn't work, it could still be an identifier.  */
16173      if (!cp_parser_parse_definitely (parser))
16174	{
16175	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16176	    {
16177	      type_start_token = cp_lexer_peek_token (parser->lexer);
16178	      id = cp_parser_identifier (parser);
16179	    }
16180	  else
16181	    id = NULL_TREE;
16182	}
16183      else
16184	{
16185	  template_id_p = true;
16186	  ++num_templates;
16187	}
16188    }
16189
16190  pop_deferring_access_checks ();
16191
16192  if (id)
16193    cp_parser_check_for_invalid_template_id (parser, id,
16194					     type_start_token->location);
16195
16196  /* If it's not a `:' or a `{' then we can't really be looking at a
16197     class-head, since a class-head only appears as part of a
16198     class-specifier.  We have to detect this situation before calling
16199     xref_tag, since that has irreversible side-effects.  */
16200  if (!cp_parser_next_token_starts_class_definition_p (parser))
16201    {
16202      cp_parser_error (parser, "expected %<{%> or %<:%>");
16203      return error_mark_node;
16204    }
16205
16206  /* At this point, we're going ahead with the class-specifier, even
16207     if some other problem occurs.  */
16208  cp_parser_commit_to_tentative_parse (parser);
16209  /* Issue the error about the overly-qualified name now.  */
16210  if (qualified_p)
16211    {
16212      cp_parser_error (parser,
16213		       "global qualification of class name is invalid");
16214      return error_mark_node;
16215    }
16216  else if (invalid_nested_name_p)
16217    {
16218      cp_parser_error (parser,
16219		       "qualified name does not name a class");
16220      return error_mark_node;
16221    }
16222  else if (nested_name_specifier)
16223    {
16224      tree scope;
16225
16226      /* Reject typedef-names in class heads.  */
16227      if (!DECL_IMPLICIT_TYPEDEF_P (type))
16228	{
16229	  error_at (type_start_token->location,
16230		    "invalid class name in declaration of %qD",
16231		    type);
16232	  type = NULL_TREE;
16233	  goto done;
16234	}
16235
16236      /* Figure out in what scope the declaration is being placed.  */
16237      scope = current_scope ();
16238      /* If that scope does not contain the scope in which the
16239	 class was originally declared, the program is invalid.  */
16240      if (scope && !is_ancestor (scope, nested_name_specifier))
16241	{
16242	  if (at_namespace_scope_p ())
16243	    error_at (type_start_token->location,
16244		      "declaration of %qD in namespace %qD which does not "
16245		      "enclose %qD",
16246		      type, scope, nested_name_specifier);
16247	  else
16248	    error_at (type_start_token->location,
16249		      "declaration of %qD in %qD which does not enclose %qD",
16250		      type, scope, nested_name_specifier);
16251	  type = NULL_TREE;
16252	  goto done;
16253	}
16254      /* [dcl.meaning]
16255
16256	 A declarator-id shall not be qualified except for the
16257	 definition of a ... nested class outside of its class
16258	 ... [or] the definition or explicit instantiation of a
16259	 class member of a namespace outside of its namespace.  */
16260      if (scope == nested_name_specifier)
16261	{
16262	  permerror (nested_name_specifier_token_start->location,
16263		     "extra qualification not allowed");
16264	  nested_name_specifier = NULL_TREE;
16265	  num_templates = 0;
16266	}
16267    }
16268  /* An explicit-specialization must be preceded by "template <>".  If
16269     it is not, try to recover gracefully.  */
16270  if (at_namespace_scope_p ()
16271      && parser->num_template_parameter_lists == 0
16272      && template_id_p)
16273    {
16274      error_at (type_start_token->location,
16275		"an explicit specialization must be preceded by %<template <>%>");
16276      invalid_explicit_specialization_p = true;
16277      /* Take the same action that would have been taken by
16278	 cp_parser_explicit_specialization.  */
16279      ++parser->num_template_parameter_lists;
16280      begin_specialization ();
16281    }
16282  /* There must be no "return" statements between this point and the
16283     end of this function; set "type "to the correct return value and
16284     use "goto done;" to return.  */
16285  /* Make sure that the right number of template parameters were
16286     present.  */
16287  if (!cp_parser_check_template_parameters (parser, num_templates,
16288					    type_start_token->location,
16289					    /*declarator=*/NULL))
16290    {
16291      /* If something went wrong, there is no point in even trying to
16292	 process the class-definition.  */
16293      type = NULL_TREE;
16294      goto done;
16295    }
16296
16297  /* Look up the type.  */
16298  if (template_id_p)
16299    {
16300      if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16301	  && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16302	      || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16303	{
16304	  error_at (type_start_token->location,
16305		    "function template %qD redeclared as a class template", id);
16306	  type = error_mark_node;
16307	}
16308      else
16309	{
16310	  type = TREE_TYPE (id);
16311	  type = maybe_process_partial_specialization (type);
16312	}
16313      if (nested_name_specifier)
16314	pushed_scope = push_scope (nested_name_specifier);
16315    }
16316  else if (nested_name_specifier)
16317    {
16318      tree class_type;
16319
16320      /* Given:
16321
16322	    template <typename T> struct S { struct T };
16323	    template <typename T> struct S<T>::T { };
16324
16325	 we will get a TYPENAME_TYPE when processing the definition of
16326	 `S::T'.  We need to resolve it to the actual type before we
16327	 try to define it.  */
16328      if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16329	{
16330	  class_type = resolve_typename_type (TREE_TYPE (type),
16331					      /*only_current_p=*/false);
16332	  if (TREE_CODE (class_type) != TYPENAME_TYPE)
16333	    type = TYPE_NAME (class_type);
16334	  else
16335	    {
16336	      cp_parser_error (parser, "could not resolve typename type");
16337	      type = error_mark_node;
16338	    }
16339	}
16340
16341      if (maybe_process_partial_specialization (TREE_TYPE (type))
16342	  == error_mark_node)
16343	{
16344	  type = NULL_TREE;
16345	  goto done;
16346	}
16347
16348      class_type = current_class_type;
16349      /* Enter the scope indicated by the nested-name-specifier.  */
16350      pushed_scope = push_scope (nested_name_specifier);
16351      /* Get the canonical version of this type.  */
16352      type = TYPE_MAIN_DECL (TREE_TYPE (type));
16353      if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16354	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16355	{
16356	  type = push_template_decl (type);
16357	  if (type == error_mark_node)
16358	    {
16359	      type = NULL_TREE;
16360	      goto done;
16361	    }
16362	}
16363
16364      type = TREE_TYPE (type);
16365      *nested_name_specifier_p = true;
16366    }
16367  else      /* The name is not a nested name.  */
16368    {
16369      /* If the class was unnamed, create a dummy name.  */
16370      if (!id)
16371	id = make_anon_name ();
16372      type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16373		       parser->num_template_parameter_lists);
16374    }
16375
16376  /* Indicate whether this class was declared as a `class' or as a
16377     `struct'.  */
16378  if (TREE_CODE (type) == RECORD_TYPE)
16379    CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16380  cp_parser_check_class_key (class_key, type);
16381
16382  /* If this type was already complete, and we see another definition,
16383     that's an error.  */
16384  if (type != error_mark_node && COMPLETE_TYPE_P (type))
16385    {
16386      error_at (type_start_token->location, "redefinition of %q#T",
16387		type);
16388      error_at (type_start_token->location, "previous definition of %q+#T",
16389		type);
16390      type = NULL_TREE;
16391      goto done;
16392    }
16393  else if (type == error_mark_node)
16394    type = NULL_TREE;
16395
16396  /* We will have entered the scope containing the class; the names of
16397     base classes should be looked up in that context.  For example:
16398
16399       struct A { struct B {}; struct C; };
16400       struct A::C : B {};
16401
16402     is valid.  */
16403
16404  /* Get the list of base-classes, if there is one.  */
16405  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16406    *bases = cp_parser_base_clause (parser);
16407
16408 done:
16409  /* Leave the scope given by the nested-name-specifier.  We will
16410     enter the class scope itself while processing the members.  */
16411  if (pushed_scope)
16412    pop_scope (pushed_scope);
16413
16414  if (invalid_explicit_specialization_p)
16415    {
16416      end_specialization ();
16417      --parser->num_template_parameter_lists;
16418    }
16419  *attributes_p = attributes;
16420  return type;
16421}
16422
16423/* Parse a class-key.
16424
16425   class-key:
16426     class
16427     struct
16428     union
16429
16430   Returns the kind of class-key specified, or none_type to indicate
16431   error.  */
16432
16433static enum tag_types
16434cp_parser_class_key (cp_parser* parser)
16435{
16436  cp_token *token;
16437  enum tag_types tag_type;
16438
16439  /* Look for the class-key.  */
16440  token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16441  if (!token)
16442    return none_type;
16443
16444  /* Check to see if the TOKEN is a class-key.  */
16445  tag_type = cp_parser_token_is_class_key (token);
16446  if (!tag_type)
16447    cp_parser_error (parser, "expected class-key");
16448  return tag_type;
16449}
16450
16451/* Parse an (optional) member-specification.
16452
16453   member-specification:
16454     member-declaration member-specification [opt]
16455     access-specifier : member-specification [opt]  */
16456
16457static void
16458cp_parser_member_specification_opt (cp_parser* parser)
16459{
16460  while (true)
16461    {
16462      cp_token *token;
16463      enum rid keyword;
16464
16465      /* Peek at the next token.  */
16466      token = cp_lexer_peek_token (parser->lexer);
16467      /* If it's a `}', or EOF then we've seen all the members.  */
16468      if (token->type == CPP_CLOSE_BRACE
16469	  || token->type == CPP_EOF
16470	  || token->type == CPP_PRAGMA_EOL)
16471	break;
16472
16473      /* See if this token is a keyword.  */
16474      keyword = token->keyword;
16475      switch (keyword)
16476	{
16477	case RID_PUBLIC:
16478	case RID_PROTECTED:
16479	case RID_PRIVATE:
16480	  /* Consume the access-specifier.  */
16481	  cp_lexer_consume_token (parser->lexer);
16482	  /* Remember which access-specifier is active.  */
16483	  current_access_specifier = token->u.value;
16484	  /* Look for the `:'.  */
16485	  cp_parser_require (parser, CPP_COLON, "%<:%>");
16486	  break;
16487
16488	default:
16489	  /* Accept #pragmas at class scope.  */
16490	  if (token->type == CPP_PRAGMA)
16491	    {
16492	      cp_parser_pragma (parser, pragma_external);
16493	      break;
16494	    }
16495
16496	  /* Otherwise, the next construction must be a
16497	     member-declaration.  */
16498	  cp_parser_member_declaration (parser);
16499	}
16500    }
16501}
16502
16503/* Parse a member-declaration.
16504
16505   member-declaration:
16506     decl-specifier-seq [opt] member-declarator-list [opt] ;
16507     function-definition ; [opt]
16508     :: [opt] nested-name-specifier template [opt] unqualified-id ;
16509     using-declaration
16510     template-declaration
16511
16512   member-declarator-list:
16513     member-declarator
16514     member-declarator-list , member-declarator
16515
16516   member-declarator:
16517     declarator pure-specifier [opt]
16518     declarator constant-initializer [opt]
16519     identifier [opt] : constant-expression
16520
16521   GNU Extensions:
16522
16523   member-declaration:
16524     __extension__ member-declaration
16525
16526   member-declarator:
16527     declarator attributes [opt] pure-specifier [opt]
16528     declarator attributes [opt] constant-initializer [opt]
16529     identifier [opt] attributes [opt] : constant-expression
16530
16531   C++0x Extensions:
16532
16533   member-declaration:
16534     static_assert-declaration  */
16535
16536static void
16537cp_parser_member_declaration (cp_parser* parser)
16538{
16539  cp_decl_specifier_seq decl_specifiers;
16540  tree prefix_attributes;
16541  tree decl;
16542  int declares_class_or_enum;
16543  bool friend_p;
16544  cp_token *token = NULL;
16545  cp_token *decl_spec_token_start = NULL;
16546  cp_token *initializer_token_start = NULL;
16547  int saved_pedantic;
16548
16549  /* Check for the `__extension__' keyword.  */
16550  if (cp_parser_extension_opt (parser, &saved_pedantic))
16551    {
16552      /* Recurse.  */
16553      cp_parser_member_declaration (parser);
16554      /* Restore the old value of the PEDANTIC flag.  */
16555      pedantic = saved_pedantic;
16556
16557      return;
16558    }
16559
16560  /* Check for a template-declaration.  */
16561  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16562    {
16563      /* An explicit specialization here is an error condition, and we
16564	 expect the specialization handler to detect and report this.  */
16565      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16566	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16567	cp_parser_explicit_specialization (parser);
16568      else
16569	cp_parser_template_declaration (parser, /*member_p=*/true);
16570
16571      return;
16572    }
16573
16574  /* Check for a using-declaration.  */
16575  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16576    {
16577      /* Parse the using-declaration.  */
16578      cp_parser_using_declaration (parser,
16579				   /*access_declaration_p=*/false);
16580      return;
16581    }
16582
16583  /* Check for @defs.  */
16584  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16585    {
16586      tree ivar, member;
16587      tree ivar_chains = cp_parser_objc_defs_expression (parser);
16588      ivar = ivar_chains;
16589      while (ivar)
16590	{
16591	  member = ivar;
16592	  ivar = TREE_CHAIN (member);
16593	  TREE_CHAIN (member) = NULL_TREE;
16594	  finish_member_declaration (member);
16595	}
16596      return;
16597    }
16598
16599  /* If the next token is `static_assert' we have a static assertion.  */
16600  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16601    {
16602      cp_parser_static_assert (parser, /*member_p=*/true);
16603      return;
16604    }
16605
16606  if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16607    return;
16608
16609  /* Parse the decl-specifier-seq.  */
16610  decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16611  cp_parser_decl_specifier_seq (parser,
16612				CP_PARSER_FLAGS_OPTIONAL,
16613				&decl_specifiers,
16614				&declares_class_or_enum);
16615  prefix_attributes = decl_specifiers.attributes;
16616  decl_specifiers.attributes = NULL_TREE;
16617  /* Check for an invalid type-name.  */
16618  if (!decl_specifiers.any_type_specifiers_p
16619      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16620    return;
16621  /* If there is no declarator, then the decl-specifier-seq should
16622     specify a type.  */
16623  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16624    {
16625      /* If there was no decl-specifier-seq, and the next token is a
16626	 `;', then we have something like:
16627
16628	   struct S { ; };
16629
16630	 [class.mem]
16631
16632	 Each member-declaration shall declare at least one member
16633	 name of the class.  */
16634      if (!decl_specifiers.any_specifiers_p)
16635	{
16636	  cp_token *token = cp_lexer_peek_token (parser->lexer);
16637	  if (!in_system_header_at (token->location))
16638	    pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16639	}
16640      else
16641	{
16642	  tree type;
16643
16644	  /* See if this declaration is a friend.  */
16645	  friend_p = cp_parser_friend_p (&decl_specifiers);
16646	  /* If there were decl-specifiers, check to see if there was
16647	     a class-declaration.  */
16648	  type = check_tag_decl (&decl_specifiers);
16649	  /* Nested classes have already been added to the class, but
16650	     a `friend' needs to be explicitly registered.  */
16651	  if (friend_p)
16652	    {
16653	      /* If the `friend' keyword was present, the friend must
16654		 be introduced with a class-key.  */
16655	       if (!declares_class_or_enum)
16656		 error_at (decl_spec_token_start->location,
16657			   "a class-key must be used when declaring a friend");
16658	       /* In this case:
16659
16660		    template <typename T> struct A {
16661		      friend struct A<T>::B;
16662		    };
16663
16664		  A<T>::B will be represented by a TYPENAME_TYPE, and
16665		  therefore not recognized by check_tag_decl.  */
16666	       if (!type
16667		   && decl_specifiers.type
16668		   && TYPE_P (decl_specifiers.type))
16669		 type = decl_specifiers.type;
16670	       if (!type || !TYPE_P (type))
16671		 error_at (decl_spec_token_start->location,
16672			   "friend declaration does not name a class or "
16673			   "function");
16674	       else
16675		 make_friend_class (current_class_type, type,
16676				    /*complain=*/true);
16677	    }
16678	  /* If there is no TYPE, an error message will already have
16679	     been issued.  */
16680	  else if (!type || type == error_mark_node)
16681	    ;
16682	  /* An anonymous aggregate has to be handled specially; such
16683	     a declaration really declares a data member (with a
16684	     particular type), as opposed to a nested class.  */
16685	  else if (ANON_AGGR_TYPE_P (type))
16686	    {
16687	      /* Remove constructors and such from TYPE, now that we
16688		 know it is an anonymous aggregate.  */
16689	      fixup_anonymous_aggr (type);
16690	      /* And make the corresponding data member.  */
16691	      decl = build_decl (decl_spec_token_start->location,
16692				 FIELD_DECL, NULL_TREE, type);
16693	      /* Add it to the class.  */
16694	      finish_member_declaration (decl);
16695	    }
16696	  else
16697	    cp_parser_check_access_in_redeclaration
16698					      (TYPE_NAME (type),
16699					       decl_spec_token_start->location);
16700	}
16701    }
16702  else
16703    {
16704      /* See if these declarations will be friends.  */
16705      friend_p = cp_parser_friend_p (&decl_specifiers);
16706
16707      /* Keep going until we hit the `;' at the end of the
16708	 declaration.  */
16709      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16710	{
16711	  tree attributes = NULL_TREE;
16712	  tree first_attribute;
16713
16714	  /* Peek at the next token.  */
16715	  token = cp_lexer_peek_token (parser->lexer);
16716
16717	  /* Check for a bitfield declaration.  */
16718	  if (token->type == CPP_COLON
16719	      || (token->type == CPP_NAME
16720		  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16721		  == CPP_COLON))
16722	    {
16723	      tree identifier;
16724	      tree width;
16725
16726	      /* Get the name of the bitfield.  Note that we cannot just
16727		 check TOKEN here because it may have been invalidated by
16728		 the call to cp_lexer_peek_nth_token above.  */
16729	      if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16730		identifier = cp_parser_identifier (parser);
16731	      else
16732		identifier = NULL_TREE;
16733
16734	      /* Consume the `:' token.  */
16735	      cp_lexer_consume_token (parser->lexer);
16736	      /* Get the width of the bitfield.  */
16737	      width
16738		= cp_parser_constant_expression (parser,
16739						 /*allow_non_constant=*/false,
16740						 NULL);
16741
16742	      /* Look for attributes that apply to the bitfield.  */
16743	      attributes = cp_parser_attributes_opt (parser);
16744	      /* Remember which attributes are prefix attributes and
16745		 which are not.  */
16746	      first_attribute = attributes;
16747	      /* Combine the attributes.  */
16748	      attributes = chainon (prefix_attributes, attributes);
16749
16750	      /* Create the bitfield declaration.  */
16751	      decl = grokbitfield (identifier
16752				   ? make_id_declarator (NULL_TREE,
16753							 identifier,
16754							 sfk_none)
16755				   : NULL,
16756				   &decl_specifiers,
16757				   width,
16758				   attributes);
16759	    }
16760	  else
16761	    {
16762	      cp_declarator *declarator;
16763	      tree initializer;
16764	      tree asm_specification;
16765	      int ctor_dtor_or_conv_p;
16766
16767	      /* Parse the declarator.  */
16768	      declarator
16769		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16770					&ctor_dtor_or_conv_p,
16771					/*parenthesized_p=*/NULL,
16772					/*member_p=*/true);
16773
16774	      /* If something went wrong parsing the declarator, make sure
16775		 that we at least consume some tokens.  */
16776	      if (declarator == cp_error_declarator)
16777		{
16778		  /* Skip to the end of the statement.  */
16779		  cp_parser_skip_to_end_of_statement (parser);
16780		  /* If the next token is not a semicolon, that is
16781		     probably because we just skipped over the body of
16782		     a function.  So, we consume a semicolon if
16783		     present, but do not issue an error message if it
16784		     is not present.  */
16785		  if (cp_lexer_next_token_is (parser->lexer,
16786					      CPP_SEMICOLON))
16787		    cp_lexer_consume_token (parser->lexer);
16788		  return;
16789		}
16790
16791	      if (declares_class_or_enum & 2)
16792		cp_parser_check_for_definition_in_return_type
16793					    (declarator, decl_specifiers.type,
16794					     decl_specifiers.type_location);
16795
16796	      /* Look for an asm-specification.  */
16797	      asm_specification = cp_parser_asm_specification_opt (parser);
16798	      /* Look for attributes that apply to the declaration.  */
16799	      attributes = cp_parser_attributes_opt (parser);
16800	      /* Remember which attributes are prefix attributes and
16801		 which are not.  */
16802	      first_attribute = attributes;
16803	      /* Combine the attributes.  */
16804	      attributes = chainon (prefix_attributes, attributes);
16805
16806	      /* If it's an `=', then we have a constant-initializer or a
16807		 pure-specifier.  It is not correct to parse the
16808		 initializer before registering the member declaration
16809		 since the member declaration should be in scope while
16810		 its initializer is processed.  However, the rest of the
16811		 front end does not yet provide an interface that allows
16812		 us to handle this correctly.  */
16813	      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16814		{
16815		  /* In [class.mem]:
16816
16817		     A pure-specifier shall be used only in the declaration of
16818		     a virtual function.
16819
16820		     A member-declarator can contain a constant-initializer
16821		     only if it declares a static member of integral or
16822		     enumeration type.
16823
16824		     Therefore, if the DECLARATOR is for a function, we look
16825		     for a pure-specifier; otherwise, we look for a
16826		     constant-initializer.  When we call `grokfield', it will
16827		     perform more stringent semantics checks.  */
16828		  initializer_token_start = cp_lexer_peek_token (parser->lexer);
16829		  if (function_declarator_p (declarator))
16830		    initializer = cp_parser_pure_specifier (parser);
16831		  else
16832		    /* Parse the initializer.  */
16833		    initializer = cp_parser_constant_initializer (parser);
16834		}
16835	      /* Otherwise, there is no initializer.  */
16836	      else
16837		initializer = NULL_TREE;
16838
16839	      /* See if we are probably looking at a function
16840		 definition.  We are certainly not looking at a
16841		 member-declarator.  Calling `grokfield' has
16842		 side-effects, so we must not do it unless we are sure
16843		 that we are looking at a member-declarator.  */
16844	      if (cp_parser_token_starts_function_definition_p
16845		  (cp_lexer_peek_token (parser->lexer)))
16846		{
16847		  /* The grammar does not allow a pure-specifier to be
16848		     used when a member function is defined.  (It is
16849		     possible that this fact is an oversight in the
16850		     standard, since a pure function may be defined
16851		     outside of the class-specifier.  */
16852		  if (initializer)
16853		    error_at (initializer_token_start->location,
16854			      "pure-specifier on function-definition");
16855		  decl = cp_parser_save_member_function_body (parser,
16856							      &decl_specifiers,
16857							      declarator,
16858							      attributes);
16859		  /* If the member was not a friend, declare it here.  */
16860		  if (!friend_p)
16861		    finish_member_declaration (decl);
16862		  /* Peek at the next token.  */
16863		  token = cp_lexer_peek_token (parser->lexer);
16864		  /* If the next token is a semicolon, consume it.  */
16865		  if (token->type == CPP_SEMICOLON)
16866		    cp_lexer_consume_token (parser->lexer);
16867		  return;
16868		}
16869	      else
16870		if (declarator->kind == cdk_function)
16871		  declarator->id_loc = token->location;
16872		/* Create the declaration.  */
16873		decl = grokfield (declarator, &decl_specifiers,
16874				  initializer, /*init_const_expr_p=*/true,
16875				  asm_specification,
16876				  attributes);
16877	    }
16878
16879	  /* Reset PREFIX_ATTRIBUTES.  */
16880	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
16881	    attributes = TREE_CHAIN (attributes);
16882	  if (attributes)
16883	    TREE_CHAIN (attributes) = NULL_TREE;
16884
16885	  /* If there is any qualification still in effect, clear it
16886	     now; we will be starting fresh with the next declarator.  */
16887	  parser->scope = NULL_TREE;
16888	  parser->qualifying_scope = NULL_TREE;
16889	  parser->object_scope = NULL_TREE;
16890	  /* If it's a `,', then there are more declarators.  */
16891	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16892	    cp_lexer_consume_token (parser->lexer);
16893	  /* If the next token isn't a `;', then we have a parse error.  */
16894	  else if (cp_lexer_next_token_is_not (parser->lexer,
16895					       CPP_SEMICOLON))
16896	    {
16897	      cp_parser_error (parser, "expected %<;%>");
16898	      /* Skip tokens until we find a `;'.  */
16899	      cp_parser_skip_to_end_of_statement (parser);
16900
16901	      break;
16902	    }
16903
16904	  if (decl)
16905	    {
16906	      /* Add DECL to the list of members.  */
16907	      if (!friend_p)
16908		finish_member_declaration (decl);
16909
16910	      if (TREE_CODE (decl) == FUNCTION_DECL)
16911		cp_parser_save_default_args (parser, decl);
16912	    }
16913	}
16914    }
16915
16916  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16917}
16918
16919/* Parse a pure-specifier.
16920
16921   pure-specifier:
16922     = 0
16923
16924   Returns INTEGER_ZERO_NODE if a pure specifier is found.
16925   Otherwise, ERROR_MARK_NODE is returned.  */
16926
16927static tree
16928cp_parser_pure_specifier (cp_parser* parser)
16929{
16930  cp_token *token;
16931
16932  /* Look for the `=' token.  */
16933  if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16934    return error_mark_node;
16935  /* Look for the `0' token.  */
16936  token = cp_lexer_peek_token (parser->lexer);
16937
16938  if (token->type == CPP_EOF
16939      || token->type == CPP_PRAGMA_EOL)
16940    return error_mark_node;
16941
16942  cp_lexer_consume_token (parser->lexer);
16943
16944  /* Accept = default or = delete in c++0x mode.  */
16945  if (token->keyword == RID_DEFAULT
16946      || token->keyword == RID_DELETE)
16947    {
16948      maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16949      return token->u.value;
16950    }
16951
16952  /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16953  if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16954    {
16955      cp_parser_error (parser,
16956		       "invalid pure specifier (only %<= 0%> is allowed)");
16957      cp_parser_skip_to_end_of_statement (parser);
16958      return error_mark_node;
16959    }
16960  if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16961    {
16962      error_at (token->location, "templates may not be %<virtual%>");
16963      return error_mark_node;
16964    }
16965
16966  return integer_zero_node;
16967}
16968
16969/* Parse a constant-initializer.
16970
16971   constant-initializer:
16972     = constant-expression
16973
16974   Returns a representation of the constant-expression.  */
16975
16976static tree
16977cp_parser_constant_initializer (cp_parser* parser)
16978{
16979  /* Look for the `=' token.  */
16980  if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16981    return error_mark_node;
16982
16983  /* It is invalid to write:
16984
16985       struct S { static const int i = { 7 }; };
16986
16987     */
16988  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16989    {
16990      cp_parser_error (parser,
16991		       "a brace-enclosed initializer is not allowed here");
16992      /* Consume the opening brace.  */
16993      cp_lexer_consume_token (parser->lexer);
16994      /* Skip the initializer.  */
16995      cp_parser_skip_to_closing_brace (parser);
16996      /* Look for the trailing `}'.  */
16997      cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16998
16999      return error_mark_node;
17000    }
17001
17002  return cp_parser_constant_expression (parser,
17003					/*allow_non_constant=*/false,
17004					NULL);
17005}
17006
17007/* Derived classes [gram.class.derived] */
17008
17009/* Parse a base-clause.
17010
17011   base-clause:
17012     : base-specifier-list
17013
17014   base-specifier-list:
17015     base-specifier ... [opt]
17016     base-specifier-list , base-specifier ... [opt]
17017
17018   Returns a TREE_LIST representing the base-classes, in the order in
17019   which they were declared.  The representation of each node is as
17020   described by cp_parser_base_specifier.
17021
17022   In the case that no bases are specified, this function will return
17023   NULL_TREE, not ERROR_MARK_NODE.  */
17024
17025static tree
17026cp_parser_base_clause (cp_parser* parser)
17027{
17028  tree bases = NULL_TREE;
17029
17030  /* Look for the `:' that begins the list.  */
17031  cp_parser_require (parser, CPP_COLON, "%<:%>");
17032
17033  /* Scan the base-specifier-list.  */
17034  while (true)
17035    {
17036      cp_token *token;
17037      tree base;
17038      bool pack_expansion_p = false;
17039
17040      /* Look for the base-specifier.  */
17041      base = cp_parser_base_specifier (parser);
17042      /* Look for the (optional) ellipsis. */
17043      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17044        {
17045          /* Consume the `...'. */
17046          cp_lexer_consume_token (parser->lexer);
17047
17048          pack_expansion_p = true;
17049        }
17050
17051      /* Add BASE to the front of the list.  */
17052      if (base != error_mark_node)
17053	{
17054          if (pack_expansion_p)
17055            /* Make this a pack expansion type. */
17056            TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17057
17058
17059          if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17060            {
17061              TREE_CHAIN (base) = bases;
17062              bases = base;
17063            }
17064	}
17065      /* Peek at the next token.  */
17066      token = cp_lexer_peek_token (parser->lexer);
17067      /* If it's not a comma, then the list is complete.  */
17068      if (token->type != CPP_COMMA)
17069	break;
17070      /* Consume the `,'.  */
17071      cp_lexer_consume_token (parser->lexer);
17072    }
17073
17074  /* PARSER->SCOPE may still be non-NULL at this point, if the last
17075     base class had a qualified name.  However, the next name that
17076     appears is certainly not qualified.  */
17077  parser->scope = NULL_TREE;
17078  parser->qualifying_scope = NULL_TREE;
17079  parser->object_scope = NULL_TREE;
17080
17081  return nreverse (bases);
17082}
17083
17084/* Parse a base-specifier.
17085
17086   base-specifier:
17087     :: [opt] nested-name-specifier [opt] class-name
17088     virtual access-specifier [opt] :: [opt] nested-name-specifier
17089       [opt] class-name
17090     access-specifier virtual [opt] :: [opt] nested-name-specifier
17091       [opt] class-name
17092
17093   Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17094   ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17095   indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17096   (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17097
17098static tree
17099cp_parser_base_specifier (cp_parser* parser)
17100{
17101  cp_token *token;
17102  bool done = false;
17103  bool virtual_p = false;
17104  bool duplicate_virtual_error_issued_p = false;
17105  bool duplicate_access_error_issued_p = false;
17106  bool class_scope_p, template_p;
17107  tree access = access_default_node;
17108  tree type;
17109
17110  /* Process the optional `virtual' and `access-specifier'.  */
17111  while (!done)
17112    {
17113      /* Peek at the next token.  */
17114      token = cp_lexer_peek_token (parser->lexer);
17115      /* Process `virtual'.  */
17116      switch (token->keyword)
17117	{
17118	case RID_VIRTUAL:
17119	  /* If `virtual' appears more than once, issue an error.  */
17120	  if (virtual_p && !duplicate_virtual_error_issued_p)
17121	    {
17122	      cp_parser_error (parser,
17123			       "%<virtual%> specified more than once in base-specified");
17124	      duplicate_virtual_error_issued_p = true;
17125	    }
17126
17127	  virtual_p = true;
17128
17129	  /* Consume the `virtual' token.  */
17130	  cp_lexer_consume_token (parser->lexer);
17131
17132	  break;
17133
17134	case RID_PUBLIC:
17135	case RID_PROTECTED:
17136	case RID_PRIVATE:
17137	  /* If more than one access specifier appears, issue an
17138	     error.  */
17139	  if (access != access_default_node
17140	      && !duplicate_access_error_issued_p)
17141	    {
17142	      cp_parser_error (parser,
17143			       "more than one access specifier in base-specified");
17144	      duplicate_access_error_issued_p = true;
17145	    }
17146
17147	  access = ridpointers[(int) token->keyword];
17148
17149	  /* Consume the access-specifier.  */
17150	  cp_lexer_consume_token (parser->lexer);
17151
17152	  break;
17153
17154	default:
17155	  done = true;
17156	  break;
17157	}
17158    }
17159  /* It is not uncommon to see programs mechanically, erroneously, use
17160     the 'typename' keyword to denote (dependent) qualified types
17161     as base classes.  */
17162  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17163    {
17164      token = cp_lexer_peek_token (parser->lexer);
17165      if (!processing_template_decl)
17166	error_at (token->location,
17167		  "keyword %<typename%> not allowed outside of templates");
17168      else
17169	error_at (token->location,
17170		  "keyword %<typename%> not allowed in this context "
17171		  "(the base class is implicitly a type)");
17172      cp_lexer_consume_token (parser->lexer);
17173    }
17174
17175  /* Look for the optional `::' operator.  */
17176  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17177  /* Look for the nested-name-specifier.  The simplest way to
17178     implement:
17179
17180       [temp.res]
17181
17182       The keyword `typename' is not permitted in a base-specifier or
17183       mem-initializer; in these contexts a qualified name that
17184       depends on a template-parameter is implicitly assumed to be a
17185       type name.
17186
17187     is to pretend that we have seen the `typename' keyword at this
17188     point.  */
17189  cp_parser_nested_name_specifier_opt (parser,
17190				       /*typename_keyword_p=*/true,
17191				       /*check_dependency_p=*/true,
17192				       typename_type,
17193				       /*is_declaration=*/true);
17194  /* If the base class is given by a qualified name, assume that names
17195     we see are type names or templates, as appropriate.  */
17196  class_scope_p = (parser->scope && TYPE_P (parser->scope));
17197  template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17198
17199  /* Finally, look for the class-name.  */
17200  type = cp_parser_class_name (parser,
17201			       class_scope_p,
17202			       template_p,
17203			       typename_type,
17204			       /*check_dependency_p=*/true,
17205			       /*class_head_p=*/false,
17206			       /*is_declaration=*/true);
17207
17208  if (type == error_mark_node)
17209    return error_mark_node;
17210
17211  return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17212}
17213
17214/* Exception handling [gram.exception] */
17215
17216/* Parse an (optional) exception-specification.
17217
17218   exception-specification:
17219     throw ( type-id-list [opt] )
17220
17221   Returns a TREE_LIST representing the exception-specification.  The
17222   TREE_VALUE of each node is a type.  */
17223
17224static tree
17225cp_parser_exception_specification_opt (cp_parser* parser)
17226{
17227  cp_token *token;
17228  tree type_id_list;
17229
17230  /* Peek at the next token.  */
17231  token = cp_lexer_peek_token (parser->lexer);
17232  /* If it's not `throw', then there's no exception-specification.  */
17233  if (!cp_parser_is_keyword (token, RID_THROW))
17234    return NULL_TREE;
17235
17236  /* Consume the `throw'.  */
17237  cp_lexer_consume_token (parser->lexer);
17238
17239  /* Look for the `('.  */
17240  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17241
17242  /* Peek at the next token.  */
17243  token = cp_lexer_peek_token (parser->lexer);
17244  /* If it's not a `)', then there is a type-id-list.  */
17245  if (token->type != CPP_CLOSE_PAREN)
17246    {
17247      const char *saved_message;
17248
17249      /* Types may not be defined in an exception-specification.  */
17250      saved_message = parser->type_definition_forbidden_message;
17251      parser->type_definition_forbidden_message
17252	= G_("types may not be defined in an exception-specification");
17253      /* Parse the type-id-list.  */
17254      type_id_list = cp_parser_type_id_list (parser);
17255      /* Restore the saved message.  */
17256      parser->type_definition_forbidden_message = saved_message;
17257    }
17258  else
17259    type_id_list = empty_except_spec;
17260
17261  /* Look for the `)'.  */
17262  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17263
17264  return type_id_list;
17265}
17266
17267/* Parse an (optional) type-id-list.
17268
17269   type-id-list:
17270     type-id ... [opt]
17271     type-id-list , type-id ... [opt]
17272
17273   Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17274   in the order that the types were presented.  */
17275
17276static tree
17277cp_parser_type_id_list (cp_parser* parser)
17278{
17279  tree types = NULL_TREE;
17280
17281  while (true)
17282    {
17283      cp_token *token;
17284      tree type;
17285
17286      /* Get the next type-id.  */
17287      type = cp_parser_type_id (parser);
17288      /* Parse the optional ellipsis. */
17289      if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17290        {
17291          /* Consume the `...'. */
17292          cp_lexer_consume_token (parser->lexer);
17293
17294          /* Turn the type into a pack expansion expression. */
17295          type = make_pack_expansion (type);
17296        }
17297      /* Add it to the list.  */
17298      types = add_exception_specifier (types, type, /*complain=*/1);
17299      /* Peek at the next token.  */
17300      token = cp_lexer_peek_token (parser->lexer);
17301      /* If it is not a `,', we are done.  */
17302      if (token->type != CPP_COMMA)
17303	break;
17304      /* Consume the `,'.  */
17305      cp_lexer_consume_token (parser->lexer);
17306    }
17307
17308  return nreverse (types);
17309}
17310
17311/* Parse a try-block.
17312
17313   try-block:
17314     try compound-statement handler-seq  */
17315
17316static tree
17317cp_parser_try_block (cp_parser* parser)
17318{
17319  tree try_block;
17320
17321  cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17322  try_block = begin_try_block ();
17323  cp_parser_compound_statement (parser, NULL, true);
17324  finish_try_block (try_block);
17325  cp_parser_handler_seq (parser);
17326  finish_handler_sequence (try_block);
17327
17328  return try_block;
17329}
17330
17331/* Parse a function-try-block.
17332
17333   function-try-block:
17334     try ctor-initializer [opt] function-body handler-seq  */
17335
17336static bool
17337cp_parser_function_try_block (cp_parser* parser)
17338{
17339  tree compound_stmt;
17340  tree try_block;
17341  bool ctor_initializer_p;
17342
17343  /* Look for the `try' keyword.  */
17344  if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17345    return false;
17346  /* Let the rest of the front end know where we are.  */
17347  try_block = begin_function_try_block (&compound_stmt);
17348  /* Parse the function-body.  */
17349  ctor_initializer_p
17350    = cp_parser_ctor_initializer_opt_and_function_body (parser);
17351  /* We're done with the `try' part.  */
17352  finish_function_try_block (try_block);
17353  /* Parse the handlers.  */
17354  cp_parser_handler_seq (parser);
17355  /* We're done with the handlers.  */
17356  finish_function_handler_sequence (try_block, compound_stmt);
17357
17358  return ctor_initializer_p;
17359}
17360
17361/* Parse a handler-seq.
17362
17363   handler-seq:
17364     handler handler-seq [opt]  */
17365
17366static void
17367cp_parser_handler_seq (cp_parser* parser)
17368{
17369  while (true)
17370    {
17371      cp_token *token;
17372
17373      /* Parse the handler.  */
17374      cp_parser_handler (parser);
17375      /* Peek at the next token.  */
17376      token = cp_lexer_peek_token (parser->lexer);
17377      /* If it's not `catch' then there are no more handlers.  */
17378      if (!cp_parser_is_keyword (token, RID_CATCH))
17379	break;
17380    }
17381}
17382
17383/* Parse a handler.
17384
17385   handler:
17386     catch ( exception-declaration ) compound-statement  */
17387
17388static void
17389cp_parser_handler (cp_parser* parser)
17390{
17391  tree handler;
17392  tree declaration;
17393
17394  cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17395  handler = begin_handler ();
17396  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17397  declaration = cp_parser_exception_declaration (parser);
17398  finish_handler_parms (declaration, handler);
17399  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17400  cp_parser_compound_statement (parser, NULL, false);
17401  finish_handler (handler);
17402}
17403
17404/* Parse an exception-declaration.
17405
17406   exception-declaration:
17407     type-specifier-seq declarator
17408     type-specifier-seq abstract-declarator
17409     type-specifier-seq
17410     ...
17411
17412   Returns a VAR_DECL for the declaration, or NULL_TREE if the
17413   ellipsis variant is used.  */
17414
17415static tree
17416cp_parser_exception_declaration (cp_parser* parser)
17417{
17418  cp_decl_specifier_seq type_specifiers;
17419  cp_declarator *declarator;
17420  const char *saved_message;
17421
17422  /* If it's an ellipsis, it's easy to handle.  */
17423  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17424    {
17425      /* Consume the `...' token.  */
17426      cp_lexer_consume_token (parser->lexer);
17427      return NULL_TREE;
17428    }
17429
17430  /* Types may not be defined in exception-declarations.  */
17431  saved_message = parser->type_definition_forbidden_message;
17432  parser->type_definition_forbidden_message
17433    = G_("types may not be defined in exception-declarations");
17434
17435  /* Parse the type-specifier-seq.  */
17436  cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17437				/*is_trailing_return=*/false,
17438				&type_specifiers);
17439  /* If it's a `)', then there is no declarator.  */
17440  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17441    declarator = NULL;
17442  else
17443    declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17444				       /*ctor_dtor_or_conv_p=*/NULL,
17445				       /*parenthesized_p=*/NULL,
17446				       /*member_p=*/false);
17447
17448  /* Restore the saved message.  */
17449  parser->type_definition_forbidden_message = saved_message;
17450
17451  if (!type_specifiers.any_specifiers_p)
17452    return error_mark_node;
17453
17454  return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17455}
17456
17457/* Parse a throw-expression.
17458
17459   throw-expression:
17460     throw assignment-expression [opt]
17461
17462   Returns a THROW_EXPR representing the throw-expression.  */
17463
17464static tree
17465cp_parser_throw_expression (cp_parser* parser)
17466{
17467  tree expression;
17468  cp_token* token;
17469
17470  cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17471  token = cp_lexer_peek_token (parser->lexer);
17472  /* Figure out whether or not there is an assignment-expression
17473     following the "throw" keyword.  */
17474  if (token->type == CPP_COMMA
17475      || token->type == CPP_SEMICOLON
17476      || token->type == CPP_CLOSE_PAREN
17477      || token->type == CPP_CLOSE_SQUARE
17478      || token->type == CPP_CLOSE_BRACE
17479      || token->type == CPP_COLON)
17480    expression = NULL_TREE;
17481  else
17482    expression = cp_parser_assignment_expression (parser,
17483						  /*cast_p=*/false, NULL);
17484
17485  return build_throw (expression);
17486}
17487
17488/* GNU Extensions */
17489
17490/* Parse an (optional) asm-specification.
17491
17492   asm-specification:
17493     asm ( string-literal )
17494
17495   If the asm-specification is present, returns a STRING_CST
17496   corresponding to the string-literal.  Otherwise, returns
17497   NULL_TREE.  */
17498
17499static tree
17500cp_parser_asm_specification_opt (cp_parser* parser)
17501{
17502  cp_token *token;
17503  tree asm_specification;
17504
17505  /* Peek at the next token.  */
17506  token = cp_lexer_peek_token (parser->lexer);
17507  /* If the next token isn't the `asm' keyword, then there's no
17508     asm-specification.  */
17509  if (!cp_parser_is_keyword (token, RID_ASM))
17510    return NULL_TREE;
17511
17512  /* Consume the `asm' token.  */
17513  cp_lexer_consume_token (parser->lexer);
17514  /* Look for the `('.  */
17515  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17516
17517  /* Look for the string-literal.  */
17518  asm_specification = cp_parser_string_literal (parser, false, false);
17519
17520  /* Look for the `)'.  */
17521  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17522
17523  return asm_specification;
17524}
17525
17526/* Parse an asm-operand-list.
17527
17528   asm-operand-list:
17529     asm-operand
17530     asm-operand-list , asm-operand
17531
17532   asm-operand:
17533     string-literal ( expression )
17534     [ string-literal ] string-literal ( expression )
17535
17536   Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17537   each node is the expression.  The TREE_PURPOSE is itself a
17538   TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17539   string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17540   is a STRING_CST for the string literal before the parenthesis. Returns
17541   ERROR_MARK_NODE if any of the operands are invalid.  */
17542
17543static tree
17544cp_parser_asm_operand_list (cp_parser* parser)
17545{
17546  tree asm_operands = NULL_TREE;
17547  bool invalid_operands = false;
17548
17549  while (true)
17550    {
17551      tree string_literal;
17552      tree expression;
17553      tree name;
17554
17555      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17556	{
17557	  /* Consume the `[' token.  */
17558	  cp_lexer_consume_token (parser->lexer);
17559	  /* Read the operand name.  */
17560	  name = cp_parser_identifier (parser);
17561	  if (name != error_mark_node)
17562	    name = build_string (IDENTIFIER_LENGTH (name),
17563				 IDENTIFIER_POINTER (name));
17564	  /* Look for the closing `]'.  */
17565	  cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17566	}
17567      else
17568	name = NULL_TREE;
17569      /* Look for the string-literal.  */
17570      string_literal = cp_parser_string_literal (parser, false, false);
17571
17572      /* Look for the `('.  */
17573      cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17574      /* Parse the expression.  */
17575      expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17576      /* Look for the `)'.  */
17577      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17578
17579      if (name == error_mark_node
17580	  || string_literal == error_mark_node
17581	  || expression == error_mark_node)
17582        invalid_operands = true;
17583
17584      /* Add this operand to the list.  */
17585      asm_operands = tree_cons (build_tree_list (name, string_literal),
17586				expression,
17587				asm_operands);
17588      /* If the next token is not a `,', there are no more
17589	 operands.  */
17590      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17591	break;
17592      /* Consume the `,'.  */
17593      cp_lexer_consume_token (parser->lexer);
17594    }
17595
17596  return invalid_operands ? error_mark_node : nreverse (asm_operands);
17597}
17598
17599/* Parse an asm-clobber-list.
17600
17601   asm-clobber-list:
17602     string-literal
17603     asm-clobber-list , string-literal
17604
17605   Returns a TREE_LIST, indicating the clobbers in the order that they
17606   appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17607
17608static tree
17609cp_parser_asm_clobber_list (cp_parser* parser)
17610{
17611  tree clobbers = NULL_TREE;
17612
17613  while (true)
17614    {
17615      tree string_literal;
17616
17617      /* Look for the string literal.  */
17618      string_literal = cp_parser_string_literal (parser, false, false);
17619      /* Add it to the list.  */
17620      clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17621      /* If the next token is not a `,', then the list is
17622	 complete.  */
17623      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17624	break;
17625      /* Consume the `,' token.  */
17626      cp_lexer_consume_token (parser->lexer);
17627    }
17628
17629  return clobbers;
17630}
17631
17632/* Parse an asm-label-list.
17633
17634   asm-label-list:
17635     identifier
17636     asm-label-list , identifier
17637
17638   Returns a TREE_LIST, indicating the labels in the order that they
17639   appeared.  The TREE_VALUE of each node is a label.  */
17640
17641static tree
17642cp_parser_asm_label_list (cp_parser* parser)
17643{
17644  tree labels = NULL_TREE;
17645
17646  while (true)
17647    {
17648      tree identifier, label, name;
17649
17650      /* Look for the identifier.  */
17651      identifier = cp_parser_identifier (parser);
17652      if (!error_operand_p (identifier))
17653        {
17654	  label = lookup_label (identifier);
17655	  if (TREE_CODE (label) == LABEL_DECL)
17656	    {
17657	      TREE_USED (label) = 1;
17658	      check_goto (label);
17659	      name = build_string (IDENTIFIER_LENGTH (identifier),
17660				   IDENTIFIER_POINTER (identifier));
17661	      labels = tree_cons (name, label, labels);
17662	    }
17663	}
17664      /* If the next token is not a `,', then the list is
17665	 complete.  */
17666      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17667	break;
17668      /* Consume the `,' token.  */
17669      cp_lexer_consume_token (parser->lexer);
17670    }
17671
17672  return nreverse (labels);
17673}
17674
17675/* Parse an (optional) series of attributes.
17676
17677   attributes:
17678     attributes attribute
17679
17680   attribute:
17681     __attribute__ (( attribute-list [opt] ))
17682
17683   The return value is as for cp_parser_attribute_list.  */
17684
17685static tree
17686cp_parser_attributes_opt (cp_parser* parser)
17687{
17688  tree attributes = NULL_TREE;
17689
17690  while (true)
17691    {
17692      cp_token *token;
17693      tree attribute_list;
17694
17695      /* Peek at the next token.  */
17696      token = cp_lexer_peek_token (parser->lexer);
17697      /* If it's not `__attribute__', then we're done.  */
17698      if (token->keyword != RID_ATTRIBUTE)
17699	break;
17700
17701      /* Consume the `__attribute__' keyword.  */
17702      cp_lexer_consume_token (parser->lexer);
17703      /* Look for the two `(' tokens.  */
17704      cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17705      cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17706
17707      /* Peek at the next token.  */
17708      token = cp_lexer_peek_token (parser->lexer);
17709      if (token->type != CPP_CLOSE_PAREN)
17710	/* Parse the attribute-list.  */
17711	attribute_list = cp_parser_attribute_list (parser);
17712      else
17713	/* If the next token is a `)', then there is no attribute
17714	   list.  */
17715	attribute_list = NULL;
17716
17717      /* Look for the two `)' tokens.  */
17718      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17719      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17720
17721      /* Add these new attributes to the list.  */
17722      attributes = chainon (attributes, attribute_list);
17723    }
17724
17725  return attributes;
17726}
17727
17728/* Parse an attribute-list.
17729
17730   attribute-list:
17731     attribute
17732     attribute-list , attribute
17733
17734   attribute:
17735     identifier
17736     identifier ( identifier )
17737     identifier ( identifier , expression-list )
17738     identifier ( expression-list )
17739
17740   Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17741   to an attribute.  The TREE_PURPOSE of each node is the identifier
17742   indicating which attribute is in use.  The TREE_VALUE represents
17743   the arguments, if any.  */
17744
17745static tree
17746cp_parser_attribute_list (cp_parser* parser)
17747{
17748  tree attribute_list = NULL_TREE;
17749  bool save_translate_strings_p = parser->translate_strings_p;
17750
17751  parser->translate_strings_p = false;
17752  while (true)
17753    {
17754      cp_token *token;
17755      tree identifier;
17756      tree attribute;
17757
17758      /* Look for the identifier.  We also allow keywords here; for
17759	 example `__attribute__ ((const))' is legal.  */
17760      token = cp_lexer_peek_token (parser->lexer);
17761      if (token->type == CPP_NAME
17762	  || token->type == CPP_KEYWORD)
17763	{
17764	  tree arguments = NULL_TREE;
17765
17766	  /* Consume the token.  */
17767	  token = cp_lexer_consume_token (parser->lexer);
17768
17769	  /* Save away the identifier that indicates which attribute
17770	     this is.  */
17771	  identifier = (token->type == CPP_KEYWORD)
17772	    /* For keywords, use the canonical spelling, not the
17773	       parsed identifier.  */
17774	    ? ridpointers[(int) token->keyword]
17775	    : token->u.value;
17776
17777	  attribute = build_tree_list (identifier, NULL_TREE);
17778
17779	  /* Peek at the next token.  */
17780	  token = cp_lexer_peek_token (parser->lexer);
17781	  /* If it's an `(', then parse the attribute arguments.  */
17782	  if (token->type == CPP_OPEN_PAREN)
17783	    {
17784	      VEC(tree,gc) *vec;
17785	      vec = cp_parser_parenthesized_expression_list
17786		    (parser, true, /*cast_p=*/false,
17787		     /*allow_expansion_p=*/false,
17788		     /*non_constant_p=*/NULL);
17789	      if (vec == NULL)
17790		arguments = error_mark_node;
17791	      else
17792		{
17793		  arguments = build_tree_list_vec (vec);
17794		  release_tree_vector (vec);
17795		}
17796	      /* Save the arguments away.  */
17797	      TREE_VALUE (attribute) = arguments;
17798	    }
17799
17800	  if (arguments != error_mark_node)
17801	    {
17802	      /* Add this attribute to the list.  */
17803	      TREE_CHAIN (attribute) = attribute_list;
17804	      attribute_list = attribute;
17805	    }
17806
17807	  token = cp_lexer_peek_token (parser->lexer);
17808	}
17809      /* Now, look for more attributes.  If the next token isn't a
17810	 `,', we're done.  */
17811      if (token->type != CPP_COMMA)
17812	break;
17813
17814      /* Consume the comma and keep going.  */
17815      cp_lexer_consume_token (parser->lexer);
17816    }
17817  parser->translate_strings_p = save_translate_strings_p;
17818
17819  /* We built up the list in reverse order.  */
17820  return nreverse (attribute_list);
17821}
17822
17823/* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17824   present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17825   current value of the PEDANTIC flag, regardless of whether or not
17826   the `__extension__' keyword is present.  The caller is responsible
17827   for restoring the value of the PEDANTIC flag.  */
17828
17829static bool
17830cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17831{
17832  /* Save the old value of the PEDANTIC flag.  */
17833  *saved_pedantic = pedantic;
17834
17835  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17836    {
17837      /* Consume the `__extension__' token.  */
17838      cp_lexer_consume_token (parser->lexer);
17839      /* We're not being pedantic while the `__extension__' keyword is
17840	 in effect.  */
17841      pedantic = 0;
17842
17843      return true;
17844    }
17845
17846  return false;
17847}
17848
17849/* Parse a label declaration.
17850
17851   label-declaration:
17852     __label__ label-declarator-seq ;
17853
17854   label-declarator-seq:
17855     identifier , label-declarator-seq
17856     identifier  */
17857
17858static void
17859cp_parser_label_declaration (cp_parser* parser)
17860{
17861  /* Look for the `__label__' keyword.  */
17862  cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17863
17864  while (true)
17865    {
17866      tree identifier;
17867
17868      /* Look for an identifier.  */
17869      identifier = cp_parser_identifier (parser);
17870      /* If we failed, stop.  */
17871      if (identifier == error_mark_node)
17872	break;
17873      /* Declare it as a label.  */
17874      finish_label_decl (identifier);
17875      /* If the next token is a `;', stop.  */
17876      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17877	break;
17878      /* Look for the `,' separating the label declarations.  */
17879      cp_parser_require (parser, CPP_COMMA, "%<,%>");
17880    }
17881
17882  /* Look for the final `;'.  */
17883  cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17884}
17885
17886/* Support Functions */
17887
17888/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17889   NAME should have one of the representations used for an
17890   id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17891   is returned.  If PARSER->SCOPE is a dependent type, then a
17892   SCOPE_REF is returned.
17893
17894   If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17895   returned; the name was already resolved when the TEMPLATE_ID_EXPR
17896   was formed.  Abstractly, such entities should not be passed to this
17897   function, because they do not need to be looked up, but it is
17898   simpler to check for this special case here, rather than at the
17899   call-sites.
17900
17901   In cases not explicitly covered above, this function returns a
17902   DECL, OVERLOAD, or baselink representing the result of the lookup.
17903   If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17904   is returned.
17905
17906   If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17907   (e.g., "struct") that was used.  In that case bindings that do not
17908   refer to types are ignored.
17909
17910   If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17911   ignored.
17912
17913   If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17914   are ignored.
17915
17916   If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17917   types.
17918
17919   If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17920   TREE_LIST of candidates if name-lookup results in an ambiguity, and
17921   NULL_TREE otherwise.  */
17922
17923static tree
17924cp_parser_lookup_name (cp_parser *parser, tree name,
17925		       enum tag_types tag_type,
17926		       bool is_template,
17927		       bool is_namespace,
17928		       bool check_dependency,
17929		       tree *ambiguous_decls,
17930		       location_t name_location)
17931{
17932  int flags = 0;
17933  tree decl;
17934  tree object_type = parser->context->object_type;
17935
17936  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17937    flags |= LOOKUP_COMPLAIN;
17938
17939  /* Assume that the lookup will be unambiguous.  */
17940  if (ambiguous_decls)
17941    *ambiguous_decls = NULL_TREE;
17942
17943  /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17944     no longer valid.  Note that if we are parsing tentatively, and
17945     the parse fails, OBJECT_TYPE will be automatically restored.  */
17946  parser->context->object_type = NULL_TREE;
17947
17948  if (name == error_mark_node)
17949    return error_mark_node;
17950
17951  /* A template-id has already been resolved; there is no lookup to
17952     do.  */
17953  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17954    return name;
17955  if (BASELINK_P (name))
17956    {
17957      gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17958		  == TEMPLATE_ID_EXPR);
17959      return name;
17960    }
17961
17962  /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17963     it should already have been checked to make sure that the name
17964     used matches the type being destroyed.  */
17965  if (TREE_CODE (name) == BIT_NOT_EXPR)
17966    {
17967      tree type;
17968
17969      /* Figure out to which type this destructor applies.  */
17970      if (parser->scope)
17971	type = parser->scope;
17972      else if (object_type)
17973	type = object_type;
17974      else
17975	type = current_class_type;
17976      /* If that's not a class type, there is no destructor.  */
17977      if (!type || !CLASS_TYPE_P (type))
17978	return error_mark_node;
17979      if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17980	lazily_declare_fn (sfk_destructor, type);
17981      if (!CLASSTYPE_DESTRUCTORS (type))
17982	  return error_mark_node;
17983      /* If it was a class type, return the destructor.  */
17984      return CLASSTYPE_DESTRUCTORS (type);
17985    }
17986
17987  /* By this point, the NAME should be an ordinary identifier.  If
17988     the id-expression was a qualified name, the qualifying scope is
17989     stored in PARSER->SCOPE at this point.  */
17990  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17991
17992  /* Perform the lookup.  */
17993  if (parser->scope)
17994    {
17995      bool dependent_p;
17996
17997      if (parser->scope == error_mark_node)
17998	return error_mark_node;
17999
18000      /* If the SCOPE is dependent, the lookup must be deferred until
18001	 the template is instantiated -- unless we are explicitly
18002	 looking up names in uninstantiated templates.  Even then, we
18003	 cannot look up the name if the scope is not a class type; it
18004	 might, for example, be a template type parameter.  */
18005      dependent_p = (TYPE_P (parser->scope)
18006		     && dependent_scope_p (parser->scope));
18007      if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18008	  && dependent_p)
18009	/* Defer lookup.  */
18010	decl = error_mark_node;
18011      else
18012	{
18013	  tree pushed_scope = NULL_TREE;
18014
18015	  /* If PARSER->SCOPE is a dependent type, then it must be a
18016	     class type, and we must not be checking dependencies;
18017	     otherwise, we would have processed this lookup above.  So
18018	     that PARSER->SCOPE is not considered a dependent base by
18019	     lookup_member, we must enter the scope here.  */
18020	  if (dependent_p)
18021	    pushed_scope = push_scope (parser->scope);
18022
18023	  /* If the PARSER->SCOPE is a template specialization, it
18024	     may be instantiated during name lookup.  In that case,
18025	     errors may be issued.  Even if we rollback the current
18026	     tentative parse, those errors are valid.  */
18027	  decl = lookup_qualified_name (parser->scope, name,
18028					tag_type != none_type,
18029					/*complain=*/true);
18030
18031	  /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18032	     lookup result and the nested-name-specifier nominates a class C:
18033	       * if the name specified after the nested-name-specifier, when
18034	       looked up in C, is the injected-class-name of C (Clause 9), or
18035	       * if the name specified after the nested-name-specifier is the
18036	       same as the identifier or the simple-template-id's template-
18037	       name in the last component of the nested-name-specifier,
18038	     the name is instead considered to name the constructor of
18039	     class C. [ Note: for example, the constructor is not an
18040	     acceptable lookup result in an elaborated-type-specifier so
18041	     the constructor would not be used in place of the
18042	     injected-class-name. --end note ] Such a constructor name
18043	     shall be used only in the declarator-id of a declaration that
18044	     names a constructor or in a using-declaration.  */
18045	  if (tag_type == none_type
18046	      && DECL_SELF_REFERENCE_P (decl)
18047	      && same_type_p (DECL_CONTEXT (decl), parser->scope))
18048	    decl = lookup_qualified_name (parser->scope, ctor_identifier,
18049					  tag_type != none_type,
18050					  /*complain=*/true);
18051
18052	  /* If we have a single function from a using decl, pull it out.  */
18053	  if (TREE_CODE (decl) == OVERLOAD
18054	      && !really_overloaded_fn (decl))
18055	    decl = OVL_FUNCTION (decl);
18056
18057	  if (pushed_scope)
18058	    pop_scope (pushed_scope);
18059	}
18060
18061      /* If the scope is a dependent type and either we deferred lookup or
18062	 we did lookup but didn't find the name, rememeber the name.  */
18063      if (decl == error_mark_node && TYPE_P (parser->scope)
18064	  && dependent_type_p (parser->scope))
18065	{
18066	  if (tag_type)
18067	    {
18068	      tree type;
18069
18070	      /* The resolution to Core Issue 180 says that `struct
18071		 A::B' should be considered a type-name, even if `A'
18072		 is dependent.  */
18073	      type = make_typename_type (parser->scope, name, tag_type,
18074					 /*complain=*/tf_error);
18075	      decl = TYPE_NAME (type);
18076	    }
18077	  else if (is_template
18078		   && (cp_parser_next_token_ends_template_argument_p (parser)
18079		       || cp_lexer_next_token_is (parser->lexer,
18080						  CPP_CLOSE_PAREN)))
18081	    decl = make_unbound_class_template (parser->scope,
18082						name, NULL_TREE,
18083						/*complain=*/tf_error);
18084	  else
18085	    decl = build_qualified_name (/*type=*/NULL_TREE,
18086					 parser->scope, name,
18087					 is_template);
18088	}
18089      parser->qualifying_scope = parser->scope;
18090      parser->object_scope = NULL_TREE;
18091    }
18092  else if (object_type)
18093    {
18094      tree object_decl = NULL_TREE;
18095      /* Look up the name in the scope of the OBJECT_TYPE, unless the
18096	 OBJECT_TYPE is not a class.  */
18097      if (CLASS_TYPE_P (object_type))
18098	/* If the OBJECT_TYPE is a template specialization, it may
18099	   be instantiated during name lookup.  In that case, errors
18100	   may be issued.  Even if we rollback the current tentative
18101	   parse, those errors are valid.  */
18102	object_decl = lookup_member (object_type,
18103				     name,
18104				     /*protect=*/0,
18105				     tag_type != none_type);
18106      /* Look it up in the enclosing context, too.  */
18107      decl = lookup_name_real (name, tag_type != none_type,
18108			       /*nonclass=*/0,
18109			       /*block_p=*/true, is_namespace, flags);
18110      parser->object_scope = object_type;
18111      parser->qualifying_scope = NULL_TREE;
18112      if (object_decl)
18113	decl = object_decl;
18114    }
18115  else
18116    {
18117      decl = lookup_name_real (name, tag_type != none_type,
18118			       /*nonclass=*/0,
18119			       /*block_p=*/true, is_namespace, flags);
18120      parser->qualifying_scope = NULL_TREE;
18121      parser->object_scope = NULL_TREE;
18122    }
18123
18124  /* If the lookup failed, let our caller know.  */
18125  if (!decl || decl == error_mark_node)
18126    return error_mark_node;
18127
18128  /* Pull out the template from an injected-class-name (or multiple).  */
18129  if (is_template)
18130    decl = maybe_get_template_decl_from_type_decl (decl);
18131
18132  /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18133  if (TREE_CODE (decl) == TREE_LIST)
18134    {
18135      if (ambiguous_decls)
18136	*ambiguous_decls = decl;
18137      /* The error message we have to print is too complicated for
18138	 cp_parser_error, so we incorporate its actions directly.  */
18139      if (!cp_parser_simulate_error (parser))
18140	{
18141	  error_at (name_location, "reference to %qD is ambiguous",
18142		    name);
18143	  print_candidates (decl);
18144	}
18145      return error_mark_node;
18146    }
18147
18148  gcc_assert (DECL_P (decl)
18149	      || TREE_CODE (decl) == OVERLOAD
18150	      || TREE_CODE (decl) == SCOPE_REF
18151	      || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18152	      || BASELINK_P (decl));
18153
18154  /* If we have resolved the name of a member declaration, check to
18155     see if the declaration is accessible.  When the name resolves to
18156     set of overloaded functions, accessibility is checked when
18157     overload resolution is done.
18158
18159     During an explicit instantiation, access is not checked at all,
18160     as per [temp.explicit].  */
18161  if (DECL_P (decl))
18162    check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18163
18164  return decl;
18165}
18166
18167/* Like cp_parser_lookup_name, but for use in the typical case where
18168   CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18169   IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18170
18171static tree
18172cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18173{
18174  return cp_parser_lookup_name (parser, name,
18175				none_type,
18176				/*is_template=*/false,
18177				/*is_namespace=*/false,
18178				/*check_dependency=*/true,
18179				/*ambiguous_decls=*/NULL,
18180				location);
18181}
18182
18183/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18184   the current context, return the TYPE_DECL.  If TAG_NAME_P is
18185   true, the DECL indicates the class being defined in a class-head,
18186   or declared in an elaborated-type-specifier.
18187
18188   Otherwise, return DECL.  */
18189
18190static tree
18191cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18192{
18193  /* If the TEMPLATE_DECL is being declared as part of a class-head,
18194     the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18195
18196       struct A {
18197	 template <typename T> struct B;
18198       };
18199
18200       template <typename T> struct A::B {};
18201
18202     Similarly, in an elaborated-type-specifier:
18203
18204       namespace N { struct X{}; }
18205
18206       struct A {
18207	 template <typename T> friend struct N::X;
18208       };
18209
18210     However, if the DECL refers to a class type, and we are in
18211     the scope of the class, then the name lookup automatically
18212     finds the TYPE_DECL created by build_self_reference rather
18213     than a TEMPLATE_DECL.  For example, in:
18214
18215       template <class T> struct S {
18216	 S s;
18217       };
18218
18219     there is no need to handle such case.  */
18220
18221  if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18222    return DECL_TEMPLATE_RESULT (decl);
18223
18224  return decl;
18225}
18226
18227/* If too many, or too few, template-parameter lists apply to the
18228   declarator, issue an error message.  Returns TRUE if all went well,
18229   and FALSE otherwise.  */
18230
18231static bool
18232cp_parser_check_declarator_template_parameters (cp_parser* parser,
18233						cp_declarator *declarator,
18234						location_t declarator_location)
18235{
18236  unsigned num_templates;
18237
18238  /* We haven't seen any classes that involve template parameters yet.  */
18239  num_templates = 0;
18240
18241  switch (declarator->kind)
18242    {
18243    case cdk_id:
18244      if (declarator->u.id.qualifying_scope)
18245	{
18246	  tree scope;
18247
18248	  scope = declarator->u.id.qualifying_scope;
18249
18250	  while (scope && CLASS_TYPE_P (scope))
18251	    {
18252	      /* You're supposed to have one `template <...>'
18253		 for every template class, but you don't need one
18254		 for a full specialization.  For example:
18255
18256		 template <class T> struct S{};
18257		 template <> struct S<int> { void f(); };
18258		 void S<int>::f () {}
18259
18260		 is correct; there shouldn't be a `template <>' for
18261		 the definition of `S<int>::f'.  */
18262	      if (!CLASSTYPE_TEMPLATE_INFO (scope))
18263		/* If SCOPE does not have template information of any
18264		   kind, then it is not a template, nor is it nested
18265		   within a template.  */
18266		break;
18267	      if (explicit_class_specialization_p (scope))
18268		break;
18269	      if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18270		++num_templates;
18271
18272	      scope = TYPE_CONTEXT (scope);
18273	    }
18274	}
18275      else if (TREE_CODE (declarator->u.id.unqualified_name)
18276	       == TEMPLATE_ID_EXPR)
18277	/* If the DECLARATOR has the form `X<y>' then it uses one
18278	   additional level of template parameters.  */
18279	++num_templates;
18280
18281      return cp_parser_check_template_parameters
18282	(parser, num_templates, declarator_location, declarator);
18283
18284
18285    case cdk_function:
18286    case cdk_array:
18287    case cdk_pointer:
18288    case cdk_reference:
18289    case cdk_ptrmem:
18290      return (cp_parser_check_declarator_template_parameters
18291	      (parser, declarator->declarator, declarator_location));
18292
18293    case cdk_error:
18294      return true;
18295
18296    default:
18297      gcc_unreachable ();
18298    }
18299  return false;
18300}
18301
18302/* NUM_TEMPLATES were used in the current declaration.  If that is
18303   invalid, return FALSE and issue an error messages.  Otherwise,
18304   return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18305   declarator and we can print more accurate diagnostics.  */
18306
18307static bool
18308cp_parser_check_template_parameters (cp_parser* parser,
18309				     unsigned num_templates,
18310				     location_t location,
18311				     cp_declarator *declarator)
18312{
18313  /* If there are the same number of template classes and parameter
18314     lists, that's OK.  */
18315  if (parser->num_template_parameter_lists == num_templates)
18316    return true;
18317  /* If there are more, but only one more, then we are referring to a
18318     member template.  That's OK too.  */
18319  if (parser->num_template_parameter_lists == num_templates + 1)
18320    return true;
18321  /* If there are more template classes than parameter lists, we have
18322     something like:
18323
18324       template <class T> void S<T>::R<T>::f ();  */
18325  if (parser->num_template_parameter_lists < num_templates)
18326    {
18327      if (declarator && !current_function_decl)
18328	error_at (location, "specializing member %<%T::%E%> "
18329		  "requires %<template<>%> syntax",
18330		  declarator->u.id.qualifying_scope,
18331		  declarator->u.id.unqualified_name);
18332      else if (declarator)
18333	error_at (location, "invalid declaration of %<%T::%E%>",
18334		  declarator->u.id.qualifying_scope,
18335		  declarator->u.id.unqualified_name);
18336      else
18337	error_at (location, "too few template-parameter-lists");
18338      return false;
18339    }
18340  /* Otherwise, there are too many template parameter lists.  We have
18341     something like:
18342
18343     template <class T> template <class U> void S::f();  */
18344  error_at (location, "too many template-parameter-lists");
18345  return false;
18346}
18347
18348/* Parse an optional `::' token indicating that the following name is
18349   from the global namespace.  If so, PARSER->SCOPE is set to the
18350   GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18351   unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18352   Returns the new value of PARSER->SCOPE, if the `::' token is
18353   present, and NULL_TREE otherwise.  */
18354
18355static tree
18356cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18357{
18358  cp_token *token;
18359
18360  /* Peek at the next token.  */
18361  token = cp_lexer_peek_token (parser->lexer);
18362  /* If we're looking at a `::' token then we're starting from the
18363     global namespace, not our current location.  */
18364  if (token->type == CPP_SCOPE)
18365    {
18366      /* Consume the `::' token.  */
18367      cp_lexer_consume_token (parser->lexer);
18368      /* Set the SCOPE so that we know where to start the lookup.  */
18369      parser->scope = global_namespace;
18370      parser->qualifying_scope = global_namespace;
18371      parser->object_scope = NULL_TREE;
18372
18373      return parser->scope;
18374    }
18375  else if (!current_scope_valid_p)
18376    {
18377      parser->scope = NULL_TREE;
18378      parser->qualifying_scope = NULL_TREE;
18379      parser->object_scope = NULL_TREE;
18380    }
18381
18382  return NULL_TREE;
18383}
18384
18385/* Returns TRUE if the upcoming token sequence is the start of a
18386   constructor declarator.  If FRIEND_P is true, the declarator is
18387   preceded by the `friend' specifier.  */
18388
18389static bool
18390cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18391{
18392  bool constructor_p;
18393  tree nested_name_specifier;
18394  cp_token *next_token;
18395
18396  /* The common case is that this is not a constructor declarator, so
18397     try to avoid doing lots of work if at all possible.  It's not
18398     valid declare a constructor at function scope.  */
18399  if (parser->in_function_body)
18400    return false;
18401  /* And only certain tokens can begin a constructor declarator.  */
18402  next_token = cp_lexer_peek_token (parser->lexer);
18403  if (next_token->type != CPP_NAME
18404      && next_token->type != CPP_SCOPE
18405      && next_token->type != CPP_NESTED_NAME_SPECIFIER
18406      && next_token->type != CPP_TEMPLATE_ID)
18407    return false;
18408
18409  /* Parse tentatively; we are going to roll back all of the tokens
18410     consumed here.  */
18411  cp_parser_parse_tentatively (parser);
18412  /* Assume that we are looking at a constructor declarator.  */
18413  constructor_p = true;
18414
18415  /* Look for the optional `::' operator.  */
18416  cp_parser_global_scope_opt (parser,
18417			      /*current_scope_valid_p=*/false);
18418  /* Look for the nested-name-specifier.  */
18419  nested_name_specifier
18420    = (cp_parser_nested_name_specifier_opt (parser,
18421					    /*typename_keyword_p=*/false,
18422					    /*check_dependency_p=*/false,
18423					    /*type_p=*/false,
18424					    /*is_declaration=*/false));
18425  /* Outside of a class-specifier, there must be a
18426     nested-name-specifier.  */
18427  if (!nested_name_specifier &&
18428      (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18429       || friend_p))
18430    constructor_p = false;
18431  else if (nested_name_specifier == error_mark_node)
18432    constructor_p = false;
18433
18434  /* If we have a class scope, this is easy; DR 147 says that S::S always
18435     names the constructor, and no other qualified name could.  */
18436  if (constructor_p && nested_name_specifier
18437      && TYPE_P (nested_name_specifier))
18438    {
18439      tree id = cp_parser_unqualified_id (parser,
18440					  /*template_keyword_p=*/false,
18441					  /*check_dependency_p=*/false,
18442					  /*declarator_p=*/true,
18443					  /*optional_p=*/false);
18444      if (is_overloaded_fn (id))
18445	id = DECL_NAME (get_first_fn (id));
18446      if (!constructor_name_p (id, nested_name_specifier))
18447	constructor_p = false;
18448    }
18449  /* If we still think that this might be a constructor-declarator,
18450     look for a class-name.  */
18451  else if (constructor_p)
18452    {
18453      /* If we have:
18454
18455	   template <typename T> struct S {
18456	     S();
18457	   };
18458
18459	 we must recognize that the nested `S' names a class.  */
18460      tree type_decl;
18461      type_decl = cp_parser_class_name (parser,
18462					/*typename_keyword_p=*/false,
18463					/*template_keyword_p=*/false,
18464					none_type,
18465					/*check_dependency_p=*/false,
18466					/*class_head_p=*/false,
18467					/*is_declaration=*/false);
18468      /* If there was no class-name, then this is not a constructor.  */
18469      constructor_p = !cp_parser_error_occurred (parser);
18470
18471      /* If we're still considering a constructor, we have to see a `(',
18472	 to begin the parameter-declaration-clause, followed by either a
18473	 `)', an `...', or a decl-specifier.  We need to check for a
18474	 type-specifier to avoid being fooled into thinking that:
18475
18476	   S (f) (int);
18477
18478	 is a constructor.  (It is actually a function named `f' that
18479	 takes one parameter (of type `int') and returns a value of type
18480	 `S'.  */
18481      if (constructor_p
18482	  && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18483	constructor_p = false;
18484
18485      if (constructor_p
18486	  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18487	  && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18488	  /* A parameter declaration begins with a decl-specifier,
18489	     which is either the "attribute" keyword, a storage class
18490	     specifier, or (usually) a type-specifier.  */
18491	  && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18492	{
18493	  tree type;
18494	  tree pushed_scope = NULL_TREE;
18495	  unsigned saved_num_template_parameter_lists;
18496
18497	  /* Names appearing in the type-specifier should be looked up
18498	     in the scope of the class.  */
18499	  if (current_class_type)
18500	    type = NULL_TREE;
18501	  else
18502	    {
18503	      type = TREE_TYPE (type_decl);
18504	      if (TREE_CODE (type) == TYPENAME_TYPE)
18505		{
18506		  type = resolve_typename_type (type,
18507						/*only_current_p=*/false);
18508		  if (TREE_CODE (type) == TYPENAME_TYPE)
18509		    {
18510		      cp_parser_abort_tentative_parse (parser);
18511		      return false;
18512		    }
18513		}
18514	      pushed_scope = push_scope (type);
18515	    }
18516
18517	  /* Inside the constructor parameter list, surrounding
18518	     template-parameter-lists do not apply.  */
18519	  saved_num_template_parameter_lists
18520	    = parser->num_template_parameter_lists;
18521	  parser->num_template_parameter_lists = 0;
18522
18523	  /* Look for the type-specifier.  */
18524	  cp_parser_type_specifier (parser,
18525				    CP_PARSER_FLAGS_NONE,
18526				    /*decl_specs=*/NULL,
18527				    /*is_declarator=*/true,
18528				    /*declares_class_or_enum=*/NULL,
18529				    /*is_cv_qualifier=*/NULL);
18530
18531	  parser->num_template_parameter_lists
18532	    = saved_num_template_parameter_lists;
18533
18534	  /* Leave the scope of the class.  */
18535	  if (pushed_scope)
18536	    pop_scope (pushed_scope);
18537
18538	  constructor_p = !cp_parser_error_occurred (parser);
18539	}
18540    }
18541
18542  /* We did not really want to consume any tokens.  */
18543  cp_parser_abort_tentative_parse (parser);
18544
18545  return constructor_p;
18546}
18547
18548/* Parse the definition of the function given by the DECL_SPECIFIERS,
18549   ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18550   they must be performed once we are in the scope of the function.
18551
18552   Returns the function defined.  */
18553
18554static tree
18555cp_parser_function_definition_from_specifiers_and_declarator
18556  (cp_parser* parser,
18557   cp_decl_specifier_seq *decl_specifiers,
18558   tree attributes,
18559   const cp_declarator *declarator)
18560{
18561  tree fn;
18562  bool success_p;
18563
18564  /* Begin the function-definition.  */
18565  success_p = start_function (decl_specifiers, declarator, attributes);
18566
18567  /* The things we're about to see are not directly qualified by any
18568     template headers we've seen thus far.  */
18569  reset_specialization ();
18570
18571  /* If there were names looked up in the decl-specifier-seq that we
18572     did not check, check them now.  We must wait until we are in the
18573     scope of the function to perform the checks, since the function
18574     might be a friend.  */
18575  perform_deferred_access_checks ();
18576
18577  if (!success_p)
18578    {
18579      /* Skip the entire function.  */
18580      cp_parser_skip_to_end_of_block_or_statement (parser);
18581      fn = error_mark_node;
18582    }
18583  else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18584    {
18585      /* Seen already, skip it.  An error message has already been output.  */
18586      cp_parser_skip_to_end_of_block_or_statement (parser);
18587      fn = current_function_decl;
18588      current_function_decl = NULL_TREE;
18589      /* If this is a function from a class, pop the nested class.  */
18590      if (current_class_name)
18591	pop_nested_class ();
18592    }
18593  else
18594    fn = cp_parser_function_definition_after_declarator (parser,
18595							 /*inline_p=*/false);
18596
18597  return fn;
18598}
18599
18600/* Parse the part of a function-definition that follows the
18601   declarator.  INLINE_P is TRUE iff this function is an inline
18602   function defined within a class-specifier.
18603
18604   Returns the function defined.  */
18605
18606static tree
18607cp_parser_function_definition_after_declarator (cp_parser* parser,
18608						bool inline_p)
18609{
18610  tree fn;
18611  bool ctor_initializer_p = false;
18612  bool saved_in_unbraced_linkage_specification_p;
18613  bool saved_in_function_body;
18614  unsigned saved_num_template_parameter_lists;
18615  cp_token *token;
18616
18617  saved_in_function_body = parser->in_function_body;
18618  parser->in_function_body = true;
18619  /* If the next token is `return', then the code may be trying to
18620     make use of the "named return value" extension that G++ used to
18621     support.  */
18622  token = cp_lexer_peek_token (parser->lexer);
18623  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18624    {
18625      /* Consume the `return' keyword.  */
18626      cp_lexer_consume_token (parser->lexer);
18627      /* Look for the identifier that indicates what value is to be
18628	 returned.  */
18629      cp_parser_identifier (parser);
18630      /* Issue an error message.  */
18631      error_at (token->location,
18632		"named return values are no longer supported");
18633      /* Skip tokens until we reach the start of the function body.  */
18634      while (true)
18635	{
18636	  cp_token *token = cp_lexer_peek_token (parser->lexer);
18637	  if (token->type == CPP_OPEN_BRACE
18638	      || token->type == CPP_EOF
18639	      || token->type == CPP_PRAGMA_EOL)
18640	    break;
18641	  cp_lexer_consume_token (parser->lexer);
18642	}
18643    }
18644  /* The `extern' in `extern "C" void f () { ... }' does not apply to
18645     anything declared inside `f'.  */
18646  saved_in_unbraced_linkage_specification_p
18647    = parser->in_unbraced_linkage_specification_p;
18648  parser->in_unbraced_linkage_specification_p = false;
18649  /* Inside the function, surrounding template-parameter-lists do not
18650     apply.  */
18651  saved_num_template_parameter_lists
18652    = parser->num_template_parameter_lists;
18653  parser->num_template_parameter_lists = 0;
18654
18655  start_lambda_scope (current_function_decl);
18656
18657  /* If the next token is `try', then we are looking at a
18658     function-try-block.  */
18659  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18660    ctor_initializer_p = cp_parser_function_try_block (parser);
18661  /* A function-try-block includes the function-body, so we only do
18662     this next part if we're not processing a function-try-block.  */
18663  else
18664    ctor_initializer_p
18665      = cp_parser_ctor_initializer_opt_and_function_body (parser);
18666
18667  finish_lambda_scope ();
18668
18669  /* Finish the function.  */
18670  fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18671			(inline_p ? 2 : 0));
18672  /* Generate code for it, if necessary.  */
18673  expand_or_defer_fn (fn);
18674  /* Restore the saved values.  */
18675  parser->in_unbraced_linkage_specification_p
18676    = saved_in_unbraced_linkage_specification_p;
18677  parser->num_template_parameter_lists
18678    = saved_num_template_parameter_lists;
18679  parser->in_function_body = saved_in_function_body;
18680
18681  return fn;
18682}
18683
18684/* Parse a template-declaration, assuming that the `export' (and
18685   `extern') keywords, if present, has already been scanned.  MEMBER_P
18686   is as for cp_parser_template_declaration.  */
18687
18688static void
18689cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18690{
18691  tree decl = NULL_TREE;
18692  VEC (deferred_access_check,gc) *checks;
18693  tree parameter_list;
18694  bool friend_p = false;
18695  bool need_lang_pop;
18696  cp_token *token;
18697
18698  /* Look for the `template' keyword.  */
18699  token = cp_lexer_peek_token (parser->lexer);
18700  if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18701    return;
18702
18703  /* And the `<'.  */
18704  if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18705    return;
18706  if (at_class_scope_p () && current_function_decl)
18707    {
18708      /* 14.5.2.2 [temp.mem]
18709
18710         A local class shall not have member templates.  */
18711      error_at (token->location,
18712		"invalid declaration of member template in local class");
18713      cp_parser_skip_to_end_of_block_or_statement (parser);
18714      return;
18715    }
18716  /* [temp]
18717
18718     A template ... shall not have C linkage.  */
18719  if (current_lang_name == lang_name_c)
18720    {
18721      error_at (token->location, "template with C linkage");
18722      /* Give it C++ linkage to avoid confusing other parts of the
18723	 front end.  */
18724      push_lang_context (lang_name_cplusplus);
18725      need_lang_pop = true;
18726    }
18727  else
18728    need_lang_pop = false;
18729
18730  /* We cannot perform access checks on the template parameter
18731     declarations until we know what is being declared, just as we
18732     cannot check the decl-specifier list.  */
18733  push_deferring_access_checks (dk_deferred);
18734
18735  /* If the next token is `>', then we have an invalid
18736     specialization.  Rather than complain about an invalid template
18737     parameter, issue an error message here.  */
18738  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18739    {
18740      cp_parser_error (parser, "invalid explicit specialization");
18741      begin_specialization ();
18742      parameter_list = NULL_TREE;
18743    }
18744  else
18745    /* Parse the template parameters.  */
18746    parameter_list = cp_parser_template_parameter_list (parser);
18747
18748  /* Get the deferred access checks from the parameter list.  These
18749     will be checked once we know what is being declared, as for a
18750     member template the checks must be performed in the scope of the
18751     class containing the member.  */
18752  checks = get_deferred_access_checks ();
18753
18754  /* Look for the `>'.  */
18755  cp_parser_skip_to_end_of_template_parameter_list (parser);
18756  /* We just processed one more parameter list.  */
18757  ++parser->num_template_parameter_lists;
18758  /* If the next token is `template', there are more template
18759     parameters.  */
18760  if (cp_lexer_next_token_is_keyword (parser->lexer,
18761				      RID_TEMPLATE))
18762    cp_parser_template_declaration_after_export (parser, member_p);
18763  else
18764    {
18765      /* There are no access checks when parsing a template, as we do not
18766	 know if a specialization will be a friend.  */
18767      push_deferring_access_checks (dk_no_check);
18768      token = cp_lexer_peek_token (parser->lexer);
18769      decl = cp_parser_single_declaration (parser,
18770					   checks,
18771					   member_p,
18772                                           /*explicit_specialization_p=*/false,
18773					   &friend_p);
18774      pop_deferring_access_checks ();
18775
18776      /* If this is a member template declaration, let the front
18777	 end know.  */
18778      if (member_p && !friend_p && decl)
18779	{
18780	  if (TREE_CODE (decl) == TYPE_DECL)
18781	    cp_parser_check_access_in_redeclaration (decl, token->location);
18782
18783	  decl = finish_member_template_decl (decl);
18784	}
18785      else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18786	make_friend_class (current_class_type, TREE_TYPE (decl),
18787			   /*complain=*/true);
18788    }
18789  /* We are done with the current parameter list.  */
18790  --parser->num_template_parameter_lists;
18791
18792  pop_deferring_access_checks ();
18793
18794  /* Finish up.  */
18795  finish_template_decl (parameter_list);
18796
18797  /* Register member declarations.  */
18798  if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18799    finish_member_declaration (decl);
18800  /* For the erroneous case of a template with C linkage, we pushed an
18801     implicit C++ linkage scope; exit that scope now.  */
18802  if (need_lang_pop)
18803    pop_lang_context ();
18804  /* If DECL is a function template, we must return to parse it later.
18805     (Even though there is no definition, there might be default
18806     arguments that need handling.)  */
18807  if (member_p && decl
18808      && (TREE_CODE (decl) == FUNCTION_DECL
18809	  || DECL_FUNCTION_TEMPLATE_P (decl)))
18810    TREE_VALUE (parser->unparsed_functions_queues)
18811      = tree_cons (NULL_TREE, decl,
18812		   TREE_VALUE (parser->unparsed_functions_queues));
18813}
18814
18815/* Perform the deferred access checks from a template-parameter-list.
18816   CHECKS is a TREE_LIST of access checks, as returned by
18817   get_deferred_access_checks.  */
18818
18819static void
18820cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18821{
18822  ++processing_template_parmlist;
18823  perform_access_checks (checks);
18824  --processing_template_parmlist;
18825}
18826
18827/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18828   `function-definition' sequence.  MEMBER_P is true, this declaration
18829   appears in a class scope.
18830
18831   Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18832   *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18833
18834static tree
18835cp_parser_single_declaration (cp_parser* parser,
18836			      VEC (deferred_access_check,gc)* checks,
18837			      bool member_p,
18838                              bool explicit_specialization_p,
18839			      bool* friend_p)
18840{
18841  int declares_class_or_enum;
18842  tree decl = NULL_TREE;
18843  cp_decl_specifier_seq decl_specifiers;
18844  bool function_definition_p = false;
18845  cp_token *decl_spec_token_start;
18846
18847  /* This function is only used when processing a template
18848     declaration.  */
18849  gcc_assert (innermost_scope_kind () == sk_template_parms
18850	      || innermost_scope_kind () == sk_template_spec);
18851
18852  /* Defer access checks until we know what is being declared.  */
18853  push_deferring_access_checks (dk_deferred);
18854
18855  /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18856     alternative.  */
18857  decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18858  cp_parser_decl_specifier_seq (parser,
18859				CP_PARSER_FLAGS_OPTIONAL,
18860				&decl_specifiers,
18861				&declares_class_or_enum);
18862  if (friend_p)
18863    *friend_p = cp_parser_friend_p (&decl_specifiers);
18864
18865  /* There are no template typedefs.  */
18866  if (decl_specifiers.specs[(int) ds_typedef])
18867    {
18868      error_at (decl_spec_token_start->location,
18869		"template declaration of %<typedef%>");
18870      decl = error_mark_node;
18871    }
18872
18873  /* Gather up the access checks that occurred the
18874     decl-specifier-seq.  */
18875  stop_deferring_access_checks ();
18876
18877  /* Check for the declaration of a template class.  */
18878  if (declares_class_or_enum)
18879    {
18880      if (cp_parser_declares_only_class_p (parser))
18881	{
18882	  decl = shadow_tag (&decl_specifiers);
18883
18884	  /* In this case:
18885
18886	       struct C {
18887		 friend template <typename T> struct A<T>::B;
18888	       };
18889
18890	     A<T>::B will be represented by a TYPENAME_TYPE, and
18891	     therefore not recognized by shadow_tag.  */
18892	  if (friend_p && *friend_p
18893	      && !decl
18894	      && decl_specifiers.type
18895	      && TYPE_P (decl_specifiers.type))
18896	    decl = decl_specifiers.type;
18897
18898	  if (decl && decl != error_mark_node)
18899	    decl = TYPE_NAME (decl);
18900	  else
18901	    decl = error_mark_node;
18902
18903	  /* Perform access checks for template parameters.  */
18904	  cp_parser_perform_template_parameter_access_checks (checks);
18905	}
18906    }
18907
18908  /* Complain about missing 'typename' or other invalid type names.  */
18909  if (!decl_specifiers.any_type_specifiers_p)
18910    cp_parser_parse_and_diagnose_invalid_type_name (parser);
18911
18912  /* If it's not a template class, try for a template function.  If
18913     the next token is a `;', then this declaration does not declare
18914     anything.  But, if there were errors in the decl-specifiers, then
18915     the error might well have come from an attempted class-specifier.
18916     In that case, there's no need to warn about a missing declarator.  */
18917  if (!decl
18918      && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18919	  || decl_specifiers.type != error_mark_node))
18920    {
18921      decl = cp_parser_init_declarator (parser,
18922				        &decl_specifiers,
18923				        checks,
18924				        /*function_definition_allowed_p=*/true,
18925				        member_p,
18926				        declares_class_or_enum,
18927				        &function_definition_p);
18928
18929    /* 7.1.1-1 [dcl.stc]
18930
18931       A storage-class-specifier shall not be specified in an explicit
18932       specialization...  */
18933    if (decl
18934        && explicit_specialization_p
18935        && decl_specifiers.storage_class != sc_none)
18936      {
18937        error_at (decl_spec_token_start->location,
18938		  "explicit template specialization cannot have a storage class");
18939        decl = error_mark_node;
18940      }
18941    }
18942
18943  pop_deferring_access_checks ();
18944
18945  /* Clear any current qualification; whatever comes next is the start
18946     of something new.  */
18947  parser->scope = NULL_TREE;
18948  parser->qualifying_scope = NULL_TREE;
18949  parser->object_scope = NULL_TREE;
18950  /* Look for a trailing `;' after the declaration.  */
18951  if (!function_definition_p
18952      && (decl == error_mark_node
18953	  || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18954    cp_parser_skip_to_end_of_block_or_statement (parser);
18955
18956  return decl;
18957}
18958
18959/* Parse a cast-expression that is not the operand of a unary "&".  */
18960
18961static tree
18962cp_parser_simple_cast_expression (cp_parser *parser)
18963{
18964  return cp_parser_cast_expression (parser, /*address_p=*/false,
18965				    /*cast_p=*/false, NULL);
18966}
18967
18968/* Parse a functional cast to TYPE.  Returns an expression
18969   representing the cast.  */
18970
18971static tree
18972cp_parser_functional_cast (cp_parser* parser, tree type)
18973{
18974  VEC(tree,gc) *vec;
18975  tree expression_list;
18976  tree cast;
18977  bool nonconst_p;
18978
18979  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18980    {
18981      maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18982      expression_list = cp_parser_braced_list (parser, &nonconst_p);
18983      CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18984      if (TREE_CODE (type) == TYPE_DECL)
18985	type = TREE_TYPE (type);
18986      return finish_compound_literal (type, expression_list);
18987    }
18988
18989
18990  vec = cp_parser_parenthesized_expression_list (parser, false,
18991						 /*cast_p=*/true,
18992						 /*allow_expansion_p=*/true,
18993						 /*non_constant_p=*/NULL);
18994  if (vec == NULL)
18995    expression_list = error_mark_node;
18996  else
18997    {
18998      expression_list = build_tree_list_vec (vec);
18999      release_tree_vector (vec);
19000    }
19001
19002  cast = build_functional_cast (type, expression_list,
19003                                tf_warning_or_error);
19004  /* [expr.const]/1: In an integral constant expression "only type
19005     conversions to integral or enumeration type can be used".  */
19006  if (TREE_CODE (type) == TYPE_DECL)
19007    type = TREE_TYPE (type);
19008  if (cast != error_mark_node
19009      && !cast_valid_in_integral_constant_expression_p (type)
19010      && (cp_parser_non_integral_constant_expression
19011	  (parser, "a call to a constructor")))
19012    return error_mark_node;
19013  return cast;
19014}
19015
19016/* Save the tokens that make up the body of a member function defined
19017   in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
19018   already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
19019   specifiers applied to the declaration.  Returns the FUNCTION_DECL
19020   for the member function.  */
19021
19022static tree
19023cp_parser_save_member_function_body (cp_parser* parser,
19024				     cp_decl_specifier_seq *decl_specifiers,
19025				     cp_declarator *declarator,
19026				     tree attributes)
19027{
19028  cp_token *first;
19029  cp_token *last;
19030  tree fn;
19031
19032  /* Create the FUNCTION_DECL.  */
19033  fn = grokmethod (decl_specifiers, declarator, attributes);
19034  /* If something went badly wrong, bail out now.  */
19035  if (fn == error_mark_node)
19036    {
19037      /* If there's a function-body, skip it.  */
19038      if (cp_parser_token_starts_function_definition_p
19039	  (cp_lexer_peek_token (parser->lexer)))
19040	cp_parser_skip_to_end_of_block_or_statement (parser);
19041      return error_mark_node;
19042    }
19043
19044  /* Remember it, if there default args to post process.  */
19045  cp_parser_save_default_args (parser, fn);
19046
19047  /* Save away the tokens that make up the body of the
19048     function.  */
19049  first = parser->lexer->next_token;
19050  /* We can have braced-init-list mem-initializers before the fn body.  */
19051  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19052    {
19053      cp_lexer_consume_token (parser->lexer);
19054      while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19055	     && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19056	{
19057	  /* cache_group will stop after an un-nested { } pair, too.  */
19058	  if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19059	    break;
19060
19061	  /* variadic mem-inits have ... after the ')'.  */
19062	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19063	    cp_lexer_consume_token (parser->lexer);
19064	}
19065    }
19066  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19067  /* Handle function try blocks.  */
19068  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19069    cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19070  last = parser->lexer->next_token;
19071
19072  /* Save away the inline definition; we will process it when the
19073     class is complete.  */
19074  DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19075  DECL_PENDING_INLINE_P (fn) = 1;
19076
19077  /* We need to know that this was defined in the class, so that
19078     friend templates are handled correctly.  */
19079  DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19080
19081  /* Add FN to the queue of functions to be parsed later.  */
19082  TREE_VALUE (parser->unparsed_functions_queues)
19083    = tree_cons (NULL_TREE, fn,
19084		 TREE_VALUE (parser->unparsed_functions_queues));
19085
19086  return fn;
19087}
19088
19089/* Parse a template-argument-list, as well as the trailing ">" (but
19090   not the opening ">").  See cp_parser_template_argument_list for the
19091   return value.  */
19092
19093static tree
19094cp_parser_enclosed_template_argument_list (cp_parser* parser)
19095{
19096  tree arguments;
19097  tree saved_scope;
19098  tree saved_qualifying_scope;
19099  tree saved_object_scope;
19100  bool saved_greater_than_is_operator_p;
19101  int saved_unevaluated_operand;
19102  int saved_inhibit_evaluation_warnings;
19103
19104  /* [temp.names]
19105
19106     When parsing a template-id, the first non-nested `>' is taken as
19107     the end of the template-argument-list rather than a greater-than
19108     operator.  */
19109  saved_greater_than_is_operator_p
19110    = parser->greater_than_is_operator_p;
19111  parser->greater_than_is_operator_p = false;
19112  /* Parsing the argument list may modify SCOPE, so we save it
19113     here.  */
19114  saved_scope = parser->scope;
19115  saved_qualifying_scope = parser->qualifying_scope;
19116  saved_object_scope = parser->object_scope;
19117  /* We need to evaluate the template arguments, even though this
19118     template-id may be nested within a "sizeof".  */
19119  saved_unevaluated_operand = cp_unevaluated_operand;
19120  cp_unevaluated_operand = 0;
19121  saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19122  c_inhibit_evaluation_warnings = 0;
19123  /* Parse the template-argument-list itself.  */
19124  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19125      || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19126    arguments = NULL_TREE;
19127  else
19128    arguments = cp_parser_template_argument_list (parser);
19129  /* Look for the `>' that ends the template-argument-list. If we find
19130     a '>>' instead, it's probably just a typo.  */
19131  if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19132    {
19133      if (cxx_dialect != cxx98)
19134        {
19135          /* In C++0x, a `>>' in a template argument list or cast
19136             expression is considered to be two separate `>'
19137             tokens. So, change the current token to a `>', but don't
19138             consume it: it will be consumed later when the outer
19139             template argument list (or cast expression) is parsed.
19140             Note that this replacement of `>' for `>>' is necessary
19141             even if we are parsing tentatively: in the tentative
19142             case, after calling
19143             cp_parser_enclosed_template_argument_list we will always
19144             throw away all of the template arguments and the first
19145             closing `>', either because the template argument list
19146             was erroneous or because we are replacing those tokens
19147             with a CPP_TEMPLATE_ID token.  The second `>' (which will
19148             not have been thrown away) is needed either to close an
19149             outer template argument list or to complete a new-style
19150             cast.  */
19151	  cp_token *token = cp_lexer_peek_token (parser->lexer);
19152          token->type = CPP_GREATER;
19153        }
19154      else if (!saved_greater_than_is_operator_p)
19155	{
19156	  /* If we're in a nested template argument list, the '>>' has
19157	    to be a typo for '> >'. We emit the error message, but we
19158	    continue parsing and we push a '>' as next token, so that
19159	    the argument list will be parsed correctly.  Note that the
19160	    global source location is still on the token before the
19161	    '>>', so we need to say explicitly where we want it.  */
19162	  cp_token *token = cp_lexer_peek_token (parser->lexer);
19163	  error_at (token->location, "%<>>%> should be %<> >%> "
19164		    "within a nested template argument list");
19165
19166	  token->type = CPP_GREATER;
19167	}
19168      else
19169	{
19170	  /* If this is not a nested template argument list, the '>>'
19171	    is a typo for '>'. Emit an error message and continue.
19172	    Same deal about the token location, but here we can get it
19173	    right by consuming the '>>' before issuing the diagnostic.  */
19174	  cp_token *token = cp_lexer_consume_token (parser->lexer);
19175	  error_at (token->location,
19176		    "spurious %<>>%>, use %<>%> to terminate "
19177		    "a template argument list");
19178	}
19179    }
19180  else
19181    cp_parser_skip_to_end_of_template_parameter_list (parser);
19182  /* The `>' token might be a greater-than operator again now.  */
19183  parser->greater_than_is_operator_p
19184    = saved_greater_than_is_operator_p;
19185  /* Restore the SAVED_SCOPE.  */
19186  parser->scope = saved_scope;
19187  parser->qualifying_scope = saved_qualifying_scope;
19188  parser->object_scope = saved_object_scope;
19189  cp_unevaluated_operand = saved_unevaluated_operand;
19190  c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19191
19192  return arguments;
19193}
19194
19195/* MEMBER_FUNCTION is a member function, or a friend.  If default
19196   arguments, or the body of the function have not yet been parsed,
19197   parse them now.  */
19198
19199static void
19200cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19201{
19202  /* If this member is a template, get the underlying
19203     FUNCTION_DECL.  */
19204  if (DECL_FUNCTION_TEMPLATE_P (member_function))
19205    member_function = DECL_TEMPLATE_RESULT (member_function);
19206
19207  /* There should not be any class definitions in progress at this
19208     point; the bodies of members are only parsed outside of all class
19209     definitions.  */
19210  gcc_assert (parser->num_classes_being_defined == 0);
19211  /* While we're parsing the member functions we might encounter more
19212     classes.  We want to handle them right away, but we don't want
19213     them getting mixed up with functions that are currently in the
19214     queue.  */
19215  parser->unparsed_functions_queues
19216    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19217
19218  /* Make sure that any template parameters are in scope.  */
19219  maybe_begin_member_template_processing (member_function);
19220
19221  /* If the body of the function has not yet been parsed, parse it
19222     now.  */
19223  if (DECL_PENDING_INLINE_P (member_function))
19224    {
19225      tree function_scope;
19226      cp_token_cache *tokens;
19227
19228      /* The function is no longer pending; we are processing it.  */
19229      tokens = DECL_PENDING_INLINE_INFO (member_function);
19230      DECL_PENDING_INLINE_INFO (member_function) = NULL;
19231      DECL_PENDING_INLINE_P (member_function) = 0;
19232
19233      /* If this is a local class, enter the scope of the containing
19234	 function.  */
19235      function_scope = current_function_decl;
19236      if (function_scope)
19237	push_function_context ();
19238
19239      /* Push the body of the function onto the lexer stack.  */
19240      cp_parser_push_lexer_for_tokens (parser, tokens);
19241
19242      /* Let the front end know that we going to be defining this
19243	 function.  */
19244      start_preparsed_function (member_function, NULL_TREE,
19245				SF_PRE_PARSED | SF_INCLASS_INLINE);
19246
19247      /* Don't do access checking if it is a templated function.  */
19248      if (processing_template_decl)
19249	push_deferring_access_checks (dk_no_check);
19250
19251      /* Now, parse the body of the function.  */
19252      cp_parser_function_definition_after_declarator (parser,
19253						      /*inline_p=*/true);
19254
19255      if (processing_template_decl)
19256	pop_deferring_access_checks ();
19257
19258      /* Leave the scope of the containing function.  */
19259      if (function_scope)
19260	pop_function_context ();
19261      cp_parser_pop_lexer (parser);
19262    }
19263
19264  /* Remove any template parameters from the symbol table.  */
19265  maybe_end_member_template_processing ();
19266
19267  /* Restore the queue.  */
19268  parser->unparsed_functions_queues
19269    = TREE_CHAIN (parser->unparsed_functions_queues);
19270}
19271
19272/* If DECL contains any default args, remember it on the unparsed
19273   functions queue.  */
19274
19275static void
19276cp_parser_save_default_args (cp_parser* parser, tree decl)
19277{
19278  tree probe;
19279
19280  for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19281       probe;
19282       probe = TREE_CHAIN (probe))
19283    if (TREE_PURPOSE (probe))
19284      {
19285	TREE_PURPOSE (parser->unparsed_functions_queues)
19286	  = tree_cons (current_class_type, decl,
19287		       TREE_PURPOSE (parser->unparsed_functions_queues));
19288	break;
19289      }
19290}
19291
19292/* FN is a FUNCTION_DECL which may contains a parameter with an
19293   unparsed DEFAULT_ARG.  Parse the default args now.  This function
19294   assumes that the current scope is the scope in which the default
19295   argument should be processed.  */
19296
19297static void
19298cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19299{
19300  bool saved_local_variables_forbidden_p;
19301  tree parm, parmdecl;
19302
19303  /* While we're parsing the default args, we might (due to the
19304     statement expression extension) encounter more classes.  We want
19305     to handle them right away, but we don't want them getting mixed
19306     up with default args that are currently in the queue.  */
19307  parser->unparsed_functions_queues
19308    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19309
19310  /* Local variable names (and the `this' keyword) may not appear
19311     in a default argument.  */
19312  saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19313  parser->local_variables_forbidden_p = true;
19314
19315  for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19316	 parmdecl = DECL_ARGUMENTS (fn);
19317       parm && parm != void_list_node;
19318       parm = TREE_CHAIN (parm),
19319	 parmdecl = TREE_CHAIN (parmdecl))
19320    {
19321      cp_token_cache *tokens;
19322      tree default_arg = TREE_PURPOSE (parm);
19323      tree parsed_arg;
19324      VEC(tree,gc) *insts;
19325      tree copy;
19326      unsigned ix;
19327
19328      if (!default_arg)
19329	continue;
19330
19331      if (TREE_CODE (default_arg) != DEFAULT_ARG)
19332	/* This can happen for a friend declaration for a function
19333	   already declared with default arguments.  */
19334	continue;
19335
19336       /* Push the saved tokens for the default argument onto the parser's
19337	  lexer stack.  */
19338      tokens = DEFARG_TOKENS (default_arg);
19339      cp_parser_push_lexer_for_tokens (parser, tokens);
19340
19341      start_lambda_scope (parmdecl);
19342
19343      /* Parse the assignment-expression.  */
19344      parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19345      if (parsed_arg == error_mark_node)
19346	{
19347	  cp_parser_pop_lexer (parser);
19348	  continue;
19349	}
19350
19351      if (!processing_template_decl)
19352	parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19353
19354      TREE_PURPOSE (parm) = parsed_arg;
19355
19356      /* Update any instantiations we've already created.  */
19357      for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19358	   VEC_iterate (tree, insts, ix, copy); ix++)
19359	TREE_PURPOSE (copy) = parsed_arg;
19360
19361      finish_lambda_scope ();
19362
19363      /* If the token stream has not been completely used up, then
19364	 there was extra junk after the end of the default
19365	 argument.  */
19366      if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19367	cp_parser_error (parser, "expected %<,%>");
19368
19369      /* Revert to the main lexer.  */
19370      cp_parser_pop_lexer (parser);
19371    }
19372
19373  /* Make sure no default arg is missing.  */
19374  check_default_args (fn);
19375
19376  /* Restore the state of local_variables_forbidden_p.  */
19377  parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19378
19379  /* Restore the queue.  */
19380  parser->unparsed_functions_queues
19381    = TREE_CHAIN (parser->unparsed_functions_queues);
19382}
19383
19384/* Parse the operand of `sizeof' (or a similar operator).  Returns
19385   either a TYPE or an expression, depending on the form of the
19386   input.  The KEYWORD indicates which kind of expression we have
19387   encountered.  */
19388
19389static tree
19390cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19391{
19392  tree expr = NULL_TREE;
19393  const char *saved_message;
19394  char *tmp;
19395  bool saved_integral_constant_expression_p;
19396  bool saved_non_integral_constant_expression_p;
19397  bool pack_expansion_p = false;
19398
19399  /* Types cannot be defined in a `sizeof' expression.  Save away the
19400     old message.  */
19401  saved_message = parser->type_definition_forbidden_message;
19402  /* And create the new one.  */
19403  tmp = concat ("types may not be defined in %<",
19404		IDENTIFIER_POINTER (ridpointers[keyword]),
19405		"%> expressions", NULL);
19406  parser->type_definition_forbidden_message = tmp;
19407
19408  /* The restrictions on constant-expressions do not apply inside
19409     sizeof expressions.  */
19410  saved_integral_constant_expression_p
19411    = parser->integral_constant_expression_p;
19412  saved_non_integral_constant_expression_p
19413    = parser->non_integral_constant_expression_p;
19414  parser->integral_constant_expression_p = false;
19415
19416  /* If it's a `...', then we are computing the length of a parameter
19417     pack.  */
19418  if (keyword == RID_SIZEOF
19419      && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19420    {
19421      /* Consume the `...'.  */
19422      cp_lexer_consume_token (parser->lexer);
19423      maybe_warn_variadic_templates ();
19424
19425      /* Note that this is an expansion.  */
19426      pack_expansion_p = true;
19427    }
19428
19429  /* Do not actually evaluate the expression.  */
19430  ++cp_unevaluated_operand;
19431  ++c_inhibit_evaluation_warnings;
19432  /* If it's a `(', then we might be looking at the type-id
19433     construction.  */
19434  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19435    {
19436      tree type;
19437      bool saved_in_type_id_in_expr_p;
19438
19439      /* We can't be sure yet whether we're looking at a type-id or an
19440	 expression.  */
19441      cp_parser_parse_tentatively (parser);
19442      /* Consume the `('.  */
19443      cp_lexer_consume_token (parser->lexer);
19444      /* Parse the type-id.  */
19445      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19446      parser->in_type_id_in_expr_p = true;
19447      type = cp_parser_type_id (parser);
19448      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19449      /* Now, look for the trailing `)'.  */
19450      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19451      /* If all went well, then we're done.  */
19452      if (cp_parser_parse_definitely (parser))
19453	{
19454	  cp_decl_specifier_seq decl_specs;
19455
19456	  /* Build a trivial decl-specifier-seq.  */
19457	  clear_decl_specs (&decl_specs);
19458	  decl_specs.type = type;
19459
19460	  /* Call grokdeclarator to figure out what type this is.  */
19461	  expr = grokdeclarator (NULL,
19462				 &decl_specs,
19463				 TYPENAME,
19464				 /*initialized=*/0,
19465				 /*attrlist=*/NULL);
19466	}
19467    }
19468
19469  /* If the type-id production did not work out, then we must be
19470     looking at the unary-expression production.  */
19471  if (!expr)
19472    expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19473				       /*cast_p=*/false, NULL);
19474
19475  if (pack_expansion_p)
19476    /* Build a pack expansion. */
19477    expr = make_pack_expansion (expr);
19478
19479  /* Go back to evaluating expressions.  */
19480  --cp_unevaluated_operand;
19481  --c_inhibit_evaluation_warnings;
19482
19483  /* Free the message we created.  */
19484  free (tmp);
19485  /* And restore the old one.  */
19486  parser->type_definition_forbidden_message = saved_message;
19487  parser->integral_constant_expression_p
19488    = saved_integral_constant_expression_p;
19489  parser->non_integral_constant_expression_p
19490    = saved_non_integral_constant_expression_p;
19491
19492  return expr;
19493}
19494
19495/* If the current declaration has no declarator, return true.  */
19496
19497static bool
19498cp_parser_declares_only_class_p (cp_parser *parser)
19499{
19500  /* If the next token is a `;' or a `,' then there is no
19501     declarator.  */
19502  return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19503	  || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19504}
19505
19506/* Update the DECL_SPECS to reflect the storage class indicated by
19507   KEYWORD.  */
19508
19509static void
19510cp_parser_set_storage_class (cp_parser *parser,
19511			     cp_decl_specifier_seq *decl_specs,
19512			     enum rid keyword,
19513			     location_t location)
19514{
19515  cp_storage_class storage_class;
19516
19517  if (parser->in_unbraced_linkage_specification_p)
19518    {
19519      error_at (location, "invalid use of %qD in linkage specification",
19520		ridpointers[keyword]);
19521      return;
19522    }
19523  else if (decl_specs->storage_class != sc_none)
19524    {
19525      decl_specs->conflicting_specifiers_p = true;
19526      return;
19527    }
19528
19529  if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19530      && decl_specs->specs[(int) ds_thread])
19531    {
19532      error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19533      decl_specs->specs[(int) ds_thread] = 0;
19534    }
19535
19536  switch (keyword)
19537    {
19538    case RID_AUTO:
19539      storage_class = sc_auto;
19540      break;
19541    case RID_REGISTER:
19542      storage_class = sc_register;
19543      break;
19544    case RID_STATIC:
19545      storage_class = sc_static;
19546      break;
19547    case RID_EXTERN:
19548      storage_class = sc_extern;
19549      break;
19550    case RID_MUTABLE:
19551      storage_class = sc_mutable;
19552      break;
19553    default:
19554      gcc_unreachable ();
19555    }
19556  decl_specs->storage_class = storage_class;
19557
19558  /* A storage class specifier cannot be applied alongside a typedef
19559     specifier. If there is a typedef specifier present then set
19560     conflicting_specifiers_p which will trigger an error later
19561     on in grokdeclarator. */
19562  if (decl_specs->specs[(int)ds_typedef])
19563    decl_specs->conflicting_specifiers_p = true;
19564}
19565
19566/* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19567   is true, the type is a user-defined type; otherwise it is a
19568   built-in type specified by a keyword.  */
19569
19570static void
19571cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19572			      tree type_spec,
19573			      location_t location,
19574			      bool user_defined_p)
19575{
19576  decl_specs->any_specifiers_p = true;
19577
19578  /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19579     (with, for example, in "typedef int wchar_t;") we remember that
19580     this is what happened.  In system headers, we ignore these
19581     declarations so that G++ can work with system headers that are not
19582     C++-safe.  */
19583  if (decl_specs->specs[(int) ds_typedef]
19584      && !user_defined_p
19585      && (type_spec == boolean_type_node
19586	  || type_spec == char16_type_node
19587	  || type_spec == char32_type_node
19588	  || type_spec == wchar_type_node)
19589      && (decl_specs->type
19590	  || decl_specs->specs[(int) ds_long]
19591	  || decl_specs->specs[(int) ds_short]
19592	  || decl_specs->specs[(int) ds_unsigned]
19593	  || decl_specs->specs[(int) ds_signed]))
19594    {
19595      decl_specs->redefined_builtin_type = type_spec;
19596      if (!decl_specs->type)
19597	{
19598	  decl_specs->type = type_spec;
19599	  decl_specs->user_defined_type_p = false;
19600	  decl_specs->type_location = location;
19601	}
19602    }
19603  else if (decl_specs->type)
19604    decl_specs->multiple_types_p = true;
19605  else
19606    {
19607      decl_specs->type = type_spec;
19608      decl_specs->user_defined_type_p = user_defined_p;
19609      decl_specs->redefined_builtin_type = NULL_TREE;
19610      decl_specs->type_location = location;
19611    }
19612}
19613
19614/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19615   Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19616
19617static bool
19618cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19619{
19620  return decl_specifiers->specs[(int) ds_friend] != 0;
19621}
19622
19623/* If the next token is of the indicated TYPE, consume it.  Otherwise,
19624   issue an error message indicating that TOKEN_DESC was expected.
19625
19626   Returns the token consumed, if the token had the appropriate type.
19627   Otherwise, returns NULL.  */
19628
19629static cp_token *
19630cp_parser_require (cp_parser* parser,
19631		   enum cpp_ttype type,
19632		   const char* token_desc)
19633{
19634  if (cp_lexer_next_token_is (parser->lexer, type))
19635    return cp_lexer_consume_token (parser->lexer);
19636  else
19637    {
19638      /* Output the MESSAGE -- unless we're parsing tentatively.  */
19639      if (!cp_parser_simulate_error (parser))
19640	{
19641	  char *message = concat ("expected ", token_desc, NULL);
19642	  cp_parser_error (parser, message);
19643	  free (message);
19644	}
19645      return NULL;
19646    }
19647}
19648
19649/* An error message is produced if the next token is not '>'.
19650   All further tokens are skipped until the desired token is
19651   found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19652
19653static void
19654cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19655{
19656  /* Current level of '< ... >'.  */
19657  unsigned level = 0;
19658  /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19659  unsigned nesting_depth = 0;
19660
19661  /* Are we ready, yet?  If not, issue error message.  */
19662  if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19663    return;
19664
19665  /* Skip tokens until the desired token is found.  */
19666  while (true)
19667    {
19668      /* Peek at the next token.  */
19669      switch (cp_lexer_peek_token (parser->lexer)->type)
19670	{
19671	case CPP_LESS:
19672	  if (!nesting_depth)
19673	    ++level;
19674	  break;
19675
19676        case CPP_RSHIFT:
19677          if (cxx_dialect == cxx98)
19678            /* C++0x views the `>>' operator as two `>' tokens, but
19679               C++98 does not. */
19680            break;
19681          else if (!nesting_depth && level-- == 0)
19682	    {
19683              /* We've hit a `>>' where the first `>' closes the
19684                 template argument list, and the second `>' is
19685                 spurious.  Just consume the `>>' and stop; we've
19686                 already produced at least one error.  */
19687	      cp_lexer_consume_token (parser->lexer);
19688	      return;
19689	    }
19690          /* Fall through for C++0x, so we handle the second `>' in
19691             the `>>'.  */
19692
19693	case CPP_GREATER:
19694	  if (!nesting_depth && level-- == 0)
19695	    {
19696	      /* We've reached the token we want, consume it and stop.  */
19697	      cp_lexer_consume_token (parser->lexer);
19698	      return;
19699	    }
19700	  break;
19701
19702	case CPP_OPEN_PAREN:
19703	case CPP_OPEN_SQUARE:
19704	  ++nesting_depth;
19705	  break;
19706
19707	case CPP_CLOSE_PAREN:
19708	case CPP_CLOSE_SQUARE:
19709	  if (nesting_depth-- == 0)
19710	    return;
19711	  break;
19712
19713	case CPP_EOF:
19714	case CPP_PRAGMA_EOL:
19715	case CPP_SEMICOLON:
19716	case CPP_OPEN_BRACE:
19717	case CPP_CLOSE_BRACE:
19718	  /* The '>' was probably forgotten, don't look further.  */
19719	  return;
19720
19721	default:
19722	  break;
19723	}
19724
19725      /* Consume this token.  */
19726      cp_lexer_consume_token (parser->lexer);
19727    }
19728}
19729
19730/* If the next token is the indicated keyword, consume it.  Otherwise,
19731   issue an error message indicating that TOKEN_DESC was expected.
19732
19733   Returns the token consumed, if the token had the appropriate type.
19734   Otherwise, returns NULL.  */
19735
19736static cp_token *
19737cp_parser_require_keyword (cp_parser* parser,
19738			   enum rid keyword,
19739			   const char* token_desc)
19740{
19741  cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19742
19743  if (token && token->keyword != keyword)
19744    {
19745      dyn_string_t error_msg;
19746
19747      /* Format the error message.  */
19748      error_msg = dyn_string_new (0);
19749      dyn_string_append_cstr (error_msg, "expected ");
19750      dyn_string_append_cstr (error_msg, token_desc);
19751      cp_parser_error (parser, error_msg->s);
19752      dyn_string_delete (error_msg);
19753      return NULL;
19754    }
19755
19756  return token;
19757}
19758
19759/* Returns TRUE iff TOKEN is a token that can begin the body of a
19760   function-definition.  */
19761
19762static bool
19763cp_parser_token_starts_function_definition_p (cp_token* token)
19764{
19765  return (/* An ordinary function-body begins with an `{'.  */
19766	  token->type == CPP_OPEN_BRACE
19767	  /* A ctor-initializer begins with a `:'.  */
19768	  || token->type == CPP_COLON
19769	  /* A function-try-block begins with `try'.  */
19770	  || token->keyword == RID_TRY
19771	  /* The named return value extension begins with `return'.  */
19772	  || token->keyword == RID_RETURN);
19773}
19774
19775/* Returns TRUE iff the next token is the ":" or "{" beginning a class
19776   definition.  */
19777
19778static bool
19779cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19780{
19781  cp_token *token;
19782
19783  token = cp_lexer_peek_token (parser->lexer);
19784  return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19785}
19786
19787/* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19788   C++0x) ending a template-argument.  */
19789
19790static bool
19791cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19792{
19793  cp_token *token;
19794
19795  token = cp_lexer_peek_token (parser->lexer);
19796  return (token->type == CPP_COMMA
19797          || token->type == CPP_GREATER
19798          || token->type == CPP_ELLIPSIS
19799	  || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19800}
19801
19802/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19803   (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19804
19805static bool
19806cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19807						     size_t n)
19808{
19809  cp_token *token;
19810
19811  token = cp_lexer_peek_nth_token (parser->lexer, n);
19812  if (token->type == CPP_LESS)
19813    return true;
19814  /* Check for the sequence `<::' in the original code. It would be lexed as
19815     `[:', where `[' is a digraph, and there is no whitespace before
19816     `:'.  */
19817  if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19818    {
19819      cp_token *token2;
19820      token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19821      if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19822	return true;
19823    }
19824  return false;
19825}
19826
19827/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19828   or none_type otherwise.  */
19829
19830static enum tag_types
19831cp_parser_token_is_class_key (cp_token* token)
19832{
19833  switch (token->keyword)
19834    {
19835    case RID_CLASS:
19836      return class_type;
19837    case RID_STRUCT:
19838      return record_type;
19839    case RID_UNION:
19840      return union_type;
19841
19842    default:
19843      return none_type;
19844    }
19845}
19846
19847/* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19848
19849static void
19850cp_parser_check_class_key (enum tag_types class_key, tree type)
19851{
19852  if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19853    permerror (input_location, "%qs tag used in naming %q#T",
19854	    class_key == union_type ? "union"
19855	     : class_key == record_type ? "struct" : "class",
19856	     type);
19857}
19858
19859/* Issue an error message if DECL is redeclared with different
19860   access than its original declaration [class.access.spec/3].
19861   This applies to nested classes and nested class templates.
19862   [class.mem/1].  */
19863
19864static void
19865cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19866{
19867  if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19868    return;
19869
19870  if ((TREE_PRIVATE (decl)
19871       != (current_access_specifier == access_private_node))
19872      || (TREE_PROTECTED (decl)
19873	  != (current_access_specifier == access_protected_node)))
19874    error_at (location, "%qD redeclared with different access", decl);
19875}
19876
19877/* Look for the `template' keyword, as a syntactic disambiguator.
19878   Return TRUE iff it is present, in which case it will be
19879   consumed.  */
19880
19881static bool
19882cp_parser_optional_template_keyword (cp_parser *parser)
19883{
19884  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19885    {
19886      /* The `template' keyword can only be used within templates;
19887	 outside templates the parser can always figure out what is a
19888	 template and what is not.  */
19889      if (!processing_template_decl)
19890	{
19891	  cp_token *token = cp_lexer_peek_token (parser->lexer);
19892	  error_at (token->location,
19893		    "%<template%> (as a disambiguator) is only allowed "
19894		    "within templates");
19895	  /* If this part of the token stream is rescanned, the same
19896	     error message would be generated.  So, we purge the token
19897	     from the stream.  */
19898	  cp_lexer_purge_token (parser->lexer);
19899	  return false;
19900	}
19901      else
19902	{
19903	  /* Consume the `template' keyword.  */
19904	  cp_lexer_consume_token (parser->lexer);
19905	  return true;
19906	}
19907    }
19908
19909  return false;
19910}
19911
19912/* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19913   set PARSER->SCOPE, and perform other related actions.  */
19914
19915static void
19916cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19917{
19918  int i;
19919  struct tree_check *check_value;
19920  deferred_access_check *chk;
19921  VEC (deferred_access_check,gc) *checks;
19922
19923  /* Get the stored value.  */
19924  check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19925  /* Perform any access checks that were deferred.  */
19926  checks = check_value->checks;
19927  if (checks)
19928    {
19929      for (i = 0 ;
19930	   VEC_iterate (deferred_access_check, checks, i, chk) ;
19931	   ++i)
19932	{
19933	  perform_or_defer_access_check (chk->binfo,
19934					 chk->decl,
19935					 chk->diag_decl);
19936	}
19937    }
19938  /* Set the scope from the stored value.  */
19939  parser->scope = check_value->value;
19940  parser->qualifying_scope = check_value->qualifying_scope;
19941  parser->object_scope = NULL_TREE;
19942}
19943
19944/* Consume tokens up through a non-nested END token.  Returns TRUE if we
19945   encounter the end of a block before what we were looking for.  */
19946
19947static bool
19948cp_parser_cache_group (cp_parser *parser,
19949		       enum cpp_ttype end,
19950		       unsigned depth)
19951{
19952  while (true)
19953    {
19954      cp_token *token = cp_lexer_peek_token (parser->lexer);
19955
19956      /* Abort a parenthesized expression if we encounter a semicolon.  */
19957      if ((end == CPP_CLOSE_PAREN || depth == 0)
19958	  && token->type == CPP_SEMICOLON)
19959	return true;
19960      /* If we've reached the end of the file, stop.  */
19961      if (token->type == CPP_EOF
19962	  || (end != CPP_PRAGMA_EOL
19963	      && token->type == CPP_PRAGMA_EOL))
19964	return true;
19965      if (token->type == CPP_CLOSE_BRACE && depth == 0)
19966	/* We've hit the end of an enclosing block, so there's been some
19967	   kind of syntax error.  */
19968	return true;
19969
19970      /* Consume the token.  */
19971      cp_lexer_consume_token (parser->lexer);
19972      /* See if it starts a new group.  */
19973      if (token->type == CPP_OPEN_BRACE)
19974	{
19975	  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19976	  /* In theory this should probably check end == '}', but
19977	     cp_parser_save_member_function_body needs it to exit
19978	     after either '}' or ')' when called with ')'.  */
19979	  if (depth == 0)
19980	    return false;
19981	}
19982      else if (token->type == CPP_OPEN_PAREN)
19983	{
19984	  cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19985	  if (depth == 0 && end == CPP_CLOSE_PAREN)
19986	    return false;
19987	}
19988      else if (token->type == CPP_PRAGMA)
19989	cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19990      else if (token->type == end)
19991	return false;
19992    }
19993}
19994
19995/* Begin parsing tentatively.  We always save tokens while parsing
19996   tentatively so that if the tentative parsing fails we can restore the
19997   tokens.  */
19998
19999static void
20000cp_parser_parse_tentatively (cp_parser* parser)
20001{
20002  /* Enter a new parsing context.  */
20003  parser->context = cp_parser_context_new (parser->context);
20004  /* Begin saving tokens.  */
20005  cp_lexer_save_tokens (parser->lexer);
20006  /* In order to avoid repetitive access control error messages,
20007     access checks are queued up until we are no longer parsing
20008     tentatively.  */
20009  push_deferring_access_checks (dk_deferred);
20010}
20011
20012/* Commit to the currently active tentative parse.  */
20013
20014static void
20015cp_parser_commit_to_tentative_parse (cp_parser* parser)
20016{
20017  cp_parser_context *context;
20018  cp_lexer *lexer;
20019
20020  /* Mark all of the levels as committed.  */
20021  lexer = parser->lexer;
20022  for (context = parser->context; context->next; context = context->next)
20023    {
20024      if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20025	break;
20026      context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20027      while (!cp_lexer_saving_tokens (lexer))
20028	lexer = lexer->next;
20029      cp_lexer_commit_tokens (lexer);
20030    }
20031}
20032
20033/* Abort the currently active tentative parse.  All consumed tokens
20034   will be rolled back, and no diagnostics will be issued.  */
20035
20036static void
20037cp_parser_abort_tentative_parse (cp_parser* parser)
20038{
20039  cp_parser_simulate_error (parser);
20040  /* Now, pretend that we want to see if the construct was
20041     successfully parsed.  */
20042  cp_parser_parse_definitely (parser);
20043}
20044
20045/* Stop parsing tentatively.  If a parse error has occurred, restore the
20046   token stream.  Otherwise, commit to the tokens we have consumed.
20047   Returns true if no error occurred; false otherwise.  */
20048
20049static bool
20050cp_parser_parse_definitely (cp_parser* parser)
20051{
20052  bool error_occurred;
20053  cp_parser_context *context;
20054
20055  /* Remember whether or not an error occurred, since we are about to
20056     destroy that information.  */
20057  error_occurred = cp_parser_error_occurred (parser);
20058  /* Remove the topmost context from the stack.  */
20059  context = parser->context;
20060  parser->context = context->next;
20061  /* If no parse errors occurred, commit to the tentative parse.  */
20062  if (!error_occurred)
20063    {
20064      /* Commit to the tokens read tentatively, unless that was
20065	 already done.  */
20066      if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20067	cp_lexer_commit_tokens (parser->lexer);
20068
20069      pop_to_parent_deferring_access_checks ();
20070    }
20071  /* Otherwise, if errors occurred, roll back our state so that things
20072     are just as they were before we began the tentative parse.  */
20073  else
20074    {
20075      cp_lexer_rollback_tokens (parser->lexer);
20076      pop_deferring_access_checks ();
20077    }
20078  /* Add the context to the front of the free list.  */
20079  context->next = cp_parser_context_free_list;
20080  cp_parser_context_free_list = context;
20081
20082  return !error_occurred;
20083}
20084
20085/* Returns true if we are parsing tentatively and are not committed to
20086   this tentative parse.  */
20087
20088static bool
20089cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20090{
20091  return (cp_parser_parsing_tentatively (parser)
20092	  && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20093}
20094
20095/* Returns nonzero iff an error has occurred during the most recent
20096   tentative parse.  */
20097
20098static bool
20099cp_parser_error_occurred (cp_parser* parser)
20100{
20101  return (cp_parser_parsing_tentatively (parser)
20102	  && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20103}
20104
20105/* Returns nonzero if GNU extensions are allowed.  */
20106
20107static bool
20108cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20109{
20110  return parser->allow_gnu_extensions_p;
20111}
20112
20113/* Objective-C++ Productions */
20114
20115
20116/* Parse an Objective-C expression, which feeds into a primary-expression
20117   above.
20118
20119   objc-expression:
20120     objc-message-expression
20121     objc-string-literal
20122     objc-encode-expression
20123     objc-protocol-expression
20124     objc-selector-expression
20125
20126  Returns a tree representation of the expression.  */
20127
20128static tree
20129cp_parser_objc_expression (cp_parser* parser)
20130{
20131  /* Try to figure out what kind of declaration is present.  */
20132  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20133
20134  switch (kwd->type)
20135    {
20136    case CPP_OPEN_SQUARE:
20137      return cp_parser_objc_message_expression (parser);
20138
20139    case CPP_OBJC_STRING:
20140      kwd = cp_lexer_consume_token (parser->lexer);
20141      return objc_build_string_object (kwd->u.value);
20142
20143    case CPP_KEYWORD:
20144      switch (kwd->keyword)
20145	{
20146	case RID_AT_ENCODE:
20147	  return cp_parser_objc_encode_expression (parser);
20148
20149	case RID_AT_PROTOCOL:
20150	  return cp_parser_objc_protocol_expression (parser);
20151
20152	case RID_AT_SELECTOR:
20153	  return cp_parser_objc_selector_expression (parser);
20154
20155	default:
20156	  break;
20157	}
20158    default:
20159      error_at (kwd->location,
20160		"misplaced %<@%D%> Objective-C++ construct",
20161		kwd->u.value);
20162      cp_parser_skip_to_end_of_block_or_statement (parser);
20163    }
20164
20165  return error_mark_node;
20166}
20167
20168/* Parse an Objective-C message expression.
20169
20170   objc-message-expression:
20171     [ objc-message-receiver objc-message-args ]
20172
20173   Returns a representation of an Objective-C message.  */
20174
20175static tree
20176cp_parser_objc_message_expression (cp_parser* parser)
20177{
20178  tree receiver, messageargs;
20179
20180  cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20181  receiver = cp_parser_objc_message_receiver (parser);
20182  messageargs = cp_parser_objc_message_args (parser);
20183  cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20184
20185  return objc_build_message_expr (build_tree_list (receiver, messageargs));
20186}
20187
20188/* Parse an objc-message-receiver.
20189
20190   objc-message-receiver:
20191     expression
20192     simple-type-specifier
20193
20194  Returns a representation of the type or expression.  */
20195
20196static tree
20197cp_parser_objc_message_receiver (cp_parser* parser)
20198{
20199  tree rcv;
20200
20201  /* An Objective-C message receiver may be either (1) a type
20202     or (2) an expression.  */
20203  cp_parser_parse_tentatively (parser);
20204  rcv = cp_parser_expression (parser, false, NULL);
20205
20206  if (cp_parser_parse_definitely (parser))
20207    return rcv;
20208
20209  rcv = cp_parser_simple_type_specifier (parser,
20210					 /*decl_specs=*/NULL,
20211					 CP_PARSER_FLAGS_NONE);
20212
20213  return objc_get_class_reference (rcv);
20214}
20215
20216/* Parse the arguments and selectors comprising an Objective-C message.
20217
20218   objc-message-args:
20219     objc-selector
20220     objc-selector-args
20221     objc-selector-args , objc-comma-args
20222
20223   objc-selector-args:
20224     objc-selector [opt] : assignment-expression
20225     objc-selector-args objc-selector [opt] : assignment-expression
20226
20227   objc-comma-args:
20228     assignment-expression
20229     objc-comma-args , assignment-expression
20230
20231   Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20232   selector arguments and TREE_VALUE containing a list of comma
20233   arguments.  */
20234
20235static tree
20236cp_parser_objc_message_args (cp_parser* parser)
20237{
20238  tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20239  bool maybe_unary_selector_p = true;
20240  cp_token *token = cp_lexer_peek_token (parser->lexer);
20241
20242  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20243    {
20244      tree selector = NULL_TREE, arg;
20245
20246      if (token->type != CPP_COLON)
20247	selector = cp_parser_objc_selector (parser);
20248
20249      /* Detect if we have a unary selector.  */
20250      if (maybe_unary_selector_p
20251	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20252	return build_tree_list (selector, NULL_TREE);
20253
20254      maybe_unary_selector_p = false;
20255      cp_parser_require (parser, CPP_COLON, "%<:%>");
20256      arg = cp_parser_assignment_expression (parser, false, NULL);
20257
20258      sel_args
20259	= chainon (sel_args,
20260		   build_tree_list (selector, arg));
20261
20262      token = cp_lexer_peek_token (parser->lexer);
20263    }
20264
20265  /* Handle non-selector arguments, if any. */
20266  while (token->type == CPP_COMMA)
20267    {
20268      tree arg;
20269
20270      cp_lexer_consume_token (parser->lexer);
20271      arg = cp_parser_assignment_expression (parser, false, NULL);
20272
20273      addl_args
20274	= chainon (addl_args,
20275		   build_tree_list (NULL_TREE, arg));
20276
20277      token = cp_lexer_peek_token (parser->lexer);
20278    }
20279
20280  return build_tree_list (sel_args, addl_args);
20281}
20282
20283/* Parse an Objective-C encode expression.
20284
20285   objc-encode-expression:
20286     @encode objc-typename
20287
20288   Returns an encoded representation of the type argument.  */
20289
20290static tree
20291cp_parser_objc_encode_expression (cp_parser* parser)
20292{
20293  tree type;
20294  cp_token *token;
20295
20296  cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20297  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20298  token = cp_lexer_peek_token (parser->lexer);
20299  type = complete_type (cp_parser_type_id (parser));
20300  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20301
20302  if (!type)
20303    {
20304      error_at (token->location,
20305		"%<@encode%> must specify a type as an argument");
20306      return error_mark_node;
20307    }
20308
20309  return objc_build_encode_expr (type);
20310}
20311
20312/* Parse an Objective-C @defs expression.  */
20313
20314static tree
20315cp_parser_objc_defs_expression (cp_parser *parser)
20316{
20317  tree name;
20318
20319  cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20320  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20321  name = cp_parser_identifier (parser);
20322  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20323
20324  return objc_get_class_ivars (name);
20325}
20326
20327/* Parse an Objective-C protocol expression.
20328
20329  objc-protocol-expression:
20330    @protocol ( identifier )
20331
20332  Returns a representation of the protocol expression.  */
20333
20334static tree
20335cp_parser_objc_protocol_expression (cp_parser* parser)
20336{
20337  tree proto;
20338
20339  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20340  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20341  proto = cp_parser_identifier (parser);
20342  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20343
20344  return objc_build_protocol_expr (proto);
20345}
20346
20347/* Parse an Objective-C selector expression.
20348
20349   objc-selector-expression:
20350     @selector ( objc-method-signature )
20351
20352   objc-method-signature:
20353     objc-selector
20354     objc-selector-seq
20355
20356   objc-selector-seq:
20357     objc-selector :
20358     objc-selector-seq objc-selector :
20359
20360  Returns a representation of the method selector.  */
20361
20362static tree
20363cp_parser_objc_selector_expression (cp_parser* parser)
20364{
20365  tree sel_seq = NULL_TREE;
20366  bool maybe_unary_selector_p = true;
20367  cp_token *token;
20368  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20369
20370  cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20371  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20372  token = cp_lexer_peek_token (parser->lexer);
20373
20374  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20375	 || token->type == CPP_SCOPE)
20376    {
20377      tree selector = NULL_TREE;
20378
20379      if (token->type != CPP_COLON
20380	  || token->type == CPP_SCOPE)
20381	selector = cp_parser_objc_selector (parser);
20382
20383      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20384	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20385	{
20386	  /* Detect if we have a unary selector.  */
20387	  if (maybe_unary_selector_p)
20388	    {
20389	      sel_seq = selector;
20390	      goto finish_selector;
20391	    }
20392	  else
20393	    {
20394	      cp_parser_error (parser, "expected %<:%>");
20395	    }
20396	}
20397      maybe_unary_selector_p = false;
20398      token = cp_lexer_consume_token (parser->lexer);
20399
20400      if (token->type == CPP_SCOPE)
20401	{
20402	  sel_seq
20403	    = chainon (sel_seq,
20404		       build_tree_list (selector, NULL_TREE));
20405	  sel_seq
20406	    = chainon (sel_seq,
20407		       build_tree_list (NULL_TREE, NULL_TREE));
20408	}
20409      else
20410	sel_seq
20411	  = chainon (sel_seq,
20412		     build_tree_list (selector, NULL_TREE));
20413
20414      token = cp_lexer_peek_token (parser->lexer);
20415    }
20416
20417 finish_selector:
20418  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20419
20420  return objc_build_selector_expr (loc, sel_seq);
20421}
20422
20423/* Parse a list of identifiers.
20424
20425   objc-identifier-list:
20426     identifier
20427     objc-identifier-list , identifier
20428
20429   Returns a TREE_LIST of identifier nodes.  */
20430
20431static tree
20432cp_parser_objc_identifier_list (cp_parser* parser)
20433{
20434  tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20435  cp_token *sep = cp_lexer_peek_token (parser->lexer);
20436
20437  while (sep->type == CPP_COMMA)
20438    {
20439      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20440      list = chainon (list,
20441		      build_tree_list (NULL_TREE,
20442				       cp_parser_identifier (parser)));
20443      sep = cp_lexer_peek_token (parser->lexer);
20444    }
20445
20446  return list;
20447}
20448
20449/* Parse an Objective-C alias declaration.
20450
20451   objc-alias-declaration:
20452     @compatibility_alias identifier identifier ;
20453
20454   This function registers the alias mapping with the Objective-C front end.
20455   It returns nothing.  */
20456
20457static void
20458cp_parser_objc_alias_declaration (cp_parser* parser)
20459{
20460  tree alias, orig;
20461
20462  cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20463  alias = cp_parser_identifier (parser);
20464  orig = cp_parser_identifier (parser);
20465  objc_declare_alias (alias, orig);
20466  cp_parser_consume_semicolon_at_end_of_statement (parser);
20467}
20468
20469/* Parse an Objective-C class forward-declaration.
20470
20471   objc-class-declaration:
20472     @class objc-identifier-list ;
20473
20474   The function registers the forward declarations with the Objective-C
20475   front end.  It returns nothing.  */
20476
20477static void
20478cp_parser_objc_class_declaration (cp_parser* parser)
20479{
20480  cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20481  objc_declare_class (cp_parser_objc_identifier_list (parser));
20482  cp_parser_consume_semicolon_at_end_of_statement (parser);
20483}
20484
20485/* Parse a list of Objective-C protocol references.
20486
20487   objc-protocol-refs-opt:
20488     objc-protocol-refs [opt]
20489
20490   objc-protocol-refs:
20491     < objc-identifier-list >
20492
20493   Returns a TREE_LIST of identifiers, if any.  */
20494
20495static tree
20496cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20497{
20498  tree protorefs = NULL_TREE;
20499
20500  if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20501    {
20502      cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20503      protorefs = cp_parser_objc_identifier_list (parser);
20504      cp_parser_require (parser, CPP_GREATER, "%<>%>");
20505    }
20506
20507  return protorefs;
20508}
20509
20510/* Parse a Objective-C visibility specification.  */
20511
20512static void
20513cp_parser_objc_visibility_spec (cp_parser* parser)
20514{
20515  cp_token *vis = cp_lexer_peek_token (parser->lexer);
20516
20517  switch (vis->keyword)
20518    {
20519    case RID_AT_PRIVATE:
20520      objc_set_visibility (2);
20521      break;
20522    case RID_AT_PROTECTED:
20523      objc_set_visibility (0);
20524      break;
20525    case RID_AT_PUBLIC:
20526      objc_set_visibility (1);
20527      break;
20528    default:
20529      return;
20530    }
20531
20532  /* Eat '@private'/'@protected'/'@public'.  */
20533  cp_lexer_consume_token (parser->lexer);
20534}
20535
20536/* Parse an Objective-C method type.  */
20537
20538static void
20539cp_parser_objc_method_type (cp_parser* parser)
20540{
20541  objc_set_method_type
20542   (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20543    ? PLUS_EXPR
20544    : MINUS_EXPR);
20545}
20546
20547/* Parse an Objective-C protocol qualifier.  */
20548
20549static tree
20550cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20551{
20552  tree quals = NULL_TREE, node;
20553  cp_token *token = cp_lexer_peek_token (parser->lexer);
20554
20555  node = token->u.value;
20556
20557  while (node && TREE_CODE (node) == IDENTIFIER_NODE
20558	 && (node == ridpointers [(int) RID_IN]
20559	     || node == ridpointers [(int) RID_OUT]
20560	     || node == ridpointers [(int) RID_INOUT]
20561	     || node == ridpointers [(int) RID_BYCOPY]
20562	     || node == ridpointers [(int) RID_BYREF]
20563	     || node == ridpointers [(int) RID_ONEWAY]))
20564    {
20565      quals = tree_cons (NULL_TREE, node, quals);
20566      cp_lexer_consume_token (parser->lexer);
20567      token = cp_lexer_peek_token (parser->lexer);
20568      node = token->u.value;
20569    }
20570
20571  return quals;
20572}
20573
20574/* Parse an Objective-C typename.  */
20575
20576static tree
20577cp_parser_objc_typename (cp_parser* parser)
20578{
20579  tree type_name = NULL_TREE;
20580
20581  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20582    {
20583      tree proto_quals, cp_type = NULL_TREE;
20584
20585      cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20586      proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20587
20588      /* An ObjC type name may consist of just protocol qualifiers, in which
20589	 case the type shall default to 'id'.  */
20590      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20591	cp_type = cp_parser_type_id (parser);
20592
20593      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20594      type_name = build_tree_list (proto_quals, cp_type);
20595    }
20596
20597  return type_name;
20598}
20599
20600/* Check to see if TYPE refers to an Objective-C selector name.  */
20601
20602static bool
20603cp_parser_objc_selector_p (enum cpp_ttype type)
20604{
20605  return (type == CPP_NAME || type == CPP_KEYWORD
20606	  || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20607	  || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20608	  || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20609	  || type == CPP_XOR || type == CPP_XOR_EQ);
20610}
20611
20612/* Parse an Objective-C selector.  */
20613
20614static tree
20615cp_parser_objc_selector (cp_parser* parser)
20616{
20617  cp_token *token = cp_lexer_consume_token (parser->lexer);
20618
20619  if (!cp_parser_objc_selector_p (token->type))
20620    {
20621      error_at (token->location, "invalid Objective-C++ selector name");
20622      return error_mark_node;
20623    }
20624
20625  /* C++ operator names are allowed to appear in ObjC selectors.  */
20626  switch (token->type)
20627    {
20628    case CPP_AND_AND: return get_identifier ("and");
20629    case CPP_AND_EQ: return get_identifier ("and_eq");
20630    case CPP_AND: return get_identifier ("bitand");
20631    case CPP_OR: return get_identifier ("bitor");
20632    case CPP_COMPL: return get_identifier ("compl");
20633    case CPP_NOT: return get_identifier ("not");
20634    case CPP_NOT_EQ: return get_identifier ("not_eq");
20635    case CPP_OR_OR: return get_identifier ("or");
20636    case CPP_OR_EQ: return get_identifier ("or_eq");
20637    case CPP_XOR: return get_identifier ("xor");
20638    case CPP_XOR_EQ: return get_identifier ("xor_eq");
20639    default: return token->u.value;
20640    }
20641}
20642
20643/* Parse an Objective-C params list.  */
20644
20645static tree
20646cp_parser_objc_method_keyword_params (cp_parser* parser)
20647{
20648  tree params = NULL_TREE;
20649  bool maybe_unary_selector_p = true;
20650  cp_token *token = cp_lexer_peek_token (parser->lexer);
20651
20652  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20653    {
20654      tree selector = NULL_TREE, type_name, identifier;
20655
20656      if (token->type != CPP_COLON)
20657	selector = cp_parser_objc_selector (parser);
20658
20659      /* Detect if we have a unary selector.  */
20660      if (maybe_unary_selector_p
20661	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20662	return selector;
20663
20664      maybe_unary_selector_p = false;
20665      cp_parser_require (parser, CPP_COLON, "%<:%>");
20666      type_name = cp_parser_objc_typename (parser);
20667      identifier = cp_parser_identifier (parser);
20668
20669      params
20670	= chainon (params,
20671		   objc_build_keyword_decl (selector,
20672					    type_name,
20673					    identifier));
20674
20675      token = cp_lexer_peek_token (parser->lexer);
20676    }
20677
20678  return params;
20679}
20680
20681/* Parse the non-keyword Objective-C params.  */
20682
20683static tree
20684cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20685{
20686  tree params = make_node (TREE_LIST);
20687  cp_token *token = cp_lexer_peek_token (parser->lexer);
20688  *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20689
20690  while (token->type == CPP_COMMA)
20691    {
20692      cp_parameter_declarator *parmdecl;
20693      tree parm;
20694
20695      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20696      token = cp_lexer_peek_token (parser->lexer);
20697
20698      if (token->type == CPP_ELLIPSIS)
20699	{
20700	  cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20701	  *ellipsisp = true;
20702	  break;
20703	}
20704
20705      parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20706      parm = grokdeclarator (parmdecl->declarator,
20707			     &parmdecl->decl_specifiers,
20708			     PARM, /*initialized=*/0,
20709			     /*attrlist=*/NULL);
20710
20711      chainon (params, build_tree_list (NULL_TREE, parm));
20712      token = cp_lexer_peek_token (parser->lexer);
20713    }
20714
20715  return params;
20716}
20717
20718/* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20719
20720static void
20721cp_parser_objc_interstitial_code (cp_parser* parser)
20722{
20723  cp_token *token = cp_lexer_peek_token (parser->lexer);
20724
20725  /* If the next token is `extern' and the following token is a string
20726     literal, then we have a linkage specification.  */
20727  if (token->keyword == RID_EXTERN
20728      && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20729    cp_parser_linkage_specification (parser);
20730  /* Handle #pragma, if any.  */
20731  else if (token->type == CPP_PRAGMA)
20732    cp_parser_pragma (parser, pragma_external);
20733  /* Allow stray semicolons.  */
20734  else if (token->type == CPP_SEMICOLON)
20735    cp_lexer_consume_token (parser->lexer);
20736  /* Finally, try to parse a block-declaration, or a function-definition.  */
20737  else
20738    cp_parser_block_declaration (parser, /*statement_p=*/false);
20739}
20740
20741/* Parse a method signature.  */
20742
20743static tree
20744cp_parser_objc_method_signature (cp_parser* parser)
20745{
20746  tree rettype, kwdparms, optparms;
20747  bool ellipsis = false;
20748
20749  cp_parser_objc_method_type (parser);
20750  rettype = cp_parser_objc_typename (parser);
20751  kwdparms = cp_parser_objc_method_keyword_params (parser);
20752  optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20753
20754  return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20755}
20756
20757/* Pars an Objective-C method prototype list.  */
20758
20759static void
20760cp_parser_objc_method_prototype_list (cp_parser* parser)
20761{
20762  cp_token *token = cp_lexer_peek_token (parser->lexer);
20763
20764  while (token->keyword != RID_AT_END)
20765    {
20766      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20767	{
20768	  objc_add_method_declaration
20769	   (cp_parser_objc_method_signature (parser));
20770	  cp_parser_consume_semicolon_at_end_of_statement (parser);
20771	}
20772      else
20773	/* Allow for interspersed non-ObjC++ code.  */
20774	cp_parser_objc_interstitial_code (parser);
20775
20776      token = cp_lexer_peek_token (parser->lexer);
20777    }
20778
20779  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20780  objc_finish_interface ();
20781}
20782
20783/* Parse an Objective-C method definition list.  */
20784
20785static void
20786cp_parser_objc_method_definition_list (cp_parser* parser)
20787{
20788  cp_token *token = cp_lexer_peek_token (parser->lexer);
20789
20790  while (token->keyword != RID_AT_END)
20791    {
20792      tree meth;
20793
20794      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20795	{
20796	  push_deferring_access_checks (dk_deferred);
20797	  objc_start_method_definition
20798	   (cp_parser_objc_method_signature (parser));
20799
20800	  /* For historical reasons, we accept an optional semicolon.  */
20801	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20802	    cp_lexer_consume_token (parser->lexer);
20803
20804	  perform_deferred_access_checks ();
20805	  stop_deferring_access_checks ();
20806	  meth = cp_parser_function_definition_after_declarator (parser,
20807								 false);
20808	  pop_deferring_access_checks ();
20809	  objc_finish_method_definition (meth);
20810	}
20811      else
20812	/* Allow for interspersed non-ObjC++ code.  */
20813	cp_parser_objc_interstitial_code (parser);
20814
20815      token = cp_lexer_peek_token (parser->lexer);
20816    }
20817
20818  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20819  objc_finish_implementation ();
20820}
20821
20822/* Parse Objective-C ivars.  */
20823
20824static void
20825cp_parser_objc_class_ivars (cp_parser* parser)
20826{
20827  cp_token *token = cp_lexer_peek_token (parser->lexer);
20828
20829  if (token->type != CPP_OPEN_BRACE)
20830    return;	/* No ivars specified.  */
20831
20832  cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20833  token = cp_lexer_peek_token (parser->lexer);
20834
20835  while (token->type != CPP_CLOSE_BRACE)
20836    {
20837      cp_decl_specifier_seq declspecs;
20838      int decl_class_or_enum_p;
20839      tree prefix_attributes;
20840
20841      cp_parser_objc_visibility_spec (parser);
20842
20843      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20844	break;
20845
20846      cp_parser_decl_specifier_seq (parser,
20847				    CP_PARSER_FLAGS_OPTIONAL,
20848				    &declspecs,
20849				    &decl_class_or_enum_p);
20850      prefix_attributes = declspecs.attributes;
20851      declspecs.attributes = NULL_TREE;
20852
20853      /* Keep going until we hit the `;' at the end of the
20854	 declaration.  */
20855      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20856	{
20857	  tree width = NULL_TREE, attributes, first_attribute, decl;
20858	  cp_declarator *declarator = NULL;
20859	  int ctor_dtor_or_conv_p;
20860
20861	  /* Check for a (possibly unnamed) bitfield declaration.  */
20862	  token = cp_lexer_peek_token (parser->lexer);
20863	  if (token->type == CPP_COLON)
20864	    goto eat_colon;
20865
20866	  if (token->type == CPP_NAME
20867	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20868		  == CPP_COLON))
20869	    {
20870	      /* Get the name of the bitfield.  */
20871	      declarator = make_id_declarator (NULL_TREE,
20872					       cp_parser_identifier (parser),
20873					       sfk_none);
20874
20875	     eat_colon:
20876	      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20877	      /* Get the width of the bitfield.  */
20878	      width
20879		= cp_parser_constant_expression (parser,
20880						 /*allow_non_constant=*/false,
20881						 NULL);
20882	    }
20883	  else
20884	    {
20885	      /* Parse the declarator.  */
20886	      declarator
20887		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20888					&ctor_dtor_or_conv_p,
20889					/*parenthesized_p=*/NULL,
20890					/*member_p=*/false);
20891	    }
20892
20893	  /* Look for attributes that apply to the ivar.  */
20894	  attributes = cp_parser_attributes_opt (parser);
20895	  /* Remember which attributes are prefix attributes and
20896	     which are not.  */
20897	  first_attribute = attributes;
20898	  /* Combine the attributes.  */
20899	  attributes = chainon (prefix_attributes, attributes);
20900
20901	  if (width)
20902	      /* Create the bitfield declaration.  */
20903	      decl = grokbitfield (declarator, &declspecs,
20904				   width,
20905				   attributes);
20906	  else
20907	    decl = grokfield (declarator, &declspecs,
20908			      NULL_TREE, /*init_const_expr_p=*/false,
20909			      NULL_TREE, attributes);
20910
20911	  /* Add the instance variable.  */
20912	  objc_add_instance_variable (decl);
20913
20914	  /* Reset PREFIX_ATTRIBUTES.  */
20915	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
20916	    attributes = TREE_CHAIN (attributes);
20917	  if (attributes)
20918	    TREE_CHAIN (attributes) = NULL_TREE;
20919
20920	  token = cp_lexer_peek_token (parser->lexer);
20921
20922	  if (token->type == CPP_COMMA)
20923	    {
20924	      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20925	      continue;
20926	    }
20927	  break;
20928	}
20929
20930      cp_parser_consume_semicolon_at_end_of_statement (parser);
20931      token = cp_lexer_peek_token (parser->lexer);
20932    }
20933
20934  cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20935  /* For historical reasons, we accept an optional semicolon.  */
20936  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20937    cp_lexer_consume_token (parser->lexer);
20938}
20939
20940/* Parse an Objective-C protocol declaration.  */
20941
20942static void
20943cp_parser_objc_protocol_declaration (cp_parser* parser)
20944{
20945  tree proto, protorefs;
20946  cp_token *tok;
20947
20948  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20949  if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20950    {
20951      tok = cp_lexer_peek_token (parser->lexer);
20952      error_at (tok->location, "identifier expected after %<@protocol%>");
20953      goto finish;
20954    }
20955
20956  /* See if we have a forward declaration or a definition.  */
20957  tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20958
20959  /* Try a forward declaration first.  */
20960  if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20961    {
20962      objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20963     finish:
20964      cp_parser_consume_semicolon_at_end_of_statement (parser);
20965    }
20966
20967  /* Ok, we got a full-fledged definition (or at least should).  */
20968  else
20969    {
20970      proto = cp_parser_identifier (parser);
20971      protorefs = cp_parser_objc_protocol_refs_opt (parser);
20972      objc_start_protocol (proto, protorefs);
20973      cp_parser_objc_method_prototype_list (parser);
20974    }
20975}
20976
20977/* Parse an Objective-C superclass or category.  */
20978
20979static void
20980cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20981							  tree *categ)
20982{
20983  cp_token *next = cp_lexer_peek_token (parser->lexer);
20984
20985  *super = *categ = NULL_TREE;
20986  if (next->type == CPP_COLON)
20987    {
20988      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20989      *super = cp_parser_identifier (parser);
20990    }
20991  else if (next->type == CPP_OPEN_PAREN)
20992    {
20993      cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20994      *categ = cp_parser_identifier (parser);
20995      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20996    }
20997}
20998
20999/* Parse an Objective-C class interface.  */
21000
21001static void
21002cp_parser_objc_class_interface (cp_parser* parser)
21003{
21004  tree name, super, categ, protos;
21005
21006  cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
21007  name = cp_parser_identifier (parser);
21008  cp_parser_objc_superclass_or_category (parser, &super, &categ);
21009  protos = cp_parser_objc_protocol_refs_opt (parser);
21010
21011  /* We have either a class or a category on our hands.  */
21012  if (categ)
21013    objc_start_category_interface (name, categ, protos);
21014  else
21015    {
21016      objc_start_class_interface (name, super, protos);
21017      /* Handle instance variable declarations, if any.  */
21018      cp_parser_objc_class_ivars (parser);
21019      objc_continue_interface ();
21020    }
21021
21022  cp_parser_objc_method_prototype_list (parser);
21023}
21024
21025/* Parse an Objective-C class implementation.  */
21026
21027static void
21028cp_parser_objc_class_implementation (cp_parser* parser)
21029{
21030  tree name, super, categ;
21031
21032  cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
21033  name = cp_parser_identifier (parser);
21034  cp_parser_objc_superclass_or_category (parser, &super, &categ);
21035
21036  /* We have either a class or a category on our hands.  */
21037  if (categ)
21038    objc_start_category_implementation (name, categ);
21039  else
21040    {
21041      objc_start_class_implementation (name, super);
21042      /* Handle instance variable declarations, if any.  */
21043      cp_parser_objc_class_ivars (parser);
21044      objc_continue_implementation ();
21045    }
21046
21047  cp_parser_objc_method_definition_list (parser);
21048}
21049
21050/* Consume the @end token and finish off the implementation.  */
21051
21052static void
21053cp_parser_objc_end_implementation (cp_parser* parser)
21054{
21055  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21056  objc_finish_implementation ();
21057}
21058
21059/* Parse an Objective-C declaration.  */
21060
21061static void
21062cp_parser_objc_declaration (cp_parser* parser)
21063{
21064  /* Try to figure out what kind of declaration is present.  */
21065  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21066
21067  switch (kwd->keyword)
21068    {
21069    case RID_AT_ALIAS:
21070      cp_parser_objc_alias_declaration (parser);
21071      break;
21072    case RID_AT_CLASS:
21073      cp_parser_objc_class_declaration (parser);
21074      break;
21075    case RID_AT_PROTOCOL:
21076      cp_parser_objc_protocol_declaration (parser);
21077      break;
21078    case RID_AT_INTERFACE:
21079      cp_parser_objc_class_interface (parser);
21080      break;
21081    case RID_AT_IMPLEMENTATION:
21082      cp_parser_objc_class_implementation (parser);
21083      break;
21084    case RID_AT_END:
21085      cp_parser_objc_end_implementation (parser);
21086      break;
21087    default:
21088      error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21089		kwd->u.value);
21090      cp_parser_skip_to_end_of_block_or_statement (parser);
21091    }
21092}
21093
21094/* Parse an Objective-C try-catch-finally statement.
21095
21096   objc-try-catch-finally-stmt:
21097     @try compound-statement objc-catch-clause-seq [opt]
21098       objc-finally-clause [opt]
21099
21100   objc-catch-clause-seq:
21101     objc-catch-clause objc-catch-clause-seq [opt]
21102
21103   objc-catch-clause:
21104     @catch ( exception-declaration ) compound-statement
21105
21106   objc-finally-clause
21107     @finally compound-statement
21108
21109   Returns NULL_TREE.  */
21110
21111static tree
21112cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21113  location_t location;
21114  tree stmt;
21115
21116  cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21117  location = cp_lexer_peek_token (parser->lexer)->location;
21118  /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21119     node, lest it get absorbed into the surrounding block.  */
21120  stmt = push_stmt_list ();
21121  cp_parser_compound_statement (parser, NULL, false);
21122  objc_begin_try_stmt (location, pop_stmt_list (stmt));
21123
21124  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21125    {
21126      cp_parameter_declarator *parmdecl;
21127      tree parm;
21128
21129      cp_lexer_consume_token (parser->lexer);
21130      cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21131      parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21132      parm = grokdeclarator (parmdecl->declarator,
21133			     &parmdecl->decl_specifiers,
21134			     PARM, /*initialized=*/0,
21135			     /*attrlist=*/NULL);
21136      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21137      objc_begin_catch_clause (parm);
21138      cp_parser_compound_statement (parser, NULL, false);
21139      objc_finish_catch_clause ();
21140    }
21141
21142  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21143    {
21144      cp_lexer_consume_token (parser->lexer);
21145      location = cp_lexer_peek_token (parser->lexer)->location;
21146      /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21147	 node, lest it get absorbed into the surrounding block.  */
21148      stmt = push_stmt_list ();
21149      cp_parser_compound_statement (parser, NULL, false);
21150      objc_build_finally_clause (location, pop_stmt_list (stmt));
21151    }
21152
21153  return objc_finish_try_stmt ();
21154}
21155
21156/* Parse an Objective-C synchronized statement.
21157
21158   objc-synchronized-stmt:
21159     @synchronized ( expression ) compound-statement
21160
21161   Returns NULL_TREE.  */
21162
21163static tree
21164cp_parser_objc_synchronized_statement (cp_parser *parser) {
21165  location_t location;
21166  tree lock, stmt;
21167
21168  cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21169
21170  location = cp_lexer_peek_token (parser->lexer)->location;
21171  cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21172  lock = cp_parser_expression (parser, false, NULL);
21173  cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21174
21175  /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21176     node, lest it get absorbed into the surrounding block.  */
21177  stmt = push_stmt_list ();
21178  cp_parser_compound_statement (parser, NULL, false);
21179
21180  return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21181}
21182
21183/* Parse an Objective-C throw statement.
21184
21185   objc-throw-stmt:
21186     @throw assignment-expression [opt] ;
21187
21188   Returns a constructed '@throw' statement.  */
21189
21190static tree
21191cp_parser_objc_throw_statement (cp_parser *parser) {
21192  tree expr = NULL_TREE;
21193  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21194
21195  cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21196
21197  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21198    expr = cp_parser_assignment_expression (parser, false, NULL);
21199
21200  cp_parser_consume_semicolon_at_end_of_statement (parser);
21201
21202  return objc_build_throw_stmt (loc, expr);
21203}
21204
21205/* Parse an Objective-C statement.  */
21206
21207static tree
21208cp_parser_objc_statement (cp_parser * parser) {
21209  /* Try to figure out what kind of declaration is present.  */
21210  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21211
21212  switch (kwd->keyword)
21213    {
21214    case RID_AT_TRY:
21215      return cp_parser_objc_try_catch_finally_statement (parser);
21216    case RID_AT_SYNCHRONIZED:
21217      return cp_parser_objc_synchronized_statement (parser);
21218    case RID_AT_THROW:
21219      return cp_parser_objc_throw_statement (parser);
21220    default:
21221      error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21222	       kwd->u.value);
21223      cp_parser_skip_to_end_of_block_or_statement (parser);
21224    }
21225
21226  return error_mark_node;
21227}
21228
21229/* OpenMP 2.5 parsing routines.  */
21230
21231/* Returns name of the next clause.
21232   If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21233   the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21234   returned and the token is consumed.  */
21235
21236static pragma_omp_clause
21237cp_parser_omp_clause_name (cp_parser *parser)
21238{
21239  pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21240
21241  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21242    result = PRAGMA_OMP_CLAUSE_IF;
21243  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21244    result = PRAGMA_OMP_CLAUSE_DEFAULT;
21245  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21246    result = PRAGMA_OMP_CLAUSE_PRIVATE;
21247  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21248    {
21249      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21250      const char *p = IDENTIFIER_POINTER (id);
21251
21252      switch (p[0])
21253	{
21254	case 'c':
21255	  if (!strcmp ("collapse", p))
21256	    result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21257	  else if (!strcmp ("copyin", p))
21258	    result = PRAGMA_OMP_CLAUSE_COPYIN;
21259	  else if (!strcmp ("copyprivate", p))
21260	    result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21261	  break;
21262	case 'f':
21263	  if (!strcmp ("firstprivate", p))
21264	    result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21265	  break;
21266	case 'l':
21267	  if (!strcmp ("lastprivate", p))
21268	    result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21269	  break;
21270	case 'n':
21271	  if (!strcmp ("nowait", p))
21272	    result = PRAGMA_OMP_CLAUSE_NOWAIT;
21273	  else if (!strcmp ("num_threads", p))
21274	    result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21275	  break;
21276	case 'o':
21277	  if (!strcmp ("ordered", p))
21278	    result = PRAGMA_OMP_CLAUSE_ORDERED;
21279	  break;
21280	case 'r':
21281	  if (!strcmp ("reduction", p))
21282	    result = PRAGMA_OMP_CLAUSE_REDUCTION;
21283	  break;
21284	case 's':
21285	  if (!strcmp ("schedule", p))
21286	    result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21287	  else if (!strcmp ("shared", p))
21288	    result = PRAGMA_OMP_CLAUSE_SHARED;
21289	  break;
21290	case 'u':
21291	  if (!strcmp ("untied", p))
21292	    result = PRAGMA_OMP_CLAUSE_UNTIED;
21293	  break;
21294	}
21295    }
21296
21297  if (result != PRAGMA_OMP_CLAUSE_NONE)
21298    cp_lexer_consume_token (parser->lexer);
21299
21300  return result;
21301}
21302
21303/* Validate that a clause of the given type does not already exist.  */
21304
21305static void
21306check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21307			   const char *name, location_t location)
21308{
21309  tree c;
21310
21311  for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21312    if (OMP_CLAUSE_CODE (c) == code)
21313      {
21314	error_at (location, "too many %qs clauses", name);
21315	break;
21316      }
21317}
21318
21319/* OpenMP 2.5:
21320   variable-list:
21321     identifier
21322     variable-list , identifier
21323
21324   In addition, we match a closing parenthesis.  An opening parenthesis
21325   will have been consumed by the caller.
21326
21327   If KIND is nonzero, create the appropriate node and install the decl
21328   in OMP_CLAUSE_DECL and add the node to the head of the list.
21329
21330   If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21331   return the list created.  */
21332
21333static tree
21334cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21335				tree list)
21336{
21337  cp_token *token;
21338  while (1)
21339    {
21340      tree name, decl;
21341
21342      token = cp_lexer_peek_token (parser->lexer);
21343      name = cp_parser_id_expression (parser, /*template_p=*/false,
21344				      /*check_dependency_p=*/true,
21345				      /*template_p=*/NULL,
21346				      /*declarator_p=*/false,
21347				      /*optional_p=*/false);
21348      if (name == error_mark_node)
21349	goto skip_comma;
21350
21351      decl = cp_parser_lookup_name_simple (parser, name, token->location);
21352      if (decl == error_mark_node)
21353	cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21354      else if (kind != 0)
21355	{
21356	  tree u = build_omp_clause (token->location, kind);
21357	  OMP_CLAUSE_DECL (u) = decl;
21358	  OMP_CLAUSE_CHAIN (u) = list;
21359	  list = u;
21360	}
21361      else
21362	list = tree_cons (decl, NULL_TREE, list);
21363
21364    get_comma:
21365      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21366	break;
21367      cp_lexer_consume_token (parser->lexer);
21368    }
21369
21370  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21371    {
21372      int ending;
21373
21374      /* Try to resync to an unnested comma.  Copied from
21375	 cp_parser_parenthesized_expression_list.  */
21376    skip_comma:
21377      ending = cp_parser_skip_to_closing_parenthesis (parser,
21378						      /*recovering=*/true,
21379						      /*or_comma=*/true,
21380						      /*consume_paren=*/true);
21381      if (ending < 0)
21382	goto get_comma;
21383    }
21384
21385  return list;
21386}
21387
21388/* Similarly, but expect leading and trailing parenthesis.  This is a very
21389   common case for omp clauses.  */
21390
21391static tree
21392cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21393{
21394  if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21395    return cp_parser_omp_var_list_no_open (parser, kind, list);
21396  return list;
21397}
21398
21399/* OpenMP 3.0:
21400   collapse ( constant-expression ) */
21401
21402static tree
21403cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21404{
21405  tree c, num;
21406  location_t loc;
21407  HOST_WIDE_INT n;
21408
21409  loc = cp_lexer_peek_token (parser->lexer)->location;
21410  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21411    return list;
21412
21413  num = cp_parser_constant_expression (parser, false, NULL);
21414
21415  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21416    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21417					   /*or_comma=*/false,
21418					   /*consume_paren=*/true);
21419
21420  if (num == error_mark_node)
21421    return list;
21422  num = fold_non_dependent_expr (num);
21423  if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21424      || !host_integerp (num, 0)
21425      || (n = tree_low_cst (num, 0)) <= 0
21426      || (int) n != n)
21427    {
21428      error_at (loc, "collapse argument needs positive constant integer expression");
21429      return list;
21430    }
21431
21432  check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21433  c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21434  OMP_CLAUSE_CHAIN (c) = list;
21435  OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21436
21437  return c;
21438}
21439
21440/* OpenMP 2.5:
21441   default ( shared | none ) */
21442
21443static tree
21444cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21445{
21446  enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21447  tree c;
21448
21449  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21450    return list;
21451  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21452    {
21453      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21454      const char *p = IDENTIFIER_POINTER (id);
21455
21456      switch (p[0])
21457	{
21458	case 'n':
21459	  if (strcmp ("none", p) != 0)
21460	    goto invalid_kind;
21461	  kind = OMP_CLAUSE_DEFAULT_NONE;
21462	  break;
21463
21464	case 's':
21465	  if (strcmp ("shared", p) != 0)
21466	    goto invalid_kind;
21467	  kind = OMP_CLAUSE_DEFAULT_SHARED;
21468	  break;
21469
21470	default:
21471	  goto invalid_kind;
21472	}
21473
21474      cp_lexer_consume_token (parser->lexer);
21475    }
21476  else
21477    {
21478    invalid_kind:
21479      cp_parser_error (parser, "expected %<none%> or %<shared%>");
21480    }
21481
21482  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21483    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21484					   /*or_comma=*/false,
21485					   /*consume_paren=*/true);
21486
21487  if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21488    return list;
21489
21490  check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21491  c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21492  OMP_CLAUSE_CHAIN (c) = list;
21493  OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21494
21495  return c;
21496}
21497
21498/* OpenMP 2.5:
21499   if ( expression ) */
21500
21501static tree
21502cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21503{
21504  tree t, c;
21505
21506  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21507    return list;
21508
21509  t = cp_parser_condition (parser);
21510
21511  if (t == error_mark_node
21512      || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21513    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21514					   /*or_comma=*/false,
21515					   /*consume_paren=*/true);
21516
21517  check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21518
21519  c = build_omp_clause (location, OMP_CLAUSE_IF);
21520  OMP_CLAUSE_IF_EXPR (c) = t;
21521  OMP_CLAUSE_CHAIN (c) = list;
21522
21523  return c;
21524}
21525
21526/* OpenMP 2.5:
21527   nowait */
21528
21529static tree
21530cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21531			     tree list, location_t location)
21532{
21533  tree c;
21534
21535  check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21536
21537  c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21538  OMP_CLAUSE_CHAIN (c) = list;
21539  return c;
21540}
21541
21542/* OpenMP 2.5:
21543   num_threads ( expression ) */
21544
21545static tree
21546cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21547				  location_t location)
21548{
21549  tree t, c;
21550
21551  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21552    return list;
21553
21554  t = cp_parser_expression (parser, false, NULL);
21555
21556  if (t == error_mark_node
21557      || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21558    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21559					   /*or_comma=*/false,
21560					   /*consume_paren=*/true);
21561
21562  check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21563			     "num_threads", location);
21564
21565  c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21566  OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21567  OMP_CLAUSE_CHAIN (c) = list;
21568
21569  return c;
21570}
21571
21572/* OpenMP 2.5:
21573   ordered */
21574
21575static tree
21576cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21577			      tree list, location_t location)
21578{
21579  tree c;
21580
21581  check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21582			     "ordered", location);
21583
21584  c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21585  OMP_CLAUSE_CHAIN (c) = list;
21586  return c;
21587}
21588
21589/* OpenMP 2.5:
21590   reduction ( reduction-operator : variable-list )
21591
21592   reduction-operator:
21593     One of: + * - & ^ | && || */
21594
21595static tree
21596cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21597{
21598  enum tree_code code;
21599  tree nlist, c;
21600
21601  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21602    return list;
21603
21604  switch (cp_lexer_peek_token (parser->lexer)->type)
21605    {
21606    case CPP_PLUS:
21607      code = PLUS_EXPR;
21608      break;
21609    case CPP_MULT:
21610      code = MULT_EXPR;
21611      break;
21612    case CPP_MINUS:
21613      code = MINUS_EXPR;
21614      break;
21615    case CPP_AND:
21616      code = BIT_AND_EXPR;
21617      break;
21618    case CPP_XOR:
21619      code = BIT_XOR_EXPR;
21620      break;
21621    case CPP_OR:
21622      code = BIT_IOR_EXPR;
21623      break;
21624    case CPP_AND_AND:
21625      code = TRUTH_ANDIF_EXPR;
21626      break;
21627    case CPP_OR_OR:
21628      code = TRUTH_ORIF_EXPR;
21629      break;
21630    default:
21631      cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21632			       "%<|%>, %<&&%>, or %<||%>");
21633    resync_fail:
21634      cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21635					     /*or_comma=*/false,
21636					     /*consume_paren=*/true);
21637      return list;
21638    }
21639  cp_lexer_consume_token (parser->lexer);
21640
21641  if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21642    goto resync_fail;
21643
21644  nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21645  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21646    OMP_CLAUSE_REDUCTION_CODE (c) = code;
21647
21648  return nlist;
21649}
21650
21651/* OpenMP 2.5:
21652   schedule ( schedule-kind )
21653   schedule ( schedule-kind , expression )
21654
21655   schedule-kind:
21656     static | dynamic | guided | runtime | auto  */
21657
21658static tree
21659cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21660{
21661  tree c, t;
21662
21663  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21664    return list;
21665
21666  c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21667
21668  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21669    {
21670      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21671      const char *p = IDENTIFIER_POINTER (id);
21672
21673      switch (p[0])
21674	{
21675	case 'd':
21676	  if (strcmp ("dynamic", p) != 0)
21677	    goto invalid_kind;
21678	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21679	  break;
21680
21681	case 'g':
21682	  if (strcmp ("guided", p) != 0)
21683	    goto invalid_kind;
21684	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21685	  break;
21686
21687	case 'r':
21688	  if (strcmp ("runtime", p) != 0)
21689	    goto invalid_kind;
21690	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21691	  break;
21692
21693	default:
21694	  goto invalid_kind;
21695	}
21696    }
21697  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21698    OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21699  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21700    OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21701  else
21702    goto invalid_kind;
21703  cp_lexer_consume_token (parser->lexer);
21704
21705  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21706    {
21707      cp_token *token;
21708      cp_lexer_consume_token (parser->lexer);
21709
21710      token = cp_lexer_peek_token (parser->lexer);
21711      t = cp_parser_assignment_expression (parser, false, NULL);
21712
21713      if (t == error_mark_node)
21714	goto resync_fail;
21715      else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21716	error_at (token->location, "schedule %<runtime%> does not take "
21717		  "a %<chunk_size%> parameter");
21718      else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21719	error_at (token->location, "schedule %<auto%> does not take "
21720		  "a %<chunk_size%> parameter");
21721      else
21722	OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21723
21724      if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21725	goto resync_fail;
21726    }
21727  else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21728    goto resync_fail;
21729
21730  check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21731  OMP_CLAUSE_CHAIN (c) = list;
21732  return c;
21733
21734 invalid_kind:
21735  cp_parser_error (parser, "invalid schedule kind");
21736 resync_fail:
21737  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21738					 /*or_comma=*/false,
21739					 /*consume_paren=*/true);
21740  return list;
21741}
21742
21743/* OpenMP 3.0:
21744   untied */
21745
21746static tree
21747cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21748			     tree list, location_t location)
21749{
21750  tree c;
21751
21752  check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21753
21754  c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21755  OMP_CLAUSE_CHAIN (c) = list;
21756  return c;
21757}
21758
21759/* Parse all OpenMP clauses.  The set clauses allowed by the directive
21760   is a bitmask in MASK.  Return the list of clauses found; the result
21761   of clause default goes in *pdefault.  */
21762
21763static tree
21764cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21765			   const char *where, cp_token *pragma_tok)
21766{
21767  tree clauses = NULL;
21768  bool first = true;
21769  cp_token *token = NULL;
21770
21771  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21772    {
21773      pragma_omp_clause c_kind;
21774      const char *c_name;
21775      tree prev = clauses;
21776
21777      if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21778	cp_lexer_consume_token (parser->lexer);
21779
21780      token = cp_lexer_peek_token (parser->lexer);
21781      c_kind = cp_parser_omp_clause_name (parser);
21782      first = false;
21783
21784      switch (c_kind)
21785	{
21786	case PRAGMA_OMP_CLAUSE_COLLAPSE:
21787	  clauses = cp_parser_omp_clause_collapse (parser, clauses,
21788						   token->location);
21789	  c_name = "collapse";
21790	  break;
21791	case PRAGMA_OMP_CLAUSE_COPYIN:
21792	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21793	  c_name = "copyin";
21794	  break;
21795	case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21796	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21797					    clauses);
21798	  c_name = "copyprivate";
21799	  break;
21800	case PRAGMA_OMP_CLAUSE_DEFAULT:
21801	  clauses = cp_parser_omp_clause_default (parser, clauses,
21802						  token->location);
21803	  c_name = "default";
21804	  break;
21805	case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21806	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21807					    clauses);
21808	  c_name = "firstprivate";
21809	  break;
21810	case PRAGMA_OMP_CLAUSE_IF:
21811	  clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21812	  c_name = "if";
21813	  break;
21814	case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21815	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21816					    clauses);
21817	  c_name = "lastprivate";
21818	  break;
21819	case PRAGMA_OMP_CLAUSE_NOWAIT:
21820	  clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21821	  c_name = "nowait";
21822	  break;
21823	case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21824	  clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21825						      token->location);
21826	  c_name = "num_threads";
21827	  break;
21828	case PRAGMA_OMP_CLAUSE_ORDERED:
21829	  clauses = cp_parser_omp_clause_ordered (parser, clauses,
21830						  token->location);
21831	  c_name = "ordered";
21832	  break;
21833	case PRAGMA_OMP_CLAUSE_PRIVATE:
21834	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21835					    clauses);
21836	  c_name = "private";
21837	  break;
21838	case PRAGMA_OMP_CLAUSE_REDUCTION:
21839	  clauses = cp_parser_omp_clause_reduction (parser, clauses);
21840	  c_name = "reduction";
21841	  break;
21842	case PRAGMA_OMP_CLAUSE_SCHEDULE:
21843	  clauses = cp_parser_omp_clause_schedule (parser, clauses,
21844						   token->location);
21845	  c_name = "schedule";
21846	  break;
21847	case PRAGMA_OMP_CLAUSE_SHARED:
21848	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21849					    clauses);
21850	  c_name = "shared";
21851	  break;
21852	case PRAGMA_OMP_CLAUSE_UNTIED:
21853	  clauses = cp_parser_omp_clause_untied (parser, clauses,
21854						 token->location);
21855	  c_name = "nowait";
21856	  break;
21857	default:
21858	  cp_parser_error (parser, "expected %<#pragma omp%> clause");
21859	  goto saw_error;
21860	}
21861
21862      if (((mask >> c_kind) & 1) == 0)
21863	{
21864	  /* Remove the invalid clause(s) from the list to avoid
21865	     confusing the rest of the compiler.  */
21866	  clauses = prev;
21867	  error_at (token->location, "%qs is not valid for %qs", c_name, where);
21868	}
21869    }
21870 saw_error:
21871  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21872  return finish_omp_clauses (clauses);
21873}
21874
21875/* OpenMP 2.5:
21876   structured-block:
21877     statement
21878
21879   In practice, we're also interested in adding the statement to an
21880   outer node.  So it is convenient if we work around the fact that
21881   cp_parser_statement calls add_stmt.  */
21882
21883static unsigned
21884cp_parser_begin_omp_structured_block (cp_parser *parser)
21885{
21886  unsigned save = parser->in_statement;
21887
21888  /* Only move the values to IN_OMP_BLOCK if they weren't false.
21889     This preserves the "not within loop or switch" style error messages
21890     for nonsense cases like
21891	void foo() {
21892	#pragma omp single
21893	  break;
21894	}
21895  */
21896  if (parser->in_statement)
21897    parser->in_statement = IN_OMP_BLOCK;
21898
21899  return save;
21900}
21901
21902static void
21903cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21904{
21905  parser->in_statement = save;
21906}
21907
21908static tree
21909cp_parser_omp_structured_block (cp_parser *parser)
21910{
21911  tree stmt = begin_omp_structured_block ();
21912  unsigned int save = cp_parser_begin_omp_structured_block (parser);
21913
21914  cp_parser_statement (parser, NULL_TREE, false, NULL);
21915
21916  cp_parser_end_omp_structured_block (parser, save);
21917  return finish_omp_structured_block (stmt);
21918}
21919
21920/* OpenMP 2.5:
21921   # pragma omp atomic new-line
21922     expression-stmt
21923
21924   expression-stmt:
21925     x binop= expr | x++ | ++x | x-- | --x
21926   binop:
21927     +, *, -, /, &, ^, |, <<, >>
21928
21929  where x is an lvalue expression with scalar type.  */
21930
21931static void
21932cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21933{
21934  tree lhs, rhs;
21935  enum tree_code code;
21936
21937  cp_parser_require_pragma_eol (parser, pragma_tok);
21938
21939  lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21940				    /*cast_p=*/false, NULL);
21941  switch (TREE_CODE (lhs))
21942    {
21943    case ERROR_MARK:
21944      goto saw_error;
21945
21946    case PREINCREMENT_EXPR:
21947    case POSTINCREMENT_EXPR:
21948      lhs = TREE_OPERAND (lhs, 0);
21949      code = PLUS_EXPR;
21950      rhs = integer_one_node;
21951      break;
21952
21953    case PREDECREMENT_EXPR:
21954    case POSTDECREMENT_EXPR:
21955      lhs = TREE_OPERAND (lhs, 0);
21956      code = MINUS_EXPR;
21957      rhs = integer_one_node;
21958      break;
21959
21960    case COMPOUND_EXPR:
21961      if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
21962	 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
21963	 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
21964	 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
21965	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
21966					     (TREE_OPERAND (lhs, 1), 0), 0)))
21967	    == BOOLEAN_TYPE)
21968       /* Undo effects of boolean_increment for post {in,de}crement.  */
21969       lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
21970      /* FALLTHRU */
21971    case MODIFY_EXPR:
21972      if (TREE_CODE (lhs) == MODIFY_EXPR
21973	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
21974       {
21975	 /* Undo effects of boolean_increment.  */
21976	 if (integer_onep (TREE_OPERAND (lhs, 1)))
21977	   {
21978	     /* This is pre or post increment.  */
21979	     rhs = TREE_OPERAND (lhs, 1);
21980	     lhs = TREE_OPERAND (lhs, 0);
21981	     code = NOP_EXPR;
21982	     break;
21983	   }
21984       }
21985      /* FALLTHRU */
21986    default:
21987      switch (cp_lexer_peek_token (parser->lexer)->type)
21988	{
21989	case CPP_MULT_EQ:
21990	  code = MULT_EXPR;
21991	  break;
21992	case CPP_DIV_EQ:
21993	  code = TRUNC_DIV_EXPR;
21994	  break;
21995	case CPP_PLUS_EQ:
21996	  code = PLUS_EXPR;
21997	  break;
21998	case CPP_MINUS_EQ:
21999	  code = MINUS_EXPR;
22000	  break;
22001	case CPP_LSHIFT_EQ:
22002	  code = LSHIFT_EXPR;
22003	  break;
22004	case CPP_RSHIFT_EQ:
22005	  code = RSHIFT_EXPR;
22006	  break;
22007	case CPP_AND_EQ:
22008	  code = BIT_AND_EXPR;
22009	  break;
22010	case CPP_OR_EQ:
22011	  code = BIT_IOR_EXPR;
22012	  break;
22013	case CPP_XOR_EQ:
22014	  code = BIT_XOR_EXPR;
22015	  break;
22016	default:
22017	  cp_parser_error (parser,
22018			   "invalid operator for %<#pragma omp atomic%>");
22019	  goto saw_error;
22020	}
22021      cp_lexer_consume_token (parser->lexer);
22022
22023      rhs = cp_parser_expression (parser, false, NULL);
22024      if (rhs == error_mark_node)
22025	goto saw_error;
22026      break;
22027    }
22028  finish_omp_atomic (code, lhs, rhs);
22029  cp_parser_consume_semicolon_at_end_of_statement (parser);
22030  return;
22031
22032 saw_error:
22033  cp_parser_skip_to_end_of_block_or_statement (parser);
22034}
22035
22036
22037/* OpenMP 2.5:
22038   # pragma omp barrier new-line  */
22039
22040static void
22041cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
22042{
22043  cp_parser_require_pragma_eol (parser, pragma_tok);
22044  finish_omp_barrier ();
22045}
22046
22047/* OpenMP 2.5:
22048   # pragma omp critical [(name)] new-line
22049     structured-block  */
22050
22051static tree
22052cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22053{
22054  tree stmt, name = NULL;
22055
22056  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22057    {
22058      cp_lexer_consume_token (parser->lexer);
22059
22060      name = cp_parser_identifier (parser);
22061
22062      if (name == error_mark_node
22063	  || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22064	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22065					       /*or_comma=*/false,
22066					       /*consume_paren=*/true);
22067      if (name == error_mark_node)
22068	name = NULL;
22069    }
22070  cp_parser_require_pragma_eol (parser, pragma_tok);
22071
22072  stmt = cp_parser_omp_structured_block (parser);
22073  return c_finish_omp_critical (input_location, stmt, name);
22074}
22075
22076/* OpenMP 2.5:
22077   # pragma omp flush flush-vars[opt] new-line
22078
22079   flush-vars:
22080     ( variable-list ) */
22081
22082static void
22083cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22084{
22085  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22086    (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22087  cp_parser_require_pragma_eol (parser, pragma_tok);
22088
22089  finish_omp_flush ();
22090}
22091
22092/* Helper function, to parse omp for increment expression.  */
22093
22094static tree
22095cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22096{
22097  tree cond = cp_parser_binary_expression (parser, false, true,
22098					   PREC_NOT_OPERATOR, NULL);
22099  bool overloaded_p;
22100
22101  if (cond == error_mark_node
22102      || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22103    {
22104      cp_parser_skip_to_end_of_statement (parser);
22105      return error_mark_node;
22106    }
22107
22108  switch (TREE_CODE (cond))
22109    {
22110    case GT_EXPR:
22111    case GE_EXPR:
22112    case LT_EXPR:
22113    case LE_EXPR:
22114      break;
22115    default:
22116      return error_mark_node;
22117    }
22118
22119  /* If decl is an iterator, preserve LHS and RHS of the relational
22120     expr until finish_omp_for.  */
22121  if (decl
22122      && (type_dependent_expression_p (decl)
22123	  || CLASS_TYPE_P (TREE_TYPE (decl))))
22124    return cond;
22125
22126  return build_x_binary_op (TREE_CODE (cond),
22127			    TREE_OPERAND (cond, 0), ERROR_MARK,
22128			    TREE_OPERAND (cond, 1), ERROR_MARK,
22129			    &overloaded_p, tf_warning_or_error);
22130}
22131
22132/* Helper function, to parse omp for increment expression.  */
22133
22134static tree
22135cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22136{
22137  cp_token *token = cp_lexer_peek_token (parser->lexer);
22138  enum tree_code op;
22139  tree lhs, rhs;
22140  cp_id_kind idk;
22141  bool decl_first;
22142
22143  if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22144    {
22145      op = (token->type == CPP_PLUS_PLUS
22146	    ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22147      cp_lexer_consume_token (parser->lexer);
22148      lhs = cp_parser_cast_expression (parser, false, false, NULL);
22149      if (lhs != decl)
22150	return error_mark_node;
22151      return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22152    }
22153
22154  lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22155  if (lhs != decl)
22156    return error_mark_node;
22157
22158  token = cp_lexer_peek_token (parser->lexer);
22159  if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22160    {
22161      op = (token->type == CPP_PLUS_PLUS
22162	    ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22163      cp_lexer_consume_token (parser->lexer);
22164      return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22165    }
22166
22167  op = cp_parser_assignment_operator_opt (parser);
22168  if (op == ERROR_MARK)
22169    return error_mark_node;
22170
22171  if (op != NOP_EXPR)
22172    {
22173      rhs = cp_parser_assignment_expression (parser, false, NULL);
22174      rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22175      return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22176    }
22177
22178  lhs = cp_parser_binary_expression (parser, false, false,
22179				     PREC_ADDITIVE_EXPRESSION, NULL);
22180  token = cp_lexer_peek_token (parser->lexer);
22181  decl_first = lhs == decl;
22182  if (decl_first)
22183    lhs = NULL_TREE;
22184  if (token->type != CPP_PLUS
22185      && token->type != CPP_MINUS)
22186    return error_mark_node;
22187
22188  do
22189    {
22190      op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22191      cp_lexer_consume_token (parser->lexer);
22192      rhs = cp_parser_binary_expression (parser, false, false,
22193					 PREC_ADDITIVE_EXPRESSION, NULL);
22194      token = cp_lexer_peek_token (parser->lexer);
22195      if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22196	{
22197	  if (lhs == NULL_TREE)
22198	    {
22199	      if (op == PLUS_EXPR)
22200		lhs = rhs;
22201	      else
22202		lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22203	    }
22204	  else
22205	    lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22206				     NULL, tf_warning_or_error);
22207	}
22208    }
22209  while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22210
22211  if (!decl_first)
22212    {
22213      if (rhs != decl || op == MINUS_EXPR)
22214	return error_mark_node;
22215      rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22216    }
22217  else
22218    rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22219
22220  return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22221}
22222
22223/* Parse the restricted form of the for statement allowed by OpenMP.  */
22224
22225static tree
22226cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22227{
22228  tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22229  tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22230  tree this_pre_body, cl;
22231  location_t loc_first;
22232  bool collapse_err = false;
22233  int i, collapse = 1, nbraces = 0;
22234
22235  for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22236    if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22237      collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22238
22239  gcc_assert (collapse >= 1);
22240
22241  declv = make_tree_vec (collapse);
22242  initv = make_tree_vec (collapse);
22243  condv = make_tree_vec (collapse);
22244  incrv = make_tree_vec (collapse);
22245
22246  loc_first = cp_lexer_peek_token (parser->lexer)->location;
22247
22248  for (i = 0; i < collapse; i++)
22249    {
22250      int bracecount = 0;
22251      bool add_private_clause = false;
22252      location_t loc;
22253
22254      if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22255	{
22256	  cp_parser_error (parser, "for statement expected");
22257	  return NULL;
22258	}
22259      loc = cp_lexer_consume_token (parser->lexer)->location;
22260
22261      if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22262	return NULL;
22263
22264      init = decl = real_decl = NULL;
22265      this_pre_body = push_stmt_list ();
22266      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22267	{
22268	  /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22269
22270	     init-expr:
22271	               var = lb
22272		       integer-type var = lb
22273		       random-access-iterator-type var = lb
22274		       pointer-type var = lb
22275	  */
22276	  cp_decl_specifier_seq type_specifiers;
22277
22278	  /* First, try to parse as an initialized declaration.  See
22279	     cp_parser_condition, from whence the bulk of this is copied.  */
22280
22281	  cp_parser_parse_tentatively (parser);
22282	  cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22283					/*is_trailing_return=*/false,
22284					&type_specifiers);
22285	  if (cp_parser_parse_definitely (parser))
22286	    {
22287	      /* If parsing a type specifier seq succeeded, then this
22288		 MUST be a initialized declaration.  */
22289	      tree asm_specification, attributes;
22290	      cp_declarator *declarator;
22291
22292	      declarator = cp_parser_declarator (parser,
22293						 CP_PARSER_DECLARATOR_NAMED,
22294						 /*ctor_dtor_or_conv_p=*/NULL,
22295						 /*parenthesized_p=*/NULL,
22296						 /*member_p=*/false);
22297	      attributes = cp_parser_attributes_opt (parser);
22298	      asm_specification = cp_parser_asm_specification_opt (parser);
22299
22300	      if (declarator == cp_error_declarator)
22301		cp_parser_skip_to_end_of_statement (parser);
22302
22303	      else
22304		{
22305		  tree pushed_scope, auto_node;
22306
22307		  decl = start_decl (declarator, &type_specifiers,
22308				     SD_INITIALIZED, attributes,
22309				     /*prefix_attributes=*/NULL_TREE,
22310				     &pushed_scope);
22311
22312		  auto_node = type_uses_auto (TREE_TYPE (decl));
22313		  if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22314		    {
22315		      if (cp_lexer_next_token_is (parser->lexer,
22316						  CPP_OPEN_PAREN))
22317			error ("parenthesized initialization is not allowed in "
22318			       "OpenMP %<for%> loop");
22319		      else
22320			/* Trigger an error.  */
22321			cp_parser_require (parser, CPP_EQ, "%<=%>");
22322
22323		      init = error_mark_node;
22324		      cp_parser_skip_to_end_of_statement (parser);
22325		    }
22326		  else if (CLASS_TYPE_P (TREE_TYPE (decl))
22327			   || type_dependent_expression_p (decl)
22328			   || auto_node)
22329		    {
22330		      bool is_direct_init, is_non_constant_init;
22331
22332		      init = cp_parser_initializer (parser,
22333						    &is_direct_init,
22334						    &is_non_constant_init);
22335
22336		      if (auto_node && describable_type (init))
22337			{
22338			  TREE_TYPE (decl)
22339			    = do_auto_deduction (TREE_TYPE (decl), init,
22340						 auto_node);
22341
22342			  if (!CLASS_TYPE_P (TREE_TYPE (decl))
22343			      && !type_dependent_expression_p (decl))
22344			    goto non_class;
22345			}
22346
22347		      cp_finish_decl (decl, init, !is_non_constant_init,
22348				      asm_specification,
22349				      LOOKUP_ONLYCONVERTING);
22350		      if (CLASS_TYPE_P (TREE_TYPE (decl)))
22351			{
22352			  for_block
22353			    = tree_cons (NULL, this_pre_body, for_block);
22354			  init = NULL_TREE;
22355			}
22356		      else
22357			init = pop_stmt_list (this_pre_body);
22358		      this_pre_body = NULL_TREE;
22359		    }
22360		  else
22361		    {
22362		      /* Consume '='.  */
22363		      cp_lexer_consume_token (parser->lexer);
22364		      init = cp_parser_assignment_expression (parser, false, NULL);
22365
22366		    non_class:
22367		      if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22368			init = error_mark_node;
22369		      else
22370			cp_finish_decl (decl, NULL_TREE,
22371					/*init_const_expr_p=*/false,
22372					asm_specification,
22373					LOOKUP_ONLYCONVERTING);
22374		    }
22375
22376		  if (pushed_scope)
22377		    pop_scope (pushed_scope);
22378		}
22379	    }
22380	  else
22381	    {
22382	      cp_id_kind idk;
22383	      /* If parsing a type specifier sequence failed, then
22384		 this MUST be a simple expression.  */
22385	      cp_parser_parse_tentatively (parser);
22386	      decl = cp_parser_primary_expression (parser, false, false,
22387						   false, &idk);
22388	      if (!cp_parser_error_occurred (parser)
22389		  && decl
22390		  && DECL_P (decl)
22391		  && CLASS_TYPE_P (TREE_TYPE (decl)))
22392		{
22393		  tree rhs;
22394
22395		  cp_parser_parse_definitely (parser);
22396		  cp_parser_require (parser, CPP_EQ, "%<=%>");
22397		  rhs = cp_parser_assignment_expression (parser, false, NULL);
22398		  finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22399							 rhs,
22400							 tf_warning_or_error));
22401		  add_private_clause = true;
22402		}
22403	      else
22404		{
22405		  decl = NULL;
22406		  cp_parser_abort_tentative_parse (parser);
22407		  init = cp_parser_expression (parser, false, NULL);
22408		  if (init)
22409		    {
22410		      if (TREE_CODE (init) == MODIFY_EXPR
22411			  || TREE_CODE (init) == MODOP_EXPR)
22412			real_decl = TREE_OPERAND (init, 0);
22413		    }
22414		}
22415	    }
22416	}
22417      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22418      if (this_pre_body)
22419	{
22420	  this_pre_body = pop_stmt_list (this_pre_body);
22421	  if (pre_body)
22422	    {
22423	      tree t = pre_body;
22424	      pre_body = push_stmt_list ();
22425	      add_stmt (t);
22426	      add_stmt (this_pre_body);
22427	      pre_body = pop_stmt_list (pre_body);
22428	    }
22429	  else
22430	    pre_body = this_pre_body;
22431	}
22432
22433      if (decl)
22434	real_decl = decl;
22435      if (par_clauses != NULL && real_decl != NULL_TREE)
22436	{
22437	  tree *c;
22438	  for (c = par_clauses; *c ; )
22439	    if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22440		&& OMP_CLAUSE_DECL (*c) == real_decl)
22441	      {
22442		error_at (loc, "iteration variable %qD"
22443			  " should not be firstprivate", real_decl);
22444		*c = OMP_CLAUSE_CHAIN (*c);
22445	      }
22446	    else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22447		     && OMP_CLAUSE_DECL (*c) == real_decl)
22448	      {
22449		/* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22450		   change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22451		tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22452		OMP_CLAUSE_DECL (l) = real_decl;
22453		OMP_CLAUSE_CHAIN (l) = clauses;
22454		CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22455		clauses = l;
22456		OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22457		CP_OMP_CLAUSE_INFO (*c) = NULL;
22458		add_private_clause = false;
22459	      }
22460	    else
22461	      {
22462		if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22463		    && OMP_CLAUSE_DECL (*c) == real_decl)
22464		  add_private_clause = false;
22465		c = &OMP_CLAUSE_CHAIN (*c);
22466	      }
22467	}
22468
22469      if (add_private_clause)
22470	{
22471	  tree c;
22472	  for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22473	    {
22474	      if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22475		   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22476		  && OMP_CLAUSE_DECL (c) == decl)
22477		break;
22478	      else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22479		       && OMP_CLAUSE_DECL (c) == decl)
22480		error_at (loc, "iteration variable %qD "
22481			  "should not be firstprivate",
22482			  decl);
22483	      else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22484		       && OMP_CLAUSE_DECL (c) == decl)
22485		error_at (loc, "iteration variable %qD should not be reduction",
22486			  decl);
22487	    }
22488	  if (c == NULL)
22489	    {
22490	      c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22491	      OMP_CLAUSE_DECL (c) = decl;
22492	      c = finish_omp_clauses (c);
22493	      if (c)
22494		{
22495		  OMP_CLAUSE_CHAIN (c) = clauses;
22496		  clauses = c;
22497		}
22498	    }
22499	}
22500
22501      cond = NULL;
22502      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22503	cond = cp_parser_omp_for_cond (parser, decl);
22504      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22505
22506      incr = NULL;
22507      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22508	{
22509	  /* If decl is an iterator, preserve the operator on decl
22510	     until finish_omp_for.  */
22511	  if (decl
22512	      && (type_dependent_expression_p (decl)
22513		  || CLASS_TYPE_P (TREE_TYPE (decl))))
22514	    incr = cp_parser_omp_for_incr (parser, decl);
22515	  else
22516	    incr = cp_parser_expression (parser, false, NULL);
22517	}
22518
22519      if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22520	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22521					       /*or_comma=*/false,
22522					       /*consume_paren=*/true);
22523
22524      TREE_VEC_ELT (declv, i) = decl;
22525      TREE_VEC_ELT (initv, i) = init;
22526      TREE_VEC_ELT (condv, i) = cond;
22527      TREE_VEC_ELT (incrv, i) = incr;
22528
22529      if (i == collapse - 1)
22530	break;
22531
22532      /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22533	 in between the collapsed for loops to be still considered perfectly
22534	 nested.  Hopefully the final version clarifies this.
22535	 For now handle (multiple) {'s and empty statements.  */
22536      cp_parser_parse_tentatively (parser);
22537      do
22538	{
22539	  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22540	    break;
22541	  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22542	    {
22543	      cp_lexer_consume_token (parser->lexer);
22544	      bracecount++;
22545	    }
22546	  else if (bracecount
22547		   && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22548	    cp_lexer_consume_token (parser->lexer);
22549	  else
22550	    {
22551	      loc = cp_lexer_peek_token (parser->lexer)->location;
22552	      error_at (loc, "not enough collapsed for loops");
22553	      collapse_err = true;
22554	      cp_parser_abort_tentative_parse (parser);
22555	      declv = NULL_TREE;
22556	      break;
22557	    }
22558	}
22559      while (1);
22560
22561      if (declv)
22562	{
22563	  cp_parser_parse_definitely (parser);
22564	  nbraces += bracecount;
22565	}
22566    }
22567
22568  /* Note that we saved the original contents of this flag when we entered
22569     the structured block, and so we don't need to re-save it here.  */
22570  parser->in_statement = IN_OMP_FOR;
22571
22572  /* Note that the grammar doesn't call for a structured block here,
22573     though the loop as a whole is a structured block.  */
22574  body = push_stmt_list ();
22575  cp_parser_statement (parser, NULL_TREE, false, NULL);
22576  body = pop_stmt_list (body);
22577
22578  if (declv == NULL_TREE)
22579    ret = NULL_TREE;
22580  else
22581    ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22582			  pre_body, clauses);
22583
22584  while (nbraces)
22585    {
22586      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22587	{
22588	  cp_lexer_consume_token (parser->lexer);
22589	  nbraces--;
22590	}
22591      else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22592	cp_lexer_consume_token (parser->lexer);
22593      else
22594	{
22595	  if (!collapse_err)
22596	    {
22597	      error_at (cp_lexer_peek_token (parser->lexer)->location,
22598			"collapsed loops not perfectly nested");
22599	    }
22600	  collapse_err = true;
22601	  cp_parser_statement_seq_opt (parser, NULL);
22602	  if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22603	    break;
22604	}
22605    }
22606
22607  while (for_block)
22608    {
22609      add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22610      for_block = TREE_CHAIN (for_block);
22611    }
22612
22613  return ret;
22614}
22615
22616/* OpenMP 2.5:
22617   #pragma omp for for-clause[optseq] new-line
22618     for-loop  */
22619
22620#define OMP_FOR_CLAUSE_MASK				\
22621	( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
22622	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
22623	| (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)		\
22624	| (1u << PRAGMA_OMP_CLAUSE_REDUCTION)		\
22625	| (1u << PRAGMA_OMP_CLAUSE_ORDERED)		\
22626	| (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)		\
22627	| (1u << PRAGMA_OMP_CLAUSE_NOWAIT)		\
22628	| (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22629
22630static tree
22631cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22632{
22633  tree clauses, sb, ret;
22634  unsigned int save;
22635
22636  clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22637				       "#pragma omp for", pragma_tok);
22638
22639  sb = begin_omp_structured_block ();
22640  save = cp_parser_begin_omp_structured_block (parser);
22641
22642  ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22643
22644  cp_parser_end_omp_structured_block (parser, save);
22645  add_stmt (finish_omp_structured_block (sb));
22646
22647  return ret;
22648}
22649
22650/* OpenMP 2.5:
22651   # pragma omp master new-line
22652     structured-block  */
22653
22654static tree
22655cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22656{
22657  cp_parser_require_pragma_eol (parser, pragma_tok);
22658  return c_finish_omp_master (input_location,
22659			      cp_parser_omp_structured_block (parser));
22660}
22661
22662/* OpenMP 2.5:
22663   # pragma omp ordered new-line
22664     structured-block  */
22665
22666static tree
22667cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22668{
22669  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22670  cp_parser_require_pragma_eol (parser, pragma_tok);
22671  return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22672}
22673
22674/* OpenMP 2.5:
22675
22676   section-scope:
22677     { section-sequence }
22678
22679   section-sequence:
22680     section-directive[opt] structured-block
22681     section-sequence section-directive structured-block  */
22682
22683static tree
22684cp_parser_omp_sections_scope (cp_parser *parser)
22685{
22686  tree stmt, substmt;
22687  bool error_suppress = false;
22688  cp_token *tok;
22689
22690  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22691    return NULL_TREE;
22692
22693  stmt = push_stmt_list ();
22694
22695  if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22696    {
22697      unsigned save;
22698
22699      substmt = begin_omp_structured_block ();
22700      save = cp_parser_begin_omp_structured_block (parser);
22701
22702      while (1)
22703	{
22704	  cp_parser_statement (parser, NULL_TREE, false, NULL);
22705
22706	  tok = cp_lexer_peek_token (parser->lexer);
22707	  if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22708	    break;
22709	  if (tok->type == CPP_CLOSE_BRACE)
22710	    break;
22711	  if (tok->type == CPP_EOF)
22712	    break;
22713	}
22714
22715      cp_parser_end_omp_structured_block (parser, save);
22716      substmt = finish_omp_structured_block (substmt);
22717      substmt = build1 (OMP_SECTION, void_type_node, substmt);
22718      add_stmt (substmt);
22719    }
22720
22721  while (1)
22722    {
22723      tok = cp_lexer_peek_token (parser->lexer);
22724      if (tok->type == CPP_CLOSE_BRACE)
22725	break;
22726      if (tok->type == CPP_EOF)
22727	break;
22728
22729      if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22730	{
22731	  cp_lexer_consume_token (parser->lexer);
22732	  cp_parser_require_pragma_eol (parser, tok);
22733	  error_suppress = false;
22734	}
22735      else if (!error_suppress)
22736	{
22737	  cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22738	  error_suppress = true;
22739	}
22740
22741      substmt = cp_parser_omp_structured_block (parser);
22742      substmt = build1 (OMP_SECTION, void_type_node, substmt);
22743      add_stmt (substmt);
22744    }
22745  cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22746
22747  substmt = pop_stmt_list (stmt);
22748
22749  stmt = make_node (OMP_SECTIONS);
22750  TREE_TYPE (stmt) = void_type_node;
22751  OMP_SECTIONS_BODY (stmt) = substmt;
22752
22753  add_stmt (stmt);
22754  return stmt;
22755}
22756
22757/* OpenMP 2.5:
22758   # pragma omp sections sections-clause[optseq] newline
22759     sections-scope  */
22760
22761#define OMP_SECTIONS_CLAUSE_MASK			\
22762	( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
22763	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
22764	| (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)		\
22765	| (1u << PRAGMA_OMP_CLAUSE_REDUCTION)		\
22766	| (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22767
22768static tree
22769cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22770{
22771  tree clauses, ret;
22772
22773  clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22774				       "#pragma omp sections", pragma_tok);
22775
22776  ret = cp_parser_omp_sections_scope (parser);
22777  if (ret)
22778    OMP_SECTIONS_CLAUSES (ret) = clauses;
22779
22780  return ret;
22781}
22782
22783/* OpenMP 2.5:
22784   # pragma parallel parallel-clause new-line
22785   # pragma parallel for parallel-for-clause new-line
22786   # pragma parallel sections parallel-sections-clause new-line  */
22787
22788#define OMP_PARALLEL_CLAUSE_MASK			\
22789	( (1u << PRAGMA_OMP_CLAUSE_IF)			\
22790	| (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
22791	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
22792	| (1u << PRAGMA_OMP_CLAUSE_DEFAULT)		\
22793	| (1u << PRAGMA_OMP_CLAUSE_SHARED)		\
22794	| (1u << PRAGMA_OMP_CLAUSE_COPYIN)		\
22795	| (1u << PRAGMA_OMP_CLAUSE_REDUCTION)		\
22796	| (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22797
22798static tree
22799cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22800{
22801  enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22802  const char *p_name = "#pragma omp parallel";
22803  tree stmt, clauses, par_clause, ws_clause, block;
22804  unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22805  unsigned int save;
22806  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22807
22808  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22809    {
22810      cp_lexer_consume_token (parser->lexer);
22811      p_kind = PRAGMA_OMP_PARALLEL_FOR;
22812      p_name = "#pragma omp parallel for";
22813      mask |= OMP_FOR_CLAUSE_MASK;
22814      mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22815    }
22816  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22817    {
22818      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22819      const char *p = IDENTIFIER_POINTER (id);
22820      if (strcmp (p, "sections") == 0)
22821	{
22822	  cp_lexer_consume_token (parser->lexer);
22823	  p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22824	  p_name = "#pragma omp parallel sections";
22825	  mask |= OMP_SECTIONS_CLAUSE_MASK;
22826	  mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22827	}
22828    }
22829
22830  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22831  block = begin_omp_parallel ();
22832  save = cp_parser_begin_omp_structured_block (parser);
22833
22834  switch (p_kind)
22835    {
22836    case PRAGMA_OMP_PARALLEL:
22837      cp_parser_statement (parser, NULL_TREE, false, NULL);
22838      par_clause = clauses;
22839      break;
22840
22841    case PRAGMA_OMP_PARALLEL_FOR:
22842      c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22843      cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22844      break;
22845
22846    case PRAGMA_OMP_PARALLEL_SECTIONS:
22847      c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22848      stmt = cp_parser_omp_sections_scope (parser);
22849      if (stmt)
22850	OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22851      break;
22852
22853    default:
22854      gcc_unreachable ();
22855    }
22856
22857  cp_parser_end_omp_structured_block (parser, save);
22858  stmt = finish_omp_parallel (par_clause, block);
22859  if (p_kind != PRAGMA_OMP_PARALLEL)
22860    OMP_PARALLEL_COMBINED (stmt) = 1;
22861  return stmt;
22862}
22863
22864/* OpenMP 2.5:
22865   # pragma omp single single-clause[optseq] new-line
22866     structured-block  */
22867
22868#define OMP_SINGLE_CLAUSE_MASK				\
22869	( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
22870	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
22871	| (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)		\
22872	| (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22873
22874static tree
22875cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22876{
22877  tree stmt = make_node (OMP_SINGLE);
22878  TREE_TYPE (stmt) = void_type_node;
22879
22880  OMP_SINGLE_CLAUSES (stmt)
22881    = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22882				 "#pragma omp single", pragma_tok);
22883  OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22884
22885  return add_stmt (stmt);
22886}
22887
22888/* OpenMP 3.0:
22889   # pragma omp task task-clause[optseq] new-line
22890     structured-block  */
22891
22892#define OMP_TASK_CLAUSE_MASK				\
22893	( (1u << PRAGMA_OMP_CLAUSE_IF)			\
22894	| (1u << PRAGMA_OMP_CLAUSE_UNTIED)		\
22895	| (1u << PRAGMA_OMP_CLAUSE_DEFAULT)		\
22896	| (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
22897	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
22898	| (1u << PRAGMA_OMP_CLAUSE_SHARED))
22899
22900static tree
22901cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22902{
22903  tree clauses, block;
22904  unsigned int save;
22905
22906  clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22907				       "#pragma omp task", pragma_tok);
22908  block = begin_omp_task ();
22909  save = cp_parser_begin_omp_structured_block (parser);
22910  cp_parser_statement (parser, NULL_TREE, false, NULL);
22911  cp_parser_end_omp_structured_block (parser, save);
22912  return finish_omp_task (clauses, block);
22913}
22914
22915/* OpenMP 3.0:
22916   # pragma omp taskwait new-line  */
22917
22918static void
22919cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22920{
22921  cp_parser_require_pragma_eol (parser, pragma_tok);
22922  finish_omp_taskwait ();
22923}
22924
22925/* OpenMP 2.5:
22926   # pragma omp threadprivate (variable-list) */
22927
22928static void
22929cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22930{
22931  tree vars;
22932
22933  vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22934  cp_parser_require_pragma_eol (parser, pragma_tok);
22935
22936  finish_omp_threadprivate (vars);
22937}
22938
22939/* Main entry point to OpenMP statement pragmas.  */
22940
22941static void
22942cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22943{
22944  tree stmt;
22945
22946  switch (pragma_tok->pragma_kind)
22947    {
22948    case PRAGMA_OMP_ATOMIC:
22949      cp_parser_omp_atomic (parser, pragma_tok);
22950      return;
22951    case PRAGMA_OMP_CRITICAL:
22952      stmt = cp_parser_omp_critical (parser, pragma_tok);
22953      break;
22954    case PRAGMA_OMP_FOR:
22955      stmt = cp_parser_omp_for (parser, pragma_tok);
22956      break;
22957    case PRAGMA_OMP_MASTER:
22958      stmt = cp_parser_omp_master (parser, pragma_tok);
22959      break;
22960    case PRAGMA_OMP_ORDERED:
22961      stmt = cp_parser_omp_ordered (parser, pragma_tok);
22962      break;
22963    case PRAGMA_OMP_PARALLEL:
22964      stmt = cp_parser_omp_parallel (parser, pragma_tok);
22965      break;
22966    case PRAGMA_OMP_SECTIONS:
22967      stmt = cp_parser_omp_sections (parser, pragma_tok);
22968      break;
22969    case PRAGMA_OMP_SINGLE:
22970      stmt = cp_parser_omp_single (parser, pragma_tok);
22971      break;
22972    case PRAGMA_OMP_TASK:
22973      stmt = cp_parser_omp_task (parser, pragma_tok);
22974      break;
22975    default:
22976      gcc_unreachable ();
22977    }
22978
22979  if (stmt)
22980    SET_EXPR_LOCATION (stmt, pragma_tok->location);
22981}
22982
22983/* The parser.  */
22984
22985static GTY (()) cp_parser *the_parser;
22986
22987
22988/* Special handling for the first token or line in the file.  The first
22989   thing in the file might be #pragma GCC pch_preprocess, which loads a
22990   PCH file, which is a GC collection point.  So we need to handle this
22991   first pragma without benefit of an existing lexer structure.
22992
22993   Always returns one token to the caller in *FIRST_TOKEN.  This is
22994   either the true first token of the file, or the first token after
22995   the initial pragma.  */
22996
22997static void
22998cp_parser_initial_pragma (cp_token *first_token)
22999{
23000  tree name = NULL;
23001
23002  cp_lexer_get_preprocessor_token (NULL, first_token);
23003  if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
23004    return;
23005
23006  cp_lexer_get_preprocessor_token (NULL, first_token);
23007  if (first_token->type == CPP_STRING)
23008    {
23009      name = first_token->u.value;
23010
23011      cp_lexer_get_preprocessor_token (NULL, first_token);
23012      if (first_token->type != CPP_PRAGMA_EOL)
23013	error_at (first_token->location,
23014		  "junk at end of %<#pragma GCC pch_preprocess%>");
23015    }
23016  else
23017    error_at (first_token->location, "expected string literal");
23018
23019  /* Skip to the end of the pragma.  */
23020  while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
23021    cp_lexer_get_preprocessor_token (NULL, first_token);
23022
23023  /* Now actually load the PCH file.  */
23024  if (name)
23025    c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
23026
23027  /* Read one more token to return to our caller.  We have to do this
23028     after reading the PCH file in, since its pointers have to be
23029     live.  */
23030  cp_lexer_get_preprocessor_token (NULL, first_token);
23031}
23032
23033/* Normal parsing of a pragma token.  Here we can (and must) use the
23034   regular lexer.  */
23035
23036static bool
23037cp_parser_pragma (cp_parser *parser, enum pragma_context context)
23038{
23039  cp_token *pragma_tok;
23040  unsigned int id;
23041
23042  pragma_tok = cp_lexer_consume_token (parser->lexer);
23043  gcc_assert (pragma_tok->type == CPP_PRAGMA);
23044  parser->lexer->in_pragma = true;
23045
23046  id = pragma_tok->pragma_kind;
23047  switch (id)
23048    {
23049    case PRAGMA_GCC_PCH_PREPROCESS:
23050      error_at (pragma_tok->location,
23051		"%<#pragma GCC pch_preprocess%> must be first");
23052      break;
23053
23054    case PRAGMA_OMP_BARRIER:
23055      switch (context)
23056	{
23057	case pragma_compound:
23058	  cp_parser_omp_barrier (parser, pragma_tok);
23059	  return false;
23060	case pragma_stmt:
23061	  error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23062		    "used in compound statements");
23063	  break;
23064	default:
23065	  goto bad_stmt;
23066	}
23067      break;
23068
23069    case PRAGMA_OMP_FLUSH:
23070      switch (context)
23071	{
23072	case pragma_compound:
23073	  cp_parser_omp_flush (parser, pragma_tok);
23074	  return false;
23075	case pragma_stmt:
23076	  error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23077		    "used in compound statements");
23078	  break;
23079	default:
23080	  goto bad_stmt;
23081	}
23082      break;
23083
23084    case PRAGMA_OMP_TASKWAIT:
23085      switch (context)
23086	{
23087	case pragma_compound:
23088	  cp_parser_omp_taskwait (parser, pragma_tok);
23089	  return false;
23090	case pragma_stmt:
23091	  error_at (pragma_tok->location,
23092		    "%<#pragma omp taskwait%> may only be "
23093		    "used in compound statements");
23094	  break;
23095	default:
23096	  goto bad_stmt;
23097	}
23098      break;
23099
23100    case PRAGMA_OMP_THREADPRIVATE:
23101      cp_parser_omp_threadprivate (parser, pragma_tok);
23102      return false;
23103
23104    case PRAGMA_OMP_ATOMIC:
23105    case PRAGMA_OMP_CRITICAL:
23106    case PRAGMA_OMP_FOR:
23107    case PRAGMA_OMP_MASTER:
23108    case PRAGMA_OMP_ORDERED:
23109    case PRAGMA_OMP_PARALLEL:
23110    case PRAGMA_OMP_SECTIONS:
23111    case PRAGMA_OMP_SINGLE:
23112    case PRAGMA_OMP_TASK:
23113      if (context == pragma_external)
23114	goto bad_stmt;
23115      cp_parser_omp_construct (parser, pragma_tok);
23116      return true;
23117
23118    case PRAGMA_OMP_SECTION:
23119      error_at (pragma_tok->location,
23120		"%<#pragma omp section%> may only be used in "
23121		"%<#pragma omp sections%> construct");
23122      break;
23123
23124    default:
23125      gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23126      c_invoke_pragma_handler (id);
23127      break;
23128
23129    bad_stmt:
23130      cp_parser_error (parser, "expected declaration specifiers");
23131      break;
23132    }
23133
23134  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23135  return false;
23136}
23137
23138/* The interface the pragma parsers have to the lexer.  */
23139
23140enum cpp_ttype
23141pragma_lex (tree *value)
23142{
23143  cp_token *tok;
23144  enum cpp_ttype ret;
23145
23146  tok = cp_lexer_peek_token (the_parser->lexer);
23147
23148  ret = tok->type;
23149  *value = tok->u.value;
23150
23151  if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23152    ret = CPP_EOF;
23153  else if (ret == CPP_STRING)
23154    *value = cp_parser_string_literal (the_parser, false, false);
23155  else
23156    {
23157      cp_lexer_consume_token (the_parser->lexer);
23158      if (ret == CPP_KEYWORD)
23159	ret = CPP_NAME;
23160    }
23161
23162  return ret;
23163}
23164
23165
23166/* External interface.  */
23167
23168/* Parse one entire translation unit.  */
23169
23170void
23171c_parse_file (void)
23172{
23173  static bool already_called = false;
23174
23175  if (already_called)
23176    {
23177      sorry ("inter-module optimizations not implemented for C++");
23178      return;
23179    }
23180  already_called = true;
23181
23182  the_parser = cp_parser_new ();
23183  push_deferring_access_checks (flag_access_control
23184				? dk_no_deferred : dk_no_check);
23185  cp_parser_translation_unit (the_parser);
23186  the_parser = NULL;
23187}
23188
23189#include "gt-cp-parser.h"
23190