parser.c revision 260918
1/* C++ Parser.
2   Copyright (C) 2000, 2001, 2002, 2003, 2004,
3   2005  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 2, or (at your option)
11   any later version.
12
13   GCC is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with GCC; see the file COPYING.  If not, write to the Free
20   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21   02110-1301, USA.  */
22
23#include "config.h"
24#include "system.h"
25#include "coretypes.h"
26#include "tm.h"
27#include "dyn-string.h"
28#include "varray.h"
29#include "cpplib.h"
30#include "tree.h"
31#include "cp-tree.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
42
43/* The lexer.  */
44
45/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46   and c-lex.c) and the C++ parser.  */
47
48/* A token's value and its associated deferred access checks and
49   qualifying scope.  */
50
51struct tree_check GTY(())
52{
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 cp_token GTY (())
65{
66  /* The kind of token.  */
67  ENUM_BITFIELD (cpp_ttype) type : 8;
68  /* If this token is a keyword, this value indicates which keyword.
69     Otherwise, this value is RID_MAX.  */
70  ENUM_BITFIELD (rid) keyword : 8;
71  /* Token flags.  */
72  unsigned char flags;
73  /* Identifier for the pragma.  */
74  ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75  /* True if this token is from a system header.  */
76  BOOL_BITFIELD in_system_header : 1;
77  /* True if this token is from a context where it is implicitly extern "C" */
78  BOOL_BITFIELD implicit_extern_c : 1;
79  /* True for a CPP_NAME token that is not a keyword (i.e., for which
80     KEYWORD is RID_MAX) iff this name was looked up and found to be
81     ambiguous.  An error has already been reported.  */
82  BOOL_BITFIELD ambiguous_p : 1;
83  /* The input file stack index at which this token was found.  */
84  unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
85  /* The value associated with this token, if any.  */
86  union cp_token_value {
87    /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
88    struct tree_check* GTY((tag ("1"))) tree_check_value;
89    /* Use for all other tokens.  */
90    tree GTY((tag ("0"))) value;
91  } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92  /* The location at which this token was found.  */
93  location_t location;
94} cp_token;
95
96/* We use a stack of token pointer for saving token sets.  */
97typedef struct cp_token *cp_token_position;
98DEF_VEC_P (cp_token_position);
99DEF_VEC_ALLOC_P (cp_token_position,heap);
100
101static const cp_token eof_token =
102{
103  CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
104#if USE_MAPPED_LOCATION
105  0
106#else
107  {0, 0}
108#endif
109};
110
111/* The cp_lexer structure represents the C++ lexer.  It is responsible
112   for managing the token stream from the preprocessor and supplying
113   it to the parser.  Tokens are never added to the cp_lexer after
114   it is created.  */
115
116typedef struct cp_lexer GTY (())
117{
118  /* The memory allocated for the buffer.  NULL if this lexer does not
119     own the token buffer.  */
120  cp_token * GTY ((length ("%h.buffer_length"))) buffer;
121  /* If the lexer owns the buffer, this is the number of tokens in the
122     buffer.  */
123  size_t buffer_length;
124
125  /* A pointer just past the last available token.  The tokens
126     in this lexer are [buffer, last_token).  */
127  cp_token_position GTY ((skip)) last_token;
128
129  /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
130     no more available tokens.  */
131  cp_token_position GTY ((skip)) next_token;
132
133  /* A stack indicating positions at which cp_lexer_save_tokens was
134     called.  The top entry is the most recent position at which we
135     began saving tokens.  If the stack is non-empty, we are saving
136     tokens.  */
137  VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
138
139  /* The next lexer in a linked list of lexers.  */
140  struct cp_lexer *next;
141
142  /* True if we should output debugging information.  */
143  bool debugging_p;
144
145  /* True if we're in the context of parsing a pragma, and should not
146     increment past the end-of-line marker.  */
147  bool in_pragma;
148} cp_lexer;
149
150/* cp_token_cache is a range of tokens.  There is no need to represent
151   allocate heap memory for it, since tokens are never removed from the
152   lexer's array.  There is also no need for the GC to walk through
153   a cp_token_cache, since everything in here is referenced through
154   a lexer.  */
155
156typedef struct cp_token_cache GTY(())
157{
158  /* The beginning of the token range.  */
159  cp_token * GTY((skip)) first;
160
161  /* Points immediately after the last token in the range.  */
162  cp_token * GTY ((skip)) last;
163} cp_token_cache;
164
165/* Prototypes.  */
166
167static cp_lexer *cp_lexer_new_main
168  (void);
169static cp_lexer *cp_lexer_new_from_tokens
170  (cp_token_cache *tokens);
171static void cp_lexer_destroy
172  (cp_lexer *);
173static int cp_lexer_saving_tokens
174  (const cp_lexer *);
175static cp_token_position cp_lexer_token_position
176  (cp_lexer *, bool);
177static cp_token *cp_lexer_token_at
178  (cp_lexer *, cp_token_position);
179static void cp_lexer_get_preprocessor_token
180  (cp_lexer *, cp_token *);
181static inline cp_token *cp_lexer_peek_token
182  (cp_lexer *);
183static cp_token *cp_lexer_peek_nth_token
184  (cp_lexer *, size_t);
185static inline bool cp_lexer_next_token_is
186  (cp_lexer *, enum cpp_ttype);
187static bool cp_lexer_next_token_is_not
188  (cp_lexer *, enum cpp_ttype);
189static bool cp_lexer_next_token_is_keyword
190  (cp_lexer *, enum rid);
191static cp_token *cp_lexer_consume_token
192  (cp_lexer *);
193static void cp_lexer_purge_token
194  (cp_lexer *);
195static void cp_lexer_purge_tokens_after
196  (cp_lexer *, cp_token_position);
197static void cp_lexer_save_tokens
198  (cp_lexer *);
199static void cp_lexer_commit_tokens
200  (cp_lexer *);
201static void cp_lexer_rollback_tokens
202  (cp_lexer *);
203#ifdef ENABLE_CHECKING
204static void cp_lexer_print_token
205  (FILE *, cp_token *);
206static inline bool cp_lexer_debugging_p
207  (cp_lexer *);
208static void cp_lexer_start_debugging
209  (cp_lexer *) ATTRIBUTE_UNUSED;
210static void cp_lexer_stop_debugging
211  (cp_lexer *) ATTRIBUTE_UNUSED;
212#else
213/* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214   about passing NULL to functions that require non-NULL arguments
215   (fputs, fprintf).  It will never be used, so all we need is a value
216   of the right type that's guaranteed not to be NULL.  */
217#define cp_lexer_debug_stream stdout
218#define cp_lexer_print_token(str, tok) (void) 0
219#define cp_lexer_debugging_p(lexer) 0
220#endif /* ENABLE_CHECKING */
221
222static cp_token_cache *cp_token_cache_new
223  (cp_token *, cp_token *);
224
225static void cp_parser_initial_pragma
226  (cp_token *);
227
228/* Manifest constants.  */
229#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230#define CP_SAVED_TOKEN_STACK 5
231
232/* A token type for keywords, as opposed to ordinary identifiers.  */
233#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
234
235/* A token type for template-ids.  If a template-id is processed while
236   parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237   the value of the CPP_TEMPLATE_ID is whatever was returned by
238   cp_parser_template_id.  */
239#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
240
241/* A token type for nested-name-specifiers.  If a
242   nested-name-specifier is processed while parsing tentatively, it is
243   replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244   CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245   cp_parser_nested_name_specifier_opt.  */
246#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
247
248/* A token type for tokens that are not tokens at all; these are used
249   to represent slots in the array where there used to be a token
250   that has now been deleted.  */
251#define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
252
253/* The number of token types, including C++-specific ones.  */
254#define N_CP_TTYPES ((int) (CPP_PURGED + 1))
255
256/* Variables.  */
257
258#ifdef ENABLE_CHECKING
259/* The stream to which debugging output should be written.  */
260static FILE *cp_lexer_debug_stream;
261#endif /* ENABLE_CHECKING */
262
263/* Create a new main C++ lexer, the lexer that gets tokens from the
264   preprocessor.  */
265
266static cp_lexer *
267cp_lexer_new_main (void)
268{
269  cp_token first_token;
270  cp_lexer *lexer;
271  cp_token *pos;
272  size_t alloc;
273  size_t space;
274  cp_token *buffer;
275
276  /* It's possible that parsing the first pragma will load a PCH file,
277     which is a GC collection point.  So we have to do that before
278     allocating any memory.  */
279  cp_parser_initial_pragma (&first_token);
280
281  /* Tell c_lex_with_flags not to merge string constants.  */
282  c_lex_return_raw_strings = true;
283
284  c_common_no_more_pch ();
285
286  /* Allocate the memory.  */
287  lexer = GGC_CNEW (cp_lexer);
288
289#ifdef ENABLE_CHECKING
290  /* Initially we are not debugging.  */
291  lexer->debugging_p = false;
292#endif /* ENABLE_CHECKING */
293  lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
294				   CP_SAVED_TOKEN_STACK);
295
296  /* Create the buffer.  */
297  alloc = CP_LEXER_BUFFER_SIZE;
298  buffer = GGC_NEWVEC (cp_token, alloc);
299
300  /* Put the first token in the buffer.  */
301  space = alloc;
302  pos = buffer;
303  *pos = first_token;
304
305  /* Get the remaining tokens from the preprocessor.  */
306  while (pos->type != CPP_EOF)
307    {
308      pos++;
309      if (!--space)
310	{
311	  space = alloc;
312	  alloc *= 2;
313	  buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
314	  pos = buffer + space;
315	}
316      cp_lexer_get_preprocessor_token (lexer, pos);
317    }
318  lexer->buffer = buffer;
319  lexer->buffer_length = alloc - space;
320  lexer->last_token = pos;
321  lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
322
323  /* Subsequent preprocessor diagnostics should use compiler
324     diagnostic functions to get the compiler source location.  */
325  cpp_get_options (parse_in)->client_diagnostic = true;
326  cpp_get_callbacks (parse_in)->error = cp_cpp_error;
327
328  gcc_assert (lexer->next_token->type != CPP_PURGED);
329  return lexer;
330}
331
332/* Create a new lexer whose token stream is primed with the tokens in
333   CACHE.  When these tokens are exhausted, no new tokens will be read.  */
334
335static cp_lexer *
336cp_lexer_new_from_tokens (cp_token_cache *cache)
337{
338  cp_token *first = cache->first;
339  cp_token *last = cache->last;
340  cp_lexer *lexer = GGC_CNEW (cp_lexer);
341
342  /* We do not own the buffer.  */
343  lexer->buffer = NULL;
344  lexer->buffer_length = 0;
345  lexer->next_token = first == last ? (cp_token *)&eof_token : first;
346  lexer->last_token = last;
347
348  lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
349				   CP_SAVED_TOKEN_STACK);
350
351#ifdef ENABLE_CHECKING
352  /* Initially we are not debugging.  */
353  lexer->debugging_p = false;
354#endif
355
356  gcc_assert (lexer->next_token->type != CPP_PURGED);
357  return lexer;
358}
359
360/* Frees all resources associated with LEXER.  */
361
362static void
363cp_lexer_destroy (cp_lexer *lexer)
364{
365  if (lexer->buffer)
366    ggc_free (lexer->buffer);
367  VEC_free (cp_token_position, heap, lexer->saved_tokens);
368  ggc_free (lexer);
369}
370
371/* Returns nonzero if debugging information should be output.  */
372
373#ifdef ENABLE_CHECKING
374
375static inline bool
376cp_lexer_debugging_p (cp_lexer *lexer)
377{
378  return lexer->debugging_p;
379}
380
381#endif /* ENABLE_CHECKING */
382
383static inline cp_token_position
384cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
385{
386  gcc_assert (!previous_p || lexer->next_token != &eof_token);
387
388  return lexer->next_token - previous_p;
389}
390
391static inline cp_token *
392cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
393{
394  return pos;
395}
396
397/* nonzero if we are presently saving tokens.  */
398
399static inline int
400cp_lexer_saving_tokens (const cp_lexer* lexer)
401{
402  return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
403}
404
405/* Store the next token from the preprocessor in *TOKEN.  Return true
406   if we reach EOF.  */
407
408static void
409cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
410				 cp_token *token)
411{
412  static int is_extern_c = 0;
413
414   /* Get a new token from the preprocessor.  */
415  token->type
416    = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
417  token->input_file_stack_index = input_file_stack_tick;
418  token->keyword = RID_MAX;
419  token->pragma_kind = PRAGMA_NONE;
420  token->in_system_header = in_system_header;
421
422  /* On some systems, some header files are surrounded by an
423     implicit extern "C" block.  Set a flag in the token if it
424     comes from such a header.  */
425  is_extern_c += pending_lang_change;
426  pending_lang_change = 0;
427  token->implicit_extern_c = is_extern_c > 0;
428
429  /* Check to see if this token is a keyword.  */
430  if (token->type == CPP_NAME)
431    {
432      if (C_IS_RESERVED_WORD (token->u.value))
433	{
434	  /* Mark this token as a keyword.  */
435	  token->type = CPP_KEYWORD;
436	  /* Record which keyword.  */
437	  token->keyword = C_RID_CODE (token->u.value);
438	  /* Update the value.  Some keywords are mapped to particular
439	     entities, rather than simply having the value of the
440	     corresponding IDENTIFIER_NODE.  For example, `__const' is
441	     mapped to `const'.  */
442	  token->u.value = ridpointers[token->keyword];
443	}
444      else
445	{
446	  token->ambiguous_p = false;
447	  token->keyword = RID_MAX;
448	}
449    }
450  /* Handle Objective-C++ keywords.  */
451  else if (token->type == CPP_AT_NAME)
452    {
453      token->type = CPP_KEYWORD;
454      switch (C_RID_CODE (token->u.value))
455	{
456	/* Map 'class' to '@class', 'private' to '@private', etc.  */
457	case RID_CLASS: token->keyword = RID_AT_CLASS; break;
458	case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
459	case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
460	case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
461	case RID_THROW: token->keyword = RID_AT_THROW; break;
462	case RID_TRY: token->keyword = RID_AT_TRY; break;
463	case RID_CATCH: token->keyword = RID_AT_CATCH; break;
464	default: token->keyword = C_RID_CODE (token->u.value);
465	}
466    }
467  else if (token->type == CPP_PRAGMA)
468    {
469      /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
470      token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
471      token->u.value = NULL_TREE;
472    }
473}
474
475/* Update the globals input_location and in_system_header and the
476   input file stack from TOKEN.  */
477static inline void
478cp_lexer_set_source_position_from_token (cp_token *token)
479{
480  if (token->type != CPP_EOF)
481    {
482      input_location = token->location;
483      in_system_header = token->in_system_header;
484      restore_input_file_stack (token->input_file_stack_index);
485    }
486}
487
488/* Return a pointer to the next token in the token stream, but do not
489   consume it.  */
490
491static inline cp_token *
492cp_lexer_peek_token (cp_lexer *lexer)
493{
494  if (cp_lexer_debugging_p (lexer))
495    {
496      fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497      cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498      putc ('\n', cp_lexer_debug_stream);
499    }
500  return lexer->next_token;
501}
502
503/* Return true if the next token has the indicated TYPE.  */
504
505static inline bool
506cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507{
508  return cp_lexer_peek_token (lexer)->type == type;
509}
510
511/* Return true if the next token does not have the indicated TYPE.  */
512
513static inline bool
514cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515{
516  return !cp_lexer_next_token_is (lexer, type);
517}
518
519/* Return true if the next token is the indicated KEYWORD.  */
520
521static inline bool
522cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523{
524  return cp_lexer_peek_token (lexer)->keyword == keyword;
525}
526
527/* Return true if the next token is a keyword for a decl-specifier.  */
528
529static bool
530cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
531{
532  cp_token *token;
533
534  token = cp_lexer_peek_token (lexer);
535  switch (token->keyword)
536    {
537      /* Storage classes.  */
538    case RID_AUTO:
539    case RID_REGISTER:
540    case RID_STATIC:
541    case RID_EXTERN:
542    case RID_MUTABLE:
543    case RID_THREAD:
544      /* Elaborated type specifiers.  */
545    case RID_ENUM:
546    case RID_CLASS:
547    case RID_STRUCT:
548    case RID_UNION:
549    case RID_TYPENAME:
550      /* Simple type specifiers.  */
551    case RID_CHAR:
552    case RID_WCHAR:
553    case RID_BOOL:
554    case RID_SHORT:
555    case RID_INT:
556    case RID_LONG:
557    case RID_SIGNED:
558    case RID_UNSIGNED:
559    case RID_FLOAT:
560    case RID_DOUBLE:
561    case RID_VOID:
562      /* GNU extensions.  */
563    case RID_ATTRIBUTE:
564    case RID_TYPEOF:
565      return true;
566
567    default:
568      return false;
569    }
570}
571
572/* Return a pointer to the Nth token in the token stream.  If N is 1,
573   then this is precisely equivalent to cp_lexer_peek_token (except
574   that it is not inline).  One would like to disallow that case, but
575   there is one case (cp_parser_nth_token_starts_template_id) where
576   the caller passes a variable for N and it might be 1.  */
577
578static cp_token *
579cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
580{
581  cp_token *token;
582
583  /* N is 1-based, not zero-based.  */
584  gcc_assert (n > 0);
585
586  if (cp_lexer_debugging_p (lexer))
587    fprintf (cp_lexer_debug_stream,
588	     "cp_lexer: peeking ahead %ld at token: ", (long)n);
589
590  --n;
591  token = lexer->next_token;
592  gcc_assert (!n || token != &eof_token);
593  while (n != 0)
594    {
595      ++token;
596      if (token == lexer->last_token)
597	{
598	  token = (cp_token *)&eof_token;
599	  break;
600	}
601
602      if (token->type != CPP_PURGED)
603	--n;
604    }
605
606  if (cp_lexer_debugging_p (lexer))
607    {
608      cp_lexer_print_token (cp_lexer_debug_stream, token);
609      putc ('\n', cp_lexer_debug_stream);
610    }
611
612  return token;
613}
614
615/* Return the next token, and advance the lexer's next_token pointer
616   to point to the next non-purged token.  */
617
618static cp_token *
619cp_lexer_consume_token (cp_lexer* lexer)
620{
621  cp_token *token = lexer->next_token;
622
623  gcc_assert (token != &eof_token);
624  gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
625
626  do
627    {
628      lexer->next_token++;
629      if (lexer->next_token == lexer->last_token)
630	{
631	  lexer->next_token = (cp_token *)&eof_token;
632	  break;
633	}
634
635    }
636  while (lexer->next_token->type == CPP_PURGED);
637
638  cp_lexer_set_source_position_from_token (token);
639
640  /* Provide debugging output.  */
641  if (cp_lexer_debugging_p (lexer))
642    {
643      fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
644      cp_lexer_print_token (cp_lexer_debug_stream, token);
645      putc ('\n', cp_lexer_debug_stream);
646    }
647
648  return token;
649}
650
651/* Permanently remove the next token from the token stream, and
652   advance the next_token pointer to refer to the next non-purged
653   token.  */
654
655static void
656cp_lexer_purge_token (cp_lexer *lexer)
657{
658  cp_token *tok = lexer->next_token;
659
660  gcc_assert (tok != &eof_token);
661  tok->type = CPP_PURGED;
662  tok->location = UNKNOWN_LOCATION;
663  tok->u.value = NULL_TREE;
664  tok->keyword = RID_MAX;
665
666  do
667    {
668      tok++;
669      if (tok == lexer->last_token)
670	{
671	  tok = (cp_token *)&eof_token;
672	  break;
673	}
674    }
675  while (tok->type == CPP_PURGED);
676  lexer->next_token = tok;
677}
678
679/* Permanently remove all tokens after TOK, up to, but not
680   including, the token that will be returned next by
681   cp_lexer_peek_token.  */
682
683static void
684cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
685{
686  cp_token *peek = lexer->next_token;
687
688  if (peek == &eof_token)
689    peek = lexer->last_token;
690
691  gcc_assert (tok < peek);
692
693  for ( tok += 1; tok != peek; tok += 1)
694    {
695      tok->type = CPP_PURGED;
696      tok->location = UNKNOWN_LOCATION;
697      tok->u.value = NULL_TREE;
698      tok->keyword = RID_MAX;
699    }
700}
701
702/* Begin saving tokens.  All tokens consumed after this point will be
703   preserved.  */
704
705static void
706cp_lexer_save_tokens (cp_lexer* lexer)
707{
708  /* Provide debugging output.  */
709  if (cp_lexer_debugging_p (lexer))
710    fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
711
712  VEC_safe_push (cp_token_position, heap,
713		 lexer->saved_tokens, lexer->next_token);
714}
715
716/* Commit to the portion of the token stream most recently saved.  */
717
718static void
719cp_lexer_commit_tokens (cp_lexer* lexer)
720{
721  /* Provide debugging output.  */
722  if (cp_lexer_debugging_p (lexer))
723    fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
724
725  VEC_pop (cp_token_position, lexer->saved_tokens);
726}
727
728/* Return all tokens saved since the last call to cp_lexer_save_tokens
729   to the token stream.  Stop saving tokens.  */
730
731static void
732cp_lexer_rollback_tokens (cp_lexer* lexer)
733{
734  /* Provide debugging output.  */
735  if (cp_lexer_debugging_p (lexer))
736    fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
737
738  lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
739}
740
741/* Print a representation of the TOKEN on the STREAM.  */
742
743#ifdef ENABLE_CHECKING
744
745static void
746cp_lexer_print_token (FILE * stream, cp_token *token)
747{
748  /* We don't use cpp_type2name here because the parser defines
749     a few tokens of its own.  */
750  static const char *const token_names[] = {
751    /* cpplib-defined token types */
752#define OP(e, s) #e,
753#define TK(e, s) #e,
754    TTYPE_TABLE
755#undef OP
756#undef TK
757    /* C++ parser token types - see "Manifest constants", above.  */
758    "KEYWORD",
759    "TEMPLATE_ID",
760    "NESTED_NAME_SPECIFIER",
761    "PURGED"
762  };
763
764  /* If we have a name for the token, print it out.  Otherwise, we
765     simply give the numeric code.  */
766  gcc_assert (token->type < ARRAY_SIZE(token_names));
767  fputs (token_names[token->type], stream);
768
769  /* For some tokens, print the associated data.  */
770  switch (token->type)
771    {
772    case CPP_KEYWORD:
773      /* Some keywords have a value that is not an IDENTIFIER_NODE.
774	 For example, `struct' is mapped to an INTEGER_CST.  */
775      if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
776	break;
777      /* else fall through */
778    case CPP_NAME:
779      fputs (IDENTIFIER_POINTER (token->u.value), stream);
780      break;
781
782    case CPP_STRING:
783    case CPP_WSTRING:
784      fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
785      break;
786
787    default:
788      break;
789    }
790}
791
792/* Start emitting debugging information.  */
793
794static void
795cp_lexer_start_debugging (cp_lexer* lexer)
796{
797  lexer->debugging_p = true;
798}
799
800/* Stop emitting debugging information.  */
801
802static void
803cp_lexer_stop_debugging (cp_lexer* lexer)
804{
805  lexer->debugging_p = false;
806}
807
808#endif /* ENABLE_CHECKING */
809
810/* Create a new cp_token_cache, representing a range of tokens.  */
811
812static cp_token_cache *
813cp_token_cache_new (cp_token *first, cp_token *last)
814{
815  cp_token_cache *cache = GGC_NEW (cp_token_cache);
816  cache->first = first;
817  cache->last = last;
818  return cache;
819}
820
821
822/* Decl-specifiers.  */
823
824/* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
825
826static void
827clear_decl_specs (cp_decl_specifier_seq *decl_specs)
828{
829  memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
830}
831
832/* Declarators.  */
833
834/* Nothing other than the parser should be creating declarators;
835   declarators are a semi-syntactic representation of C++ entities.
836   Other parts of the front end that need to create entities (like
837   VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
838
839static cp_declarator *make_call_declarator
840  (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
841static cp_declarator *make_array_declarator
842  (cp_declarator *, tree);
843static cp_declarator *make_pointer_declarator
844  (cp_cv_quals, cp_declarator *);
845static cp_declarator *make_reference_declarator
846  (cp_cv_quals, cp_declarator *);
847static cp_parameter_declarator *make_parameter_declarator
848  (cp_decl_specifier_seq *, cp_declarator *, tree);
849static cp_declarator *make_ptrmem_declarator
850  (cp_cv_quals, tree, cp_declarator *);
851
852/* An erroneous declarator.  */
853static cp_declarator *cp_error_declarator;
854
855/* The obstack on which declarators and related data structures are
856   allocated.  */
857static struct obstack declarator_obstack;
858
859/* Alloc BYTES from the declarator memory pool.  */
860
861static inline void *
862alloc_declarator (size_t bytes)
863{
864  return obstack_alloc (&declarator_obstack, bytes);
865}
866
867/* Allocate a declarator of the indicated KIND.  Clear fields that are
868   common to all declarators.  */
869
870static cp_declarator *
871make_declarator (cp_declarator_kind kind)
872{
873  cp_declarator *declarator;
874
875  declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
876  declarator->kind = kind;
877  declarator->attributes = NULL_TREE;
878  declarator->declarator = NULL;
879
880  return declarator;
881}
882
883/* Make a declarator for a generalized identifier.  If
884   QUALIFYING_SCOPE is non-NULL, the identifier is
885   QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
886   UNQUALIFIED_NAME.  SFK indicates the kind of special function this
887   is, if any.   */
888
889static cp_declarator *
890make_id_declarator (tree qualifying_scope, tree unqualified_name,
891		    special_function_kind sfk)
892{
893  cp_declarator *declarator;
894
895  /* It is valid to write:
896
897       class C { void f(); };
898       typedef C D;
899       void D::f();
900
901     The standard is not clear about whether `typedef const C D' is
902     legal; as of 2002-09-15 the committee is considering that
903     question.  EDG 3.0 allows that syntax.  Therefore, we do as
904     well.  */
905  if (qualifying_scope && TYPE_P (qualifying_scope))
906    qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
907
908  gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
909	      || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
910	      || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
911
912  declarator = make_declarator (cdk_id);
913  declarator->u.id.qualifying_scope = qualifying_scope;
914  declarator->u.id.unqualified_name = unqualified_name;
915  declarator->u.id.sfk = sfk;
916
917  return declarator;
918}
919
920/* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
921   of modifiers such as const or volatile to apply to the pointer
922   type, represented as identifiers.  */
923
924cp_declarator *
925make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
926{
927  cp_declarator *declarator;
928
929  declarator = make_declarator (cdk_pointer);
930  declarator->declarator = target;
931  declarator->u.pointer.qualifiers = cv_qualifiers;
932  declarator->u.pointer.class_type = NULL_TREE;
933
934  return declarator;
935}
936
937/* Like make_pointer_declarator -- but for references.  */
938
939cp_declarator *
940make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941{
942  cp_declarator *declarator;
943
944  declarator = make_declarator (cdk_reference);
945  declarator->declarator = target;
946  declarator->u.pointer.qualifiers = cv_qualifiers;
947  declarator->u.pointer.class_type = NULL_TREE;
948
949  return declarator;
950}
951
952/* Like make_pointer_declarator -- but for a pointer to a non-static
953   member of CLASS_TYPE.  */
954
955cp_declarator *
956make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
957			cp_declarator *pointee)
958{
959  cp_declarator *declarator;
960
961  declarator = make_declarator (cdk_ptrmem);
962  declarator->declarator = pointee;
963  declarator->u.pointer.qualifiers = cv_qualifiers;
964  declarator->u.pointer.class_type = class_type;
965
966  return declarator;
967}
968
969/* Make a declarator for the function given by TARGET, with the
970   indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
971   "const"-qualified member function.  The EXCEPTION_SPECIFICATION
972   indicates what exceptions can be thrown.  */
973
974cp_declarator *
975make_call_declarator (cp_declarator *target,
976		      cp_parameter_declarator *parms,
977		      cp_cv_quals cv_qualifiers,
978		      tree exception_specification)
979{
980  cp_declarator *declarator;
981
982  declarator = make_declarator (cdk_function);
983  declarator->declarator = target;
984  declarator->u.function.parameters = parms;
985  declarator->u.function.qualifiers = cv_qualifiers;
986  declarator->u.function.exception_specification = exception_specification;
987
988  return declarator;
989}
990
991/* Make a declarator for an array of BOUNDS elements, each of which is
992   defined by ELEMENT.  */
993
994cp_declarator *
995make_array_declarator (cp_declarator *element, tree bounds)
996{
997  cp_declarator *declarator;
998
999  declarator = make_declarator (cdk_array);
1000  declarator->declarator = element;
1001  declarator->u.array.bounds = bounds;
1002
1003  return declarator;
1004}
1005
1006cp_parameter_declarator *no_parameters;
1007
1008/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1009   DECLARATOR and DEFAULT_ARGUMENT.  */
1010
1011cp_parameter_declarator *
1012make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1013			   cp_declarator *declarator,
1014			   tree default_argument)
1015{
1016  cp_parameter_declarator *parameter;
1017
1018  parameter = ((cp_parameter_declarator *)
1019	       alloc_declarator (sizeof (cp_parameter_declarator)));
1020  parameter->next = NULL;
1021  if (decl_specifiers)
1022    parameter->decl_specifiers = *decl_specifiers;
1023  else
1024    clear_decl_specs (&parameter->decl_specifiers);
1025  parameter->declarator = declarator;
1026  parameter->default_argument = default_argument;
1027  parameter->ellipsis_p = false;
1028
1029  return parameter;
1030}
1031
1032/* Returns true iff DECLARATOR  is a declaration for a function.  */
1033
1034static bool
1035function_declarator_p (const cp_declarator *declarator)
1036{
1037  while (declarator)
1038    {
1039      if (declarator->kind == cdk_function
1040	  && declarator->declarator->kind == cdk_id)
1041	return true;
1042      if (declarator->kind == cdk_id
1043	  || declarator->kind == cdk_error)
1044	return false;
1045      declarator = declarator->declarator;
1046    }
1047  return false;
1048}
1049
1050/* The parser.  */
1051
1052/* Overview
1053   --------
1054
1055   A cp_parser parses the token stream as specified by the C++
1056   grammar.  Its job is purely parsing, not semantic analysis.  For
1057   example, the parser breaks the token stream into declarators,
1058   expressions, statements, and other similar syntactic constructs.
1059   It does not check that the types of the expressions on either side
1060   of an assignment-statement are compatible, or that a function is
1061   not declared with a parameter of type `void'.
1062
1063   The parser invokes routines elsewhere in the compiler to perform
1064   semantic analysis and to build up the abstract syntax tree for the
1065   code processed.
1066
1067   The parser (and the template instantiation code, which is, in a
1068   way, a close relative of parsing) are the only parts of the
1069   compiler that should be calling push_scope and pop_scope, or
1070   related functions.  The parser (and template instantiation code)
1071   keeps track of what scope is presently active; everything else
1072   should simply honor that.  (The code that generates static
1073   initializers may also need to set the scope, in order to check
1074   access control correctly when emitting the initializers.)
1075
1076   Methodology
1077   -----------
1078
1079   The parser is of the standard recursive-descent variety.  Upcoming
1080   tokens in the token stream are examined in order to determine which
1081   production to use when parsing a non-terminal.  Some C++ constructs
1082   require arbitrary look ahead to disambiguate.  For example, it is
1083   impossible, in the general case, to tell whether a statement is an
1084   expression or declaration without scanning the entire statement.
1085   Therefore, the parser is capable of "parsing tentatively."  When the
1086   parser is not sure what construct comes next, it enters this mode.
1087   Then, while we attempt to parse the construct, the parser queues up
1088   error messages, rather than issuing them immediately, and saves the
1089   tokens it consumes.  If the construct is parsed successfully, the
1090   parser "commits", i.e., it issues any queued error messages and
1091   the tokens that were being preserved are permanently discarded.
1092   If, however, the construct is not parsed successfully, the parser
1093   rolls back its state completely so that it can resume parsing using
1094   a different alternative.
1095
1096   Future Improvements
1097   -------------------
1098
1099   The performance of the parser could probably be improved substantially.
1100   We could often eliminate the need to parse tentatively by looking ahead
1101   a little bit.  In some places, this approach might not entirely eliminate
1102   the need to parse tentatively, but it might still speed up the average
1103   case.  */
1104
1105/* Flags that are passed to some parsing functions.  These values can
1106   be bitwise-ored together.  */
1107
1108typedef enum cp_parser_flags
1109{
1110  /* No flags.  */
1111  CP_PARSER_FLAGS_NONE = 0x0,
1112  /* The construct is optional.  If it is not present, then no error
1113     should be issued.  */
1114  CP_PARSER_FLAGS_OPTIONAL = 0x1,
1115  /* When parsing a type-specifier, do not allow user-defined types.  */
1116  CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1117} cp_parser_flags;
1118
1119/* The different kinds of declarators we want to parse.  */
1120
1121typedef enum cp_parser_declarator_kind
1122{
1123  /* We want an abstract declarator.  */
1124  CP_PARSER_DECLARATOR_ABSTRACT,
1125  /* We want a named declarator.  */
1126  CP_PARSER_DECLARATOR_NAMED,
1127  /* We don't mind, but the name must be an unqualified-id.  */
1128  CP_PARSER_DECLARATOR_EITHER
1129} cp_parser_declarator_kind;
1130
1131/* The precedence values used to parse binary expressions.  The minimum value
1132   of PREC must be 1, because zero is reserved to quickly discriminate
1133   binary operators from other tokens.  */
1134
1135enum cp_parser_prec
1136{
1137  PREC_NOT_OPERATOR,
1138  PREC_LOGICAL_OR_EXPRESSION,
1139  PREC_LOGICAL_AND_EXPRESSION,
1140  PREC_INCLUSIVE_OR_EXPRESSION,
1141  PREC_EXCLUSIVE_OR_EXPRESSION,
1142  PREC_AND_EXPRESSION,
1143  PREC_EQUALITY_EXPRESSION,
1144  PREC_RELATIONAL_EXPRESSION,
1145  PREC_SHIFT_EXPRESSION,
1146  PREC_ADDITIVE_EXPRESSION,
1147  PREC_MULTIPLICATIVE_EXPRESSION,
1148  PREC_PM_EXPRESSION,
1149  NUM_PREC_VALUES = PREC_PM_EXPRESSION
1150};
1151
1152/* A mapping from a token type to a corresponding tree node type, with a
1153   precedence value.  */
1154
1155typedef struct cp_parser_binary_operations_map_node
1156{
1157  /* The token type.  */
1158  enum cpp_ttype token_type;
1159  /* The corresponding tree code.  */
1160  enum tree_code tree_type;
1161  /* The precedence of this operator.  */
1162  enum cp_parser_prec prec;
1163} cp_parser_binary_operations_map_node;
1164
1165/* The status of a tentative parse.  */
1166
1167typedef enum cp_parser_status_kind
1168{
1169  /* No errors have occurred.  */
1170  CP_PARSER_STATUS_KIND_NO_ERROR,
1171  /* An error has occurred.  */
1172  CP_PARSER_STATUS_KIND_ERROR,
1173  /* We are committed to this tentative parse, whether or not an error
1174     has occurred.  */
1175  CP_PARSER_STATUS_KIND_COMMITTED
1176} cp_parser_status_kind;
1177
1178typedef struct cp_parser_expression_stack_entry
1179{
1180  /* Left hand side of the binary operation we are currently
1181     parsing.  */
1182  tree lhs;
1183  /* Original tree code for left hand side, if it was a binary
1184     expression itself (used for -Wparentheses).  */
1185  enum tree_code lhs_type;
1186  /* Tree code for the binary operation we are parsing.  */
1187  enum tree_code tree_type;
1188  /* Precedence of the binary operation we are parsing.  */
1189  int prec;
1190} cp_parser_expression_stack_entry;
1191
1192/* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1193   entries because precedence levels on the stack are monotonically
1194   increasing.  */
1195typedef struct cp_parser_expression_stack_entry
1196  cp_parser_expression_stack[NUM_PREC_VALUES];
1197
1198/* Context that is saved and restored when parsing tentatively.  */
1199typedef struct cp_parser_context GTY (())
1200{
1201  /* If this is a tentative parsing context, the status of the
1202     tentative parse.  */
1203  enum cp_parser_status_kind status;
1204  /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1205     that are looked up in this context must be looked up both in the
1206     scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1207     the context of the containing expression.  */
1208  tree object_type;
1209
1210  /* The next parsing context in the stack.  */
1211  struct cp_parser_context *next;
1212} cp_parser_context;
1213
1214/* Prototypes.  */
1215
1216/* Constructors and destructors.  */
1217
1218static cp_parser_context *cp_parser_context_new
1219  (cp_parser_context *);
1220
1221/* Class variables.  */
1222
1223static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1224
1225/* The operator-precedence table used by cp_parser_binary_expression.
1226   Transformed into an associative array (binops_by_token) by
1227   cp_parser_new.  */
1228
1229static const cp_parser_binary_operations_map_node binops[] = {
1230  { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1231  { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1232
1233  { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1234  { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1235  { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1236
1237  { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1238  { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1239
1240  { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1241  { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1242
1243  { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1244  { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1245  { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1246  { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1247
1248  { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1249  { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1250
1251  { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1252
1253  { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1254
1255  { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1256
1257  { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1258
1259  { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1260};
1261
1262/* The same as binops, but initialized by cp_parser_new so that
1263   binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1264   for speed.  */
1265static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1266
1267/* Constructors and destructors.  */
1268
1269/* Construct a new context.  The context below this one on the stack
1270   is given by NEXT.  */
1271
1272static cp_parser_context *
1273cp_parser_context_new (cp_parser_context* next)
1274{
1275  cp_parser_context *context;
1276
1277  /* Allocate the storage.  */
1278  if (cp_parser_context_free_list != NULL)
1279    {
1280      /* Pull the first entry from the free list.  */
1281      context = cp_parser_context_free_list;
1282      cp_parser_context_free_list = context->next;
1283      memset (context, 0, sizeof (*context));
1284    }
1285  else
1286    context = GGC_CNEW (cp_parser_context);
1287
1288  /* No errors have occurred yet in this context.  */
1289  context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1290  /* If this is not the bottomost context, copy information that we
1291     need from the previous context.  */
1292  if (next)
1293    {
1294      /* If, in the NEXT context, we are parsing an `x->' or `x.'
1295	 expression, then we are parsing one in this context, too.  */
1296      context->object_type = next->object_type;
1297      /* Thread the stack.  */
1298      context->next = next;
1299    }
1300
1301  return context;
1302}
1303
1304/* The cp_parser structure represents the C++ parser.  */
1305
1306typedef struct cp_parser GTY(())
1307{
1308  /* The lexer from which we are obtaining tokens.  */
1309  cp_lexer *lexer;
1310
1311  /* The scope in which names should be looked up.  If NULL_TREE, then
1312     we look up names in the scope that is currently open in the
1313     source program.  If non-NULL, this is either a TYPE or
1314     NAMESPACE_DECL for the scope in which we should look.  It can
1315     also be ERROR_MARK, when we've parsed a bogus scope.
1316
1317     This value is not cleared automatically after a name is looked
1318     up, so we must be careful to clear it before starting a new look
1319     up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1320     will look up `Z' in the scope of `X', rather than the current
1321     scope.)  Unfortunately, it is difficult to tell when name lookup
1322     is complete, because we sometimes peek at a token, look it up,
1323     and then decide not to consume it.   */
1324  tree scope;
1325
1326  /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1327     last lookup took place.  OBJECT_SCOPE is used if an expression
1328     like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1329     respectively.  QUALIFYING_SCOPE is used for an expression of the
1330     form "X::Y"; it refers to X.  */
1331  tree object_scope;
1332  tree qualifying_scope;
1333
1334  /* A stack of parsing contexts.  All but the bottom entry on the
1335     stack will be tentative contexts.
1336
1337     We parse tentatively in order to determine which construct is in
1338     use in some situations.  For example, in order to determine
1339     whether a statement is an expression-statement or a
1340     declaration-statement we parse it tentatively as a
1341     declaration-statement.  If that fails, we then reparse the same
1342     token stream as an expression-statement.  */
1343  cp_parser_context *context;
1344
1345  /* True if we are parsing GNU C++.  If this flag is not set, then
1346     GNU extensions are not recognized.  */
1347  bool allow_gnu_extensions_p;
1348
1349  /* TRUE if the `>' token should be interpreted as the greater-than
1350     operator.  FALSE if it is the end of a template-id or
1351     template-parameter-list.  */
1352  bool greater_than_is_operator_p;
1353
1354  /* TRUE if default arguments are allowed within a parameter list
1355     that starts at this point. FALSE if only a gnu extension makes
1356     them permissible.  */
1357  bool default_arg_ok_p;
1358
1359  /* TRUE if we are parsing an integral constant-expression.  See
1360     [expr.const] for a precise definition.  */
1361  bool integral_constant_expression_p;
1362
1363  /* TRUE if we are parsing an integral constant-expression -- but a
1364     non-constant expression should be permitted as well.  This flag
1365     is used when parsing an array bound so that GNU variable-length
1366     arrays are tolerated.  */
1367  bool allow_non_integral_constant_expression_p;
1368
1369  /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1370     been seen that makes the expression non-constant.  */
1371  bool non_integral_constant_expression_p;
1372
1373  /* TRUE if local variable names and `this' are forbidden in the
1374     current context.  */
1375  bool local_variables_forbidden_p;
1376
1377  /* TRUE if the declaration we are parsing is part of a
1378     linkage-specification of the form `extern string-literal
1379     declaration'.  */
1380  bool in_unbraced_linkage_specification_p;
1381
1382  /* TRUE if we are presently parsing a declarator, after the
1383     direct-declarator.  */
1384  bool in_declarator_p;
1385
1386  /* TRUE if we are presently parsing a template-argument-list.  */
1387  bool in_template_argument_list_p;
1388
1389  /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1390     to IN_OMP_BLOCK if parsing OpenMP structured block and
1391     IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1392     this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1393     iteration-statement, OpenMP block or loop within that switch.  */
1394#define IN_SWITCH_STMT		1
1395#define IN_ITERATION_STMT	2
1396#define IN_OMP_BLOCK		4
1397#define IN_OMP_FOR		8
1398  unsigned char in_statement;
1399
1400  /* TRUE if we are presently parsing the body of a switch statement.
1401     Note that this doesn't quite overlap with in_statement above.
1402     The difference relates to giving the right sets of error messages:
1403     "case not in switch" vs "break statement used with OpenMP...".  */
1404  bool in_switch_statement_p;
1405
1406  /* TRUE if we are parsing a type-id in an expression context.  In
1407     such a situation, both "type (expr)" and "type (type)" are valid
1408     alternatives.  */
1409  bool in_type_id_in_expr_p;
1410
1411  /* TRUE if we are currently in a header file where declarations are
1412     implicitly extern "C".  */
1413  bool implicit_extern_c;
1414
1415  /* TRUE if strings in expressions should be translated to the execution
1416     character set.  */
1417  bool translate_strings_p;
1418
1419  /* TRUE if we are presently parsing the body of a function, but not
1420     a local class.  */
1421  bool in_function_body;
1422
1423  /* If non-NULL, then we are parsing a construct where new type
1424     definitions are not permitted.  The string stored here will be
1425     issued as an error message if a type is defined.  */
1426  const char *type_definition_forbidden_message;
1427
1428  /* A list of lists. The outer list is a stack, used for member
1429     functions of local classes. At each level there are two sub-list,
1430     one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1431     sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1432     TREE_VALUE's. The functions are chained in reverse declaration
1433     order.
1434
1435     The TREE_PURPOSE sublist contains those functions with default
1436     arguments that need post processing, and the TREE_VALUE sublist
1437     contains those functions with definitions that need post
1438     processing.
1439
1440     These lists can only be processed once the outermost class being
1441     defined is complete.  */
1442  tree unparsed_functions_queues;
1443
1444  /* The number of classes whose definitions are currently in
1445     progress.  */
1446  unsigned num_classes_being_defined;
1447
1448  /* The number of template parameter lists that apply directly to the
1449     current declaration.  */
1450  unsigned num_template_parameter_lists;
1451} cp_parser;
1452
1453/* Prototypes.  */
1454
1455/* Constructors and destructors.  */
1456
1457static cp_parser *cp_parser_new
1458  (void);
1459
1460/* Routines to parse various constructs.
1461
1462   Those that return `tree' will return the error_mark_node (rather
1463   than NULL_TREE) if a parse error occurs, unless otherwise noted.
1464   Sometimes, they will return an ordinary node if error-recovery was
1465   attempted, even though a parse error occurred.  So, to check
1466   whether or not a parse error occurred, you should always use
1467   cp_parser_error_occurred.  If the construct is optional (indicated
1468   either by an `_opt' in the name of the function that does the
1469   parsing or via a FLAGS parameter), then NULL_TREE is returned if
1470   the construct is not present.  */
1471
1472/* Lexical conventions [gram.lex]  */
1473
1474static tree cp_parser_identifier
1475  (cp_parser *);
1476static tree cp_parser_string_literal
1477  (cp_parser *, bool, bool);
1478
1479/* Basic concepts [gram.basic]  */
1480
1481static bool cp_parser_translation_unit
1482  (cp_parser *);
1483
1484/* Expressions [gram.expr]  */
1485
1486static tree cp_parser_primary_expression
1487  (cp_parser *, bool, bool, bool, cp_id_kind *);
1488static tree cp_parser_id_expression
1489  (cp_parser *, bool, bool, bool *, bool, bool);
1490static tree cp_parser_unqualified_id
1491  (cp_parser *, bool, bool, bool, bool);
1492static tree cp_parser_nested_name_specifier_opt
1493  (cp_parser *, bool, bool, bool, bool);
1494static tree cp_parser_nested_name_specifier
1495  (cp_parser *, bool, bool, bool, bool);
1496static tree cp_parser_class_or_namespace_name
1497  (cp_parser *, bool, bool, bool, bool, bool);
1498static tree cp_parser_postfix_expression
1499  (cp_parser *, bool, bool);
1500static tree cp_parser_postfix_open_square_expression
1501  (cp_parser *, tree, bool);
1502static tree cp_parser_postfix_dot_deref_expression
1503  (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1504static tree cp_parser_parenthesized_expression_list
1505  (cp_parser *, bool, bool, bool *);
1506static void cp_parser_pseudo_destructor_name
1507  (cp_parser *, tree *, tree *);
1508static tree cp_parser_unary_expression
1509  (cp_parser *, bool, bool);
1510static enum tree_code cp_parser_unary_operator
1511  (cp_token *);
1512static tree cp_parser_new_expression
1513  (cp_parser *);
1514static tree cp_parser_new_placement
1515  (cp_parser *);
1516static tree cp_parser_new_type_id
1517  (cp_parser *, tree *);
1518static cp_declarator *cp_parser_new_declarator_opt
1519  (cp_parser *);
1520static cp_declarator *cp_parser_direct_new_declarator
1521  (cp_parser *);
1522static tree cp_parser_new_initializer
1523  (cp_parser *);
1524static tree cp_parser_delete_expression
1525  (cp_parser *);
1526static tree cp_parser_cast_expression
1527  (cp_parser *, bool, bool);
1528static tree cp_parser_binary_expression
1529  (cp_parser *, bool);
1530static tree cp_parser_question_colon_clause
1531  (cp_parser *, tree);
1532static tree cp_parser_assignment_expression
1533  (cp_parser *, bool);
1534static enum tree_code cp_parser_assignment_operator_opt
1535  (cp_parser *);
1536static tree cp_parser_expression
1537  (cp_parser *, bool);
1538static tree cp_parser_constant_expression
1539  (cp_parser *, bool, bool *);
1540static tree cp_parser_builtin_offsetof
1541  (cp_parser *);
1542
1543/* Statements [gram.stmt.stmt]  */
1544
1545static void cp_parser_statement
1546  (cp_parser *, tree, bool, bool *);
1547static void cp_parser_label_for_labeled_statement
1548  (cp_parser *);
1549static tree cp_parser_expression_statement
1550  (cp_parser *, tree);
1551static tree cp_parser_compound_statement
1552  (cp_parser *, tree, bool);
1553static void cp_parser_statement_seq_opt
1554  (cp_parser *, tree);
1555static tree cp_parser_selection_statement
1556  (cp_parser *, bool *);
1557static tree cp_parser_condition
1558  (cp_parser *);
1559static tree cp_parser_iteration_statement
1560  (cp_parser *);
1561static void cp_parser_for_init_statement
1562  (cp_parser *);
1563static tree cp_parser_jump_statement
1564  (cp_parser *);
1565static void cp_parser_declaration_statement
1566  (cp_parser *);
1567
1568static tree cp_parser_implicitly_scoped_statement
1569  (cp_parser *, bool *);
1570static void cp_parser_already_scoped_statement
1571  (cp_parser *);
1572
1573/* Declarations [gram.dcl.dcl] */
1574
1575static void cp_parser_declaration_seq_opt
1576  (cp_parser *);
1577static void cp_parser_declaration
1578  (cp_parser *);
1579static void cp_parser_block_declaration
1580  (cp_parser *, bool);
1581static void cp_parser_simple_declaration
1582  (cp_parser *, bool);
1583static void cp_parser_decl_specifier_seq
1584  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1585static tree cp_parser_storage_class_specifier_opt
1586  (cp_parser *);
1587static tree cp_parser_function_specifier_opt
1588  (cp_parser *, cp_decl_specifier_seq *);
1589static tree cp_parser_type_specifier
1590  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1591   int *, bool *);
1592static tree cp_parser_simple_type_specifier
1593  (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1594static tree cp_parser_type_name
1595  (cp_parser *);
1596static tree cp_parser_elaborated_type_specifier
1597  (cp_parser *, bool, bool);
1598static tree cp_parser_enum_specifier
1599  (cp_parser *);
1600static void cp_parser_enumerator_list
1601  (cp_parser *, tree);
1602static void cp_parser_enumerator_definition
1603  (cp_parser *, tree);
1604static tree cp_parser_namespace_name
1605  (cp_parser *);
1606static void cp_parser_namespace_definition
1607  (cp_parser *);
1608static void cp_parser_namespace_body
1609  (cp_parser *);
1610static tree cp_parser_qualified_namespace_specifier
1611  (cp_parser *);
1612static void cp_parser_namespace_alias_definition
1613  (cp_parser *);
1614static bool cp_parser_using_declaration
1615  (cp_parser *, bool);
1616static void cp_parser_using_directive
1617  (cp_parser *);
1618static void cp_parser_asm_definition
1619  (cp_parser *);
1620static void cp_parser_linkage_specification
1621  (cp_parser *);
1622
1623/* Declarators [gram.dcl.decl] */
1624
1625static tree cp_parser_init_declarator
1626  (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1627static cp_declarator *cp_parser_declarator
1628  (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1629static cp_declarator *cp_parser_direct_declarator
1630  (cp_parser *, cp_parser_declarator_kind, int *, bool);
1631static enum tree_code cp_parser_ptr_operator
1632  (cp_parser *, tree *, cp_cv_quals *);
1633static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1634  (cp_parser *);
1635static tree cp_parser_declarator_id
1636  (cp_parser *, bool);
1637static tree cp_parser_type_id
1638  (cp_parser *);
1639static void cp_parser_type_specifier_seq
1640  (cp_parser *, bool, cp_decl_specifier_seq *);
1641static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1642  (cp_parser *);
1643static cp_parameter_declarator *cp_parser_parameter_declaration_list
1644  (cp_parser *, bool *);
1645static cp_parameter_declarator *cp_parser_parameter_declaration
1646  (cp_parser *, bool, bool *);
1647static void cp_parser_function_body
1648  (cp_parser *);
1649static tree cp_parser_initializer
1650  (cp_parser *, bool *, bool *);
1651static tree cp_parser_initializer_clause
1652  (cp_parser *, bool *);
1653static VEC(constructor_elt,gc) *cp_parser_initializer_list
1654  (cp_parser *, bool *);
1655
1656static bool cp_parser_ctor_initializer_opt_and_function_body
1657  (cp_parser *);
1658
1659/* Classes [gram.class] */
1660
1661static tree cp_parser_class_name
1662  (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1663static tree cp_parser_class_specifier
1664  (cp_parser *);
1665static tree cp_parser_class_head
1666  (cp_parser *, bool *, tree *, tree *);
1667static enum tag_types cp_parser_class_key
1668  (cp_parser *);
1669static void cp_parser_member_specification_opt
1670  (cp_parser *);
1671static void cp_parser_member_declaration
1672  (cp_parser *);
1673static tree cp_parser_pure_specifier
1674  (cp_parser *);
1675static tree cp_parser_constant_initializer
1676  (cp_parser *);
1677
1678/* Derived classes [gram.class.derived] */
1679
1680static tree cp_parser_base_clause
1681  (cp_parser *);
1682static tree cp_parser_base_specifier
1683  (cp_parser *);
1684
1685/* Special member functions [gram.special] */
1686
1687static tree cp_parser_conversion_function_id
1688  (cp_parser *);
1689static tree cp_parser_conversion_type_id
1690  (cp_parser *);
1691static cp_declarator *cp_parser_conversion_declarator_opt
1692  (cp_parser *);
1693static bool cp_parser_ctor_initializer_opt
1694  (cp_parser *);
1695static void cp_parser_mem_initializer_list
1696  (cp_parser *);
1697static tree cp_parser_mem_initializer
1698  (cp_parser *);
1699static tree cp_parser_mem_initializer_id
1700  (cp_parser *);
1701
1702/* Overloading [gram.over] */
1703
1704static tree cp_parser_operator_function_id
1705  (cp_parser *);
1706static tree cp_parser_operator
1707  (cp_parser *);
1708
1709/* Templates [gram.temp] */
1710
1711static void cp_parser_template_declaration
1712  (cp_parser *, bool);
1713static tree cp_parser_template_parameter_list
1714  (cp_parser *);
1715static tree cp_parser_template_parameter
1716  (cp_parser *, bool *);
1717static tree cp_parser_type_parameter
1718  (cp_parser *);
1719static tree cp_parser_template_id
1720  (cp_parser *, bool, bool, bool);
1721static tree cp_parser_template_name
1722  (cp_parser *, bool, bool, bool, bool *);
1723static tree cp_parser_template_argument_list
1724  (cp_parser *);
1725static tree cp_parser_template_argument
1726  (cp_parser *);
1727static void cp_parser_explicit_instantiation
1728  (cp_parser *);
1729static void cp_parser_explicit_specialization
1730  (cp_parser *);
1731
1732/* Exception handling [gram.exception] */
1733
1734static tree cp_parser_try_block
1735  (cp_parser *);
1736static bool cp_parser_function_try_block
1737  (cp_parser *);
1738static void cp_parser_handler_seq
1739  (cp_parser *);
1740static void cp_parser_handler
1741  (cp_parser *);
1742static tree cp_parser_exception_declaration
1743  (cp_parser *);
1744static tree cp_parser_throw_expression
1745  (cp_parser *);
1746static tree cp_parser_exception_specification_opt
1747  (cp_parser *);
1748static tree cp_parser_type_id_list
1749  (cp_parser *);
1750
1751/* GNU Extensions */
1752
1753static tree cp_parser_asm_specification_opt
1754  (cp_parser *);
1755static tree cp_parser_asm_operand_list
1756  (cp_parser *);
1757static tree cp_parser_asm_clobber_list
1758  (cp_parser *);
1759static tree cp_parser_attributes_opt
1760  (cp_parser *);
1761static tree cp_parser_attribute_list
1762  (cp_parser *);
1763static bool cp_parser_extension_opt
1764  (cp_parser *, int *);
1765static void cp_parser_label_declaration
1766  (cp_parser *);
1767
1768enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1769static bool cp_parser_pragma
1770  (cp_parser *, enum pragma_context);
1771
1772/* Objective-C++ Productions */
1773
1774static tree cp_parser_objc_message_receiver
1775  (cp_parser *);
1776static tree cp_parser_objc_message_args
1777  (cp_parser *);
1778static tree cp_parser_objc_message_expression
1779  (cp_parser *);
1780static tree cp_parser_objc_encode_expression
1781  (cp_parser *);
1782static tree cp_parser_objc_defs_expression
1783  (cp_parser *);
1784static tree cp_parser_objc_protocol_expression
1785  (cp_parser *);
1786static tree cp_parser_objc_selector_expression
1787  (cp_parser *);
1788static tree cp_parser_objc_expression
1789  (cp_parser *);
1790static bool cp_parser_objc_selector_p
1791  (enum cpp_ttype);
1792static tree cp_parser_objc_selector
1793  (cp_parser *);
1794static tree cp_parser_objc_protocol_refs_opt
1795  (cp_parser *);
1796static void cp_parser_objc_declaration
1797  (cp_parser *);
1798static tree cp_parser_objc_statement
1799  (cp_parser *);
1800
1801/* Utility Routines */
1802
1803static tree cp_parser_lookup_name
1804  (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1805static tree cp_parser_lookup_name_simple
1806  (cp_parser *, tree);
1807static tree cp_parser_maybe_treat_template_as_class
1808  (tree, bool);
1809static bool cp_parser_check_declarator_template_parameters
1810  (cp_parser *, cp_declarator *);
1811static bool cp_parser_check_template_parameters
1812  (cp_parser *, unsigned);
1813static tree cp_parser_simple_cast_expression
1814  (cp_parser *);
1815static tree cp_parser_global_scope_opt
1816  (cp_parser *, bool);
1817static bool cp_parser_constructor_declarator_p
1818  (cp_parser *, bool);
1819static tree cp_parser_function_definition_from_specifiers_and_declarator
1820  (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1821static tree cp_parser_function_definition_after_declarator
1822  (cp_parser *, bool);
1823static void cp_parser_template_declaration_after_export
1824  (cp_parser *, bool);
1825static void cp_parser_perform_template_parameter_access_checks
1826  (VEC (deferred_access_check,gc)*);
1827static tree cp_parser_single_declaration
1828  (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1829static tree cp_parser_functional_cast
1830  (cp_parser *, tree);
1831static tree cp_parser_save_member_function_body
1832  (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1833static tree cp_parser_enclosed_template_argument_list
1834  (cp_parser *);
1835static void cp_parser_save_default_args
1836  (cp_parser *, tree);
1837static void cp_parser_late_parsing_for_member
1838  (cp_parser *, tree);
1839static void cp_parser_late_parsing_default_args
1840  (cp_parser *, tree);
1841static tree cp_parser_sizeof_operand
1842  (cp_parser *, enum rid);
1843static bool cp_parser_declares_only_class_p
1844  (cp_parser *);
1845static void cp_parser_set_storage_class
1846  (cp_parser *, cp_decl_specifier_seq *, enum rid);
1847static void cp_parser_set_decl_spec_type
1848  (cp_decl_specifier_seq *, tree, bool);
1849static bool cp_parser_friend_p
1850  (const cp_decl_specifier_seq *);
1851static cp_token *cp_parser_require
1852  (cp_parser *, enum cpp_ttype, const char *);
1853static cp_token *cp_parser_require_keyword
1854  (cp_parser *, enum rid, const char *);
1855static bool cp_parser_token_starts_function_definition_p
1856  (cp_token *);
1857static bool cp_parser_next_token_starts_class_definition_p
1858  (cp_parser *);
1859static bool cp_parser_next_token_ends_template_argument_p
1860  (cp_parser *);
1861static bool cp_parser_nth_token_starts_template_argument_list_p
1862  (cp_parser *, size_t);
1863static enum tag_types cp_parser_token_is_class_key
1864  (cp_token *);
1865static void cp_parser_check_class_key
1866  (enum tag_types, tree type);
1867static void cp_parser_check_access_in_redeclaration
1868  (tree type);
1869static bool cp_parser_optional_template_keyword
1870  (cp_parser *);
1871static void cp_parser_pre_parsed_nested_name_specifier
1872  (cp_parser *);
1873static void cp_parser_cache_group
1874  (cp_parser *, enum cpp_ttype, unsigned);
1875static void cp_parser_parse_tentatively
1876  (cp_parser *);
1877static void cp_parser_commit_to_tentative_parse
1878  (cp_parser *);
1879static void cp_parser_abort_tentative_parse
1880  (cp_parser *);
1881static bool cp_parser_parse_definitely
1882  (cp_parser *);
1883static inline bool cp_parser_parsing_tentatively
1884  (cp_parser *);
1885static bool cp_parser_uncommitted_to_tentative_parse_p
1886  (cp_parser *);
1887static void cp_parser_error
1888  (cp_parser *, const char *);
1889static void cp_parser_name_lookup_error
1890  (cp_parser *, tree, tree, const char *);
1891static bool cp_parser_simulate_error
1892  (cp_parser *);
1893static bool cp_parser_check_type_definition
1894  (cp_parser *);
1895static void cp_parser_check_for_definition_in_return_type
1896  (cp_declarator *, tree);
1897static void cp_parser_check_for_invalid_template_id
1898  (cp_parser *, tree);
1899static bool cp_parser_non_integral_constant_expression
1900  (cp_parser *, const char *);
1901static void cp_parser_diagnose_invalid_type_name
1902  (cp_parser *, tree, tree);
1903static bool cp_parser_parse_and_diagnose_invalid_type_name
1904  (cp_parser *);
1905static int cp_parser_skip_to_closing_parenthesis
1906  (cp_parser *, bool, bool, bool);
1907static void cp_parser_skip_to_end_of_statement
1908  (cp_parser *);
1909static void cp_parser_consume_semicolon_at_end_of_statement
1910  (cp_parser *);
1911static void cp_parser_skip_to_end_of_block_or_statement
1912  (cp_parser *);
1913static void cp_parser_skip_to_closing_brace
1914  (cp_parser *);
1915static void cp_parser_skip_to_end_of_template_parameter_list
1916  (cp_parser *);
1917static void cp_parser_skip_to_pragma_eol
1918  (cp_parser*, cp_token *);
1919static bool cp_parser_error_occurred
1920  (cp_parser *);
1921static bool cp_parser_allow_gnu_extensions_p
1922  (cp_parser *);
1923static bool cp_parser_is_string_literal
1924  (cp_token *);
1925static bool cp_parser_is_keyword
1926  (cp_token *, enum rid);
1927static tree cp_parser_make_typename_type
1928  (cp_parser *, tree, tree);
1929
1930/* Returns nonzero if we are parsing tentatively.  */
1931
1932static inline bool
1933cp_parser_parsing_tentatively (cp_parser* parser)
1934{
1935  return parser->context->next != NULL;
1936}
1937
1938/* Returns nonzero if TOKEN is a string literal.  */
1939
1940static bool
1941cp_parser_is_string_literal (cp_token* token)
1942{
1943  return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1944}
1945
1946/* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1947
1948static bool
1949cp_parser_is_keyword (cp_token* token, enum rid keyword)
1950{
1951  return token->keyword == keyword;
1952}
1953
1954/* If not parsing tentatively, issue a diagnostic of the form
1955      FILE:LINE: MESSAGE before TOKEN
1956   where TOKEN is the next token in the input stream.  MESSAGE
1957   (specified by the caller) is usually of the form "expected
1958   OTHER-TOKEN".  */
1959
1960static void
1961cp_parser_error (cp_parser* parser, const char* message)
1962{
1963  if (!cp_parser_simulate_error (parser))
1964    {
1965      cp_token *token = cp_lexer_peek_token (parser->lexer);
1966      /* This diagnostic makes more sense if it is tagged to the line
1967	 of the token we just peeked at.  */
1968      cp_lexer_set_source_position_from_token (token);
1969
1970      if (token->type == CPP_PRAGMA)
1971	{
1972	  error ("%<#pragma%> is not allowed here");
1973	  cp_parser_skip_to_pragma_eol (parser, token);
1974	  return;
1975	}
1976
1977      c_parse_error (message,
1978		     /* Because c_parser_error does not understand
1979			CPP_KEYWORD, keywords are treated like
1980			identifiers.  */
1981		     (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1982		     token->u.value);
1983    }
1984}
1985
1986/* Issue an error about name-lookup failing.  NAME is the
1987   IDENTIFIER_NODE DECL is the result of
1988   the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1989   the thing that we hoped to find.  */
1990
1991static void
1992cp_parser_name_lookup_error (cp_parser* parser,
1993			     tree name,
1994			     tree decl,
1995			     const char* desired)
1996{
1997  /* If name lookup completely failed, tell the user that NAME was not
1998     declared.  */
1999  if (decl == error_mark_node)
2000    {
2001      if (parser->scope && parser->scope != global_namespace)
2002	error ("%<%D::%D%> has not been declared",
2003	       parser->scope, name);
2004      else if (parser->scope == global_namespace)
2005	error ("%<::%D%> has not been declared", name);
2006      else if (parser->object_scope
2007	       && !CLASS_TYPE_P (parser->object_scope))
2008	error ("request for member %qD in non-class type %qT",
2009	       name, parser->object_scope);
2010      else if (parser->object_scope)
2011	error ("%<%T::%D%> has not been declared",
2012	       parser->object_scope, name);
2013      else
2014	error ("%qD has not been declared", name);
2015    }
2016  else if (parser->scope && parser->scope != global_namespace)
2017    error ("%<%D::%D%> %s", parser->scope, name, desired);
2018  else if (parser->scope == global_namespace)
2019    error ("%<::%D%> %s", name, desired);
2020  else
2021    error ("%qD %s", name, desired);
2022}
2023
2024/* If we are parsing tentatively, remember that an error has occurred
2025   during this tentative parse.  Returns true if the error was
2026   simulated; false if a message should be issued by the caller.  */
2027
2028static bool
2029cp_parser_simulate_error (cp_parser* parser)
2030{
2031  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2032    {
2033      parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2034      return true;
2035    }
2036  return false;
2037}
2038
2039/* Check for repeated decl-specifiers.  */
2040
2041static void
2042cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2043{
2044  cp_decl_spec ds;
2045
2046  for (ds = ds_first; ds != ds_last; ++ds)
2047    {
2048      unsigned count = decl_specs->specs[(int)ds];
2049      if (count < 2)
2050	continue;
2051      /* The "long" specifier is a special case because of "long long".  */
2052      if (ds == ds_long)
2053	{
2054	  if (count > 2)
2055	    error ("%<long long long%> is too long for GCC");
2056	  else if (pedantic && !in_system_header && warn_long_long)
2057	    pedwarn ("ISO C++ does not support %<long long%>");
2058	}
2059      else if (count > 1)
2060	{
2061	  static const char *const decl_spec_names[] = {
2062	    "signed",
2063	    "unsigned",
2064	    "short",
2065	    "long",
2066	    "const",
2067	    "volatile",
2068	    "restrict",
2069	    "inline",
2070	    "virtual",
2071	    "explicit",
2072	    "friend",
2073	    "typedef",
2074	    "__complex",
2075	    "__thread"
2076	  };
2077	  error ("duplicate %qs", decl_spec_names[(int)ds]);
2078	}
2079    }
2080}
2081
2082/* This function is called when a type is defined.  If type
2083   definitions are forbidden at this point, an error message is
2084   issued.  */
2085
2086static bool
2087cp_parser_check_type_definition (cp_parser* parser)
2088{
2089  /* If types are forbidden here, issue a message.  */
2090  if (parser->type_definition_forbidden_message)
2091    {
2092      /* Use `%s' to print the string in case there are any escape
2093	 characters in the message.  */
2094      error ("%s", parser->type_definition_forbidden_message);
2095      return false;
2096    }
2097  return true;
2098}
2099
2100/* This function is called when the DECLARATOR is processed.  The TYPE
2101   was a type defined in the decl-specifiers.  If it is invalid to
2102   define a type in the decl-specifiers for DECLARATOR, an error is
2103   issued.  */
2104
2105static void
2106cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2107					       tree type)
2108{
2109  /* [dcl.fct] forbids type definitions in return types.
2110     Unfortunately, it's not easy to know whether or not we are
2111     processing a return type until after the fact.  */
2112  while (declarator
2113	 && (declarator->kind == cdk_pointer
2114	     || declarator->kind == cdk_reference
2115	     || declarator->kind == cdk_ptrmem))
2116    declarator = declarator->declarator;
2117  if (declarator
2118      && declarator->kind == cdk_function)
2119    {
2120      error ("new types may not be defined in a return type");
2121      inform ("(perhaps a semicolon is missing after the definition of %qT)",
2122	      type);
2123    }
2124}
2125
2126/* A type-specifier (TYPE) has been parsed which cannot be followed by
2127   "<" in any valid C++ program.  If the next token is indeed "<",
2128   issue a message warning the user about what appears to be an
2129   invalid attempt to form a template-id.  */
2130
2131static void
2132cp_parser_check_for_invalid_template_id (cp_parser* parser,
2133					 tree type)
2134{
2135  cp_token_position start = 0;
2136
2137  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2138    {
2139      if (TYPE_P (type))
2140	error ("%qT is not a template", type);
2141      else if (TREE_CODE (type) == IDENTIFIER_NODE)
2142	error ("%qE is not a template", type);
2143      else
2144	error ("invalid template-id");
2145      /* Remember the location of the invalid "<".  */
2146      if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2147	start = cp_lexer_token_position (parser->lexer, true);
2148      /* Consume the "<".  */
2149      cp_lexer_consume_token (parser->lexer);
2150      /* Parse the template arguments.  */
2151      cp_parser_enclosed_template_argument_list (parser);
2152      /* Permanently remove the invalid template arguments so that
2153	 this error message is not issued again.  */
2154      if (start)
2155	cp_lexer_purge_tokens_after (parser->lexer, start);
2156    }
2157}
2158
2159/* If parsing an integral constant-expression, issue an error message
2160   about the fact that THING appeared and return true.  Otherwise,
2161   return false.  In either case, set
2162   PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2163
2164static bool
2165cp_parser_non_integral_constant_expression (cp_parser  *parser,
2166					    const char *thing)
2167{
2168  parser->non_integral_constant_expression_p = true;
2169  if (parser->integral_constant_expression_p)
2170    {
2171      if (!parser->allow_non_integral_constant_expression_p)
2172	{
2173	  error ("%s cannot appear in a constant-expression", thing);
2174	  return true;
2175	}
2176    }
2177  return false;
2178}
2179
2180/* Emit a diagnostic for an invalid type name.  SCOPE is the
2181   qualifying scope (or NULL, if none) for ID.  This function commits
2182   to the current active tentative parse, if any.  (Otherwise, the
2183   problematic construct might be encountered again later, resulting
2184   in duplicate error messages.)  */
2185
2186static void
2187cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2188{
2189  tree decl, old_scope;
2190  /* Try to lookup the identifier.  */
2191  old_scope = parser->scope;
2192  parser->scope = scope;
2193  decl = cp_parser_lookup_name_simple (parser, id);
2194  parser->scope = old_scope;
2195  /* If the lookup found a template-name, it means that the user forgot
2196  to specify an argument list. Emit a useful error message.  */
2197  if (TREE_CODE (decl) == TEMPLATE_DECL)
2198    error ("invalid use of template-name %qE without an argument list", decl);
2199  else if (TREE_CODE (id) == BIT_NOT_EXPR)
2200    error ("invalid use of destructor %qD as a type", id);
2201  else if (TREE_CODE (decl) == TYPE_DECL)
2202    /* Something like 'unsigned A a;'  */
2203    error ("invalid combination of multiple type-specifiers");
2204  else if (!parser->scope)
2205    {
2206      /* Issue an error message.  */
2207      error ("%qE does not name a type", id);
2208      /* If we're in a template class, it's possible that the user was
2209	 referring to a type from a base class.  For example:
2210
2211	   template <typename T> struct A { typedef T X; };
2212	   template <typename T> struct B : public A<T> { X x; };
2213
2214	 The user should have said "typename A<T>::X".  */
2215      if (processing_template_decl && current_class_type
2216	  && TYPE_BINFO (current_class_type))
2217	{
2218	  tree b;
2219
2220	  for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2221	       b;
2222	       b = TREE_CHAIN (b))
2223	    {
2224	      tree base_type = BINFO_TYPE (b);
2225	      if (CLASS_TYPE_P (base_type)
2226		  && dependent_type_p (base_type))
2227		{
2228		  tree field;
2229		  /* Go from a particular instantiation of the
2230		     template (which will have an empty TYPE_FIELDs),
2231		     to the main version.  */
2232		  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2233		  for (field = TYPE_FIELDS (base_type);
2234		       field;
2235		       field = TREE_CHAIN (field))
2236		    if (TREE_CODE (field) == TYPE_DECL
2237			&& DECL_NAME (field) == id)
2238		      {
2239			inform ("(perhaps %<typename %T::%E%> was intended)",
2240				BINFO_TYPE (b), id);
2241			break;
2242		      }
2243		  if (field)
2244		    break;
2245		}
2246	    }
2247	}
2248    }
2249  /* Here we diagnose qualified-ids where the scope is actually correct,
2250     but the identifier does not resolve to a valid type name.  */
2251  else if (parser->scope != error_mark_node)
2252    {
2253      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2254	error ("%qE in namespace %qE does not name a type",
2255	       id, parser->scope);
2256      else if (TYPE_P (parser->scope))
2257	error ("%qE in class %qT does not name a type", id, parser->scope);
2258      else
2259	gcc_unreachable ();
2260    }
2261  cp_parser_commit_to_tentative_parse (parser);
2262}
2263
2264/* Check for a common situation where a type-name should be present,
2265   but is not, and issue a sensible error message.  Returns true if an
2266   invalid type-name was detected.
2267
2268   The situation handled by this function are variable declarations of the
2269   form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2270   Usually, `ID' should name a type, but if we got here it means that it
2271   does not. We try to emit the best possible error message depending on
2272   how exactly the id-expression looks like.  */
2273
2274static bool
2275cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2276{
2277  tree id;
2278
2279  cp_parser_parse_tentatively (parser);
2280  id = cp_parser_id_expression (parser,
2281				/*template_keyword_p=*/false,
2282				/*check_dependency_p=*/true,
2283				/*template_p=*/NULL,
2284				/*declarator_p=*/true,
2285				/*optional_p=*/false);
2286  /* After the id-expression, there should be a plain identifier,
2287     otherwise this is not a simple variable declaration. Also, if
2288     the scope is dependent, we cannot do much.  */
2289  if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2290      || (parser->scope && TYPE_P (parser->scope)
2291	  && dependent_type_p (parser->scope))
2292      || TREE_CODE (id) == TYPE_DECL)
2293    {
2294      cp_parser_abort_tentative_parse (parser);
2295      return false;
2296    }
2297  if (!cp_parser_parse_definitely (parser))
2298    return false;
2299
2300  /* Emit a diagnostic for the invalid type.  */
2301  cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2302  /* Skip to the end of the declaration; there's no point in
2303     trying to process it.  */
2304  cp_parser_skip_to_end_of_block_or_statement (parser);
2305  return true;
2306}
2307
2308/* Consume tokens up to, and including, the next non-nested closing `)'.
2309   Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2310   are doing error recovery. Returns -1 if OR_COMMA is true and we
2311   found an unnested comma.  */
2312
2313static int
2314cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2315				       bool recovering,
2316				       bool or_comma,
2317				       bool consume_paren)
2318{
2319  unsigned paren_depth = 0;
2320  unsigned brace_depth = 0;
2321
2322  if (recovering && !or_comma
2323      && cp_parser_uncommitted_to_tentative_parse_p (parser))
2324    return 0;
2325
2326  while (true)
2327    {
2328      cp_token * token = cp_lexer_peek_token (parser->lexer);
2329
2330      switch (token->type)
2331	{
2332	case CPP_EOF:
2333	case CPP_PRAGMA_EOL:
2334	  /* If we've run out of tokens, then there is no closing `)'.  */
2335	  return 0;
2336
2337	case CPP_SEMICOLON:
2338	  /* This matches the processing in skip_to_end_of_statement.  */
2339	  if (!brace_depth)
2340	    return 0;
2341	  break;
2342
2343	case CPP_OPEN_BRACE:
2344	  ++brace_depth;
2345	  break;
2346	case CPP_CLOSE_BRACE:
2347	  if (!brace_depth--)
2348	    return 0;
2349	  break;
2350
2351	case CPP_COMMA:
2352	  if (recovering && or_comma && !brace_depth && !paren_depth)
2353	    return -1;
2354	  break;
2355
2356	case CPP_OPEN_PAREN:
2357	  if (!brace_depth)
2358	    ++paren_depth;
2359	  break;
2360
2361	case CPP_CLOSE_PAREN:
2362	  if (!brace_depth && !paren_depth--)
2363	    {
2364	      if (consume_paren)
2365		cp_lexer_consume_token (parser->lexer);
2366	      return 1;
2367	    }
2368	  break;
2369
2370	default:
2371	  break;
2372	}
2373
2374      /* Consume the token.  */
2375      cp_lexer_consume_token (parser->lexer);
2376    }
2377}
2378
2379/* Consume tokens until we reach the end of the current statement.
2380   Normally, that will be just before consuming a `;'.  However, if a
2381   non-nested `}' comes first, then we stop before consuming that.  */
2382
2383static void
2384cp_parser_skip_to_end_of_statement (cp_parser* parser)
2385{
2386  unsigned nesting_depth = 0;
2387
2388  while (true)
2389    {
2390      cp_token *token = cp_lexer_peek_token (parser->lexer);
2391
2392      switch (token->type)
2393	{
2394	case CPP_EOF:
2395	case CPP_PRAGMA_EOL:
2396	  /* If we've run out of tokens, stop.  */
2397	  return;
2398
2399	case CPP_SEMICOLON:
2400	  /* If the next token is a `;', we have reached the end of the
2401	     statement.  */
2402	  if (!nesting_depth)
2403	    return;
2404	  break;
2405
2406	case CPP_CLOSE_BRACE:
2407	  /* If this is a non-nested '}', stop before consuming it.
2408	     That way, when confronted with something like:
2409
2410	       { 3 + }
2411
2412	     we stop before consuming the closing '}', even though we
2413	     have not yet reached a `;'.  */
2414	  if (nesting_depth == 0)
2415	    return;
2416
2417	  /* If it is the closing '}' for a block that we have
2418	     scanned, stop -- but only after consuming the token.
2419	     That way given:
2420
2421		void f g () { ... }
2422		typedef int I;
2423
2424	     we will stop after the body of the erroneously declared
2425	     function, but before consuming the following `typedef'
2426	     declaration.  */
2427	  if (--nesting_depth == 0)
2428	    {
2429	      cp_lexer_consume_token (parser->lexer);
2430	      return;
2431	    }
2432
2433	case CPP_OPEN_BRACE:
2434	  ++nesting_depth;
2435	  break;
2436
2437	default:
2438	  break;
2439	}
2440
2441      /* Consume the token.  */
2442      cp_lexer_consume_token (parser->lexer);
2443    }
2444}
2445
2446/* This function is called at the end of a statement or declaration.
2447   If the next token is a semicolon, it is consumed; otherwise, error
2448   recovery is attempted.  */
2449
2450static void
2451cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2452{
2453  /* Look for the trailing `;'.  */
2454  if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2455    {
2456      /* If there is additional (erroneous) input, skip to the end of
2457	 the statement.  */
2458      cp_parser_skip_to_end_of_statement (parser);
2459      /* If the next token is now a `;', consume it.  */
2460      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2461	cp_lexer_consume_token (parser->lexer);
2462    }
2463}
2464
2465/* Skip tokens until we have consumed an entire block, or until we
2466   have consumed a non-nested `;'.  */
2467
2468static void
2469cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2470{
2471  int nesting_depth = 0;
2472
2473  while (nesting_depth >= 0)
2474    {
2475      cp_token *token = cp_lexer_peek_token (parser->lexer);
2476
2477      switch (token->type)
2478	{
2479	case CPP_EOF:
2480	case CPP_PRAGMA_EOL:
2481	  /* If we've run out of tokens, stop.  */
2482	  return;
2483
2484	case CPP_SEMICOLON:
2485	  /* Stop if this is an unnested ';'. */
2486	  if (!nesting_depth)
2487	    nesting_depth = -1;
2488	  break;
2489
2490	case CPP_CLOSE_BRACE:
2491	  /* Stop if this is an unnested '}', or closes the outermost
2492	     nesting level.  */
2493	  nesting_depth--;
2494	  if (!nesting_depth)
2495	    nesting_depth = -1;
2496	  break;
2497
2498	case CPP_OPEN_BRACE:
2499	  /* Nest. */
2500	  nesting_depth++;
2501	  break;
2502
2503	default:
2504	  break;
2505	}
2506
2507      /* Consume the token.  */
2508      cp_lexer_consume_token (parser->lexer);
2509    }
2510}
2511
2512/* Skip tokens until a non-nested closing curly brace is the next
2513   token.  */
2514
2515static void
2516cp_parser_skip_to_closing_brace (cp_parser *parser)
2517{
2518  unsigned nesting_depth = 0;
2519
2520  while (true)
2521    {
2522      cp_token *token = cp_lexer_peek_token (parser->lexer);
2523
2524      switch (token->type)
2525	{
2526	case CPP_EOF:
2527	case CPP_PRAGMA_EOL:
2528	  /* If we've run out of tokens, stop.  */
2529	  return;
2530
2531	case CPP_CLOSE_BRACE:
2532	  /* If the next token is a non-nested `}', then we have reached
2533	     the end of the current block.  */
2534	  if (nesting_depth-- == 0)
2535	    return;
2536	  break;
2537
2538	case CPP_OPEN_BRACE:
2539	  /* If it the next token is a `{', then we are entering a new
2540	     block.  Consume the entire block.  */
2541	  ++nesting_depth;
2542	  break;
2543
2544	default:
2545	  break;
2546	}
2547
2548      /* Consume the token.  */
2549      cp_lexer_consume_token (parser->lexer);
2550    }
2551}
2552
2553/* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2554   parameter is the PRAGMA token, allowing us to purge the entire pragma
2555   sequence.  */
2556
2557static void
2558cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2559{
2560  cp_token *token;
2561
2562  parser->lexer->in_pragma = false;
2563
2564  do
2565    token = cp_lexer_consume_token (parser->lexer);
2566  while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2567
2568  /* Ensure that the pragma is not parsed again.  */
2569  cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2570}
2571
2572/* Require pragma end of line, resyncing with it as necessary.  The
2573   arguments are as for cp_parser_skip_to_pragma_eol.  */
2574
2575static void
2576cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2577{
2578  parser->lexer->in_pragma = false;
2579  if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2580    cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2581}
2582
2583/* This is a simple wrapper around make_typename_type. When the id is
2584   an unresolved identifier node, we can provide a superior diagnostic
2585   using cp_parser_diagnose_invalid_type_name.  */
2586
2587static tree
2588cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2589{
2590  tree result;
2591  if (TREE_CODE (id) == IDENTIFIER_NODE)
2592    {
2593      result = make_typename_type (scope, id, typename_type,
2594				   /*complain=*/tf_none);
2595      if (result == error_mark_node)
2596	cp_parser_diagnose_invalid_type_name (parser, scope, id);
2597      return result;
2598    }
2599  return make_typename_type (scope, id, typename_type, tf_error);
2600}
2601
2602
2603/* Create a new C++ parser.  */
2604
2605static cp_parser *
2606cp_parser_new (void)
2607{
2608  cp_parser *parser;
2609  cp_lexer *lexer;
2610  unsigned i;
2611
2612  /* cp_lexer_new_main is called before calling ggc_alloc because
2613     cp_lexer_new_main might load a PCH file.  */
2614  lexer = cp_lexer_new_main ();
2615
2616  /* Initialize the binops_by_token so that we can get the tree
2617     directly from the token.  */
2618  for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2619    binops_by_token[binops[i].token_type] = binops[i];
2620
2621  parser = GGC_CNEW (cp_parser);
2622  parser->lexer = lexer;
2623  parser->context = cp_parser_context_new (NULL);
2624
2625  /* For now, we always accept GNU extensions.  */
2626  parser->allow_gnu_extensions_p = 1;
2627
2628  /* The `>' token is a greater-than operator, not the end of a
2629     template-id.  */
2630  parser->greater_than_is_operator_p = true;
2631
2632  parser->default_arg_ok_p = true;
2633
2634  /* We are not parsing a constant-expression.  */
2635  parser->integral_constant_expression_p = false;
2636  parser->allow_non_integral_constant_expression_p = false;
2637  parser->non_integral_constant_expression_p = false;
2638
2639  /* Local variable names are not forbidden.  */
2640  parser->local_variables_forbidden_p = false;
2641
2642  /* We are not processing an `extern "C"' declaration.  */
2643  parser->in_unbraced_linkage_specification_p = false;
2644
2645  /* We are not processing a declarator.  */
2646  parser->in_declarator_p = false;
2647
2648  /* We are not processing a template-argument-list.  */
2649  parser->in_template_argument_list_p = false;
2650
2651  /* We are not in an iteration statement.  */
2652  parser->in_statement = 0;
2653
2654  /* We are not in a switch statement.  */
2655  parser->in_switch_statement_p = false;
2656
2657  /* We are not parsing a type-id inside an expression.  */
2658  parser->in_type_id_in_expr_p = false;
2659
2660  /* Declarations aren't implicitly extern "C".  */
2661  parser->implicit_extern_c = false;
2662
2663  /* String literals should be translated to the execution character set.  */
2664  parser->translate_strings_p = true;
2665
2666  /* We are not parsing a function body.  */
2667  parser->in_function_body = false;
2668
2669  /* The unparsed function queue is empty.  */
2670  parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2671
2672  /* There are no classes being defined.  */
2673  parser->num_classes_being_defined = 0;
2674
2675  /* No template parameters apply.  */
2676  parser->num_template_parameter_lists = 0;
2677
2678  return parser;
2679}
2680
2681/* Create a cp_lexer structure which will emit the tokens in CACHE
2682   and push it onto the parser's lexer stack.  This is used for delayed
2683   parsing of in-class method bodies and default arguments, and should
2684   not be confused with tentative parsing.  */
2685static void
2686cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2687{
2688  cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2689  lexer->next = parser->lexer;
2690  parser->lexer = lexer;
2691
2692  /* Move the current source position to that of the first token in the
2693     new lexer.  */
2694  cp_lexer_set_source_position_from_token (lexer->next_token);
2695}
2696
2697/* Pop the top lexer off the parser stack.  This is never used for the
2698   "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2699static void
2700cp_parser_pop_lexer (cp_parser *parser)
2701{
2702  cp_lexer *lexer = parser->lexer;
2703  parser->lexer = lexer->next;
2704  cp_lexer_destroy (lexer);
2705
2706  /* Put the current source position back where it was before this
2707     lexer was pushed.  */
2708  cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2709}
2710
2711/* Lexical conventions [gram.lex]  */
2712
2713/* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2714   identifier.  */
2715
2716static tree
2717cp_parser_identifier (cp_parser* parser)
2718{
2719  cp_token *token;
2720
2721  /* Look for the identifier.  */
2722  token = cp_parser_require (parser, CPP_NAME, "identifier");
2723  /* Return the value.  */
2724  return token ? token->u.value : error_mark_node;
2725}
2726
2727/* Parse a sequence of adjacent string constants.  Returns a
2728   TREE_STRING representing the combined, nul-terminated string
2729   constant.  If TRANSLATE is true, translate the string to the
2730   execution character set.  If WIDE_OK is true, a wide string is
2731   invalid here.
2732
2733   C++98 [lex.string] says that if a narrow string literal token is
2734   adjacent to a wide string literal token, the behavior is undefined.
2735   However, C99 6.4.5p4 says that this results in a wide string literal.
2736   We follow C99 here, for consistency with the C front end.
2737
2738   This code is largely lifted from lex_string() in c-lex.c.
2739
2740   FUTURE: ObjC++ will need to handle @-strings here.  */
2741static tree
2742cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2743{
2744  tree value;
2745  bool wide = false;
2746  size_t count;
2747  struct obstack str_ob;
2748  cpp_string str, istr, *strs;
2749  cp_token *tok;
2750
2751  tok = cp_lexer_peek_token (parser->lexer);
2752  if (!cp_parser_is_string_literal (tok))
2753    {
2754      cp_parser_error (parser, "expected string-literal");
2755      return error_mark_node;
2756    }
2757
2758  /* Try to avoid the overhead of creating and destroying an obstack
2759     for the common case of just one string.  */
2760  if (!cp_parser_is_string_literal
2761      (cp_lexer_peek_nth_token (parser->lexer, 2)))
2762    {
2763      cp_lexer_consume_token (parser->lexer);
2764
2765      str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2766      str.len = TREE_STRING_LENGTH (tok->u.value);
2767      count = 1;
2768      if (tok->type == CPP_WSTRING)
2769	wide = true;
2770
2771      strs = &str;
2772    }
2773  else
2774    {
2775      gcc_obstack_init (&str_ob);
2776      count = 0;
2777
2778      do
2779	{
2780	  cp_lexer_consume_token (parser->lexer);
2781	  count++;
2782	  str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2783	  str.len = TREE_STRING_LENGTH (tok->u.value);
2784	  if (tok->type == CPP_WSTRING)
2785	    wide = true;
2786
2787	  obstack_grow (&str_ob, &str, sizeof (cpp_string));
2788
2789	  tok = cp_lexer_peek_token (parser->lexer);
2790	}
2791      while (cp_parser_is_string_literal (tok));
2792
2793      strs = (cpp_string *) obstack_finish (&str_ob);
2794    }
2795
2796  if (wide && !wide_ok)
2797    {
2798      cp_parser_error (parser, "a wide string is invalid in this context");
2799      wide = false;
2800    }
2801
2802  if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2803      (parse_in, strs, count, &istr, wide))
2804    {
2805      value = build_string (istr.len, (char *)istr.text);
2806      free ((void *)istr.text);
2807
2808      TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2809      value = fix_string_type (value);
2810    }
2811  else
2812    /* cpp_interpret_string has issued an error.  */
2813    value = error_mark_node;
2814
2815  if (count > 1)
2816    obstack_free (&str_ob, 0);
2817
2818  return value;
2819}
2820
2821
2822/* Basic concepts [gram.basic]  */
2823
2824/* Parse a translation-unit.
2825
2826   translation-unit:
2827     declaration-seq [opt]
2828
2829   Returns TRUE if all went well.  */
2830
2831static bool
2832cp_parser_translation_unit (cp_parser* parser)
2833{
2834  /* The address of the first non-permanent object on the declarator
2835     obstack.  */
2836  static void *declarator_obstack_base;
2837
2838  bool success;
2839
2840  /* Create the declarator obstack, if necessary.  */
2841  if (!cp_error_declarator)
2842    {
2843      gcc_obstack_init (&declarator_obstack);
2844      /* Create the error declarator.  */
2845      cp_error_declarator = make_declarator (cdk_error);
2846      /* Create the empty parameter list.  */
2847      no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2848      /* Remember where the base of the declarator obstack lies.  */
2849      declarator_obstack_base = obstack_next_free (&declarator_obstack);
2850    }
2851
2852  cp_parser_declaration_seq_opt (parser);
2853
2854  /* If there are no tokens left then all went well.  */
2855  if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2856    {
2857      /* Get rid of the token array; we don't need it any more.  */
2858      cp_lexer_destroy (parser->lexer);
2859      parser->lexer = NULL;
2860
2861      /* This file might have been a context that's implicitly extern
2862	 "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2863      if (parser->implicit_extern_c)
2864	{
2865	  pop_lang_context ();
2866	  parser->implicit_extern_c = false;
2867	}
2868
2869      /* Finish up.  */
2870      finish_translation_unit ();
2871
2872      success = true;
2873    }
2874  else
2875    {
2876      cp_parser_error (parser, "expected declaration");
2877      success = false;
2878    }
2879
2880  /* Make sure the declarator obstack was fully cleaned up.  */
2881  gcc_assert (obstack_next_free (&declarator_obstack)
2882	      == declarator_obstack_base);
2883
2884  /* All went well.  */
2885  return success;
2886}
2887
2888/* Expressions [gram.expr] */
2889
2890/* Parse a primary-expression.
2891
2892   primary-expression:
2893     literal
2894     this
2895     ( expression )
2896     id-expression
2897
2898   GNU Extensions:
2899
2900   primary-expression:
2901     ( compound-statement )
2902     __builtin_va_arg ( assignment-expression , type-id )
2903     __builtin_offsetof ( type-id , offsetof-expression )
2904
2905   Objective-C++ Extension:
2906
2907   primary-expression:
2908     objc-expression
2909
2910   literal:
2911     __null
2912
2913   ADDRESS_P is true iff this expression was immediately preceded by
2914   "&" and therefore might denote a pointer-to-member.  CAST_P is true
2915   iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2916   true iff this expression is a template argument.
2917
2918   Returns a representation of the expression.  Upon return, *IDK
2919   indicates what kind of id-expression (if any) was present.  */
2920
2921static tree
2922cp_parser_primary_expression (cp_parser *parser,
2923			      bool address_p,
2924			      bool cast_p,
2925			      bool template_arg_p,
2926			      cp_id_kind *idk)
2927{
2928  cp_token *token;
2929
2930  /* Assume the primary expression is not an id-expression.  */
2931  *idk = CP_ID_KIND_NONE;
2932
2933  /* Peek at the next token.  */
2934  token = cp_lexer_peek_token (parser->lexer);
2935  switch (token->type)
2936    {
2937      /* literal:
2938	   integer-literal
2939	   character-literal
2940	   floating-literal
2941	   string-literal
2942	   boolean-literal  */
2943    case CPP_CHAR:
2944    case CPP_WCHAR:
2945    case CPP_NUMBER:
2946      token = cp_lexer_consume_token (parser->lexer);
2947      /* Floating-point literals are only allowed in an integral
2948	 constant expression if they are cast to an integral or
2949	 enumeration type.  */
2950      if (TREE_CODE (token->u.value) == REAL_CST
2951	  && parser->integral_constant_expression_p
2952	  && pedantic)
2953	{
2954	  /* CAST_P will be set even in invalid code like "int(2.7 +
2955	     ...)".   Therefore, we have to check that the next token
2956	     is sure to end the cast.  */
2957	  if (cast_p)
2958	    {
2959	      cp_token *next_token;
2960
2961	      next_token = cp_lexer_peek_token (parser->lexer);
2962	      if (/* The comma at the end of an
2963		     enumerator-definition.  */
2964		  next_token->type != CPP_COMMA
2965		  /* The curly brace at the end of an enum-specifier.  */
2966		  && next_token->type != CPP_CLOSE_BRACE
2967		  /* The end of a statement.  */
2968		  && next_token->type != CPP_SEMICOLON
2969		  /* The end of the cast-expression.  */
2970		  && next_token->type != CPP_CLOSE_PAREN
2971		  /* The end of an array bound.  */
2972		  && next_token->type != CPP_CLOSE_SQUARE
2973		  /* The closing ">" in a template-argument-list.  */
2974		  && (next_token->type != CPP_GREATER
2975		      || parser->greater_than_is_operator_p))
2976		cast_p = false;
2977	    }
2978
2979	  /* If we are within a cast, then the constraint that the
2980	     cast is to an integral or enumeration type will be
2981	     checked at that point.  If we are not within a cast, then
2982	     this code is invalid.  */
2983	  if (!cast_p)
2984	    cp_parser_non_integral_constant_expression
2985	      (parser, "floating-point literal");
2986	}
2987      return token->u.value;
2988
2989    case CPP_STRING:
2990    case CPP_WSTRING:
2991      /* ??? Should wide strings be allowed when parser->translate_strings_p
2992	 is false (i.e. in attributes)?  If not, we can kill the third
2993	 argument to cp_parser_string_literal.  */
2994      return cp_parser_string_literal (parser,
2995				       parser->translate_strings_p,
2996				       true);
2997
2998    case CPP_OPEN_PAREN:
2999      {
3000	tree expr;
3001	bool saved_greater_than_is_operator_p;
3002
3003	/* Consume the `('.  */
3004	cp_lexer_consume_token (parser->lexer);
3005	/* Within a parenthesized expression, a `>' token is always
3006	   the greater-than operator.  */
3007	saved_greater_than_is_operator_p
3008	  = parser->greater_than_is_operator_p;
3009	parser->greater_than_is_operator_p = true;
3010	/* If we see `( { ' then we are looking at the beginning of
3011	   a GNU statement-expression.  */
3012	if (cp_parser_allow_gnu_extensions_p (parser)
3013	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3014	  {
3015	    /* Statement-expressions are not allowed by the standard.  */
3016	    if (pedantic)
3017	      pedwarn ("ISO C++ forbids braced-groups within expressions");
3018
3019	    /* And they're not allowed outside of a function-body; you
3020	       cannot, for example, write:
3021
3022		 int i = ({ int j = 3; j + 1; });
3023
3024	       at class or namespace scope.  */
3025	    if (!parser->in_function_body)
3026	      error ("statement-expressions are allowed only inside functions");
3027	    /* Start the statement-expression.  */
3028	    expr = begin_stmt_expr ();
3029	    /* Parse the compound-statement.  */
3030	    cp_parser_compound_statement (parser, expr, false);
3031	    /* Finish up.  */
3032	    expr = finish_stmt_expr (expr, false);
3033	  }
3034	else
3035	  {
3036	    /* Parse the parenthesized expression.  */
3037	    expr = cp_parser_expression (parser, cast_p);
3038	    /* Let the front end know that this expression was
3039	       enclosed in parentheses. This matters in case, for
3040	       example, the expression is of the form `A::B', since
3041	       `&A::B' might be a pointer-to-member, but `&(A::B)' is
3042	       not.  */
3043	    finish_parenthesized_expr (expr);
3044	  }
3045	/* The `>' token might be the end of a template-id or
3046	   template-parameter-list now.  */
3047	parser->greater_than_is_operator_p
3048	  = saved_greater_than_is_operator_p;
3049	/* Consume the `)'.  */
3050	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3051	  cp_parser_skip_to_end_of_statement (parser);
3052
3053	return expr;
3054      }
3055
3056    case CPP_KEYWORD:
3057      switch (token->keyword)
3058	{
3059	  /* These two are the boolean literals.  */
3060	case RID_TRUE:
3061	  cp_lexer_consume_token (parser->lexer);
3062	  return boolean_true_node;
3063	case RID_FALSE:
3064	  cp_lexer_consume_token (parser->lexer);
3065	  return boolean_false_node;
3066
3067	  /* The `__null' literal.  */
3068	case RID_NULL:
3069	  cp_lexer_consume_token (parser->lexer);
3070	  return null_node;
3071
3072	  /* Recognize the `this' keyword.  */
3073	case RID_THIS:
3074	  cp_lexer_consume_token (parser->lexer);
3075	  if (parser->local_variables_forbidden_p)
3076	    {
3077	      error ("%<this%> may not be used in this context");
3078	      return error_mark_node;
3079	    }
3080	  /* Pointers cannot appear in constant-expressions.  */
3081	  if (cp_parser_non_integral_constant_expression (parser,
3082							  "`this'"))
3083	    return error_mark_node;
3084	  return finish_this_expr ();
3085
3086	  /* The `operator' keyword can be the beginning of an
3087	     id-expression.  */
3088	case RID_OPERATOR:
3089	  goto id_expression;
3090
3091	case RID_FUNCTION_NAME:
3092	case RID_PRETTY_FUNCTION_NAME:
3093	case RID_C99_FUNCTION_NAME:
3094	  /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3095	     __func__ are the names of variables -- but they are
3096	     treated specially.  Therefore, they are handled here,
3097	     rather than relying on the generic id-expression logic
3098	     below.  Grammatically, these names are id-expressions.
3099
3100	     Consume the token.  */
3101	  token = cp_lexer_consume_token (parser->lexer);
3102	  /* Look up the name.  */
3103	  return finish_fname (token->u.value);
3104
3105	case RID_VA_ARG:
3106	  {
3107	    tree expression;
3108	    tree type;
3109
3110	    /* The `__builtin_va_arg' construct is used to handle
3111	       `va_arg'.  Consume the `__builtin_va_arg' token.  */
3112	    cp_lexer_consume_token (parser->lexer);
3113	    /* Look for the opening `('.  */
3114	    cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3115	    /* Now, parse the assignment-expression.  */
3116	    expression = cp_parser_assignment_expression (parser,
3117							  /*cast_p=*/false);
3118	    /* Look for the `,'.  */
3119	    cp_parser_require (parser, CPP_COMMA, "`,'");
3120	    /* Parse the type-id.  */
3121	    type = cp_parser_type_id (parser);
3122	    /* Look for the closing `)'.  */
3123	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3124	    /* Using `va_arg' in a constant-expression is not
3125	       allowed.  */
3126	    if (cp_parser_non_integral_constant_expression (parser,
3127							    "`va_arg'"))
3128	      return error_mark_node;
3129	    return build_x_va_arg (expression, type);
3130	  }
3131
3132	case RID_OFFSETOF:
3133	  return cp_parser_builtin_offsetof (parser);
3134
3135	  /* Objective-C++ expressions.  */
3136	case RID_AT_ENCODE:
3137	case RID_AT_PROTOCOL:
3138	case RID_AT_SELECTOR:
3139	  return cp_parser_objc_expression (parser);
3140
3141	default:
3142	  cp_parser_error (parser, "expected primary-expression");
3143	  return error_mark_node;
3144	}
3145
3146      /* An id-expression can start with either an identifier, a
3147	 `::' as the beginning of a qualified-id, or the "operator"
3148	 keyword.  */
3149    case CPP_NAME:
3150    case CPP_SCOPE:
3151    case CPP_TEMPLATE_ID:
3152    case CPP_NESTED_NAME_SPECIFIER:
3153      {
3154	tree id_expression;
3155	tree decl;
3156	const char *error_msg;
3157	bool template_p;
3158	bool done;
3159
3160      id_expression:
3161	/* Parse the id-expression.  */
3162	id_expression
3163	  = cp_parser_id_expression (parser,
3164				     /*template_keyword_p=*/false,
3165				     /*check_dependency_p=*/true,
3166				     &template_p,
3167				     /*declarator_p=*/false,
3168				     /*optional_p=*/false);
3169	if (id_expression == error_mark_node)
3170	  return error_mark_node;
3171	token = cp_lexer_peek_token (parser->lexer);
3172	done = (token->type != CPP_OPEN_SQUARE
3173		&& token->type != CPP_OPEN_PAREN
3174		&& token->type != CPP_DOT
3175		&& token->type != CPP_DEREF
3176		&& token->type != CPP_PLUS_PLUS
3177		&& token->type != CPP_MINUS_MINUS);
3178	/* If we have a template-id, then no further lookup is
3179	   required.  If the template-id was for a template-class, we
3180	   will sometimes have a TYPE_DECL at this point.  */
3181	if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3182		 || TREE_CODE (id_expression) == TYPE_DECL)
3183	  decl = id_expression;
3184	/* Look up the name.  */
3185	else
3186	  {
3187	    tree ambiguous_decls;
3188
3189	    decl = cp_parser_lookup_name (parser, id_expression,
3190					  none_type,
3191					  template_p,
3192					  /*is_namespace=*/false,
3193					  /*check_dependency=*/true,
3194					  &ambiguous_decls);
3195	    /* If the lookup was ambiguous, an error will already have
3196	       been issued.  */
3197	    if (ambiguous_decls)
3198	      return error_mark_node;
3199
3200	    /* In Objective-C++, an instance variable (ivar) may be preferred
3201	       to whatever cp_parser_lookup_name() found.  */
3202	    decl = objc_lookup_ivar (decl, id_expression);
3203
3204	    /* If name lookup gives us a SCOPE_REF, then the
3205	       qualifying scope was dependent.  */
3206	    if (TREE_CODE (decl) == SCOPE_REF)
3207	      {
3208		/* At this point, we do not know if DECL is a valid
3209		   integral constant expression.  We assume that it is
3210		   in fact such an expression, so that code like:
3211
3212		      template <int N> struct A {
3213			int a[B<N>::i];
3214		      };
3215
3216		   is accepted.  At template-instantiation time, we
3217		   will check that B<N>::i is actually a constant.  */
3218		return decl;
3219	      }
3220	    /* Check to see if DECL is a local variable in a context
3221	       where that is forbidden.  */
3222	    if (parser->local_variables_forbidden_p
3223		&& local_variable_p (decl))
3224	      {
3225		/* It might be that we only found DECL because we are
3226		   trying to be generous with pre-ISO scoping rules.
3227		   For example, consider:
3228
3229		     int i;
3230		     void g() {
3231		       for (int i = 0; i < 10; ++i) {}
3232		       extern void f(int j = i);
3233		     }
3234
3235		   Here, name look up will originally find the out
3236		   of scope `i'.  We need to issue a warning message,
3237		   but then use the global `i'.  */
3238		decl = check_for_out_of_scope_variable (decl);
3239		if (local_variable_p (decl))
3240		  {
3241		    error ("local variable %qD may not appear in this context",
3242			   decl);
3243		    return error_mark_node;
3244		  }
3245	      }
3246	  }
3247
3248	decl = (finish_id_expression
3249		(id_expression, decl, parser->scope,
3250		 idk,
3251		 parser->integral_constant_expression_p,
3252		 parser->allow_non_integral_constant_expression_p,
3253		 &parser->non_integral_constant_expression_p,
3254		 template_p, done, address_p,
3255		 template_arg_p,
3256		 &error_msg));
3257	if (error_msg)
3258	  cp_parser_error (parser, error_msg);
3259	return decl;
3260      }
3261
3262      /* Anything else is an error.  */
3263    default:
3264      /* ...unless we have an Objective-C++ message or string literal, that is.  */
3265      if (c_dialect_objc ()
3266	  && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3267	return cp_parser_objc_expression (parser);
3268
3269      cp_parser_error (parser, "expected primary-expression");
3270      return error_mark_node;
3271    }
3272}
3273
3274/* Parse an id-expression.
3275
3276   id-expression:
3277     unqualified-id
3278     qualified-id
3279
3280   qualified-id:
3281     :: [opt] nested-name-specifier template [opt] unqualified-id
3282     :: identifier
3283     :: operator-function-id
3284     :: template-id
3285
3286   Return a representation of the unqualified portion of the
3287   identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3288   a `::' or nested-name-specifier.
3289
3290   Often, if the id-expression was a qualified-id, the caller will
3291   want to make a SCOPE_REF to represent the qualified-id.  This
3292   function does not do this in order to avoid wastefully creating
3293   SCOPE_REFs when they are not required.
3294
3295   If TEMPLATE_KEYWORD_P is true, then we have just seen the
3296   `template' keyword.
3297
3298   If CHECK_DEPENDENCY_P is false, then names are looked up inside
3299   uninstantiated templates.
3300
3301   If *TEMPLATE_P is non-NULL, it is set to true iff the
3302   `template' keyword is used to explicitly indicate that the entity
3303   named is a template.
3304
3305   If DECLARATOR_P is true, the id-expression is appearing as part of
3306   a declarator, rather than as part of an expression.  */
3307
3308static tree
3309cp_parser_id_expression (cp_parser *parser,
3310			 bool template_keyword_p,
3311			 bool check_dependency_p,
3312			 bool *template_p,
3313			 bool declarator_p,
3314			 bool optional_p)
3315{
3316  bool global_scope_p;
3317  bool nested_name_specifier_p;
3318
3319  /* Assume the `template' keyword was not used.  */
3320  if (template_p)
3321    *template_p = template_keyword_p;
3322
3323  /* Look for the optional `::' operator.  */
3324  global_scope_p
3325    = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3326       != NULL_TREE);
3327  /* Look for the optional nested-name-specifier.  */
3328  nested_name_specifier_p
3329    = (cp_parser_nested_name_specifier_opt (parser,
3330					    /*typename_keyword_p=*/false,
3331					    check_dependency_p,
3332					    /*type_p=*/false,
3333					    declarator_p)
3334       != NULL_TREE);
3335  /* If there is a nested-name-specifier, then we are looking at
3336     the first qualified-id production.  */
3337  if (nested_name_specifier_p)
3338    {
3339      tree saved_scope;
3340      tree saved_object_scope;
3341      tree saved_qualifying_scope;
3342      tree unqualified_id;
3343      bool is_template;
3344
3345      /* See if the next token is the `template' keyword.  */
3346      if (!template_p)
3347	template_p = &is_template;
3348      *template_p = cp_parser_optional_template_keyword (parser);
3349      /* Name lookup we do during the processing of the
3350	 unqualified-id might obliterate SCOPE.  */
3351      saved_scope = parser->scope;
3352      saved_object_scope = parser->object_scope;
3353      saved_qualifying_scope = parser->qualifying_scope;
3354      /* Process the final unqualified-id.  */
3355      unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3356						 check_dependency_p,
3357						 declarator_p,
3358						 /*optional_p=*/false);
3359      /* Restore the SAVED_SCOPE for our caller.  */
3360      parser->scope = saved_scope;
3361      parser->object_scope = saved_object_scope;
3362      parser->qualifying_scope = saved_qualifying_scope;
3363
3364      return unqualified_id;
3365    }
3366  /* Otherwise, if we are in global scope, then we are looking at one
3367     of the other qualified-id productions.  */
3368  else if (global_scope_p)
3369    {
3370      cp_token *token;
3371      tree id;
3372
3373      /* Peek at the next token.  */
3374      token = cp_lexer_peek_token (parser->lexer);
3375
3376      /* If it's an identifier, and the next token is not a "<", then
3377	 we can avoid the template-id case.  This is an optimization
3378	 for this common case.  */
3379      if (token->type == CPP_NAME
3380	  && !cp_parser_nth_token_starts_template_argument_list_p
3381	       (parser, 2))
3382	return cp_parser_identifier (parser);
3383
3384      cp_parser_parse_tentatively (parser);
3385      /* Try a template-id.  */
3386      id = cp_parser_template_id (parser,
3387				  /*template_keyword_p=*/false,
3388				  /*check_dependency_p=*/true,
3389				  declarator_p);
3390      /* If that worked, we're done.  */
3391      if (cp_parser_parse_definitely (parser))
3392	return id;
3393
3394      /* Peek at the next token.  (Changes in the token buffer may
3395	 have invalidated the pointer obtained above.)  */
3396      token = cp_lexer_peek_token (parser->lexer);
3397
3398      switch (token->type)
3399	{
3400	case CPP_NAME:
3401	  return cp_parser_identifier (parser);
3402
3403	case CPP_KEYWORD:
3404	  if (token->keyword == RID_OPERATOR)
3405	    return cp_parser_operator_function_id (parser);
3406	  /* Fall through.  */
3407
3408	default:
3409	  cp_parser_error (parser, "expected id-expression");
3410	  return error_mark_node;
3411	}
3412    }
3413  else
3414    return cp_parser_unqualified_id (parser, template_keyword_p,
3415				     /*check_dependency_p=*/true,
3416				     declarator_p,
3417				     optional_p);
3418}
3419
3420/* Parse an unqualified-id.
3421
3422   unqualified-id:
3423     identifier
3424     operator-function-id
3425     conversion-function-id
3426     ~ class-name
3427     template-id
3428
3429   If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3430   keyword, in a construct like `A::template ...'.
3431
3432   Returns a representation of unqualified-id.  For the `identifier'
3433   production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3434   production a BIT_NOT_EXPR is returned; the operand of the
3435   BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3436   other productions, see the documentation accompanying the
3437   corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3438   names are looked up in uninstantiated templates.  If DECLARATOR_P
3439   is true, the unqualified-id is appearing as part of a declarator,
3440   rather than as part of an expression.  */
3441
3442static tree
3443cp_parser_unqualified_id (cp_parser* parser,
3444			  bool template_keyword_p,
3445			  bool check_dependency_p,
3446			  bool declarator_p,
3447			  bool optional_p)
3448{
3449  cp_token *token;
3450
3451  /* Peek at the next token.  */
3452  token = cp_lexer_peek_token (parser->lexer);
3453
3454  switch (token->type)
3455    {
3456    case CPP_NAME:
3457      {
3458	tree id;
3459
3460	/* We don't know yet whether or not this will be a
3461	   template-id.  */
3462	cp_parser_parse_tentatively (parser);
3463	/* Try a template-id.  */
3464	id = cp_parser_template_id (parser, template_keyword_p,
3465				    check_dependency_p,
3466				    declarator_p);
3467	/* If it worked, we're done.  */
3468	if (cp_parser_parse_definitely (parser))
3469	  return id;
3470	/* Otherwise, it's an ordinary identifier.  */
3471	return cp_parser_identifier (parser);
3472      }
3473
3474    case CPP_TEMPLATE_ID:
3475      return cp_parser_template_id (parser, template_keyword_p,
3476				    check_dependency_p,
3477				    declarator_p);
3478
3479    case CPP_COMPL:
3480      {
3481	tree type_decl;
3482	tree qualifying_scope;
3483	tree object_scope;
3484	tree scope;
3485	bool done;
3486
3487	/* Consume the `~' token.  */
3488	cp_lexer_consume_token (parser->lexer);
3489	/* Parse the class-name.  The standard, as written, seems to
3490	   say that:
3491
3492	     template <typename T> struct S { ~S (); };
3493	     template <typename T> S<T>::~S() {}
3494
3495	   is invalid, since `~' must be followed by a class-name, but
3496	   `S<T>' is dependent, and so not known to be a class.
3497	   That's not right; we need to look in uninstantiated
3498	   templates.  A further complication arises from:
3499
3500	     template <typename T> void f(T t) {
3501	       t.T::~T();
3502	     }
3503
3504	   Here, it is not possible to look up `T' in the scope of `T'
3505	   itself.  We must look in both the current scope, and the
3506	   scope of the containing complete expression.
3507
3508	   Yet another issue is:
3509
3510	     struct S {
3511	       int S;
3512	       ~S();
3513	     };
3514
3515	     S::~S() {}
3516
3517	   The standard does not seem to say that the `S' in `~S'
3518	   should refer to the type `S' and not the data member
3519	   `S::S'.  */
3520
3521	/* DR 244 says that we look up the name after the "~" in the
3522	   same scope as we looked up the qualifying name.  That idea
3523	   isn't fully worked out; it's more complicated than that.  */
3524	scope = parser->scope;
3525	object_scope = parser->object_scope;
3526	qualifying_scope = parser->qualifying_scope;
3527
3528	/* Check for invalid scopes.  */
3529	if (scope == error_mark_node)
3530	  {
3531	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3532	      cp_lexer_consume_token (parser->lexer);
3533	    return error_mark_node;
3534	  }
3535	if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3536	  {
3537	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3538	      error ("scope %qT before %<~%> is not a class-name", scope);
3539	    cp_parser_simulate_error (parser);
3540	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3541	      cp_lexer_consume_token (parser->lexer);
3542	    return error_mark_node;
3543	  }
3544	gcc_assert (!scope || TYPE_P (scope));
3545
3546	/* If the name is of the form "X::~X" it's OK.  */
3547	token = cp_lexer_peek_token (parser->lexer);
3548	if (scope
3549	    && token->type == CPP_NAME
3550	    && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3551		== CPP_OPEN_PAREN)
3552	    && constructor_name_p (token->u.value, scope))
3553	  {
3554	    cp_lexer_consume_token (parser->lexer);
3555	    return build_nt (BIT_NOT_EXPR, scope);
3556	  }
3557
3558	/* If there was an explicit qualification (S::~T), first look
3559	   in the scope given by the qualification (i.e., S).  */
3560	done = false;
3561	type_decl = NULL_TREE;
3562	if (scope)
3563	  {
3564	    cp_parser_parse_tentatively (parser);
3565	    type_decl = cp_parser_class_name (parser,
3566					      /*typename_keyword_p=*/false,
3567					      /*template_keyword_p=*/false,
3568					      none_type,
3569					      /*check_dependency=*/false,
3570					      /*class_head_p=*/false,
3571					      declarator_p);
3572	    if (cp_parser_parse_definitely (parser))
3573	      done = true;
3574	  }
3575	/* In "N::S::~S", look in "N" as well.  */
3576	if (!done && scope && qualifying_scope)
3577	  {
3578	    cp_parser_parse_tentatively (parser);
3579	    parser->scope = qualifying_scope;
3580	    parser->object_scope = NULL_TREE;
3581	    parser->qualifying_scope = NULL_TREE;
3582	    type_decl
3583	      = cp_parser_class_name (parser,
3584				      /*typename_keyword_p=*/false,
3585				      /*template_keyword_p=*/false,
3586				      none_type,
3587				      /*check_dependency=*/false,
3588				      /*class_head_p=*/false,
3589				      declarator_p);
3590	    if (cp_parser_parse_definitely (parser))
3591	      done = true;
3592	  }
3593	/* In "p->S::~T", look in the scope given by "*p" as well.  */
3594	else if (!done && object_scope)
3595	  {
3596	    cp_parser_parse_tentatively (parser);
3597	    parser->scope = object_scope;
3598	    parser->object_scope = NULL_TREE;
3599	    parser->qualifying_scope = NULL_TREE;
3600	    type_decl
3601	      = cp_parser_class_name (parser,
3602				      /*typename_keyword_p=*/false,
3603				      /*template_keyword_p=*/false,
3604				      none_type,
3605				      /*check_dependency=*/false,
3606				      /*class_head_p=*/false,
3607				      declarator_p);
3608	    if (cp_parser_parse_definitely (parser))
3609	      done = true;
3610	  }
3611	/* Look in the surrounding context.  */
3612	if (!done)
3613	  {
3614	    parser->scope = NULL_TREE;
3615	    parser->object_scope = NULL_TREE;
3616	    parser->qualifying_scope = NULL_TREE;
3617	    type_decl
3618	      = cp_parser_class_name (parser,
3619				      /*typename_keyword_p=*/false,
3620				      /*template_keyword_p=*/false,
3621				      none_type,
3622				      /*check_dependency=*/false,
3623				      /*class_head_p=*/false,
3624				      declarator_p);
3625	  }
3626	/* If an error occurred, assume that the name of the
3627	   destructor is the same as the name of the qualifying
3628	   class.  That allows us to keep parsing after running
3629	   into ill-formed destructor names.  */
3630	if (type_decl == error_mark_node && scope)
3631	  return build_nt (BIT_NOT_EXPR, scope);
3632	else if (type_decl == error_mark_node)
3633	  return error_mark_node;
3634
3635	/* Check that destructor name and scope match.  */
3636	if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3637	  {
3638	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3639	      error ("declaration of %<~%T%> as member of %qT",
3640		     type_decl, scope);
3641	    cp_parser_simulate_error (parser);
3642	    return error_mark_node;
3643	  }
3644
3645	/* [class.dtor]
3646
3647	   A typedef-name that names a class shall not be used as the
3648	   identifier in the declarator for a destructor declaration.  */
3649	if (declarator_p
3650	    && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3651	    && !DECL_SELF_REFERENCE_P (type_decl)
3652	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3653	  error ("typedef-name %qD used as destructor declarator",
3654		 type_decl);
3655
3656	return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3657      }
3658
3659    case CPP_KEYWORD:
3660      if (token->keyword == RID_OPERATOR)
3661	{
3662	  tree id;
3663
3664	  /* This could be a template-id, so we try that first.  */
3665	  cp_parser_parse_tentatively (parser);
3666	  /* Try a template-id.  */
3667	  id = cp_parser_template_id (parser, template_keyword_p,
3668				      /*check_dependency_p=*/true,
3669				      declarator_p);
3670	  /* If that worked, we're done.  */
3671	  if (cp_parser_parse_definitely (parser))
3672	    return id;
3673	  /* We still don't know whether we're looking at an
3674	     operator-function-id or a conversion-function-id.  */
3675	  cp_parser_parse_tentatively (parser);
3676	  /* Try an operator-function-id.  */
3677	  id = cp_parser_operator_function_id (parser);
3678	  /* If that didn't work, try a conversion-function-id.  */
3679	  if (!cp_parser_parse_definitely (parser))
3680	    id = cp_parser_conversion_function_id (parser);
3681
3682	  return id;
3683	}
3684      /* Fall through.  */
3685
3686    default:
3687      if (optional_p)
3688	return NULL_TREE;
3689      cp_parser_error (parser, "expected unqualified-id");
3690      return error_mark_node;
3691    }
3692}
3693
3694/* Parse an (optional) nested-name-specifier.
3695
3696   nested-name-specifier:
3697     class-or-namespace-name :: nested-name-specifier [opt]
3698     class-or-namespace-name :: template nested-name-specifier [opt]
3699
3700   PARSER->SCOPE should be set appropriately before this function is
3701   called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3702   effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3703   in name lookups.
3704
3705   Sets PARSER->SCOPE to the class (TYPE) or namespace
3706   (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3707   it unchanged if there is no nested-name-specifier.  Returns the new
3708   scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3709
3710   If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3711   part of a declaration and/or decl-specifier.  */
3712
3713static tree
3714cp_parser_nested_name_specifier_opt (cp_parser *parser,
3715				     bool typename_keyword_p,
3716				     bool check_dependency_p,
3717				     bool type_p,
3718				     bool is_declaration)
3719{
3720  bool success = false;
3721  cp_token_position start = 0;
3722  cp_token *token;
3723
3724  /* Remember where the nested-name-specifier starts.  */
3725  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3726    {
3727      start = cp_lexer_token_position (parser->lexer, false);
3728      push_deferring_access_checks (dk_deferred);
3729    }
3730
3731  while (true)
3732    {
3733      tree new_scope;
3734      tree old_scope;
3735      tree saved_qualifying_scope;
3736      bool template_keyword_p;
3737
3738      /* Spot cases that cannot be the beginning of a
3739	 nested-name-specifier.  */
3740      token = cp_lexer_peek_token (parser->lexer);
3741
3742      /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3743	 the already parsed nested-name-specifier.  */
3744      if (token->type == CPP_NESTED_NAME_SPECIFIER)
3745	{
3746	  /* Grab the nested-name-specifier and continue the loop.  */
3747	  cp_parser_pre_parsed_nested_name_specifier (parser);
3748	  /* If we originally encountered this nested-name-specifier
3749	     with IS_DECLARATION set to false, we will not have
3750	     resolved TYPENAME_TYPEs, so we must do so here.  */
3751	  if (is_declaration
3752	      && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3753	    {
3754	      new_scope = resolve_typename_type (parser->scope,
3755						 /*only_current_p=*/false);
3756	      if (new_scope != error_mark_node)
3757		parser->scope = new_scope;
3758	    }
3759	  success = true;
3760	  continue;
3761	}
3762
3763      /* Spot cases that cannot be the beginning of a
3764	 nested-name-specifier.  On the second and subsequent times
3765	 through the loop, we look for the `template' keyword.  */
3766      if (success && token->keyword == RID_TEMPLATE)
3767	;
3768      /* A template-id can start a nested-name-specifier.  */
3769      else if (token->type == CPP_TEMPLATE_ID)
3770	;
3771      else
3772	{
3773	  /* If the next token is not an identifier, then it is
3774	     definitely not a class-or-namespace-name.  */
3775	  if (token->type != CPP_NAME)
3776	    break;
3777	  /* If the following token is neither a `<' (to begin a
3778	     template-id), nor a `::', then we are not looking at a
3779	     nested-name-specifier.  */
3780	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
3781	  if (token->type != CPP_SCOPE
3782	      && !cp_parser_nth_token_starts_template_argument_list_p
3783		  (parser, 2))
3784	    break;
3785	}
3786
3787      /* The nested-name-specifier is optional, so we parse
3788	 tentatively.  */
3789      cp_parser_parse_tentatively (parser);
3790
3791      /* Look for the optional `template' keyword, if this isn't the
3792	 first time through the loop.  */
3793      if (success)
3794	template_keyword_p = cp_parser_optional_template_keyword (parser);
3795      else
3796	template_keyword_p = false;
3797
3798      /* Save the old scope since the name lookup we are about to do
3799	 might destroy it.  */
3800      old_scope = parser->scope;
3801      saved_qualifying_scope = parser->qualifying_scope;
3802      /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3803	 look up names in "X<T>::I" in order to determine that "Y" is
3804	 a template.  So, if we have a typename at this point, we make
3805	 an effort to look through it.  */
3806      if (is_declaration
3807	  && !typename_keyword_p
3808	  && parser->scope
3809	  && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3810	parser->scope = resolve_typename_type (parser->scope,
3811					       /*only_current_p=*/false);
3812      /* Parse the qualifying entity.  */
3813      new_scope
3814	= cp_parser_class_or_namespace_name (parser,
3815					     typename_keyword_p,
3816					     template_keyword_p,
3817					     check_dependency_p,
3818					     type_p,
3819					     is_declaration);
3820      /* Look for the `::' token.  */
3821      cp_parser_require (parser, CPP_SCOPE, "`::'");
3822
3823      /* If we found what we wanted, we keep going; otherwise, we're
3824	 done.  */
3825      if (!cp_parser_parse_definitely (parser))
3826	{
3827	  bool error_p = false;
3828
3829	  /* Restore the OLD_SCOPE since it was valid before the
3830	     failed attempt at finding the last
3831	     class-or-namespace-name.  */
3832	  parser->scope = old_scope;
3833	  parser->qualifying_scope = saved_qualifying_scope;
3834	  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3835	    break;
3836	  /* If the next token is an identifier, and the one after
3837	     that is a `::', then any valid interpretation would have
3838	     found a class-or-namespace-name.  */
3839	  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3840		 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3841		     == CPP_SCOPE)
3842		 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3843		     != CPP_COMPL))
3844	    {
3845	      token = cp_lexer_consume_token (parser->lexer);
3846	      if (!error_p)
3847		{
3848		  if (!token->ambiguous_p)
3849		    {
3850		      tree decl;
3851		      tree ambiguous_decls;
3852
3853		      decl = cp_parser_lookup_name (parser, token->u.value,
3854						    none_type,
3855						    /*is_template=*/false,
3856						    /*is_namespace=*/false,
3857						    /*check_dependency=*/true,
3858						    &ambiguous_decls);
3859		      if (TREE_CODE (decl) == TEMPLATE_DECL)
3860			error ("%qD used without template parameters", decl);
3861		      else if (ambiguous_decls)
3862			{
3863			  error ("reference to %qD is ambiguous",
3864				 token->u.value);
3865			  print_candidates (ambiguous_decls);
3866			  decl = error_mark_node;
3867			}
3868		      else
3869			cp_parser_name_lookup_error
3870			  (parser, token->u.value, decl,
3871			   "is not a class or namespace");
3872		    }
3873		  parser->scope = error_mark_node;
3874		  error_p = true;
3875		  /* Treat this as a successful nested-name-specifier
3876		     due to:
3877
3878		     [basic.lookup.qual]
3879
3880		     If the name found is not a class-name (clause
3881		     _class_) or namespace-name (_namespace.def_), the
3882		     program is ill-formed.  */
3883		  success = true;
3884		}
3885	      cp_lexer_consume_token (parser->lexer);
3886	    }
3887	  break;
3888	}
3889      /* We've found one valid nested-name-specifier.  */
3890      success = true;
3891      /* Name lookup always gives us a DECL.  */
3892      if (TREE_CODE (new_scope) == TYPE_DECL)
3893	new_scope = TREE_TYPE (new_scope);
3894      /* Uses of "template" must be followed by actual templates.  */
3895      if (template_keyword_p
3896	  && !(CLASS_TYPE_P (new_scope)
3897	       && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3898		    && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3899		   || CLASSTYPE_IS_TEMPLATE (new_scope)))
3900	  && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3901	       && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3902		   == TEMPLATE_ID_EXPR)))
3903	pedwarn (TYPE_P (new_scope)
3904		 ? "%qT is not a template"
3905		 : "%qD is not a template",
3906		 new_scope);
3907      /* If it is a class scope, try to complete it; we are about to
3908	 be looking up names inside the class.  */
3909      if (TYPE_P (new_scope)
3910	  /* Since checking types for dependency can be expensive,
3911	     avoid doing it if the type is already complete.  */
3912	  && !COMPLETE_TYPE_P (new_scope)
3913	  /* Do not try to complete dependent types.  */
3914	  && !dependent_type_p (new_scope))
3915	new_scope = complete_type (new_scope);
3916      /* Make sure we look in the right scope the next time through
3917	 the loop.  */
3918      parser->scope = new_scope;
3919    }
3920
3921  /* If parsing tentatively, replace the sequence of tokens that makes
3922     up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3923     token.  That way, should we re-parse the token stream, we will
3924     not have to repeat the effort required to do the parse, nor will
3925     we issue duplicate error messages.  */
3926  if (success && start)
3927    {
3928      cp_token *token;
3929
3930      token = cp_lexer_token_at (parser->lexer, start);
3931      /* Reset the contents of the START token.  */
3932      token->type = CPP_NESTED_NAME_SPECIFIER;
3933      /* Retrieve any deferred checks.  Do not pop this access checks yet
3934	 so the memory will not be reclaimed during token replacing below.  */
3935      token->u.tree_check_value = GGC_CNEW (struct tree_check);
3936      token->u.tree_check_value->value = parser->scope;
3937      token->u.tree_check_value->checks = get_deferred_access_checks ();
3938      token->u.tree_check_value->qualifying_scope =
3939	parser->qualifying_scope;
3940      token->keyword = RID_MAX;
3941
3942      /* Purge all subsequent tokens.  */
3943      cp_lexer_purge_tokens_after (parser->lexer, start);
3944    }
3945
3946  if (start)
3947    pop_to_parent_deferring_access_checks ();
3948
3949  return success ? parser->scope : NULL_TREE;
3950}
3951
3952/* Parse a nested-name-specifier.  See
3953   cp_parser_nested_name_specifier_opt for details.  This function
3954   behaves identically, except that it will an issue an error if no
3955   nested-name-specifier is present.  */
3956
3957static tree
3958cp_parser_nested_name_specifier (cp_parser *parser,
3959				 bool typename_keyword_p,
3960				 bool check_dependency_p,
3961				 bool type_p,
3962				 bool is_declaration)
3963{
3964  tree scope;
3965
3966  /* Look for the nested-name-specifier.  */
3967  scope = cp_parser_nested_name_specifier_opt (parser,
3968					       typename_keyword_p,
3969					       check_dependency_p,
3970					       type_p,
3971					       is_declaration);
3972  /* If it was not present, issue an error message.  */
3973  if (!scope)
3974    {
3975      cp_parser_error (parser, "expected nested-name-specifier");
3976      parser->scope = NULL_TREE;
3977    }
3978
3979  return scope;
3980}
3981
3982/* Parse a class-or-namespace-name.
3983
3984   class-or-namespace-name:
3985     class-name
3986     namespace-name
3987
3988   TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3989   TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3990   CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3991   TYPE_P is TRUE iff the next name should be taken as a class-name,
3992   even the same name is declared to be another entity in the same
3993   scope.
3994
3995   Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3996   specified by the class-or-namespace-name.  If neither is found the
3997   ERROR_MARK_NODE is returned.  */
3998
3999static tree
4000cp_parser_class_or_namespace_name (cp_parser *parser,
4001				   bool typename_keyword_p,
4002				   bool template_keyword_p,
4003				   bool check_dependency_p,
4004				   bool type_p,
4005				   bool is_declaration)
4006{
4007  tree saved_scope;
4008  tree saved_qualifying_scope;
4009  tree saved_object_scope;
4010  tree scope;
4011  bool only_class_p;
4012
4013  /* Before we try to parse the class-name, we must save away the
4014     current PARSER->SCOPE since cp_parser_class_name will destroy
4015     it.  */
4016  saved_scope = parser->scope;
4017  saved_qualifying_scope = parser->qualifying_scope;
4018  saved_object_scope = parser->object_scope;
4019  /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4020     there is no need to look for a namespace-name.  */
4021  only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4022  if (!only_class_p)
4023    cp_parser_parse_tentatively (parser);
4024  scope = cp_parser_class_name (parser,
4025				typename_keyword_p,
4026				template_keyword_p,
4027				type_p ? class_type : none_type,
4028				check_dependency_p,
4029				/*class_head_p=*/false,
4030				is_declaration);
4031  /* If that didn't work, try for a namespace-name.  */
4032  if (!only_class_p && !cp_parser_parse_definitely (parser))
4033    {
4034      /* Restore the saved scope.  */
4035      parser->scope = saved_scope;
4036      parser->qualifying_scope = saved_qualifying_scope;
4037      parser->object_scope = saved_object_scope;
4038      /* If we are not looking at an identifier followed by the scope
4039	 resolution operator, then this is not part of a
4040	 nested-name-specifier.  (Note that this function is only used
4041	 to parse the components of a nested-name-specifier.)  */
4042      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4043	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4044	return error_mark_node;
4045      scope = cp_parser_namespace_name (parser);
4046    }
4047
4048  return scope;
4049}
4050
4051/* Parse a postfix-expression.
4052
4053   postfix-expression:
4054     primary-expression
4055     postfix-expression [ expression ]
4056     postfix-expression ( expression-list [opt] )
4057     simple-type-specifier ( expression-list [opt] )
4058     typename :: [opt] nested-name-specifier identifier
4059       ( expression-list [opt] )
4060     typename :: [opt] nested-name-specifier template [opt] template-id
4061       ( expression-list [opt] )
4062     postfix-expression . template [opt] id-expression
4063     postfix-expression -> template [opt] id-expression
4064     postfix-expression . pseudo-destructor-name
4065     postfix-expression -> pseudo-destructor-name
4066     postfix-expression ++
4067     postfix-expression --
4068     dynamic_cast < type-id > ( expression )
4069     static_cast < type-id > ( expression )
4070     reinterpret_cast < type-id > ( expression )
4071     const_cast < type-id > ( expression )
4072     typeid ( expression )
4073     typeid ( type-id )
4074
4075   GNU Extension:
4076
4077   postfix-expression:
4078     ( type-id ) { initializer-list , [opt] }
4079
4080   This extension is a GNU version of the C99 compound-literal
4081   construct.  (The C99 grammar uses `type-name' instead of `type-id',
4082   but they are essentially the same concept.)
4083
4084   If ADDRESS_P is true, the postfix expression is the operand of the
4085   `&' operator.  CAST_P is true if this expression is the target of a
4086   cast.
4087
4088   Returns a representation of the expression.  */
4089
4090static tree
4091cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4092{
4093  cp_token *token;
4094  enum rid keyword;
4095  cp_id_kind idk = CP_ID_KIND_NONE;
4096  tree postfix_expression = NULL_TREE;
4097
4098  /* Peek at the next token.  */
4099  token = cp_lexer_peek_token (parser->lexer);
4100  /* Some of the productions are determined by keywords.  */
4101  keyword = token->keyword;
4102  switch (keyword)
4103    {
4104    case RID_DYNCAST:
4105    case RID_STATCAST:
4106    case RID_REINTCAST:
4107    case RID_CONSTCAST:
4108      {
4109	tree type;
4110	tree expression;
4111	const char *saved_message;
4112
4113	/* All of these can be handled in the same way from the point
4114	   of view of parsing.  Begin by consuming the token
4115	   identifying the cast.  */
4116	cp_lexer_consume_token (parser->lexer);
4117
4118	/* New types cannot be defined in the cast.  */
4119	saved_message = parser->type_definition_forbidden_message;
4120	parser->type_definition_forbidden_message
4121	  = "types may not be defined in casts";
4122
4123	/* Look for the opening `<'.  */
4124	cp_parser_require (parser, CPP_LESS, "`<'");
4125	/* Parse the type to which we are casting.  */
4126	type = cp_parser_type_id (parser);
4127	/* Look for the closing `>'.  */
4128	cp_parser_require (parser, CPP_GREATER, "`>'");
4129	/* Restore the old message.  */
4130	parser->type_definition_forbidden_message = saved_message;
4131
4132	/* And the expression which is being cast.  */
4133	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4134	expression = cp_parser_expression (parser, /*cast_p=*/true);
4135	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4136
4137	/* Only type conversions to integral or enumeration types
4138	   can be used in constant-expressions.  */
4139	if (!cast_valid_in_integral_constant_expression_p (type)
4140	    && (cp_parser_non_integral_constant_expression
4141		(parser,
4142		 "a cast to a type other than an integral or "
4143		 "enumeration type")))
4144	  return error_mark_node;
4145
4146	switch (keyword)
4147	  {
4148	  case RID_DYNCAST:
4149	    postfix_expression
4150	      = build_dynamic_cast (type, expression);
4151	    break;
4152	  case RID_STATCAST:
4153	    postfix_expression
4154	      = build_static_cast (type, expression);
4155	    break;
4156	  case RID_REINTCAST:
4157	    postfix_expression
4158	      = build_reinterpret_cast (type, expression);
4159	    break;
4160	  case RID_CONSTCAST:
4161	    postfix_expression
4162	      = build_const_cast (type, expression);
4163	    break;
4164	  default:
4165	    gcc_unreachable ();
4166	  }
4167      }
4168      break;
4169
4170    case RID_TYPEID:
4171      {
4172	tree type;
4173	const char *saved_message;
4174	bool saved_in_type_id_in_expr_p;
4175
4176	/* Consume the `typeid' token.  */
4177	cp_lexer_consume_token (parser->lexer);
4178	/* Look for the `(' token.  */
4179	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4180	/* Types cannot be defined in a `typeid' expression.  */
4181	saved_message = parser->type_definition_forbidden_message;
4182	parser->type_definition_forbidden_message
4183	  = "types may not be defined in a `typeid\' expression";
4184	/* We can't be sure yet whether we're looking at a type-id or an
4185	   expression.  */
4186	cp_parser_parse_tentatively (parser);
4187	/* Try a type-id first.  */
4188	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4189	parser->in_type_id_in_expr_p = true;
4190	type = cp_parser_type_id (parser);
4191	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4192	/* Look for the `)' token.  Otherwise, we can't be sure that
4193	   we're not looking at an expression: consider `typeid (int
4194	   (3))', for example.  */
4195	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4196	/* If all went well, simply lookup the type-id.  */
4197	if (cp_parser_parse_definitely (parser))
4198	  postfix_expression = get_typeid (type);
4199	/* Otherwise, fall back to the expression variant.  */
4200	else
4201	  {
4202	    tree expression;
4203
4204	    /* Look for an expression.  */
4205	    expression = cp_parser_expression (parser, /*cast_p=*/false);
4206	    /* Compute its typeid.  */
4207	    postfix_expression = build_typeid (expression);
4208	    /* Look for the `)' token.  */
4209	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4210	  }
4211	/* Restore the saved message.  */
4212	parser->type_definition_forbidden_message = saved_message;
4213	/* `typeid' may not appear in an integral constant expression.  */
4214	if (cp_parser_non_integral_constant_expression(parser,
4215						       "`typeid' operator"))
4216	  return error_mark_node;
4217      }
4218      break;
4219
4220    case RID_TYPENAME:
4221      {
4222	tree type;
4223	/* The syntax permitted here is the same permitted for an
4224	   elaborated-type-specifier.  */
4225	type = cp_parser_elaborated_type_specifier (parser,
4226						    /*is_friend=*/false,
4227						    /*is_declaration=*/false);
4228	postfix_expression = cp_parser_functional_cast (parser, type);
4229      }
4230      break;
4231
4232    default:
4233      {
4234	tree type;
4235
4236	/* If the next thing is a simple-type-specifier, we may be
4237	   looking at a functional cast.  We could also be looking at
4238	   an id-expression.  So, we try the functional cast, and if
4239	   that doesn't work we fall back to the primary-expression.  */
4240	cp_parser_parse_tentatively (parser);
4241	/* Look for the simple-type-specifier.  */
4242	type = cp_parser_simple_type_specifier (parser,
4243						/*decl_specs=*/NULL,
4244						CP_PARSER_FLAGS_NONE);
4245	/* Parse the cast itself.  */
4246	if (!cp_parser_error_occurred (parser))
4247	  postfix_expression
4248	    = cp_parser_functional_cast (parser, type);
4249	/* If that worked, we're done.  */
4250	if (cp_parser_parse_definitely (parser))
4251	  break;
4252
4253	/* If the functional-cast didn't work out, try a
4254	   compound-literal.  */
4255	if (cp_parser_allow_gnu_extensions_p (parser)
4256	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4257	  {
4258	    VEC(constructor_elt,gc) *initializer_list = NULL;
4259	    bool saved_in_type_id_in_expr_p;
4260
4261	    cp_parser_parse_tentatively (parser);
4262	    /* Consume the `('.  */
4263	    cp_lexer_consume_token (parser->lexer);
4264	    /* Parse the type.  */
4265	    saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4266	    parser->in_type_id_in_expr_p = true;
4267	    type = cp_parser_type_id (parser);
4268	    parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4269	    /* Look for the `)'.  */
4270	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4271	    /* Look for the `{'.  */
4272	    cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4273	    /* If things aren't going well, there's no need to
4274	       keep going.  */
4275	    if (!cp_parser_error_occurred (parser))
4276	      {
4277		bool non_constant_p;
4278		/* Parse the initializer-list.  */
4279		initializer_list
4280		  = cp_parser_initializer_list (parser, &non_constant_p);
4281		/* Allow a trailing `,'.  */
4282		if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4283		  cp_lexer_consume_token (parser->lexer);
4284		/* Look for the final `}'.  */
4285		cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4286	      }
4287	    /* If that worked, we're definitely looking at a
4288	       compound-literal expression.  */
4289	    if (cp_parser_parse_definitely (parser))
4290	      {
4291		/* Warn the user that a compound literal is not
4292		   allowed in standard C++.  */
4293		if (pedantic)
4294		  pedwarn ("ISO C++ forbids compound-literals");
4295		/* For simplicitly, we disallow compound literals in
4296		   constant-expressions for simpliicitly.  We could
4297		   allow compound literals of integer type, whose
4298		   initializer was a constant, in constant
4299		   expressions.  Permitting that usage, as a further
4300		   extension, would not change the meaning of any
4301		   currently accepted programs.  (Of course, as
4302		   compound literals are not part of ISO C++, the
4303		   standard has nothing to say.)  */
4304		if (cp_parser_non_integral_constant_expression
4305		    (parser, "non-constant compound literals"))
4306		  {
4307		    postfix_expression = error_mark_node;
4308		    break;
4309		  }
4310		/* Form the representation of the compound-literal.  */
4311		postfix_expression
4312		  = finish_compound_literal (type, initializer_list);
4313		break;
4314	      }
4315	  }
4316
4317	/* It must be a primary-expression.  */
4318	postfix_expression
4319	  = cp_parser_primary_expression (parser, address_p, cast_p,
4320					  /*template_arg_p=*/false,
4321					  &idk);
4322      }
4323      break;
4324    }
4325
4326  /* Keep looping until the postfix-expression is complete.  */
4327  while (true)
4328    {
4329      if (idk == CP_ID_KIND_UNQUALIFIED
4330	  && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4331	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4332	/* It is not a Koenig lookup function call.  */
4333	postfix_expression
4334	  = unqualified_name_lookup_error (postfix_expression);
4335
4336      /* Peek at the next token.  */
4337      token = cp_lexer_peek_token (parser->lexer);
4338
4339      switch (token->type)
4340	{
4341	case CPP_OPEN_SQUARE:
4342	  postfix_expression
4343	    = cp_parser_postfix_open_square_expression (parser,
4344							postfix_expression,
4345							false);
4346	  idk = CP_ID_KIND_NONE;
4347	  break;
4348
4349	case CPP_OPEN_PAREN:
4350	  /* postfix-expression ( expression-list [opt] ) */
4351	  {
4352	    bool koenig_p;
4353	    bool is_builtin_constant_p;
4354	    bool saved_integral_constant_expression_p = false;
4355	    bool saved_non_integral_constant_expression_p = false;
4356	    tree args;
4357
4358	    is_builtin_constant_p
4359	      = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4360	    if (is_builtin_constant_p)
4361	      {
4362		/* The whole point of __builtin_constant_p is to allow
4363		   non-constant expressions to appear as arguments.  */
4364		saved_integral_constant_expression_p
4365		  = parser->integral_constant_expression_p;
4366		saved_non_integral_constant_expression_p
4367		  = parser->non_integral_constant_expression_p;
4368		parser->integral_constant_expression_p = false;
4369	      }
4370	    args = (cp_parser_parenthesized_expression_list
4371		    (parser, /*is_attribute_list=*/false,
4372		     /*cast_p=*/false,
4373		     /*non_constant_p=*/NULL));
4374	    if (is_builtin_constant_p)
4375	      {
4376		parser->integral_constant_expression_p
4377		  = saved_integral_constant_expression_p;
4378		parser->non_integral_constant_expression_p
4379		  = saved_non_integral_constant_expression_p;
4380	      }
4381
4382	    if (args == error_mark_node)
4383	      {
4384		postfix_expression = error_mark_node;
4385		break;
4386	      }
4387
4388	    /* Function calls are not permitted in
4389	       constant-expressions.  */
4390	    if (! builtin_valid_in_constant_expr_p (postfix_expression)
4391		&& cp_parser_non_integral_constant_expression (parser,
4392							       "a function call"))
4393	      {
4394		postfix_expression = error_mark_node;
4395		break;
4396	      }
4397
4398	    koenig_p = false;
4399	    if (idk == CP_ID_KIND_UNQUALIFIED)
4400	      {
4401		if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4402		  {
4403		    if (args)
4404		      {
4405			koenig_p = true;
4406			postfix_expression
4407			  = perform_koenig_lookup (postfix_expression, args);
4408		      }
4409		    else
4410		      postfix_expression
4411			= unqualified_fn_lookup_error (postfix_expression);
4412		  }
4413		/* We do not perform argument-dependent lookup if
4414		   normal lookup finds a non-function, in accordance
4415		   with the expected resolution of DR 218.  */
4416		else if (args && is_overloaded_fn (postfix_expression))
4417		  {
4418		    tree fn = get_first_fn (postfix_expression);
4419
4420		    if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4421		      fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4422
4423		    /* Only do argument dependent lookup if regular
4424		       lookup does not find a set of member functions.
4425		       [basic.lookup.koenig]/2a  */
4426		    if (!DECL_FUNCTION_MEMBER_P (fn))
4427		      {
4428			koenig_p = true;
4429			postfix_expression
4430			  = perform_koenig_lookup (postfix_expression, args);
4431		      }
4432		  }
4433	      }
4434
4435	    if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4436	      {
4437		tree instance = TREE_OPERAND (postfix_expression, 0);
4438		tree fn = TREE_OPERAND (postfix_expression, 1);
4439
4440		if (processing_template_decl
4441		    && (type_dependent_expression_p (instance)
4442			|| (!BASELINK_P (fn)
4443			    && TREE_CODE (fn) != FIELD_DECL)
4444			|| type_dependent_expression_p (fn)
4445			|| any_type_dependent_arguments_p (args)))
4446		  {
4447		    postfix_expression
4448		      = build_min_nt (CALL_EXPR, postfix_expression,
4449				      args, NULL_TREE);
4450		    break;
4451		  }
4452
4453		if (BASELINK_P (fn))
4454		  postfix_expression
4455		    = (build_new_method_call
4456		       (instance, fn, args, NULL_TREE,
4457			(idk == CP_ID_KIND_QUALIFIED
4458			 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4459			/*fn_p=*/NULL));
4460		else
4461		  postfix_expression
4462		    = finish_call_expr (postfix_expression, args,
4463					/*disallow_virtual=*/false,
4464					/*koenig_p=*/false);
4465	      }
4466	    else if (TREE_CODE (postfix_expression) == OFFSET_REF
4467		     || TREE_CODE (postfix_expression) == MEMBER_REF
4468		     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4469	      postfix_expression = (build_offset_ref_call_from_tree
4470				    (postfix_expression, args));
4471	    else if (idk == CP_ID_KIND_QUALIFIED)
4472	      /* A call to a static class member, or a namespace-scope
4473		 function.  */
4474	      postfix_expression
4475		= finish_call_expr (postfix_expression, args,
4476				    /*disallow_virtual=*/true,
4477				    koenig_p);
4478	    else
4479	      /* All other function calls.  */
4480	      postfix_expression
4481		= finish_call_expr (postfix_expression, args,
4482				    /*disallow_virtual=*/false,
4483				    koenig_p);
4484
4485	    /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4486	    idk = CP_ID_KIND_NONE;
4487	  }
4488	  break;
4489
4490	case CPP_DOT:
4491	case CPP_DEREF:
4492	  /* postfix-expression . template [opt] id-expression
4493	     postfix-expression . pseudo-destructor-name
4494	     postfix-expression -> template [opt] id-expression
4495	     postfix-expression -> pseudo-destructor-name */
4496
4497	  /* Consume the `.' or `->' operator.  */
4498	  cp_lexer_consume_token (parser->lexer);
4499
4500	  postfix_expression
4501	    = cp_parser_postfix_dot_deref_expression (parser, token->type,
4502						      postfix_expression,
4503						      false, &idk);
4504	  break;
4505
4506	case CPP_PLUS_PLUS:
4507	  /* postfix-expression ++  */
4508	  /* Consume the `++' token.  */
4509	  cp_lexer_consume_token (parser->lexer);
4510	  /* Generate a representation for the complete expression.  */
4511	  postfix_expression
4512	    = finish_increment_expr (postfix_expression,
4513				     POSTINCREMENT_EXPR);
4514	  /* Increments may not appear in constant-expressions.  */
4515	  if (cp_parser_non_integral_constant_expression (parser,
4516							  "an increment"))
4517	    postfix_expression = error_mark_node;
4518	  idk = CP_ID_KIND_NONE;
4519	  break;
4520
4521	case CPP_MINUS_MINUS:
4522	  /* postfix-expression -- */
4523	  /* Consume the `--' token.  */
4524	  cp_lexer_consume_token (parser->lexer);
4525	  /* Generate a representation for the complete expression.  */
4526	  postfix_expression
4527	    = finish_increment_expr (postfix_expression,
4528				     POSTDECREMENT_EXPR);
4529	  /* Decrements may not appear in constant-expressions.  */
4530	  if (cp_parser_non_integral_constant_expression (parser,
4531							  "a decrement"))
4532	    postfix_expression = error_mark_node;
4533	  idk = CP_ID_KIND_NONE;
4534	  break;
4535
4536	default:
4537	  return postfix_expression;
4538	}
4539    }
4540
4541  /* We should never get here.  */
4542  gcc_unreachable ();
4543  return error_mark_node;
4544}
4545
4546/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4547   by cp_parser_builtin_offsetof.  We're looking for
4548
4549     postfix-expression [ expression ]
4550
4551   FOR_OFFSETOF is set if we're being called in that context, which
4552   changes how we deal with integer constant expressions.  */
4553
4554static tree
4555cp_parser_postfix_open_square_expression (cp_parser *parser,
4556					  tree postfix_expression,
4557					  bool for_offsetof)
4558{
4559  tree index;
4560
4561  /* Consume the `[' token.  */
4562  cp_lexer_consume_token (parser->lexer);
4563
4564  /* Parse the index expression.  */
4565  /* ??? For offsetof, there is a question of what to allow here.  If
4566     offsetof is not being used in an integral constant expression context,
4567     then we *could* get the right answer by computing the value at runtime.
4568     If we are in an integral constant expression context, then we might
4569     could accept any constant expression; hard to say without analysis.
4570     Rather than open the barn door too wide right away, allow only integer
4571     constant expressions here.  */
4572  if (for_offsetof)
4573    index = cp_parser_constant_expression (parser, false, NULL);
4574  else
4575    index = cp_parser_expression (parser, /*cast_p=*/false);
4576
4577  /* Look for the closing `]'.  */
4578  cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4579
4580  /* Build the ARRAY_REF.  */
4581  postfix_expression = grok_array_decl (postfix_expression, index);
4582
4583  /* When not doing offsetof, array references are not permitted in
4584     constant-expressions.  */
4585  if (!for_offsetof
4586      && (cp_parser_non_integral_constant_expression
4587	  (parser, "an array reference")))
4588    postfix_expression = error_mark_node;
4589
4590  return postfix_expression;
4591}
4592
4593/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4594   by cp_parser_builtin_offsetof.  We're looking for
4595
4596     postfix-expression . template [opt] id-expression
4597     postfix-expression . pseudo-destructor-name
4598     postfix-expression -> template [opt] id-expression
4599     postfix-expression -> pseudo-destructor-name
4600
4601   FOR_OFFSETOF is set if we're being called in that context.  That sorta
4602   limits what of the above we'll actually accept, but nevermind.
4603   TOKEN_TYPE is the "." or "->" token, which will already have been
4604   removed from the stream.  */
4605
4606static tree
4607cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4608					enum cpp_ttype token_type,
4609					tree postfix_expression,
4610					bool for_offsetof, cp_id_kind *idk)
4611{
4612  tree name;
4613  bool dependent_p;
4614  bool pseudo_destructor_p;
4615  tree scope = NULL_TREE;
4616
4617  /* If this is a `->' operator, dereference the pointer.  */
4618  if (token_type == CPP_DEREF)
4619    postfix_expression = build_x_arrow (postfix_expression);
4620  /* Check to see whether or not the expression is type-dependent.  */
4621  dependent_p = type_dependent_expression_p (postfix_expression);
4622  /* The identifier following the `->' or `.' is not qualified.  */
4623  parser->scope = NULL_TREE;
4624  parser->qualifying_scope = NULL_TREE;
4625  parser->object_scope = NULL_TREE;
4626  *idk = CP_ID_KIND_NONE;
4627  /* Enter the scope corresponding to the type of the object
4628     given by the POSTFIX_EXPRESSION.  */
4629  if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4630    {
4631      scope = TREE_TYPE (postfix_expression);
4632      /* According to the standard, no expression should ever have
4633	 reference type.  Unfortunately, we do not currently match
4634	 the standard in this respect in that our internal representation
4635	 of an expression may have reference type even when the standard
4636	 says it does not.  Therefore, we have to manually obtain the
4637	 underlying type here.  */
4638      scope = non_reference (scope);
4639      /* The type of the POSTFIX_EXPRESSION must be complete.  */
4640      if (scope == unknown_type_node)
4641	{
4642	  error ("%qE does not have class type", postfix_expression);
4643	  scope = NULL_TREE;
4644	}
4645      else
4646	scope = complete_type_or_else (scope, NULL_TREE);
4647      /* Let the name lookup machinery know that we are processing a
4648	 class member access expression.  */
4649      parser->context->object_type = scope;
4650      /* If something went wrong, we want to be able to discern that case,
4651	 as opposed to the case where there was no SCOPE due to the type
4652	 of expression being dependent.  */
4653      if (!scope)
4654	scope = error_mark_node;
4655      /* If the SCOPE was erroneous, make the various semantic analysis
4656	 functions exit quickly -- and without issuing additional error
4657	 messages.  */
4658      if (scope == error_mark_node)
4659	postfix_expression = error_mark_node;
4660    }
4661
4662  /* Assume this expression is not a pseudo-destructor access.  */
4663  pseudo_destructor_p = false;
4664
4665  /* If the SCOPE is a scalar type, then, if this is a valid program,
4666     we must be looking at a pseudo-destructor-name.  */
4667  if (scope && SCALAR_TYPE_P (scope))
4668    {
4669      tree s;
4670      tree type;
4671
4672      cp_parser_parse_tentatively (parser);
4673      /* Parse the pseudo-destructor-name.  */
4674      s = NULL_TREE;
4675      cp_parser_pseudo_destructor_name (parser, &s, &type);
4676      if (cp_parser_parse_definitely (parser))
4677	{
4678	  pseudo_destructor_p = true;
4679	  postfix_expression
4680	    = finish_pseudo_destructor_expr (postfix_expression,
4681					     s, TREE_TYPE (type));
4682	}
4683    }
4684
4685  if (!pseudo_destructor_p)
4686    {
4687      /* If the SCOPE is not a scalar type, we are looking at an
4688	 ordinary class member access expression, rather than a
4689	 pseudo-destructor-name.  */
4690      bool template_p;
4691      /* Parse the id-expression.  */
4692      name = (cp_parser_id_expression
4693	      (parser,
4694	       cp_parser_optional_template_keyword (parser),
4695	       /*check_dependency_p=*/true,
4696	       &template_p,
4697	       /*declarator_p=*/false,
4698	       /*optional_p=*/false));
4699      /* In general, build a SCOPE_REF if the member name is qualified.
4700	 However, if the name was not dependent and has already been
4701	 resolved; there is no need to build the SCOPE_REF.  For example;
4702
4703	     struct X { void f(); };
4704	     template <typename T> void f(T* t) { t->X::f(); }
4705
4706	 Even though "t" is dependent, "X::f" is not and has been resolved
4707	 to a BASELINK; there is no need to include scope information.  */
4708
4709      /* But we do need to remember that there was an explicit scope for
4710	 virtual function calls.  */
4711      if (parser->scope)
4712	*idk = CP_ID_KIND_QUALIFIED;
4713
4714      /* If the name is a template-id that names a type, we will get a
4715	 TYPE_DECL here.  That is invalid code.  */
4716      if (TREE_CODE (name) == TYPE_DECL)
4717	{
4718	  error ("invalid use of %qD", name);
4719	  postfix_expression = error_mark_node;
4720	}
4721      else
4722	{
4723	  if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4724	    {
4725	      name = build_qualified_name (/*type=*/NULL_TREE,
4726					   parser->scope,
4727					   name,
4728					   template_p);
4729	      parser->scope = NULL_TREE;
4730	      parser->qualifying_scope = NULL_TREE;
4731	      parser->object_scope = NULL_TREE;
4732	    }
4733	  if (scope && name && BASELINK_P (name))
4734	    adjust_result_of_qualified_name_lookup
4735	      (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4736	  postfix_expression
4737	    = finish_class_member_access_expr (postfix_expression, name,
4738					       template_p);
4739	}
4740    }
4741
4742  /* We no longer need to look up names in the scope of the object on
4743     the left-hand side of the `.' or `->' operator.  */
4744  parser->context->object_type = NULL_TREE;
4745
4746  /* Outside of offsetof, these operators may not appear in
4747     constant-expressions.  */
4748  if (!for_offsetof
4749      && (cp_parser_non_integral_constant_expression
4750	  (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4751    postfix_expression = error_mark_node;
4752
4753  return postfix_expression;
4754}
4755
4756/* Parse a parenthesized expression-list.
4757
4758   expression-list:
4759     assignment-expression
4760     expression-list, assignment-expression
4761
4762   attribute-list:
4763     expression-list
4764     identifier
4765     identifier, expression-list
4766
4767   CAST_P is true if this expression is the target of a cast.
4768
4769   Returns a TREE_LIST.  The TREE_VALUE of each node is a
4770   representation of an assignment-expression.  Note that a TREE_LIST
4771   is returned even if there is only a single expression in the list.
4772   error_mark_node is returned if the ( and or ) are
4773   missing. NULL_TREE is returned on no expressions. The parentheses
4774   are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4775   list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4776   indicates whether or not all of the expressions in the list were
4777   constant.  */
4778
4779static tree
4780cp_parser_parenthesized_expression_list (cp_parser* parser,
4781					 bool is_attribute_list,
4782					 bool cast_p,
4783					 bool *non_constant_p)
4784{
4785  tree expression_list = NULL_TREE;
4786  bool fold_expr_p = is_attribute_list;
4787  tree identifier = NULL_TREE;
4788
4789  /* Assume all the expressions will be constant.  */
4790  if (non_constant_p)
4791    *non_constant_p = false;
4792
4793  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4794    return error_mark_node;
4795
4796  /* Consume expressions until there are no more.  */
4797  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4798    while (true)
4799      {
4800	tree expr;
4801
4802	/* At the beginning of attribute lists, check to see if the
4803	   next token is an identifier.  */
4804	if (is_attribute_list
4805	    && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4806	  {
4807	    cp_token *token;
4808
4809	    /* Consume the identifier.  */
4810	    token = cp_lexer_consume_token (parser->lexer);
4811	    /* Save the identifier.  */
4812	    identifier = token->u.value;
4813	  }
4814	else
4815	  {
4816	    /* Parse the next assignment-expression.  */
4817	    if (non_constant_p)
4818	      {
4819		bool expr_non_constant_p;
4820		expr = (cp_parser_constant_expression
4821			(parser, /*allow_non_constant_p=*/true,
4822			 &expr_non_constant_p));
4823		if (expr_non_constant_p)
4824		  *non_constant_p = true;
4825	      }
4826	    else
4827	      expr = cp_parser_assignment_expression (parser, cast_p);
4828
4829	    if (fold_expr_p)
4830	      expr = fold_non_dependent_expr (expr);
4831
4832	     /* Add it to the list.  We add error_mark_node
4833		expressions to the list, so that we can still tell if
4834		the correct form for a parenthesized expression-list
4835		is found. That gives better errors.  */
4836	    expression_list = tree_cons (NULL_TREE, expr, expression_list);
4837
4838	    if (expr == error_mark_node)
4839	      goto skip_comma;
4840	  }
4841
4842	/* After the first item, attribute lists look the same as
4843	   expression lists.  */
4844	is_attribute_list = false;
4845
4846      get_comma:;
4847	/* If the next token isn't a `,', then we are done.  */
4848	if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4849	  break;
4850
4851	/* Otherwise, consume the `,' and keep going.  */
4852	cp_lexer_consume_token (parser->lexer);
4853      }
4854
4855  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4856    {
4857      int ending;
4858
4859    skip_comma:;
4860      /* We try and resync to an unnested comma, as that will give the
4861	 user better diagnostics.  */
4862      ending = cp_parser_skip_to_closing_parenthesis (parser,
4863						      /*recovering=*/true,
4864						      /*or_comma=*/true,
4865						      /*consume_paren=*/true);
4866      if (ending < 0)
4867	goto get_comma;
4868      if (!ending)
4869	return error_mark_node;
4870    }
4871
4872  /* We built up the list in reverse order so we must reverse it now.  */
4873  expression_list = nreverse (expression_list);
4874  if (identifier)
4875    expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4876
4877  return expression_list;
4878}
4879
4880/* Parse a pseudo-destructor-name.
4881
4882   pseudo-destructor-name:
4883     :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4884     :: [opt] nested-name-specifier template template-id :: ~ type-name
4885     :: [opt] nested-name-specifier [opt] ~ type-name
4886
4887   If either of the first two productions is used, sets *SCOPE to the
4888   TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4889   NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4890   or ERROR_MARK_NODE if the parse fails.  */
4891
4892static void
4893cp_parser_pseudo_destructor_name (cp_parser* parser,
4894				  tree* scope,
4895				  tree* type)
4896{
4897  bool nested_name_specifier_p;
4898
4899  /* Assume that things will not work out.  */
4900  *type = error_mark_node;
4901
4902  /* Look for the optional `::' operator.  */
4903  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4904  /* Look for the optional nested-name-specifier.  */
4905  nested_name_specifier_p
4906    = (cp_parser_nested_name_specifier_opt (parser,
4907					    /*typename_keyword_p=*/false,
4908					    /*check_dependency_p=*/true,
4909					    /*type_p=*/false,
4910					    /*is_declaration=*/true)
4911       != NULL_TREE);
4912  /* Now, if we saw a nested-name-specifier, we might be doing the
4913     second production.  */
4914  if (nested_name_specifier_p
4915      && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4916    {
4917      /* Consume the `template' keyword.  */
4918      cp_lexer_consume_token (parser->lexer);
4919      /* Parse the template-id.  */
4920      cp_parser_template_id (parser,
4921			     /*template_keyword_p=*/true,
4922			     /*check_dependency_p=*/false,
4923			     /*is_declaration=*/true);
4924      /* Look for the `::' token.  */
4925      cp_parser_require (parser, CPP_SCOPE, "`::'");
4926    }
4927  /* If the next token is not a `~', then there might be some
4928     additional qualification.  */
4929  else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4930    {
4931      /* Look for the type-name.  */
4932      *scope = TREE_TYPE (cp_parser_type_name (parser));
4933
4934      if (*scope == error_mark_node)
4935	return;
4936
4937      /* If we don't have ::~, then something has gone wrong.  Since
4938	 the only caller of this function is looking for something
4939	 after `.' or `->' after a scalar type, most likely the
4940	 program is trying to get a member of a non-aggregate
4941	 type.  */
4942      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4943	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4944	{
4945	  cp_parser_error (parser, "request for member of non-aggregate type");
4946	  return;
4947	}
4948
4949      /* Look for the `::' token.  */
4950      cp_parser_require (parser, CPP_SCOPE, "`::'");
4951    }
4952  else
4953    *scope = NULL_TREE;
4954
4955  /* Look for the `~'.  */
4956  cp_parser_require (parser, CPP_COMPL, "`~'");
4957  /* Look for the type-name again.  We are not responsible for
4958     checking that it matches the first type-name.  */
4959  *type = cp_parser_type_name (parser);
4960}
4961
4962/* Parse a unary-expression.
4963
4964   unary-expression:
4965     postfix-expression
4966     ++ cast-expression
4967     -- cast-expression
4968     unary-operator cast-expression
4969     sizeof unary-expression
4970     sizeof ( type-id )
4971     new-expression
4972     delete-expression
4973
4974   GNU Extensions:
4975
4976   unary-expression:
4977     __extension__ cast-expression
4978     __alignof__ unary-expression
4979     __alignof__ ( type-id )
4980     __real__ cast-expression
4981     __imag__ cast-expression
4982     && identifier
4983
4984   ADDRESS_P is true iff the unary-expression is appearing as the
4985   operand of the `&' operator.   CAST_P is true if this expression is
4986   the target of a cast.
4987
4988   Returns a representation of the expression.  */
4989
4990static tree
4991cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4992{
4993  cp_token *token;
4994  enum tree_code unary_operator;
4995
4996  /* Peek at the next token.  */
4997  token = cp_lexer_peek_token (parser->lexer);
4998  /* Some keywords give away the kind of expression.  */
4999  if (token->type == CPP_KEYWORD)
5000    {
5001      enum rid keyword = token->keyword;
5002
5003      switch (keyword)
5004	{
5005	case RID_ALIGNOF:
5006	case RID_SIZEOF:
5007	  {
5008	    tree operand;
5009	    enum tree_code op;
5010
5011	    op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5012	    /* Consume the token.  */
5013	    cp_lexer_consume_token (parser->lexer);
5014	    /* Parse the operand.  */
5015	    operand = cp_parser_sizeof_operand (parser, keyword);
5016
5017	    if (TYPE_P (operand))
5018	      return cxx_sizeof_or_alignof_type (operand, op, true);
5019	    else
5020	      return cxx_sizeof_or_alignof_expr (operand, op);
5021	  }
5022
5023	case RID_NEW:
5024	  return cp_parser_new_expression (parser);
5025
5026	case RID_DELETE:
5027	  return cp_parser_delete_expression (parser);
5028
5029	case RID_EXTENSION:
5030	  {
5031	    /* The saved value of the PEDANTIC flag.  */
5032	    int saved_pedantic;
5033	    tree expr;
5034
5035	    /* Save away the PEDANTIC flag.  */
5036	    cp_parser_extension_opt (parser, &saved_pedantic);
5037	    /* Parse the cast-expression.  */
5038	    expr = cp_parser_simple_cast_expression (parser);
5039	    /* Restore the PEDANTIC flag.  */
5040	    pedantic = saved_pedantic;
5041
5042	    return expr;
5043	  }
5044
5045	case RID_REALPART:
5046	case RID_IMAGPART:
5047	  {
5048	    tree expression;
5049
5050	    /* Consume the `__real__' or `__imag__' token.  */
5051	    cp_lexer_consume_token (parser->lexer);
5052	    /* Parse the cast-expression.  */
5053	    expression = cp_parser_simple_cast_expression (parser);
5054	    /* Create the complete representation.  */
5055	    return build_x_unary_op ((keyword == RID_REALPART
5056				      ? REALPART_EXPR : IMAGPART_EXPR),
5057				     expression);
5058	  }
5059	  break;
5060
5061	default:
5062	  break;
5063	}
5064    }
5065
5066  /* Look for the `:: new' and `:: delete', which also signal the
5067     beginning of a new-expression, or delete-expression,
5068     respectively.  If the next token is `::', then it might be one of
5069     these.  */
5070  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5071    {
5072      enum rid keyword;
5073
5074      /* See if the token after the `::' is one of the keywords in
5075	 which we're interested.  */
5076      keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5077      /* If it's `new', we have a new-expression.  */
5078      if (keyword == RID_NEW)
5079	return cp_parser_new_expression (parser);
5080      /* Similarly, for `delete'.  */
5081      else if (keyword == RID_DELETE)
5082	return cp_parser_delete_expression (parser);
5083    }
5084
5085  /* Look for a unary operator.  */
5086  unary_operator = cp_parser_unary_operator (token);
5087  /* The `++' and `--' operators can be handled similarly, even though
5088     they are not technically unary-operators in the grammar.  */
5089  if (unary_operator == ERROR_MARK)
5090    {
5091      if (token->type == CPP_PLUS_PLUS)
5092	unary_operator = PREINCREMENT_EXPR;
5093      else if (token->type == CPP_MINUS_MINUS)
5094	unary_operator = PREDECREMENT_EXPR;
5095      /* Handle the GNU address-of-label extension.  */
5096      else if (cp_parser_allow_gnu_extensions_p (parser)
5097	       && token->type == CPP_AND_AND)
5098	{
5099	  tree identifier;
5100
5101	  /* Consume the '&&' token.  */
5102	  cp_lexer_consume_token (parser->lexer);
5103	  /* Look for the identifier.  */
5104	  identifier = cp_parser_identifier (parser);
5105	  /* Create an expression representing the address.  */
5106	  return finish_label_address_expr (identifier);
5107	}
5108    }
5109  if (unary_operator != ERROR_MARK)
5110    {
5111      tree cast_expression;
5112      tree expression = error_mark_node;
5113      const char *non_constant_p = NULL;
5114
5115      /* Consume the operator token.  */
5116      token = cp_lexer_consume_token (parser->lexer);
5117      /* Parse the cast-expression.  */
5118      cast_expression
5119	= cp_parser_cast_expression (parser,
5120				     unary_operator == ADDR_EXPR,
5121				     /*cast_p=*/false);
5122      /* Now, build an appropriate representation.  */
5123      switch (unary_operator)
5124	{
5125	case INDIRECT_REF:
5126	  non_constant_p = "`*'";
5127	  expression = build_x_indirect_ref (cast_expression, "unary *");
5128	  break;
5129
5130	case ADDR_EXPR:
5131	  non_constant_p = "`&'";
5132	  /* Fall through.  */
5133	case BIT_NOT_EXPR:
5134	  expression = build_x_unary_op (unary_operator, cast_expression);
5135	  break;
5136
5137	case PREINCREMENT_EXPR:
5138	case PREDECREMENT_EXPR:
5139	  non_constant_p = (unary_operator == PREINCREMENT_EXPR
5140			    ? "`++'" : "`--'");
5141	  /* Fall through.  */
5142	case UNARY_PLUS_EXPR:
5143	case NEGATE_EXPR:
5144	case TRUTH_NOT_EXPR:
5145	  expression = finish_unary_op_expr (unary_operator, cast_expression);
5146	  break;
5147
5148	default:
5149	  gcc_unreachable ();
5150	}
5151
5152      if (non_constant_p
5153	  && cp_parser_non_integral_constant_expression (parser,
5154							 non_constant_p))
5155	expression = error_mark_node;
5156
5157      return expression;
5158    }
5159
5160  return cp_parser_postfix_expression (parser, address_p, cast_p);
5161}
5162
5163/* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5164   unary-operator, the corresponding tree code is returned.  */
5165
5166static enum tree_code
5167cp_parser_unary_operator (cp_token* token)
5168{
5169  switch (token->type)
5170    {
5171    case CPP_MULT:
5172      return INDIRECT_REF;
5173
5174    case CPP_AND:
5175      return ADDR_EXPR;
5176
5177    case CPP_PLUS:
5178      return UNARY_PLUS_EXPR;
5179
5180    case CPP_MINUS:
5181      return NEGATE_EXPR;
5182
5183    case CPP_NOT:
5184      return TRUTH_NOT_EXPR;
5185
5186    case CPP_COMPL:
5187      return BIT_NOT_EXPR;
5188
5189    default:
5190      return ERROR_MARK;
5191    }
5192}
5193
5194/* Parse a new-expression.
5195
5196   new-expression:
5197     :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5198     :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5199
5200   Returns a representation of the expression.  */
5201
5202static tree
5203cp_parser_new_expression (cp_parser* parser)
5204{
5205  bool global_scope_p;
5206  tree placement;
5207  tree type;
5208  tree initializer;
5209  tree nelts;
5210
5211  /* Look for the optional `::' operator.  */
5212  global_scope_p
5213    = (cp_parser_global_scope_opt (parser,
5214				   /*current_scope_valid_p=*/false)
5215       != NULL_TREE);
5216  /* Look for the `new' operator.  */
5217  cp_parser_require_keyword (parser, RID_NEW, "`new'");
5218  /* There's no easy way to tell a new-placement from the
5219     `( type-id )' construct.  */
5220  cp_parser_parse_tentatively (parser);
5221  /* Look for a new-placement.  */
5222  placement = cp_parser_new_placement (parser);
5223  /* If that didn't work out, there's no new-placement.  */
5224  if (!cp_parser_parse_definitely (parser))
5225    placement = NULL_TREE;
5226
5227  /* If the next token is a `(', then we have a parenthesized
5228     type-id.  */
5229  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5230    {
5231      /* Consume the `('.  */
5232      cp_lexer_consume_token (parser->lexer);
5233      /* Parse the type-id.  */
5234      type = cp_parser_type_id (parser);
5235      /* Look for the closing `)'.  */
5236      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5237      /* There should not be a direct-new-declarator in this production,
5238	 but GCC used to allowed this, so we check and emit a sensible error
5239	 message for this case.  */
5240      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5241	{
5242	  error ("array bound forbidden after parenthesized type-id");
5243	  inform ("try removing the parentheses around the type-id");
5244	  cp_parser_direct_new_declarator (parser);
5245	}
5246      nelts = NULL_TREE;
5247    }
5248  /* Otherwise, there must be a new-type-id.  */
5249  else
5250    type = cp_parser_new_type_id (parser, &nelts);
5251
5252  /* If the next token is a `(', then we have a new-initializer.  */
5253  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5254    initializer = cp_parser_new_initializer (parser);
5255  else
5256    initializer = NULL_TREE;
5257
5258  /* A new-expression may not appear in an integral constant
5259     expression.  */
5260  if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5261    return error_mark_node;
5262
5263  /* Create a representation of the new-expression.  */
5264  return build_new (placement, type, nelts, initializer, global_scope_p);
5265}
5266
5267/* Parse a new-placement.
5268
5269   new-placement:
5270     ( expression-list )
5271
5272   Returns the same representation as for an expression-list.  */
5273
5274static tree
5275cp_parser_new_placement (cp_parser* parser)
5276{
5277  tree expression_list;
5278
5279  /* Parse the expression-list.  */
5280  expression_list = (cp_parser_parenthesized_expression_list
5281		     (parser, false, /*cast_p=*/false,
5282		      /*non_constant_p=*/NULL));
5283
5284  return expression_list;
5285}
5286
5287/* Parse a new-type-id.
5288
5289   new-type-id:
5290     type-specifier-seq new-declarator [opt]
5291
5292   Returns the TYPE allocated.  If the new-type-id indicates an array
5293   type, *NELTS is set to the number of elements in the last array
5294   bound; the TYPE will not include the last array bound.  */
5295
5296static tree
5297cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5298{
5299  cp_decl_specifier_seq type_specifier_seq;
5300  cp_declarator *new_declarator;
5301  cp_declarator *declarator;
5302  cp_declarator *outer_declarator;
5303  const char *saved_message;
5304  tree type;
5305
5306  /* The type-specifier sequence must not contain type definitions.
5307     (It cannot contain declarations of new types either, but if they
5308     are not definitions we will catch that because they are not
5309     complete.)  */
5310  saved_message = parser->type_definition_forbidden_message;
5311  parser->type_definition_forbidden_message
5312    = "types may not be defined in a new-type-id";
5313  /* Parse the type-specifier-seq.  */
5314  cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5315				&type_specifier_seq);
5316  /* Restore the old message.  */
5317  parser->type_definition_forbidden_message = saved_message;
5318  /* Parse the new-declarator.  */
5319  new_declarator = cp_parser_new_declarator_opt (parser);
5320
5321  /* Determine the number of elements in the last array dimension, if
5322     any.  */
5323  *nelts = NULL_TREE;
5324  /* Skip down to the last array dimension.  */
5325  declarator = new_declarator;
5326  outer_declarator = NULL;
5327  while (declarator && (declarator->kind == cdk_pointer
5328			|| declarator->kind == cdk_ptrmem))
5329    {
5330      outer_declarator = declarator;
5331      declarator = declarator->declarator;
5332    }
5333  while (declarator
5334	 && declarator->kind == cdk_array
5335	 && declarator->declarator
5336	 && declarator->declarator->kind == cdk_array)
5337    {
5338      outer_declarator = declarator;
5339      declarator = declarator->declarator;
5340    }
5341
5342  if (declarator && declarator->kind == cdk_array)
5343    {
5344      *nelts = declarator->u.array.bounds;
5345      if (*nelts == error_mark_node)
5346	*nelts = integer_one_node;
5347
5348      if (outer_declarator)
5349	outer_declarator->declarator = declarator->declarator;
5350      else
5351	new_declarator = NULL;
5352    }
5353
5354  type = groktypename (&type_specifier_seq, new_declarator);
5355  if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5356    {
5357      *nelts = array_type_nelts_top (type);
5358      type = TREE_TYPE (type);
5359    }
5360  return type;
5361}
5362
5363/* Parse an (optional) new-declarator.
5364
5365   new-declarator:
5366     ptr-operator new-declarator [opt]
5367     direct-new-declarator
5368
5369   Returns the declarator.  */
5370
5371static cp_declarator *
5372cp_parser_new_declarator_opt (cp_parser* parser)
5373{
5374  enum tree_code code;
5375  tree type;
5376  cp_cv_quals cv_quals;
5377
5378  /* We don't know if there's a ptr-operator next, or not.  */
5379  cp_parser_parse_tentatively (parser);
5380  /* Look for a ptr-operator.  */
5381  code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5382  /* If that worked, look for more new-declarators.  */
5383  if (cp_parser_parse_definitely (parser))
5384    {
5385      cp_declarator *declarator;
5386
5387      /* Parse another optional declarator.  */
5388      declarator = cp_parser_new_declarator_opt (parser);
5389
5390      /* Create the representation of the declarator.  */
5391      if (type)
5392	declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5393      else if (code == INDIRECT_REF)
5394	declarator = make_pointer_declarator (cv_quals, declarator);
5395      else
5396	declarator = make_reference_declarator (cv_quals, declarator);
5397
5398      return declarator;
5399    }
5400
5401  /* If the next token is a `[', there is a direct-new-declarator.  */
5402  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5403    return cp_parser_direct_new_declarator (parser);
5404
5405  return NULL;
5406}
5407
5408/* Parse a direct-new-declarator.
5409
5410   direct-new-declarator:
5411     [ expression ]
5412     direct-new-declarator [constant-expression]
5413
5414   */
5415
5416static cp_declarator *
5417cp_parser_direct_new_declarator (cp_parser* parser)
5418{
5419  cp_declarator *declarator = NULL;
5420
5421  while (true)
5422    {
5423      tree expression;
5424
5425      /* Look for the opening `['.  */
5426      cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5427      /* The first expression is not required to be constant.  */
5428      if (!declarator)
5429	{
5430	  expression = cp_parser_expression (parser, /*cast_p=*/false);
5431	  /* The standard requires that the expression have integral
5432	     type.  DR 74 adds enumeration types.  We believe that the
5433	     real intent is that these expressions be handled like the
5434	     expression in a `switch' condition, which also allows
5435	     classes with a single conversion to integral or
5436	     enumeration type.  */
5437	  if (!processing_template_decl)
5438	    {
5439	      expression
5440		= build_expr_type_conversion (WANT_INT | WANT_ENUM,
5441					      expression,
5442					      /*complain=*/true);
5443	      if (!expression)
5444		{
5445		  error ("expression in new-declarator must have integral "
5446			 "or enumeration type");
5447		  expression = error_mark_node;
5448		}
5449	    }
5450	}
5451      /* But all the other expressions must be.  */
5452      else
5453	expression
5454	  = cp_parser_constant_expression (parser,
5455					   /*allow_non_constant=*/false,
5456					   NULL);
5457      /* Look for the closing `]'.  */
5458      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5459
5460      /* Add this bound to the declarator.  */
5461      declarator = make_array_declarator (declarator, expression);
5462
5463      /* If the next token is not a `[', then there are no more
5464	 bounds.  */
5465      if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5466	break;
5467    }
5468
5469  return declarator;
5470}
5471
5472/* Parse a new-initializer.
5473
5474   new-initializer:
5475     ( expression-list [opt] )
5476
5477   Returns a representation of the expression-list.  If there is no
5478   expression-list, VOID_ZERO_NODE is returned.  */
5479
5480static tree
5481cp_parser_new_initializer (cp_parser* parser)
5482{
5483  tree expression_list;
5484
5485  expression_list = (cp_parser_parenthesized_expression_list
5486		     (parser, false, /*cast_p=*/false,
5487		      /*non_constant_p=*/NULL));
5488  if (!expression_list)
5489    expression_list = void_zero_node;
5490
5491  return expression_list;
5492}
5493
5494/* Parse a delete-expression.
5495
5496   delete-expression:
5497     :: [opt] delete cast-expression
5498     :: [opt] delete [ ] cast-expression
5499
5500   Returns a representation of the expression.  */
5501
5502static tree
5503cp_parser_delete_expression (cp_parser* parser)
5504{
5505  bool global_scope_p;
5506  bool array_p;
5507  tree expression;
5508
5509  /* Look for the optional `::' operator.  */
5510  global_scope_p
5511    = (cp_parser_global_scope_opt (parser,
5512				   /*current_scope_valid_p=*/false)
5513       != NULL_TREE);
5514  /* Look for the `delete' keyword.  */
5515  cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5516  /* See if the array syntax is in use.  */
5517  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5518    {
5519      /* Consume the `[' token.  */
5520      cp_lexer_consume_token (parser->lexer);
5521      /* Look for the `]' token.  */
5522      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5523      /* Remember that this is the `[]' construct.  */
5524      array_p = true;
5525    }
5526  else
5527    array_p = false;
5528
5529  /* Parse the cast-expression.  */
5530  expression = cp_parser_simple_cast_expression (parser);
5531
5532  /* A delete-expression may not appear in an integral constant
5533     expression.  */
5534  if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5535    return error_mark_node;
5536
5537  return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5538}
5539
5540/* Parse a cast-expression.
5541
5542   cast-expression:
5543     unary-expression
5544     ( type-id ) cast-expression
5545
5546   ADDRESS_P is true iff the unary-expression is appearing as the
5547   operand of the `&' operator.   CAST_P is true if this expression is
5548   the target of a cast.
5549
5550   Returns a representation of the expression.  */
5551
5552static tree
5553cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5554{
5555  /* If it's a `(', then we might be looking at a cast.  */
5556  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5557    {
5558      tree type = NULL_TREE;
5559      tree expr = NULL_TREE;
5560      bool compound_literal_p;
5561      const char *saved_message;
5562
5563      /* There's no way to know yet whether or not this is a cast.
5564	 For example, `(int (3))' is a unary-expression, while `(int)
5565	 3' is a cast.  So, we resort to parsing tentatively.  */
5566      cp_parser_parse_tentatively (parser);
5567      /* Types may not be defined in a cast.  */
5568      saved_message = parser->type_definition_forbidden_message;
5569      parser->type_definition_forbidden_message
5570	= "types may not be defined in casts";
5571      /* Consume the `('.  */
5572      cp_lexer_consume_token (parser->lexer);
5573      /* A very tricky bit is that `(struct S) { 3 }' is a
5574	 compound-literal (which we permit in C++ as an extension).
5575	 But, that construct is not a cast-expression -- it is a
5576	 postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5577	 is legal; if the compound-literal were a cast-expression,
5578	 you'd need an extra set of parentheses.)  But, if we parse
5579	 the type-id, and it happens to be a class-specifier, then we
5580	 will commit to the parse at that point, because we cannot
5581	 undo the action that is done when creating a new class.  So,
5582	 then we cannot back up and do a postfix-expression.
5583
5584	 Therefore, we scan ahead to the closing `)', and check to see
5585	 if the token after the `)' is a `{'.  If so, we are not
5586	 looking at a cast-expression.
5587
5588	 Save tokens so that we can put them back.  */
5589      cp_lexer_save_tokens (parser->lexer);
5590      /* Skip tokens until the next token is a closing parenthesis.
5591	 If we find the closing `)', and the next token is a `{', then
5592	 we are looking at a compound-literal.  */
5593      compound_literal_p
5594	= (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5595						  /*consume_paren=*/true)
5596	   && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5597      /* Roll back the tokens we skipped.  */
5598      cp_lexer_rollback_tokens (parser->lexer);
5599      /* If we were looking at a compound-literal, simulate an error
5600	 so that the call to cp_parser_parse_definitely below will
5601	 fail.  */
5602      if (compound_literal_p)
5603	cp_parser_simulate_error (parser);
5604      else
5605	{
5606	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5607	  parser->in_type_id_in_expr_p = true;
5608	  /* Look for the type-id.  */
5609	  type = cp_parser_type_id (parser);
5610	  /* Look for the closing `)'.  */
5611	  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5612	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5613	}
5614
5615      /* Restore the saved message.  */
5616      parser->type_definition_forbidden_message = saved_message;
5617
5618      /* If ok so far, parse the dependent expression. We cannot be
5619	 sure it is a cast. Consider `(T ())'.  It is a parenthesized
5620	 ctor of T, but looks like a cast to function returning T
5621	 without a dependent expression.  */
5622      if (!cp_parser_error_occurred (parser))
5623	expr = cp_parser_cast_expression (parser,
5624					  /*address_p=*/false,
5625					  /*cast_p=*/true);
5626
5627      if (cp_parser_parse_definitely (parser))
5628	{
5629	  /* Warn about old-style casts, if so requested.  */
5630	  if (warn_old_style_cast
5631	      && !in_system_header
5632	      && !VOID_TYPE_P (type)
5633	      && current_lang_name != lang_name_c)
5634	    warning (OPT_Wold_style_cast, "use of old-style cast");
5635
5636	  /* Only type conversions to integral or enumeration types
5637	     can be used in constant-expressions.  */
5638	  if (!cast_valid_in_integral_constant_expression_p (type)
5639	      && (cp_parser_non_integral_constant_expression
5640		  (parser,
5641		   "a cast to a type other than an integral or "
5642		   "enumeration type")))
5643	    return error_mark_node;
5644
5645	  /* Perform the cast.  */
5646	  expr = build_c_cast (type, expr);
5647	  return expr;
5648	}
5649    }
5650
5651  /* If we get here, then it's not a cast, so it must be a
5652     unary-expression.  */
5653  return cp_parser_unary_expression (parser, address_p, cast_p);
5654}
5655
5656/* Parse a binary expression of the general form:
5657
5658   pm-expression:
5659     cast-expression
5660     pm-expression .* cast-expression
5661     pm-expression ->* cast-expression
5662
5663   multiplicative-expression:
5664     pm-expression
5665     multiplicative-expression * pm-expression
5666     multiplicative-expression / pm-expression
5667     multiplicative-expression % pm-expression
5668
5669   additive-expression:
5670     multiplicative-expression
5671     additive-expression + multiplicative-expression
5672     additive-expression - multiplicative-expression
5673
5674   shift-expression:
5675     additive-expression
5676     shift-expression << additive-expression
5677     shift-expression >> additive-expression
5678
5679   relational-expression:
5680     shift-expression
5681     relational-expression < shift-expression
5682     relational-expression > shift-expression
5683     relational-expression <= shift-expression
5684     relational-expression >= shift-expression
5685
5686  GNU Extension:
5687
5688   relational-expression:
5689     relational-expression <? shift-expression
5690     relational-expression >? shift-expression
5691
5692   equality-expression:
5693     relational-expression
5694     equality-expression == relational-expression
5695     equality-expression != relational-expression
5696
5697   and-expression:
5698     equality-expression
5699     and-expression & equality-expression
5700
5701   exclusive-or-expression:
5702     and-expression
5703     exclusive-or-expression ^ and-expression
5704
5705   inclusive-or-expression:
5706     exclusive-or-expression
5707     inclusive-or-expression | exclusive-or-expression
5708
5709   logical-and-expression:
5710     inclusive-or-expression
5711     logical-and-expression && inclusive-or-expression
5712
5713   logical-or-expression:
5714     logical-and-expression
5715     logical-or-expression || logical-and-expression
5716
5717   All these are implemented with a single function like:
5718
5719   binary-expression:
5720     simple-cast-expression
5721     binary-expression <token> binary-expression
5722
5723   CAST_P is true if this expression is the target of a cast.
5724
5725   The binops_by_token map is used to get the tree codes for each <token> type.
5726   binary-expressions are associated according to a precedence table.  */
5727
5728#define TOKEN_PRECEDENCE(token) \
5729  ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5730   ? PREC_NOT_OPERATOR \
5731   : binops_by_token[token->type].prec)
5732
5733static tree
5734cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5735{
5736  cp_parser_expression_stack stack;
5737  cp_parser_expression_stack_entry *sp = &stack[0];
5738  tree lhs, rhs;
5739  cp_token *token;
5740  enum tree_code tree_type, lhs_type, rhs_type;
5741  enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5742  bool overloaded_p;
5743
5744  /* Parse the first expression.  */
5745  lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5746  lhs_type = ERROR_MARK;
5747
5748  for (;;)
5749    {
5750      /* Get an operator token.  */
5751      token = cp_lexer_peek_token (parser->lexer);
5752
5753      new_prec = TOKEN_PRECEDENCE (token);
5754
5755      /* Popping an entry off the stack means we completed a subexpression:
5756	 - either we found a token which is not an operator (`>' where it is not
5757	   an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5758	   will happen repeatedly;
5759	 - or, we found an operator which has lower priority.  This is the case
5760	   where the recursive descent *ascends*, as in `3 * 4 + 5' after
5761	   parsing `3 * 4'.  */
5762      if (new_prec <= prec)
5763	{
5764	  if (sp == stack)
5765	    break;
5766	  else
5767	    goto pop;
5768	}
5769
5770     get_rhs:
5771      tree_type = binops_by_token[token->type].tree_type;
5772
5773      /* We used the operator token.  */
5774      cp_lexer_consume_token (parser->lexer);
5775
5776      /* Extract another operand.  It may be the RHS of this expression
5777	 or the LHS of a new, higher priority expression.  */
5778      rhs = cp_parser_simple_cast_expression (parser);
5779      rhs_type = ERROR_MARK;
5780
5781      /* Get another operator token.  Look up its precedence to avoid
5782	 building a useless (immediately popped) stack entry for common
5783	 cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5784      token = cp_lexer_peek_token (parser->lexer);
5785      lookahead_prec = TOKEN_PRECEDENCE (token);
5786      if (lookahead_prec > new_prec)
5787	{
5788	  /* ... and prepare to parse the RHS of the new, higher priority
5789	     expression.  Since precedence levels on the stack are
5790	     monotonically increasing, we do not have to care about
5791	     stack overflows.  */
5792	  sp->prec = prec;
5793	  sp->tree_type = tree_type;
5794	  sp->lhs = lhs;
5795	  sp->lhs_type = lhs_type;
5796	  sp++;
5797	  lhs = rhs;
5798	  lhs_type = rhs_type;
5799	  prec = new_prec;
5800	  new_prec = lookahead_prec;
5801	  goto get_rhs;
5802
5803	 pop:
5804	  /* If the stack is not empty, we have parsed into LHS the right side
5805	     (`4' in the example above) of an expression we had suspended.
5806	     We can use the information on the stack to recover the LHS (`3')
5807	     from the stack together with the tree code (`MULT_EXPR'), and
5808	     the precedence of the higher level subexpression
5809	     (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5810	     which will be used to actually build the additive expression.  */
5811	  --sp;
5812	  prec = sp->prec;
5813	  tree_type = sp->tree_type;
5814	  rhs = lhs;
5815	  rhs_type = lhs_type;
5816	  lhs = sp->lhs;
5817	  lhs_type = sp->lhs_type;
5818	}
5819
5820      overloaded_p = false;
5821      lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5822			       &overloaded_p);
5823      lhs_type = tree_type;
5824
5825      /* If the binary operator required the use of an overloaded operator,
5826	 then this expression cannot be an integral constant-expression.
5827	 An overloaded operator can be used even if both operands are
5828	 otherwise permissible in an integral constant-expression if at
5829	 least one of the operands is of enumeration type.  */
5830
5831      if (overloaded_p
5832	  && (cp_parser_non_integral_constant_expression
5833	      (parser, "calls to overloaded operators")))
5834	return error_mark_node;
5835    }
5836
5837  return lhs;
5838}
5839
5840
5841/* Parse the `? expression : assignment-expression' part of a
5842   conditional-expression.  The LOGICAL_OR_EXPR is the
5843   logical-or-expression that started the conditional-expression.
5844   Returns a representation of the entire conditional-expression.
5845
5846   This routine is used by cp_parser_assignment_expression.
5847
5848     ? expression : assignment-expression
5849
5850   GNU Extensions:
5851
5852     ? : assignment-expression */
5853
5854static tree
5855cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5856{
5857  tree expr;
5858  tree assignment_expr;
5859
5860  /* Consume the `?' token.  */
5861  cp_lexer_consume_token (parser->lexer);
5862  if (cp_parser_allow_gnu_extensions_p (parser)
5863      && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5864    /* Implicit true clause.  */
5865    expr = NULL_TREE;
5866  else
5867    /* Parse the expression.  */
5868    expr = cp_parser_expression (parser, /*cast_p=*/false);
5869
5870  /* The next token should be a `:'.  */
5871  cp_parser_require (parser, CPP_COLON, "`:'");
5872  /* Parse the assignment-expression.  */
5873  assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5874
5875  /* Build the conditional-expression.  */
5876  return build_x_conditional_expr (logical_or_expr,
5877				   expr,
5878				   assignment_expr);
5879}
5880
5881/* Parse an assignment-expression.
5882
5883   assignment-expression:
5884     conditional-expression
5885     logical-or-expression assignment-operator assignment_expression
5886     throw-expression
5887
5888   CAST_P is true if this expression is the target of a cast.
5889
5890   Returns a representation for the expression.  */
5891
5892static tree
5893cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5894{
5895  tree expr;
5896
5897  /* If the next token is the `throw' keyword, then we're looking at
5898     a throw-expression.  */
5899  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5900    expr = cp_parser_throw_expression (parser);
5901  /* Otherwise, it must be that we are looking at a
5902     logical-or-expression.  */
5903  else
5904    {
5905      /* Parse the binary expressions (logical-or-expression).  */
5906      expr = cp_parser_binary_expression (parser, cast_p);
5907      /* If the next token is a `?' then we're actually looking at a
5908	 conditional-expression.  */
5909      if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5910	return cp_parser_question_colon_clause (parser, expr);
5911      else
5912	{
5913	  enum tree_code assignment_operator;
5914
5915	  /* If it's an assignment-operator, we're using the second
5916	     production.  */
5917	  assignment_operator
5918	    = cp_parser_assignment_operator_opt (parser);
5919	  if (assignment_operator != ERROR_MARK)
5920	    {
5921	      tree rhs;
5922
5923	      /* Parse the right-hand side of the assignment.  */
5924	      rhs = cp_parser_assignment_expression (parser, cast_p);
5925	      /* An assignment may not appear in a
5926		 constant-expression.  */
5927	      if (cp_parser_non_integral_constant_expression (parser,
5928							      "an assignment"))
5929		return error_mark_node;
5930	      /* Build the assignment expression.  */
5931	      expr = build_x_modify_expr (expr,
5932					  assignment_operator,
5933					  rhs);
5934	    }
5935	}
5936    }
5937
5938  return expr;
5939}
5940
5941/* Parse an (optional) assignment-operator.
5942
5943   assignment-operator: one of
5944     = *= /= %= += -= >>= <<= &= ^= |=
5945
5946   GNU Extension:
5947
5948   assignment-operator: one of
5949     <?= >?=
5950
5951   If the next token is an assignment operator, the corresponding tree
5952   code is returned, and the token is consumed.  For example, for
5953   `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5954   NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5955   TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5956   operator, ERROR_MARK is returned.  */
5957
5958static enum tree_code
5959cp_parser_assignment_operator_opt (cp_parser* parser)
5960{
5961  enum tree_code op;
5962  cp_token *token;
5963
5964  /* Peek at the next toen.  */
5965  token = cp_lexer_peek_token (parser->lexer);
5966
5967  switch (token->type)
5968    {
5969    case CPP_EQ:
5970      op = NOP_EXPR;
5971      break;
5972
5973    case CPP_MULT_EQ:
5974      op = MULT_EXPR;
5975      break;
5976
5977    case CPP_DIV_EQ:
5978      op = TRUNC_DIV_EXPR;
5979      break;
5980
5981    case CPP_MOD_EQ:
5982      op = TRUNC_MOD_EXPR;
5983      break;
5984
5985    case CPP_PLUS_EQ:
5986      op = PLUS_EXPR;
5987      break;
5988
5989    case CPP_MINUS_EQ:
5990      op = MINUS_EXPR;
5991      break;
5992
5993    case CPP_RSHIFT_EQ:
5994      op = RSHIFT_EXPR;
5995      break;
5996
5997    case CPP_LSHIFT_EQ:
5998      op = LSHIFT_EXPR;
5999      break;
6000
6001    case CPP_AND_EQ:
6002      op = BIT_AND_EXPR;
6003      break;
6004
6005    case CPP_XOR_EQ:
6006      op = BIT_XOR_EXPR;
6007      break;
6008
6009    case CPP_OR_EQ:
6010      op = BIT_IOR_EXPR;
6011      break;
6012
6013    default:
6014      /* Nothing else is an assignment operator.  */
6015      op = ERROR_MARK;
6016    }
6017
6018  /* If it was an assignment operator, consume it.  */
6019  if (op != ERROR_MARK)
6020    cp_lexer_consume_token (parser->lexer);
6021
6022  return op;
6023}
6024
6025/* Parse an expression.
6026
6027   expression:
6028     assignment-expression
6029     expression , assignment-expression
6030
6031   CAST_P is true if this expression is the target of a cast.
6032
6033   Returns a representation of the expression.  */
6034
6035static tree
6036cp_parser_expression (cp_parser* parser, bool cast_p)
6037{
6038  tree expression = NULL_TREE;
6039
6040  while (true)
6041    {
6042      tree assignment_expression;
6043
6044      /* Parse the next assignment-expression.  */
6045      assignment_expression
6046	= cp_parser_assignment_expression (parser, cast_p);
6047      /* If this is the first assignment-expression, we can just
6048	 save it away.  */
6049      if (!expression)
6050	expression = assignment_expression;
6051      else
6052	expression = build_x_compound_expr (expression,
6053					    assignment_expression);
6054      /* If the next token is not a comma, then we are done with the
6055	 expression.  */
6056      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6057	break;
6058      /* Consume the `,'.  */
6059      cp_lexer_consume_token (parser->lexer);
6060      /* A comma operator cannot appear in a constant-expression.  */
6061      if (cp_parser_non_integral_constant_expression (parser,
6062						      "a comma operator"))
6063	expression = error_mark_node;
6064    }
6065
6066  return expression;
6067}
6068
6069/* Parse a constant-expression.
6070
6071   constant-expression:
6072     conditional-expression
6073
6074  If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6075  accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6076  constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6077  is false, NON_CONSTANT_P should be NULL.  */
6078
6079static tree
6080cp_parser_constant_expression (cp_parser* parser,
6081			       bool allow_non_constant_p,
6082			       bool *non_constant_p)
6083{
6084  bool saved_integral_constant_expression_p;
6085  bool saved_allow_non_integral_constant_expression_p;
6086  bool saved_non_integral_constant_expression_p;
6087  tree expression;
6088
6089  /* It might seem that we could simply parse the
6090     conditional-expression, and then check to see if it were
6091     TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6092     one that the compiler can figure out is constant, possibly after
6093     doing some simplifications or optimizations.  The standard has a
6094     precise definition of constant-expression, and we must honor
6095     that, even though it is somewhat more restrictive.
6096
6097     For example:
6098
6099       int i[(2, 3)];
6100
6101     is not a legal declaration, because `(2, 3)' is not a
6102     constant-expression.  The `,' operator is forbidden in a
6103     constant-expression.  However, GCC's constant-folding machinery
6104     will fold this operation to an INTEGER_CST for `3'.  */
6105
6106  /* Save the old settings.  */
6107  saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6108  saved_allow_non_integral_constant_expression_p
6109    = parser->allow_non_integral_constant_expression_p;
6110  saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6111  /* We are now parsing a constant-expression.  */
6112  parser->integral_constant_expression_p = true;
6113  parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6114  parser->non_integral_constant_expression_p = false;
6115  /* Although the grammar says "conditional-expression", we parse an
6116     "assignment-expression", which also permits "throw-expression"
6117     and the use of assignment operators.  In the case that
6118     ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6119     otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6120     actually essential that we look for an assignment-expression.
6121     For example, cp_parser_initializer_clauses uses this function to
6122     determine whether a particular assignment-expression is in fact
6123     constant.  */
6124  expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6125  /* Restore the old settings.  */
6126  parser->integral_constant_expression_p
6127    = saved_integral_constant_expression_p;
6128  parser->allow_non_integral_constant_expression_p
6129    = saved_allow_non_integral_constant_expression_p;
6130  if (allow_non_constant_p)
6131    *non_constant_p = parser->non_integral_constant_expression_p;
6132  else if (parser->non_integral_constant_expression_p)
6133    expression = error_mark_node;
6134  parser->non_integral_constant_expression_p
6135    = saved_non_integral_constant_expression_p;
6136
6137  return expression;
6138}
6139
6140/* Parse __builtin_offsetof.
6141
6142   offsetof-expression:
6143     "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6144
6145   offsetof-member-designator:
6146     id-expression
6147     | offsetof-member-designator "." id-expression
6148     | offsetof-member-designator "[" expression "]"  */
6149
6150static tree
6151cp_parser_builtin_offsetof (cp_parser *parser)
6152{
6153  int save_ice_p, save_non_ice_p;
6154  tree type, expr;
6155  cp_id_kind dummy;
6156
6157  /* We're about to accept non-integral-constant things, but will
6158     definitely yield an integral constant expression.  Save and
6159     restore these values around our local parsing.  */
6160  save_ice_p = parser->integral_constant_expression_p;
6161  save_non_ice_p = parser->non_integral_constant_expression_p;
6162
6163  /* Consume the "__builtin_offsetof" token.  */
6164  cp_lexer_consume_token (parser->lexer);
6165  /* Consume the opening `('.  */
6166  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6167  /* Parse the type-id.  */
6168  type = cp_parser_type_id (parser);
6169  /* Look for the `,'.  */
6170  cp_parser_require (parser, CPP_COMMA, "`,'");
6171
6172  /* Build the (type *)null that begins the traditional offsetof macro.  */
6173  expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6174
6175  /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6176  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6177						 true, &dummy);
6178  while (true)
6179    {
6180      cp_token *token = cp_lexer_peek_token (parser->lexer);
6181      switch (token->type)
6182	{
6183	case CPP_OPEN_SQUARE:
6184	  /* offsetof-member-designator "[" expression "]" */
6185	  expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6186	  break;
6187
6188	case CPP_DOT:
6189	  /* offsetof-member-designator "." identifier */
6190	  cp_lexer_consume_token (parser->lexer);
6191	  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6192							 true, &dummy);
6193	  break;
6194
6195	case CPP_CLOSE_PAREN:
6196	  /* Consume the ")" token.  */
6197	  cp_lexer_consume_token (parser->lexer);
6198	  goto success;
6199
6200	default:
6201	  /* Error.  We know the following require will fail, but
6202	     that gives the proper error message.  */
6203	  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6204	  cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6205	  expr = error_mark_node;
6206	  goto failure;
6207	}
6208    }
6209
6210 success:
6211  /* If we're processing a template, we can't finish the semantics yet.
6212     Otherwise we can fold the entire expression now.  */
6213  if (processing_template_decl)
6214    expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6215  else
6216    expr = finish_offsetof (expr);
6217
6218 failure:
6219  parser->integral_constant_expression_p = save_ice_p;
6220  parser->non_integral_constant_expression_p = save_non_ice_p;
6221
6222  return expr;
6223}
6224
6225/* Statements [gram.stmt.stmt]  */
6226
6227/* Parse a statement.
6228
6229   statement:
6230     labeled-statement
6231     expression-statement
6232     compound-statement
6233     selection-statement
6234     iteration-statement
6235     jump-statement
6236     declaration-statement
6237     try-block
6238
6239  IN_COMPOUND is true when the statement is nested inside a
6240  cp_parser_compound_statement; this matters for certain pragmas.
6241
6242  If IF_P is not NULL, *IF_P is set to indicate whether the statement
6243  is a (possibly labeled) if statement which is not enclosed in braces
6244  and has an else clause.  This is used to implement -Wparentheses.  */
6245
6246static void
6247cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6248		     bool in_compound, bool *if_p)
6249{
6250  tree statement;
6251  cp_token *token;
6252  location_t statement_location;
6253
6254 restart:
6255  if (if_p != NULL)
6256    *if_p = false;
6257  /* There is no statement yet.  */
6258  statement = NULL_TREE;
6259  /* Peek at the next token.  */
6260  token = cp_lexer_peek_token (parser->lexer);
6261  /* Remember the location of the first token in the statement.  */
6262  statement_location = token->location;
6263  /* If this is a keyword, then that will often determine what kind of
6264     statement we have.  */
6265  if (token->type == CPP_KEYWORD)
6266    {
6267      enum rid keyword = token->keyword;
6268
6269      switch (keyword)
6270	{
6271	case RID_CASE:
6272	case RID_DEFAULT:
6273	  /* Looks like a labeled-statement with a case label.
6274	     Parse the label, and then use tail recursion to parse
6275	     the statement.  */
6276	  cp_parser_label_for_labeled_statement (parser);
6277	  goto restart;
6278
6279	case RID_IF:
6280	case RID_SWITCH:
6281	  statement = cp_parser_selection_statement (parser, if_p);
6282	  break;
6283
6284	case RID_WHILE:
6285	case RID_DO:
6286	case RID_FOR:
6287	  statement = cp_parser_iteration_statement (parser);
6288	  break;
6289
6290	case RID_BREAK:
6291	case RID_CONTINUE:
6292	case RID_RETURN:
6293	case RID_GOTO:
6294	  statement = cp_parser_jump_statement (parser);
6295	  break;
6296
6297	  /* Objective-C++ exception-handling constructs.  */
6298	case RID_AT_TRY:
6299	case RID_AT_CATCH:
6300	case RID_AT_FINALLY:
6301	case RID_AT_SYNCHRONIZED:
6302	case RID_AT_THROW:
6303	  statement = cp_parser_objc_statement (parser);
6304	  break;
6305
6306	case RID_TRY:
6307	  statement = cp_parser_try_block (parser);
6308	  break;
6309
6310	default:
6311	  /* It might be a keyword like `int' that can start a
6312	     declaration-statement.  */
6313	  break;
6314	}
6315    }
6316  else if (token->type == CPP_NAME)
6317    {
6318      /* If the next token is a `:', then we are looking at a
6319	 labeled-statement.  */
6320      token = cp_lexer_peek_nth_token (parser->lexer, 2);
6321      if (token->type == CPP_COLON)
6322	{
6323	  /* Looks like a labeled-statement with an ordinary label.
6324	     Parse the label, and then use tail recursion to parse
6325	     the statement.  */
6326	  cp_parser_label_for_labeled_statement (parser);
6327	  goto restart;
6328	}
6329    }
6330  /* Anything that starts with a `{' must be a compound-statement.  */
6331  else if (token->type == CPP_OPEN_BRACE)
6332    statement = cp_parser_compound_statement (parser, NULL, false);
6333  /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6334     a statement all its own.  */
6335  else if (token->type == CPP_PRAGMA)
6336    {
6337      /* Only certain OpenMP pragmas are attached to statements, and thus
6338	 are considered statements themselves.  All others are not.  In
6339	 the context of a compound, accept the pragma as a "statement" and
6340	 return so that we can check for a close brace.  Otherwise we
6341	 require a real statement and must go back and read one.  */
6342      if (in_compound)
6343	cp_parser_pragma (parser, pragma_compound);
6344      else if (!cp_parser_pragma (parser, pragma_stmt))
6345	goto restart;
6346      return;
6347    }
6348  else if (token->type == CPP_EOF)
6349    {
6350      cp_parser_error (parser, "expected statement");
6351      return;
6352    }
6353
6354  /* Everything else must be a declaration-statement or an
6355     expression-statement.  Try for the declaration-statement
6356     first, unless we are looking at a `;', in which case we know that
6357     we have an expression-statement.  */
6358  if (!statement)
6359    {
6360      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6361	{
6362	  cp_parser_parse_tentatively (parser);
6363	  /* Try to parse the declaration-statement.  */
6364	  cp_parser_declaration_statement (parser);
6365	  /* If that worked, we're done.  */
6366	  if (cp_parser_parse_definitely (parser))
6367	    return;
6368	}
6369      /* Look for an expression-statement instead.  */
6370      statement = cp_parser_expression_statement (parser, in_statement_expr);
6371    }
6372
6373  /* Set the line number for the statement.  */
6374  if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6375    SET_EXPR_LOCATION (statement, statement_location);
6376}
6377
6378/* Parse the label for a labeled-statement, i.e.
6379
6380   identifier :
6381   case constant-expression :
6382   default :
6383
6384   GNU Extension:
6385   case constant-expression ... constant-expression : statement
6386
6387   When a label is parsed without errors, the label is added to the
6388   parse tree by the finish_* functions, so this function doesn't
6389   have to return the label.  */
6390
6391static void
6392cp_parser_label_for_labeled_statement (cp_parser* parser)
6393{
6394  cp_token *token;
6395
6396  /* The next token should be an identifier.  */
6397  token = cp_lexer_peek_token (parser->lexer);
6398  if (token->type != CPP_NAME
6399      && token->type != CPP_KEYWORD)
6400    {
6401      cp_parser_error (parser, "expected labeled-statement");
6402      return;
6403    }
6404
6405  switch (token->keyword)
6406    {
6407    case RID_CASE:
6408      {
6409	tree expr, expr_hi;
6410	cp_token *ellipsis;
6411
6412	/* Consume the `case' token.  */
6413	cp_lexer_consume_token (parser->lexer);
6414	/* Parse the constant-expression.  */
6415	expr = cp_parser_constant_expression (parser,
6416					      /*allow_non_constant_p=*/false,
6417					      NULL);
6418
6419	ellipsis = cp_lexer_peek_token (parser->lexer);
6420	if (ellipsis->type == CPP_ELLIPSIS)
6421	  {
6422	    /* Consume the `...' token.  */
6423	    cp_lexer_consume_token (parser->lexer);
6424	    expr_hi =
6425	      cp_parser_constant_expression (parser,
6426					     /*allow_non_constant_p=*/false,
6427					     NULL);
6428	    /* We don't need to emit warnings here, as the common code
6429	       will do this for us.  */
6430	  }
6431	else
6432	  expr_hi = NULL_TREE;
6433
6434	if (parser->in_switch_statement_p)
6435	  finish_case_label (expr, expr_hi);
6436	else
6437	  error ("case label %qE not within a switch statement", expr);
6438      }
6439      break;
6440
6441    case RID_DEFAULT:
6442      /* Consume the `default' token.  */
6443      cp_lexer_consume_token (parser->lexer);
6444
6445      if (parser->in_switch_statement_p)
6446	finish_case_label (NULL_TREE, NULL_TREE);
6447      else
6448	error ("case label not within a switch statement");
6449      break;
6450
6451    default:
6452      /* Anything else must be an ordinary label.  */
6453      finish_label_stmt (cp_parser_identifier (parser));
6454      break;
6455    }
6456
6457  /* Require the `:' token.  */
6458  cp_parser_require (parser, CPP_COLON, "`:'");
6459}
6460
6461/* Parse an expression-statement.
6462
6463   expression-statement:
6464     expression [opt] ;
6465
6466   Returns the new EXPR_STMT -- or NULL_TREE if the expression
6467   statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6468   indicates whether this expression-statement is part of an
6469   expression statement.  */
6470
6471static tree
6472cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6473{
6474  tree statement = NULL_TREE;
6475
6476  /* If the next token is a ';', then there is no expression
6477     statement.  */
6478  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6479    statement = cp_parser_expression (parser, /*cast_p=*/false);
6480
6481  /* Consume the final `;'.  */
6482  cp_parser_consume_semicolon_at_end_of_statement (parser);
6483
6484  if (in_statement_expr
6485      && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6486    /* This is the final expression statement of a statement
6487       expression.  */
6488    statement = finish_stmt_expr_expr (statement, in_statement_expr);
6489  else if (statement)
6490    statement = finish_expr_stmt (statement);
6491  else
6492    finish_stmt ();
6493
6494  return statement;
6495}
6496
6497/* Parse a compound-statement.
6498
6499   compound-statement:
6500     { statement-seq [opt] }
6501
6502   Returns a tree representing the statement.  */
6503
6504static tree
6505cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6506			      bool in_try)
6507{
6508  tree compound_stmt;
6509
6510  /* Consume the `{'.  */
6511  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6512    return error_mark_node;
6513  /* Begin the compound-statement.  */
6514  compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6515  /* Parse an (optional) statement-seq.  */
6516  cp_parser_statement_seq_opt (parser, in_statement_expr);
6517  /* Finish the compound-statement.  */
6518  finish_compound_stmt (compound_stmt);
6519  /* Consume the `}'.  */
6520  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6521
6522  return compound_stmt;
6523}
6524
6525/* Parse an (optional) statement-seq.
6526
6527   statement-seq:
6528     statement
6529     statement-seq [opt] statement  */
6530
6531static void
6532cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6533{
6534  /* Scan statements until there aren't any more.  */
6535  while (true)
6536    {
6537      cp_token *token = cp_lexer_peek_token (parser->lexer);
6538
6539      /* If we're looking at a `}', then we've run out of statements.  */
6540      if (token->type == CPP_CLOSE_BRACE
6541	  || token->type == CPP_EOF
6542	  || token->type == CPP_PRAGMA_EOL)
6543	break;
6544
6545      /* Parse the statement.  */
6546      cp_parser_statement (parser, in_statement_expr, true, NULL);
6547    }
6548}
6549
6550/* Parse a selection-statement.
6551
6552   selection-statement:
6553     if ( condition ) statement
6554     if ( condition ) statement else statement
6555     switch ( condition ) statement
6556
6557   Returns the new IF_STMT or SWITCH_STMT.
6558
6559   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6560   is a (possibly labeled) if statement which is not enclosed in
6561   braces and has an else clause.  This is used to implement
6562   -Wparentheses.  */
6563
6564static tree
6565cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6566{
6567  cp_token *token;
6568  enum rid keyword;
6569
6570  if (if_p != NULL)
6571    *if_p = false;
6572
6573  /* Peek at the next token.  */
6574  token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6575
6576  /* See what kind of keyword it is.  */
6577  keyword = token->keyword;
6578  switch (keyword)
6579    {
6580    case RID_IF:
6581    case RID_SWITCH:
6582      {
6583	tree statement;
6584	tree condition;
6585
6586	/* Look for the `('.  */
6587	if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6588	  {
6589	    cp_parser_skip_to_end_of_statement (parser);
6590	    return error_mark_node;
6591	  }
6592
6593	/* Begin the selection-statement.  */
6594	if (keyword == RID_IF)
6595	  statement = begin_if_stmt ();
6596	else
6597	  statement = begin_switch_stmt ();
6598
6599	/* Parse the condition.  */
6600	condition = cp_parser_condition (parser);
6601	/* Look for the `)'.  */
6602	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6603	  cp_parser_skip_to_closing_parenthesis (parser, true, false,
6604						 /*consume_paren=*/true);
6605
6606	if (keyword == RID_IF)
6607	  {
6608	    bool nested_if;
6609
6610	    /* Add the condition.  */
6611	    finish_if_stmt_cond (condition, statement);
6612
6613	    /* Parse the then-clause.  */
6614	    cp_parser_implicitly_scoped_statement (parser, &nested_if);
6615	    finish_then_clause (statement);
6616
6617	    /* If the next token is `else', parse the else-clause.  */
6618	    if (cp_lexer_next_token_is_keyword (parser->lexer,
6619						RID_ELSE))
6620	      {
6621		/* Consume the `else' keyword.  */
6622		cp_lexer_consume_token (parser->lexer);
6623		begin_else_clause (statement);
6624		/* Parse the else-clause.  */
6625		cp_parser_implicitly_scoped_statement (parser, NULL);
6626		finish_else_clause (statement);
6627
6628		/* If we are currently parsing a then-clause, then
6629		   IF_P will not be NULL.  We set it to true to
6630		   indicate that this if statement has an else clause.
6631		   This may trigger the Wparentheses warning below
6632		   when we get back up to the parent if statement.  */
6633		if (if_p != NULL)
6634		  *if_p = true;
6635	      }
6636	    else
6637	      {
6638		/* This if statement does not have an else clause.  If
6639		   NESTED_IF is true, then the then-clause is an if
6640		   statement which does have an else clause.  We warn
6641		   about the potential ambiguity.  */
6642		if (nested_if)
6643		  warning (OPT_Wparentheses,
6644			   ("%Hsuggest explicit braces "
6645			    "to avoid ambiguous %<else%>"),
6646			   EXPR_LOCUS (statement));
6647	      }
6648
6649	    /* Now we're all done with the if-statement.  */
6650	    finish_if_stmt (statement);
6651	  }
6652	else
6653	  {
6654	    bool in_switch_statement_p;
6655	    unsigned char in_statement;
6656
6657	    /* Add the condition.  */
6658	    finish_switch_cond (condition, statement);
6659
6660	    /* Parse the body of the switch-statement.  */
6661	    in_switch_statement_p = parser->in_switch_statement_p;
6662	    in_statement = parser->in_statement;
6663	    parser->in_switch_statement_p = true;
6664	    parser->in_statement |= IN_SWITCH_STMT;
6665	    cp_parser_implicitly_scoped_statement (parser, NULL);
6666	    parser->in_switch_statement_p = in_switch_statement_p;
6667	    parser->in_statement = in_statement;
6668
6669	    /* Now we're all done with the switch-statement.  */
6670	    finish_switch_stmt (statement);
6671	  }
6672
6673	return statement;
6674      }
6675      break;
6676
6677    default:
6678      cp_parser_error (parser, "expected selection-statement");
6679      return error_mark_node;
6680    }
6681}
6682
6683/* Parse a condition.
6684
6685   condition:
6686     expression
6687     type-specifier-seq declarator = assignment-expression
6688
6689   GNU Extension:
6690
6691   condition:
6692     type-specifier-seq declarator asm-specification [opt]
6693       attributes [opt] = assignment-expression
6694
6695   Returns the expression that should be tested.  */
6696
6697static tree
6698cp_parser_condition (cp_parser* parser)
6699{
6700  cp_decl_specifier_seq type_specifiers;
6701  const char *saved_message;
6702
6703  /* Try the declaration first.  */
6704  cp_parser_parse_tentatively (parser);
6705  /* New types are not allowed in the type-specifier-seq for a
6706     condition.  */
6707  saved_message = parser->type_definition_forbidden_message;
6708  parser->type_definition_forbidden_message
6709    = "types may not be defined in conditions";
6710  /* Parse the type-specifier-seq.  */
6711  cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6712				&type_specifiers);
6713  /* Restore the saved message.  */
6714  parser->type_definition_forbidden_message = saved_message;
6715  /* If all is well, we might be looking at a declaration.  */
6716  if (!cp_parser_error_occurred (parser))
6717    {
6718      tree decl;
6719      tree asm_specification;
6720      tree attributes;
6721      cp_declarator *declarator;
6722      tree initializer = NULL_TREE;
6723
6724      /* Parse the declarator.  */
6725      declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6726					 /*ctor_dtor_or_conv_p=*/NULL,
6727					 /*parenthesized_p=*/NULL,
6728					 /*member_p=*/false);
6729      /* Parse the attributes.  */
6730      attributes = cp_parser_attributes_opt (parser);
6731      /* Parse the asm-specification.  */
6732      asm_specification = cp_parser_asm_specification_opt (parser);
6733      /* If the next token is not an `=', then we might still be
6734	 looking at an expression.  For example:
6735
6736	   if (A(a).x)
6737
6738	 looks like a decl-specifier-seq and a declarator -- but then
6739	 there is no `=', so this is an expression.  */
6740      cp_parser_require (parser, CPP_EQ, "`='");
6741      /* If we did see an `=', then we are looking at a declaration
6742	 for sure.  */
6743      if (cp_parser_parse_definitely (parser))
6744	{
6745	  tree pushed_scope;
6746	  bool non_constant_p;
6747
6748	  /* Create the declaration.  */
6749	  decl = start_decl (declarator, &type_specifiers,
6750			     /*initialized_p=*/true,
6751			     attributes, /*prefix_attributes=*/NULL_TREE,
6752			     &pushed_scope);
6753	  /* Parse the assignment-expression.  */
6754	  initializer
6755	    = cp_parser_constant_expression (parser,
6756					     /*allow_non_constant_p=*/true,
6757					     &non_constant_p);
6758	  if (!non_constant_p)
6759	    initializer = fold_non_dependent_expr (initializer);
6760
6761	  /* Process the initializer.  */
6762	  cp_finish_decl (decl,
6763			  initializer, !non_constant_p,
6764			  asm_specification,
6765			  LOOKUP_ONLYCONVERTING);
6766
6767	  if (pushed_scope)
6768	    pop_scope (pushed_scope);
6769
6770	  return convert_from_reference (decl);
6771	}
6772    }
6773  /* If we didn't even get past the declarator successfully, we are
6774     definitely not looking at a declaration.  */
6775  else
6776    cp_parser_abort_tentative_parse (parser);
6777
6778  /* Otherwise, we are looking at an expression.  */
6779  return cp_parser_expression (parser, /*cast_p=*/false);
6780}
6781
6782/* Parse an iteration-statement.
6783
6784   iteration-statement:
6785     while ( condition ) statement
6786     do statement while ( expression ) ;
6787     for ( for-init-statement condition [opt] ; expression [opt] )
6788       statement
6789
6790   APPLE LOCAL begin for-fsf-4_4 3274130 5295549
6791   GNU extension:
6792
6793     while attributes [opt] ( condition ) statement
6794     do attributes [opt] statement while ( expression ) ;
6795     for attributes [opt]
6796       ( for-init-statement condition [opt] ; expression [opt] )
6797       statement
6798
6799   APPLE LOCAL end for-fsf-4_4 3274130 5295549
6800   Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6801
6802static tree
6803cp_parser_iteration_statement (cp_parser* parser)
6804{
6805  cp_token *token;
6806  enum rid keyword;
6807/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
6808  tree statement, attributes;
6809/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
6810  unsigned char in_statement;
6811
6812/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
6813  /* Get the keyword at the start of the loop.  */
6814/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
6815  token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6816  if (!token)
6817    return error_mark_node;
6818
6819  /* Remember whether or not we are already within an iteration
6820     statement.  */
6821  in_statement = parser->in_statement;
6822
6823/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
6824  /* Parse the attributes, if any.  */
6825  attributes = cp_parser_attributes_opt (parser);
6826
6827/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
6828  /* See what kind of keyword it is.  */
6829  keyword = token->keyword;
6830  switch (keyword)
6831    {
6832    case RID_WHILE:
6833      {
6834	tree condition;
6835
6836	/* Begin the while-statement.  */
6837/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
6838	statement = begin_while_stmt (attributes);
6839/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
6840	/* Look for the `('.  */
6841	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6842	/* Parse the condition.  */
6843	condition = cp_parser_condition (parser);
6844	finish_while_stmt_cond (condition, statement);
6845	/* Look for the `)'.  */
6846	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6847	/* Parse the dependent statement.  */
6848	parser->in_statement = IN_ITERATION_STMT;
6849	cp_parser_already_scoped_statement (parser);
6850	parser->in_statement = in_statement;
6851	/* We're done with the while-statement.  */
6852	finish_while_stmt (statement);
6853      }
6854      break;
6855
6856    case RID_DO:
6857      {
6858	tree expression;
6859
6860	/* Begin the do-statement.  */
6861/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
6862	statement = begin_do_stmt (attributes);
6863/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
6864	/* Parse the body of the do-statement.  */
6865	parser->in_statement = IN_ITERATION_STMT;
6866	cp_parser_implicitly_scoped_statement (parser, NULL);
6867	parser->in_statement = in_statement;
6868	finish_do_body (statement);
6869	/* Look for the `while' keyword.  */
6870	cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6871	/* Look for the `('.  */
6872	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6873	/* Parse the expression.  */
6874	expression = cp_parser_expression (parser, /*cast_p=*/false);
6875	/* We're done with the do-statement.  */
6876	finish_do_stmt (expression, statement);
6877	/* Look for the `)'.  */
6878	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6879	/* Look for the `;'.  */
6880	cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6881      }
6882      break;
6883
6884    case RID_FOR:
6885      {
6886	tree condition = NULL_TREE;
6887	tree expression = NULL_TREE;
6888
6889	/* Begin the for-statement.  */
6890/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
6891	statement = begin_for_stmt (attributes);
6892/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
6893	/* Look for the `('.  */
6894	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6895	/* Parse the initialization.  */
6896	cp_parser_for_init_statement (parser);
6897	finish_for_init_stmt (statement);
6898
6899	/* If there's a condition, process it.  */
6900	if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6901	  condition = cp_parser_condition (parser);
6902	finish_for_cond (condition, statement);
6903	/* Look for the `;'.  */
6904	cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6905
6906	/* If there's an expression, process it.  */
6907	if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6908	  expression = cp_parser_expression (parser, /*cast_p=*/false);
6909	finish_for_expr (expression, statement);
6910	/* Look for the `)'.  */
6911	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6912
6913	/* Parse the body of the for-statement.  */
6914	parser->in_statement = IN_ITERATION_STMT;
6915	cp_parser_already_scoped_statement (parser);
6916	parser->in_statement = in_statement;
6917
6918	/* We're done with the for-statement.  */
6919	finish_for_stmt (statement);
6920      }
6921      break;
6922
6923    default:
6924      cp_parser_error (parser, "expected iteration-statement");
6925      statement = error_mark_node;
6926      break;
6927    }
6928
6929  return statement;
6930}
6931
6932/* Parse a for-init-statement.
6933
6934   for-init-statement:
6935     expression-statement
6936     simple-declaration  */
6937
6938static void
6939cp_parser_for_init_statement (cp_parser* parser)
6940{
6941  /* If the next token is a `;', then we have an empty
6942     expression-statement.  Grammatically, this is also a
6943     simple-declaration, but an invalid one, because it does not
6944     declare anything.  Therefore, if we did not handle this case
6945     specially, we would issue an error message about an invalid
6946     declaration.  */
6947  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6948    {
6949      /* We're going to speculatively look for a declaration, falling back
6950	 to an expression, if necessary.  */
6951      cp_parser_parse_tentatively (parser);
6952      /* Parse the declaration.  */
6953      cp_parser_simple_declaration (parser,
6954				    /*function_definition_allowed_p=*/false);
6955      /* If the tentative parse failed, then we shall need to look for an
6956	 expression-statement.  */
6957      if (cp_parser_parse_definitely (parser))
6958	return;
6959    }
6960
6961  cp_parser_expression_statement (parser, false);
6962}
6963
6964/* Parse a jump-statement.
6965
6966   jump-statement:
6967     break ;
6968     continue ;
6969     return expression [opt] ;
6970     goto identifier ;
6971
6972   GNU extension:
6973
6974   jump-statement:
6975     goto * expression ;
6976
6977   Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6978
6979static tree
6980cp_parser_jump_statement (cp_parser* parser)
6981{
6982  tree statement = error_mark_node;
6983  cp_token *token;
6984  enum rid keyword;
6985
6986  /* Peek at the next token.  */
6987  token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6988  if (!token)
6989    return error_mark_node;
6990
6991  /* See what kind of keyword it is.  */
6992  keyword = token->keyword;
6993  switch (keyword)
6994    {
6995    case RID_BREAK:
6996      switch (parser->in_statement)
6997	{
6998	case 0:
6999	  error ("break statement not within loop or switch");
7000	  break;
7001	default:
7002	  gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
7003		      || parser->in_statement == IN_ITERATION_STMT);
7004	  statement = finish_break_stmt ();
7005	  break;
7006	case IN_OMP_BLOCK:
7007	  error ("invalid exit from OpenMP structured block");
7008	  break;
7009	case IN_OMP_FOR:
7010	  error ("break statement used with OpenMP for loop");
7011	  break;
7012	}
7013      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7014      break;
7015
7016    case RID_CONTINUE:
7017      switch (parser->in_statement & ~IN_SWITCH_STMT)
7018	{
7019	case 0:
7020	  error ("continue statement not within a loop");
7021	  break;
7022	case IN_ITERATION_STMT:
7023	case IN_OMP_FOR:
7024	  statement = finish_continue_stmt ();
7025	  break;
7026	case IN_OMP_BLOCK:
7027	  error ("invalid exit from OpenMP structured block");
7028	  break;
7029	default:
7030	  gcc_unreachable ();
7031	}
7032      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7033      break;
7034
7035    case RID_RETURN:
7036      {
7037	tree expr;
7038
7039	/* If the next token is a `;', then there is no
7040	   expression.  */
7041	if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7042	  expr = cp_parser_expression (parser, /*cast_p=*/false);
7043	else
7044	  expr = NULL_TREE;
7045	/* Build the return-statement.  */
7046	statement = finish_return_stmt (expr);
7047	/* Look for the final `;'.  */
7048	cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7049      }
7050      break;
7051
7052    case RID_GOTO:
7053      /* Create the goto-statement.  */
7054      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7055	{
7056	  /* Issue a warning about this use of a GNU extension.  */
7057	  if (pedantic)
7058	    pedwarn ("ISO C++ forbids computed gotos");
7059	  /* Consume the '*' token.  */
7060	  cp_lexer_consume_token (parser->lexer);
7061	  /* Parse the dependent expression.  */
7062	  finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7063	}
7064      else
7065	finish_goto_stmt (cp_parser_identifier (parser));
7066      /* Look for the final `;'.  */
7067      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7068      break;
7069
7070    default:
7071      cp_parser_error (parser, "expected jump-statement");
7072      break;
7073    }
7074
7075  return statement;
7076}
7077
7078/* Parse a declaration-statement.
7079
7080   declaration-statement:
7081     block-declaration  */
7082
7083static void
7084cp_parser_declaration_statement (cp_parser* parser)
7085{
7086  void *p;
7087
7088  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7089  p = obstack_alloc (&declarator_obstack, 0);
7090
7091 /* Parse the block-declaration.  */
7092  cp_parser_block_declaration (parser, /*statement_p=*/true);
7093
7094  /* Free any declarators allocated.  */
7095  obstack_free (&declarator_obstack, p);
7096
7097  /* Finish off the statement.  */
7098  finish_stmt ();
7099}
7100
7101/* Some dependent statements (like `if (cond) statement'), are
7102   implicitly in their own scope.  In other words, if the statement is
7103   a single statement (as opposed to a compound-statement), it is
7104   none-the-less treated as if it were enclosed in braces.  Any
7105   declarations appearing in the dependent statement are out of scope
7106   after control passes that point.  This function parses a statement,
7107   but ensures that is in its own scope, even if it is not a
7108   compound-statement.
7109
7110   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7111   is a (possibly labeled) if statement which is not enclosed in
7112   braces and has an else clause.  This is used to implement
7113   -Wparentheses.
7114
7115   Returns the new statement.  */
7116
7117static tree
7118cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7119{
7120  tree statement;
7121
7122  if (if_p != NULL)
7123    *if_p = false;
7124
7125  /* Mark if () ; with a special NOP_EXPR.  */
7126  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7127    {
7128      cp_lexer_consume_token (parser->lexer);
7129      statement = add_stmt (build_empty_stmt ());
7130    }
7131  /* if a compound is opened, we simply parse the statement directly.  */
7132  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7133    statement = cp_parser_compound_statement (parser, NULL, false);
7134  /* If the token is not a `{', then we must take special action.  */
7135  else
7136    {
7137      /* Create a compound-statement.  */
7138      statement = begin_compound_stmt (0);
7139      /* Parse the dependent-statement.  */
7140      cp_parser_statement (parser, NULL_TREE, false, if_p);
7141      /* Finish the dummy compound-statement.  */
7142      finish_compound_stmt (statement);
7143    }
7144
7145  /* Return the statement.  */
7146  return statement;
7147}
7148
7149/* For some dependent statements (like `while (cond) statement'), we
7150   have already created a scope.  Therefore, even if the dependent
7151   statement is a compound-statement, we do not want to create another
7152   scope.  */
7153
7154static void
7155cp_parser_already_scoped_statement (cp_parser* parser)
7156{
7157  /* If the token is a `{', then we must take special action.  */
7158  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7159    cp_parser_statement (parser, NULL_TREE, false, NULL);
7160  else
7161    {
7162      /* Avoid calling cp_parser_compound_statement, so that we
7163	 don't create a new scope.  Do everything else by hand.  */
7164      cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7165      cp_parser_statement_seq_opt (parser, NULL_TREE);
7166      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7167    }
7168}
7169
7170/* Declarations [gram.dcl.dcl] */
7171
7172/* Parse an optional declaration-sequence.
7173
7174   declaration-seq:
7175     declaration
7176     declaration-seq declaration  */
7177
7178static void
7179cp_parser_declaration_seq_opt (cp_parser* parser)
7180{
7181  while (true)
7182    {
7183      cp_token *token;
7184
7185      token = cp_lexer_peek_token (parser->lexer);
7186
7187      if (token->type == CPP_CLOSE_BRACE
7188	  || token->type == CPP_EOF
7189	  || token->type == CPP_PRAGMA_EOL)
7190	break;
7191
7192      if (token->type == CPP_SEMICOLON)
7193	{
7194	  /* A declaration consisting of a single semicolon is
7195	     invalid.  Allow it unless we're being pedantic.  */
7196	  cp_lexer_consume_token (parser->lexer);
7197	  if (pedantic && !in_system_header)
7198	    pedwarn ("extra %<;%>");
7199	  continue;
7200	}
7201
7202      /* If we're entering or exiting a region that's implicitly
7203	 extern "C", modify the lang context appropriately.  */
7204      if (!parser->implicit_extern_c && token->implicit_extern_c)
7205	{
7206	  push_lang_context (lang_name_c);
7207	  parser->implicit_extern_c = true;
7208	}
7209      else if (parser->implicit_extern_c && !token->implicit_extern_c)
7210	{
7211	  pop_lang_context ();
7212	  parser->implicit_extern_c = false;
7213	}
7214
7215      if (token->type == CPP_PRAGMA)
7216	{
7217	  /* A top-level declaration can consist solely of a #pragma.
7218	     A nested declaration cannot, so this is done here and not
7219	     in cp_parser_declaration.  (A #pragma at block scope is
7220	     handled in cp_parser_statement.)  */
7221	  cp_parser_pragma (parser, pragma_external);
7222	  continue;
7223	}
7224
7225      /* Parse the declaration itself.  */
7226      cp_parser_declaration (parser);
7227    }
7228}
7229
7230/* Parse a declaration.
7231
7232   declaration:
7233     block-declaration
7234     function-definition
7235     template-declaration
7236     explicit-instantiation
7237     explicit-specialization
7238     linkage-specification
7239     namespace-definition
7240
7241   GNU extension:
7242
7243   declaration:
7244      __extension__ declaration */
7245
7246static void
7247cp_parser_declaration (cp_parser* parser)
7248{
7249  cp_token token1;
7250  cp_token token2;
7251  int saved_pedantic;
7252  void *p;
7253
7254  /* Check for the `__extension__' keyword.  */
7255  if (cp_parser_extension_opt (parser, &saved_pedantic))
7256    {
7257      /* Parse the qualified declaration.  */
7258      cp_parser_declaration (parser);
7259      /* Restore the PEDANTIC flag.  */
7260      pedantic = saved_pedantic;
7261
7262      return;
7263    }
7264
7265  /* Try to figure out what kind of declaration is present.  */
7266  token1 = *cp_lexer_peek_token (parser->lexer);
7267
7268  if (token1.type != CPP_EOF)
7269    token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7270  else
7271    {
7272      token2.type = CPP_EOF;
7273      token2.keyword = RID_MAX;
7274    }
7275
7276  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7277  p = obstack_alloc (&declarator_obstack, 0);
7278
7279  /* If the next token is `extern' and the following token is a string
7280     literal, then we have a linkage specification.  */
7281  if (token1.keyword == RID_EXTERN
7282      && cp_parser_is_string_literal (&token2))
7283    cp_parser_linkage_specification (parser);
7284  /* If the next token is `template', then we have either a template
7285     declaration, an explicit instantiation, or an explicit
7286     specialization.  */
7287  else if (token1.keyword == RID_TEMPLATE)
7288    {
7289      /* `template <>' indicates a template specialization.  */
7290      if (token2.type == CPP_LESS
7291	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7292	cp_parser_explicit_specialization (parser);
7293      /* `template <' indicates a template declaration.  */
7294      else if (token2.type == CPP_LESS)
7295	cp_parser_template_declaration (parser, /*member_p=*/false);
7296      /* Anything else must be an explicit instantiation.  */
7297      else
7298	cp_parser_explicit_instantiation (parser);
7299    }
7300  /* If the next token is `export', then we have a template
7301     declaration.  */
7302  else if (token1.keyword == RID_EXPORT)
7303    cp_parser_template_declaration (parser, /*member_p=*/false);
7304  /* If the next token is `extern', 'static' or 'inline' and the one
7305     after that is `template', we have a GNU extended explicit
7306     instantiation directive.  */
7307  else if (cp_parser_allow_gnu_extensions_p (parser)
7308	   && (token1.keyword == RID_EXTERN
7309	       || token1.keyword == RID_STATIC
7310	       || token1.keyword == RID_INLINE)
7311	   && token2.keyword == RID_TEMPLATE)
7312    cp_parser_explicit_instantiation (parser);
7313  /* If the next token is `namespace', check for a named or unnamed
7314     namespace definition.  */
7315  else if (token1.keyword == RID_NAMESPACE
7316	   && (/* A named namespace definition.  */
7317	       (token2.type == CPP_NAME
7318		&& (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7319		    != CPP_EQ))
7320	       /* An unnamed namespace definition.  */
7321	       || token2.type == CPP_OPEN_BRACE
7322	       || token2.keyword == RID_ATTRIBUTE))
7323    cp_parser_namespace_definition (parser);
7324  /* Objective-C++ declaration/definition.  */
7325  else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7326    cp_parser_objc_declaration (parser);
7327  /* We must have either a block declaration or a function
7328     definition.  */
7329  else
7330    /* Try to parse a block-declaration, or a function-definition.  */
7331    cp_parser_block_declaration (parser, /*statement_p=*/false);
7332
7333  /* Free any declarators allocated.  */
7334  obstack_free (&declarator_obstack, p);
7335}
7336
7337/* Parse a block-declaration.
7338
7339   block-declaration:
7340     simple-declaration
7341     asm-definition
7342     namespace-alias-definition
7343     using-declaration
7344     using-directive
7345
7346   GNU Extension:
7347
7348   block-declaration:
7349     __extension__ block-declaration
7350     label-declaration
7351
7352   If STATEMENT_P is TRUE, then this block-declaration is occurring as
7353   part of a declaration-statement.  */
7354
7355static void
7356cp_parser_block_declaration (cp_parser *parser,
7357			     bool      statement_p)
7358{
7359  cp_token *token1;
7360  int saved_pedantic;
7361
7362  /* Check for the `__extension__' keyword.  */
7363  if (cp_parser_extension_opt (parser, &saved_pedantic))
7364    {
7365      /* Parse the qualified declaration.  */
7366      cp_parser_block_declaration (parser, statement_p);
7367      /* Restore the PEDANTIC flag.  */
7368      pedantic = saved_pedantic;
7369
7370      return;
7371    }
7372
7373  /* Peek at the next token to figure out which kind of declaration is
7374     present.  */
7375  token1 = cp_lexer_peek_token (parser->lexer);
7376
7377  /* If the next keyword is `asm', we have an asm-definition.  */
7378  if (token1->keyword == RID_ASM)
7379    {
7380      if (statement_p)
7381	cp_parser_commit_to_tentative_parse (parser);
7382      cp_parser_asm_definition (parser);
7383    }
7384  /* If the next keyword is `namespace', we have a
7385     namespace-alias-definition.  */
7386  else if (token1->keyword == RID_NAMESPACE)
7387    cp_parser_namespace_alias_definition (parser);
7388  /* If the next keyword is `using', we have either a
7389     using-declaration or a using-directive.  */
7390  else if (token1->keyword == RID_USING)
7391    {
7392      cp_token *token2;
7393
7394      if (statement_p)
7395	cp_parser_commit_to_tentative_parse (parser);
7396      /* If the token after `using' is `namespace', then we have a
7397	 using-directive.  */
7398      token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7399      if (token2->keyword == RID_NAMESPACE)
7400	cp_parser_using_directive (parser);
7401      /* Otherwise, it's a using-declaration.  */
7402      else
7403	cp_parser_using_declaration (parser,
7404				     /*access_declaration_p=*/false);
7405    }
7406  /* If the next keyword is `__label__' we have a label declaration.  */
7407  else if (token1->keyword == RID_LABEL)
7408    {
7409      if (statement_p)
7410	cp_parser_commit_to_tentative_parse (parser);
7411      cp_parser_label_declaration (parser);
7412    }
7413  /* Anything else must be a simple-declaration.  */
7414  else
7415    cp_parser_simple_declaration (parser, !statement_p);
7416}
7417
7418/* Parse a simple-declaration.
7419
7420   simple-declaration:
7421     decl-specifier-seq [opt] init-declarator-list [opt] ;
7422
7423   init-declarator-list:
7424     init-declarator
7425     init-declarator-list , init-declarator
7426
7427   If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7428   function-definition as a simple-declaration.  */
7429
7430static void
7431cp_parser_simple_declaration (cp_parser* parser,
7432			      bool function_definition_allowed_p)
7433{
7434  cp_decl_specifier_seq decl_specifiers;
7435  int declares_class_or_enum;
7436  bool saw_declarator;
7437
7438  /* Defer access checks until we know what is being declared; the
7439     checks for names appearing in the decl-specifier-seq should be
7440     done as if we were in the scope of the thing being declared.  */
7441  push_deferring_access_checks (dk_deferred);
7442
7443  /* Parse the decl-specifier-seq.  We have to keep track of whether
7444     or not the decl-specifier-seq declares a named class or
7445     enumeration type, since that is the only case in which the
7446     init-declarator-list is allowed to be empty.
7447
7448     [dcl.dcl]
7449
7450     In a simple-declaration, the optional init-declarator-list can be
7451     omitted only when declaring a class or enumeration, that is when
7452     the decl-specifier-seq contains either a class-specifier, an
7453     elaborated-type-specifier, or an enum-specifier.  */
7454  cp_parser_decl_specifier_seq (parser,
7455				CP_PARSER_FLAGS_OPTIONAL,
7456				&decl_specifiers,
7457				&declares_class_or_enum);
7458  /* We no longer need to defer access checks.  */
7459  stop_deferring_access_checks ();
7460
7461  /* In a block scope, a valid declaration must always have a
7462     decl-specifier-seq.  By not trying to parse declarators, we can
7463     resolve the declaration/expression ambiguity more quickly.  */
7464  if (!function_definition_allowed_p
7465      && !decl_specifiers.any_specifiers_p)
7466    {
7467      cp_parser_error (parser, "expected declaration");
7468      goto done;
7469    }
7470
7471  /* If the next two tokens are both identifiers, the code is
7472     erroneous. The usual cause of this situation is code like:
7473
7474       T t;
7475
7476     where "T" should name a type -- but does not.  */
7477  if (!decl_specifiers.type
7478      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7479    {
7480      /* If parsing tentatively, we should commit; we really are
7481	 looking at a declaration.  */
7482      cp_parser_commit_to_tentative_parse (parser);
7483      /* Give up.  */
7484      goto done;
7485    }
7486
7487  /* If we have seen at least one decl-specifier, and the next token
7488     is not a parenthesis, then we must be looking at a declaration.
7489     (After "int (" we might be looking at a functional cast.)  */
7490  if (decl_specifiers.any_specifiers_p
7491      && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7492    cp_parser_commit_to_tentative_parse (parser);
7493
7494  /* Keep going until we hit the `;' at the end of the simple
7495     declaration.  */
7496  saw_declarator = false;
7497  while (cp_lexer_next_token_is_not (parser->lexer,
7498				     CPP_SEMICOLON))
7499    {
7500      cp_token *token;
7501      bool function_definition_p;
7502      tree decl;
7503
7504      if (saw_declarator)
7505	{
7506	  /* If we are processing next declarator, coma is expected */
7507	  token = cp_lexer_peek_token (parser->lexer);
7508	  gcc_assert (token->type == CPP_COMMA);
7509	  cp_lexer_consume_token (parser->lexer);
7510	}
7511      else
7512	saw_declarator = true;
7513
7514      /* Parse the init-declarator.  */
7515      decl = cp_parser_init_declarator (parser, &decl_specifiers,
7516					/*checks=*/NULL,
7517					function_definition_allowed_p,
7518					/*member_p=*/false,
7519					declares_class_or_enum,
7520					&function_definition_p);
7521      /* If an error occurred while parsing tentatively, exit quickly.
7522	 (That usually happens when in the body of a function; each
7523	 statement is treated as a declaration-statement until proven
7524	 otherwise.)  */
7525      if (cp_parser_error_occurred (parser))
7526	goto done;
7527      /* Handle function definitions specially.  */
7528      if (function_definition_p)
7529	{
7530	  /* If the next token is a `,', then we are probably
7531	     processing something like:
7532
7533	       void f() {}, *p;
7534
7535	     which is erroneous.  */
7536	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7537	    error ("mixing declarations and function-definitions is forbidden");
7538	  /* Otherwise, we're done with the list of declarators.  */
7539	  else
7540	    {
7541	      pop_deferring_access_checks ();
7542	      return;
7543	    }
7544	}
7545      /* The next token should be either a `,' or a `;'.  */
7546      token = cp_lexer_peek_token (parser->lexer);
7547      /* If it's a `,', there are more declarators to come.  */
7548      if (token->type == CPP_COMMA)
7549	/* will be consumed next time around */;
7550      /* If it's a `;', we are done.  */
7551      else if (token->type == CPP_SEMICOLON)
7552	break;
7553      /* Anything else is an error.  */
7554      else
7555	{
7556	  /* If we have already issued an error message we don't need
7557	     to issue another one.  */
7558	  if (decl != error_mark_node
7559	      || cp_parser_uncommitted_to_tentative_parse_p (parser))
7560	    cp_parser_error (parser, "expected %<,%> or %<;%>");
7561	  /* Skip tokens until we reach the end of the statement.  */
7562	  cp_parser_skip_to_end_of_statement (parser);
7563	  /* If the next token is now a `;', consume it.  */
7564	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7565	    cp_lexer_consume_token (parser->lexer);
7566	  goto done;
7567	}
7568      /* After the first time around, a function-definition is not
7569	 allowed -- even if it was OK at first.  For example:
7570
7571	   int i, f() {}
7572
7573	 is not valid.  */
7574      function_definition_allowed_p = false;
7575    }
7576
7577  /* Issue an error message if no declarators are present, and the
7578     decl-specifier-seq does not itself declare a class or
7579     enumeration.  */
7580  if (!saw_declarator)
7581    {
7582      if (cp_parser_declares_only_class_p (parser))
7583	shadow_tag (&decl_specifiers);
7584      /* Perform any deferred access checks.  */
7585      perform_deferred_access_checks ();
7586    }
7587
7588  /* Consume the `;'.  */
7589  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7590
7591 done:
7592  pop_deferring_access_checks ();
7593}
7594
7595/* Parse a decl-specifier-seq.
7596
7597   decl-specifier-seq:
7598     decl-specifier-seq [opt] decl-specifier
7599
7600   decl-specifier:
7601     storage-class-specifier
7602     type-specifier
7603     function-specifier
7604     friend
7605     typedef
7606
7607   GNU Extension:
7608
7609   decl-specifier:
7610     attributes
7611
7612   Set *DECL_SPECS to a representation of the decl-specifier-seq.
7613
7614   The parser flags FLAGS is used to control type-specifier parsing.
7615
7616   *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7617   flags:
7618
7619     1: one of the decl-specifiers is an elaborated-type-specifier
7620	(i.e., a type declaration)
7621     2: one of the decl-specifiers is an enum-specifier or a
7622	class-specifier (i.e., a type definition)
7623
7624   */
7625
7626static void
7627cp_parser_decl_specifier_seq (cp_parser* parser,
7628			      cp_parser_flags flags,
7629			      cp_decl_specifier_seq *decl_specs,
7630			      int* declares_class_or_enum)
7631{
7632  bool constructor_possible_p = !parser->in_declarator_p;
7633
7634  /* Clear DECL_SPECS.  */
7635  clear_decl_specs (decl_specs);
7636
7637  /* Assume no class or enumeration type is declared.  */
7638  *declares_class_or_enum = 0;
7639
7640  /* Keep reading specifiers until there are no more to read.  */
7641  while (true)
7642    {
7643      bool constructor_p;
7644      bool found_decl_spec;
7645      cp_token *token;
7646
7647      /* Peek at the next token.  */
7648      token = cp_lexer_peek_token (parser->lexer);
7649      /* Handle attributes.  */
7650      if (token->keyword == RID_ATTRIBUTE)
7651	{
7652	  /* Parse the attributes.  */
7653	  decl_specs->attributes
7654	    = chainon (decl_specs->attributes,
7655		       cp_parser_attributes_opt (parser));
7656	  continue;
7657	}
7658      /* Assume we will find a decl-specifier keyword.  */
7659      found_decl_spec = true;
7660      /* If the next token is an appropriate keyword, we can simply
7661	 add it to the list.  */
7662      switch (token->keyword)
7663	{
7664	  /* decl-specifier:
7665	       friend  */
7666	case RID_FRIEND:
7667	  if (!at_class_scope_p ())
7668	    {
7669	      error ("%<friend%> used outside of class");
7670	      cp_lexer_purge_token (parser->lexer);
7671	    }
7672	  else
7673	    {
7674	      ++decl_specs->specs[(int) ds_friend];
7675	      /* Consume the token.  */
7676	      cp_lexer_consume_token (parser->lexer);
7677	    }
7678	  break;
7679
7680	  /* function-specifier:
7681	       inline
7682	       virtual
7683	       explicit  */
7684	case RID_INLINE:
7685	case RID_VIRTUAL:
7686	case RID_EXPLICIT:
7687	  cp_parser_function_specifier_opt (parser, decl_specs);
7688	  break;
7689
7690	  /* decl-specifier:
7691	       typedef  */
7692	case RID_TYPEDEF:
7693	  ++decl_specs->specs[(int) ds_typedef];
7694	  /* Consume the token.  */
7695	  cp_lexer_consume_token (parser->lexer);
7696	  /* A constructor declarator cannot appear in a typedef.  */
7697	  constructor_possible_p = false;
7698	  /* The "typedef" keyword can only occur in a declaration; we
7699	     may as well commit at this point.  */
7700	  cp_parser_commit_to_tentative_parse (parser);
7701
7702          if (decl_specs->storage_class != sc_none)
7703            decl_specs->conflicting_specifiers_p = true;
7704	  break;
7705
7706	  /* storage-class-specifier:
7707	       auto
7708	       register
7709	       static
7710	       extern
7711	       mutable
7712
7713	     GNU Extension:
7714	       thread  */
7715	case RID_AUTO:
7716	case RID_REGISTER:
7717	case RID_STATIC:
7718	case RID_EXTERN:
7719	case RID_MUTABLE:
7720	  /* Consume the token.  */
7721	  cp_lexer_consume_token (parser->lexer);
7722	  cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7723	  break;
7724	case RID_THREAD:
7725	  /* Consume the token.  */
7726	  cp_lexer_consume_token (parser->lexer);
7727	  ++decl_specs->specs[(int) ds_thread];
7728	  break;
7729
7730	default:
7731	  /* We did not yet find a decl-specifier yet.  */
7732	  found_decl_spec = false;
7733	  break;
7734	}
7735
7736      /* Constructors are a special case.  The `S' in `S()' is not a
7737	 decl-specifier; it is the beginning of the declarator.  */
7738      constructor_p
7739	= (!found_decl_spec
7740	   && constructor_possible_p
7741	   && (cp_parser_constructor_declarator_p
7742	       (parser, decl_specs->specs[(int) ds_friend] != 0)));
7743
7744      /* If we don't have a DECL_SPEC yet, then we must be looking at
7745	 a type-specifier.  */
7746      if (!found_decl_spec && !constructor_p)
7747	{
7748	  int decl_spec_declares_class_or_enum;
7749	  bool is_cv_qualifier;
7750	  tree type_spec;
7751
7752	  type_spec
7753	    = cp_parser_type_specifier (parser, flags,
7754					decl_specs,
7755					/*is_declaration=*/true,
7756					&decl_spec_declares_class_or_enum,
7757					&is_cv_qualifier);
7758
7759	  *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7760
7761	  /* If this type-specifier referenced a user-defined type
7762	     (a typedef, class-name, etc.), then we can't allow any
7763	     more such type-specifiers henceforth.
7764
7765	     [dcl.spec]
7766
7767	     The longest sequence of decl-specifiers that could
7768	     possibly be a type name is taken as the
7769	     decl-specifier-seq of a declaration.  The sequence shall
7770	     be self-consistent as described below.
7771
7772	     [dcl.type]
7773
7774	     As a general rule, at most one type-specifier is allowed
7775	     in the complete decl-specifier-seq of a declaration.  The
7776	     only exceptions are the following:
7777
7778	     -- const or volatile can be combined with any other
7779		type-specifier.
7780
7781	     -- signed or unsigned can be combined with char, long,
7782		short, or int.
7783
7784	     -- ..
7785
7786	     Example:
7787
7788	       typedef char* Pc;
7789	       void g (const int Pc);
7790
7791	     Here, Pc is *not* part of the decl-specifier seq; it's
7792	     the declarator.  Therefore, once we see a type-specifier
7793	     (other than a cv-qualifier), we forbid any additional
7794	     user-defined types.  We *do* still allow things like `int
7795	     int' to be considered a decl-specifier-seq, and issue the
7796	     error message later.  */
7797	  if (type_spec && !is_cv_qualifier)
7798	    flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7799	  /* A constructor declarator cannot follow a type-specifier.  */
7800	  if (type_spec)
7801	    {
7802	      constructor_possible_p = false;
7803	      found_decl_spec = true;
7804	    }
7805	}
7806
7807      /* If we still do not have a DECL_SPEC, then there are no more
7808	 decl-specifiers.  */
7809      if (!found_decl_spec)
7810	break;
7811
7812      decl_specs->any_specifiers_p = true;
7813      /* After we see one decl-specifier, further decl-specifiers are
7814	 always optional.  */
7815      flags |= CP_PARSER_FLAGS_OPTIONAL;
7816    }
7817
7818  cp_parser_check_decl_spec (decl_specs);
7819
7820  /* Don't allow a friend specifier with a class definition.  */
7821  if (decl_specs->specs[(int) ds_friend] != 0
7822      && (*declares_class_or_enum & 2))
7823    error ("class definition may not be declared a friend");
7824}
7825
7826/* Parse an (optional) storage-class-specifier.
7827
7828   storage-class-specifier:
7829     auto
7830     register
7831     static
7832     extern
7833     mutable
7834
7835   GNU Extension:
7836
7837   storage-class-specifier:
7838     thread
7839
7840   Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7841
7842static tree
7843cp_parser_storage_class_specifier_opt (cp_parser* parser)
7844{
7845  switch (cp_lexer_peek_token (parser->lexer)->keyword)
7846    {
7847    case RID_AUTO:
7848    case RID_REGISTER:
7849    case RID_STATIC:
7850    case RID_EXTERN:
7851    case RID_MUTABLE:
7852    case RID_THREAD:
7853      /* Consume the token.  */
7854      return cp_lexer_consume_token (parser->lexer)->u.value;
7855
7856    default:
7857      return NULL_TREE;
7858    }
7859}
7860
7861/* Parse an (optional) function-specifier.
7862
7863   function-specifier:
7864     inline
7865     virtual
7866     explicit
7867
7868   Returns an IDENTIFIER_NODE corresponding to the keyword used.
7869   Updates DECL_SPECS, if it is non-NULL.  */
7870
7871static tree
7872cp_parser_function_specifier_opt (cp_parser* parser,
7873				  cp_decl_specifier_seq *decl_specs)
7874{
7875  switch (cp_lexer_peek_token (parser->lexer)->keyword)
7876    {
7877    case RID_INLINE:
7878      if (decl_specs)
7879	++decl_specs->specs[(int) ds_inline];
7880      break;
7881
7882    case RID_VIRTUAL:
7883      /* 14.5.2.3 [temp.mem]
7884
7885	 A member function template shall not be virtual.  */
7886      if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7887	error ("templates may not be %<virtual%>");
7888      else if (decl_specs)
7889	++decl_specs->specs[(int) ds_virtual];
7890      break;
7891
7892    case RID_EXPLICIT:
7893      if (decl_specs)
7894	++decl_specs->specs[(int) ds_explicit];
7895      break;
7896
7897    default:
7898      return NULL_TREE;
7899    }
7900
7901  /* Consume the token.  */
7902  return cp_lexer_consume_token (parser->lexer)->u.value;
7903}
7904
7905/* Parse a linkage-specification.
7906
7907   linkage-specification:
7908     extern string-literal { declaration-seq [opt] }
7909     extern string-literal declaration  */
7910
7911static void
7912cp_parser_linkage_specification (cp_parser* parser)
7913{
7914  tree linkage;
7915
7916  /* Look for the `extern' keyword.  */
7917  cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7918
7919  /* Look for the string-literal.  */
7920  linkage = cp_parser_string_literal (parser, false, false);
7921
7922  /* Transform the literal into an identifier.  If the literal is a
7923     wide-character string, or contains embedded NULs, then we can't
7924     handle it as the user wants.  */
7925  if (strlen (TREE_STRING_POINTER (linkage))
7926      != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7927    {
7928      cp_parser_error (parser, "invalid linkage-specification");
7929      /* Assume C++ linkage.  */
7930      linkage = lang_name_cplusplus;
7931    }
7932  else
7933    linkage = get_identifier (TREE_STRING_POINTER (linkage));
7934
7935  /* We're now using the new linkage.  */
7936  push_lang_context (linkage);
7937
7938  /* If the next token is a `{', then we're using the first
7939     production.  */
7940  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7941    {
7942      /* Consume the `{' token.  */
7943      cp_lexer_consume_token (parser->lexer);
7944      /* Parse the declarations.  */
7945      cp_parser_declaration_seq_opt (parser);
7946      /* Look for the closing `}'.  */
7947      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7948    }
7949  /* Otherwise, there's just one declaration.  */
7950  else
7951    {
7952      bool saved_in_unbraced_linkage_specification_p;
7953
7954      saved_in_unbraced_linkage_specification_p
7955	= parser->in_unbraced_linkage_specification_p;
7956      parser->in_unbraced_linkage_specification_p = true;
7957      cp_parser_declaration (parser);
7958      parser->in_unbraced_linkage_specification_p
7959	= saved_in_unbraced_linkage_specification_p;
7960    }
7961
7962  /* We're done with the linkage-specification.  */
7963  pop_lang_context ();
7964}
7965
7966/* Special member functions [gram.special] */
7967
7968/* Parse a conversion-function-id.
7969
7970   conversion-function-id:
7971     operator conversion-type-id
7972
7973   Returns an IDENTIFIER_NODE representing the operator.  */
7974
7975static tree
7976cp_parser_conversion_function_id (cp_parser* parser)
7977{
7978  tree type;
7979  tree saved_scope;
7980  tree saved_qualifying_scope;
7981  tree saved_object_scope;
7982  tree pushed_scope = NULL_TREE;
7983
7984  /* Look for the `operator' token.  */
7985  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7986    return error_mark_node;
7987  /* When we parse the conversion-type-id, the current scope will be
7988     reset.  However, we need that information in able to look up the
7989     conversion function later, so we save it here.  */
7990  saved_scope = parser->scope;
7991  saved_qualifying_scope = parser->qualifying_scope;
7992  saved_object_scope = parser->object_scope;
7993  /* We must enter the scope of the class so that the names of
7994     entities declared within the class are available in the
7995     conversion-type-id.  For example, consider:
7996
7997       struct S {
7998	 typedef int I;
7999	 operator I();
8000       };
8001
8002       S::operator I() { ... }
8003
8004     In order to see that `I' is a type-name in the definition, we
8005     must be in the scope of `S'.  */
8006  if (saved_scope)
8007    pushed_scope = push_scope (saved_scope);
8008  /* Parse the conversion-type-id.  */
8009  type = cp_parser_conversion_type_id (parser);
8010  /* Leave the scope of the class, if any.  */
8011  if (pushed_scope)
8012    pop_scope (pushed_scope);
8013  /* Restore the saved scope.  */
8014  parser->scope = saved_scope;
8015  parser->qualifying_scope = saved_qualifying_scope;
8016  parser->object_scope = saved_object_scope;
8017  /* If the TYPE is invalid, indicate failure.  */
8018  if (type == error_mark_node)
8019    return error_mark_node;
8020  return mangle_conv_op_name_for_type (type);
8021}
8022
8023/* Parse a conversion-type-id:
8024
8025   conversion-type-id:
8026     type-specifier-seq conversion-declarator [opt]
8027
8028   Returns the TYPE specified.  */
8029
8030static tree
8031cp_parser_conversion_type_id (cp_parser* parser)
8032{
8033  tree attributes;
8034  cp_decl_specifier_seq type_specifiers;
8035  cp_declarator *declarator;
8036  tree type_specified;
8037
8038  /* Parse the attributes.  */
8039  attributes = cp_parser_attributes_opt (parser);
8040  /* Parse the type-specifiers.  */
8041  cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8042				&type_specifiers);
8043  /* If that didn't work, stop.  */
8044  if (type_specifiers.type == error_mark_node)
8045    return error_mark_node;
8046  /* Parse the conversion-declarator.  */
8047  declarator = cp_parser_conversion_declarator_opt (parser);
8048
8049  type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8050				    /*initialized=*/0, &attributes);
8051  if (attributes)
8052    cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8053  return type_specified;
8054}
8055
8056/* Parse an (optional) conversion-declarator.
8057
8058   conversion-declarator:
8059     ptr-operator conversion-declarator [opt]
8060
8061   */
8062
8063static cp_declarator *
8064cp_parser_conversion_declarator_opt (cp_parser* parser)
8065{
8066  enum tree_code code;
8067  tree class_type;
8068  cp_cv_quals cv_quals;
8069
8070  /* We don't know if there's a ptr-operator next, or not.  */
8071  cp_parser_parse_tentatively (parser);
8072  /* Try the ptr-operator.  */
8073  code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8074  /* If it worked, look for more conversion-declarators.  */
8075  if (cp_parser_parse_definitely (parser))
8076    {
8077      cp_declarator *declarator;
8078
8079      /* Parse another optional declarator.  */
8080      declarator = cp_parser_conversion_declarator_opt (parser);
8081
8082      /* Create the representation of the declarator.  */
8083      if (class_type)
8084	declarator = make_ptrmem_declarator (cv_quals, class_type,
8085					     declarator);
8086      else if (code == INDIRECT_REF)
8087	declarator = make_pointer_declarator (cv_quals, declarator);
8088      else
8089	declarator = make_reference_declarator (cv_quals, declarator);
8090
8091      return declarator;
8092   }
8093
8094  return NULL;
8095}
8096
8097/* Parse an (optional) ctor-initializer.
8098
8099   ctor-initializer:
8100     : mem-initializer-list
8101
8102   Returns TRUE iff the ctor-initializer was actually present.  */
8103
8104static bool
8105cp_parser_ctor_initializer_opt (cp_parser* parser)
8106{
8107  /* If the next token is not a `:', then there is no
8108     ctor-initializer.  */
8109  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8110    {
8111      /* Do default initialization of any bases and members.  */
8112      if (DECL_CONSTRUCTOR_P (current_function_decl))
8113	finish_mem_initializers (NULL_TREE);
8114
8115      return false;
8116    }
8117
8118  /* Consume the `:' token.  */
8119  cp_lexer_consume_token (parser->lexer);
8120  /* And the mem-initializer-list.  */
8121  cp_parser_mem_initializer_list (parser);
8122
8123  return true;
8124}
8125
8126/* Parse a mem-initializer-list.
8127
8128   mem-initializer-list:
8129     mem-initializer
8130     mem-initializer , mem-initializer-list  */
8131
8132static void
8133cp_parser_mem_initializer_list (cp_parser* parser)
8134{
8135  tree mem_initializer_list = NULL_TREE;
8136
8137  /* Let the semantic analysis code know that we are starting the
8138     mem-initializer-list.  */
8139  if (!DECL_CONSTRUCTOR_P (current_function_decl))
8140    error ("only constructors take base initializers");
8141
8142  /* Loop through the list.  */
8143  while (true)
8144    {
8145      tree mem_initializer;
8146
8147      /* Parse the mem-initializer.  */
8148      mem_initializer = cp_parser_mem_initializer (parser);
8149      /* Add it to the list, unless it was erroneous.  */
8150      if (mem_initializer != error_mark_node)
8151	{
8152	  TREE_CHAIN (mem_initializer) = mem_initializer_list;
8153	  mem_initializer_list = mem_initializer;
8154	}
8155      /* If the next token is not a `,', we're done.  */
8156      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8157	break;
8158      /* Consume the `,' token.  */
8159      cp_lexer_consume_token (parser->lexer);
8160    }
8161
8162  /* Perform semantic analysis.  */
8163  if (DECL_CONSTRUCTOR_P (current_function_decl))
8164    finish_mem_initializers (mem_initializer_list);
8165}
8166
8167/* Parse a mem-initializer.
8168
8169   mem-initializer:
8170     mem-initializer-id ( expression-list [opt] )
8171
8172   GNU extension:
8173
8174   mem-initializer:
8175     ( expression-list [opt] )
8176
8177   Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8178   class) or FIELD_DECL (for a non-static data member) to initialize;
8179   the TREE_VALUE is the expression-list.  An empty initialization
8180   list is represented by void_list_node.  */
8181
8182static tree
8183cp_parser_mem_initializer (cp_parser* parser)
8184{
8185  tree mem_initializer_id;
8186  tree expression_list;
8187  tree member;
8188
8189  /* Find out what is being initialized.  */
8190  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8191    {
8192      pedwarn ("anachronistic old-style base class initializer");
8193      mem_initializer_id = NULL_TREE;
8194    }
8195  else
8196    mem_initializer_id = cp_parser_mem_initializer_id (parser);
8197  member = expand_member_init (mem_initializer_id);
8198  if (member && !DECL_P (member))
8199    in_base_initializer = 1;
8200
8201  expression_list
8202    = cp_parser_parenthesized_expression_list (parser, false,
8203					       /*cast_p=*/false,
8204					       /*non_constant_p=*/NULL);
8205  if (expression_list == error_mark_node)
8206    return error_mark_node;
8207  if (!expression_list)
8208    expression_list = void_type_node;
8209
8210  in_base_initializer = 0;
8211
8212  return member ? build_tree_list (member, expression_list) : error_mark_node;
8213}
8214
8215/* Parse a mem-initializer-id.
8216
8217   mem-initializer-id:
8218     :: [opt] nested-name-specifier [opt] class-name
8219     identifier
8220
8221   Returns a TYPE indicating the class to be initializer for the first
8222   production.  Returns an IDENTIFIER_NODE indicating the data member
8223   to be initialized for the second production.  */
8224
8225static tree
8226cp_parser_mem_initializer_id (cp_parser* parser)
8227{
8228  bool global_scope_p;
8229  bool nested_name_specifier_p;
8230  bool template_p = false;
8231  tree id;
8232
8233  /* `typename' is not allowed in this context ([temp.res]).  */
8234  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8235    {
8236      error ("keyword %<typename%> not allowed in this context (a qualified "
8237	     "member initializer is implicitly a type)");
8238      cp_lexer_consume_token (parser->lexer);
8239    }
8240  /* Look for the optional `::' operator.  */
8241  global_scope_p
8242    = (cp_parser_global_scope_opt (parser,
8243				   /*current_scope_valid_p=*/false)
8244       != NULL_TREE);
8245  /* Look for the optional nested-name-specifier.  The simplest way to
8246     implement:
8247
8248       [temp.res]
8249
8250       The keyword `typename' is not permitted in a base-specifier or
8251       mem-initializer; in these contexts a qualified name that
8252       depends on a template-parameter is implicitly assumed to be a
8253       type name.
8254
8255     is to assume that we have seen the `typename' keyword at this
8256     point.  */
8257  nested_name_specifier_p
8258    = (cp_parser_nested_name_specifier_opt (parser,
8259					    /*typename_keyword_p=*/true,
8260					    /*check_dependency_p=*/true,
8261					    /*type_p=*/true,
8262					    /*is_declaration=*/true)
8263       != NULL_TREE);
8264  if (nested_name_specifier_p)
8265    template_p = cp_parser_optional_template_keyword (parser);
8266  /* If there is a `::' operator or a nested-name-specifier, then we
8267     are definitely looking for a class-name.  */
8268  if (global_scope_p || nested_name_specifier_p)
8269    return cp_parser_class_name (parser,
8270				 /*typename_keyword_p=*/true,
8271				 /*template_keyword_p=*/template_p,
8272				 none_type,
8273				 /*check_dependency_p=*/true,
8274				 /*class_head_p=*/false,
8275				 /*is_declaration=*/true);
8276  /* Otherwise, we could also be looking for an ordinary identifier.  */
8277  cp_parser_parse_tentatively (parser);
8278  /* Try a class-name.  */
8279  id = cp_parser_class_name (parser,
8280			     /*typename_keyword_p=*/true,
8281			     /*template_keyword_p=*/false,
8282			     none_type,
8283			     /*check_dependency_p=*/true,
8284			     /*class_head_p=*/false,
8285			     /*is_declaration=*/true);
8286  /* If we found one, we're done.  */
8287  if (cp_parser_parse_definitely (parser))
8288    return id;
8289  /* Otherwise, look for an ordinary identifier.  */
8290  return cp_parser_identifier (parser);
8291}
8292
8293/* Overloading [gram.over] */
8294
8295/* Parse an operator-function-id.
8296
8297   operator-function-id:
8298     operator operator
8299
8300   Returns an IDENTIFIER_NODE for the operator which is a
8301   human-readable spelling of the identifier, e.g., `operator +'.  */
8302
8303static tree
8304cp_parser_operator_function_id (cp_parser* parser)
8305{
8306  /* Look for the `operator' keyword.  */
8307  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8308    return error_mark_node;
8309  /* And then the name of the operator itself.  */
8310  return cp_parser_operator (parser);
8311}
8312
8313/* Parse an operator.
8314
8315   operator:
8316     new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8317     += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8318     || ++ -- , ->* -> () []
8319
8320   GNU Extensions:
8321
8322   operator:
8323     <? >? <?= >?=
8324
8325   Returns an IDENTIFIER_NODE for the operator which is a
8326   human-readable spelling of the identifier, e.g., `operator +'.  */
8327
8328static tree
8329cp_parser_operator (cp_parser* parser)
8330{
8331  tree id = NULL_TREE;
8332  cp_token *token;
8333
8334  /* Peek at the next token.  */
8335  token = cp_lexer_peek_token (parser->lexer);
8336  /* Figure out which operator we have.  */
8337  switch (token->type)
8338    {
8339    case CPP_KEYWORD:
8340      {
8341	enum tree_code op;
8342
8343	/* The keyword should be either `new' or `delete'.  */
8344	if (token->keyword == RID_NEW)
8345	  op = NEW_EXPR;
8346	else if (token->keyword == RID_DELETE)
8347	  op = DELETE_EXPR;
8348	else
8349	  break;
8350
8351	/* Consume the `new' or `delete' token.  */
8352	cp_lexer_consume_token (parser->lexer);
8353
8354	/* Peek at the next token.  */
8355	token = cp_lexer_peek_token (parser->lexer);
8356	/* If it's a `[' token then this is the array variant of the
8357	   operator.  */
8358	if (token->type == CPP_OPEN_SQUARE)
8359	  {
8360	    /* Consume the `[' token.  */
8361	    cp_lexer_consume_token (parser->lexer);
8362	    /* Look for the `]' token.  */
8363	    cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8364	    id = ansi_opname (op == NEW_EXPR
8365			      ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8366	  }
8367	/* Otherwise, we have the non-array variant.  */
8368	else
8369	  id = ansi_opname (op);
8370
8371	return id;
8372      }
8373
8374    case CPP_PLUS:
8375      id = ansi_opname (PLUS_EXPR);
8376      break;
8377
8378    case CPP_MINUS:
8379      id = ansi_opname (MINUS_EXPR);
8380      break;
8381
8382    case CPP_MULT:
8383      id = ansi_opname (MULT_EXPR);
8384      break;
8385
8386    case CPP_DIV:
8387      id = ansi_opname (TRUNC_DIV_EXPR);
8388      break;
8389
8390    case CPP_MOD:
8391      id = ansi_opname (TRUNC_MOD_EXPR);
8392      break;
8393
8394    case CPP_XOR:
8395      id = ansi_opname (BIT_XOR_EXPR);
8396      break;
8397
8398    case CPP_AND:
8399      id = ansi_opname (BIT_AND_EXPR);
8400      break;
8401
8402    case CPP_OR:
8403      id = ansi_opname (BIT_IOR_EXPR);
8404      break;
8405
8406    case CPP_COMPL:
8407      id = ansi_opname (BIT_NOT_EXPR);
8408      break;
8409
8410    case CPP_NOT:
8411      id = ansi_opname (TRUTH_NOT_EXPR);
8412      break;
8413
8414    case CPP_EQ:
8415      id = ansi_assopname (NOP_EXPR);
8416      break;
8417
8418    case CPP_LESS:
8419      id = ansi_opname (LT_EXPR);
8420      break;
8421
8422    case CPP_GREATER:
8423      id = ansi_opname (GT_EXPR);
8424      break;
8425
8426    case CPP_PLUS_EQ:
8427      id = ansi_assopname (PLUS_EXPR);
8428      break;
8429
8430    case CPP_MINUS_EQ:
8431      id = ansi_assopname (MINUS_EXPR);
8432      break;
8433
8434    case CPP_MULT_EQ:
8435      id = ansi_assopname (MULT_EXPR);
8436      break;
8437
8438    case CPP_DIV_EQ:
8439      id = ansi_assopname (TRUNC_DIV_EXPR);
8440      break;
8441
8442    case CPP_MOD_EQ:
8443      id = ansi_assopname (TRUNC_MOD_EXPR);
8444      break;
8445
8446    case CPP_XOR_EQ:
8447      id = ansi_assopname (BIT_XOR_EXPR);
8448      break;
8449
8450    case CPP_AND_EQ:
8451      id = ansi_assopname (BIT_AND_EXPR);
8452      break;
8453
8454    case CPP_OR_EQ:
8455      id = ansi_assopname (BIT_IOR_EXPR);
8456      break;
8457
8458    case CPP_LSHIFT:
8459      id = ansi_opname (LSHIFT_EXPR);
8460      break;
8461
8462    case CPP_RSHIFT:
8463      id = ansi_opname (RSHIFT_EXPR);
8464      break;
8465
8466    case CPP_LSHIFT_EQ:
8467      id = ansi_assopname (LSHIFT_EXPR);
8468      break;
8469
8470    case CPP_RSHIFT_EQ:
8471      id = ansi_assopname (RSHIFT_EXPR);
8472      break;
8473
8474    case CPP_EQ_EQ:
8475      id = ansi_opname (EQ_EXPR);
8476      break;
8477
8478    case CPP_NOT_EQ:
8479      id = ansi_opname (NE_EXPR);
8480      break;
8481
8482    case CPP_LESS_EQ:
8483      id = ansi_opname (LE_EXPR);
8484      break;
8485
8486    case CPP_GREATER_EQ:
8487      id = ansi_opname (GE_EXPR);
8488      break;
8489
8490    case CPP_AND_AND:
8491      id = ansi_opname (TRUTH_ANDIF_EXPR);
8492      break;
8493
8494    case CPP_OR_OR:
8495      id = ansi_opname (TRUTH_ORIF_EXPR);
8496      break;
8497
8498    case CPP_PLUS_PLUS:
8499      id = ansi_opname (POSTINCREMENT_EXPR);
8500      break;
8501
8502    case CPP_MINUS_MINUS:
8503      id = ansi_opname (PREDECREMENT_EXPR);
8504      break;
8505
8506    case CPP_COMMA:
8507      id = ansi_opname (COMPOUND_EXPR);
8508      break;
8509
8510    case CPP_DEREF_STAR:
8511      id = ansi_opname (MEMBER_REF);
8512      break;
8513
8514    case CPP_DEREF:
8515      id = ansi_opname (COMPONENT_REF);
8516      break;
8517
8518    case CPP_OPEN_PAREN:
8519      /* Consume the `('.  */
8520      cp_lexer_consume_token (parser->lexer);
8521      /* Look for the matching `)'.  */
8522      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8523      return ansi_opname (CALL_EXPR);
8524
8525    case CPP_OPEN_SQUARE:
8526      /* Consume the `['.  */
8527      cp_lexer_consume_token (parser->lexer);
8528      /* Look for the matching `]'.  */
8529      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8530      return ansi_opname (ARRAY_REF);
8531
8532    default:
8533      /* Anything else is an error.  */
8534      break;
8535    }
8536
8537  /* If we have selected an identifier, we need to consume the
8538     operator token.  */
8539  if (id)
8540    cp_lexer_consume_token (parser->lexer);
8541  /* Otherwise, no valid operator name was present.  */
8542  else
8543    {
8544      cp_parser_error (parser, "expected operator");
8545      id = error_mark_node;
8546    }
8547
8548  return id;
8549}
8550
8551/* Parse a template-declaration.
8552
8553   template-declaration:
8554     export [opt] template < template-parameter-list > declaration
8555
8556   If MEMBER_P is TRUE, this template-declaration occurs within a
8557   class-specifier.
8558
8559   The grammar rule given by the standard isn't correct.  What
8560   is really meant is:
8561
8562   template-declaration:
8563     export [opt] template-parameter-list-seq
8564       decl-specifier-seq [opt] init-declarator [opt] ;
8565     export [opt] template-parameter-list-seq
8566       function-definition
8567
8568   template-parameter-list-seq:
8569     template-parameter-list-seq [opt]
8570     template < template-parameter-list >  */
8571
8572static void
8573cp_parser_template_declaration (cp_parser* parser, bool member_p)
8574{
8575  /* Check for `export'.  */
8576  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8577    {
8578      /* Consume the `export' token.  */
8579      cp_lexer_consume_token (parser->lexer);
8580      /* Warn that we do not support `export'.  */
8581      warning (0, "keyword %<export%> not implemented, and will be ignored");
8582    }
8583
8584  cp_parser_template_declaration_after_export (parser, member_p);
8585}
8586
8587/* Parse a template-parameter-list.
8588
8589   template-parameter-list:
8590     template-parameter
8591     template-parameter-list , template-parameter
8592
8593   Returns a TREE_LIST.  Each node represents a template parameter.
8594   The nodes are connected via their TREE_CHAINs.  */
8595
8596static tree
8597cp_parser_template_parameter_list (cp_parser* parser)
8598{
8599  tree parameter_list = NULL_TREE;
8600
8601  begin_template_parm_list ();
8602  while (true)
8603    {
8604      tree parameter;
8605      cp_token *token;
8606      bool is_non_type;
8607
8608      /* Parse the template-parameter.  */
8609      parameter = cp_parser_template_parameter (parser, &is_non_type);
8610      /* Add it to the list.  */
8611      if (parameter != error_mark_node)
8612	parameter_list = process_template_parm (parameter_list,
8613						parameter,
8614						is_non_type);
8615      else
8616       {
8617         tree err_parm = build_tree_list (parameter, parameter);
8618         TREE_VALUE (err_parm) = error_mark_node;
8619         parameter_list = chainon (parameter_list, err_parm);
8620       }
8621
8622      /* Peek at the next token.  */
8623      token = cp_lexer_peek_token (parser->lexer);
8624      /* If it's not a `,', we're done.  */
8625      if (token->type != CPP_COMMA)
8626	break;
8627      /* Otherwise, consume the `,' token.  */
8628      cp_lexer_consume_token (parser->lexer);
8629    }
8630
8631  return end_template_parm_list (parameter_list);
8632}
8633
8634/* Parse a template-parameter.
8635
8636   template-parameter:
8637     type-parameter
8638     parameter-declaration
8639
8640   If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8641   the parameter.  The TREE_PURPOSE is the default value, if any.
8642   Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8643   iff this parameter is a non-type parameter.  */
8644
8645static tree
8646cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8647{
8648  cp_token *token;
8649  cp_parameter_declarator *parameter_declarator;
8650  tree parm;
8651
8652  /* Assume it is a type parameter or a template parameter.  */
8653  *is_non_type = false;
8654  /* Peek at the next token.  */
8655  token = cp_lexer_peek_token (parser->lexer);
8656  /* If it is `class' or `template', we have a type-parameter.  */
8657  if (token->keyword == RID_TEMPLATE)
8658    return cp_parser_type_parameter (parser);
8659  /* If it is `class' or `typename' we do not know yet whether it is a
8660     type parameter or a non-type parameter.  Consider:
8661
8662       template <typename T, typename T::X X> ...
8663
8664     or:
8665
8666       template <class C, class D*> ...
8667
8668     Here, the first parameter is a type parameter, and the second is
8669     a non-type parameter.  We can tell by looking at the token after
8670     the identifier -- if it is a `,', `=', or `>' then we have a type
8671     parameter.  */
8672  if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8673    {
8674      /* Peek at the token after `class' or `typename'.  */
8675      token = cp_lexer_peek_nth_token (parser->lexer, 2);
8676      /* If it's an identifier, skip it.  */
8677      if (token->type == CPP_NAME)
8678	token = cp_lexer_peek_nth_token (parser->lexer, 3);
8679      /* Now, see if the token looks like the end of a template
8680	 parameter.  */
8681      if (token->type == CPP_COMMA
8682	  || token->type == CPP_EQ
8683	  || token->type == CPP_GREATER)
8684	return cp_parser_type_parameter (parser);
8685    }
8686
8687  /* Otherwise, it is a non-type parameter.
8688
8689     [temp.param]
8690
8691     When parsing a default template-argument for a non-type
8692     template-parameter, the first non-nested `>' is taken as the end
8693     of the template parameter-list rather than a greater-than
8694     operator.  */
8695  *is_non_type = true;
8696  parameter_declarator
8697     = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8698					/*parenthesized_p=*/NULL);
8699  parm = grokdeclarator (parameter_declarator->declarator,
8700			 &parameter_declarator->decl_specifiers,
8701			 PARM, /*initialized=*/0,
8702			 /*attrlist=*/NULL);
8703  if (parm == error_mark_node)
8704    return error_mark_node;
8705  return build_tree_list (parameter_declarator->default_argument, parm);
8706}
8707
8708/* Parse a type-parameter.
8709
8710   type-parameter:
8711     class identifier [opt]
8712     class identifier [opt] = type-id
8713     typename identifier [opt]
8714     typename identifier [opt] = type-id
8715     template < template-parameter-list > class identifier [opt]
8716     template < template-parameter-list > class identifier [opt]
8717       = id-expression
8718
8719   Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8720   TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8721   the declaration of the parameter.  */
8722
8723static tree
8724cp_parser_type_parameter (cp_parser* parser)
8725{
8726  cp_token *token;
8727  tree parameter;
8728
8729  /* Look for a keyword to tell us what kind of parameter this is.  */
8730  token = cp_parser_require (parser, CPP_KEYWORD,
8731			     "`class', `typename', or `template'");
8732  if (!token)
8733    return error_mark_node;
8734
8735  switch (token->keyword)
8736    {
8737    case RID_CLASS:
8738    case RID_TYPENAME:
8739      {
8740	tree identifier;
8741	tree default_argument;
8742
8743	/* If the next token is an identifier, then it names the
8744	   parameter.  */
8745	if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8746	  identifier = cp_parser_identifier (parser);
8747	else
8748	  identifier = NULL_TREE;
8749
8750	/* Create the parameter.  */
8751	parameter = finish_template_type_parm (class_type_node, identifier);
8752
8753	/* If the next token is an `=', we have a default argument.  */
8754	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8755	  {
8756	    /* Consume the `=' token.  */
8757	    cp_lexer_consume_token (parser->lexer);
8758	    /* Parse the default-argument.  */
8759	    push_deferring_access_checks (dk_no_deferred);
8760	    default_argument = cp_parser_type_id (parser);
8761	    pop_deferring_access_checks ();
8762	  }
8763	else
8764	  default_argument = NULL_TREE;
8765
8766	/* Create the combined representation of the parameter and the
8767	   default argument.  */
8768	parameter = build_tree_list (default_argument, parameter);
8769      }
8770      break;
8771
8772    case RID_TEMPLATE:
8773      {
8774	tree parameter_list;
8775	tree identifier;
8776	tree default_argument;
8777
8778	/* Look for the `<'.  */
8779	cp_parser_require (parser, CPP_LESS, "`<'");
8780	/* Parse the template-parameter-list.  */
8781	parameter_list = cp_parser_template_parameter_list (parser);
8782	/* Look for the `>'.  */
8783	cp_parser_require (parser, CPP_GREATER, "`>'");
8784	/* Look for the `class' keyword.  */
8785	cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8786	/* If the next token is an `=', then there is a
8787	   default-argument.  If the next token is a `>', we are at
8788	   the end of the parameter-list.  If the next token is a `,',
8789	   then we are at the end of this parameter.  */
8790	if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8791	    && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8792	    && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8793	  {
8794	    identifier = cp_parser_identifier (parser);
8795	    /* Treat invalid names as if the parameter were nameless.  */
8796	    if (identifier == error_mark_node)
8797	      identifier = NULL_TREE;
8798	  }
8799	else
8800	  identifier = NULL_TREE;
8801
8802	/* Create the template parameter.  */
8803	parameter = finish_template_template_parm (class_type_node,
8804						   identifier);
8805
8806	/* If the next token is an `=', then there is a
8807	   default-argument.  */
8808	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8809	  {
8810	    bool is_template;
8811
8812	    /* Consume the `='.  */
8813	    cp_lexer_consume_token (parser->lexer);
8814	    /* Parse the id-expression.  */
8815	    push_deferring_access_checks (dk_no_deferred);
8816	    default_argument
8817	      = cp_parser_id_expression (parser,
8818					 /*template_keyword_p=*/false,
8819					 /*check_dependency_p=*/true,
8820					 /*template_p=*/&is_template,
8821					 /*declarator_p=*/false,
8822					 /*optional_p=*/false);
8823	    if (TREE_CODE (default_argument) == TYPE_DECL)
8824	      /* If the id-expression was a template-id that refers to
8825		 a template-class, we already have the declaration here,
8826		 so no further lookup is needed.  */
8827		 ;
8828	    else
8829	      /* Look up the name.  */
8830	      default_argument
8831		= cp_parser_lookup_name (parser, default_argument,
8832					 none_type,
8833					 /*is_template=*/is_template,
8834					 /*is_namespace=*/false,
8835					 /*check_dependency=*/true,
8836					 /*ambiguous_decls=*/NULL);
8837	    /* See if the default argument is valid.  */
8838	    default_argument
8839	      = check_template_template_default_arg (default_argument);
8840	    pop_deferring_access_checks ();
8841	  }
8842	else
8843	  default_argument = NULL_TREE;
8844
8845	/* Create the combined representation of the parameter and the
8846	   default argument.  */
8847	parameter = build_tree_list (default_argument, parameter);
8848      }
8849      break;
8850
8851    default:
8852      gcc_unreachable ();
8853      break;
8854    }
8855
8856  return parameter;
8857}
8858
8859/* Parse a template-id.
8860
8861   template-id:
8862     template-name < template-argument-list [opt] >
8863
8864   If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8865   `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8866   returned.  Otherwise, if the template-name names a function, or set
8867   of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8868   names a class, returns a TYPE_DECL for the specialization.
8869
8870   If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8871   uninstantiated templates.  */
8872
8873static tree
8874cp_parser_template_id (cp_parser *parser,
8875		       bool template_keyword_p,
8876		       bool check_dependency_p,
8877		       bool is_declaration)
8878{
8879  int i;
8880  tree template;
8881  tree arguments;
8882  tree template_id;
8883  cp_token_position start_of_id = 0;
8884  deferred_access_check *chk;
8885  VEC (deferred_access_check,gc) *access_check;
8886  cp_token *next_token, *next_token_2;
8887  bool is_identifier;
8888
8889  /* If the next token corresponds to a template-id, there is no need
8890     to reparse it.  */
8891  next_token = cp_lexer_peek_token (parser->lexer);
8892  if (next_token->type == CPP_TEMPLATE_ID)
8893    {
8894      struct tree_check *check_value;
8895
8896      /* Get the stored value.  */
8897      check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
8898      /* Perform any access checks that were deferred.  */
8899      access_check = check_value->checks;
8900      if (access_check)
8901	{
8902	  for (i = 0 ;
8903	       VEC_iterate (deferred_access_check, access_check, i, chk) ;
8904	       ++i)
8905	    {
8906	      perform_or_defer_access_check (chk->binfo,
8907					     chk->decl,
8908					     chk->diag_decl);
8909	    }
8910	}
8911      /* Return the stored value.  */
8912      return check_value->value;
8913    }
8914
8915  /* Avoid performing name lookup if there is no possibility of
8916     finding a template-id.  */
8917  if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8918      || (next_token->type == CPP_NAME
8919	  && !cp_parser_nth_token_starts_template_argument_list_p
8920	       (parser, 2)))
8921    {
8922      cp_parser_error (parser, "expected template-id");
8923      return error_mark_node;
8924    }
8925
8926  /* Remember where the template-id starts.  */
8927  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8928    start_of_id = cp_lexer_token_position (parser->lexer, false);
8929
8930  push_deferring_access_checks (dk_deferred);
8931
8932  /* Parse the template-name.  */
8933  is_identifier = false;
8934  template = cp_parser_template_name (parser, template_keyword_p,
8935				      check_dependency_p,
8936				      is_declaration,
8937				      &is_identifier);
8938  if (template == error_mark_node || is_identifier)
8939    {
8940      pop_deferring_access_checks ();
8941      return template;
8942    }
8943
8944  /* If we find the sequence `[:' after a template-name, it's probably
8945     a digraph-typo for `< ::'. Substitute the tokens and check if we can
8946     parse correctly the argument list.  */
8947  next_token = cp_lexer_peek_token (parser->lexer);
8948  next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8949  if (next_token->type == CPP_OPEN_SQUARE
8950      && next_token->flags & DIGRAPH
8951      && next_token_2->type == CPP_COLON
8952      && !(next_token_2->flags & PREV_WHITE))
8953    {
8954      cp_parser_parse_tentatively (parser);
8955      /* Change `:' into `::'.  */
8956      next_token_2->type = CPP_SCOPE;
8957      /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8958	 CPP_LESS.  */
8959      cp_lexer_consume_token (parser->lexer);
8960      /* Parse the arguments.  */
8961      arguments = cp_parser_enclosed_template_argument_list (parser);
8962      if (!cp_parser_parse_definitely (parser))
8963	{
8964	  /* If we couldn't parse an argument list, then we revert our changes
8965	     and return simply an error. Maybe this is not a template-id
8966	     after all.  */
8967	  next_token_2->type = CPP_COLON;
8968	  cp_parser_error (parser, "expected %<<%>");
8969	  pop_deferring_access_checks ();
8970	  return error_mark_node;
8971	}
8972      /* Otherwise, emit an error about the invalid digraph, but continue
8973	 parsing because we got our argument list.  */
8974      pedwarn ("%<<::%> cannot begin a template-argument list");
8975      inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8976	      "between %<<%> and %<::%>");
8977      if (!flag_permissive)
8978	{
8979	  static bool hint;
8980	  if (!hint)
8981	    {
8982	      inform ("(if you use -fpermissive G++ will accept your code)");
8983	      hint = true;
8984	    }
8985	}
8986    }
8987  else
8988    {
8989      /* Look for the `<' that starts the template-argument-list.  */
8990      if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8991	{
8992	  pop_deferring_access_checks ();
8993	  return error_mark_node;
8994	}
8995      /* Parse the arguments.  */
8996      arguments = cp_parser_enclosed_template_argument_list (parser);
8997    }
8998
8999  /* Build a representation of the specialization.  */
9000  if (TREE_CODE (template) == IDENTIFIER_NODE)
9001    template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9002  else if (DECL_CLASS_TEMPLATE_P (template)
9003	   || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9004    {
9005      bool entering_scope;
9006      /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9007	 template (rather than some instantiation thereof) only if
9008	 is not nested within some other construct.  For example, in
9009	 "template <typename T> void f(T) { A<T>::", A<T> is just an
9010	 instantiation of A.  */
9011      entering_scope = (template_parm_scope_p ()
9012			&& cp_lexer_next_token_is (parser->lexer,
9013						   CPP_SCOPE));
9014      template_id
9015	= finish_template_type (template, arguments, entering_scope);
9016    }
9017  else
9018    {
9019      /* If it's not a class-template or a template-template, it should be
9020	 a function-template.  */
9021      gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9022		   || TREE_CODE (template) == OVERLOAD
9023		   || BASELINK_P (template)));
9024
9025      template_id = lookup_template_function (template, arguments);
9026    }
9027
9028  /* If parsing tentatively, replace the sequence of tokens that makes
9029     up the template-id with a CPP_TEMPLATE_ID token.  That way,
9030     should we re-parse the token stream, we will not have to repeat
9031     the effort required to do the parse, nor will we issue duplicate
9032     error messages about problems during instantiation of the
9033     template.  */
9034  if (start_of_id)
9035    {
9036      cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9037
9038      /* Reset the contents of the START_OF_ID token.  */
9039      token->type = CPP_TEMPLATE_ID;
9040      /* Retrieve any deferred checks.  Do not pop this access checks yet
9041	 so the memory will not be reclaimed during token replacing below.  */
9042      token->u.tree_check_value = GGC_CNEW (struct tree_check);
9043      token->u.tree_check_value->value = template_id;
9044      token->u.tree_check_value->checks = get_deferred_access_checks ();
9045      token->keyword = RID_MAX;
9046
9047      /* Purge all subsequent tokens.  */
9048      cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9049
9050      /* ??? Can we actually assume that, if template_id ==
9051	 error_mark_node, we will have issued a diagnostic to the
9052	 user, as opposed to simply marking the tentative parse as
9053	 failed?  */
9054      if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9055	error ("parse error in template argument list");
9056    }
9057
9058  pop_deferring_access_checks ();
9059  return template_id;
9060}
9061
9062/* Parse a template-name.
9063
9064   template-name:
9065     identifier
9066
9067   The standard should actually say:
9068
9069   template-name:
9070     identifier
9071     operator-function-id
9072
9073   A defect report has been filed about this issue.
9074
9075   A conversion-function-id cannot be a template name because they cannot
9076   be part of a template-id. In fact, looking at this code:
9077
9078   a.operator K<int>()
9079
9080   the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9081   It is impossible to call a templated conversion-function-id with an
9082   explicit argument list, since the only allowed template parameter is
9083   the type to which it is converting.
9084
9085   If TEMPLATE_KEYWORD_P is true, then we have just seen the
9086   `template' keyword, in a construction like:
9087
9088     T::template f<3>()
9089
9090   In that case `f' is taken to be a template-name, even though there
9091   is no way of knowing for sure.
9092
9093   Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9094   name refers to a set of overloaded functions, at least one of which
9095   is a template, or an IDENTIFIER_NODE with the name of the template,
9096   if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9097   names are looked up inside uninstantiated templates.  */
9098
9099static tree
9100cp_parser_template_name (cp_parser* parser,
9101			 bool template_keyword_p,
9102			 bool check_dependency_p,
9103			 bool is_declaration,
9104			 bool *is_identifier)
9105{
9106  tree identifier;
9107  tree decl;
9108  tree fns;
9109
9110  /* If the next token is `operator', then we have either an
9111     operator-function-id or a conversion-function-id.  */
9112  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9113    {
9114      /* We don't know whether we're looking at an
9115	 operator-function-id or a conversion-function-id.  */
9116      cp_parser_parse_tentatively (parser);
9117      /* Try an operator-function-id.  */
9118      identifier = cp_parser_operator_function_id (parser);
9119      /* If that didn't work, try a conversion-function-id.  */
9120      if (!cp_parser_parse_definitely (parser))
9121	{
9122	  cp_parser_error (parser, "expected template-name");
9123	  return error_mark_node;
9124	}
9125    }
9126  /* Look for the identifier.  */
9127  else
9128    identifier = cp_parser_identifier (parser);
9129
9130  /* If we didn't find an identifier, we don't have a template-id.  */
9131  if (identifier == error_mark_node)
9132    return error_mark_node;
9133
9134  /* If the name immediately followed the `template' keyword, then it
9135     is a template-name.  However, if the next token is not `<', then
9136     we do not treat it as a template-name, since it is not being used
9137     as part of a template-id.  This enables us to handle constructs
9138     like:
9139
9140       template <typename T> struct S { S(); };
9141       template <typename T> S<T>::S();
9142
9143     correctly.  We would treat `S' as a template -- if it were `S<T>'
9144     -- but we do not if there is no `<'.  */
9145
9146  if (processing_template_decl
9147      && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9148    {
9149      /* In a declaration, in a dependent context, we pretend that the
9150	 "template" keyword was present in order to improve error
9151	 recovery.  For example, given:
9152
9153	   template <typename T> void f(T::X<int>);
9154
9155	 we want to treat "X<int>" as a template-id.  */
9156      if (is_declaration
9157	  && !template_keyword_p
9158	  && parser->scope && TYPE_P (parser->scope)
9159	  && check_dependency_p
9160	  && dependent_type_p (parser->scope)
9161	  /* Do not do this for dtors (or ctors), since they never
9162	     need the template keyword before their name.  */
9163	  && !constructor_name_p (identifier, parser->scope))
9164	{
9165	  cp_token_position start = 0;
9166
9167	  /* Explain what went wrong.  */
9168	  error ("non-template %qD used as template", identifier);
9169	  inform ("use %<%T::template %D%> to indicate that it is a template",
9170		  parser->scope, identifier);
9171	  /* If parsing tentatively, find the location of the "<" token.  */
9172	  if (cp_parser_simulate_error (parser))
9173	    start = cp_lexer_token_position (parser->lexer, true);
9174	  /* Parse the template arguments so that we can issue error
9175	     messages about them.  */
9176	  cp_lexer_consume_token (parser->lexer);
9177	  cp_parser_enclosed_template_argument_list (parser);
9178	  /* Skip tokens until we find a good place from which to
9179	     continue parsing.  */
9180	  cp_parser_skip_to_closing_parenthesis (parser,
9181						 /*recovering=*/true,
9182						 /*or_comma=*/true,
9183						 /*consume_paren=*/false);
9184	  /* If parsing tentatively, permanently remove the
9185	     template argument list.  That will prevent duplicate
9186	     error messages from being issued about the missing
9187	     "template" keyword.  */
9188	  if (start)
9189	    cp_lexer_purge_tokens_after (parser->lexer, start);
9190	  if (is_identifier)
9191	    *is_identifier = true;
9192	  return identifier;
9193	}
9194
9195      /* If the "template" keyword is present, then there is generally
9196	 no point in doing name-lookup, so we just return IDENTIFIER.
9197	 But, if the qualifying scope is non-dependent then we can
9198	 (and must) do name-lookup normally.  */
9199      if (template_keyword_p
9200	  && (!parser->scope
9201	      || (TYPE_P (parser->scope)
9202		  && dependent_type_p (parser->scope))))
9203	return identifier;
9204    }
9205
9206  /* Look up the name.  */
9207  decl = cp_parser_lookup_name (parser, identifier,
9208				none_type,
9209				/*is_template=*/false,
9210				/*is_namespace=*/false,
9211				check_dependency_p,
9212				/*ambiguous_decls=*/NULL);
9213  decl = maybe_get_template_decl_from_type_decl (decl);
9214
9215  /* If DECL is a template, then the name was a template-name.  */
9216  if (TREE_CODE (decl) == TEMPLATE_DECL)
9217    ;
9218  else
9219    {
9220      tree fn = NULL_TREE;
9221
9222      /* The standard does not explicitly indicate whether a name that
9223	 names a set of overloaded declarations, some of which are
9224	 templates, is a template-name.  However, such a name should
9225	 be a template-name; otherwise, there is no way to form a
9226	 template-id for the overloaded templates.  */
9227      fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9228      if (TREE_CODE (fns) == OVERLOAD)
9229	for (fn = fns; fn; fn = OVL_NEXT (fn))
9230	  if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9231	    break;
9232
9233      if (!fn)
9234	{
9235	  /* The name does not name a template.  */
9236	  cp_parser_error (parser, "expected template-name");
9237	  return error_mark_node;
9238	}
9239    }
9240
9241  /* If DECL is dependent, and refers to a function, then just return
9242     its name; we will look it up again during template instantiation.  */
9243  if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9244    {
9245      tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9246      if (TYPE_P (scope) && dependent_type_p (scope))
9247	return identifier;
9248    }
9249
9250  return decl;
9251}
9252
9253/* Parse a template-argument-list.
9254
9255   template-argument-list:
9256     template-argument
9257     template-argument-list , template-argument
9258
9259   Returns a TREE_VEC containing the arguments.  */
9260
9261static tree
9262cp_parser_template_argument_list (cp_parser* parser)
9263{
9264  tree fixed_args[10];
9265  unsigned n_args = 0;
9266  unsigned alloced = 10;
9267  tree *arg_ary = fixed_args;
9268  tree vec;
9269  bool saved_in_template_argument_list_p;
9270  bool saved_ice_p;
9271  bool saved_non_ice_p;
9272
9273  saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9274  parser->in_template_argument_list_p = true;
9275  /* Even if the template-id appears in an integral
9276     constant-expression, the contents of the argument list do
9277     not.  */
9278  saved_ice_p = parser->integral_constant_expression_p;
9279  parser->integral_constant_expression_p = false;
9280  saved_non_ice_p = parser->non_integral_constant_expression_p;
9281  parser->non_integral_constant_expression_p = false;
9282  /* Parse the arguments.  */
9283  do
9284    {
9285      tree argument;
9286
9287      if (n_args)
9288	/* Consume the comma.  */
9289	cp_lexer_consume_token (parser->lexer);
9290
9291      /* Parse the template-argument.  */
9292      argument = cp_parser_template_argument (parser);
9293      if (n_args == alloced)
9294	{
9295	  alloced *= 2;
9296
9297	  if (arg_ary == fixed_args)
9298	    {
9299	      arg_ary = XNEWVEC (tree, alloced);
9300	      memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9301	    }
9302	  else
9303	    arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9304	}
9305      arg_ary[n_args++] = argument;
9306    }
9307  while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9308
9309  vec = make_tree_vec (n_args);
9310
9311  while (n_args--)
9312    TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9313
9314  if (arg_ary != fixed_args)
9315    free (arg_ary);
9316  parser->non_integral_constant_expression_p = saved_non_ice_p;
9317  parser->integral_constant_expression_p = saved_ice_p;
9318  parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9319  return vec;
9320}
9321
9322/* Parse a template-argument.
9323
9324   template-argument:
9325     assignment-expression
9326     type-id
9327     id-expression
9328
9329   The representation is that of an assignment-expression, type-id, or
9330   id-expression -- except that the qualified id-expression is
9331   evaluated, so that the value returned is either a DECL or an
9332   OVERLOAD.
9333
9334   Although the standard says "assignment-expression", it forbids
9335   throw-expressions or assignments in the template argument.
9336   Therefore, we use "conditional-expression" instead.  */
9337
9338static tree
9339cp_parser_template_argument (cp_parser* parser)
9340{
9341  tree argument;
9342  bool template_p;
9343  bool address_p;
9344  bool maybe_type_id = false;
9345  cp_token *token;
9346  cp_id_kind idk;
9347
9348  /* There's really no way to know what we're looking at, so we just
9349     try each alternative in order.
9350
9351       [temp.arg]
9352
9353       In a template-argument, an ambiguity between a type-id and an
9354       expression is resolved to a type-id, regardless of the form of
9355       the corresponding template-parameter.
9356
9357     Therefore, we try a type-id first.  */
9358  cp_parser_parse_tentatively (parser);
9359  argument = cp_parser_type_id (parser);
9360  /* If there was no error parsing the type-id but the next token is a '>>',
9361     we probably found a typo for '> >'. But there are type-id which are
9362     also valid expressions. For instance:
9363
9364     struct X { int operator >> (int); };
9365     template <int V> struct Foo {};
9366     Foo<X () >> 5> r;
9367
9368     Here 'X()' is a valid type-id of a function type, but the user just
9369     wanted to write the expression "X() >> 5". Thus, we remember that we
9370     found a valid type-id, but we still try to parse the argument as an
9371     expression to see what happens.  */
9372  if (!cp_parser_error_occurred (parser)
9373      && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9374    {
9375      maybe_type_id = true;
9376      cp_parser_abort_tentative_parse (parser);
9377    }
9378  else
9379    {
9380      /* If the next token isn't a `,' or a `>', then this argument wasn't
9381      really finished. This means that the argument is not a valid
9382      type-id.  */
9383      if (!cp_parser_next_token_ends_template_argument_p (parser))
9384	cp_parser_error (parser, "expected template-argument");
9385      /* If that worked, we're done.  */
9386      if (cp_parser_parse_definitely (parser))
9387	return argument;
9388    }
9389  /* We're still not sure what the argument will be.  */
9390  cp_parser_parse_tentatively (parser);
9391  /* Try a template.  */
9392  argument = cp_parser_id_expression (parser,
9393				      /*template_keyword_p=*/false,
9394				      /*check_dependency_p=*/true,
9395				      &template_p,
9396				      /*declarator_p=*/false,
9397				      /*optional_p=*/false);
9398  /* If the next token isn't a `,' or a `>', then this argument wasn't
9399     really finished.  */
9400  if (!cp_parser_next_token_ends_template_argument_p (parser))
9401    cp_parser_error (parser, "expected template-argument");
9402  if (!cp_parser_error_occurred (parser))
9403    {
9404      /* Figure out what is being referred to.  If the id-expression
9405	 was for a class template specialization, then we will have a
9406	 TYPE_DECL at this point.  There is no need to do name lookup
9407	 at this point in that case.  */
9408      if (TREE_CODE (argument) != TYPE_DECL)
9409	argument = cp_parser_lookup_name (parser, argument,
9410					  none_type,
9411					  /*is_template=*/template_p,
9412					  /*is_namespace=*/false,
9413					  /*check_dependency=*/true,
9414					  /*ambiguous_decls=*/NULL);
9415      if (TREE_CODE (argument) != TEMPLATE_DECL
9416	  && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9417	cp_parser_error (parser, "expected template-name");
9418    }
9419  if (cp_parser_parse_definitely (parser))
9420    return argument;
9421  /* It must be a non-type argument.  There permitted cases are given
9422     in [temp.arg.nontype]:
9423
9424     -- an integral constant-expression of integral or enumeration
9425	type; or
9426
9427     -- the name of a non-type template-parameter; or
9428
9429     -- the name of an object or function with external linkage...
9430
9431     -- the address of an object or function with external linkage...
9432
9433     -- a pointer to member...  */
9434  /* Look for a non-type template parameter.  */
9435  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9436    {
9437      cp_parser_parse_tentatively (parser);
9438      argument = cp_parser_primary_expression (parser,
9439					       /*adress_p=*/false,
9440					       /*cast_p=*/false,
9441					       /*template_arg_p=*/true,
9442					       &idk);
9443      if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9444	  || !cp_parser_next_token_ends_template_argument_p (parser))
9445	cp_parser_simulate_error (parser);
9446      if (cp_parser_parse_definitely (parser))
9447	return argument;
9448    }
9449
9450  /* If the next token is "&", the argument must be the address of an
9451     object or function with external linkage.  */
9452  address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9453  if (address_p)
9454    cp_lexer_consume_token (parser->lexer);
9455  /* See if we might have an id-expression.  */
9456  token = cp_lexer_peek_token (parser->lexer);
9457  if (token->type == CPP_NAME
9458      || token->keyword == RID_OPERATOR
9459      || token->type == CPP_SCOPE
9460      || token->type == CPP_TEMPLATE_ID
9461      || token->type == CPP_NESTED_NAME_SPECIFIER)
9462    {
9463      cp_parser_parse_tentatively (parser);
9464      argument = cp_parser_primary_expression (parser,
9465					       address_p,
9466					       /*cast_p=*/false,
9467					       /*template_arg_p=*/true,
9468					       &idk);
9469      if (cp_parser_error_occurred (parser)
9470	  || !cp_parser_next_token_ends_template_argument_p (parser))
9471	cp_parser_abort_tentative_parse (parser);
9472      else
9473	{
9474	  if (TREE_CODE (argument) == INDIRECT_REF)
9475	    {
9476	      gcc_assert (REFERENCE_REF_P (argument));
9477	      argument = TREE_OPERAND (argument, 0);
9478	    }
9479
9480	  if (TREE_CODE (argument) == VAR_DECL)
9481	    {
9482	      /* A variable without external linkage might still be a
9483		 valid constant-expression, so no error is issued here
9484		 if the external-linkage check fails.  */
9485	      if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9486		cp_parser_simulate_error (parser);
9487	    }
9488	  else if (is_overloaded_fn (argument))
9489	    /* All overloaded functions are allowed; if the external
9490	       linkage test does not pass, an error will be issued
9491	       later.  */
9492	    ;
9493	  else if (address_p
9494		   && (TREE_CODE (argument) == OFFSET_REF
9495		       || TREE_CODE (argument) == SCOPE_REF))
9496	    /* A pointer-to-member.  */
9497	    ;
9498	  else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9499	    ;
9500	  else
9501	    cp_parser_simulate_error (parser);
9502
9503	  if (cp_parser_parse_definitely (parser))
9504	    {
9505	      if (address_p)
9506		argument = build_x_unary_op (ADDR_EXPR, argument);
9507	      return argument;
9508	    }
9509	}
9510    }
9511  /* If the argument started with "&", there are no other valid
9512     alternatives at this point.  */
9513  if (address_p)
9514    {
9515      cp_parser_error (parser, "invalid non-type template argument");
9516      return error_mark_node;
9517    }
9518
9519  /* If the argument wasn't successfully parsed as a type-id followed
9520     by '>>', the argument can only be a constant expression now.
9521     Otherwise, we try parsing the constant-expression tentatively,
9522     because the argument could really be a type-id.  */
9523  if (maybe_type_id)
9524    cp_parser_parse_tentatively (parser);
9525  argument = cp_parser_constant_expression (parser,
9526					    /*allow_non_constant_p=*/false,
9527					    /*non_constant_p=*/NULL);
9528  argument = fold_non_dependent_expr (argument);
9529  if (!maybe_type_id)
9530    return argument;
9531  if (!cp_parser_next_token_ends_template_argument_p (parser))
9532    cp_parser_error (parser, "expected template-argument");
9533  if (cp_parser_parse_definitely (parser))
9534    return argument;
9535  /* We did our best to parse the argument as a non type-id, but that
9536     was the only alternative that matched (albeit with a '>' after
9537     it). We can assume it's just a typo from the user, and a
9538     diagnostic will then be issued.  */
9539  return cp_parser_type_id (parser);
9540}
9541
9542/* Parse an explicit-instantiation.
9543
9544   explicit-instantiation:
9545     template declaration
9546
9547   Although the standard says `declaration', what it really means is:
9548
9549   explicit-instantiation:
9550     template decl-specifier-seq [opt] declarator [opt] ;
9551
9552   Things like `template int S<int>::i = 5, int S<double>::j;' are not
9553   supposed to be allowed.  A defect report has been filed about this
9554   issue.
9555
9556   GNU Extension:
9557
9558   explicit-instantiation:
9559     storage-class-specifier template
9560       decl-specifier-seq [opt] declarator [opt] ;
9561     function-specifier template
9562       decl-specifier-seq [opt] declarator [opt] ;  */
9563
9564static void
9565cp_parser_explicit_instantiation (cp_parser* parser)
9566{
9567  int declares_class_or_enum;
9568  cp_decl_specifier_seq decl_specifiers;
9569  tree extension_specifier = NULL_TREE;
9570
9571  /* Look for an (optional) storage-class-specifier or
9572     function-specifier.  */
9573  if (cp_parser_allow_gnu_extensions_p (parser))
9574    {
9575      extension_specifier
9576	= cp_parser_storage_class_specifier_opt (parser);
9577      if (!extension_specifier)
9578	extension_specifier
9579	  = cp_parser_function_specifier_opt (parser,
9580					      /*decl_specs=*/NULL);
9581    }
9582
9583  /* Look for the `template' keyword.  */
9584  cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9585  /* Let the front end know that we are processing an explicit
9586     instantiation.  */
9587  begin_explicit_instantiation ();
9588  /* [temp.explicit] says that we are supposed to ignore access
9589     control while processing explicit instantiation directives.  */
9590  push_deferring_access_checks (dk_no_check);
9591  /* Parse a decl-specifier-seq.  */
9592  cp_parser_decl_specifier_seq (parser,
9593				CP_PARSER_FLAGS_OPTIONAL,
9594				&decl_specifiers,
9595				&declares_class_or_enum);
9596  /* If there was exactly one decl-specifier, and it declared a class,
9597     and there's no declarator, then we have an explicit type
9598     instantiation.  */
9599  if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9600    {
9601      tree type;
9602
9603      type = check_tag_decl (&decl_specifiers);
9604      /* Turn access control back on for names used during
9605	 template instantiation.  */
9606      pop_deferring_access_checks ();
9607      if (type)
9608	do_type_instantiation (type, extension_specifier,
9609			       /*complain=*/tf_error);
9610    }
9611  else
9612    {
9613      cp_declarator *declarator;
9614      tree decl;
9615
9616      /* Parse the declarator.  */
9617      declarator
9618	= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9619				/*ctor_dtor_or_conv_p=*/NULL,
9620				/*parenthesized_p=*/NULL,
9621				/*member_p=*/false);
9622      if (declares_class_or_enum & 2)
9623	cp_parser_check_for_definition_in_return_type (declarator,
9624						       decl_specifiers.type);
9625      if (declarator != cp_error_declarator)
9626	{
9627	  decl = grokdeclarator (declarator, &decl_specifiers,
9628				 NORMAL, 0, &decl_specifiers.attributes);
9629	  /* Turn access control back on for names used during
9630	     template instantiation.  */
9631	  pop_deferring_access_checks ();
9632	  /* Do the explicit instantiation.  */
9633	  do_decl_instantiation (decl, extension_specifier);
9634	}
9635      else
9636	{
9637	  pop_deferring_access_checks ();
9638	  /* Skip the body of the explicit instantiation.  */
9639	  cp_parser_skip_to_end_of_statement (parser);
9640	}
9641    }
9642  /* We're done with the instantiation.  */
9643  end_explicit_instantiation ();
9644
9645  cp_parser_consume_semicolon_at_end_of_statement (parser);
9646}
9647
9648/* Parse an explicit-specialization.
9649
9650   explicit-specialization:
9651     template < > declaration
9652
9653   Although the standard says `declaration', what it really means is:
9654
9655   explicit-specialization:
9656     template <> decl-specifier [opt] init-declarator [opt] ;
9657     template <> function-definition
9658     template <> explicit-specialization
9659     template <> template-declaration  */
9660
9661static void
9662cp_parser_explicit_specialization (cp_parser* parser)
9663{
9664  bool need_lang_pop;
9665  /* Look for the `template' keyword.  */
9666  cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9667  /* Look for the `<'.  */
9668  cp_parser_require (parser, CPP_LESS, "`<'");
9669  /* Look for the `>'.  */
9670  cp_parser_require (parser, CPP_GREATER, "`>'");
9671  /* We have processed another parameter list.  */
9672  ++parser->num_template_parameter_lists;
9673  /* [temp]
9674
9675     A template ... explicit specialization ... shall not have C
9676     linkage.  */
9677  if (current_lang_name == lang_name_c)
9678    {
9679      error ("template specialization with C linkage");
9680      /* Give it C++ linkage to avoid confusing other parts of the
9681	 front end.  */
9682      push_lang_context (lang_name_cplusplus);
9683      need_lang_pop = true;
9684    }
9685  else
9686    need_lang_pop = false;
9687  /* Let the front end know that we are beginning a specialization.  */
9688  if (!begin_specialization ())
9689    {
9690      end_specialization ();
9691      cp_parser_skip_to_end_of_block_or_statement (parser);
9692      return;
9693    }
9694
9695  /* If the next keyword is `template', we need to figure out whether
9696     or not we're looking a template-declaration.  */
9697  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9698    {
9699      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9700	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9701	cp_parser_template_declaration_after_export (parser,
9702						     /*member_p=*/false);
9703      else
9704	cp_parser_explicit_specialization (parser);
9705    }
9706  else
9707    /* Parse the dependent declaration.  */
9708    cp_parser_single_declaration (parser,
9709				  /*checks=*/NULL,
9710				  /*member_p=*/false,
9711				  /*friend_p=*/NULL);
9712  /* We're done with the specialization.  */
9713  end_specialization ();
9714  /* For the erroneous case of a template with C linkage, we pushed an
9715     implicit C++ linkage scope; exit that scope now.  */
9716  if (need_lang_pop)
9717    pop_lang_context ();
9718  /* We're done with this parameter list.  */
9719  --parser->num_template_parameter_lists;
9720}
9721
9722/* Parse a type-specifier.
9723
9724   type-specifier:
9725     simple-type-specifier
9726     class-specifier
9727     enum-specifier
9728     elaborated-type-specifier
9729     cv-qualifier
9730
9731   GNU Extension:
9732
9733   type-specifier:
9734     __complex__
9735
9736   Returns a representation of the type-specifier.  For a
9737   class-specifier, enum-specifier, or elaborated-type-specifier, a
9738   TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9739
9740   The parser flags FLAGS is used to control type-specifier parsing.
9741
9742   If IS_DECLARATION is TRUE, then this type-specifier is appearing
9743   in a decl-specifier-seq.
9744
9745   If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9746   class-specifier, enum-specifier, or elaborated-type-specifier, then
9747   *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9748   if a type is declared; 2 if it is defined.  Otherwise, it is set to
9749   zero.
9750
9751   If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9752   cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9753   is set to FALSE.  */
9754
9755static tree
9756cp_parser_type_specifier (cp_parser* parser,
9757			  cp_parser_flags flags,
9758			  cp_decl_specifier_seq *decl_specs,
9759			  bool is_declaration,
9760			  int* declares_class_or_enum,
9761			  bool* is_cv_qualifier)
9762{
9763  tree type_spec = NULL_TREE;
9764  cp_token *token;
9765  enum rid keyword;
9766  cp_decl_spec ds = ds_last;
9767
9768  /* Assume this type-specifier does not declare a new type.  */
9769  if (declares_class_or_enum)
9770    *declares_class_or_enum = 0;
9771  /* And that it does not specify a cv-qualifier.  */
9772  if (is_cv_qualifier)
9773    *is_cv_qualifier = false;
9774  /* Peek at the next token.  */
9775  token = cp_lexer_peek_token (parser->lexer);
9776
9777  /* If we're looking at a keyword, we can use that to guide the
9778     production we choose.  */
9779  keyword = token->keyword;
9780  switch (keyword)
9781    {
9782    case RID_ENUM:
9783      /* Look for the enum-specifier.  */
9784      type_spec = cp_parser_enum_specifier (parser);
9785      /* If that worked, we're done.  */
9786      if (type_spec)
9787	{
9788	  if (declares_class_or_enum)
9789	    *declares_class_or_enum = 2;
9790	  if (decl_specs)
9791	    cp_parser_set_decl_spec_type (decl_specs,
9792					  type_spec,
9793					  /*user_defined_p=*/true);
9794	  return type_spec;
9795	}
9796      else
9797	goto elaborated_type_specifier;
9798
9799      /* Any of these indicate either a class-specifier, or an
9800	 elaborated-type-specifier.  */
9801    case RID_CLASS:
9802    case RID_STRUCT:
9803    case RID_UNION:
9804      /* Parse tentatively so that we can back up if we don't find a
9805	 class-specifier.  */
9806      cp_parser_parse_tentatively (parser);
9807      /* Look for the class-specifier.  */
9808      type_spec = cp_parser_class_specifier (parser);
9809      /* If that worked, we're done.  */
9810      if (cp_parser_parse_definitely (parser))
9811	{
9812	  if (declares_class_or_enum)
9813	    *declares_class_or_enum = 2;
9814	  if (decl_specs)
9815	    cp_parser_set_decl_spec_type (decl_specs,
9816					  type_spec,
9817					  /*user_defined_p=*/true);
9818	  return type_spec;
9819	}
9820
9821      /* Fall through.  */
9822    elaborated_type_specifier:
9823      /* We're declaring (not defining) a class or enum.  */
9824      if (declares_class_or_enum)
9825	*declares_class_or_enum = 1;
9826
9827      /* Fall through.  */
9828    case RID_TYPENAME:
9829      /* Look for an elaborated-type-specifier.  */
9830      type_spec
9831	= (cp_parser_elaborated_type_specifier
9832	   (parser,
9833	    decl_specs && decl_specs->specs[(int) ds_friend],
9834	    is_declaration));
9835      if (decl_specs)
9836	cp_parser_set_decl_spec_type (decl_specs,
9837				      type_spec,
9838				      /*user_defined_p=*/true);
9839      return type_spec;
9840
9841    case RID_CONST:
9842      ds = ds_const;
9843      if (is_cv_qualifier)
9844	*is_cv_qualifier = true;
9845      break;
9846
9847    case RID_VOLATILE:
9848      ds = ds_volatile;
9849      if (is_cv_qualifier)
9850	*is_cv_qualifier = true;
9851      break;
9852
9853    case RID_RESTRICT:
9854      ds = ds_restrict;
9855      if (is_cv_qualifier)
9856	*is_cv_qualifier = true;
9857      break;
9858
9859    case RID_COMPLEX:
9860      /* The `__complex__' keyword is a GNU extension.  */
9861      ds = ds_complex;
9862      break;
9863
9864    default:
9865      break;
9866    }
9867
9868  /* Handle simple keywords.  */
9869  if (ds != ds_last)
9870    {
9871      if (decl_specs)
9872	{
9873	  ++decl_specs->specs[(int)ds];
9874	  decl_specs->any_specifiers_p = true;
9875	}
9876      return cp_lexer_consume_token (parser->lexer)->u.value;
9877    }
9878
9879  /* If we do not already have a type-specifier, assume we are looking
9880     at a simple-type-specifier.  */
9881  type_spec = cp_parser_simple_type_specifier (parser,
9882					       decl_specs,
9883					       flags);
9884
9885  /* If we didn't find a type-specifier, and a type-specifier was not
9886     optional in this context, issue an error message.  */
9887  if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9888    {
9889      cp_parser_error (parser, "expected type specifier");
9890      return error_mark_node;
9891    }
9892
9893  return type_spec;
9894}
9895
9896/* Parse a simple-type-specifier.
9897
9898   simple-type-specifier:
9899     :: [opt] nested-name-specifier [opt] type-name
9900     :: [opt] nested-name-specifier template template-id
9901     char
9902     wchar_t
9903     bool
9904     short
9905     int
9906     long
9907     signed
9908     unsigned
9909     float
9910     double
9911     void
9912
9913   GNU Extension:
9914
9915   simple-type-specifier:
9916     __typeof__ unary-expression
9917     __typeof__ ( type-id )
9918
9919   Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9920   appropriately updated.  */
9921
9922static tree
9923cp_parser_simple_type_specifier (cp_parser* parser,
9924				 cp_decl_specifier_seq *decl_specs,
9925				 cp_parser_flags flags)
9926{
9927  tree type = NULL_TREE;
9928  cp_token *token;
9929
9930  /* Peek at the next token.  */
9931  token = cp_lexer_peek_token (parser->lexer);
9932
9933  /* If we're looking at a keyword, things are easy.  */
9934  switch (token->keyword)
9935    {
9936    case RID_CHAR:
9937      if (decl_specs)
9938	decl_specs->explicit_char_p = true;
9939      type = char_type_node;
9940      break;
9941    case RID_WCHAR:
9942      type = wchar_type_node;
9943      break;
9944    case RID_BOOL:
9945      type = boolean_type_node;
9946      break;
9947    case RID_SHORT:
9948      if (decl_specs)
9949	++decl_specs->specs[(int) ds_short];
9950      type = short_integer_type_node;
9951      break;
9952    case RID_INT:
9953      if (decl_specs)
9954	decl_specs->explicit_int_p = true;
9955      type = integer_type_node;
9956      break;
9957    case RID_LONG:
9958      if (decl_specs)
9959	++decl_specs->specs[(int) ds_long];
9960      type = long_integer_type_node;
9961      break;
9962    case RID_SIGNED:
9963      if (decl_specs)
9964	++decl_specs->specs[(int) ds_signed];
9965      type = integer_type_node;
9966      break;
9967    case RID_UNSIGNED:
9968      if (decl_specs)
9969	++decl_specs->specs[(int) ds_unsigned];
9970      type = unsigned_type_node;
9971      break;
9972    case RID_FLOAT:
9973      type = float_type_node;
9974      break;
9975    case RID_DOUBLE:
9976      type = double_type_node;
9977      break;
9978    case RID_VOID:
9979      type = void_type_node;
9980      break;
9981
9982    case RID_TYPEOF:
9983      /* Consume the `typeof' token.  */
9984      cp_lexer_consume_token (parser->lexer);
9985      /* Parse the operand to `typeof'.  */
9986      type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9987      /* If it is not already a TYPE, take its type.  */
9988      if (!TYPE_P (type))
9989	type = finish_typeof (type);
9990
9991      if (decl_specs)
9992	cp_parser_set_decl_spec_type (decl_specs, type,
9993				      /*user_defined_p=*/true);
9994
9995      return type;
9996
9997    default:
9998      break;
9999    }
10000
10001  /* If the type-specifier was for a built-in type, we're done.  */
10002  if (type)
10003    {
10004      tree id;
10005
10006      /* Record the type.  */
10007      if (decl_specs
10008	  && (token->keyword != RID_SIGNED
10009	      && token->keyword != RID_UNSIGNED
10010	      && token->keyword != RID_SHORT
10011	      && token->keyword != RID_LONG))
10012	cp_parser_set_decl_spec_type (decl_specs,
10013				      type,
10014				      /*user_defined=*/false);
10015      if (decl_specs)
10016	decl_specs->any_specifiers_p = true;
10017
10018      /* Consume the token.  */
10019      id = cp_lexer_consume_token (parser->lexer)->u.value;
10020
10021      /* There is no valid C++ program where a non-template type is
10022	 followed by a "<".  That usually indicates that the user thought
10023	 that the type was a template.  */
10024      cp_parser_check_for_invalid_template_id (parser, type);
10025
10026      return TYPE_NAME (type);
10027    }
10028
10029  /* The type-specifier must be a user-defined type.  */
10030  if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10031    {
10032      bool qualified_p;
10033      bool global_p;
10034
10035      /* Don't gobble tokens or issue error messages if this is an
10036	 optional type-specifier.  */
10037      if (flags & CP_PARSER_FLAGS_OPTIONAL)
10038	cp_parser_parse_tentatively (parser);
10039
10040      /* Look for the optional `::' operator.  */
10041      global_p
10042	= (cp_parser_global_scope_opt (parser,
10043				       /*current_scope_valid_p=*/false)
10044	   != NULL_TREE);
10045      /* Look for the nested-name specifier.  */
10046      qualified_p
10047	= (cp_parser_nested_name_specifier_opt (parser,
10048						/*typename_keyword_p=*/false,
10049						/*check_dependency_p=*/true,
10050						/*type_p=*/false,
10051						/*is_declaration=*/false)
10052	   != NULL_TREE);
10053      /* If we have seen a nested-name-specifier, and the next token
10054	 is `template', then we are using the template-id production.  */
10055      if (parser->scope
10056	  && cp_parser_optional_template_keyword (parser))
10057	{
10058	  /* Look for the template-id.  */
10059	  type = cp_parser_template_id (parser,
10060					/*template_keyword_p=*/true,
10061					/*check_dependency_p=*/true,
10062					/*is_declaration=*/false);
10063	  /* If the template-id did not name a type, we are out of
10064	     luck.  */
10065	  if (TREE_CODE (type) != TYPE_DECL)
10066	    {
10067	      cp_parser_error (parser, "expected template-id for type");
10068	      type = NULL_TREE;
10069	    }
10070	}
10071      /* Otherwise, look for a type-name.  */
10072      else
10073	type = cp_parser_type_name (parser);
10074      /* Keep track of all name-lookups performed in class scopes.  */
10075      if (type
10076	  && !global_p
10077	  && !qualified_p
10078	  && TREE_CODE (type) == TYPE_DECL
10079	  && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10080	maybe_note_name_used_in_class (DECL_NAME (type), type);
10081      /* If it didn't work out, we don't have a TYPE.  */
10082      if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10083	  && !cp_parser_parse_definitely (parser))
10084	type = NULL_TREE;
10085      if (type && decl_specs)
10086	cp_parser_set_decl_spec_type (decl_specs, type,
10087				      /*user_defined=*/true);
10088    }
10089
10090  /* If we didn't get a type-name, issue an error message.  */
10091  if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10092    {
10093      cp_parser_error (parser, "expected type-name");
10094      return error_mark_node;
10095    }
10096
10097  /* There is no valid C++ program where a non-template type is
10098     followed by a "<".  That usually indicates that the user thought
10099     that the type was a template.  */
10100  if (type && type != error_mark_node)
10101    {
10102      /* As a last-ditch effort, see if TYPE is an Objective-C type.
10103	 If it is, then the '<'...'>' enclose protocol names rather than
10104	 template arguments, and so everything is fine.  */
10105      if (c_dialect_objc ()
10106	  && (objc_is_id (type) || objc_is_class_name (type)))
10107	{
10108	  tree protos = cp_parser_objc_protocol_refs_opt (parser);
10109	  tree qual_type = objc_get_protocol_qualified_type (type, protos);
10110
10111	  /* Clobber the "unqualified" type previously entered into
10112	     DECL_SPECS with the new, improved protocol-qualified version.  */
10113	  if (decl_specs)
10114	    decl_specs->type = qual_type;
10115
10116	  return qual_type;
10117	}
10118
10119      cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10120    }
10121
10122  return type;
10123}
10124
10125/* Parse a type-name.
10126
10127   type-name:
10128     class-name
10129     enum-name
10130     typedef-name
10131
10132   enum-name:
10133     identifier
10134
10135   typedef-name:
10136     identifier
10137
10138   Returns a TYPE_DECL for the type.  */
10139
10140static tree
10141cp_parser_type_name (cp_parser* parser)
10142{
10143  tree type_decl;
10144  tree identifier;
10145
10146  /* We can't know yet whether it is a class-name or not.  */
10147  cp_parser_parse_tentatively (parser);
10148  /* Try a class-name.  */
10149  type_decl = cp_parser_class_name (parser,
10150				    /*typename_keyword_p=*/false,
10151				    /*template_keyword_p=*/false,
10152				    none_type,
10153				    /*check_dependency_p=*/true,
10154				    /*class_head_p=*/false,
10155				    /*is_declaration=*/false);
10156  /* If it's not a class-name, keep looking.  */
10157  if (!cp_parser_parse_definitely (parser))
10158    {
10159      /* It must be a typedef-name or an enum-name.  */
10160      identifier = cp_parser_identifier (parser);
10161      if (identifier == error_mark_node)
10162	return error_mark_node;
10163
10164      /* Look up the type-name.  */
10165      type_decl = cp_parser_lookup_name_simple (parser, identifier);
10166
10167      if (TREE_CODE (type_decl) != TYPE_DECL
10168	  && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10169	{
10170	  /* See if this is an Objective-C type.  */
10171	  tree protos = cp_parser_objc_protocol_refs_opt (parser);
10172	  tree type = objc_get_protocol_qualified_type (identifier, protos);
10173	  if (type)
10174	    type_decl = TYPE_NAME (type);
10175	}
10176
10177      /* Issue an error if we did not find a type-name.  */
10178      if (TREE_CODE (type_decl) != TYPE_DECL)
10179	{
10180	  if (!cp_parser_simulate_error (parser))
10181	    cp_parser_name_lookup_error (parser, identifier, type_decl,
10182					 "is not a type");
10183	  type_decl = error_mark_node;
10184	}
10185      /* Remember that the name was used in the definition of the
10186	 current class so that we can check later to see if the
10187	 meaning would have been different after the class was
10188	 entirely defined.  */
10189      else if (type_decl != error_mark_node
10190	       && !parser->scope)
10191	maybe_note_name_used_in_class (identifier, type_decl);
10192    }
10193
10194  return type_decl;
10195}
10196
10197
10198/* Parse an elaborated-type-specifier.  Note that the grammar given
10199   here incorporates the resolution to DR68.
10200
10201   elaborated-type-specifier:
10202     class-key :: [opt] nested-name-specifier [opt] identifier
10203     class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10204     enum :: [opt] nested-name-specifier [opt] identifier
10205     typename :: [opt] nested-name-specifier identifier
10206     typename :: [opt] nested-name-specifier template [opt]
10207       template-id
10208
10209   GNU extension:
10210
10211   elaborated-type-specifier:
10212     class-key attributes :: [opt] nested-name-specifier [opt] identifier
10213     class-key attributes :: [opt] nested-name-specifier [opt]
10214	       template [opt] template-id
10215     enum attributes :: [opt] nested-name-specifier [opt] identifier
10216
10217   If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10218   declared `friend'.  If IS_DECLARATION is TRUE, then this
10219   elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10220   something is being declared.
10221
10222   Returns the TYPE specified.  */
10223
10224static tree
10225cp_parser_elaborated_type_specifier (cp_parser* parser,
10226				     bool is_friend,
10227				     bool is_declaration)
10228{
10229  enum tag_types tag_type;
10230  tree identifier;
10231  tree type = NULL_TREE;
10232  tree attributes = NULL_TREE;
10233
10234  /* See if we're looking at the `enum' keyword.  */
10235  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10236    {
10237      /* Consume the `enum' token.  */
10238      cp_lexer_consume_token (parser->lexer);
10239      /* Remember that it's an enumeration type.  */
10240      tag_type = enum_type;
10241      /* Parse the attributes.  */
10242      attributes = cp_parser_attributes_opt (parser);
10243    }
10244  /* Or, it might be `typename'.  */
10245  else if (cp_lexer_next_token_is_keyword (parser->lexer,
10246					   RID_TYPENAME))
10247    {
10248      /* Consume the `typename' token.  */
10249      cp_lexer_consume_token (parser->lexer);
10250      /* Remember that it's a `typename' type.  */
10251      tag_type = typename_type;
10252      /* The `typename' keyword is only allowed in templates.  */
10253      if (!processing_template_decl)
10254	pedwarn ("using %<typename%> outside of template");
10255    }
10256  /* Otherwise it must be a class-key.  */
10257  else
10258    {
10259      tag_type = cp_parser_class_key (parser);
10260      if (tag_type == none_type)
10261	return error_mark_node;
10262      /* Parse the attributes.  */
10263      attributes = cp_parser_attributes_opt (parser);
10264    }
10265
10266  /* Look for the `::' operator.  */
10267  cp_parser_global_scope_opt (parser,
10268			      /*current_scope_valid_p=*/false);
10269  /* Look for the nested-name-specifier.  */
10270  if (tag_type == typename_type)
10271    {
10272      if (!cp_parser_nested_name_specifier (parser,
10273					   /*typename_keyword_p=*/true,
10274					   /*check_dependency_p=*/true,
10275					   /*type_p=*/true,
10276					    is_declaration))
10277	return error_mark_node;
10278    }
10279  else
10280    /* Even though `typename' is not present, the proposed resolution
10281       to Core Issue 180 says that in `class A<T>::B', `B' should be
10282       considered a type-name, even if `A<T>' is dependent.  */
10283    cp_parser_nested_name_specifier_opt (parser,
10284					 /*typename_keyword_p=*/true,
10285					 /*check_dependency_p=*/true,
10286					 /*type_p=*/true,
10287					 is_declaration);
10288  /* For everything but enumeration types, consider a template-id.
10289     For an enumeration type, consider only a plain identifier.  */
10290  if (tag_type != enum_type)
10291    {
10292      bool template_p = false;
10293      tree decl;
10294
10295      /* Allow the `template' keyword.  */
10296      template_p = cp_parser_optional_template_keyword (parser);
10297      /* If we didn't see `template', we don't know if there's a
10298	 template-id or not.  */
10299      if (!template_p)
10300	cp_parser_parse_tentatively (parser);
10301      /* Parse the template-id.  */
10302      decl = cp_parser_template_id (parser, template_p,
10303				    /*check_dependency_p=*/true,
10304				    is_declaration);
10305      /* If we didn't find a template-id, look for an ordinary
10306	 identifier.  */
10307      if (!template_p && !cp_parser_parse_definitely (parser))
10308	;
10309      /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10310	 in effect, then we must assume that, upon instantiation, the
10311	 template will correspond to a class.  */
10312      else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10313	       && tag_type == typename_type)
10314	type = make_typename_type (parser->scope, decl,
10315				   typename_type,
10316				   /*complain=*/tf_error);
10317      else
10318	type = TREE_TYPE (decl);
10319    }
10320
10321  if (!type)
10322    {
10323      identifier = cp_parser_identifier (parser);
10324
10325      if (identifier == error_mark_node)
10326	{
10327	  parser->scope = NULL_TREE;
10328	  return error_mark_node;
10329	}
10330
10331      /* For a `typename', we needn't call xref_tag.  */
10332      if (tag_type == typename_type
10333	  && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10334	return cp_parser_make_typename_type (parser, parser->scope,
10335					     identifier);
10336      /* Look up a qualified name in the usual way.  */
10337      if (parser->scope)
10338	{
10339	  tree decl;
10340
10341	  decl = cp_parser_lookup_name (parser, identifier,
10342					tag_type,
10343					/*is_template=*/false,
10344					/*is_namespace=*/false,
10345					/*check_dependency=*/true,
10346					/*ambiguous_decls=*/NULL);
10347
10348	  /* If we are parsing friend declaration, DECL may be a
10349	     TEMPLATE_DECL tree node here.  However, we need to check
10350	     whether this TEMPLATE_DECL results in valid code.  Consider
10351	     the following example:
10352
10353	       namespace N {
10354		 template <class T> class C {};
10355	       }
10356	       class X {
10357		 template <class T> friend class N::C; // #1, valid code
10358	       };
10359	       template <class T> class Y {
10360		 friend class N::C;		       // #2, invalid code
10361	       };
10362
10363	     For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10364	     name lookup of `N::C'.  We see that friend declaration must
10365	     be template for the code to be valid.  Note that
10366	     processing_template_decl does not work here since it is
10367	     always 1 for the above two cases.  */
10368
10369	  decl = (cp_parser_maybe_treat_template_as_class
10370		  (decl, /*tag_name_p=*/is_friend
10371			 && parser->num_template_parameter_lists));
10372
10373	  if (TREE_CODE (decl) != TYPE_DECL)
10374	    {
10375	      cp_parser_diagnose_invalid_type_name (parser,
10376						    parser->scope,
10377						    identifier);
10378	      return error_mark_node;
10379	    }
10380
10381	  if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10382            {
10383              bool allow_template = (parser->num_template_parameter_lists
10384		                      || DECL_SELF_REFERENCE_P (decl));
10385              type = check_elaborated_type_specifier (tag_type, decl,
10386                                                      allow_template);
10387
10388              if (type == error_mark_node)
10389                return error_mark_node;
10390            }
10391
10392	  type = TREE_TYPE (decl);
10393	}
10394      else
10395	{
10396	  /* An elaborated-type-specifier sometimes introduces a new type and
10397	     sometimes names an existing type.  Normally, the rule is that it
10398	     introduces a new type only if there is not an existing type of
10399	     the same name already in scope.  For example, given:
10400
10401	       struct S {};
10402	       void f() { struct S s; }
10403
10404	     the `struct S' in the body of `f' is the same `struct S' as in
10405	     the global scope; the existing definition is used.  However, if
10406	     there were no global declaration, this would introduce a new
10407	     local class named `S'.
10408
10409	     An exception to this rule applies to the following code:
10410
10411	       namespace N { struct S; }
10412
10413	     Here, the elaborated-type-specifier names a new type
10414	     unconditionally; even if there is already an `S' in the
10415	     containing scope this declaration names a new type.
10416	     This exception only applies if the elaborated-type-specifier
10417	     forms the complete declaration:
10418
10419	       [class.name]
10420
10421	       A declaration consisting solely of `class-key identifier ;' is
10422	       either a redeclaration of the name in the current scope or a
10423	       forward declaration of the identifier as a class name.  It
10424	       introduces the name into the current scope.
10425
10426	     We are in this situation precisely when the next token is a `;'.
10427
10428	     An exception to the exception is that a `friend' declaration does
10429	     *not* name a new type; i.e., given:
10430
10431	       struct S { friend struct T; };
10432
10433	     `T' is not a new type in the scope of `S'.
10434
10435	     Also, `new struct S' or `sizeof (struct S)' never results in the
10436	     definition of a new type; a new type can only be declared in a
10437	     declaration context.  */
10438
10439	  tag_scope ts;
10440	  bool template_p;
10441
10442	  if (is_friend)
10443	    /* Friends have special name lookup rules.  */
10444	    ts = ts_within_enclosing_non_class;
10445	  else if (is_declaration
10446		   && cp_lexer_next_token_is (parser->lexer,
10447					      CPP_SEMICOLON))
10448	    /* This is a `class-key identifier ;' */
10449	    ts = ts_current;
10450	  else
10451	    ts = ts_global;
10452
10453	  template_p =
10454	    (parser->num_template_parameter_lists
10455	     && (cp_parser_next_token_starts_class_definition_p (parser)
10456		 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10457	  /* An unqualified name was used to reference this type, so
10458	     there were no qualifying templates.  */
10459	  if (!cp_parser_check_template_parameters (parser,
10460						    /*num_templates=*/0))
10461	    return error_mark_node;
10462	  type = xref_tag (tag_type, identifier, ts, template_p);
10463	}
10464    }
10465
10466  if (type == error_mark_node)
10467    return error_mark_node;
10468
10469  /* Allow attributes on forward declarations of classes.  */
10470  if (attributes)
10471    {
10472      if (TREE_CODE (type) == TYPENAME_TYPE)
10473	warning (OPT_Wattributes,
10474		 "attributes ignored on uninstantiated type");
10475      else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10476	       && ! processing_explicit_instantiation)
10477	warning (OPT_Wattributes,
10478		 "attributes ignored on template instantiation");
10479      else if (is_declaration && cp_parser_declares_only_class_p (parser))
10480	cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10481      else
10482	warning (OPT_Wattributes,
10483		 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10484    }
10485
10486  if (tag_type != enum_type)
10487    cp_parser_check_class_key (tag_type, type);
10488
10489  /* A "<" cannot follow an elaborated type specifier.  If that
10490     happens, the user was probably trying to form a template-id.  */
10491  cp_parser_check_for_invalid_template_id (parser, type);
10492
10493  return type;
10494}
10495
10496/* Parse an enum-specifier.
10497
10498   enum-specifier:
10499     enum identifier [opt] { enumerator-list [opt] }
10500
10501   GNU Extensions:
10502     enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10503       attributes[opt]
10504
10505   Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10506   if the token stream isn't an enum-specifier after all.  */
10507
10508static tree
10509cp_parser_enum_specifier (cp_parser* parser)
10510{
10511  tree identifier;
10512  tree type;
10513  tree attributes;
10514
10515  /* Parse tentatively so that we can back up if we don't find a
10516     enum-specifier.  */
10517  cp_parser_parse_tentatively (parser);
10518
10519  /* Caller guarantees that the current token is 'enum', an identifier
10520     possibly follows, and the token after that is an opening brace.
10521     If we don't have an identifier, fabricate an anonymous name for
10522     the enumeration being defined.  */
10523  cp_lexer_consume_token (parser->lexer);
10524
10525  attributes = cp_parser_attributes_opt (parser);
10526
10527  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10528    identifier = cp_parser_identifier (parser);
10529  else
10530    identifier = make_anon_name ();
10531
10532  /* Look for the `{' but don't consume it yet.  */
10533  if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10534    cp_parser_simulate_error (parser);
10535
10536  if (!cp_parser_parse_definitely (parser))
10537    return NULL_TREE;
10538
10539  /* Issue an error message if type-definitions are forbidden here.  */
10540  if (!cp_parser_check_type_definition (parser))
10541    type = error_mark_node;
10542  else
10543    /* Create the new type.  We do this before consuming the opening
10544       brace so the enum will be recorded as being on the line of its
10545       tag (or the 'enum' keyword, if there is no tag).  */
10546    type = start_enum (identifier);
10547
10548  /* Consume the opening brace.  */
10549  cp_lexer_consume_token (parser->lexer);
10550
10551  if (type == error_mark_node)
10552    {
10553      cp_parser_skip_to_end_of_block_or_statement (parser);
10554      return error_mark_node;
10555    }
10556
10557  /* If the next token is not '}', then there are some enumerators.  */
10558  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10559    cp_parser_enumerator_list (parser, type);
10560
10561  /* Consume the final '}'.  */
10562  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10563
10564  /* Look for trailing attributes to apply to this enumeration, and
10565     apply them if appropriate.  */
10566  if (cp_parser_allow_gnu_extensions_p (parser))
10567    {
10568      tree trailing_attr = cp_parser_attributes_opt (parser);
10569      cplus_decl_attributes (&type,
10570			     trailing_attr,
10571			     (int) ATTR_FLAG_TYPE_IN_PLACE);
10572    }
10573
10574  /* Finish up the enumeration.  */
10575  finish_enum (type);
10576
10577  return type;
10578}
10579
10580/* Parse an enumerator-list.  The enumerators all have the indicated
10581   TYPE.
10582
10583   enumerator-list:
10584     enumerator-definition
10585     enumerator-list , enumerator-definition  */
10586
10587static void
10588cp_parser_enumerator_list (cp_parser* parser, tree type)
10589{
10590  while (true)
10591    {
10592      /* Parse an enumerator-definition.  */
10593      cp_parser_enumerator_definition (parser, type);
10594
10595      /* If the next token is not a ',', we've reached the end of
10596	 the list.  */
10597      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10598	break;
10599      /* Otherwise, consume the `,' and keep going.  */
10600      cp_lexer_consume_token (parser->lexer);
10601      /* If the next token is a `}', there is a trailing comma.  */
10602      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10603	{
10604	  if (pedantic && !in_system_header)
10605	    pedwarn ("comma at end of enumerator list");
10606	  break;
10607	}
10608    }
10609}
10610
10611/* Parse an enumerator-definition.  The enumerator has the indicated
10612   TYPE.
10613
10614   enumerator-definition:
10615     enumerator
10616     enumerator = constant-expression
10617
10618   enumerator:
10619     identifier  */
10620
10621static void
10622cp_parser_enumerator_definition (cp_parser* parser, tree type)
10623{
10624  tree identifier;
10625  tree value;
10626
10627  /* Look for the identifier.  */
10628  identifier = cp_parser_identifier (parser);
10629  if (identifier == error_mark_node)
10630    return;
10631
10632  /* If the next token is an '=', then there is an explicit value.  */
10633  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10634    {
10635      /* Consume the `=' token.  */
10636      cp_lexer_consume_token (parser->lexer);
10637      /* Parse the value.  */
10638      value = cp_parser_constant_expression (parser,
10639					     /*allow_non_constant_p=*/false,
10640					     NULL);
10641    }
10642  else
10643    value = NULL_TREE;
10644
10645  /* Create the enumerator.  */
10646  build_enumerator (identifier, value, type);
10647}
10648
10649/* Parse a namespace-name.
10650
10651   namespace-name:
10652     original-namespace-name
10653     namespace-alias
10654
10655   Returns the NAMESPACE_DECL for the namespace.  */
10656
10657static tree
10658cp_parser_namespace_name (cp_parser* parser)
10659{
10660  tree identifier;
10661  tree namespace_decl;
10662
10663  /* Get the name of the namespace.  */
10664  identifier = cp_parser_identifier (parser);
10665  if (identifier == error_mark_node)
10666    return error_mark_node;
10667
10668  /* Look up the identifier in the currently active scope.  Look only
10669     for namespaces, due to:
10670
10671       [basic.lookup.udir]
10672
10673       When looking up a namespace-name in a using-directive or alias
10674       definition, only namespace names are considered.
10675
10676     And:
10677
10678       [basic.lookup.qual]
10679
10680       During the lookup of a name preceding the :: scope resolution
10681       operator, object, function, and enumerator names are ignored.
10682
10683     (Note that cp_parser_class_or_namespace_name only calls this
10684     function if the token after the name is the scope resolution
10685     operator.)  */
10686  namespace_decl = cp_parser_lookup_name (parser, identifier,
10687					  none_type,
10688					  /*is_template=*/false,
10689					  /*is_namespace=*/true,
10690					  /*check_dependency=*/true,
10691					  /*ambiguous_decls=*/NULL);
10692  /* If it's not a namespace, issue an error.  */
10693  if (namespace_decl == error_mark_node
10694      || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10695    {
10696      if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10697	error ("%qD is not a namespace-name", identifier);
10698      cp_parser_error (parser, "expected namespace-name");
10699      namespace_decl = error_mark_node;
10700    }
10701
10702  return namespace_decl;
10703}
10704
10705/* Parse a namespace-definition.
10706
10707   namespace-definition:
10708     named-namespace-definition
10709     unnamed-namespace-definition
10710
10711   named-namespace-definition:
10712     original-namespace-definition
10713     extension-namespace-definition
10714
10715   original-namespace-definition:
10716     namespace identifier { namespace-body }
10717
10718   extension-namespace-definition:
10719     namespace original-namespace-name { namespace-body }
10720
10721   unnamed-namespace-definition:
10722     namespace { namespace-body } */
10723
10724static void
10725cp_parser_namespace_definition (cp_parser* parser)
10726{
10727  tree identifier, attribs;
10728
10729  /* Look for the `namespace' keyword.  */
10730  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10731
10732  /* Get the name of the namespace.  We do not attempt to distinguish
10733     between an original-namespace-definition and an
10734     extension-namespace-definition at this point.  The semantic
10735     analysis routines are responsible for that.  */
10736  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10737    identifier = cp_parser_identifier (parser);
10738  else
10739    identifier = NULL_TREE;
10740
10741  /* Parse any specified attributes.  */
10742  attribs = cp_parser_attributes_opt (parser);
10743
10744  /* Look for the `{' to start the namespace.  */
10745  cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10746  /* Start the namespace.  */
10747  push_namespace_with_attribs (identifier, attribs);
10748  /* Parse the body of the namespace.  */
10749  cp_parser_namespace_body (parser);
10750  /* Finish the namespace.  */
10751  pop_namespace ();
10752  /* Look for the final `}'.  */
10753  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10754}
10755
10756/* Parse a namespace-body.
10757
10758   namespace-body:
10759     declaration-seq [opt]  */
10760
10761static void
10762cp_parser_namespace_body (cp_parser* parser)
10763{
10764  cp_parser_declaration_seq_opt (parser);
10765}
10766
10767/* Parse a namespace-alias-definition.
10768
10769   namespace-alias-definition:
10770     namespace identifier = qualified-namespace-specifier ;  */
10771
10772static void
10773cp_parser_namespace_alias_definition (cp_parser* parser)
10774{
10775  tree identifier;
10776  tree namespace_specifier;
10777
10778  /* Look for the `namespace' keyword.  */
10779  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10780  /* Look for the identifier.  */
10781  identifier = cp_parser_identifier (parser);
10782  if (identifier == error_mark_node)
10783    return;
10784  /* Look for the `=' token.  */
10785  cp_parser_require (parser, CPP_EQ, "`='");
10786  /* Look for the qualified-namespace-specifier.  */
10787  namespace_specifier
10788    = cp_parser_qualified_namespace_specifier (parser);
10789  /* Look for the `;' token.  */
10790  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10791
10792  /* Register the alias in the symbol table.  */
10793  do_namespace_alias (identifier, namespace_specifier);
10794}
10795
10796/* Parse a qualified-namespace-specifier.
10797
10798   qualified-namespace-specifier:
10799     :: [opt] nested-name-specifier [opt] namespace-name
10800
10801   Returns a NAMESPACE_DECL corresponding to the specified
10802   namespace.  */
10803
10804static tree
10805cp_parser_qualified_namespace_specifier (cp_parser* parser)
10806{
10807  /* Look for the optional `::'.  */
10808  cp_parser_global_scope_opt (parser,
10809			      /*current_scope_valid_p=*/false);
10810
10811  /* Look for the optional nested-name-specifier.  */
10812  cp_parser_nested_name_specifier_opt (parser,
10813				       /*typename_keyword_p=*/false,
10814				       /*check_dependency_p=*/true,
10815				       /*type_p=*/false,
10816				       /*is_declaration=*/true);
10817
10818  return cp_parser_namespace_name (parser);
10819}
10820
10821/* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10822   access declaration.
10823
10824   using-declaration:
10825     using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10826     using :: unqualified-id ;
10827
10828   access-declaration:
10829     qualified-id ;
10830
10831   */
10832
10833static bool
10834cp_parser_using_declaration (cp_parser* parser,
10835			     bool access_declaration_p)
10836{
10837  cp_token *token;
10838  bool typename_p = false;
10839  bool global_scope_p;
10840  tree decl;
10841  tree identifier;
10842  tree qscope;
10843
10844  if (access_declaration_p)
10845    cp_parser_parse_tentatively (parser);
10846  else
10847    {
10848      /* Look for the `using' keyword.  */
10849      cp_parser_require_keyword (parser, RID_USING, "`using'");
10850
10851      /* Peek at the next token.  */
10852      token = cp_lexer_peek_token (parser->lexer);
10853      /* See if it's `typename'.  */
10854      if (token->keyword == RID_TYPENAME)
10855	{
10856	  /* Remember that we've seen it.  */
10857	  typename_p = true;
10858	  /* Consume the `typename' token.  */
10859	  cp_lexer_consume_token (parser->lexer);
10860	}
10861    }
10862
10863  /* Look for the optional global scope qualification.  */
10864  global_scope_p
10865    = (cp_parser_global_scope_opt (parser,
10866				   /*current_scope_valid_p=*/false)
10867       != NULL_TREE);
10868
10869  /* If we saw `typename', or didn't see `::', then there must be a
10870     nested-name-specifier present.  */
10871  if (typename_p || !global_scope_p)
10872    qscope = cp_parser_nested_name_specifier (parser, typename_p,
10873					      /*check_dependency_p=*/true,
10874					      /*type_p=*/false,
10875					      /*is_declaration=*/true);
10876  /* Otherwise, we could be in either of the two productions.  In that
10877     case, treat the nested-name-specifier as optional.  */
10878  else
10879    qscope = cp_parser_nested_name_specifier_opt (parser,
10880						  /*typename_keyword_p=*/false,
10881						  /*check_dependency_p=*/true,
10882						  /*type_p=*/false,
10883						  /*is_declaration=*/true);
10884  if (!qscope)
10885    qscope = global_namespace;
10886
10887  if (access_declaration_p && cp_parser_error_occurred (parser))
10888    /* Something has already gone wrong; there's no need to parse
10889       further.  Since an error has occurred, the return value of
10890       cp_parser_parse_definitely will be false, as required.  */
10891    return cp_parser_parse_definitely (parser);
10892
10893  /* Parse the unqualified-id.  */
10894  identifier = cp_parser_unqualified_id (parser,
10895					 /*template_keyword_p=*/false,
10896					 /*check_dependency_p=*/true,
10897					 /*declarator_p=*/true,
10898					 /*optional_p=*/false);
10899
10900  if (access_declaration_p)
10901    {
10902      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10903	cp_parser_simulate_error (parser);
10904      if (!cp_parser_parse_definitely (parser))
10905	return false;
10906    }
10907
10908  /* The function we call to handle a using-declaration is different
10909     depending on what scope we are in.  */
10910  if (qscope == error_mark_node || identifier == error_mark_node)
10911    ;
10912  else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10913	   && TREE_CODE (identifier) != BIT_NOT_EXPR)
10914    /* [namespace.udecl]
10915
10916       A using declaration shall not name a template-id.  */
10917    error ("a template-id may not appear in a using-declaration");
10918  else
10919    {
10920      if (at_class_scope_p ())
10921	{
10922	  /* Create the USING_DECL.  */
10923	  decl = do_class_using_decl (parser->scope, identifier);
10924	  /* Add it to the list of members in this class.  */
10925	  finish_member_declaration (decl);
10926	}
10927      else
10928	{
10929	  decl = cp_parser_lookup_name_simple (parser, identifier);
10930	  if (decl == error_mark_node)
10931	    cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10932	  else if (!at_namespace_scope_p ())
10933	    do_local_using_decl (decl, qscope, identifier);
10934	  else
10935	    do_toplevel_using_decl (decl, qscope, identifier);
10936	}
10937    }
10938
10939  /* Look for the final `;'.  */
10940  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10941
10942  return true;
10943}
10944
10945/* Parse a using-directive.
10946
10947   using-directive:
10948     using namespace :: [opt] nested-name-specifier [opt]
10949       namespace-name ;  */
10950
10951static void
10952cp_parser_using_directive (cp_parser* parser)
10953{
10954  tree namespace_decl;
10955  tree attribs;
10956
10957  /* Look for the `using' keyword.  */
10958  cp_parser_require_keyword (parser, RID_USING, "`using'");
10959  /* And the `namespace' keyword.  */
10960  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10961  /* Look for the optional `::' operator.  */
10962  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10963  /* And the optional nested-name-specifier.  */
10964  cp_parser_nested_name_specifier_opt (parser,
10965				       /*typename_keyword_p=*/false,
10966				       /*check_dependency_p=*/true,
10967				       /*type_p=*/false,
10968				       /*is_declaration=*/true);
10969  /* Get the namespace being used.  */
10970  namespace_decl = cp_parser_namespace_name (parser);
10971  /* And any specified attributes.  */
10972  attribs = cp_parser_attributes_opt (parser);
10973  /* Update the symbol table.  */
10974  parse_using_directive (namespace_decl, attribs);
10975  /* Look for the final `;'.  */
10976  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10977}
10978
10979/* Parse an asm-definition.
10980
10981   asm-definition:
10982     asm ( string-literal ) ;
10983
10984   GNU Extension:
10985
10986   asm-definition:
10987     asm volatile [opt] ( string-literal ) ;
10988     asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10989     asm volatile [opt] ( string-literal : asm-operand-list [opt]
10990			  : asm-operand-list [opt] ) ;
10991     asm volatile [opt] ( string-literal : asm-operand-list [opt]
10992			  : asm-operand-list [opt]
10993			  : asm-operand-list [opt] ) ;  */
10994
10995static void
10996cp_parser_asm_definition (cp_parser* parser)
10997{
10998  tree string;
10999  tree outputs = NULL_TREE;
11000  tree inputs = NULL_TREE;
11001  tree clobbers = NULL_TREE;
11002  tree asm_stmt;
11003  bool volatile_p = false;
11004  bool extended_p = false;
11005
11006  /* Look for the `asm' keyword.  */
11007  cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11008  /* See if the next token is `volatile'.  */
11009  if (cp_parser_allow_gnu_extensions_p (parser)
11010      && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11011    {
11012      /* Remember that we saw the `volatile' keyword.  */
11013      volatile_p = true;
11014      /* Consume the token.  */
11015      cp_lexer_consume_token (parser->lexer);
11016    }
11017  /* Look for the opening `('.  */
11018  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11019    return;
11020  /* Look for the string.  */
11021  string = cp_parser_string_literal (parser, false, false);
11022  if (string == error_mark_node)
11023    {
11024      cp_parser_skip_to_closing_parenthesis (parser, true, false,
11025					     /*consume_paren=*/true);
11026      return;
11027    }
11028
11029  /* If we're allowing GNU extensions, check for the extended assembly
11030     syntax.  Unfortunately, the `:' tokens need not be separated by
11031     a space in C, and so, for compatibility, we tolerate that here
11032     too.  Doing that means that we have to treat the `::' operator as
11033     two `:' tokens.  */
11034  if (cp_parser_allow_gnu_extensions_p (parser)
11035      && parser->in_function_body
11036      && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11037	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11038    {
11039      bool inputs_p = false;
11040      bool clobbers_p = false;
11041
11042      /* The extended syntax was used.  */
11043      extended_p = true;
11044
11045      /* Look for outputs.  */
11046      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11047	{
11048	  /* Consume the `:'.  */
11049	  cp_lexer_consume_token (parser->lexer);
11050	  /* Parse the output-operands.  */
11051	  if (cp_lexer_next_token_is_not (parser->lexer,
11052					  CPP_COLON)
11053	      && cp_lexer_next_token_is_not (parser->lexer,
11054					     CPP_SCOPE)
11055	      && cp_lexer_next_token_is_not (parser->lexer,
11056					     CPP_CLOSE_PAREN))
11057	    outputs = cp_parser_asm_operand_list (parser);
11058	}
11059      /* If the next token is `::', there are no outputs, and the
11060	 next token is the beginning of the inputs.  */
11061      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11062	/* The inputs are coming next.  */
11063	inputs_p = true;
11064
11065      /* Look for inputs.  */
11066      if (inputs_p
11067	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11068	{
11069	  /* Consume the `:' or `::'.  */
11070	  cp_lexer_consume_token (parser->lexer);
11071	  /* Parse the output-operands.  */
11072	  if (cp_lexer_next_token_is_not (parser->lexer,
11073					  CPP_COLON)
11074	      && cp_lexer_next_token_is_not (parser->lexer,
11075					     CPP_CLOSE_PAREN))
11076	    inputs = cp_parser_asm_operand_list (parser);
11077	}
11078      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11079	/* The clobbers are coming next.  */
11080	clobbers_p = true;
11081
11082      /* Look for clobbers.  */
11083      if (clobbers_p
11084	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11085	{
11086	  /* Consume the `:' or `::'.  */
11087	  cp_lexer_consume_token (parser->lexer);
11088	  /* Parse the clobbers.  */
11089	  if (cp_lexer_next_token_is_not (parser->lexer,
11090					  CPP_CLOSE_PAREN))
11091	    clobbers = cp_parser_asm_clobber_list (parser);
11092	}
11093    }
11094  /* Look for the closing `)'.  */
11095  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11096    cp_parser_skip_to_closing_parenthesis (parser, true, false,
11097					   /*consume_paren=*/true);
11098  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11099
11100  /* Create the ASM_EXPR.  */
11101  if (parser->in_function_body)
11102    {
11103      asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11104				  inputs, clobbers);
11105      /* If the extended syntax was not used, mark the ASM_EXPR.  */
11106      if (!extended_p)
11107	{
11108	  tree temp = asm_stmt;
11109	  if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11110	    temp = TREE_OPERAND (temp, 0);
11111
11112	  ASM_INPUT_P (temp) = 1;
11113	}
11114    }
11115  else
11116    cgraph_add_asm_node (string);
11117}
11118
11119/* Declarators [gram.dcl.decl] */
11120
11121/* Parse an init-declarator.
11122
11123   init-declarator:
11124     declarator initializer [opt]
11125
11126   GNU Extension:
11127
11128   init-declarator:
11129     declarator asm-specification [opt] attributes [opt] initializer [opt]
11130
11131   function-definition:
11132     decl-specifier-seq [opt] declarator ctor-initializer [opt]
11133       function-body
11134     decl-specifier-seq [opt] declarator function-try-block
11135
11136   GNU Extension:
11137
11138   function-definition:
11139     __extension__ function-definition
11140
11141   The DECL_SPECIFIERS apply to this declarator.  Returns a
11142   representation of the entity declared.  If MEMBER_P is TRUE, then
11143   this declarator appears in a class scope.  The new DECL created by
11144   this declarator is returned.
11145
11146   The CHECKS are access checks that should be performed once we know
11147   what entity is being declared (and, therefore, what classes have
11148   befriended it).
11149
11150   If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11151   for a function-definition here as well.  If the declarator is a
11152   declarator for a function-definition, *FUNCTION_DEFINITION_P will
11153   be TRUE upon return.  By that point, the function-definition will
11154   have been completely parsed.
11155
11156   FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11157   is FALSE.  */
11158
11159static tree
11160cp_parser_init_declarator (cp_parser* parser,
11161			   cp_decl_specifier_seq *decl_specifiers,
11162			   VEC (deferred_access_check,gc)* checks,
11163			   bool function_definition_allowed_p,
11164			   bool member_p,
11165			   int declares_class_or_enum,
11166			   bool* function_definition_p)
11167{
11168  cp_token *token;
11169  cp_declarator *declarator;
11170  tree prefix_attributes;
11171  tree attributes;
11172  tree asm_specification;
11173  tree initializer;
11174  tree decl = NULL_TREE;
11175  tree scope;
11176  bool is_initialized;
11177  /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
11178     initialized with "= ..", CPP_OPEN_PAREN if initialized with
11179     "(...)".  */
11180  enum cpp_ttype initialization_kind;
11181  bool is_parenthesized_init = false;
11182  bool is_non_constant_init;
11183  int ctor_dtor_or_conv_p;
11184  bool friend_p;
11185  tree pushed_scope = NULL;
11186
11187  /* Gather the attributes that were provided with the
11188     decl-specifiers.  */
11189  prefix_attributes = decl_specifiers->attributes;
11190
11191  /* Assume that this is not the declarator for a function
11192     definition.  */
11193  if (function_definition_p)
11194    *function_definition_p = false;
11195
11196  /* Defer access checks while parsing the declarator; we cannot know
11197     what names are accessible until we know what is being
11198     declared.  */
11199  resume_deferring_access_checks ();
11200
11201  /* Parse the declarator.  */
11202  declarator
11203    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11204			    &ctor_dtor_or_conv_p,
11205			    /*parenthesized_p=*/NULL,
11206			    /*member_p=*/false);
11207  /* Gather up the deferred checks.  */
11208  stop_deferring_access_checks ();
11209
11210  /* If the DECLARATOR was erroneous, there's no need to go
11211     further.  */
11212  if (declarator == cp_error_declarator)
11213    return error_mark_node;
11214
11215  /* Check that the number of template-parameter-lists is OK.  */
11216  if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11217    return error_mark_node;
11218
11219  if (declares_class_or_enum & 2)
11220    cp_parser_check_for_definition_in_return_type (declarator,
11221						   decl_specifiers->type);
11222
11223  /* Figure out what scope the entity declared by the DECLARATOR is
11224     located in.  `grokdeclarator' sometimes changes the scope, so
11225     we compute it now.  */
11226  scope = get_scope_of_declarator (declarator);
11227
11228  /* If we're allowing GNU extensions, look for an asm-specification
11229     and attributes.  */
11230  if (cp_parser_allow_gnu_extensions_p (parser))
11231    {
11232      /* Look for an asm-specification.  */
11233      asm_specification = cp_parser_asm_specification_opt (parser);
11234      /* And attributes.  */
11235      attributes = cp_parser_attributes_opt (parser);
11236    }
11237  else
11238    {
11239      asm_specification = NULL_TREE;
11240      attributes = NULL_TREE;
11241    }
11242
11243  /* Peek at the next token.  */
11244  token = cp_lexer_peek_token (parser->lexer);
11245  /* Check to see if the token indicates the start of a
11246     function-definition.  */
11247  if (cp_parser_token_starts_function_definition_p (token))
11248    {
11249      if (!function_definition_allowed_p)
11250	{
11251	  /* If a function-definition should not appear here, issue an
11252	     error message.  */
11253	  cp_parser_error (parser,
11254			   "a function-definition is not allowed here");
11255	  return error_mark_node;
11256	}
11257      else
11258	{
11259	  /* Neither attributes nor an asm-specification are allowed
11260	     on a function-definition.  */
11261	  if (asm_specification)
11262	    error ("an asm-specification is not allowed on a function-definition");
11263	  if (attributes)
11264	    error ("attributes are not allowed on a function-definition");
11265	  /* This is a function-definition.  */
11266	  *function_definition_p = true;
11267
11268	  /* Parse the function definition.  */
11269	  if (member_p)
11270	    decl = cp_parser_save_member_function_body (parser,
11271							decl_specifiers,
11272							declarator,
11273							prefix_attributes);
11274	  else
11275	    decl
11276	      = (cp_parser_function_definition_from_specifiers_and_declarator
11277		 (parser, decl_specifiers, prefix_attributes, declarator));
11278
11279	  return decl;
11280	}
11281    }
11282
11283  /* [dcl.dcl]
11284
11285     Only in function declarations for constructors, destructors, and
11286     type conversions can the decl-specifier-seq be omitted.
11287
11288     We explicitly postpone this check past the point where we handle
11289     function-definitions because we tolerate function-definitions
11290     that are missing their return types in some modes.  */
11291  if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11292    {
11293      cp_parser_error (parser,
11294		       "expected constructor, destructor, or type conversion");
11295      return error_mark_node;
11296    }
11297
11298  /* An `=' or an `(' indicates an initializer.  */
11299  if (token->type == CPP_EQ
11300      || token->type == CPP_OPEN_PAREN)
11301    {
11302      is_initialized = true;
11303      initialization_kind = token->type;
11304    }
11305  else
11306    {
11307      /* If the init-declarator isn't initialized and isn't followed by a
11308	 `,' or `;', it's not a valid init-declarator.  */
11309      if (token->type != CPP_COMMA
11310	  && token->type != CPP_SEMICOLON)
11311	{
11312	  cp_parser_error (parser, "expected initializer");
11313	  return error_mark_node;
11314	}
11315      is_initialized = false;
11316      initialization_kind = CPP_EOF;
11317    }
11318
11319  /* Because start_decl has side-effects, we should only call it if we
11320     know we're going ahead.  By this point, we know that we cannot
11321     possibly be looking at any other construct.  */
11322  cp_parser_commit_to_tentative_parse (parser);
11323
11324  /* If the decl specifiers were bad, issue an error now that we're
11325     sure this was intended to be a declarator.  Then continue
11326     declaring the variable(s), as int, to try to cut down on further
11327     errors.  */
11328  if (decl_specifiers->any_specifiers_p
11329      && decl_specifiers->type == error_mark_node)
11330    {
11331      cp_parser_error (parser, "invalid type in declaration");
11332      decl_specifiers->type = integer_type_node;
11333    }
11334
11335  /* Check to see whether or not this declaration is a friend.  */
11336  friend_p = cp_parser_friend_p (decl_specifiers);
11337
11338  /* Enter the newly declared entry in the symbol table.  If we're
11339     processing a declaration in a class-specifier, we wait until
11340     after processing the initializer.  */
11341  if (!member_p)
11342    {
11343      if (parser->in_unbraced_linkage_specification_p)
11344	decl_specifiers->storage_class = sc_extern;
11345      decl = start_decl (declarator, decl_specifiers,
11346			 is_initialized, attributes, prefix_attributes,
11347			 &pushed_scope);
11348    }
11349  else if (scope)
11350    /* Enter the SCOPE.  That way unqualified names appearing in the
11351       initializer will be looked up in SCOPE.  */
11352    pushed_scope = push_scope (scope);
11353
11354  /* Perform deferred access control checks, now that we know in which
11355     SCOPE the declared entity resides.  */
11356  if (!member_p && decl)
11357    {
11358      tree saved_current_function_decl = NULL_TREE;
11359
11360      /* If the entity being declared is a function, pretend that we
11361	 are in its scope.  If it is a `friend', it may have access to
11362	 things that would not otherwise be accessible.  */
11363      if (TREE_CODE (decl) == FUNCTION_DECL)
11364	{
11365	  saved_current_function_decl = current_function_decl;
11366	  current_function_decl = decl;
11367	}
11368
11369      /* Perform access checks for template parameters.  */
11370      cp_parser_perform_template_parameter_access_checks (checks);
11371
11372      /* Perform the access control checks for the declarator and the
11373	 the decl-specifiers.  */
11374      perform_deferred_access_checks ();
11375
11376      /* Restore the saved value.  */
11377      if (TREE_CODE (decl) == FUNCTION_DECL)
11378	current_function_decl = saved_current_function_decl;
11379    }
11380
11381  /* Parse the initializer.  */
11382  initializer = NULL_TREE;
11383  is_parenthesized_init = false;
11384  is_non_constant_init = true;
11385  if (is_initialized)
11386    {
11387      if (function_declarator_p (declarator))
11388	{
11389	   if (initialization_kind == CPP_EQ)
11390	     initializer = cp_parser_pure_specifier (parser);
11391	   else
11392	     {
11393	       /* If the declaration was erroneous, we don't really
11394		  know what the user intended, so just silently
11395		  consume the initializer.  */
11396	       if (decl != error_mark_node)
11397		 error ("initializer provided for function");
11398	       cp_parser_skip_to_closing_parenthesis (parser,
11399						      /*recovering=*/true,
11400						      /*or_comma=*/false,
11401						      /*consume_paren=*/true);
11402	     }
11403	}
11404      else
11405	initializer = cp_parser_initializer (parser,
11406					     &is_parenthesized_init,
11407					     &is_non_constant_init);
11408    }
11409
11410  /* The old parser allows attributes to appear after a parenthesized
11411     initializer.  Mark Mitchell proposed removing this functionality
11412     on the GCC mailing lists on 2002-08-13.  This parser accepts the
11413     attributes -- but ignores them.  */
11414  if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11415    if (cp_parser_attributes_opt (parser))
11416      warning (OPT_Wattributes,
11417	       "attributes after parenthesized initializer ignored");
11418
11419  /* For an in-class declaration, use `grokfield' to create the
11420     declaration.  */
11421  if (member_p)
11422    {
11423      if (pushed_scope)
11424	{
11425	  pop_scope (pushed_scope);
11426	  pushed_scope = false;
11427	}
11428      decl = grokfield (declarator, decl_specifiers,
11429			initializer, !is_non_constant_init,
11430			/*asmspec=*/NULL_TREE,
11431			prefix_attributes);
11432      if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11433	cp_parser_save_default_args (parser, decl);
11434    }
11435
11436  /* Finish processing the declaration.  But, skip friend
11437     declarations.  */
11438  if (!friend_p && decl && decl != error_mark_node)
11439    {
11440      cp_finish_decl (decl,
11441		      initializer, !is_non_constant_init,
11442		      asm_specification,
11443		      /* If the initializer is in parentheses, then this is
11444			 a direct-initialization, which means that an
11445			 `explicit' constructor is OK.  Otherwise, an
11446			 `explicit' constructor cannot be used.  */
11447		      ((is_parenthesized_init || !is_initialized)
11448		     ? 0 : LOOKUP_ONLYCONVERTING));
11449    }
11450  if (!friend_p && pushed_scope)
11451    pop_scope (pushed_scope);
11452
11453  return decl;
11454}
11455
11456/* Parse a declarator.
11457
11458   declarator:
11459     direct-declarator
11460     ptr-operator declarator
11461
11462   abstract-declarator:
11463     ptr-operator abstract-declarator [opt]
11464     direct-abstract-declarator
11465
11466   GNU Extensions:
11467
11468   declarator:
11469     attributes [opt] direct-declarator
11470     attributes [opt] ptr-operator declarator
11471
11472   abstract-declarator:
11473     attributes [opt] ptr-operator abstract-declarator [opt]
11474     attributes [opt] direct-abstract-declarator
11475
11476   If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11477   detect constructor, destructor or conversion operators. It is set
11478   to -1 if the declarator is a name, and +1 if it is a
11479   function. Otherwise it is set to zero. Usually you just want to
11480   test for >0, but internally the negative value is used.
11481
11482   (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11483   a decl-specifier-seq unless it declares a constructor, destructor,
11484   or conversion.  It might seem that we could check this condition in
11485   semantic analysis, rather than parsing, but that makes it difficult
11486   to handle something like `f()'.  We want to notice that there are
11487   no decl-specifiers, and therefore realize that this is an
11488   expression, not a declaration.)
11489
11490   If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11491   the declarator is a direct-declarator of the form "(...)".
11492
11493   MEMBER_P is true iff this declarator is a member-declarator.  */
11494
11495static cp_declarator *
11496cp_parser_declarator (cp_parser* parser,
11497		      cp_parser_declarator_kind dcl_kind,
11498		      int* ctor_dtor_or_conv_p,
11499		      bool* parenthesized_p,
11500		      bool member_p)
11501{
11502  cp_token *token;
11503  cp_declarator *declarator;
11504  enum tree_code code;
11505  cp_cv_quals cv_quals;
11506  tree class_type;
11507  tree attributes = NULL_TREE;
11508
11509  /* Assume this is not a constructor, destructor, or type-conversion
11510     operator.  */
11511  if (ctor_dtor_or_conv_p)
11512    *ctor_dtor_or_conv_p = 0;
11513
11514  if (cp_parser_allow_gnu_extensions_p (parser))
11515    attributes = cp_parser_attributes_opt (parser);
11516
11517  /* Peek at the next token.  */
11518  token = cp_lexer_peek_token (parser->lexer);
11519
11520  /* Check for the ptr-operator production.  */
11521  cp_parser_parse_tentatively (parser);
11522  /* Parse the ptr-operator.  */
11523  code = cp_parser_ptr_operator (parser,
11524				 &class_type,
11525				 &cv_quals);
11526  /* If that worked, then we have a ptr-operator.  */
11527  if (cp_parser_parse_definitely (parser))
11528    {
11529      /* If a ptr-operator was found, then this declarator was not
11530	 parenthesized.  */
11531      if (parenthesized_p)
11532	*parenthesized_p = true;
11533      /* The dependent declarator is optional if we are parsing an
11534	 abstract-declarator.  */
11535      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11536	cp_parser_parse_tentatively (parser);
11537
11538      /* Parse the dependent declarator.  */
11539      declarator = cp_parser_declarator (parser, dcl_kind,
11540					 /*ctor_dtor_or_conv_p=*/NULL,
11541					 /*parenthesized_p=*/NULL,
11542					 /*member_p=*/false);
11543
11544      /* If we are parsing an abstract-declarator, we must handle the
11545	 case where the dependent declarator is absent.  */
11546      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11547	  && !cp_parser_parse_definitely (parser))
11548	declarator = NULL;
11549
11550      /* Build the representation of the ptr-operator.  */
11551      if (class_type)
11552	declarator = make_ptrmem_declarator (cv_quals,
11553					     class_type,
11554					     declarator);
11555      else if (code == INDIRECT_REF)
11556	declarator = make_pointer_declarator (cv_quals, declarator);
11557      else
11558	declarator = make_reference_declarator (cv_quals, declarator);
11559    }
11560  /* Everything else is a direct-declarator.  */
11561  else
11562    {
11563      if (parenthesized_p)
11564	*parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11565						   CPP_OPEN_PAREN);
11566      declarator = cp_parser_direct_declarator (parser, dcl_kind,
11567						ctor_dtor_or_conv_p,
11568						member_p);
11569    }
11570
11571  if (attributes && declarator && declarator != cp_error_declarator)
11572    declarator->attributes = attributes;
11573
11574  return declarator;
11575}
11576
11577/* Parse a direct-declarator or direct-abstract-declarator.
11578
11579   direct-declarator:
11580     declarator-id
11581     direct-declarator ( parameter-declaration-clause )
11582       cv-qualifier-seq [opt]
11583       exception-specification [opt]
11584     direct-declarator [ constant-expression [opt] ]
11585     ( declarator )
11586
11587   direct-abstract-declarator:
11588     direct-abstract-declarator [opt]
11589       ( parameter-declaration-clause )
11590       cv-qualifier-seq [opt]
11591       exception-specification [opt]
11592     direct-abstract-declarator [opt] [ constant-expression [opt] ]
11593     ( abstract-declarator )
11594
11595   Returns a representation of the declarator.  DCL_KIND is
11596   CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11597   direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11598   we are parsing a direct-declarator.  It is
11599   CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11600   of ambiguity we prefer an abstract declarator, as per
11601   [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11602   cp_parser_declarator.  */
11603
11604static cp_declarator *
11605cp_parser_direct_declarator (cp_parser* parser,
11606			     cp_parser_declarator_kind dcl_kind,
11607			     int* ctor_dtor_or_conv_p,
11608			     bool member_p)
11609{
11610  cp_token *token;
11611  cp_declarator *declarator = NULL;
11612  tree scope = NULL_TREE;
11613  bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11614  bool saved_in_declarator_p = parser->in_declarator_p;
11615  bool first = true;
11616  tree pushed_scope = NULL_TREE;
11617
11618  while (true)
11619    {
11620      /* Peek at the next token.  */
11621      token = cp_lexer_peek_token (parser->lexer);
11622      if (token->type == CPP_OPEN_PAREN)
11623	{
11624	  /* This is either a parameter-declaration-clause, or a
11625	     parenthesized declarator. When we know we are parsing a
11626	     named declarator, it must be a parenthesized declarator
11627	     if FIRST is true. For instance, `(int)' is a
11628	     parameter-declaration-clause, with an omitted
11629	     direct-abstract-declarator. But `((*))', is a
11630	     parenthesized abstract declarator. Finally, when T is a
11631	     template parameter `(T)' is a
11632	     parameter-declaration-clause, and not a parenthesized
11633	     named declarator.
11634
11635	     We first try and parse a parameter-declaration-clause,
11636	     and then try a nested declarator (if FIRST is true).
11637
11638	     It is not an error for it not to be a
11639	     parameter-declaration-clause, even when FIRST is
11640	     false. Consider,
11641
11642	       int i (int);
11643	       int i (3);
11644
11645	     The first is the declaration of a function while the
11646	     second is a the definition of a variable, including its
11647	     initializer.
11648
11649	     Having seen only the parenthesis, we cannot know which of
11650	     these two alternatives should be selected.  Even more
11651	     complex are examples like:
11652
11653	       int i (int (a));
11654	       int i (int (3));
11655
11656	     The former is a function-declaration; the latter is a
11657	     variable initialization.
11658
11659	     Thus again, we try a parameter-declaration-clause, and if
11660	     that fails, we back out and return.  */
11661
11662	  if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11663	    {
11664	      cp_parameter_declarator *params;
11665	      unsigned saved_num_template_parameter_lists;
11666
11667	      /* In a member-declarator, the only valid interpretation
11668		 of a parenthesis is the start of a
11669		 parameter-declaration-clause.  (It is invalid to
11670		 initialize a static data member with a parenthesized
11671		 initializer; only the "=" form of initialization is
11672		 permitted.)  */
11673	      if (!member_p)
11674		cp_parser_parse_tentatively (parser);
11675
11676	      /* Consume the `('.  */
11677	      cp_lexer_consume_token (parser->lexer);
11678	      if (first)
11679		{
11680		  /* If this is going to be an abstract declarator, we're
11681		     in a declarator and we can't have default args.  */
11682		  parser->default_arg_ok_p = false;
11683		  parser->in_declarator_p = true;
11684		}
11685
11686	      /* Inside the function parameter list, surrounding
11687		 template-parameter-lists do not apply.  */
11688	      saved_num_template_parameter_lists
11689		= parser->num_template_parameter_lists;
11690	      parser->num_template_parameter_lists = 0;
11691
11692	      /* Parse the parameter-declaration-clause.  */
11693	      params = cp_parser_parameter_declaration_clause (parser);
11694
11695	      parser->num_template_parameter_lists
11696		= saved_num_template_parameter_lists;
11697
11698	      /* If all went well, parse the cv-qualifier-seq and the
11699		 exception-specification.  */
11700	      if (member_p || cp_parser_parse_definitely (parser))
11701		{
11702		  cp_cv_quals cv_quals;
11703		  tree exception_specification;
11704
11705		  if (ctor_dtor_or_conv_p)
11706		    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11707		  first = false;
11708		  /* Consume the `)'.  */
11709		  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11710
11711		  /* Parse the cv-qualifier-seq.  */
11712		  cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11713		  /* And the exception-specification.  */
11714		  exception_specification
11715		    = cp_parser_exception_specification_opt (parser);
11716
11717		  /* Create the function-declarator.  */
11718		  declarator = make_call_declarator (declarator,
11719						     params,
11720						     cv_quals,
11721						     exception_specification);
11722		  /* Any subsequent parameter lists are to do with
11723		     return type, so are not those of the declared
11724		     function.  */
11725		  parser->default_arg_ok_p = false;
11726
11727		  /* Repeat the main loop.  */
11728		  continue;
11729		}
11730	    }
11731
11732	  /* If this is the first, we can try a parenthesized
11733	     declarator.  */
11734	  if (first)
11735	    {
11736	      bool saved_in_type_id_in_expr_p;
11737
11738	      parser->default_arg_ok_p = saved_default_arg_ok_p;
11739	      parser->in_declarator_p = saved_in_declarator_p;
11740
11741	      /* Consume the `('.  */
11742	      cp_lexer_consume_token (parser->lexer);
11743	      /* Parse the nested declarator.  */
11744	      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11745	      parser->in_type_id_in_expr_p = true;
11746	      declarator
11747		= cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11748					/*parenthesized_p=*/NULL,
11749					member_p);
11750	      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11751	      first = false;
11752	      /* Expect a `)'.  */
11753	      if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11754		declarator = cp_error_declarator;
11755	      if (declarator == cp_error_declarator)
11756		break;
11757
11758	      goto handle_declarator;
11759	    }
11760	  /* Otherwise, we must be done.  */
11761	  else
11762	    break;
11763	}
11764      else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11765	       && token->type == CPP_OPEN_SQUARE)
11766	{
11767	  /* Parse an array-declarator.  */
11768	  tree bounds;
11769
11770	  if (ctor_dtor_or_conv_p)
11771	    *ctor_dtor_or_conv_p = 0;
11772
11773	  first = false;
11774	  parser->default_arg_ok_p = false;
11775	  parser->in_declarator_p = true;
11776	  /* Consume the `['.  */
11777	  cp_lexer_consume_token (parser->lexer);
11778	  /* Peek at the next token.  */
11779	  token = cp_lexer_peek_token (parser->lexer);
11780	  /* If the next token is `]', then there is no
11781	     constant-expression.  */
11782	  if (token->type != CPP_CLOSE_SQUARE)
11783	    {
11784	      bool non_constant_p;
11785
11786	      bounds
11787		= cp_parser_constant_expression (parser,
11788						 /*allow_non_constant=*/true,
11789						 &non_constant_p);
11790	      if (!non_constant_p)
11791		bounds = fold_non_dependent_expr (bounds);
11792	      /* Normally, the array bound must be an integral constant
11793		 expression.  However, as an extension, we allow VLAs
11794		 in function scopes.  */
11795	      else if (!parser->in_function_body)
11796		{
11797		  error ("array bound is not an integer constant");
11798		  bounds = error_mark_node;
11799		}
11800	    }
11801	  else
11802	    bounds = NULL_TREE;
11803	  /* Look for the closing `]'.  */
11804	  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11805	    {
11806	      declarator = cp_error_declarator;
11807	      break;
11808	    }
11809
11810	  declarator = make_array_declarator (declarator, bounds);
11811	}
11812      else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11813	{
11814	  tree qualifying_scope;
11815	  tree unqualified_name;
11816	  special_function_kind sfk;
11817	  bool abstract_ok;
11818
11819	  /* Parse a declarator-id */
11820	  abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11821	  if (abstract_ok)
11822	    cp_parser_parse_tentatively (parser);
11823	  unqualified_name
11824	    = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11825	  qualifying_scope = parser->scope;
11826	  if (abstract_ok)
11827	    {
11828	      if (!cp_parser_parse_definitely (parser))
11829		unqualified_name = error_mark_node;
11830	      else if (unqualified_name
11831		       && (qualifying_scope
11832			   || (TREE_CODE (unqualified_name)
11833			       != IDENTIFIER_NODE)))
11834		{
11835		  cp_parser_error (parser, "expected unqualified-id");
11836		  unqualified_name = error_mark_node;
11837		}
11838	    }
11839
11840	  if (!unqualified_name)
11841	    return NULL;
11842	  if (unqualified_name == error_mark_node)
11843	    {
11844	      declarator = cp_error_declarator;
11845	      break;
11846	    }
11847
11848	  if (qualifying_scope && at_namespace_scope_p ()
11849	      && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11850	    {
11851	      /* In the declaration of a member of a template class
11852		 outside of the class itself, the SCOPE will sometimes
11853		 be a TYPENAME_TYPE.  For example, given:
11854
11855		 template <typename T>
11856		 int S<T>::R::i = 3;
11857
11858		 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11859		 this context, we must resolve S<T>::R to an ordinary
11860		 type, rather than a typename type.
11861
11862		 The reason we normally avoid resolving TYPENAME_TYPEs
11863		 is that a specialization of `S' might render
11864		 `S<T>::R' not a type.  However, if `S' is
11865		 specialized, then this `i' will not be used, so there
11866		 is no harm in resolving the types here.  */
11867	      tree type;
11868
11869	      /* Resolve the TYPENAME_TYPE.  */
11870	      type = resolve_typename_type (qualifying_scope,
11871					    /*only_current_p=*/false);
11872	      /* If that failed, the declarator is invalid.  */
11873	      if (type == error_mark_node)
11874		error ("%<%T::%D%> is not a type",
11875		       TYPE_CONTEXT (qualifying_scope),
11876		       TYPE_IDENTIFIER (qualifying_scope));
11877	      qualifying_scope = type;
11878	    }
11879
11880	  sfk = sfk_none;
11881	  if (unqualified_name)
11882	    {
11883	      tree class_type;
11884
11885	      if (qualifying_scope
11886		  && CLASS_TYPE_P (qualifying_scope))
11887		class_type = qualifying_scope;
11888	      else
11889		class_type = current_class_type;
11890
11891	      if (TREE_CODE (unqualified_name) == TYPE_DECL)
11892		{
11893		  tree name_type = TREE_TYPE (unqualified_name);
11894		  if (class_type && same_type_p (name_type, class_type))
11895		    {
11896		      if (qualifying_scope
11897			  && CLASSTYPE_USE_TEMPLATE (name_type))
11898			{
11899			  error ("invalid use of constructor as a template");
11900			  inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11901				  "name the constructor in a qualified name",
11902				  class_type,
11903				  DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11904				  class_type, name_type);
11905			  declarator = cp_error_declarator;
11906			  break;
11907			}
11908		      else
11909			unqualified_name = constructor_name (class_type);
11910		    }
11911		  else
11912		    {
11913		      /* We do not attempt to print the declarator
11914			 here because we do not have enough
11915			 information about its original syntactic
11916			 form.  */
11917		      cp_parser_error (parser, "invalid declarator");
11918		      declarator = cp_error_declarator;
11919		      break;
11920		    }
11921		}
11922
11923	      if (class_type)
11924		{
11925		  if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11926		    sfk = sfk_destructor;
11927		  else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11928		    sfk = sfk_conversion;
11929		  else if (/* There's no way to declare a constructor
11930			      for an anonymous type, even if the type
11931			      got a name for linkage purposes.  */
11932			   !TYPE_WAS_ANONYMOUS (class_type)
11933			   && constructor_name_p (unqualified_name,
11934						  class_type))
11935		    {
11936		      unqualified_name = constructor_name (class_type);
11937		      sfk = sfk_constructor;
11938		    }
11939
11940		  if (ctor_dtor_or_conv_p && sfk != sfk_none)
11941		    *ctor_dtor_or_conv_p = -1;
11942		}
11943	    }
11944	  declarator = make_id_declarator (qualifying_scope,
11945					   unqualified_name,
11946					   sfk);
11947	  declarator->id_loc = token->location;
11948
11949	handle_declarator:;
11950	  scope = get_scope_of_declarator (declarator);
11951	  if (scope)
11952	    /* Any names that appear after the declarator-id for a
11953	       member are looked up in the containing scope.  */
11954	    pushed_scope = push_scope (scope);
11955	  parser->in_declarator_p = true;
11956	  if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11957	      || (declarator && declarator->kind == cdk_id))
11958	    /* Default args are only allowed on function
11959	       declarations.  */
11960	    parser->default_arg_ok_p = saved_default_arg_ok_p;
11961	  else
11962	    parser->default_arg_ok_p = false;
11963
11964	  first = false;
11965	}
11966      /* We're done.  */
11967      else
11968	break;
11969    }
11970
11971  /* For an abstract declarator, we might wind up with nothing at this
11972     point.  That's an error; the declarator is not optional.  */
11973  if (!declarator)
11974    cp_parser_error (parser, "expected declarator");
11975
11976  /* If we entered a scope, we must exit it now.  */
11977  if (pushed_scope)
11978    pop_scope (pushed_scope);
11979
11980  parser->default_arg_ok_p = saved_default_arg_ok_p;
11981  parser->in_declarator_p = saved_in_declarator_p;
11982
11983  return declarator;
11984}
11985
11986/* Parse a ptr-operator.
11987
11988   ptr-operator:
11989     * cv-qualifier-seq [opt]
11990     &
11991     :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11992
11993   GNU Extension:
11994
11995   ptr-operator:
11996     & cv-qualifier-seq [opt]
11997
11998   Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11999   Returns ADDR_EXPR if a reference was used.  In the case of a
12000   pointer-to-member, *TYPE is filled in with the TYPE containing the
12001   member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
12002   TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
12003   ERROR_MARK if an error occurred.  */
12004
12005static enum tree_code
12006cp_parser_ptr_operator (cp_parser* parser,
12007			tree* type,
12008			cp_cv_quals *cv_quals)
12009{
12010  enum tree_code code = ERROR_MARK;
12011  cp_token *token;
12012
12013  /* Assume that it's not a pointer-to-member.  */
12014  *type = NULL_TREE;
12015  /* And that there are no cv-qualifiers.  */
12016  *cv_quals = TYPE_UNQUALIFIED;
12017
12018  /* Peek at the next token.  */
12019  token = cp_lexer_peek_token (parser->lexer);
12020  /* If it's a `*' or `&' we have a pointer or reference.  */
12021  if (token->type == CPP_MULT || token->type == CPP_AND)
12022    {
12023      /* Remember which ptr-operator we were processing.  */
12024      code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12025
12026      /* Consume the `*' or `&'.  */
12027      cp_lexer_consume_token (parser->lexer);
12028
12029      /* A `*' can be followed by a cv-qualifier-seq, and so can a
12030	 `&', if we are allowing GNU extensions.  (The only qualifier
12031	 that can legally appear after `&' is `restrict', but that is
12032	 enforced during semantic analysis.  */
12033      if (code == INDIRECT_REF
12034	  || cp_parser_allow_gnu_extensions_p (parser))
12035	*cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12036    }
12037  else
12038    {
12039      /* Try the pointer-to-member case.  */
12040      cp_parser_parse_tentatively (parser);
12041      /* Look for the optional `::' operator.  */
12042      cp_parser_global_scope_opt (parser,
12043				  /*current_scope_valid_p=*/false);
12044      /* Look for the nested-name specifier.  */
12045      cp_parser_nested_name_specifier (parser,
12046				       /*typename_keyword_p=*/false,
12047				       /*check_dependency_p=*/true,
12048				       /*type_p=*/false,
12049				       /*is_declaration=*/false);
12050      /* If we found it, and the next token is a `*', then we are
12051	 indeed looking at a pointer-to-member operator.  */
12052      if (!cp_parser_error_occurred (parser)
12053	  && cp_parser_require (parser, CPP_MULT, "`*'"))
12054	{
12055	  /* Indicate that the `*' operator was used.  */
12056	  code = INDIRECT_REF;
12057
12058	  if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12059	    error ("%qD is a namespace", parser->scope);
12060	  else
12061	    {
12062	      /* The type of which the member is a member is given by the
12063		 current SCOPE.  */
12064	      *type = parser->scope;
12065	      /* The next name will not be qualified.  */
12066	      parser->scope = NULL_TREE;
12067	      parser->qualifying_scope = NULL_TREE;
12068	      parser->object_scope = NULL_TREE;
12069	      /* Look for the optional cv-qualifier-seq.  */
12070	      *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12071	    }
12072	}
12073      /* If that didn't work we don't have a ptr-operator.  */
12074      if (!cp_parser_parse_definitely (parser))
12075	cp_parser_error (parser, "expected ptr-operator");
12076    }
12077
12078  return code;
12079}
12080
12081/* Parse an (optional) cv-qualifier-seq.
12082
12083   cv-qualifier-seq:
12084     cv-qualifier cv-qualifier-seq [opt]
12085
12086   cv-qualifier:
12087     const
12088     volatile
12089
12090   GNU Extension:
12091
12092   cv-qualifier:
12093     __restrict__
12094
12095   Returns a bitmask representing the cv-qualifiers.  */
12096
12097static cp_cv_quals
12098cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12099{
12100  cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12101
12102  while (true)
12103    {
12104      cp_token *token;
12105      cp_cv_quals cv_qualifier;
12106
12107      /* Peek at the next token.  */
12108      token = cp_lexer_peek_token (parser->lexer);
12109      /* See if it's a cv-qualifier.  */
12110      switch (token->keyword)
12111	{
12112	case RID_CONST:
12113	  cv_qualifier = TYPE_QUAL_CONST;
12114	  break;
12115
12116	case RID_VOLATILE:
12117	  cv_qualifier = TYPE_QUAL_VOLATILE;
12118	  break;
12119
12120	case RID_RESTRICT:
12121	  cv_qualifier = TYPE_QUAL_RESTRICT;
12122	  break;
12123
12124	default:
12125	  cv_qualifier = TYPE_UNQUALIFIED;
12126	  break;
12127	}
12128
12129      if (!cv_qualifier)
12130	break;
12131
12132      if (cv_quals & cv_qualifier)
12133	{
12134	  error ("duplicate cv-qualifier");
12135	  cp_lexer_purge_token (parser->lexer);
12136	}
12137      else
12138	{
12139	  cp_lexer_consume_token (parser->lexer);
12140	  cv_quals |= cv_qualifier;
12141	}
12142    }
12143
12144  return cv_quals;
12145}
12146
12147/* Parse a declarator-id.
12148
12149   declarator-id:
12150     id-expression
12151     :: [opt] nested-name-specifier [opt] type-name
12152
12153   In the `id-expression' case, the value returned is as for
12154   cp_parser_id_expression if the id-expression was an unqualified-id.
12155   If the id-expression was a qualified-id, then a SCOPE_REF is
12156   returned.  The first operand is the scope (either a NAMESPACE_DECL
12157   or TREE_TYPE), but the second is still just a representation of an
12158   unqualified-id.  */
12159
12160static tree
12161cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12162{
12163  tree id;
12164  /* The expression must be an id-expression.  Assume that qualified
12165     names are the names of types so that:
12166
12167       template <class T>
12168       int S<T>::R::i = 3;
12169
12170     will work; we must treat `S<T>::R' as the name of a type.
12171     Similarly, assume that qualified names are templates, where
12172     required, so that:
12173
12174       template <class T>
12175       int S<T>::R<T>::i = 3;
12176
12177     will work, too.  */
12178  id = cp_parser_id_expression (parser,
12179				/*template_keyword_p=*/false,
12180				/*check_dependency_p=*/false,
12181				/*template_p=*/NULL,
12182				/*declarator_p=*/true,
12183				optional_p);
12184  if (id && BASELINK_P (id))
12185    id = BASELINK_FUNCTIONS (id);
12186  return id;
12187}
12188
12189/* Parse a type-id.
12190
12191   type-id:
12192     type-specifier-seq abstract-declarator [opt]
12193
12194   Returns the TYPE specified.  */
12195
12196static tree
12197cp_parser_type_id (cp_parser* parser)
12198{
12199  cp_decl_specifier_seq type_specifier_seq;
12200  cp_declarator *abstract_declarator;
12201
12202  /* Parse the type-specifier-seq.  */
12203  cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12204				&type_specifier_seq);
12205  if (type_specifier_seq.type == error_mark_node)
12206    return error_mark_node;
12207
12208  /* There might or might not be an abstract declarator.  */
12209  cp_parser_parse_tentatively (parser);
12210  /* Look for the declarator.  */
12211  abstract_declarator
12212    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12213			    /*parenthesized_p=*/NULL,
12214			    /*member_p=*/false);
12215  /* Check to see if there really was a declarator.  */
12216  if (!cp_parser_parse_definitely (parser))
12217    abstract_declarator = NULL;
12218
12219  return groktypename (&type_specifier_seq, abstract_declarator);
12220}
12221
12222/* Parse a type-specifier-seq.
12223
12224   type-specifier-seq:
12225     type-specifier type-specifier-seq [opt]
12226
12227   GNU extension:
12228
12229   type-specifier-seq:
12230     attributes type-specifier-seq [opt]
12231
12232   If IS_CONDITION is true, we are at the start of a "condition",
12233   e.g., we've just seen "if (".
12234
12235   Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
12236
12237static void
12238cp_parser_type_specifier_seq (cp_parser* parser,
12239			      bool is_condition,
12240			      cp_decl_specifier_seq *type_specifier_seq)
12241{
12242  bool seen_type_specifier = false;
12243  cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12244
12245  /* Clear the TYPE_SPECIFIER_SEQ.  */
12246  clear_decl_specs (type_specifier_seq);
12247
12248  /* Parse the type-specifiers and attributes.  */
12249  while (true)
12250    {
12251      tree type_specifier;
12252      bool is_cv_qualifier;
12253
12254      /* Check for attributes first.  */
12255      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12256	{
12257	  type_specifier_seq->attributes =
12258	    chainon (type_specifier_seq->attributes,
12259		     cp_parser_attributes_opt (parser));
12260	  continue;
12261	}
12262
12263      /* Look for the type-specifier.  */
12264      type_specifier = cp_parser_type_specifier (parser,
12265						 flags,
12266						 type_specifier_seq,
12267						 /*is_declaration=*/false,
12268						 NULL,
12269						 &is_cv_qualifier);
12270      if (!type_specifier)
12271	{
12272	  /* If the first type-specifier could not be found, this is not a
12273	     type-specifier-seq at all.  */
12274	  if (!seen_type_specifier)
12275	    {
12276	      cp_parser_error (parser, "expected type-specifier");
12277	      type_specifier_seq->type = error_mark_node;
12278	      return;
12279	    }
12280	  /* If subsequent type-specifiers could not be found, the
12281	     type-specifier-seq is complete.  */
12282	  break;
12283	}
12284
12285      seen_type_specifier = true;
12286      /* The standard says that a condition can be:
12287
12288	    type-specifier-seq declarator = assignment-expression
12289
12290	 However, given:
12291
12292	   struct S {};
12293	   if (int S = ...)
12294
12295	 we should treat the "S" as a declarator, not as a
12296	 type-specifier.  The standard doesn't say that explicitly for
12297	 type-specifier-seq, but it does say that for
12298	 decl-specifier-seq in an ordinary declaration.  Perhaps it
12299	 would be clearer just to allow a decl-specifier-seq here, and
12300	 then add a semantic restriction that if any decl-specifiers
12301	 that are not type-specifiers appear, the program is invalid.  */
12302      if (is_condition && !is_cv_qualifier)
12303	flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12304    }
12305
12306  cp_parser_check_decl_spec (type_specifier_seq);
12307}
12308
12309/* Parse a parameter-declaration-clause.
12310
12311   parameter-declaration-clause:
12312     parameter-declaration-list [opt] ... [opt]
12313     parameter-declaration-list , ...
12314
12315   Returns a representation for the parameter declarations.  A return
12316   value of NULL indicates a parameter-declaration-clause consisting
12317   only of an ellipsis.  */
12318
12319static cp_parameter_declarator *
12320cp_parser_parameter_declaration_clause (cp_parser* parser)
12321{
12322  cp_parameter_declarator *parameters;
12323  cp_token *token;
12324  bool ellipsis_p;
12325  bool is_error;
12326
12327  /* Peek at the next token.  */
12328  token = cp_lexer_peek_token (parser->lexer);
12329  /* Check for trivial parameter-declaration-clauses.  */
12330  if (token->type == CPP_ELLIPSIS)
12331    {
12332      /* Consume the `...' token.  */
12333      cp_lexer_consume_token (parser->lexer);
12334      return NULL;
12335    }
12336  else if (token->type == CPP_CLOSE_PAREN)
12337    /* There are no parameters.  */
12338    {
12339#ifndef NO_IMPLICIT_EXTERN_C
12340      if (in_system_header && current_class_type == NULL
12341	  && current_lang_name == lang_name_c)
12342	return NULL;
12343      else
12344#endif
12345	return no_parameters;
12346    }
12347  /* Check for `(void)', too, which is a special case.  */
12348  else if (token->keyword == RID_VOID
12349	   && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12350	       == CPP_CLOSE_PAREN))
12351    {
12352      /* Consume the `void' token.  */
12353      cp_lexer_consume_token (parser->lexer);
12354      /* There are no parameters.  */
12355      return no_parameters;
12356    }
12357
12358  /* Parse the parameter-declaration-list.  */
12359  parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12360  /* If a parse error occurred while parsing the
12361     parameter-declaration-list, then the entire
12362     parameter-declaration-clause is erroneous.  */
12363  if (is_error)
12364    return NULL;
12365
12366  /* Peek at the next token.  */
12367  token = cp_lexer_peek_token (parser->lexer);
12368  /* If it's a `,', the clause should terminate with an ellipsis.  */
12369  if (token->type == CPP_COMMA)
12370    {
12371      /* Consume the `,'.  */
12372      cp_lexer_consume_token (parser->lexer);
12373      /* Expect an ellipsis.  */
12374      ellipsis_p
12375	= (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12376    }
12377  /* It might also be `...' if the optional trailing `,' was
12378     omitted.  */
12379  else if (token->type == CPP_ELLIPSIS)
12380    {
12381      /* Consume the `...' token.  */
12382      cp_lexer_consume_token (parser->lexer);
12383      /* And remember that we saw it.  */
12384      ellipsis_p = true;
12385    }
12386  else
12387    ellipsis_p = false;
12388
12389  /* Finish the parameter list.  */
12390  if (parameters && ellipsis_p)
12391    parameters->ellipsis_p = true;
12392
12393  return parameters;
12394}
12395
12396/* Parse a parameter-declaration-list.
12397
12398   parameter-declaration-list:
12399     parameter-declaration
12400     parameter-declaration-list , parameter-declaration
12401
12402   Returns a representation of the parameter-declaration-list, as for
12403   cp_parser_parameter_declaration_clause.  However, the
12404   `void_list_node' is never appended to the list.  Upon return,
12405   *IS_ERROR will be true iff an error occurred.  */
12406
12407static cp_parameter_declarator *
12408cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12409{
12410  cp_parameter_declarator *parameters = NULL;
12411  cp_parameter_declarator **tail = &parameters;
12412  bool saved_in_unbraced_linkage_specification_p;
12413
12414  /* Assume all will go well.  */
12415  *is_error = false;
12416  /* The special considerations that apply to a function within an
12417     unbraced linkage specifications do not apply to the parameters
12418     to the function.  */
12419  saved_in_unbraced_linkage_specification_p
12420    = parser->in_unbraced_linkage_specification_p;
12421  parser->in_unbraced_linkage_specification_p = false;
12422
12423  /* Look for more parameters.  */
12424  while (true)
12425    {
12426      cp_parameter_declarator *parameter;
12427      bool parenthesized_p;
12428      /* Parse the parameter.  */
12429      parameter
12430	= cp_parser_parameter_declaration (parser,
12431					   /*template_parm_p=*/false,
12432					   &parenthesized_p);
12433
12434      /* If a parse error occurred parsing the parameter declaration,
12435	 then the entire parameter-declaration-list is erroneous.  */
12436      if (!parameter)
12437	{
12438	  *is_error = true;
12439	  parameters = NULL;
12440	  break;
12441	}
12442      /* Add the new parameter to the list.  */
12443      *tail = parameter;
12444      tail = &parameter->next;
12445
12446      /* Peek at the next token.  */
12447      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12448	  || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12449	  /* These are for Objective-C++ */
12450	  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12451	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12452	/* The parameter-declaration-list is complete.  */
12453	break;
12454      else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12455	{
12456	  cp_token *token;
12457
12458	  /* Peek at the next token.  */
12459	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
12460	  /* If it's an ellipsis, then the list is complete.  */
12461	  if (token->type == CPP_ELLIPSIS)
12462	    break;
12463	  /* Otherwise, there must be more parameters.  Consume the
12464	     `,'.  */
12465	  cp_lexer_consume_token (parser->lexer);
12466	  /* When parsing something like:
12467
12468		int i(float f, double d)
12469
12470	     we can tell after seeing the declaration for "f" that we
12471	     are not looking at an initialization of a variable "i",
12472	     but rather at the declaration of a function "i".
12473
12474	     Due to the fact that the parsing of template arguments
12475	     (as specified to a template-id) requires backtracking we
12476	     cannot use this technique when inside a template argument
12477	     list.  */
12478	  if (!parser->in_template_argument_list_p
12479	      && !parser->in_type_id_in_expr_p
12480	      && cp_parser_uncommitted_to_tentative_parse_p (parser)
12481	      /* However, a parameter-declaration of the form
12482		 "foat(f)" (which is a valid declaration of a
12483		 parameter "f") can also be interpreted as an
12484		 expression (the conversion of "f" to "float").  */
12485	      && !parenthesized_p)
12486	    cp_parser_commit_to_tentative_parse (parser);
12487	}
12488      else
12489	{
12490	  cp_parser_error (parser, "expected %<,%> or %<...%>");
12491	  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12492	    cp_parser_skip_to_closing_parenthesis (parser,
12493						   /*recovering=*/true,
12494						   /*or_comma=*/false,
12495						   /*consume_paren=*/false);
12496	  break;
12497	}
12498    }
12499
12500  parser->in_unbraced_linkage_specification_p
12501    = saved_in_unbraced_linkage_specification_p;
12502
12503  return parameters;
12504}
12505
12506/* Parse a parameter declaration.
12507
12508   parameter-declaration:
12509     decl-specifier-seq declarator
12510     decl-specifier-seq declarator = assignment-expression
12511     decl-specifier-seq abstract-declarator [opt]
12512     decl-specifier-seq abstract-declarator [opt] = assignment-expression
12513
12514   If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12515   declares a template parameter.  (In that case, a non-nested `>'
12516   token encountered during the parsing of the assignment-expression
12517   is not interpreted as a greater-than operator.)
12518
12519   Returns a representation of the parameter, or NULL if an error
12520   occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12521   true iff the declarator is of the form "(p)".  */
12522
12523static cp_parameter_declarator *
12524cp_parser_parameter_declaration (cp_parser *parser,
12525				 bool template_parm_p,
12526				 bool *parenthesized_p)
12527{
12528  int declares_class_or_enum;
12529  bool greater_than_is_operator_p;
12530  cp_decl_specifier_seq decl_specifiers;
12531  cp_declarator *declarator;
12532  tree default_argument;
12533  cp_token *token;
12534  const char *saved_message;
12535
12536  /* In a template parameter, `>' is not an operator.
12537
12538     [temp.param]
12539
12540     When parsing a default template-argument for a non-type
12541     template-parameter, the first non-nested `>' is taken as the end
12542     of the template parameter-list rather than a greater-than
12543     operator.  */
12544  greater_than_is_operator_p = !template_parm_p;
12545
12546  /* Type definitions may not appear in parameter types.  */
12547  saved_message = parser->type_definition_forbidden_message;
12548  parser->type_definition_forbidden_message
12549    = "types may not be defined in parameter types";
12550
12551  /* Parse the declaration-specifiers.  */
12552  cp_parser_decl_specifier_seq (parser,
12553				CP_PARSER_FLAGS_NONE,
12554				&decl_specifiers,
12555				&declares_class_or_enum);
12556  /* If an error occurred, there's no reason to attempt to parse the
12557     rest of the declaration.  */
12558  if (cp_parser_error_occurred (parser))
12559    {
12560      parser->type_definition_forbidden_message = saved_message;
12561      return NULL;
12562    }
12563
12564  /* Peek at the next token.  */
12565  token = cp_lexer_peek_token (parser->lexer);
12566  /* If the next token is a `)', `,', `=', `>', or `...', then there
12567     is no declarator.  */
12568  if (token->type == CPP_CLOSE_PAREN
12569      || token->type == CPP_COMMA
12570      || token->type == CPP_EQ
12571      || token->type == CPP_ELLIPSIS
12572      || token->type == CPP_GREATER)
12573    {
12574      declarator = NULL;
12575      if (parenthesized_p)
12576	*parenthesized_p = false;
12577    }
12578  /* Otherwise, there should be a declarator.  */
12579  else
12580    {
12581      bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12582      parser->default_arg_ok_p = false;
12583
12584      /* After seeing a decl-specifier-seq, if the next token is not a
12585	 "(", there is no possibility that the code is a valid
12586	 expression.  Therefore, if parsing tentatively, we commit at
12587	 this point.  */
12588      if (!parser->in_template_argument_list_p
12589	  /* In an expression context, having seen:
12590
12591	       (int((char ...
12592
12593	     we cannot be sure whether we are looking at a
12594	     function-type (taking a "char" as a parameter) or a cast
12595	     of some object of type "char" to "int".  */
12596	  && !parser->in_type_id_in_expr_p
12597	  && cp_parser_uncommitted_to_tentative_parse_p (parser)
12598	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12599	cp_parser_commit_to_tentative_parse (parser);
12600      /* Parse the declarator.  */
12601      declarator = cp_parser_declarator (parser,
12602					 CP_PARSER_DECLARATOR_EITHER,
12603					 /*ctor_dtor_or_conv_p=*/NULL,
12604					 parenthesized_p,
12605					 /*member_p=*/false);
12606      parser->default_arg_ok_p = saved_default_arg_ok_p;
12607      /* After the declarator, allow more attributes.  */
12608      decl_specifiers.attributes
12609	= chainon (decl_specifiers.attributes,
12610		   cp_parser_attributes_opt (parser));
12611    }
12612
12613  /* The restriction on defining new types applies only to the type
12614     of the parameter, not to the default argument.  */
12615  parser->type_definition_forbidden_message = saved_message;
12616
12617  /* If the next token is `=', then process a default argument.  */
12618  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12619    {
12620      bool saved_greater_than_is_operator_p;
12621      /* Consume the `='.  */
12622      cp_lexer_consume_token (parser->lexer);
12623
12624      /* If we are defining a class, then the tokens that make up the
12625	 default argument must be saved and processed later.  */
12626      if (!template_parm_p && at_class_scope_p ()
12627	  && TYPE_BEING_DEFINED (current_class_type))
12628	{
12629	  unsigned depth = 0;
12630	  cp_token *first_token;
12631	  cp_token *token;
12632
12633	  /* Add tokens until we have processed the entire default
12634	     argument.  We add the range [first_token, token).  */
12635	  first_token = cp_lexer_peek_token (parser->lexer);
12636	  while (true)
12637	    {
12638	      bool done = false;
12639
12640	      /* Peek at the next token.  */
12641	      token = cp_lexer_peek_token (parser->lexer);
12642	      /* What we do depends on what token we have.  */
12643	      switch (token->type)
12644		{
12645		  /* In valid code, a default argument must be
12646		     immediately followed by a `,' `)', or `...'.  */
12647		case CPP_COMMA:
12648		case CPP_CLOSE_PAREN:
12649		case CPP_ELLIPSIS:
12650		  /* If we run into a non-nested `;', `}', or `]',
12651		     then the code is invalid -- but the default
12652		     argument is certainly over.  */
12653		case CPP_SEMICOLON:
12654		case CPP_CLOSE_BRACE:
12655		case CPP_CLOSE_SQUARE:
12656		  if (depth == 0)
12657		    done = true;
12658		  /* Update DEPTH, if necessary.  */
12659		  else if (token->type == CPP_CLOSE_PAREN
12660			   || token->type == CPP_CLOSE_BRACE
12661			   || token->type == CPP_CLOSE_SQUARE)
12662		    --depth;
12663		  break;
12664
12665		case CPP_OPEN_PAREN:
12666		case CPP_OPEN_SQUARE:
12667		case CPP_OPEN_BRACE:
12668		  ++depth;
12669		  break;
12670
12671		case CPP_GREATER:
12672		  /* If we see a non-nested `>', and `>' is not an
12673		     operator, then it marks the end of the default
12674		     argument.  */
12675		  if (!depth && !greater_than_is_operator_p)
12676		    done = true;
12677		  break;
12678
12679		  /* If we run out of tokens, issue an error message.  */
12680		case CPP_EOF:
12681		case CPP_PRAGMA_EOL:
12682		  error ("file ends in default argument");
12683		  done = true;
12684		  break;
12685
12686		case CPP_NAME:
12687		case CPP_SCOPE:
12688		  /* In these cases, we should look for template-ids.
12689		     For example, if the default argument is
12690		     `X<int, double>()', we need to do name lookup to
12691		     figure out whether or not `X' is a template; if
12692		     so, the `,' does not end the default argument.
12693
12694		     That is not yet done.  */
12695		  break;
12696
12697		default:
12698		  break;
12699		}
12700
12701	      /* If we've reached the end, stop.  */
12702	      if (done)
12703		break;
12704
12705	      /* Add the token to the token block.  */
12706	      token = cp_lexer_consume_token (parser->lexer);
12707	    }
12708
12709	  /* Create a DEFAULT_ARG to represented the unparsed default
12710	     argument.  */
12711	  default_argument = make_node (DEFAULT_ARG);
12712	  DEFARG_TOKENS (default_argument)
12713	    = cp_token_cache_new (first_token, token);
12714	  DEFARG_INSTANTIATIONS (default_argument) = NULL;
12715	}
12716      /* Outside of a class definition, we can just parse the
12717	 assignment-expression.  */
12718      else
12719	{
12720	  bool saved_local_variables_forbidden_p;
12721
12722	  /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12723	     set correctly.  */
12724	  saved_greater_than_is_operator_p
12725	    = parser->greater_than_is_operator_p;
12726	  parser->greater_than_is_operator_p = greater_than_is_operator_p;
12727	  /* Local variable names (and the `this' keyword) may not
12728	     appear in a default argument.  */
12729	  saved_local_variables_forbidden_p
12730	    = parser->local_variables_forbidden_p;
12731	  parser->local_variables_forbidden_p = true;
12732	  /* The default argument expression may cause implicitly
12733	     defined member functions to be synthesized, which will
12734	     result in garbage collection.  We must treat this
12735	     situation as if we were within the body of function so as
12736	     to avoid collecting live data on the stack.  */
12737	  ++function_depth;
12738	  /* Parse the assignment-expression.  */
12739	  if (template_parm_p)
12740	    push_deferring_access_checks (dk_no_deferred);
12741	  default_argument
12742	    = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12743	  if (template_parm_p)
12744	    pop_deferring_access_checks ();
12745	  /* Restore saved state.  */
12746	  --function_depth;
12747	  parser->greater_than_is_operator_p
12748	    = saved_greater_than_is_operator_p;
12749	  parser->local_variables_forbidden_p
12750	    = saved_local_variables_forbidden_p;
12751	}
12752      if (!parser->default_arg_ok_p)
12753	{
12754	  if (!flag_pedantic_errors)
12755	    warning (0, "deprecated use of default argument for parameter of non-function");
12756	  else
12757	    {
12758	      error ("default arguments are only permitted for function parameters");
12759	      default_argument = NULL_TREE;
12760	    }
12761	}
12762    }
12763  else
12764    default_argument = NULL_TREE;
12765
12766  return make_parameter_declarator (&decl_specifiers,
12767				    declarator,
12768				    default_argument);
12769}
12770
12771/* Parse a function-body.
12772
12773   function-body:
12774     compound_statement  */
12775
12776static void
12777cp_parser_function_body (cp_parser *parser)
12778{
12779  cp_parser_compound_statement (parser, NULL, false);
12780}
12781
12782/* Parse a ctor-initializer-opt followed by a function-body.  Return
12783   true if a ctor-initializer was present.  */
12784
12785static bool
12786cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12787{
12788  tree body;
12789  bool ctor_initializer_p;
12790
12791  /* Begin the function body.  */
12792  body = begin_function_body ();
12793  /* Parse the optional ctor-initializer.  */
12794  ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12795  /* Parse the function-body.  */
12796  cp_parser_function_body (parser);
12797  /* Finish the function body.  */
12798  finish_function_body (body);
12799
12800  return ctor_initializer_p;
12801}
12802
12803/* Parse an initializer.
12804
12805   initializer:
12806     = initializer-clause
12807     ( expression-list )
12808
12809   Returns an expression representing the initializer.  If no
12810   initializer is present, NULL_TREE is returned.
12811
12812   *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12813   production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12814   set to FALSE if there is no initializer present.  If there is an
12815   initializer, and it is not a constant-expression, *NON_CONSTANT_P
12816   is set to true; otherwise it is set to false.  */
12817
12818static tree
12819cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12820		       bool* non_constant_p)
12821{
12822  cp_token *token;
12823  tree init;
12824
12825  /* Peek at the next token.  */
12826  token = cp_lexer_peek_token (parser->lexer);
12827
12828  /* Let our caller know whether or not this initializer was
12829     parenthesized.  */
12830  *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12831  /* Assume that the initializer is constant.  */
12832  *non_constant_p = false;
12833
12834  if (token->type == CPP_EQ)
12835    {
12836      /* Consume the `='.  */
12837      cp_lexer_consume_token (parser->lexer);
12838      /* Parse the initializer-clause.  */
12839      init = cp_parser_initializer_clause (parser, non_constant_p);
12840    }
12841  else if (token->type == CPP_OPEN_PAREN)
12842    init = cp_parser_parenthesized_expression_list (parser, false,
12843						    /*cast_p=*/false,
12844						    non_constant_p);
12845  else
12846    {
12847      /* Anything else is an error.  */
12848      cp_parser_error (parser, "expected initializer");
12849      init = error_mark_node;
12850    }
12851
12852  return init;
12853}
12854
12855/* Parse an initializer-clause.
12856
12857   initializer-clause:
12858     assignment-expression
12859     { initializer-list , [opt] }
12860     { }
12861
12862   Returns an expression representing the initializer.
12863
12864   If the `assignment-expression' production is used the value
12865   returned is simply a representation for the expression.
12866
12867   Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12868   the elements of the initializer-list (or NULL, if the last
12869   production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12870   NULL_TREE.  There is no way to detect whether or not the optional
12871   trailing `,' was provided.  NON_CONSTANT_P is as for
12872   cp_parser_initializer.  */
12873
12874static tree
12875cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12876{
12877  tree initializer;
12878
12879  /* Assume the expression is constant.  */
12880  *non_constant_p = false;
12881
12882  /* If it is not a `{', then we are looking at an
12883     assignment-expression.  */
12884  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12885    {
12886      initializer
12887	= cp_parser_constant_expression (parser,
12888					/*allow_non_constant_p=*/true,
12889					non_constant_p);
12890      if (!*non_constant_p)
12891	initializer = fold_non_dependent_expr (initializer);
12892    }
12893  else
12894    {
12895      /* Consume the `{' token.  */
12896      cp_lexer_consume_token (parser->lexer);
12897      /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12898      initializer = make_node (CONSTRUCTOR);
12899      /* If it's not a `}', then there is a non-trivial initializer.  */
12900      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12901	{
12902	  /* Parse the initializer list.  */
12903	  CONSTRUCTOR_ELTS (initializer)
12904	    = cp_parser_initializer_list (parser, non_constant_p);
12905	  /* A trailing `,' token is allowed.  */
12906	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12907	    cp_lexer_consume_token (parser->lexer);
12908	}
12909      /* Now, there should be a trailing `}'.  */
12910      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12911    }
12912
12913  return initializer;
12914}
12915
12916/* Parse an initializer-list.
12917
12918   initializer-list:
12919     initializer-clause
12920     initializer-list , initializer-clause
12921
12922   GNU Extension:
12923
12924   initializer-list:
12925     identifier : initializer-clause
12926     initializer-list, identifier : initializer-clause
12927
12928   Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12929   for the initializer.  If the INDEX of the elt is non-NULL, it is the
12930   IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12931   as for cp_parser_initializer.  */
12932
12933static VEC(constructor_elt,gc) *
12934cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12935{
12936  VEC(constructor_elt,gc) *v = NULL;
12937
12938  /* Assume all of the expressions are constant.  */
12939  *non_constant_p = false;
12940
12941  /* Parse the rest of the list.  */
12942  while (true)
12943    {
12944      cp_token *token;
12945      tree identifier;
12946      tree initializer;
12947      bool clause_non_constant_p;
12948
12949      /* If the next token is an identifier and the following one is a
12950	 colon, we are looking at the GNU designated-initializer
12951	 syntax.  */
12952      if (cp_parser_allow_gnu_extensions_p (parser)
12953	  && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12954	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12955	{
12956	  /* Warn the user that they are using an extension.  */
12957	  if (pedantic)
12958	    pedwarn ("ISO C++ does not allow designated initializers");
12959	  /* Consume the identifier.  */
12960	  identifier = cp_lexer_consume_token (parser->lexer)->u.value;
12961	  /* Consume the `:'.  */
12962	  cp_lexer_consume_token (parser->lexer);
12963	}
12964      else
12965	identifier = NULL_TREE;
12966
12967      /* Parse the initializer.  */
12968      initializer = cp_parser_initializer_clause (parser,
12969						  &clause_non_constant_p);
12970      /* If any clause is non-constant, so is the entire initializer.  */
12971      if (clause_non_constant_p)
12972	*non_constant_p = true;
12973
12974      /* Add it to the vector.  */
12975      CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12976
12977      /* If the next token is not a comma, we have reached the end of
12978	 the list.  */
12979      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12980	break;
12981
12982      /* Peek at the next token.  */
12983      token = cp_lexer_peek_nth_token (parser->lexer, 2);
12984      /* If the next token is a `}', then we're still done.  An
12985	 initializer-clause can have a trailing `,' after the
12986	 initializer-list and before the closing `}'.  */
12987      if (token->type == CPP_CLOSE_BRACE)
12988	break;
12989
12990      /* Consume the `,' token.  */
12991      cp_lexer_consume_token (parser->lexer);
12992    }
12993
12994  return v;
12995}
12996
12997/* Classes [gram.class] */
12998
12999/* Parse a class-name.
13000
13001   class-name:
13002     identifier
13003     template-id
13004
13005   TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13006   to indicate that names looked up in dependent types should be
13007   assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
13008   keyword has been used to indicate that the name that appears next
13009   is a template.  TAG_TYPE indicates the explicit tag given before
13010   the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
13011   looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
13012   is the class being defined in a class-head.
13013
13014   Returns the TYPE_DECL representing the class.  */
13015
13016static tree
13017cp_parser_class_name (cp_parser *parser,
13018		      bool typename_keyword_p,
13019		      bool template_keyword_p,
13020		      enum tag_types tag_type,
13021		      bool check_dependency_p,
13022		      bool class_head_p,
13023		      bool is_declaration)
13024{
13025  tree decl;
13026  tree scope;
13027  bool typename_p;
13028  cp_token *token;
13029
13030  /* All class-names start with an identifier.  */
13031  token = cp_lexer_peek_token (parser->lexer);
13032  if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13033    {
13034      cp_parser_error (parser, "expected class-name");
13035      return error_mark_node;
13036    }
13037
13038  /* PARSER->SCOPE can be cleared when parsing the template-arguments
13039     to a template-id, so we save it here.  */
13040  scope = parser->scope;
13041  if (scope == error_mark_node)
13042    return error_mark_node;
13043
13044  /* Any name names a type if we're following the `typename' keyword
13045     in a qualified name where the enclosing scope is type-dependent.  */
13046  typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13047		&& dependent_type_p (scope));
13048  /* Handle the common case (an identifier, but not a template-id)
13049     efficiently.  */
13050  if (token->type == CPP_NAME
13051      && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13052    {
13053      cp_token *identifier_token;
13054      tree identifier;
13055      bool ambiguous_p;
13056
13057      /* Look for the identifier.  */
13058      identifier_token = cp_lexer_peek_token (parser->lexer);
13059      ambiguous_p = identifier_token->ambiguous_p;
13060      identifier = cp_parser_identifier (parser);
13061      /* If the next token isn't an identifier, we are certainly not
13062	 looking at a class-name.  */
13063      if (identifier == error_mark_node)
13064	decl = error_mark_node;
13065      /* If we know this is a type-name, there's no need to look it
13066	 up.  */
13067      else if (typename_p)
13068	decl = identifier;
13069      else
13070	{
13071	  tree ambiguous_decls;
13072	  /* If we already know that this lookup is ambiguous, then
13073	     we've already issued an error message; there's no reason
13074	     to check again.  */
13075	  if (ambiguous_p)
13076	    {
13077	      cp_parser_simulate_error (parser);
13078	      return error_mark_node;
13079	    }
13080	  /* If the next token is a `::', then the name must be a type
13081	     name.
13082
13083	     [basic.lookup.qual]
13084
13085	     During the lookup for a name preceding the :: scope
13086	     resolution operator, object, function, and enumerator
13087	     names are ignored.  */
13088	  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13089	    tag_type = typename_type;
13090	  /* Look up the name.  */
13091	  decl = cp_parser_lookup_name (parser, identifier,
13092					tag_type,
13093					/*is_template=*/false,
13094					/*is_namespace=*/false,
13095					check_dependency_p,
13096					&ambiguous_decls);
13097	  if (ambiguous_decls)
13098	    {
13099	      error ("reference to %qD is ambiguous", identifier);
13100	      print_candidates (ambiguous_decls);
13101	      if (cp_parser_parsing_tentatively (parser))
13102		{
13103		  identifier_token->ambiguous_p = true;
13104		  cp_parser_simulate_error (parser);
13105		}
13106	      return error_mark_node;
13107	    }
13108	}
13109    }
13110  else
13111    {
13112      /* Try a template-id.  */
13113      decl = cp_parser_template_id (parser, template_keyword_p,
13114				    check_dependency_p,
13115				    is_declaration);
13116      if (decl == error_mark_node)
13117	return error_mark_node;
13118    }
13119
13120  decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13121
13122  /* If this is a typename, create a TYPENAME_TYPE.  */
13123  if (typename_p && decl != error_mark_node)
13124    {
13125      decl = make_typename_type (scope, decl, typename_type,
13126				 /*complain=*/tf_error);
13127      if (decl != error_mark_node)
13128	decl = TYPE_NAME (decl);
13129    }
13130
13131  /* Check to see that it is really the name of a class.  */
13132  if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13133      && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13134      && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13135    /* Situations like this:
13136
13137	 template <typename T> struct A {
13138	   typename T::template X<int>::I i;
13139	 };
13140
13141       are problematic.  Is `T::template X<int>' a class-name?  The
13142       standard does not seem to be definitive, but there is no other
13143       valid interpretation of the following `::'.  Therefore, those
13144       names are considered class-names.  */
13145    {
13146      decl = make_typename_type (scope, decl, tag_type, tf_error);
13147      if (decl != error_mark_node)
13148	decl = TYPE_NAME (decl);
13149    }
13150  else if (TREE_CODE (decl) != TYPE_DECL
13151	   || TREE_TYPE (decl) == error_mark_node
13152	   || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13153    decl = error_mark_node;
13154
13155  if (decl == error_mark_node)
13156    cp_parser_error (parser, "expected class-name");
13157
13158  return decl;
13159}
13160
13161/* Parse a class-specifier.
13162
13163   class-specifier:
13164     class-head { member-specification [opt] }
13165
13166   Returns the TREE_TYPE representing the class.  */
13167
13168static tree
13169cp_parser_class_specifier (cp_parser* parser)
13170{
13171  cp_token *token;
13172  tree type;
13173  tree attributes = NULL_TREE;
13174  int has_trailing_semicolon;
13175  bool nested_name_specifier_p;
13176  unsigned saved_num_template_parameter_lists;
13177  bool saved_in_function_body;
13178  tree old_scope = NULL_TREE;
13179  tree scope = NULL_TREE;
13180  tree bases;
13181
13182  push_deferring_access_checks (dk_no_deferred);
13183
13184  /* Parse the class-head.  */
13185  type = cp_parser_class_head (parser,
13186			       &nested_name_specifier_p,
13187			       &attributes,
13188			       &bases);
13189  /* If the class-head was a semantic disaster, skip the entire body
13190     of the class.  */
13191  if (!type)
13192    {
13193      cp_parser_skip_to_end_of_block_or_statement (parser);
13194      pop_deferring_access_checks ();
13195      return error_mark_node;
13196    }
13197
13198  /* Look for the `{'.  */
13199  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13200    {
13201      pop_deferring_access_checks ();
13202      return error_mark_node;
13203    }
13204
13205  /* Process the base classes. If they're invalid, skip the
13206     entire class body.  */
13207  if (!xref_basetypes (type, bases))
13208    {
13209      cp_parser_skip_to_closing_brace (parser);
13210
13211      /* Consuming the closing brace yields better error messages
13212         later on.  */
13213      cp_lexer_consume_token (parser->lexer);
13214      pop_deferring_access_checks ();
13215      return error_mark_node;
13216    }
13217
13218  /* Issue an error message if type-definitions are forbidden here.  */
13219  cp_parser_check_type_definition (parser);
13220  /* Remember that we are defining one more class.  */
13221  ++parser->num_classes_being_defined;
13222  /* Inside the class, surrounding template-parameter-lists do not
13223     apply.  */
13224  saved_num_template_parameter_lists
13225    = parser->num_template_parameter_lists;
13226  parser->num_template_parameter_lists = 0;
13227  /* We are not in a function body.  */
13228  saved_in_function_body = parser->in_function_body;
13229  parser->in_function_body = false;
13230
13231  /* Start the class.  */
13232  if (nested_name_specifier_p)
13233    {
13234      scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13235      old_scope = push_inner_scope (scope);
13236    }
13237  type = begin_class_definition (type, attributes);
13238
13239  if (type == error_mark_node)
13240    /* If the type is erroneous, skip the entire body of the class.  */
13241    cp_parser_skip_to_closing_brace (parser);
13242  else
13243    /* Parse the member-specification.  */
13244    cp_parser_member_specification_opt (parser);
13245
13246  /* Look for the trailing `}'.  */
13247  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13248  /* We get better error messages by noticing a common problem: a
13249     missing trailing `;'.  */
13250  token = cp_lexer_peek_token (parser->lexer);
13251  has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13252  /* Look for trailing attributes to apply to this class.  */
13253  if (cp_parser_allow_gnu_extensions_p (parser))
13254    attributes = cp_parser_attributes_opt (parser);
13255  if (type != error_mark_node)
13256    type = finish_struct (type, attributes);
13257  if (nested_name_specifier_p)
13258    pop_inner_scope (old_scope, scope);
13259  /* If this class is not itself within the scope of another class,
13260     then we need to parse the bodies of all of the queued function
13261     definitions.  Note that the queued functions defined in a class
13262     are not always processed immediately following the
13263     class-specifier for that class.  Consider:
13264
13265       struct A {
13266	 struct B { void f() { sizeof (A); } };
13267       };
13268
13269     If `f' were processed before the processing of `A' were
13270     completed, there would be no way to compute the size of `A'.
13271     Note that the nesting we are interested in here is lexical --
13272     not the semantic nesting given by TYPE_CONTEXT.  In particular,
13273     for:
13274
13275       struct A { struct B; };
13276       struct A::B { void f() { } };
13277
13278     there is no need to delay the parsing of `A::B::f'.  */
13279  if (--parser->num_classes_being_defined == 0)
13280    {
13281      tree queue_entry;
13282      tree fn;
13283      tree class_type = NULL_TREE;
13284      tree pushed_scope = NULL_TREE;
13285
13286      /* In a first pass, parse default arguments to the functions.
13287	 Then, in a second pass, parse the bodies of the functions.
13288	 This two-phased approach handles cases like:
13289
13290	    struct S {
13291	      void f() { g(); }
13292	      void g(int i = 3);
13293	    };
13294
13295	 */
13296      for (TREE_PURPOSE (parser->unparsed_functions_queues)
13297	     = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13298	   (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13299	   TREE_PURPOSE (parser->unparsed_functions_queues)
13300	     = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13301	{
13302	  fn = TREE_VALUE (queue_entry);
13303	  /* If there are default arguments that have not yet been processed,
13304	     take care of them now.  */
13305	  if (class_type != TREE_PURPOSE (queue_entry))
13306	    {
13307	      if (pushed_scope)
13308		pop_scope (pushed_scope);
13309	      class_type = TREE_PURPOSE (queue_entry);
13310	      pushed_scope = push_scope (class_type);
13311	    }
13312	  /* Make sure that any template parameters are in scope.  */
13313	  maybe_begin_member_template_processing (fn);
13314	  /* Parse the default argument expressions.  */
13315	  cp_parser_late_parsing_default_args (parser, fn);
13316	  /* Remove any template parameters from the symbol table.  */
13317	  maybe_end_member_template_processing ();
13318	}
13319      if (pushed_scope)
13320	pop_scope (pushed_scope);
13321      /* Now parse the body of the functions.  */
13322      for (TREE_VALUE (parser->unparsed_functions_queues)
13323	     = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13324	   (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13325	   TREE_VALUE (parser->unparsed_functions_queues)
13326	     = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13327	{
13328	  /* Figure out which function we need to process.  */
13329	  fn = TREE_VALUE (queue_entry);
13330	  /* Parse the function.  */
13331	  cp_parser_late_parsing_for_member (parser, fn);
13332	}
13333    }
13334
13335  /* Put back any saved access checks.  */
13336  pop_deferring_access_checks ();
13337
13338  /* Restore saved state.  */
13339  parser->in_function_body = saved_in_function_body;
13340  parser->num_template_parameter_lists
13341    = saved_num_template_parameter_lists;
13342
13343  return type;
13344}
13345
13346/* Parse a class-head.
13347
13348   class-head:
13349     class-key identifier [opt] base-clause [opt]
13350     class-key nested-name-specifier identifier base-clause [opt]
13351     class-key nested-name-specifier [opt] template-id
13352       base-clause [opt]
13353
13354   GNU Extensions:
13355     class-key attributes identifier [opt] base-clause [opt]
13356     class-key attributes nested-name-specifier identifier base-clause [opt]
13357     class-key attributes nested-name-specifier [opt] template-id
13358       base-clause [opt]
13359
13360   Returns the TYPE of the indicated class.  Sets
13361   *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13362   involving a nested-name-specifier was used, and FALSE otherwise.
13363
13364   Returns error_mark_node if this is not a class-head.
13365
13366   Returns NULL_TREE if the class-head is syntactically valid, but
13367   semantically invalid in a way that means we should skip the entire
13368   body of the class.  */
13369
13370static tree
13371cp_parser_class_head (cp_parser* parser,
13372		      bool* nested_name_specifier_p,
13373		      tree *attributes_p,
13374		      tree *bases)
13375{
13376  tree nested_name_specifier;
13377  enum tag_types class_key;
13378  tree id = NULL_TREE;
13379  tree type = NULL_TREE;
13380  tree attributes;
13381  bool template_id_p = false;
13382  bool qualified_p = false;
13383  bool invalid_nested_name_p = false;
13384  bool invalid_explicit_specialization_p = false;
13385  tree pushed_scope = NULL_TREE;
13386  unsigned num_templates;
13387
13388  /* Assume no nested-name-specifier will be present.  */
13389  *nested_name_specifier_p = false;
13390  /* Assume no template parameter lists will be used in defining the
13391     type.  */
13392  num_templates = 0;
13393
13394  /* Look for the class-key.  */
13395  class_key = cp_parser_class_key (parser);
13396  if (class_key == none_type)
13397    return error_mark_node;
13398
13399  /* Parse the attributes.  */
13400  attributes = cp_parser_attributes_opt (parser);
13401
13402  /* If the next token is `::', that is invalid -- but sometimes
13403     people do try to write:
13404
13405       struct ::S {};
13406
13407     Handle this gracefully by accepting the extra qualifier, and then
13408     issuing an error about it later if this really is a
13409     class-head.  If it turns out just to be an elaborated type
13410     specifier, remain silent.  */
13411  if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13412    qualified_p = true;
13413
13414  push_deferring_access_checks (dk_no_check);
13415
13416  /* Determine the name of the class.  Begin by looking for an
13417     optional nested-name-specifier.  */
13418  nested_name_specifier
13419    = cp_parser_nested_name_specifier_opt (parser,
13420					   /*typename_keyword_p=*/false,
13421					   /*check_dependency_p=*/false,
13422					   /*type_p=*/false,
13423					   /*is_declaration=*/false);
13424  /* If there was a nested-name-specifier, then there *must* be an
13425     identifier.  */
13426  if (nested_name_specifier)
13427    {
13428      /* Although the grammar says `identifier', it really means
13429	 `class-name' or `template-name'.  You are only allowed to
13430	 define a class that has already been declared with this
13431	 syntax.
13432
13433	 The proposed resolution for Core Issue 180 says that wherever
13434	 you see `class T::X' you should treat `X' as a type-name.
13435
13436	 It is OK to define an inaccessible class; for example:
13437
13438	   class A { class B; };
13439	   class A::B {};
13440
13441	 We do not know if we will see a class-name, or a
13442	 template-name.  We look for a class-name first, in case the
13443	 class-name is a template-id; if we looked for the
13444	 template-name first we would stop after the template-name.  */
13445      cp_parser_parse_tentatively (parser);
13446      type = cp_parser_class_name (parser,
13447				   /*typename_keyword_p=*/false,
13448				   /*template_keyword_p=*/false,
13449				   class_type,
13450				   /*check_dependency_p=*/false,
13451				   /*class_head_p=*/true,
13452				   /*is_declaration=*/false);
13453      /* If that didn't work, ignore the nested-name-specifier.  */
13454      if (!cp_parser_parse_definitely (parser))
13455	{
13456	  invalid_nested_name_p = true;
13457	  id = cp_parser_identifier (parser);
13458	  if (id == error_mark_node)
13459	    id = NULL_TREE;
13460	}
13461      /* If we could not find a corresponding TYPE, treat this
13462	 declaration like an unqualified declaration.  */
13463      if (type == error_mark_node)
13464	nested_name_specifier = NULL_TREE;
13465      /* Otherwise, count the number of templates used in TYPE and its
13466	 containing scopes.  */
13467      else
13468	{
13469	  tree scope;
13470
13471	  for (scope = TREE_TYPE (type);
13472	       scope && TREE_CODE (scope) != NAMESPACE_DECL;
13473	       scope = (TYPE_P (scope)
13474			? TYPE_CONTEXT (scope)
13475			: DECL_CONTEXT (scope)))
13476	    if (TYPE_P (scope)
13477		&& CLASS_TYPE_P (scope)
13478		&& CLASSTYPE_TEMPLATE_INFO (scope)
13479		&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13480		&& !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13481	      ++num_templates;
13482	}
13483    }
13484  /* Otherwise, the identifier is optional.  */
13485  else
13486    {
13487      /* We don't know whether what comes next is a template-id,
13488	 an identifier, or nothing at all.  */
13489      cp_parser_parse_tentatively (parser);
13490      /* Check for a template-id.  */
13491      id = cp_parser_template_id (parser,
13492				  /*template_keyword_p=*/false,
13493				  /*check_dependency_p=*/true,
13494				  /*is_declaration=*/true);
13495      /* If that didn't work, it could still be an identifier.  */
13496      if (!cp_parser_parse_definitely (parser))
13497	{
13498	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13499	    id = cp_parser_identifier (parser);
13500	  else
13501	    id = NULL_TREE;
13502	}
13503      else
13504	{
13505	  template_id_p = true;
13506	  ++num_templates;
13507	}
13508    }
13509
13510  pop_deferring_access_checks ();
13511
13512  if (id)
13513    cp_parser_check_for_invalid_template_id (parser, id);
13514
13515  /* If it's not a `:' or a `{' then we can't really be looking at a
13516     class-head, since a class-head only appears as part of a
13517     class-specifier.  We have to detect this situation before calling
13518     xref_tag, since that has irreversible side-effects.  */
13519  if (!cp_parser_next_token_starts_class_definition_p (parser))
13520    {
13521      cp_parser_error (parser, "expected %<{%> or %<:%>");
13522      return error_mark_node;
13523    }
13524
13525  /* At this point, we're going ahead with the class-specifier, even
13526     if some other problem occurs.  */
13527  cp_parser_commit_to_tentative_parse (parser);
13528  /* Issue the error about the overly-qualified name now.  */
13529  if (qualified_p)
13530    cp_parser_error (parser,
13531		     "global qualification of class name is invalid");
13532  else if (invalid_nested_name_p)
13533    cp_parser_error (parser,
13534		     "qualified name does not name a class");
13535  else if (nested_name_specifier)
13536    {
13537      tree scope;
13538
13539      /* Reject typedef-names in class heads.  */
13540      if (!DECL_IMPLICIT_TYPEDEF_P (type))
13541	{
13542	  error ("invalid class name in declaration of %qD", type);
13543	  type = NULL_TREE;
13544	  goto done;
13545	}
13546
13547      /* Figure out in what scope the declaration is being placed.  */
13548      scope = current_scope ();
13549      /* If that scope does not contain the scope in which the
13550	 class was originally declared, the program is invalid.  */
13551      if (scope && !is_ancestor (scope, nested_name_specifier))
13552	{
13553	  error ("declaration of %qD in %qD which does not enclose %qD",
13554		 type, scope, nested_name_specifier);
13555	  type = NULL_TREE;
13556	  goto done;
13557	}
13558      /* [dcl.meaning]
13559
13560	 A declarator-id shall not be qualified exception of the
13561	 definition of a ... nested class outside of its class
13562	 ... [or] a the definition or explicit instantiation of a
13563	 class member of a namespace outside of its namespace.  */
13564      if (scope == nested_name_specifier)
13565	{
13566	  pedwarn ("extra qualification ignored");
13567	  nested_name_specifier = NULL_TREE;
13568	  num_templates = 0;
13569	}
13570    }
13571  /* An explicit-specialization must be preceded by "template <>".  If
13572     it is not, try to recover gracefully.  */
13573  if (at_namespace_scope_p ()
13574      && parser->num_template_parameter_lists == 0
13575      && template_id_p)
13576    {
13577      error ("an explicit specialization must be preceded by %<template <>%>");
13578      invalid_explicit_specialization_p = true;
13579      /* Take the same action that would have been taken by
13580	 cp_parser_explicit_specialization.  */
13581      ++parser->num_template_parameter_lists;
13582      begin_specialization ();
13583    }
13584  /* There must be no "return" statements between this point and the
13585     end of this function; set "type "to the correct return value and
13586     use "goto done;" to return.  */
13587  /* Make sure that the right number of template parameters were
13588     present.  */
13589  if (!cp_parser_check_template_parameters (parser, num_templates))
13590    {
13591      /* If something went wrong, there is no point in even trying to
13592	 process the class-definition.  */
13593      type = NULL_TREE;
13594      goto done;
13595    }
13596
13597  /* Look up the type.  */
13598  if (template_id_p)
13599    {
13600      type = TREE_TYPE (id);
13601      type = maybe_process_partial_specialization (type);
13602      if (nested_name_specifier)
13603	pushed_scope = push_scope (nested_name_specifier);
13604    }
13605  else if (nested_name_specifier)
13606    {
13607      tree class_type;
13608
13609      /* Given:
13610
13611	    template <typename T> struct S { struct T };
13612	    template <typename T> struct S<T>::T { };
13613
13614	 we will get a TYPENAME_TYPE when processing the definition of
13615	 `S::T'.  We need to resolve it to the actual type before we
13616	 try to define it.  */
13617      if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13618	{
13619	  class_type = resolve_typename_type (TREE_TYPE (type),
13620					      /*only_current_p=*/false);
13621	  if (class_type != error_mark_node)
13622	    type = TYPE_NAME (class_type);
13623	  else
13624	    {
13625	      cp_parser_error (parser, "could not resolve typename type");
13626	      type = error_mark_node;
13627	    }
13628	}
13629
13630      maybe_process_partial_specialization (TREE_TYPE (type));
13631      class_type = current_class_type;
13632      /* Enter the scope indicated by the nested-name-specifier.  */
13633      pushed_scope = push_scope (nested_name_specifier);
13634      /* Get the canonical version of this type.  */
13635      type = TYPE_MAIN_DECL (TREE_TYPE (type));
13636      if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13637	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13638	{
13639	  type = push_template_decl (type);
13640	  if (type == error_mark_node)
13641	    {
13642	      type = NULL_TREE;
13643	      goto done;
13644	    }
13645	}
13646
13647      type = TREE_TYPE (type);
13648      *nested_name_specifier_p = true;
13649    }
13650  else      /* The name is not a nested name.  */
13651    {
13652      /* If the class was unnamed, create a dummy name.  */
13653      if (!id)
13654	id = make_anon_name ();
13655      type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13656		       parser->num_template_parameter_lists);
13657    }
13658
13659  /* Indicate whether this class was declared as a `class' or as a
13660     `struct'.  */
13661  if (TREE_CODE (type) == RECORD_TYPE)
13662    CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13663  cp_parser_check_class_key (class_key, type);
13664
13665  /* If this type was already complete, and we see another definition,
13666     that's an error.  */
13667  if (type != error_mark_node && COMPLETE_TYPE_P (type))
13668    {
13669      error ("redefinition of %q#T", type);
13670      error ("previous definition of %q+#T", type);
13671      type = NULL_TREE;
13672      goto done;
13673    }
13674  else if (type == error_mark_node)
13675    type = NULL_TREE;
13676
13677  /* We will have entered the scope containing the class; the names of
13678     base classes should be looked up in that context.  For example:
13679
13680       struct A { struct B {}; struct C; };
13681       struct A::C : B {};
13682
13683     is valid.  */
13684  *bases = NULL_TREE;
13685
13686  /* Get the list of base-classes, if there is one.  */
13687  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13688    *bases = cp_parser_base_clause (parser);
13689
13690 done:
13691  /* Leave the scope given by the nested-name-specifier.  We will
13692     enter the class scope itself while processing the members.  */
13693  if (pushed_scope)
13694    pop_scope (pushed_scope);
13695
13696  if (invalid_explicit_specialization_p)
13697    {
13698      end_specialization ();
13699      --parser->num_template_parameter_lists;
13700    }
13701  *attributes_p = attributes;
13702  return type;
13703}
13704
13705/* Parse a class-key.
13706
13707   class-key:
13708     class
13709     struct
13710     union
13711
13712   Returns the kind of class-key specified, or none_type to indicate
13713   error.  */
13714
13715static enum tag_types
13716cp_parser_class_key (cp_parser* parser)
13717{
13718  cp_token *token;
13719  enum tag_types tag_type;
13720
13721  /* Look for the class-key.  */
13722  token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13723  if (!token)
13724    return none_type;
13725
13726  /* Check to see if the TOKEN is a class-key.  */
13727  tag_type = cp_parser_token_is_class_key (token);
13728  if (!tag_type)
13729    cp_parser_error (parser, "expected class-key");
13730  return tag_type;
13731}
13732
13733/* Parse an (optional) member-specification.
13734
13735   member-specification:
13736     member-declaration member-specification [opt]
13737     access-specifier : member-specification [opt]  */
13738
13739static void
13740cp_parser_member_specification_opt (cp_parser* parser)
13741{
13742  while (true)
13743    {
13744      cp_token *token;
13745      enum rid keyword;
13746
13747      /* Peek at the next token.  */
13748      token = cp_lexer_peek_token (parser->lexer);
13749      /* If it's a `}', or EOF then we've seen all the members.  */
13750      if (token->type == CPP_CLOSE_BRACE
13751	  || token->type == CPP_EOF
13752	  || token->type == CPP_PRAGMA_EOL)
13753	break;
13754
13755      /* See if this token is a keyword.  */
13756      keyword = token->keyword;
13757      switch (keyword)
13758	{
13759	case RID_PUBLIC:
13760	case RID_PROTECTED:
13761	case RID_PRIVATE:
13762	  /* Consume the access-specifier.  */
13763	  cp_lexer_consume_token (parser->lexer);
13764	  /* Remember which access-specifier is active.  */
13765	  current_access_specifier = token->u.value;
13766	  /* Look for the `:'.  */
13767	  cp_parser_require (parser, CPP_COLON, "`:'");
13768	  break;
13769
13770	default:
13771	  /* Accept #pragmas at class scope.  */
13772	  if (token->type == CPP_PRAGMA)
13773	    {
13774	      cp_parser_pragma (parser, pragma_external);
13775	      break;
13776	    }
13777
13778	  /* Otherwise, the next construction must be a
13779	     member-declaration.  */
13780	  cp_parser_member_declaration (parser);
13781	}
13782    }
13783}
13784
13785/* Parse a member-declaration.
13786
13787   member-declaration:
13788     decl-specifier-seq [opt] member-declarator-list [opt] ;
13789     function-definition ; [opt]
13790     :: [opt] nested-name-specifier template [opt] unqualified-id ;
13791     using-declaration
13792     template-declaration
13793
13794   member-declarator-list:
13795     member-declarator
13796     member-declarator-list , member-declarator
13797
13798   member-declarator:
13799     declarator pure-specifier [opt]
13800     declarator constant-initializer [opt]
13801     identifier [opt] : constant-expression
13802
13803   GNU Extensions:
13804
13805   member-declaration:
13806     __extension__ member-declaration
13807
13808   member-declarator:
13809     declarator attributes [opt] pure-specifier [opt]
13810     declarator attributes [opt] constant-initializer [opt]
13811     identifier [opt] attributes [opt] : constant-expression  */
13812
13813static void
13814cp_parser_member_declaration (cp_parser* parser)
13815{
13816  cp_decl_specifier_seq decl_specifiers;
13817  tree prefix_attributes;
13818  tree decl;
13819  int declares_class_or_enum;
13820  bool friend_p;
13821  cp_token *token;
13822  int saved_pedantic;
13823
13824  /* Check for the `__extension__' keyword.  */
13825  if (cp_parser_extension_opt (parser, &saved_pedantic))
13826    {
13827      /* Recurse.  */
13828      cp_parser_member_declaration (parser);
13829      /* Restore the old value of the PEDANTIC flag.  */
13830      pedantic = saved_pedantic;
13831
13832      return;
13833    }
13834
13835  /* Check for a template-declaration.  */
13836  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13837    {
13838      /* An explicit specialization here is an error condition, and we
13839	 expect the specialization handler to detect and report this.  */
13840      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13841	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13842	cp_parser_explicit_specialization (parser);
13843      else
13844	cp_parser_template_declaration (parser, /*member_p=*/true);
13845
13846      return;
13847    }
13848
13849  /* Check for a using-declaration.  */
13850  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13851    {
13852      /* Parse the using-declaration.  */
13853      cp_parser_using_declaration (parser,
13854				   /*access_declaration_p=*/false);
13855      return;
13856    }
13857
13858  /* Check for @defs.  */
13859  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13860    {
13861      tree ivar, member;
13862      tree ivar_chains = cp_parser_objc_defs_expression (parser);
13863      ivar = ivar_chains;
13864      while (ivar)
13865	{
13866	  member = ivar;
13867	  ivar = TREE_CHAIN (member);
13868	  TREE_CHAIN (member) = NULL_TREE;
13869	  finish_member_declaration (member);
13870	}
13871      return;
13872    }
13873
13874  if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13875    return;
13876
13877  /* Parse the decl-specifier-seq.  */
13878  cp_parser_decl_specifier_seq (parser,
13879				CP_PARSER_FLAGS_OPTIONAL,
13880				&decl_specifiers,
13881				&declares_class_or_enum);
13882  prefix_attributes = decl_specifiers.attributes;
13883  decl_specifiers.attributes = NULL_TREE;
13884  /* Check for an invalid type-name.  */
13885  if (!decl_specifiers.type
13886      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13887    return;
13888  /* If there is no declarator, then the decl-specifier-seq should
13889     specify a type.  */
13890  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13891    {
13892      /* If there was no decl-specifier-seq, and the next token is a
13893	 `;', then we have something like:
13894
13895	   struct S { ; };
13896
13897	 [class.mem]
13898
13899	 Each member-declaration shall declare at least one member
13900	 name of the class.  */
13901      if (!decl_specifiers.any_specifiers_p)
13902	{
13903	  cp_token *token = cp_lexer_peek_token (parser->lexer);
13904	  if (pedantic && !token->in_system_header)
13905	    pedwarn ("%Hextra %<;%>", &token->location);
13906	}
13907      else
13908	{
13909	  tree type;
13910
13911	  /* See if this declaration is a friend.  */
13912	  friend_p = cp_parser_friend_p (&decl_specifiers);
13913	  /* If there were decl-specifiers, check to see if there was
13914	     a class-declaration.  */
13915	  type = check_tag_decl (&decl_specifiers);
13916	  /* Nested classes have already been added to the class, but
13917	     a `friend' needs to be explicitly registered.  */
13918	  if (friend_p)
13919	    {
13920	      /* If the `friend' keyword was present, the friend must
13921		 be introduced with a class-key.  */
13922	       if (!declares_class_or_enum)
13923		 error ("a class-key must be used when declaring a friend");
13924	       /* In this case:
13925
13926		    template <typename T> struct A {
13927		      friend struct A<T>::B;
13928		    };
13929
13930		  A<T>::B will be represented by a TYPENAME_TYPE, and
13931		  therefore not recognized by check_tag_decl.  */
13932	       if (!type
13933		   && decl_specifiers.type
13934		   && TYPE_P (decl_specifiers.type))
13935		 type = decl_specifiers.type;
13936	       if (!type || !TYPE_P (type))
13937		 error ("friend declaration does not name a class or "
13938			"function");
13939	       else
13940		 make_friend_class (current_class_type, type,
13941				    /*complain=*/true);
13942	    }
13943	  /* If there is no TYPE, an error message will already have
13944	     been issued.  */
13945	  else if (!type || type == error_mark_node)
13946	    ;
13947	  /* An anonymous aggregate has to be handled specially; such
13948	     a declaration really declares a data member (with a
13949	     particular type), as opposed to a nested class.  */
13950	  else if (ANON_AGGR_TYPE_P (type))
13951	    {
13952	      /* Remove constructors and such from TYPE, now that we
13953		 know it is an anonymous aggregate.  */
13954	      fixup_anonymous_aggr (type);
13955	      /* And make the corresponding data member.  */
13956	      decl = build_decl (FIELD_DECL, NULL_TREE, type);
13957	      /* Add it to the class.  */
13958	      finish_member_declaration (decl);
13959	    }
13960	  else
13961	    cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13962	}
13963    }
13964  else
13965    {
13966      /* See if these declarations will be friends.  */
13967      friend_p = cp_parser_friend_p (&decl_specifiers);
13968
13969      /* Keep going until we hit the `;' at the end of the
13970	 declaration.  */
13971      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13972	{
13973	  tree attributes = NULL_TREE;
13974	  tree first_attribute;
13975
13976	  /* Peek at the next token.  */
13977	  token = cp_lexer_peek_token (parser->lexer);
13978
13979	  /* Check for a bitfield declaration.  */
13980	  if (token->type == CPP_COLON
13981	      || (token->type == CPP_NAME
13982		  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13983		  == CPP_COLON))
13984	    {
13985	      tree identifier;
13986	      tree width;
13987
13988	      /* Get the name of the bitfield.  Note that we cannot just
13989		 check TOKEN here because it may have been invalidated by
13990		 the call to cp_lexer_peek_nth_token above.  */
13991	      if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13992		identifier = cp_parser_identifier (parser);
13993	      else
13994		identifier = NULL_TREE;
13995
13996	      /* Consume the `:' token.  */
13997	      cp_lexer_consume_token (parser->lexer);
13998	      /* Get the width of the bitfield.  */
13999	      width
14000		= cp_parser_constant_expression (parser,
14001						 /*allow_non_constant=*/false,
14002						 NULL);
14003
14004	      /* Look for attributes that apply to the bitfield.  */
14005	      attributes = cp_parser_attributes_opt (parser);
14006	      /* Remember which attributes are prefix attributes and
14007		 which are not.  */
14008	      first_attribute = attributes;
14009	      /* Combine the attributes.  */
14010	      attributes = chainon (prefix_attributes, attributes);
14011
14012	      /* Create the bitfield declaration.  */
14013	      decl = grokbitfield (identifier
14014				   ? make_id_declarator (NULL_TREE,
14015							 identifier,
14016							 sfk_none)
14017				   : NULL,
14018				   &decl_specifiers,
14019				   width);
14020	      /* Apply the attributes.  */
14021	      cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14022	    }
14023	  else
14024	    {
14025	      cp_declarator *declarator;
14026	      tree initializer;
14027	      tree asm_specification;
14028	      int ctor_dtor_or_conv_p;
14029
14030	      /* Parse the declarator.  */
14031	      declarator
14032		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14033					&ctor_dtor_or_conv_p,
14034					/*parenthesized_p=*/NULL,
14035					/*member_p=*/true);
14036
14037	      /* If something went wrong parsing the declarator, make sure
14038		 that we at least consume some tokens.  */
14039	      if (declarator == cp_error_declarator)
14040		{
14041		  /* Skip to the end of the statement.  */
14042		  cp_parser_skip_to_end_of_statement (parser);
14043		  /* If the next token is not a semicolon, that is
14044		     probably because we just skipped over the body of
14045		     a function.  So, we consume a semicolon if
14046		     present, but do not issue an error message if it
14047		     is not present.  */
14048		  if (cp_lexer_next_token_is (parser->lexer,
14049					      CPP_SEMICOLON))
14050		    cp_lexer_consume_token (parser->lexer);
14051		  return;
14052		}
14053
14054	      if (declares_class_or_enum & 2)
14055		cp_parser_check_for_definition_in_return_type
14056		  (declarator, decl_specifiers.type);
14057
14058	      /* Look for an asm-specification.  */
14059	      asm_specification = cp_parser_asm_specification_opt (parser);
14060	      /* Look for attributes that apply to the declaration.  */
14061	      attributes = cp_parser_attributes_opt (parser);
14062	      /* Remember which attributes are prefix attributes and
14063		 which are not.  */
14064	      first_attribute = attributes;
14065	      /* Combine the attributes.  */
14066	      attributes = chainon (prefix_attributes, attributes);
14067
14068	      /* If it's an `=', then we have a constant-initializer or a
14069		 pure-specifier.  It is not correct to parse the
14070		 initializer before registering the member declaration
14071		 since the member declaration should be in scope while
14072		 its initializer is processed.  However, the rest of the
14073		 front end does not yet provide an interface that allows
14074		 us to handle this correctly.  */
14075	      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14076		{
14077		  /* In [class.mem]:
14078
14079		     A pure-specifier shall be used only in the declaration of
14080		     a virtual function.
14081
14082		     A member-declarator can contain a constant-initializer
14083		     only if it declares a static member of integral or
14084		     enumeration type.
14085
14086		     Therefore, if the DECLARATOR is for a function, we look
14087		     for a pure-specifier; otherwise, we look for a
14088		     constant-initializer.  When we call `grokfield', it will
14089		     perform more stringent semantics checks.  */
14090		  if (function_declarator_p (declarator))
14091		    initializer = cp_parser_pure_specifier (parser);
14092		  else
14093		    /* Parse the initializer.  */
14094		    initializer = cp_parser_constant_initializer (parser);
14095		}
14096	      /* Otherwise, there is no initializer.  */
14097	      else
14098		initializer = NULL_TREE;
14099
14100	      /* See if we are probably looking at a function
14101		 definition.  We are certainly not looking at a
14102		 member-declarator.  Calling `grokfield' has
14103		 side-effects, so we must not do it unless we are sure
14104		 that we are looking at a member-declarator.  */
14105	      if (cp_parser_token_starts_function_definition_p
14106		  (cp_lexer_peek_token (parser->lexer)))
14107		{
14108		  /* The grammar does not allow a pure-specifier to be
14109		     used when a member function is defined.  (It is
14110		     possible that this fact is an oversight in the
14111		     standard, since a pure function may be defined
14112		     outside of the class-specifier.  */
14113		  if (initializer)
14114		    error ("pure-specifier on function-definition");
14115		  decl = cp_parser_save_member_function_body (parser,
14116							      &decl_specifiers,
14117							      declarator,
14118							      attributes);
14119		  /* If the member was not a friend, declare it here.  */
14120		  if (!friend_p)
14121		    finish_member_declaration (decl);
14122		  /* Peek at the next token.  */
14123		  token = cp_lexer_peek_token (parser->lexer);
14124		  /* If the next token is a semicolon, consume it.  */
14125		  if (token->type == CPP_SEMICOLON)
14126		    cp_lexer_consume_token (parser->lexer);
14127		  return;
14128		}
14129	      else
14130		/* Create the declaration.  */
14131		decl = grokfield (declarator, &decl_specifiers,
14132				  initializer, /*init_const_expr_p=*/true,
14133				  asm_specification,
14134				  attributes);
14135	    }
14136
14137	  /* Reset PREFIX_ATTRIBUTES.  */
14138	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
14139	    attributes = TREE_CHAIN (attributes);
14140	  if (attributes)
14141	    TREE_CHAIN (attributes) = NULL_TREE;
14142
14143	  /* If there is any qualification still in effect, clear it
14144	     now; we will be starting fresh with the next declarator.  */
14145	  parser->scope = NULL_TREE;
14146	  parser->qualifying_scope = NULL_TREE;
14147	  parser->object_scope = NULL_TREE;
14148	  /* If it's a `,', then there are more declarators.  */
14149	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14150	    cp_lexer_consume_token (parser->lexer);
14151	  /* If the next token isn't a `;', then we have a parse error.  */
14152	  else if (cp_lexer_next_token_is_not (parser->lexer,
14153					       CPP_SEMICOLON))
14154	    {
14155	      cp_parser_error (parser, "expected %<;%>");
14156	      /* Skip tokens until we find a `;'.  */
14157	      cp_parser_skip_to_end_of_statement (parser);
14158
14159	      break;
14160	    }
14161
14162	  if (decl)
14163	    {
14164	      /* Add DECL to the list of members.  */
14165	      if (!friend_p)
14166		finish_member_declaration (decl);
14167
14168	      if (TREE_CODE (decl) == FUNCTION_DECL)
14169		cp_parser_save_default_args (parser, decl);
14170	    }
14171	}
14172    }
14173
14174  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14175}
14176
14177/* Parse a pure-specifier.
14178
14179   pure-specifier:
14180     = 0
14181
14182   Returns INTEGER_ZERO_NODE if a pure specifier is found.
14183   Otherwise, ERROR_MARK_NODE is returned.  */
14184
14185static tree
14186cp_parser_pure_specifier (cp_parser* parser)
14187{
14188  cp_token *token;
14189
14190  /* Look for the `=' token.  */
14191  if (!cp_parser_require (parser, CPP_EQ, "`='"))
14192    return error_mark_node;
14193  /* Look for the `0' token.  */
14194  token = cp_lexer_consume_token (parser->lexer);
14195  /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
14196  if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14197    {
14198      cp_parser_error (parser,
14199		       "invalid pure specifier (only `= 0' is allowed)");
14200      cp_parser_skip_to_end_of_statement (parser);
14201      return error_mark_node;
14202    }
14203  if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14204    {
14205      error ("templates may not be %<virtual%>");
14206      return error_mark_node;
14207    }
14208
14209  return integer_zero_node;
14210}
14211
14212/* Parse a constant-initializer.
14213
14214   constant-initializer:
14215     = constant-expression
14216
14217   Returns a representation of the constant-expression.  */
14218
14219static tree
14220cp_parser_constant_initializer (cp_parser* parser)
14221{
14222  /* Look for the `=' token.  */
14223  if (!cp_parser_require (parser, CPP_EQ, "`='"))
14224    return error_mark_node;
14225
14226  /* It is invalid to write:
14227
14228       struct S { static const int i = { 7 }; };
14229
14230     */
14231  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14232    {
14233      cp_parser_error (parser,
14234		       "a brace-enclosed initializer is not allowed here");
14235      /* Consume the opening brace.  */
14236      cp_lexer_consume_token (parser->lexer);
14237      /* Skip the initializer.  */
14238      cp_parser_skip_to_closing_brace (parser);
14239      /* Look for the trailing `}'.  */
14240      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14241
14242      return error_mark_node;
14243    }
14244
14245  return cp_parser_constant_expression (parser,
14246					/*allow_non_constant=*/false,
14247					NULL);
14248}
14249
14250/* Derived classes [gram.class.derived] */
14251
14252/* Parse a base-clause.
14253
14254   base-clause:
14255     : base-specifier-list
14256
14257   base-specifier-list:
14258     base-specifier
14259     base-specifier-list , base-specifier
14260
14261   Returns a TREE_LIST representing the base-classes, in the order in
14262   which they were declared.  The representation of each node is as
14263   described by cp_parser_base_specifier.
14264
14265   In the case that no bases are specified, this function will return
14266   NULL_TREE, not ERROR_MARK_NODE.  */
14267
14268static tree
14269cp_parser_base_clause (cp_parser* parser)
14270{
14271  tree bases = NULL_TREE;
14272
14273  /* Look for the `:' that begins the list.  */
14274  cp_parser_require (parser, CPP_COLON, "`:'");
14275
14276  /* Scan the base-specifier-list.  */
14277  while (true)
14278    {
14279      cp_token *token;
14280      tree base;
14281
14282      /* Look for the base-specifier.  */
14283      base = cp_parser_base_specifier (parser);
14284      /* Add BASE to the front of the list.  */
14285      if (base != error_mark_node)
14286	{
14287	  TREE_CHAIN (base) = bases;
14288	  bases = base;
14289	}
14290      /* Peek at the next token.  */
14291      token = cp_lexer_peek_token (parser->lexer);
14292      /* If it's not a comma, then the list is complete.  */
14293      if (token->type != CPP_COMMA)
14294	break;
14295      /* Consume the `,'.  */
14296      cp_lexer_consume_token (parser->lexer);
14297    }
14298
14299  /* PARSER->SCOPE may still be non-NULL at this point, if the last
14300     base class had a qualified name.  However, the next name that
14301     appears is certainly not qualified.  */
14302  parser->scope = NULL_TREE;
14303  parser->qualifying_scope = NULL_TREE;
14304  parser->object_scope = NULL_TREE;
14305
14306  return nreverse (bases);
14307}
14308
14309/* Parse a base-specifier.
14310
14311   base-specifier:
14312     :: [opt] nested-name-specifier [opt] class-name
14313     virtual access-specifier [opt] :: [opt] nested-name-specifier
14314       [opt] class-name
14315     access-specifier virtual [opt] :: [opt] nested-name-specifier
14316       [opt] class-name
14317
14318   Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14319   ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14320   indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14321   (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14322
14323static tree
14324cp_parser_base_specifier (cp_parser* parser)
14325{
14326  cp_token *token;
14327  bool done = false;
14328  bool virtual_p = false;
14329  bool duplicate_virtual_error_issued_p = false;
14330  bool duplicate_access_error_issued_p = false;
14331  bool class_scope_p, template_p;
14332  tree access = access_default_node;
14333  tree type;
14334
14335  /* Process the optional `virtual' and `access-specifier'.  */
14336  while (!done)
14337    {
14338      /* Peek at the next token.  */
14339      token = cp_lexer_peek_token (parser->lexer);
14340      /* Process `virtual'.  */
14341      switch (token->keyword)
14342	{
14343	case RID_VIRTUAL:
14344	  /* If `virtual' appears more than once, issue an error.  */
14345	  if (virtual_p && !duplicate_virtual_error_issued_p)
14346	    {
14347	      cp_parser_error (parser,
14348			       "%<virtual%> specified more than once in base-specified");
14349	      duplicate_virtual_error_issued_p = true;
14350	    }
14351
14352	  virtual_p = true;
14353
14354	  /* Consume the `virtual' token.  */
14355	  cp_lexer_consume_token (parser->lexer);
14356
14357	  break;
14358
14359	case RID_PUBLIC:
14360	case RID_PROTECTED:
14361	case RID_PRIVATE:
14362	  /* If more than one access specifier appears, issue an
14363	     error.  */
14364	  if (access != access_default_node
14365	      && !duplicate_access_error_issued_p)
14366	    {
14367	      cp_parser_error (parser,
14368			       "more than one access specifier in base-specified");
14369	      duplicate_access_error_issued_p = true;
14370	    }
14371
14372	  access = ridpointers[(int) token->keyword];
14373
14374	  /* Consume the access-specifier.  */
14375	  cp_lexer_consume_token (parser->lexer);
14376
14377	  break;
14378
14379	default:
14380	  done = true;
14381	  break;
14382	}
14383    }
14384  /* It is not uncommon to see programs mechanically, erroneously, use
14385     the 'typename' keyword to denote (dependent) qualified types
14386     as base classes.  */
14387  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14388    {
14389      if (!processing_template_decl)
14390	error ("keyword %<typename%> not allowed outside of templates");
14391      else
14392	error ("keyword %<typename%> not allowed in this context "
14393	       "(the base class is implicitly a type)");
14394      cp_lexer_consume_token (parser->lexer);
14395    }
14396
14397  /* Look for the optional `::' operator.  */
14398  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14399  /* Look for the nested-name-specifier.  The simplest way to
14400     implement:
14401
14402       [temp.res]
14403
14404       The keyword `typename' is not permitted in a base-specifier or
14405       mem-initializer; in these contexts a qualified name that
14406       depends on a template-parameter is implicitly assumed to be a
14407       type name.
14408
14409     is to pretend that we have seen the `typename' keyword at this
14410     point.  */
14411  cp_parser_nested_name_specifier_opt (parser,
14412				       /*typename_keyword_p=*/true,
14413				       /*check_dependency_p=*/true,
14414				       typename_type,
14415				       /*is_declaration=*/true);
14416  /* If the base class is given by a qualified name, assume that names
14417     we see are type names or templates, as appropriate.  */
14418  class_scope_p = (parser->scope && TYPE_P (parser->scope));
14419  template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14420
14421  /* Finally, look for the class-name.  */
14422  type = cp_parser_class_name (parser,
14423			       class_scope_p,
14424			       template_p,
14425			       typename_type,
14426			       /*check_dependency_p=*/true,
14427			       /*class_head_p=*/false,
14428			       /*is_declaration=*/true);
14429
14430  if (type == error_mark_node)
14431    return error_mark_node;
14432
14433  return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14434}
14435
14436/* Exception handling [gram.exception] */
14437
14438/* Parse an (optional) exception-specification.
14439
14440   exception-specification:
14441     throw ( type-id-list [opt] )
14442
14443   Returns a TREE_LIST representing the exception-specification.  The
14444   TREE_VALUE of each node is a type.  */
14445
14446static tree
14447cp_parser_exception_specification_opt (cp_parser* parser)
14448{
14449  cp_token *token;
14450  tree type_id_list;
14451
14452  /* Peek at the next token.  */
14453  token = cp_lexer_peek_token (parser->lexer);
14454  /* If it's not `throw', then there's no exception-specification.  */
14455  if (!cp_parser_is_keyword (token, RID_THROW))
14456    return NULL_TREE;
14457
14458  /* Consume the `throw'.  */
14459  cp_lexer_consume_token (parser->lexer);
14460
14461  /* Look for the `('.  */
14462  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14463
14464  /* Peek at the next token.  */
14465  token = cp_lexer_peek_token (parser->lexer);
14466  /* If it's not a `)', then there is a type-id-list.  */
14467  if (token->type != CPP_CLOSE_PAREN)
14468    {
14469      const char *saved_message;
14470
14471      /* Types may not be defined in an exception-specification.  */
14472      saved_message = parser->type_definition_forbidden_message;
14473      parser->type_definition_forbidden_message
14474	= "types may not be defined in an exception-specification";
14475      /* Parse the type-id-list.  */
14476      type_id_list = cp_parser_type_id_list (parser);
14477      /* Restore the saved message.  */
14478      parser->type_definition_forbidden_message = saved_message;
14479    }
14480  else
14481    type_id_list = empty_except_spec;
14482
14483  /* Look for the `)'.  */
14484  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14485
14486  return type_id_list;
14487}
14488
14489/* Parse an (optional) type-id-list.
14490
14491   type-id-list:
14492     type-id
14493     type-id-list , type-id
14494
14495   Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14496   in the order that the types were presented.  */
14497
14498static tree
14499cp_parser_type_id_list (cp_parser* parser)
14500{
14501  tree types = NULL_TREE;
14502
14503  while (true)
14504    {
14505      cp_token *token;
14506      tree type;
14507
14508      /* Get the next type-id.  */
14509      type = cp_parser_type_id (parser);
14510      /* Add it to the list.  */
14511      types = add_exception_specifier (types, type, /*complain=*/1);
14512      /* Peek at the next token.  */
14513      token = cp_lexer_peek_token (parser->lexer);
14514      /* If it is not a `,', we are done.  */
14515      if (token->type != CPP_COMMA)
14516	break;
14517      /* Consume the `,'.  */
14518      cp_lexer_consume_token (parser->lexer);
14519    }
14520
14521  return nreverse (types);
14522}
14523
14524/* Parse a try-block.
14525
14526   try-block:
14527     try compound-statement handler-seq  */
14528
14529static tree
14530cp_parser_try_block (cp_parser* parser)
14531{
14532  tree try_block;
14533
14534  cp_parser_require_keyword (parser, RID_TRY, "`try'");
14535  try_block = begin_try_block ();
14536  cp_parser_compound_statement (parser, NULL, true);
14537  finish_try_block (try_block);
14538  cp_parser_handler_seq (parser);
14539  finish_handler_sequence (try_block);
14540
14541  return try_block;
14542}
14543
14544/* Parse a function-try-block.
14545
14546   function-try-block:
14547     try ctor-initializer [opt] function-body handler-seq  */
14548
14549static bool
14550cp_parser_function_try_block (cp_parser* parser)
14551{
14552  tree compound_stmt;
14553  tree try_block;
14554  bool ctor_initializer_p;
14555
14556  /* Look for the `try' keyword.  */
14557  if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14558    return false;
14559  /* Let the rest of the front-end know where we are.  */
14560  try_block = begin_function_try_block (&compound_stmt);
14561  /* Parse the function-body.  */
14562  ctor_initializer_p
14563    = cp_parser_ctor_initializer_opt_and_function_body (parser);
14564  /* We're done with the `try' part.  */
14565  finish_function_try_block (try_block);
14566  /* Parse the handlers.  */
14567  cp_parser_handler_seq (parser);
14568  /* We're done with the handlers.  */
14569  finish_function_handler_sequence (try_block, compound_stmt);
14570
14571  return ctor_initializer_p;
14572}
14573
14574/* Parse a handler-seq.
14575
14576   handler-seq:
14577     handler handler-seq [opt]  */
14578
14579static void
14580cp_parser_handler_seq (cp_parser* parser)
14581{
14582  while (true)
14583    {
14584      cp_token *token;
14585
14586      /* Parse the handler.  */
14587      cp_parser_handler (parser);
14588      /* Peek at the next token.  */
14589      token = cp_lexer_peek_token (parser->lexer);
14590      /* If it's not `catch' then there are no more handlers.  */
14591      if (!cp_parser_is_keyword (token, RID_CATCH))
14592	break;
14593    }
14594}
14595
14596/* Parse a handler.
14597
14598   handler:
14599     catch ( exception-declaration ) compound-statement  */
14600
14601static void
14602cp_parser_handler (cp_parser* parser)
14603{
14604  tree handler;
14605  tree declaration;
14606
14607  cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14608  handler = begin_handler ();
14609  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14610  declaration = cp_parser_exception_declaration (parser);
14611  finish_handler_parms (declaration, handler);
14612  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14613  cp_parser_compound_statement (parser, NULL, false);
14614  finish_handler (handler);
14615}
14616
14617/* Parse an exception-declaration.
14618
14619   exception-declaration:
14620     type-specifier-seq declarator
14621     type-specifier-seq abstract-declarator
14622     type-specifier-seq
14623     ...
14624
14625   Returns a VAR_DECL for the declaration, or NULL_TREE if the
14626   ellipsis variant is used.  */
14627
14628static tree
14629cp_parser_exception_declaration (cp_parser* parser)
14630{
14631  cp_decl_specifier_seq type_specifiers;
14632  cp_declarator *declarator;
14633  const char *saved_message;
14634
14635  /* If it's an ellipsis, it's easy to handle.  */
14636  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14637    {
14638      /* Consume the `...' token.  */
14639      cp_lexer_consume_token (parser->lexer);
14640      return NULL_TREE;
14641    }
14642
14643  /* Types may not be defined in exception-declarations.  */
14644  saved_message = parser->type_definition_forbidden_message;
14645  parser->type_definition_forbidden_message
14646    = "types may not be defined in exception-declarations";
14647
14648  /* Parse the type-specifier-seq.  */
14649  cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14650				&type_specifiers);
14651  /* If it's a `)', then there is no declarator.  */
14652  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14653    declarator = NULL;
14654  else
14655    declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14656				       /*ctor_dtor_or_conv_p=*/NULL,
14657				       /*parenthesized_p=*/NULL,
14658				       /*member_p=*/false);
14659
14660  /* Restore the saved message.  */
14661  parser->type_definition_forbidden_message = saved_message;
14662
14663  if (!type_specifiers.any_specifiers_p)
14664    return error_mark_node;
14665
14666  return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14667}
14668
14669/* Parse a throw-expression.
14670
14671   throw-expression:
14672     throw assignment-expression [opt]
14673
14674   Returns a THROW_EXPR representing the throw-expression.  */
14675
14676static tree
14677cp_parser_throw_expression (cp_parser* parser)
14678{
14679  tree expression;
14680  cp_token* token;
14681
14682  cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14683  token = cp_lexer_peek_token (parser->lexer);
14684  /* Figure out whether or not there is an assignment-expression
14685     following the "throw" keyword.  */
14686  if (token->type == CPP_COMMA
14687      || token->type == CPP_SEMICOLON
14688      || token->type == CPP_CLOSE_PAREN
14689      || token->type == CPP_CLOSE_SQUARE
14690      || token->type == CPP_CLOSE_BRACE
14691      || token->type == CPP_COLON)
14692    expression = NULL_TREE;
14693  else
14694    expression = cp_parser_assignment_expression (parser,
14695						  /*cast_p=*/false);
14696
14697  return build_throw (expression);
14698}
14699
14700/* GNU Extensions */
14701
14702/* Parse an (optional) asm-specification.
14703
14704   asm-specification:
14705     asm ( string-literal )
14706
14707   If the asm-specification is present, returns a STRING_CST
14708   corresponding to the string-literal.  Otherwise, returns
14709   NULL_TREE.  */
14710
14711static tree
14712cp_parser_asm_specification_opt (cp_parser* parser)
14713{
14714  cp_token *token;
14715  tree asm_specification;
14716
14717  /* Peek at the next token.  */
14718  token = cp_lexer_peek_token (parser->lexer);
14719  /* If the next token isn't the `asm' keyword, then there's no
14720     asm-specification.  */
14721  if (!cp_parser_is_keyword (token, RID_ASM))
14722    return NULL_TREE;
14723
14724  /* Consume the `asm' token.  */
14725  cp_lexer_consume_token (parser->lexer);
14726  /* Look for the `('.  */
14727  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14728
14729  /* Look for the string-literal.  */
14730  asm_specification = cp_parser_string_literal (parser, false, false);
14731
14732  /* Look for the `)'.  */
14733  cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14734
14735  return asm_specification;
14736}
14737
14738/* Parse an asm-operand-list.
14739
14740   asm-operand-list:
14741     asm-operand
14742     asm-operand-list , asm-operand
14743
14744   asm-operand:
14745     string-literal ( expression )
14746     [ string-literal ] string-literal ( expression )
14747
14748   Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14749   each node is the expression.  The TREE_PURPOSE is itself a
14750   TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14751   string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14752   is a STRING_CST for the string literal before the parenthesis.  */
14753
14754static tree
14755cp_parser_asm_operand_list (cp_parser* parser)
14756{
14757  tree asm_operands = NULL_TREE;
14758
14759  while (true)
14760    {
14761      tree string_literal;
14762      tree expression;
14763      tree name;
14764
14765      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14766	{
14767	  /* Consume the `[' token.  */
14768	  cp_lexer_consume_token (parser->lexer);
14769	  /* Read the operand name.  */
14770	  name = cp_parser_identifier (parser);
14771	  if (name != error_mark_node)
14772	    name = build_string (IDENTIFIER_LENGTH (name),
14773				 IDENTIFIER_POINTER (name));
14774	  /* Look for the closing `]'.  */
14775	  cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14776	}
14777      else
14778	name = NULL_TREE;
14779      /* Look for the string-literal.  */
14780      string_literal = cp_parser_string_literal (parser, false, false);
14781
14782      /* Look for the `('.  */
14783      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14784      /* Parse the expression.  */
14785      expression = cp_parser_expression (parser, /*cast_p=*/false);
14786      /* Look for the `)'.  */
14787      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14788
14789      /* Add this operand to the list.  */
14790      asm_operands = tree_cons (build_tree_list (name, string_literal),
14791				expression,
14792				asm_operands);
14793      /* If the next token is not a `,', there are no more
14794	 operands.  */
14795      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14796	break;
14797      /* Consume the `,'.  */
14798      cp_lexer_consume_token (parser->lexer);
14799    }
14800
14801  return nreverse (asm_operands);
14802}
14803
14804/* Parse an asm-clobber-list.
14805
14806   asm-clobber-list:
14807     string-literal
14808     asm-clobber-list , string-literal
14809
14810   Returns a TREE_LIST, indicating the clobbers in the order that they
14811   appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14812
14813static tree
14814cp_parser_asm_clobber_list (cp_parser* parser)
14815{
14816  tree clobbers = NULL_TREE;
14817
14818  while (true)
14819    {
14820      tree string_literal;
14821
14822      /* Look for the string literal.  */
14823      string_literal = cp_parser_string_literal (parser, false, false);
14824      /* Add it to the list.  */
14825      clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14826      /* If the next token is not a `,', then the list is
14827	 complete.  */
14828      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14829	break;
14830      /* Consume the `,' token.  */
14831      cp_lexer_consume_token (parser->lexer);
14832    }
14833
14834  return clobbers;
14835}
14836
14837/* Parse an (optional) series of attributes.
14838
14839   attributes:
14840     attributes attribute
14841
14842   attribute:
14843     __attribute__ (( attribute-list [opt] ))
14844
14845   The return value is as for cp_parser_attribute_list.  */
14846
14847static tree
14848cp_parser_attributes_opt (cp_parser* parser)
14849{
14850  tree attributes = NULL_TREE;
14851
14852  while (true)
14853    {
14854      cp_token *token;
14855      tree attribute_list;
14856
14857      /* Peek at the next token.  */
14858      token = cp_lexer_peek_token (parser->lexer);
14859      /* If it's not `__attribute__', then we're done.  */
14860      if (token->keyword != RID_ATTRIBUTE)
14861	break;
14862
14863      /* Consume the `__attribute__' keyword.  */
14864      cp_lexer_consume_token (parser->lexer);
14865      /* Look for the two `(' tokens.  */
14866      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14867      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14868
14869      /* Peek at the next token.  */
14870      token = cp_lexer_peek_token (parser->lexer);
14871      if (token->type != CPP_CLOSE_PAREN)
14872	/* Parse the attribute-list.  */
14873	attribute_list = cp_parser_attribute_list (parser);
14874      else
14875	/* If the next token is a `)', then there is no attribute
14876	   list.  */
14877	attribute_list = NULL;
14878
14879      /* Look for the two `)' tokens.  */
14880      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14881      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14882
14883      /* Add these new attributes to the list.  */
14884      attributes = chainon (attributes, attribute_list);
14885    }
14886
14887  return attributes;
14888}
14889
14890/* Parse an attribute-list.
14891
14892   attribute-list:
14893     attribute
14894     attribute-list , attribute
14895
14896   attribute:
14897     identifier
14898     identifier ( identifier )
14899     identifier ( identifier , expression-list )
14900     identifier ( expression-list )
14901
14902   Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14903   to an attribute.  The TREE_PURPOSE of each node is the identifier
14904   indicating which attribute is in use.  The TREE_VALUE represents
14905   the arguments, if any.  */
14906
14907static tree
14908cp_parser_attribute_list (cp_parser* parser)
14909{
14910  tree attribute_list = NULL_TREE;
14911  bool save_translate_strings_p = parser->translate_strings_p;
14912
14913  parser->translate_strings_p = false;
14914  while (true)
14915    {
14916      cp_token *token;
14917      tree identifier;
14918      tree attribute;
14919
14920      /* Look for the identifier.  We also allow keywords here; for
14921	 example `__attribute__ ((const))' is legal.  */
14922      token = cp_lexer_peek_token (parser->lexer);
14923      if (token->type == CPP_NAME
14924	  || token->type == CPP_KEYWORD)
14925	{
14926	  tree arguments = NULL_TREE;
14927
14928	  /* Consume the token.  */
14929	  token = cp_lexer_consume_token (parser->lexer);
14930
14931	  /* Save away the identifier that indicates which attribute
14932	     this is.  */
14933	  identifier = token->u.value;
14934	  attribute = build_tree_list (identifier, NULL_TREE);
14935
14936	  /* Peek at the next token.  */
14937	  token = cp_lexer_peek_token (parser->lexer);
14938	  /* If it's an `(', then parse the attribute arguments.  */
14939	  if (token->type == CPP_OPEN_PAREN)
14940	    {
14941	      arguments = cp_parser_parenthesized_expression_list
14942			  (parser, true, /*cast_p=*/false,
14943			   /*non_constant_p=*/NULL);
14944	      /* Save the arguments away.  */
14945	      TREE_VALUE (attribute) = arguments;
14946	    }
14947
14948	  if (arguments != error_mark_node)
14949	    {
14950	      /* Add this attribute to the list.  */
14951	      TREE_CHAIN (attribute) = attribute_list;
14952	      attribute_list = attribute;
14953	    }
14954
14955	  token = cp_lexer_peek_token (parser->lexer);
14956	}
14957      /* Now, look for more attributes.  If the next token isn't a
14958	 `,', we're done.  */
14959      if (token->type != CPP_COMMA)
14960	break;
14961
14962      /* Consume the comma and keep going.  */
14963      cp_lexer_consume_token (parser->lexer);
14964    }
14965  parser->translate_strings_p = save_translate_strings_p;
14966
14967  /* We built up the list in reverse order.  */
14968  return nreverse (attribute_list);
14969}
14970
14971/* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14972   present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14973   current value of the PEDANTIC flag, regardless of whether or not
14974   the `__extension__' keyword is present.  The caller is responsible
14975   for restoring the value of the PEDANTIC flag.  */
14976
14977static bool
14978cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14979{
14980  /* Save the old value of the PEDANTIC flag.  */
14981  *saved_pedantic = pedantic;
14982
14983  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14984    {
14985      /* Consume the `__extension__' token.  */
14986      cp_lexer_consume_token (parser->lexer);
14987      /* We're not being pedantic while the `__extension__' keyword is
14988	 in effect.  */
14989      pedantic = 0;
14990
14991      return true;
14992    }
14993
14994  return false;
14995}
14996
14997/* Parse a label declaration.
14998
14999   label-declaration:
15000     __label__ label-declarator-seq ;
15001
15002   label-declarator-seq:
15003     identifier , label-declarator-seq
15004     identifier  */
15005
15006static void
15007cp_parser_label_declaration (cp_parser* parser)
15008{
15009  /* Look for the `__label__' keyword.  */
15010  cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15011
15012  while (true)
15013    {
15014      tree identifier;
15015
15016      /* Look for an identifier.  */
15017      identifier = cp_parser_identifier (parser);
15018      /* If we failed, stop.  */
15019      if (identifier == error_mark_node)
15020	break;
15021      /* Declare it as a label.  */
15022      finish_label_decl (identifier);
15023      /* If the next token is a `;', stop.  */
15024      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15025	break;
15026      /* Look for the `,' separating the label declarations.  */
15027      cp_parser_require (parser, CPP_COMMA, "`,'");
15028    }
15029
15030  /* Look for the final `;'.  */
15031  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15032}
15033
15034/* Support Functions */
15035
15036/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15037   NAME should have one of the representations used for an
15038   id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15039   is returned.  If PARSER->SCOPE is a dependent type, then a
15040   SCOPE_REF is returned.
15041
15042   If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15043   returned; the name was already resolved when the TEMPLATE_ID_EXPR
15044   was formed.  Abstractly, such entities should not be passed to this
15045   function, because they do not need to be looked up, but it is
15046   simpler to check for this special case here, rather than at the
15047   call-sites.
15048
15049   In cases not explicitly covered above, this function returns a
15050   DECL, OVERLOAD, or baselink representing the result of the lookup.
15051   If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15052   is returned.
15053
15054   If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15055   (e.g., "struct") that was used.  In that case bindings that do not
15056   refer to types are ignored.
15057
15058   If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15059   ignored.
15060
15061   If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15062   are ignored.
15063
15064   If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15065   types.
15066
15067   If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15068   TREE_LIST of candidates if name-lookup results in an ambiguity, and
15069   NULL_TREE otherwise.  */
15070
15071static tree
15072cp_parser_lookup_name (cp_parser *parser, tree name,
15073		       enum tag_types tag_type,
15074		       bool is_template,
15075		       bool is_namespace,
15076		       bool check_dependency,
15077		       tree *ambiguous_decls)
15078{
15079  int flags = 0;
15080  tree decl;
15081  tree object_type = parser->context->object_type;
15082
15083  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15084    flags |= LOOKUP_COMPLAIN;
15085
15086  /* Assume that the lookup will be unambiguous.  */
15087  if (ambiguous_decls)
15088    *ambiguous_decls = NULL_TREE;
15089
15090  /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15091     no longer valid.  Note that if we are parsing tentatively, and
15092     the parse fails, OBJECT_TYPE will be automatically restored.  */
15093  parser->context->object_type = NULL_TREE;
15094
15095  if (name == error_mark_node)
15096    return error_mark_node;
15097
15098  /* A template-id has already been resolved; there is no lookup to
15099     do.  */
15100  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15101    return name;
15102  if (BASELINK_P (name))
15103    {
15104      gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15105		  == TEMPLATE_ID_EXPR);
15106      return name;
15107    }
15108
15109  /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
15110     it should already have been checked to make sure that the name
15111     used matches the type being destroyed.  */
15112  if (TREE_CODE (name) == BIT_NOT_EXPR)
15113    {
15114      tree type;
15115
15116      /* Figure out to which type this destructor applies.  */
15117      if (parser->scope)
15118	type = parser->scope;
15119      else if (object_type)
15120	type = object_type;
15121      else
15122	type = current_class_type;
15123      /* If that's not a class type, there is no destructor.  */
15124      if (!type || !CLASS_TYPE_P (type))
15125	return error_mark_node;
15126      if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15127	lazily_declare_fn (sfk_destructor, type);
15128      if (!CLASSTYPE_DESTRUCTORS (type))
15129	  return error_mark_node;
15130      /* If it was a class type, return the destructor.  */
15131      return CLASSTYPE_DESTRUCTORS (type);
15132    }
15133
15134  /* By this point, the NAME should be an ordinary identifier.  If
15135     the id-expression was a qualified name, the qualifying scope is
15136     stored in PARSER->SCOPE at this point.  */
15137  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15138
15139  /* Perform the lookup.  */
15140  if (parser->scope)
15141    {
15142      bool dependent_p;
15143
15144      if (parser->scope == error_mark_node)
15145	return error_mark_node;
15146
15147      /* If the SCOPE is dependent, the lookup must be deferred until
15148	 the template is instantiated -- unless we are explicitly
15149	 looking up names in uninstantiated templates.  Even then, we
15150	 cannot look up the name if the scope is not a class type; it
15151	 might, for example, be a template type parameter.  */
15152      dependent_p = (TYPE_P (parser->scope)
15153		     && !(parser->in_declarator_p
15154			  && currently_open_class (parser->scope))
15155		     && dependent_type_p (parser->scope));
15156      if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15157	   && dependent_p)
15158	{
15159	  if (tag_type)
15160	    {
15161	      tree type;
15162
15163	      /* The resolution to Core Issue 180 says that `struct
15164		 A::B' should be considered a type-name, even if `A'
15165		 is dependent.  */
15166	      type = make_typename_type (parser->scope, name, tag_type,
15167					 /*complain=*/tf_error);
15168	      decl = TYPE_NAME (type);
15169	    }
15170	  else if (is_template
15171		   && (cp_parser_next_token_ends_template_argument_p (parser)
15172		       || cp_lexer_next_token_is (parser->lexer,
15173						  CPP_CLOSE_PAREN)))
15174	    decl = make_unbound_class_template (parser->scope,
15175						name, NULL_TREE,
15176						/*complain=*/tf_error);
15177	  else
15178	    decl = build_qualified_name (/*type=*/NULL_TREE,
15179					 parser->scope, name,
15180					 is_template);
15181	}
15182      else
15183	{
15184	  tree pushed_scope = NULL_TREE;
15185
15186	  /* If PARSER->SCOPE is a dependent type, then it must be a
15187	     class type, and we must not be checking dependencies;
15188	     otherwise, we would have processed this lookup above.  So
15189	     that PARSER->SCOPE is not considered a dependent base by
15190	     lookup_member, we must enter the scope here.  */
15191	  if (dependent_p)
15192	    pushed_scope = push_scope (parser->scope);
15193	  /* If the PARSER->SCOPE is a template specialization, it
15194	     may be instantiated during name lookup.  In that case,
15195	     errors may be issued.  Even if we rollback the current
15196	     tentative parse, those errors are valid.  */
15197	  decl = lookup_qualified_name (parser->scope, name,
15198					tag_type != none_type,
15199					/*complain=*/true);
15200	  if (pushed_scope)
15201	    pop_scope (pushed_scope);
15202	}
15203      parser->qualifying_scope = parser->scope;
15204      parser->object_scope = NULL_TREE;
15205    }
15206  else if (object_type)
15207    {
15208      tree object_decl = NULL_TREE;
15209      /* Look up the name in the scope of the OBJECT_TYPE, unless the
15210	 OBJECT_TYPE is not a class.  */
15211      if (CLASS_TYPE_P (object_type))
15212	/* If the OBJECT_TYPE is a template specialization, it may
15213	   be instantiated during name lookup.  In that case, errors
15214	   may be issued.  Even if we rollback the current tentative
15215	   parse, those errors are valid.  */
15216	object_decl = lookup_member (object_type,
15217				     name,
15218				     /*protect=*/0,
15219				     tag_type != none_type);
15220      /* Look it up in the enclosing context, too.  */
15221      decl = lookup_name_real (name, tag_type != none_type,
15222			       /*nonclass=*/0,
15223			       /*block_p=*/true, is_namespace, flags);
15224      parser->object_scope = object_type;
15225      parser->qualifying_scope = NULL_TREE;
15226      if (object_decl)
15227	decl = object_decl;
15228    }
15229  else
15230    {
15231      decl = lookup_name_real (name, tag_type != none_type,
15232			       /*nonclass=*/0,
15233			       /*block_p=*/true, is_namespace, flags);
15234      parser->qualifying_scope = NULL_TREE;
15235      parser->object_scope = NULL_TREE;
15236    }
15237
15238  /* If the lookup failed, let our caller know.  */
15239  if (!decl || decl == error_mark_node)
15240    return error_mark_node;
15241
15242  /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
15243  if (TREE_CODE (decl) == TREE_LIST)
15244    {
15245      if (ambiguous_decls)
15246	*ambiguous_decls = decl;
15247      /* The error message we have to print is too complicated for
15248	 cp_parser_error, so we incorporate its actions directly.  */
15249      if (!cp_parser_simulate_error (parser))
15250	{
15251	  error ("reference to %qD is ambiguous", name);
15252	  print_candidates (decl);
15253	}
15254      return error_mark_node;
15255    }
15256
15257  gcc_assert (DECL_P (decl)
15258	      || TREE_CODE (decl) == OVERLOAD
15259	      || TREE_CODE (decl) == SCOPE_REF
15260	      || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15261	      || BASELINK_P (decl));
15262
15263  /* If we have resolved the name of a member declaration, check to
15264     see if the declaration is accessible.  When the name resolves to
15265     set of overloaded functions, accessibility is checked when
15266     overload resolution is done.
15267
15268     During an explicit instantiation, access is not checked at all,
15269     as per [temp.explicit].  */
15270  if (DECL_P (decl))
15271    check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15272
15273  return decl;
15274}
15275
15276/* Like cp_parser_lookup_name, but for use in the typical case where
15277   CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15278   IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15279
15280static tree
15281cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15282{
15283  return cp_parser_lookup_name (parser, name,
15284				none_type,
15285				/*is_template=*/false,
15286				/*is_namespace=*/false,
15287				/*check_dependency=*/true,
15288				/*ambiguous_decls=*/NULL);
15289}
15290
15291/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15292   the current context, return the TYPE_DECL.  If TAG_NAME_P is
15293   true, the DECL indicates the class being defined in a class-head,
15294   or declared in an elaborated-type-specifier.
15295
15296   Otherwise, return DECL.  */
15297
15298static tree
15299cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15300{
15301  /* If the TEMPLATE_DECL is being declared as part of a class-head,
15302     the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15303
15304       struct A {
15305	 template <typename T> struct B;
15306       };
15307
15308       template <typename T> struct A::B {};
15309
15310     Similarly, in an elaborated-type-specifier:
15311
15312       namespace N { struct X{}; }
15313
15314       struct A {
15315	 template <typename T> friend struct N::X;
15316       };
15317
15318     However, if the DECL refers to a class type, and we are in
15319     the scope of the class, then the name lookup automatically
15320     finds the TYPE_DECL created by build_self_reference rather
15321     than a TEMPLATE_DECL.  For example, in:
15322
15323       template <class T> struct S {
15324	 S s;
15325       };
15326
15327     there is no need to handle such case.  */
15328
15329  if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15330    return DECL_TEMPLATE_RESULT (decl);
15331
15332  return decl;
15333}
15334
15335/* If too many, or too few, template-parameter lists apply to the
15336   declarator, issue an error message.  Returns TRUE if all went well,
15337   and FALSE otherwise.  */
15338
15339static bool
15340cp_parser_check_declarator_template_parameters (cp_parser* parser,
15341						cp_declarator *declarator)
15342{
15343  unsigned num_templates;
15344
15345  /* We haven't seen any classes that involve template parameters yet.  */
15346  num_templates = 0;
15347
15348  switch (declarator->kind)
15349    {
15350    case cdk_id:
15351      if (declarator->u.id.qualifying_scope)
15352	{
15353	  tree scope;
15354	  tree member;
15355
15356	  scope = declarator->u.id.qualifying_scope;
15357	  member = declarator->u.id.unqualified_name;
15358
15359	  while (scope && CLASS_TYPE_P (scope))
15360	    {
15361	      /* You're supposed to have one `template <...>'
15362		 for every template class, but you don't need one
15363		 for a full specialization.  For example:
15364
15365		 template <class T> struct S{};
15366		 template <> struct S<int> { void f(); };
15367		 void S<int>::f () {}
15368
15369		 is correct; there shouldn't be a `template <>' for
15370		 the definition of `S<int>::f'.  */
15371	      if (!CLASSTYPE_TEMPLATE_INFO (scope))
15372		/* If SCOPE does not have template information of any
15373		   kind, then it is not a template, nor is it nested
15374		   within a template.  */
15375		break;
15376	      if (explicit_class_specialization_p (scope))
15377		break;
15378	      if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15379		++num_templates;
15380
15381	      scope = TYPE_CONTEXT (scope);
15382	    }
15383	}
15384      else if (TREE_CODE (declarator->u.id.unqualified_name)
15385	       == TEMPLATE_ID_EXPR)
15386	/* If the DECLARATOR has the form `X<y>' then it uses one
15387	   additional level of template parameters.  */
15388	++num_templates;
15389
15390      return cp_parser_check_template_parameters (parser,
15391						  num_templates);
15392
15393    case cdk_function:
15394    case cdk_array:
15395    case cdk_pointer:
15396    case cdk_reference:
15397    case cdk_ptrmem:
15398      return (cp_parser_check_declarator_template_parameters
15399	      (parser, declarator->declarator));
15400
15401    case cdk_error:
15402      return true;
15403
15404    default:
15405      gcc_unreachable ();
15406    }
15407  return false;
15408}
15409
15410/* NUM_TEMPLATES were used in the current declaration.  If that is
15411   invalid, return FALSE and issue an error messages.  Otherwise,
15412   return TRUE.  */
15413
15414static bool
15415cp_parser_check_template_parameters (cp_parser* parser,
15416				     unsigned num_templates)
15417{
15418  /* If there are more template classes than parameter lists, we have
15419     something like:
15420
15421       template <class T> void S<T>::R<T>::f ();  */
15422  if (parser->num_template_parameter_lists < num_templates)
15423    {
15424      error ("too few template-parameter-lists");
15425      return false;
15426    }
15427  /* If there are the same number of template classes and parameter
15428     lists, that's OK.  */
15429  if (parser->num_template_parameter_lists == num_templates)
15430    return true;
15431  /* If there are more, but only one more, then we are referring to a
15432     member template.  That's OK too.  */
15433  if (parser->num_template_parameter_lists == num_templates + 1)
15434      return true;
15435  /* Otherwise, there are too many template parameter lists.  We have
15436     something like:
15437
15438     template <class T> template <class U> void S::f();  */
15439  error ("too many template-parameter-lists");
15440  return false;
15441}
15442
15443/* Parse an optional `::' token indicating that the following name is
15444   from the global namespace.  If so, PARSER->SCOPE is set to the
15445   GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15446   unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15447   Returns the new value of PARSER->SCOPE, if the `::' token is
15448   present, and NULL_TREE otherwise.  */
15449
15450static tree
15451cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15452{
15453  cp_token *token;
15454
15455  /* Peek at the next token.  */
15456  token = cp_lexer_peek_token (parser->lexer);
15457  /* If we're looking at a `::' token then we're starting from the
15458     global namespace, not our current location.  */
15459  if (token->type == CPP_SCOPE)
15460    {
15461      /* Consume the `::' token.  */
15462      cp_lexer_consume_token (parser->lexer);
15463      /* Set the SCOPE so that we know where to start the lookup.  */
15464      parser->scope = global_namespace;
15465      parser->qualifying_scope = global_namespace;
15466      parser->object_scope = NULL_TREE;
15467
15468      return parser->scope;
15469    }
15470  else if (!current_scope_valid_p)
15471    {
15472      parser->scope = NULL_TREE;
15473      parser->qualifying_scope = NULL_TREE;
15474      parser->object_scope = NULL_TREE;
15475    }
15476
15477  return NULL_TREE;
15478}
15479
15480/* Returns TRUE if the upcoming token sequence is the start of a
15481   constructor declarator.  If FRIEND_P is true, the declarator is
15482   preceded by the `friend' specifier.  */
15483
15484static bool
15485cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15486{
15487  bool constructor_p;
15488  tree type_decl = NULL_TREE;
15489  bool nested_name_p;
15490  cp_token *next_token;
15491
15492  /* The common case is that this is not a constructor declarator, so
15493     try to avoid doing lots of work if at all possible.  It's not
15494     valid declare a constructor at function scope.  */
15495  if (parser->in_function_body)
15496    return false;
15497  /* And only certain tokens can begin a constructor declarator.  */
15498  next_token = cp_lexer_peek_token (parser->lexer);
15499  if (next_token->type != CPP_NAME
15500      && next_token->type != CPP_SCOPE
15501      && next_token->type != CPP_NESTED_NAME_SPECIFIER
15502      && next_token->type != CPP_TEMPLATE_ID)
15503    return false;
15504
15505  /* Parse tentatively; we are going to roll back all of the tokens
15506     consumed here.  */
15507  cp_parser_parse_tentatively (parser);
15508  /* Assume that we are looking at a constructor declarator.  */
15509  constructor_p = true;
15510
15511  /* Look for the optional `::' operator.  */
15512  cp_parser_global_scope_opt (parser,
15513			      /*current_scope_valid_p=*/false);
15514  /* Look for the nested-name-specifier.  */
15515  nested_name_p
15516    = (cp_parser_nested_name_specifier_opt (parser,
15517					    /*typename_keyword_p=*/false,
15518					    /*check_dependency_p=*/false,
15519					    /*type_p=*/false,
15520					    /*is_declaration=*/false)
15521       != NULL_TREE);
15522  /* Outside of a class-specifier, there must be a
15523     nested-name-specifier.  */
15524  if (!nested_name_p &&
15525      (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15526       || friend_p))
15527    constructor_p = false;
15528  /* If we still think that this might be a constructor-declarator,
15529     look for a class-name.  */
15530  if (constructor_p)
15531    {
15532      /* If we have:
15533
15534	   template <typename T> struct S { S(); };
15535	   template <typename T> S<T>::S ();
15536
15537	 we must recognize that the nested `S' names a class.
15538	 Similarly, for:
15539
15540	   template <typename T> S<T>::S<T> ();
15541
15542	 we must recognize that the nested `S' names a template.  */
15543      type_decl = cp_parser_class_name (parser,
15544					/*typename_keyword_p=*/false,
15545					/*template_keyword_p=*/false,
15546					none_type,
15547					/*check_dependency_p=*/false,
15548					/*class_head_p=*/false,
15549					/*is_declaration=*/false);
15550      /* If there was no class-name, then this is not a constructor.  */
15551      constructor_p = !cp_parser_error_occurred (parser);
15552    }
15553
15554  /* If we're still considering a constructor, we have to see a `(',
15555     to begin the parameter-declaration-clause, followed by either a
15556     `)', an `...', or a decl-specifier.  We need to check for a
15557     type-specifier to avoid being fooled into thinking that:
15558
15559       S::S (f) (int);
15560
15561     is a constructor.  (It is actually a function named `f' that
15562     takes one parameter (of type `int') and returns a value of type
15563     `S::S'.  */
15564  if (constructor_p
15565      && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15566    {
15567      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15568	  && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15569	  /* A parameter declaration begins with a decl-specifier,
15570	     which is either the "attribute" keyword, a storage class
15571	     specifier, or (usually) a type-specifier.  */
15572	  && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15573	{
15574	  tree type;
15575	  tree pushed_scope = NULL_TREE;
15576	  unsigned saved_num_template_parameter_lists;
15577
15578	  /* Names appearing in the type-specifier should be looked up
15579	     in the scope of the class.  */
15580	  if (current_class_type)
15581	    type = NULL_TREE;
15582	  else
15583	    {
15584	      type = TREE_TYPE (type_decl);
15585	      if (TREE_CODE (type) == TYPENAME_TYPE)
15586		{
15587		  type = resolve_typename_type (type,
15588						/*only_current_p=*/false);
15589		  if (type == error_mark_node)
15590		    {
15591		      cp_parser_abort_tentative_parse (parser);
15592		      return false;
15593		    }
15594		}
15595	      pushed_scope = push_scope (type);
15596	    }
15597
15598	  /* Inside the constructor parameter list, surrounding
15599	     template-parameter-lists do not apply.  */
15600	  saved_num_template_parameter_lists
15601	    = parser->num_template_parameter_lists;
15602	  parser->num_template_parameter_lists = 0;
15603
15604	  /* Look for the type-specifier.  */
15605	  cp_parser_type_specifier (parser,
15606				    CP_PARSER_FLAGS_NONE,
15607				    /*decl_specs=*/NULL,
15608				    /*is_declarator=*/true,
15609				    /*declares_class_or_enum=*/NULL,
15610				    /*is_cv_qualifier=*/NULL);
15611
15612	  parser->num_template_parameter_lists
15613	    = saved_num_template_parameter_lists;
15614
15615	  /* Leave the scope of the class.  */
15616	  if (pushed_scope)
15617	    pop_scope (pushed_scope);
15618
15619	  constructor_p = !cp_parser_error_occurred (parser);
15620	}
15621    }
15622  else
15623    constructor_p = false;
15624  /* We did not really want to consume any tokens.  */
15625  cp_parser_abort_tentative_parse (parser);
15626
15627  return constructor_p;
15628}
15629
15630/* Parse the definition of the function given by the DECL_SPECIFIERS,
15631   ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15632   they must be performed once we are in the scope of the function.
15633
15634   Returns the function defined.  */
15635
15636static tree
15637cp_parser_function_definition_from_specifiers_and_declarator
15638  (cp_parser* parser,
15639   cp_decl_specifier_seq *decl_specifiers,
15640   tree attributes,
15641   const cp_declarator *declarator)
15642{
15643  tree fn;
15644  bool success_p;
15645
15646  /* Begin the function-definition.  */
15647  success_p = start_function (decl_specifiers, declarator, attributes);
15648
15649  /* The things we're about to see are not directly qualified by any
15650     template headers we've seen thus far.  */
15651  reset_specialization ();
15652
15653  /* If there were names looked up in the decl-specifier-seq that we
15654     did not check, check them now.  We must wait until we are in the
15655     scope of the function to perform the checks, since the function
15656     might be a friend.  */
15657  perform_deferred_access_checks ();
15658
15659  if (!success_p)
15660    {
15661      /* Skip the entire function.  */
15662      cp_parser_skip_to_end_of_block_or_statement (parser);
15663      fn = error_mark_node;
15664    }
15665  else if (DECL_INITIAL (current_function_decl) != error_mark_node)
15666    {
15667      /* Seen already, skip it.  An error message has already been output.  */
15668      cp_parser_skip_to_end_of_block_or_statement (parser);
15669      fn = current_function_decl;
15670      current_function_decl = NULL_TREE;
15671      /* If this is a function from a class, pop the nested class.  */
15672      if (current_class_name)
15673	pop_nested_class ();
15674    }
15675  else
15676    fn = cp_parser_function_definition_after_declarator (parser,
15677							 /*inline_p=*/false);
15678
15679  return fn;
15680}
15681
15682/* Parse the part of a function-definition that follows the
15683   declarator.  INLINE_P is TRUE iff this function is an inline
15684   function defined with a class-specifier.
15685
15686   Returns the function defined.  */
15687
15688static tree
15689cp_parser_function_definition_after_declarator (cp_parser* parser,
15690						bool inline_p)
15691{
15692  tree fn;
15693  bool ctor_initializer_p = false;
15694  bool saved_in_unbraced_linkage_specification_p;
15695  bool saved_in_function_body;
15696  unsigned saved_num_template_parameter_lists;
15697
15698  saved_in_function_body = parser->in_function_body;
15699  parser->in_function_body = true;
15700  /* If the next token is `return', then the code may be trying to
15701     make use of the "named return value" extension that G++ used to
15702     support.  */
15703  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15704    {
15705      /* Consume the `return' keyword.  */
15706      cp_lexer_consume_token (parser->lexer);
15707      /* Look for the identifier that indicates what value is to be
15708	 returned.  */
15709      cp_parser_identifier (parser);
15710      /* Issue an error message.  */
15711      error ("named return values are no longer supported");
15712      /* Skip tokens until we reach the start of the function body.  */
15713      while (true)
15714	{
15715	  cp_token *token = cp_lexer_peek_token (parser->lexer);
15716	  if (token->type == CPP_OPEN_BRACE
15717	      || token->type == CPP_EOF
15718	      || token->type == CPP_PRAGMA_EOL)
15719	    break;
15720	  cp_lexer_consume_token (parser->lexer);
15721	}
15722    }
15723  /* The `extern' in `extern "C" void f () { ... }' does not apply to
15724     anything declared inside `f'.  */
15725  saved_in_unbraced_linkage_specification_p
15726    = parser->in_unbraced_linkage_specification_p;
15727  parser->in_unbraced_linkage_specification_p = false;
15728  /* Inside the function, surrounding template-parameter-lists do not
15729     apply.  */
15730  saved_num_template_parameter_lists
15731    = parser->num_template_parameter_lists;
15732  parser->num_template_parameter_lists = 0;
15733  /* If the next token is `try', then we are looking at a
15734     function-try-block.  */
15735  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15736    ctor_initializer_p = cp_parser_function_try_block (parser);
15737  /* A function-try-block includes the function-body, so we only do
15738     this next part if we're not processing a function-try-block.  */
15739  else
15740    ctor_initializer_p
15741      = cp_parser_ctor_initializer_opt_and_function_body (parser);
15742
15743  /* Finish the function.  */
15744  fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15745			(inline_p ? 2 : 0));
15746  /* Generate code for it, if necessary.  */
15747  expand_or_defer_fn (fn);
15748  /* Restore the saved values.  */
15749  parser->in_unbraced_linkage_specification_p
15750    = saved_in_unbraced_linkage_specification_p;
15751  parser->num_template_parameter_lists
15752    = saved_num_template_parameter_lists;
15753  parser->in_function_body = saved_in_function_body;
15754
15755  return fn;
15756}
15757
15758/* Parse a template-declaration, assuming that the `export' (and
15759   `extern') keywords, if present, has already been scanned.  MEMBER_P
15760   is as for cp_parser_template_declaration.  */
15761
15762static void
15763cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15764{
15765  tree decl = NULL_TREE;
15766  VEC (deferred_access_check,gc) *checks;
15767  tree parameter_list;
15768  bool friend_p = false;
15769  bool need_lang_pop;
15770
15771  /* Look for the `template' keyword.  */
15772  if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15773    return;
15774
15775  /* And the `<'.  */
15776  if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15777    return;
15778  if (at_class_scope_p () && current_function_decl)
15779    {
15780      /* 14.5.2.2 [temp.mem]
15781
15782         A local class shall not have member templates.  */
15783      error ("invalid declaration of member template in local class");
15784      cp_parser_skip_to_end_of_block_or_statement (parser);
15785      return;
15786    }
15787  /* [temp]
15788
15789     A template ... shall not have C linkage.  */
15790  if (current_lang_name == lang_name_c)
15791    {
15792      error ("template with C linkage");
15793      /* Give it C++ linkage to avoid confusing other parts of the
15794	 front end.  */
15795      push_lang_context (lang_name_cplusplus);
15796      need_lang_pop = true;
15797    }
15798  else
15799    need_lang_pop = false;
15800
15801  /* We cannot perform access checks on the template parameter
15802     declarations until we know what is being declared, just as we
15803     cannot check the decl-specifier list.  */
15804  push_deferring_access_checks (dk_deferred);
15805
15806  /* If the next token is `>', then we have an invalid
15807     specialization.  Rather than complain about an invalid template
15808     parameter, issue an error message here.  */
15809  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15810    {
15811      cp_parser_error (parser, "invalid explicit specialization");
15812      begin_specialization ();
15813      parameter_list = NULL_TREE;
15814    }
15815  else
15816    /* Parse the template parameters.  */
15817    parameter_list = cp_parser_template_parameter_list (parser);
15818
15819  /* Get the deferred access checks from the parameter list.  These
15820     will be checked once we know what is being declared, as for a
15821     member template the checks must be performed in the scope of the
15822     class containing the member.  */
15823  checks = get_deferred_access_checks ();
15824
15825  /* Look for the `>'.  */
15826  cp_parser_skip_to_end_of_template_parameter_list (parser);
15827  /* We just processed one more parameter list.  */
15828  ++parser->num_template_parameter_lists;
15829  /* If the next token is `template', there are more template
15830     parameters.  */
15831  if (cp_lexer_next_token_is_keyword (parser->lexer,
15832				      RID_TEMPLATE))
15833    cp_parser_template_declaration_after_export (parser, member_p);
15834  else
15835    {
15836      /* There are no access checks when parsing a template, as we do not
15837	 know if a specialization will be a friend.  */
15838      push_deferring_access_checks (dk_no_check);
15839      decl = cp_parser_single_declaration (parser,
15840					   checks,
15841					   member_p,
15842					   &friend_p);
15843      pop_deferring_access_checks ();
15844
15845      /* If this is a member template declaration, let the front
15846	 end know.  */
15847      if (member_p && !friend_p && decl)
15848	{
15849	  if (TREE_CODE (decl) == TYPE_DECL)
15850	    cp_parser_check_access_in_redeclaration (decl);
15851
15852	  decl = finish_member_template_decl (decl);
15853	}
15854      else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15855	make_friend_class (current_class_type, TREE_TYPE (decl),
15856			   /*complain=*/true);
15857    }
15858  /* We are done with the current parameter list.  */
15859  --parser->num_template_parameter_lists;
15860
15861  pop_deferring_access_checks ();
15862
15863  /* Finish up.  */
15864  finish_template_decl (parameter_list);
15865
15866  /* Register member declarations.  */
15867  if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15868    finish_member_declaration (decl);
15869  /* For the erroneous case of a template with C linkage, we pushed an
15870     implicit C++ linkage scope; exit that scope now.  */
15871  if (need_lang_pop)
15872    pop_lang_context ();
15873  /* If DECL is a function template, we must return to parse it later.
15874     (Even though there is no definition, there might be default
15875     arguments that need handling.)  */
15876  if (member_p && decl
15877      && (TREE_CODE (decl) == FUNCTION_DECL
15878	  || DECL_FUNCTION_TEMPLATE_P (decl)))
15879    TREE_VALUE (parser->unparsed_functions_queues)
15880      = tree_cons (NULL_TREE, decl,
15881		   TREE_VALUE (parser->unparsed_functions_queues));
15882}
15883
15884/* Perform the deferred access checks from a template-parameter-list.
15885   CHECKS is a TREE_LIST of access checks, as returned by
15886   get_deferred_access_checks.  */
15887
15888static void
15889cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
15890{
15891  ++processing_template_parmlist;
15892  perform_access_checks (checks);
15893  --processing_template_parmlist;
15894}
15895
15896/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15897   `function-definition' sequence.  MEMBER_P is true, this declaration
15898   appears in a class scope.
15899
15900   Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15901   *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15902
15903static tree
15904cp_parser_single_declaration (cp_parser* parser,
15905			      VEC (deferred_access_check,gc)* checks,
15906			      bool member_p,
15907			      bool* friend_p)
15908{
15909  int declares_class_or_enum;
15910  tree decl = NULL_TREE;
15911  cp_decl_specifier_seq decl_specifiers;
15912  bool function_definition_p = false;
15913
15914  /* This function is only used when processing a template
15915     declaration.  */
15916  gcc_assert (innermost_scope_kind () == sk_template_parms
15917	      || innermost_scope_kind () == sk_template_spec);
15918
15919  /* Defer access checks until we know what is being declared.  */
15920  push_deferring_access_checks (dk_deferred);
15921
15922  /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15923     alternative.  */
15924  cp_parser_decl_specifier_seq (parser,
15925				CP_PARSER_FLAGS_OPTIONAL,
15926				&decl_specifiers,
15927				&declares_class_or_enum);
15928  if (friend_p)
15929    *friend_p = cp_parser_friend_p (&decl_specifiers);
15930
15931  /* There are no template typedefs.  */
15932  if (decl_specifiers.specs[(int) ds_typedef])
15933    {
15934      error ("template declaration of %qs", "typedef");
15935      decl = error_mark_node;
15936    }
15937
15938  /* Gather up the access checks that occurred the
15939     decl-specifier-seq.  */
15940  stop_deferring_access_checks ();
15941
15942  /* Check for the declaration of a template class.  */
15943  if (declares_class_or_enum)
15944    {
15945      if (cp_parser_declares_only_class_p (parser))
15946	{
15947	  decl = shadow_tag (&decl_specifiers);
15948
15949	  /* In this case:
15950
15951	       struct C {
15952		 friend template <typename T> struct A<T>::B;
15953	       };
15954
15955	     A<T>::B will be represented by a TYPENAME_TYPE, and
15956	     therefore not recognized by shadow_tag.  */
15957	  if (friend_p && *friend_p
15958	      && !decl
15959	      && decl_specifiers.type
15960	      && TYPE_P (decl_specifiers.type))
15961	    decl = decl_specifiers.type;
15962
15963	  if (decl && decl != error_mark_node)
15964	    decl = TYPE_NAME (decl);
15965	  else
15966	    decl = error_mark_node;
15967
15968	  /* Perform access checks for template parameters.  */
15969	  cp_parser_perform_template_parameter_access_checks (checks);
15970	}
15971    }
15972  /* If it's not a template class, try for a template function.  If
15973     the next token is a `;', then this declaration does not declare
15974     anything.  But, if there were errors in the decl-specifiers, then
15975     the error might well have come from an attempted class-specifier.
15976     In that case, there's no need to warn about a missing declarator.  */
15977  if (!decl
15978      && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15979	  || decl_specifiers.type != error_mark_node))
15980    decl = cp_parser_init_declarator (parser,
15981				      &decl_specifiers,
15982				      checks,
15983				      /*function_definition_allowed_p=*/true,
15984				      member_p,
15985				      declares_class_or_enum,
15986				      &function_definition_p);
15987
15988  pop_deferring_access_checks ();
15989
15990  /* Clear any current qualification; whatever comes next is the start
15991     of something new.  */
15992  parser->scope = NULL_TREE;
15993  parser->qualifying_scope = NULL_TREE;
15994  parser->object_scope = NULL_TREE;
15995  /* Look for a trailing `;' after the declaration.  */
15996  if (!function_definition_p
15997      && (decl == error_mark_node
15998	  || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15999    cp_parser_skip_to_end_of_block_or_statement (parser);
16000
16001  return decl;
16002}
16003
16004/* Parse a cast-expression that is not the operand of a unary "&".  */
16005
16006static tree
16007cp_parser_simple_cast_expression (cp_parser *parser)
16008{
16009  return cp_parser_cast_expression (parser, /*address_p=*/false,
16010				    /*cast_p=*/false);
16011}
16012
16013/* Parse a functional cast to TYPE.  Returns an expression
16014   representing the cast.  */
16015
16016static tree
16017cp_parser_functional_cast (cp_parser* parser, tree type)
16018{
16019  tree expression_list;
16020  tree cast;
16021
16022  expression_list
16023    = cp_parser_parenthesized_expression_list (parser, false,
16024					       /*cast_p=*/true,
16025					       /*non_constant_p=*/NULL);
16026
16027  cast = build_functional_cast (type, expression_list);
16028  /* [expr.const]/1: In an integral constant expression "only type
16029     conversions to integral or enumeration type can be used".  */
16030  if (TREE_CODE (type) == TYPE_DECL)
16031    type = TREE_TYPE (type);
16032  if (cast != error_mark_node
16033      && !cast_valid_in_integral_constant_expression_p (type)
16034      && (cp_parser_non_integral_constant_expression
16035	  (parser, "a call to a constructor")))
16036    return error_mark_node;
16037  return cast;
16038}
16039
16040/* Save the tokens that make up the body of a member function defined
16041   in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
16042   already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
16043   specifiers applied to the declaration.  Returns the FUNCTION_DECL
16044   for the member function.  */
16045
16046static tree
16047cp_parser_save_member_function_body (cp_parser* parser,
16048				     cp_decl_specifier_seq *decl_specifiers,
16049				     cp_declarator *declarator,
16050				     tree attributes)
16051{
16052  cp_token *first;
16053  cp_token *last;
16054  tree fn;
16055
16056  /* Create the function-declaration.  */
16057  fn = start_method (decl_specifiers, declarator, attributes);
16058  /* If something went badly wrong, bail out now.  */
16059  if (fn == error_mark_node)
16060    {
16061      /* If there's a function-body, skip it.  */
16062      if (cp_parser_token_starts_function_definition_p
16063	  (cp_lexer_peek_token (parser->lexer)))
16064	cp_parser_skip_to_end_of_block_or_statement (parser);
16065      return error_mark_node;
16066    }
16067
16068  /* Remember it, if there default args to post process.  */
16069  cp_parser_save_default_args (parser, fn);
16070
16071  /* Save away the tokens that make up the body of the
16072     function.  */
16073  first = parser->lexer->next_token;
16074  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16075  /* Handle function try blocks.  */
16076  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16077    cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16078  last = parser->lexer->next_token;
16079
16080  /* Save away the inline definition; we will process it when the
16081     class is complete.  */
16082  DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16083  DECL_PENDING_INLINE_P (fn) = 1;
16084
16085  /* We need to know that this was defined in the class, so that
16086     friend templates are handled correctly.  */
16087  DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16088
16089  /* We're done with the inline definition.  */
16090  finish_method (fn);
16091
16092  /* Add FN to the queue of functions to be parsed later.  */
16093  TREE_VALUE (parser->unparsed_functions_queues)
16094    = tree_cons (NULL_TREE, fn,
16095		 TREE_VALUE (parser->unparsed_functions_queues));
16096
16097  return fn;
16098}
16099
16100/* Parse a template-argument-list, as well as the trailing ">" (but
16101   not the opening ">").  See cp_parser_template_argument_list for the
16102   return value.  */
16103
16104static tree
16105cp_parser_enclosed_template_argument_list (cp_parser* parser)
16106{
16107  tree arguments;
16108  tree saved_scope;
16109  tree saved_qualifying_scope;
16110  tree saved_object_scope;
16111  bool saved_greater_than_is_operator_p;
16112  bool saved_skip_evaluation;
16113
16114  /* [temp.names]
16115
16116     When parsing a template-id, the first non-nested `>' is taken as
16117     the end of the template-argument-list rather than a greater-than
16118     operator.  */
16119  saved_greater_than_is_operator_p
16120    = parser->greater_than_is_operator_p;
16121  parser->greater_than_is_operator_p = false;
16122  /* Parsing the argument list may modify SCOPE, so we save it
16123     here.  */
16124  saved_scope = parser->scope;
16125  saved_qualifying_scope = parser->qualifying_scope;
16126  saved_object_scope = parser->object_scope;
16127  /* We need to evaluate the template arguments, even though this
16128     template-id may be nested within a "sizeof".  */
16129  saved_skip_evaluation = skip_evaluation;
16130  skip_evaluation = false;
16131  /* Parse the template-argument-list itself.  */
16132  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16133    arguments = NULL_TREE;
16134  else
16135    arguments = cp_parser_template_argument_list (parser);
16136  /* Look for the `>' that ends the template-argument-list. If we find
16137     a '>>' instead, it's probably just a typo.  */
16138  if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16139    {
16140      if (!saved_greater_than_is_operator_p)
16141	{
16142	  /* If we're in a nested template argument list, the '>>' has
16143	    to be a typo for '> >'. We emit the error message, but we
16144	    continue parsing and we push a '>' as next token, so that
16145	    the argument list will be parsed correctly.  Note that the
16146	    global source location is still on the token before the
16147	    '>>', so we need to say explicitly where we want it.  */
16148	  cp_token *token = cp_lexer_peek_token (parser->lexer);
16149	  error ("%H%<>>%> should be %<> >%> "
16150		 "within a nested template argument list",
16151		 &token->location);
16152
16153	  /* ??? Proper recovery should terminate two levels of
16154	     template argument list here.  */
16155	  token->type = CPP_GREATER;
16156	}
16157      else
16158	{
16159	  /* If this is not a nested template argument list, the '>>'
16160	    is a typo for '>'. Emit an error message and continue.
16161	    Same deal about the token location, but here we can get it
16162	    right by consuming the '>>' before issuing the diagnostic.  */
16163	  cp_lexer_consume_token (parser->lexer);
16164	  error ("spurious %<>>%>, use %<>%> to terminate "
16165		 "a template argument list");
16166	}
16167    }
16168  else
16169    cp_parser_skip_to_end_of_template_parameter_list (parser);
16170  /* The `>' token might be a greater-than operator again now.  */
16171  parser->greater_than_is_operator_p
16172    = saved_greater_than_is_operator_p;
16173  /* Restore the SAVED_SCOPE.  */
16174  parser->scope = saved_scope;
16175  parser->qualifying_scope = saved_qualifying_scope;
16176  parser->object_scope = saved_object_scope;
16177  skip_evaluation = saved_skip_evaluation;
16178
16179  return arguments;
16180}
16181
16182/* MEMBER_FUNCTION is a member function, or a friend.  If default
16183   arguments, or the body of the function have not yet been parsed,
16184   parse them now.  */
16185
16186static void
16187cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16188{
16189  /* If this member is a template, get the underlying
16190     FUNCTION_DECL.  */
16191  if (DECL_FUNCTION_TEMPLATE_P (member_function))
16192    member_function = DECL_TEMPLATE_RESULT (member_function);
16193
16194  /* There should not be any class definitions in progress at this
16195     point; the bodies of members are only parsed outside of all class
16196     definitions.  */
16197  gcc_assert (parser->num_classes_being_defined == 0);
16198  /* While we're parsing the member functions we might encounter more
16199     classes.  We want to handle them right away, but we don't want
16200     them getting mixed up with functions that are currently in the
16201     queue.  */
16202  parser->unparsed_functions_queues
16203    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16204
16205  /* Make sure that any template parameters are in scope.  */
16206  maybe_begin_member_template_processing (member_function);
16207
16208  /* If the body of the function has not yet been parsed, parse it
16209     now.  */
16210  if (DECL_PENDING_INLINE_P (member_function))
16211    {
16212      tree function_scope;
16213      cp_token_cache *tokens;
16214
16215      /* The function is no longer pending; we are processing it.  */
16216      tokens = DECL_PENDING_INLINE_INFO (member_function);
16217      DECL_PENDING_INLINE_INFO (member_function) = NULL;
16218      DECL_PENDING_INLINE_P (member_function) = 0;
16219
16220      /* If this is a local class, enter the scope of the containing
16221	 function.  */
16222      function_scope = current_function_decl;
16223      if (function_scope)
16224	push_function_context_to (function_scope);
16225
16226
16227      /* Push the body of the function onto the lexer stack.  */
16228      cp_parser_push_lexer_for_tokens (parser, tokens);
16229
16230      /* Let the front end know that we going to be defining this
16231	 function.  */
16232      start_preparsed_function (member_function, NULL_TREE,
16233				SF_PRE_PARSED | SF_INCLASS_INLINE);
16234
16235      /* Don't do access checking if it is a templated function.  */
16236      if (processing_template_decl)
16237	push_deferring_access_checks (dk_no_check);
16238
16239      /* Now, parse the body of the function.  */
16240      cp_parser_function_definition_after_declarator (parser,
16241						      /*inline_p=*/true);
16242
16243      if (processing_template_decl)
16244	pop_deferring_access_checks ();
16245
16246      /* Leave the scope of the containing function.  */
16247      if (function_scope)
16248	pop_function_context_from (function_scope);
16249      cp_parser_pop_lexer (parser);
16250    }
16251
16252  /* Remove any template parameters from the symbol table.  */
16253  maybe_end_member_template_processing ();
16254
16255  /* Restore the queue.  */
16256  parser->unparsed_functions_queues
16257    = TREE_CHAIN (parser->unparsed_functions_queues);
16258}
16259
16260/* If DECL contains any default args, remember it on the unparsed
16261   functions queue.  */
16262
16263static void
16264cp_parser_save_default_args (cp_parser* parser, tree decl)
16265{
16266  tree probe;
16267
16268  for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16269       probe;
16270       probe = TREE_CHAIN (probe))
16271    if (TREE_PURPOSE (probe))
16272      {
16273	TREE_PURPOSE (parser->unparsed_functions_queues)
16274	  = tree_cons (current_class_type, decl,
16275		       TREE_PURPOSE (parser->unparsed_functions_queues));
16276	break;
16277      }
16278}
16279
16280/* FN is a FUNCTION_DECL which may contains a parameter with an
16281   unparsed DEFAULT_ARG.  Parse the default args now.  This function
16282   assumes that the current scope is the scope in which the default
16283   argument should be processed.  */
16284
16285static void
16286cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16287{
16288  bool saved_local_variables_forbidden_p;
16289  tree parm;
16290
16291  /* While we're parsing the default args, we might (due to the
16292     statement expression extension) encounter more classes.  We want
16293     to handle them right away, but we don't want them getting mixed
16294     up with default args that are currently in the queue.  */
16295  parser->unparsed_functions_queues
16296    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16297
16298  /* Local variable names (and the `this' keyword) may not appear
16299     in a default argument.  */
16300  saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16301  parser->local_variables_forbidden_p = true;
16302
16303  for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16304       parm;
16305       parm = TREE_CHAIN (parm))
16306    {
16307      cp_token_cache *tokens;
16308      tree default_arg = TREE_PURPOSE (parm);
16309      tree parsed_arg;
16310      VEC(tree,gc) *insts;
16311      tree copy;
16312      unsigned ix;
16313
16314      if (!default_arg)
16315	continue;
16316
16317      if (TREE_CODE (default_arg) != DEFAULT_ARG)
16318	/* This can happen for a friend declaration for a function
16319	   already declared with default arguments.  */
16320	continue;
16321
16322       /* Push the saved tokens for the default argument onto the parser's
16323	  lexer stack.  */
16324      tokens = DEFARG_TOKENS (default_arg);
16325      cp_parser_push_lexer_for_tokens (parser, tokens);
16326
16327      /* Parse the assignment-expression.  */
16328      parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16329
16330      if (!processing_template_decl)
16331	parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16332
16333      TREE_PURPOSE (parm) = parsed_arg;
16334
16335      /* Update any instantiations we've already created.  */
16336      for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16337	   VEC_iterate (tree, insts, ix, copy); ix++)
16338	TREE_PURPOSE (copy) = parsed_arg;
16339
16340      /* If the token stream has not been completely used up, then
16341	 there was extra junk after the end of the default
16342	 argument.  */
16343      if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16344	cp_parser_error (parser, "expected %<,%>");
16345
16346      /* Revert to the main lexer.  */
16347      cp_parser_pop_lexer (parser);
16348    }
16349
16350  /* Make sure no default arg is missing.  */
16351  check_default_args (fn);
16352
16353  /* Restore the state of local_variables_forbidden_p.  */
16354  parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16355
16356  /* Restore the queue.  */
16357  parser->unparsed_functions_queues
16358    = TREE_CHAIN (parser->unparsed_functions_queues);
16359}
16360
16361/* Parse the operand of `sizeof' (or a similar operator).  Returns
16362   either a TYPE or an expression, depending on the form of the
16363   input.  The KEYWORD indicates which kind of expression we have
16364   encountered.  */
16365
16366static tree
16367cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16368{
16369  static const char *format;
16370  tree expr = NULL_TREE;
16371  const char *saved_message;
16372  bool saved_integral_constant_expression_p;
16373  bool saved_non_integral_constant_expression_p;
16374
16375  /* Initialize FORMAT the first time we get here.  */
16376  if (!format)
16377    format = "types may not be defined in '%s' expressions";
16378
16379  /* Types cannot be defined in a `sizeof' expression.  Save away the
16380     old message.  */
16381  saved_message = parser->type_definition_forbidden_message;
16382  /* And create the new one.  */
16383  parser->type_definition_forbidden_message
16384    = XNEWVEC (const char, strlen (format)
16385	       + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16386	       + 1 /* `\0' */);
16387  sprintf ((char *) parser->type_definition_forbidden_message,
16388	   format, IDENTIFIER_POINTER (ridpointers[keyword]));
16389
16390  /* The restrictions on constant-expressions do not apply inside
16391     sizeof expressions.  */
16392  saved_integral_constant_expression_p
16393    = parser->integral_constant_expression_p;
16394  saved_non_integral_constant_expression_p
16395    = parser->non_integral_constant_expression_p;
16396  parser->integral_constant_expression_p = false;
16397
16398  /* Do not actually evaluate the expression.  */
16399  ++skip_evaluation;
16400  /* If it's a `(', then we might be looking at the type-id
16401     construction.  */
16402  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16403    {
16404      tree type;
16405      bool saved_in_type_id_in_expr_p;
16406
16407      /* We can't be sure yet whether we're looking at a type-id or an
16408	 expression.  */
16409      cp_parser_parse_tentatively (parser);
16410      /* Consume the `('.  */
16411      cp_lexer_consume_token (parser->lexer);
16412      /* Parse the type-id.  */
16413      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16414      parser->in_type_id_in_expr_p = true;
16415      type = cp_parser_type_id (parser);
16416      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16417      /* Now, look for the trailing `)'.  */
16418      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16419      /* If all went well, then we're done.  */
16420      if (cp_parser_parse_definitely (parser))
16421	{
16422	  cp_decl_specifier_seq decl_specs;
16423
16424	  /* Build a trivial decl-specifier-seq.  */
16425	  clear_decl_specs (&decl_specs);
16426	  decl_specs.type = type;
16427
16428	  /* Call grokdeclarator to figure out what type this is.  */
16429	  expr = grokdeclarator (NULL,
16430				 &decl_specs,
16431				 TYPENAME,
16432				 /*initialized=*/0,
16433				 /*attrlist=*/NULL);
16434	}
16435    }
16436
16437  /* If the type-id production did not work out, then we must be
16438     looking at the unary-expression production.  */
16439  if (!expr)
16440    expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16441				       /*cast_p=*/false);
16442  /* Go back to evaluating expressions.  */
16443  --skip_evaluation;
16444
16445  /* Free the message we created.  */
16446  free ((char *) parser->type_definition_forbidden_message);
16447  /* And restore the old one.  */
16448  parser->type_definition_forbidden_message = saved_message;
16449  parser->integral_constant_expression_p
16450    = saved_integral_constant_expression_p;
16451  parser->non_integral_constant_expression_p
16452    = saved_non_integral_constant_expression_p;
16453
16454  return expr;
16455}
16456
16457/* If the current declaration has no declarator, return true.  */
16458
16459static bool
16460cp_parser_declares_only_class_p (cp_parser *parser)
16461{
16462  /* If the next token is a `;' or a `,' then there is no
16463     declarator.  */
16464  return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16465	  || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16466}
16467
16468/* Update the DECL_SPECS to reflect the storage class indicated by
16469   KEYWORD.  */
16470
16471static void
16472cp_parser_set_storage_class (cp_parser *parser,
16473			     cp_decl_specifier_seq *decl_specs,
16474			     enum rid keyword)
16475{
16476  cp_storage_class storage_class;
16477
16478  if (parser->in_unbraced_linkage_specification_p)
16479    {
16480      error ("invalid use of %qD in linkage specification",
16481	     ridpointers[keyword]);
16482      return;
16483    }
16484  else if (decl_specs->storage_class != sc_none)
16485    {
16486      decl_specs->conflicting_specifiers_p = true;
16487      return;
16488    }
16489
16490  if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16491      && decl_specs->specs[(int) ds_thread])
16492    {
16493      error ("%<__thread%> before %qD", ridpointers[keyword]);
16494      decl_specs->specs[(int) ds_thread] = 0;
16495    }
16496
16497  switch (keyword)
16498    {
16499    case RID_AUTO:
16500      storage_class = sc_auto;
16501      break;
16502    case RID_REGISTER:
16503      storage_class = sc_register;
16504      break;
16505    case RID_STATIC:
16506      storage_class = sc_static;
16507      break;
16508    case RID_EXTERN:
16509      storage_class = sc_extern;
16510      break;
16511    case RID_MUTABLE:
16512      storage_class = sc_mutable;
16513      break;
16514    default:
16515      gcc_unreachable ();
16516    }
16517  decl_specs->storage_class = storage_class;
16518
16519  /* A storage class specifier cannot be applied alongside a typedef
16520     specifier. If there is a typedef specifier present then set
16521     conflicting_specifiers_p which will trigger an error later
16522     on in grokdeclarator. */
16523  if (decl_specs->specs[(int)ds_typedef])
16524    decl_specs->conflicting_specifiers_p = true;
16525}
16526
16527/* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16528   is true, the type is a user-defined type; otherwise it is a
16529   built-in type specified by a keyword.  */
16530
16531static void
16532cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16533			      tree type_spec,
16534			      bool user_defined_p)
16535{
16536  decl_specs->any_specifiers_p = true;
16537
16538  /* If the user tries to redeclare bool or wchar_t (with, for
16539     example, in "typedef int wchar_t;") we remember that this is what
16540     happened.  In system headers, we ignore these declarations so
16541     that G++ can work with system headers that are not C++-safe.  */
16542  if (decl_specs->specs[(int) ds_typedef]
16543      && !user_defined_p
16544      && (type_spec == boolean_type_node
16545	  || type_spec == wchar_type_node)
16546      && (decl_specs->type
16547	  || decl_specs->specs[(int) ds_long]
16548	  || decl_specs->specs[(int) ds_short]
16549	  || decl_specs->specs[(int) ds_unsigned]
16550	  || decl_specs->specs[(int) ds_signed]))
16551    {
16552      decl_specs->redefined_builtin_type = type_spec;
16553      if (!decl_specs->type)
16554	{
16555	  decl_specs->type = type_spec;
16556	  decl_specs->user_defined_type_p = false;
16557	}
16558    }
16559  else if (decl_specs->type)
16560    decl_specs->multiple_types_p = true;
16561  else
16562    {
16563      decl_specs->type = type_spec;
16564      decl_specs->user_defined_type_p = user_defined_p;
16565      decl_specs->redefined_builtin_type = NULL_TREE;
16566    }
16567}
16568
16569/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16570   Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16571
16572static bool
16573cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16574{
16575  return decl_specifiers->specs[(int) ds_friend] != 0;
16576}
16577
16578/* If the next token is of the indicated TYPE, consume it.  Otherwise,
16579   issue an error message indicating that TOKEN_DESC was expected.
16580
16581   Returns the token consumed, if the token had the appropriate type.
16582   Otherwise, returns NULL.  */
16583
16584static cp_token *
16585cp_parser_require (cp_parser* parser,
16586		   enum cpp_ttype type,
16587		   const char* token_desc)
16588{
16589  if (cp_lexer_next_token_is (parser->lexer, type))
16590    return cp_lexer_consume_token (parser->lexer);
16591  else
16592    {
16593      /* Output the MESSAGE -- unless we're parsing tentatively.  */
16594      if (!cp_parser_simulate_error (parser))
16595	{
16596	  char *message = concat ("expected ", token_desc, NULL);
16597	  cp_parser_error (parser, message);
16598	  free (message);
16599	}
16600      return NULL;
16601    }
16602}
16603
16604/* An error message is produced if the next token is not '>'.
16605   All further tokens are skipped until the desired token is
16606   found or '{', '}', ';' or an unbalanced ')' or ']'.  */
16607
16608static void
16609cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16610{
16611  /* Current level of '< ... >'.  */
16612  unsigned level = 0;
16613  /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
16614  unsigned nesting_depth = 0;
16615
16616  /* Are we ready, yet?  If not, issue error message.  */
16617  if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16618    return;
16619
16620  /* Skip tokens until the desired token is found.  */
16621  while (true)
16622    {
16623      /* Peek at the next token.  */
16624      switch (cp_lexer_peek_token (parser->lexer)->type)
16625	{
16626	case CPP_LESS:
16627	  if (!nesting_depth)
16628	    ++level;
16629	  break;
16630
16631	case CPP_GREATER:
16632	  if (!nesting_depth && level-- == 0)
16633	    {
16634	      /* We've reached the token we want, consume it and stop.  */
16635	      cp_lexer_consume_token (parser->lexer);
16636	      return;
16637	    }
16638	  break;
16639
16640	case CPP_OPEN_PAREN:
16641	case CPP_OPEN_SQUARE:
16642	  ++nesting_depth;
16643	  break;
16644
16645	case CPP_CLOSE_PAREN:
16646	case CPP_CLOSE_SQUARE:
16647	  if (nesting_depth-- == 0)
16648	    return;
16649	  break;
16650
16651	case CPP_EOF:
16652	case CPP_PRAGMA_EOL:
16653	case CPP_SEMICOLON:
16654	case CPP_OPEN_BRACE:
16655	case CPP_CLOSE_BRACE:
16656	  /* The '>' was probably forgotten, don't look further.  */
16657	  return;
16658
16659	default:
16660	  break;
16661	}
16662
16663      /* Consume this token.  */
16664      cp_lexer_consume_token (parser->lexer);
16665    }
16666}
16667
16668/* If the next token is the indicated keyword, consume it.  Otherwise,
16669   issue an error message indicating that TOKEN_DESC was expected.
16670
16671   Returns the token consumed, if the token had the appropriate type.
16672   Otherwise, returns NULL.  */
16673
16674static cp_token *
16675cp_parser_require_keyword (cp_parser* parser,
16676			   enum rid keyword,
16677			   const char* token_desc)
16678{
16679  cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16680
16681  if (token && token->keyword != keyword)
16682    {
16683      dyn_string_t error_msg;
16684
16685      /* Format the error message.  */
16686      error_msg = dyn_string_new (0);
16687      dyn_string_append_cstr (error_msg, "expected ");
16688      dyn_string_append_cstr (error_msg, token_desc);
16689      cp_parser_error (parser, error_msg->s);
16690      dyn_string_delete (error_msg);
16691      return NULL;
16692    }
16693
16694  return token;
16695}
16696
16697/* Returns TRUE iff TOKEN is a token that can begin the body of a
16698   function-definition.  */
16699
16700static bool
16701cp_parser_token_starts_function_definition_p (cp_token* token)
16702{
16703  return (/* An ordinary function-body begins with an `{'.  */
16704	  token->type == CPP_OPEN_BRACE
16705	  /* A ctor-initializer begins with a `:'.  */
16706	  || token->type == CPP_COLON
16707	  /* A function-try-block begins with `try'.  */
16708	  || token->keyword == RID_TRY
16709	  /* The named return value extension begins with `return'.  */
16710	  || token->keyword == RID_RETURN);
16711}
16712
16713/* Returns TRUE iff the next token is the ":" or "{" beginning a class
16714   definition.  */
16715
16716static bool
16717cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16718{
16719  cp_token *token;
16720
16721  token = cp_lexer_peek_token (parser->lexer);
16722  return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16723}
16724
16725/* Returns TRUE iff the next token is the "," or ">" ending a
16726   template-argument.  */
16727
16728static bool
16729cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16730{
16731  cp_token *token;
16732
16733  token = cp_lexer_peek_token (parser->lexer);
16734  return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16735}
16736
16737/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16738   (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16739
16740static bool
16741cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16742						     size_t n)
16743{
16744  cp_token *token;
16745
16746  token = cp_lexer_peek_nth_token (parser->lexer, n);
16747  if (token->type == CPP_LESS)
16748    return true;
16749  /* Check for the sequence `<::' in the original code. It would be lexed as
16750     `[:', where `[' is a digraph, and there is no whitespace before
16751     `:'.  */
16752  if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16753    {
16754      cp_token *token2;
16755      token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16756      if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16757	return true;
16758    }
16759  return false;
16760}
16761
16762/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16763   or none_type otherwise.  */
16764
16765static enum tag_types
16766cp_parser_token_is_class_key (cp_token* token)
16767{
16768  switch (token->keyword)
16769    {
16770    case RID_CLASS:
16771      return class_type;
16772    case RID_STRUCT:
16773      return record_type;
16774    case RID_UNION:
16775      return union_type;
16776
16777    default:
16778      return none_type;
16779    }
16780}
16781
16782/* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16783
16784static void
16785cp_parser_check_class_key (enum tag_types class_key, tree type)
16786{
16787  if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16788    pedwarn ("%qs tag used in naming %q#T",
16789	    class_key == union_type ? "union"
16790	     : class_key == record_type ? "struct" : "class",
16791	     type);
16792}
16793
16794/* Issue an error message if DECL is redeclared with different
16795   access than its original declaration [class.access.spec/3].
16796   This applies to nested classes and nested class templates.
16797   [class.mem/1].  */
16798
16799static void
16800cp_parser_check_access_in_redeclaration (tree decl)
16801{
16802  if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16803    return;
16804
16805  if ((TREE_PRIVATE (decl)
16806       != (current_access_specifier == access_private_node))
16807      || (TREE_PROTECTED (decl)
16808	  != (current_access_specifier == access_protected_node)))
16809    error ("%qD redeclared with different access", decl);
16810}
16811
16812/* Look for the `template' keyword, as a syntactic disambiguator.
16813   Return TRUE iff it is present, in which case it will be
16814   consumed.  */
16815
16816static bool
16817cp_parser_optional_template_keyword (cp_parser *parser)
16818{
16819  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16820    {
16821      /* The `template' keyword can only be used within templates;
16822	 outside templates the parser can always figure out what is a
16823	 template and what is not.  */
16824      if (!processing_template_decl)
16825	{
16826	  error ("%<template%> (as a disambiguator) is only allowed "
16827		 "within templates");
16828	  /* If this part of the token stream is rescanned, the same
16829	     error message would be generated.  So, we purge the token
16830	     from the stream.  */
16831	  cp_lexer_purge_token (parser->lexer);
16832	  return false;
16833	}
16834      else
16835	{
16836	  /* Consume the `template' keyword.  */
16837	  cp_lexer_consume_token (parser->lexer);
16838	  return true;
16839	}
16840    }
16841
16842  return false;
16843}
16844
16845/* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16846   set PARSER->SCOPE, and perform other related actions.  */
16847
16848static void
16849cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16850{
16851  int i;
16852  struct tree_check *check_value;
16853  deferred_access_check *chk;
16854  VEC (deferred_access_check,gc) *checks;
16855
16856  /* Get the stored value.  */
16857  check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
16858  /* Perform any access checks that were deferred.  */
16859  checks = check_value->checks;
16860  if (checks)
16861    {
16862      for (i = 0 ;
16863	   VEC_iterate (deferred_access_check, checks, i, chk) ;
16864	   ++i)
16865	{
16866	  perform_or_defer_access_check (chk->binfo,
16867					 chk->decl,
16868					 chk->diag_decl);
16869	}
16870    }
16871  /* Set the scope from the stored value.  */
16872  parser->scope = check_value->value;
16873  parser->qualifying_scope = check_value->qualifying_scope;
16874  parser->object_scope = NULL_TREE;
16875}
16876
16877/* Consume tokens up through a non-nested END token.  */
16878
16879static void
16880cp_parser_cache_group (cp_parser *parser,
16881		       enum cpp_ttype end,
16882		       unsigned depth)
16883{
16884  while (true)
16885    {
16886      cp_token *token;
16887
16888      /* Abort a parenthesized expression if we encounter a brace.  */
16889      if ((end == CPP_CLOSE_PAREN || depth == 0)
16890	  && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16891	return;
16892      /* If we've reached the end of the file, stop.  */
16893      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16894	  || (end != CPP_PRAGMA_EOL
16895	      && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16896	return;
16897      /* Consume the next token.  */
16898      token = cp_lexer_consume_token (parser->lexer);
16899      /* See if it starts a new group.  */
16900      if (token->type == CPP_OPEN_BRACE)
16901	{
16902	  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16903	  if (depth == 0)
16904	    return;
16905	}
16906      else if (token->type == CPP_OPEN_PAREN)
16907	cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16908      else if (token->type == CPP_PRAGMA)
16909	cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16910      else if (token->type == end)
16911	return;
16912    }
16913}
16914
16915/* Begin parsing tentatively.  We always save tokens while parsing
16916   tentatively so that if the tentative parsing fails we can restore the
16917   tokens.  */
16918
16919static void
16920cp_parser_parse_tentatively (cp_parser* parser)
16921{
16922  /* Enter a new parsing context.  */
16923  parser->context = cp_parser_context_new (parser->context);
16924  /* Begin saving tokens.  */
16925  cp_lexer_save_tokens (parser->lexer);
16926  /* In order to avoid repetitive access control error messages,
16927     access checks are queued up until we are no longer parsing
16928     tentatively.  */
16929  push_deferring_access_checks (dk_deferred);
16930}
16931
16932/* Commit to the currently active tentative parse.  */
16933
16934static void
16935cp_parser_commit_to_tentative_parse (cp_parser* parser)
16936{
16937  cp_parser_context *context;
16938  cp_lexer *lexer;
16939
16940  /* Mark all of the levels as committed.  */
16941  lexer = parser->lexer;
16942  for (context = parser->context; context->next; context = context->next)
16943    {
16944      if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16945	break;
16946      context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16947      while (!cp_lexer_saving_tokens (lexer))
16948	lexer = lexer->next;
16949      cp_lexer_commit_tokens (lexer);
16950    }
16951}
16952
16953/* Abort the currently active tentative parse.  All consumed tokens
16954   will be rolled back, and no diagnostics will be issued.  */
16955
16956static void
16957cp_parser_abort_tentative_parse (cp_parser* parser)
16958{
16959  cp_parser_simulate_error (parser);
16960  /* Now, pretend that we want to see if the construct was
16961     successfully parsed.  */
16962  cp_parser_parse_definitely (parser);
16963}
16964
16965/* Stop parsing tentatively.  If a parse error has occurred, restore the
16966   token stream.  Otherwise, commit to the tokens we have consumed.
16967   Returns true if no error occurred; false otherwise.  */
16968
16969static bool
16970cp_parser_parse_definitely (cp_parser* parser)
16971{
16972  bool error_occurred;
16973  cp_parser_context *context;
16974
16975  /* Remember whether or not an error occurred, since we are about to
16976     destroy that information.  */
16977  error_occurred = cp_parser_error_occurred (parser);
16978  /* Remove the topmost context from the stack.  */
16979  context = parser->context;
16980  parser->context = context->next;
16981  /* If no parse errors occurred, commit to the tentative parse.  */
16982  if (!error_occurred)
16983    {
16984      /* Commit to the tokens read tentatively, unless that was
16985	 already done.  */
16986      if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16987	cp_lexer_commit_tokens (parser->lexer);
16988
16989      pop_to_parent_deferring_access_checks ();
16990    }
16991  /* Otherwise, if errors occurred, roll back our state so that things
16992     are just as they were before we began the tentative parse.  */
16993  else
16994    {
16995      cp_lexer_rollback_tokens (parser->lexer);
16996      pop_deferring_access_checks ();
16997    }
16998  /* Add the context to the front of the free list.  */
16999  context->next = cp_parser_context_free_list;
17000  cp_parser_context_free_list = context;
17001
17002  return !error_occurred;
17003}
17004
17005/* Returns true if we are parsing tentatively and are not committed to
17006   this tentative parse.  */
17007
17008static bool
17009cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17010{
17011  return (cp_parser_parsing_tentatively (parser)
17012	  && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17013}
17014
17015/* Returns nonzero iff an error has occurred during the most recent
17016   tentative parse.  */
17017
17018static bool
17019cp_parser_error_occurred (cp_parser* parser)
17020{
17021  return (cp_parser_parsing_tentatively (parser)
17022	  && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17023}
17024
17025/* Returns nonzero if GNU extensions are allowed.  */
17026
17027static bool
17028cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17029{
17030  return parser->allow_gnu_extensions_p;
17031}
17032
17033/* Objective-C++ Productions */
17034
17035
17036/* Parse an Objective-C expression, which feeds into a primary-expression
17037   above.
17038
17039   objc-expression:
17040     objc-message-expression
17041     objc-string-literal
17042     objc-encode-expression
17043     objc-protocol-expression
17044     objc-selector-expression
17045
17046  Returns a tree representation of the expression.  */
17047
17048static tree
17049cp_parser_objc_expression (cp_parser* parser)
17050{
17051  /* Try to figure out what kind of declaration is present.  */
17052  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17053
17054  switch (kwd->type)
17055    {
17056    case CPP_OPEN_SQUARE:
17057      return cp_parser_objc_message_expression (parser);
17058
17059    case CPP_OBJC_STRING:
17060      kwd = cp_lexer_consume_token (parser->lexer);
17061      return objc_build_string_object (kwd->u.value);
17062
17063    case CPP_KEYWORD:
17064      switch (kwd->keyword)
17065	{
17066	case RID_AT_ENCODE:
17067	  return cp_parser_objc_encode_expression (parser);
17068
17069	case RID_AT_PROTOCOL:
17070	  return cp_parser_objc_protocol_expression (parser);
17071
17072	case RID_AT_SELECTOR:
17073	  return cp_parser_objc_selector_expression (parser);
17074
17075	default:
17076	  break;
17077	}
17078    default:
17079      error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17080      cp_parser_skip_to_end_of_block_or_statement (parser);
17081    }
17082
17083  return error_mark_node;
17084}
17085
17086/* Parse an Objective-C message expression.
17087
17088   objc-message-expression:
17089     [ objc-message-receiver objc-message-args ]
17090
17091   Returns a representation of an Objective-C message.  */
17092
17093static tree
17094cp_parser_objc_message_expression (cp_parser* parser)
17095{
17096  tree receiver, messageargs;
17097
17098  cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
17099  receiver = cp_parser_objc_message_receiver (parser);
17100  messageargs = cp_parser_objc_message_args (parser);
17101  cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17102
17103  return objc_build_message_expr (build_tree_list (receiver, messageargs));
17104}
17105
17106/* Parse an objc-message-receiver.
17107
17108   objc-message-receiver:
17109     expression
17110     simple-type-specifier
17111
17112  Returns a representation of the type or expression.  */
17113
17114static tree
17115cp_parser_objc_message_receiver (cp_parser* parser)
17116{
17117  tree rcv;
17118
17119  /* An Objective-C message receiver may be either (1) a type
17120     or (2) an expression.  */
17121  cp_parser_parse_tentatively (parser);
17122  rcv = cp_parser_expression (parser, false);
17123
17124  if (cp_parser_parse_definitely (parser))
17125    return rcv;
17126
17127  rcv = cp_parser_simple_type_specifier (parser,
17128					 /*decl_specs=*/NULL,
17129					 CP_PARSER_FLAGS_NONE);
17130
17131  return objc_get_class_reference (rcv);
17132}
17133
17134/* Parse the arguments and selectors comprising an Objective-C message.
17135
17136   objc-message-args:
17137     objc-selector
17138     objc-selector-args
17139     objc-selector-args , objc-comma-args
17140
17141   objc-selector-args:
17142     objc-selector [opt] : assignment-expression
17143     objc-selector-args objc-selector [opt] : assignment-expression
17144
17145   objc-comma-args:
17146     assignment-expression
17147     objc-comma-args , assignment-expression
17148
17149   Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17150   selector arguments and TREE_VALUE containing a list of comma
17151   arguments.  */
17152
17153static tree
17154cp_parser_objc_message_args (cp_parser* parser)
17155{
17156  tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17157  bool maybe_unary_selector_p = true;
17158  cp_token *token = cp_lexer_peek_token (parser->lexer);
17159
17160  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17161    {
17162      tree selector = NULL_TREE, arg;
17163
17164      if (token->type != CPP_COLON)
17165	selector = cp_parser_objc_selector (parser);
17166
17167      /* Detect if we have a unary selector.  */
17168      if (maybe_unary_selector_p
17169	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17170	return build_tree_list (selector, NULL_TREE);
17171
17172      maybe_unary_selector_p = false;
17173      cp_parser_require (parser, CPP_COLON, "`:'");
17174      arg = cp_parser_assignment_expression (parser, false);
17175
17176      sel_args
17177	= chainon (sel_args,
17178		   build_tree_list (selector, arg));
17179
17180      token = cp_lexer_peek_token (parser->lexer);
17181    }
17182
17183  /* Handle non-selector arguments, if any. */
17184  while (token->type == CPP_COMMA)
17185    {
17186      tree arg;
17187
17188      cp_lexer_consume_token (parser->lexer);
17189      arg = cp_parser_assignment_expression (parser, false);
17190
17191      addl_args
17192	= chainon (addl_args,
17193		   build_tree_list (NULL_TREE, arg));
17194
17195      token = cp_lexer_peek_token (parser->lexer);
17196    }
17197
17198  return build_tree_list (sel_args, addl_args);
17199}
17200
17201/* Parse an Objective-C encode expression.
17202
17203   objc-encode-expression:
17204     @encode objc-typename
17205
17206   Returns an encoded representation of the type argument.  */
17207
17208static tree
17209cp_parser_objc_encode_expression (cp_parser* parser)
17210{
17211  tree type;
17212
17213  cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
17214  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17215  type = complete_type (cp_parser_type_id (parser));
17216  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17217
17218  if (!type)
17219    {
17220      error ("%<@encode%> must specify a type as an argument");
17221      return error_mark_node;
17222    }
17223
17224  return objc_build_encode_expr (type);
17225}
17226
17227/* Parse an Objective-C @defs expression.  */
17228
17229static tree
17230cp_parser_objc_defs_expression (cp_parser *parser)
17231{
17232  tree name;
17233
17234  cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
17235  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17236  name = cp_parser_identifier (parser);
17237  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17238
17239  return objc_get_class_ivars (name);
17240}
17241
17242/* Parse an Objective-C protocol expression.
17243
17244  objc-protocol-expression:
17245    @protocol ( identifier )
17246
17247  Returns a representation of the protocol expression.  */
17248
17249static tree
17250cp_parser_objc_protocol_expression (cp_parser* parser)
17251{
17252  tree proto;
17253
17254  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17255  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17256  proto = cp_parser_identifier (parser);
17257  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17258
17259  return objc_build_protocol_expr (proto);
17260}
17261
17262/* Parse an Objective-C selector expression.
17263
17264   objc-selector-expression:
17265     @selector ( objc-method-signature )
17266
17267   objc-method-signature:
17268     objc-selector
17269     objc-selector-seq
17270
17271   objc-selector-seq:
17272     objc-selector :
17273     objc-selector-seq objc-selector :
17274
17275  Returns a representation of the method selector.  */
17276
17277static tree
17278cp_parser_objc_selector_expression (cp_parser* parser)
17279{
17280  tree sel_seq = NULL_TREE;
17281  bool maybe_unary_selector_p = true;
17282  cp_token *token;
17283
17284  cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
17285  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17286  token = cp_lexer_peek_token (parser->lexer);
17287
17288  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17289	 || token->type == CPP_SCOPE)
17290    {
17291      tree selector = NULL_TREE;
17292
17293      if (token->type != CPP_COLON
17294	  || token->type == CPP_SCOPE)
17295	selector = cp_parser_objc_selector (parser);
17296
17297      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17298	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17299	{
17300	  /* Detect if we have a unary selector.  */
17301	  if (maybe_unary_selector_p)
17302	    {
17303	      sel_seq = selector;
17304	      goto finish_selector;
17305	    }
17306	  else
17307	    {
17308	      cp_parser_error (parser, "expected %<:%>");
17309	    }
17310	}
17311      maybe_unary_selector_p = false;
17312      token = cp_lexer_consume_token (parser->lexer);
17313
17314      if (token->type == CPP_SCOPE)
17315	{
17316	  sel_seq
17317	    = chainon (sel_seq,
17318		       build_tree_list (selector, NULL_TREE));
17319	  sel_seq
17320	    = chainon (sel_seq,
17321		       build_tree_list (NULL_TREE, NULL_TREE));
17322	}
17323      else
17324	sel_seq
17325	  = chainon (sel_seq,
17326		     build_tree_list (selector, NULL_TREE));
17327
17328      token = cp_lexer_peek_token (parser->lexer);
17329    }
17330
17331 finish_selector:
17332  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17333
17334  return objc_build_selector_expr (sel_seq);
17335}
17336
17337/* Parse a list of identifiers.
17338
17339   objc-identifier-list:
17340     identifier
17341     objc-identifier-list , identifier
17342
17343   Returns a TREE_LIST of identifier nodes.  */
17344
17345static tree
17346cp_parser_objc_identifier_list (cp_parser* parser)
17347{
17348  tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17349  cp_token *sep = cp_lexer_peek_token (parser->lexer);
17350
17351  while (sep->type == CPP_COMMA)
17352    {
17353      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17354      list = chainon (list,
17355		      build_tree_list (NULL_TREE,
17356				       cp_parser_identifier (parser)));
17357      sep = cp_lexer_peek_token (parser->lexer);
17358    }
17359
17360  return list;
17361}
17362
17363/* Parse an Objective-C alias declaration.
17364
17365   objc-alias-declaration:
17366     @compatibility_alias identifier identifier ;
17367
17368   This function registers the alias mapping with the Objective-C front-end.
17369   It returns nothing.  */
17370
17371static void
17372cp_parser_objc_alias_declaration (cp_parser* parser)
17373{
17374  tree alias, orig;
17375
17376  cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17377  alias = cp_parser_identifier (parser);
17378  orig = cp_parser_identifier (parser);
17379  objc_declare_alias (alias, orig);
17380  cp_parser_consume_semicolon_at_end_of_statement (parser);
17381}
17382
17383/* Parse an Objective-C class forward-declaration.
17384
17385   objc-class-declaration:
17386     @class objc-identifier-list ;
17387
17388   The function registers the forward declarations with the Objective-C
17389   front-end.  It returns nothing.  */
17390
17391static void
17392cp_parser_objc_class_declaration (cp_parser* parser)
17393{
17394  cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17395  objc_declare_class (cp_parser_objc_identifier_list (parser));
17396  cp_parser_consume_semicolon_at_end_of_statement (parser);
17397}
17398
17399/* Parse a list of Objective-C protocol references.
17400
17401   objc-protocol-refs-opt:
17402     objc-protocol-refs [opt]
17403
17404   objc-protocol-refs:
17405     < objc-identifier-list >
17406
17407   Returns a TREE_LIST of identifiers, if any.  */
17408
17409static tree
17410cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17411{
17412  tree protorefs = NULL_TREE;
17413
17414  if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17415    {
17416      cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17417      protorefs = cp_parser_objc_identifier_list (parser);
17418      cp_parser_require (parser, CPP_GREATER, "`>'");
17419    }
17420
17421  return protorefs;
17422}
17423
17424/* Parse a Objective-C visibility specification.  */
17425
17426static void
17427cp_parser_objc_visibility_spec (cp_parser* parser)
17428{
17429  cp_token *vis = cp_lexer_peek_token (parser->lexer);
17430
17431  switch (vis->keyword)
17432    {
17433    case RID_AT_PRIVATE:
17434      objc_set_visibility (2);
17435      break;
17436    case RID_AT_PROTECTED:
17437      objc_set_visibility (0);
17438      break;
17439    case RID_AT_PUBLIC:
17440      objc_set_visibility (1);
17441      break;
17442    default:
17443      return;
17444    }
17445
17446  /* Eat '@private'/'@protected'/'@public'.  */
17447  cp_lexer_consume_token (parser->lexer);
17448}
17449
17450/* Parse an Objective-C method type.  */
17451
17452static void
17453cp_parser_objc_method_type (cp_parser* parser)
17454{
17455  objc_set_method_type
17456   (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17457    ? PLUS_EXPR
17458    : MINUS_EXPR);
17459}
17460
17461/* Parse an Objective-C protocol qualifier.  */
17462
17463static tree
17464cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17465{
17466  tree quals = NULL_TREE, node;
17467  cp_token *token = cp_lexer_peek_token (parser->lexer);
17468
17469  node = token->u.value;
17470
17471  while (node && TREE_CODE (node) == IDENTIFIER_NODE
17472	 && (node == ridpointers [(int) RID_IN]
17473	     || node == ridpointers [(int) RID_OUT]
17474	     || node == ridpointers [(int) RID_INOUT]
17475	     || node == ridpointers [(int) RID_BYCOPY]
17476	     || node == ridpointers [(int) RID_BYREF]
17477	     || node == ridpointers [(int) RID_ONEWAY]))
17478    {
17479      quals = tree_cons (NULL_TREE, node, quals);
17480      cp_lexer_consume_token (parser->lexer);
17481      token = cp_lexer_peek_token (parser->lexer);
17482      node = token->u.value;
17483    }
17484
17485  return quals;
17486}
17487
17488/* Parse an Objective-C typename.  */
17489
17490static tree
17491cp_parser_objc_typename (cp_parser* parser)
17492{
17493  tree typename = NULL_TREE;
17494
17495  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17496    {
17497      tree proto_quals, cp_type = NULL_TREE;
17498
17499      cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17500      proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17501
17502      /* An ObjC type name may consist of just protocol qualifiers, in which
17503	 case the type shall default to 'id'.  */
17504      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17505	cp_type = cp_parser_type_id (parser);
17506
17507      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17508      typename = build_tree_list (proto_quals, cp_type);
17509    }
17510
17511  return typename;
17512}
17513
17514/* Check to see if TYPE refers to an Objective-C selector name.  */
17515
17516static bool
17517cp_parser_objc_selector_p (enum cpp_ttype type)
17518{
17519  return (type == CPP_NAME || type == CPP_KEYWORD
17520	  || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17521	  || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17522	  || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17523	  || type == CPP_XOR || type == CPP_XOR_EQ);
17524}
17525
17526/* Parse an Objective-C selector.  */
17527
17528static tree
17529cp_parser_objc_selector (cp_parser* parser)
17530{
17531  cp_token *token = cp_lexer_consume_token (parser->lexer);
17532
17533  if (!cp_parser_objc_selector_p (token->type))
17534    {
17535      error ("invalid Objective-C++ selector name");
17536      return error_mark_node;
17537    }
17538
17539  /* C++ operator names are allowed to appear in ObjC selectors.  */
17540  switch (token->type)
17541    {
17542    case CPP_AND_AND: return get_identifier ("and");
17543    case CPP_AND_EQ: return get_identifier ("and_eq");
17544    case CPP_AND: return get_identifier ("bitand");
17545    case CPP_OR: return get_identifier ("bitor");
17546    case CPP_COMPL: return get_identifier ("compl");
17547    case CPP_NOT: return get_identifier ("not");
17548    case CPP_NOT_EQ: return get_identifier ("not_eq");
17549    case CPP_OR_OR: return get_identifier ("or");
17550    case CPP_OR_EQ: return get_identifier ("or_eq");
17551    case CPP_XOR: return get_identifier ("xor");
17552    case CPP_XOR_EQ: return get_identifier ("xor_eq");
17553    default: return token->u.value;
17554    }
17555}
17556
17557/* Parse an Objective-C params list.  */
17558
17559static tree
17560cp_parser_objc_method_keyword_params (cp_parser* parser)
17561{
17562  tree params = NULL_TREE;
17563  bool maybe_unary_selector_p = true;
17564  cp_token *token = cp_lexer_peek_token (parser->lexer);
17565
17566  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17567    {
17568      tree selector = NULL_TREE, typename, identifier;
17569
17570      if (token->type != CPP_COLON)
17571	selector = cp_parser_objc_selector (parser);
17572
17573      /* Detect if we have a unary selector.  */
17574      if (maybe_unary_selector_p
17575	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17576	return selector;
17577
17578      maybe_unary_selector_p = false;
17579      cp_parser_require (parser, CPP_COLON, "`:'");
17580      typename = cp_parser_objc_typename (parser);
17581      identifier = cp_parser_identifier (parser);
17582
17583      params
17584	= chainon (params,
17585		   objc_build_keyword_decl (selector,
17586					    typename,
17587					    identifier));
17588
17589      token = cp_lexer_peek_token (parser->lexer);
17590    }
17591
17592  return params;
17593}
17594
17595/* Parse the non-keyword Objective-C params.  */
17596
17597static tree
17598cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17599{
17600  tree params = make_node (TREE_LIST);
17601  cp_token *token = cp_lexer_peek_token (parser->lexer);
17602  *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17603
17604  while (token->type == CPP_COMMA)
17605    {
17606      cp_parameter_declarator *parmdecl;
17607      tree parm;
17608
17609      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17610      token = cp_lexer_peek_token (parser->lexer);
17611
17612      if (token->type == CPP_ELLIPSIS)
17613	{
17614	  cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17615	  *ellipsisp = true;
17616	  break;
17617	}
17618
17619      parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17620      parm = grokdeclarator (parmdecl->declarator,
17621			     &parmdecl->decl_specifiers,
17622			     PARM, /*initialized=*/0,
17623			     /*attrlist=*/NULL);
17624
17625      chainon (params, build_tree_list (NULL_TREE, parm));
17626      token = cp_lexer_peek_token (parser->lexer);
17627    }
17628
17629  return params;
17630}
17631
17632/* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17633
17634static void
17635cp_parser_objc_interstitial_code (cp_parser* parser)
17636{
17637  cp_token *token = cp_lexer_peek_token (parser->lexer);
17638
17639  /* If the next token is `extern' and the following token is a string
17640     literal, then we have a linkage specification.  */
17641  if (token->keyword == RID_EXTERN
17642      && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17643    cp_parser_linkage_specification (parser);
17644  /* Handle #pragma, if any.  */
17645  else if (token->type == CPP_PRAGMA)
17646    cp_parser_pragma (parser, pragma_external);
17647  /* Allow stray semicolons.  */
17648  else if (token->type == CPP_SEMICOLON)
17649    cp_lexer_consume_token (parser->lexer);
17650  /* Finally, try to parse a block-declaration, or a function-definition.  */
17651  else
17652    cp_parser_block_declaration (parser, /*statement_p=*/false);
17653}
17654
17655/* Parse a method signature.  */
17656
17657static tree
17658cp_parser_objc_method_signature (cp_parser* parser)
17659{
17660  tree rettype, kwdparms, optparms;
17661  bool ellipsis = false;
17662
17663  cp_parser_objc_method_type (parser);
17664  rettype = cp_parser_objc_typename (parser);
17665  kwdparms = cp_parser_objc_method_keyword_params (parser);
17666  optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17667
17668  return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17669}
17670
17671/* Pars an Objective-C method prototype list.  */
17672
17673static void
17674cp_parser_objc_method_prototype_list (cp_parser* parser)
17675{
17676  cp_token *token = cp_lexer_peek_token (parser->lexer);
17677
17678  while (token->keyword != RID_AT_END)
17679    {
17680      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17681	{
17682	  objc_add_method_declaration
17683	   (cp_parser_objc_method_signature (parser));
17684	  cp_parser_consume_semicolon_at_end_of_statement (parser);
17685	}
17686      else
17687	/* Allow for interspersed non-ObjC++ code.  */
17688	cp_parser_objc_interstitial_code (parser);
17689
17690      token = cp_lexer_peek_token (parser->lexer);
17691    }
17692
17693  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17694  objc_finish_interface ();
17695}
17696
17697/* Parse an Objective-C method definition list.  */
17698
17699static void
17700cp_parser_objc_method_definition_list (cp_parser* parser)
17701{
17702  cp_token *token = cp_lexer_peek_token (parser->lexer);
17703
17704  while (token->keyword != RID_AT_END)
17705    {
17706      tree meth;
17707
17708      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17709	{
17710	  push_deferring_access_checks (dk_deferred);
17711	  objc_start_method_definition
17712	   (cp_parser_objc_method_signature (parser));
17713
17714	  /* For historical reasons, we accept an optional semicolon.  */
17715	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17716	    cp_lexer_consume_token (parser->lexer);
17717
17718	  perform_deferred_access_checks ();
17719	  stop_deferring_access_checks ();
17720	  meth = cp_parser_function_definition_after_declarator (parser,
17721								 false);
17722	  pop_deferring_access_checks ();
17723	  objc_finish_method_definition (meth);
17724	}
17725      else
17726	/* Allow for interspersed non-ObjC++ code.  */
17727	cp_parser_objc_interstitial_code (parser);
17728
17729      token = cp_lexer_peek_token (parser->lexer);
17730    }
17731
17732  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17733  objc_finish_implementation ();
17734}
17735
17736/* Parse Objective-C ivars.  */
17737
17738static void
17739cp_parser_objc_class_ivars (cp_parser* parser)
17740{
17741  cp_token *token = cp_lexer_peek_token (parser->lexer);
17742
17743  if (token->type != CPP_OPEN_BRACE)
17744    return;	/* No ivars specified.  */
17745
17746  cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17747  token = cp_lexer_peek_token (parser->lexer);
17748
17749  while (token->type != CPP_CLOSE_BRACE)
17750    {
17751      cp_decl_specifier_seq declspecs;
17752      int decl_class_or_enum_p;
17753      tree prefix_attributes;
17754
17755      cp_parser_objc_visibility_spec (parser);
17756
17757      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17758	break;
17759
17760      cp_parser_decl_specifier_seq (parser,
17761				    CP_PARSER_FLAGS_OPTIONAL,
17762				    &declspecs,
17763				    &decl_class_or_enum_p);
17764      prefix_attributes = declspecs.attributes;
17765      declspecs.attributes = NULL_TREE;
17766
17767      /* Keep going until we hit the `;' at the end of the
17768	 declaration.  */
17769      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17770	{
17771	  tree width = NULL_TREE, attributes, first_attribute, decl;
17772	  cp_declarator *declarator = NULL;
17773	  int ctor_dtor_or_conv_p;
17774
17775	  /* Check for a (possibly unnamed) bitfield declaration.  */
17776	  token = cp_lexer_peek_token (parser->lexer);
17777	  if (token->type == CPP_COLON)
17778	    goto eat_colon;
17779
17780	  if (token->type == CPP_NAME
17781	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17782		  == CPP_COLON))
17783	    {
17784	      /* Get the name of the bitfield.  */
17785	      declarator = make_id_declarator (NULL_TREE,
17786					       cp_parser_identifier (parser),
17787					       sfk_none);
17788
17789	     eat_colon:
17790	      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17791	      /* Get the width of the bitfield.  */
17792	      width
17793		= cp_parser_constant_expression (parser,
17794						 /*allow_non_constant=*/false,
17795						 NULL);
17796	    }
17797	  else
17798	    {
17799	      /* Parse the declarator.  */
17800	      declarator
17801		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17802					&ctor_dtor_or_conv_p,
17803					/*parenthesized_p=*/NULL,
17804					/*member_p=*/false);
17805	    }
17806
17807	  /* Look for attributes that apply to the ivar.  */
17808	  attributes = cp_parser_attributes_opt (parser);
17809	  /* Remember which attributes are prefix attributes and
17810	     which are not.  */
17811	  first_attribute = attributes;
17812	  /* Combine the attributes.  */
17813	  attributes = chainon (prefix_attributes, attributes);
17814
17815	  if (width)
17816	    {
17817	      /* Create the bitfield declaration.  */
17818	      decl = grokbitfield (declarator, &declspecs, width);
17819	      cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17820	    }
17821	  else
17822	    decl = grokfield (declarator, &declspecs,
17823			      NULL_TREE, /*init_const_expr_p=*/false,
17824			      NULL_TREE, attributes);
17825
17826	  /* Add the instance variable.  */
17827	  objc_add_instance_variable (decl);
17828
17829	  /* Reset PREFIX_ATTRIBUTES.  */
17830	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
17831	    attributes = TREE_CHAIN (attributes);
17832	  if (attributes)
17833	    TREE_CHAIN (attributes) = NULL_TREE;
17834
17835	  token = cp_lexer_peek_token (parser->lexer);
17836
17837	  if (token->type == CPP_COMMA)
17838	    {
17839	      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17840	      continue;
17841	    }
17842	  break;
17843	}
17844
17845      cp_parser_consume_semicolon_at_end_of_statement (parser);
17846      token = cp_lexer_peek_token (parser->lexer);
17847    }
17848
17849  cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17850  /* For historical reasons, we accept an optional semicolon.  */
17851  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17852    cp_lexer_consume_token (parser->lexer);
17853}
17854
17855/* Parse an Objective-C protocol declaration.  */
17856
17857static void
17858cp_parser_objc_protocol_declaration (cp_parser* parser)
17859{
17860  tree proto, protorefs;
17861  cp_token *tok;
17862
17863  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17864  if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17865    {
17866      error ("identifier expected after %<@protocol%>");
17867      goto finish;
17868    }
17869
17870  /* See if we have a forward declaration or a definition.  */
17871  tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17872
17873  /* Try a forward declaration first.  */
17874  if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17875    {
17876      objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17877     finish:
17878      cp_parser_consume_semicolon_at_end_of_statement (parser);
17879    }
17880
17881  /* Ok, we got a full-fledged definition (or at least should).  */
17882  else
17883    {
17884      proto = cp_parser_identifier (parser);
17885      protorefs = cp_parser_objc_protocol_refs_opt (parser);
17886      objc_start_protocol (proto, protorefs);
17887      cp_parser_objc_method_prototype_list (parser);
17888    }
17889}
17890
17891/* Parse an Objective-C superclass or category.  */
17892
17893static void
17894cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17895							  tree *categ)
17896{
17897  cp_token *next = cp_lexer_peek_token (parser->lexer);
17898
17899  *super = *categ = NULL_TREE;
17900  if (next->type == CPP_COLON)
17901    {
17902      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17903      *super = cp_parser_identifier (parser);
17904    }
17905  else if (next->type == CPP_OPEN_PAREN)
17906    {
17907      cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17908      *categ = cp_parser_identifier (parser);
17909      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17910    }
17911}
17912
17913/* Parse an Objective-C class interface.  */
17914
17915static void
17916cp_parser_objc_class_interface (cp_parser* parser)
17917{
17918  tree name, super, categ, protos;
17919
17920  cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17921  name = cp_parser_identifier (parser);
17922  cp_parser_objc_superclass_or_category (parser, &super, &categ);
17923  protos = cp_parser_objc_protocol_refs_opt (parser);
17924
17925  /* We have either a class or a category on our hands.  */
17926  if (categ)
17927    objc_start_category_interface (name, categ, protos);
17928  else
17929    {
17930      objc_start_class_interface (name, super, protos);
17931      /* Handle instance variable declarations, if any.  */
17932      cp_parser_objc_class_ivars (parser);
17933      objc_continue_interface ();
17934    }
17935
17936  cp_parser_objc_method_prototype_list (parser);
17937}
17938
17939/* Parse an Objective-C class implementation.  */
17940
17941static void
17942cp_parser_objc_class_implementation (cp_parser* parser)
17943{
17944  tree name, super, categ;
17945
17946  cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17947  name = cp_parser_identifier (parser);
17948  cp_parser_objc_superclass_or_category (parser, &super, &categ);
17949
17950  /* We have either a class or a category on our hands.  */
17951  if (categ)
17952    objc_start_category_implementation (name, categ);
17953  else
17954    {
17955      objc_start_class_implementation (name, super);
17956      /* Handle instance variable declarations, if any.  */
17957      cp_parser_objc_class_ivars (parser);
17958      objc_continue_implementation ();
17959    }
17960
17961  cp_parser_objc_method_definition_list (parser);
17962}
17963
17964/* Consume the @end token and finish off the implementation.  */
17965
17966static void
17967cp_parser_objc_end_implementation (cp_parser* parser)
17968{
17969  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17970  objc_finish_implementation ();
17971}
17972
17973/* Parse an Objective-C declaration.  */
17974
17975static void
17976cp_parser_objc_declaration (cp_parser* parser)
17977{
17978  /* Try to figure out what kind of declaration is present.  */
17979  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17980
17981  switch (kwd->keyword)
17982    {
17983    case RID_AT_ALIAS:
17984      cp_parser_objc_alias_declaration (parser);
17985      break;
17986    case RID_AT_CLASS:
17987      cp_parser_objc_class_declaration (parser);
17988      break;
17989    case RID_AT_PROTOCOL:
17990      cp_parser_objc_protocol_declaration (parser);
17991      break;
17992    case RID_AT_INTERFACE:
17993      cp_parser_objc_class_interface (parser);
17994      break;
17995    case RID_AT_IMPLEMENTATION:
17996      cp_parser_objc_class_implementation (parser);
17997      break;
17998    case RID_AT_END:
17999      cp_parser_objc_end_implementation (parser);
18000      break;
18001    default:
18002      error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18003      cp_parser_skip_to_end_of_block_or_statement (parser);
18004    }
18005}
18006
18007/* Parse an Objective-C try-catch-finally statement.
18008
18009   objc-try-catch-finally-stmt:
18010     @try compound-statement objc-catch-clause-seq [opt]
18011       objc-finally-clause [opt]
18012
18013   objc-catch-clause-seq:
18014     objc-catch-clause objc-catch-clause-seq [opt]
18015
18016   objc-catch-clause:
18017     @catch ( exception-declaration ) compound-statement
18018
18019   objc-finally-clause
18020     @finally compound-statement
18021
18022   Returns NULL_TREE.  */
18023
18024static tree
18025cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18026  location_t location;
18027  tree stmt;
18028
18029  cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18030  location = cp_lexer_peek_token (parser->lexer)->location;
18031  /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18032     node, lest it get absorbed into the surrounding block.  */
18033  stmt = push_stmt_list ();
18034  cp_parser_compound_statement (parser, NULL, false);
18035  objc_begin_try_stmt (location, pop_stmt_list (stmt));
18036
18037  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18038    {
18039      cp_parameter_declarator *parmdecl;
18040      tree parm;
18041
18042      cp_lexer_consume_token (parser->lexer);
18043      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18044      parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18045      parm = grokdeclarator (parmdecl->declarator,
18046			     &parmdecl->decl_specifiers,
18047			     PARM, /*initialized=*/0,
18048			     /*attrlist=*/NULL);
18049      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18050      objc_begin_catch_clause (parm);
18051      cp_parser_compound_statement (parser, NULL, false);
18052      objc_finish_catch_clause ();
18053    }
18054
18055  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18056    {
18057      cp_lexer_consume_token (parser->lexer);
18058      location = cp_lexer_peek_token (parser->lexer)->location;
18059      /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18060	 node, lest it get absorbed into the surrounding block.  */
18061      stmt = push_stmt_list ();
18062      cp_parser_compound_statement (parser, NULL, false);
18063      objc_build_finally_clause (location, pop_stmt_list (stmt));
18064    }
18065
18066  return objc_finish_try_stmt ();
18067}
18068
18069/* Parse an Objective-C synchronized statement.
18070
18071   objc-synchronized-stmt:
18072     @synchronized ( expression ) compound-statement
18073
18074   Returns NULL_TREE.  */
18075
18076static tree
18077cp_parser_objc_synchronized_statement (cp_parser *parser) {
18078  location_t location;
18079  tree lock, stmt;
18080
18081  cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18082
18083  location = cp_lexer_peek_token (parser->lexer)->location;
18084  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18085  lock = cp_parser_expression (parser, false);
18086  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18087
18088  /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18089     node, lest it get absorbed into the surrounding block.  */
18090  stmt = push_stmt_list ();
18091  cp_parser_compound_statement (parser, NULL, false);
18092
18093  return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18094}
18095
18096/* Parse an Objective-C throw statement.
18097
18098   objc-throw-stmt:
18099     @throw assignment-expression [opt] ;
18100
18101   Returns a constructed '@throw' statement.  */
18102
18103static tree
18104cp_parser_objc_throw_statement (cp_parser *parser) {
18105  tree expr = NULL_TREE;
18106
18107  cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18108
18109  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18110    expr = cp_parser_assignment_expression (parser, false);
18111
18112  cp_parser_consume_semicolon_at_end_of_statement (parser);
18113
18114  return objc_build_throw_stmt (expr);
18115}
18116
18117/* Parse an Objective-C statement.  */
18118
18119static tree
18120cp_parser_objc_statement (cp_parser * parser) {
18121  /* Try to figure out what kind of declaration is present.  */
18122  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18123
18124  switch (kwd->keyword)
18125    {
18126    case RID_AT_TRY:
18127      return cp_parser_objc_try_catch_finally_statement (parser);
18128    case RID_AT_SYNCHRONIZED:
18129      return cp_parser_objc_synchronized_statement (parser);
18130    case RID_AT_THROW:
18131      return cp_parser_objc_throw_statement (parser);
18132    default:
18133      error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18134      cp_parser_skip_to_end_of_block_or_statement (parser);
18135    }
18136
18137  return error_mark_node;
18138}
18139
18140/* OpenMP 2.5 parsing routines.  */
18141
18142/* All OpenMP clauses.  OpenMP 2.5.  */
18143typedef enum pragma_omp_clause {
18144  PRAGMA_OMP_CLAUSE_NONE = 0,
18145
18146  PRAGMA_OMP_CLAUSE_COPYIN,
18147  PRAGMA_OMP_CLAUSE_COPYPRIVATE,
18148  PRAGMA_OMP_CLAUSE_DEFAULT,
18149  PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
18150  PRAGMA_OMP_CLAUSE_IF,
18151  PRAGMA_OMP_CLAUSE_LASTPRIVATE,
18152  PRAGMA_OMP_CLAUSE_NOWAIT,
18153  PRAGMA_OMP_CLAUSE_NUM_THREADS,
18154  PRAGMA_OMP_CLAUSE_ORDERED,
18155  PRAGMA_OMP_CLAUSE_PRIVATE,
18156  PRAGMA_OMP_CLAUSE_REDUCTION,
18157  PRAGMA_OMP_CLAUSE_SCHEDULE,
18158  PRAGMA_OMP_CLAUSE_SHARED
18159} pragma_omp_clause;
18160
18161/* Returns name of the next clause.
18162   If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18163   the token is not consumed.  Otherwise appropriate pragma_omp_clause is
18164   returned and the token is consumed.  */
18165
18166static pragma_omp_clause
18167cp_parser_omp_clause_name (cp_parser *parser)
18168{
18169  pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18170
18171  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18172    result = PRAGMA_OMP_CLAUSE_IF;
18173  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18174    result = PRAGMA_OMP_CLAUSE_DEFAULT;
18175  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18176    result = PRAGMA_OMP_CLAUSE_PRIVATE;
18177  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18178    {
18179      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18180      const char *p = IDENTIFIER_POINTER (id);
18181
18182      switch (p[0])
18183	{
18184	case 'c':
18185	  if (!strcmp ("copyin", p))
18186	    result = PRAGMA_OMP_CLAUSE_COPYIN;
18187	  else if (!strcmp ("copyprivate", p))
18188	    result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18189	  break;
18190	case 'f':
18191	  if (!strcmp ("firstprivate", p))
18192	    result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18193	  break;
18194	case 'l':
18195	  if (!strcmp ("lastprivate", p))
18196	    result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18197	  break;
18198	case 'n':
18199	  if (!strcmp ("nowait", p))
18200	    result = PRAGMA_OMP_CLAUSE_NOWAIT;
18201	  else if (!strcmp ("num_threads", p))
18202	    result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18203	  break;
18204	case 'o':
18205	  if (!strcmp ("ordered", p))
18206	    result = PRAGMA_OMP_CLAUSE_ORDERED;
18207	  break;
18208	case 'r':
18209	  if (!strcmp ("reduction", p))
18210	    result = PRAGMA_OMP_CLAUSE_REDUCTION;
18211	  break;
18212	case 's':
18213	  if (!strcmp ("schedule", p))
18214	    result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18215	  else if (!strcmp ("shared", p))
18216	    result = PRAGMA_OMP_CLAUSE_SHARED;
18217	  break;
18218	}
18219    }
18220
18221  if (result != PRAGMA_OMP_CLAUSE_NONE)
18222    cp_lexer_consume_token (parser->lexer);
18223
18224  return result;
18225}
18226
18227/* Validate that a clause of the given type does not already exist.  */
18228
18229static void
18230check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18231{
18232  tree c;
18233
18234  for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18235    if (OMP_CLAUSE_CODE (c) == code)
18236      {
18237	error ("too many %qs clauses", name);
18238	break;
18239      }
18240}
18241
18242/* OpenMP 2.5:
18243   variable-list:
18244     identifier
18245     variable-list , identifier
18246
18247   In addition, we match a closing parenthesis.  An opening parenthesis
18248   will have been consumed by the caller.
18249
18250   If KIND is nonzero, create the appropriate node and install the decl
18251   in OMP_CLAUSE_DECL and add the node to the head of the list.
18252
18253   If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18254   return the list created.  */
18255
18256static tree
18257cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18258				tree list)
18259{
18260  while (1)
18261    {
18262      tree name, decl;
18263
18264      name = cp_parser_id_expression (parser, /*template_p=*/false,
18265				      /*check_dependency_p=*/true,
18266				      /*template_p=*/NULL,
18267				      /*declarator_p=*/false,
18268				      /*optional_p=*/false);
18269      if (name == error_mark_node)
18270	goto skip_comma;
18271
18272      decl = cp_parser_lookup_name_simple (parser, name);
18273      if (decl == error_mark_node)
18274	cp_parser_name_lookup_error (parser, name, decl, NULL);
18275      else if (kind != 0)
18276	{
18277	  tree u = build_omp_clause (kind);
18278	  OMP_CLAUSE_DECL (u) = decl;
18279	  OMP_CLAUSE_CHAIN (u) = list;
18280	  list = u;
18281	}
18282      else
18283	list = tree_cons (decl, NULL_TREE, list);
18284
18285    get_comma:
18286      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18287	break;
18288      cp_lexer_consume_token (parser->lexer);
18289    }
18290
18291  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18292    {
18293      int ending;
18294
18295      /* Try to resync to an unnested comma.  Copied from
18296	 cp_parser_parenthesized_expression_list.  */
18297    skip_comma:
18298      ending = cp_parser_skip_to_closing_parenthesis (parser,
18299						      /*recovering=*/true,
18300						      /*or_comma=*/true,
18301						      /*consume_paren=*/true);
18302      if (ending < 0)
18303	goto get_comma;
18304    }
18305
18306  return list;
18307}
18308
18309/* Similarly, but expect leading and trailing parenthesis.  This is a very
18310   common case for omp clauses.  */
18311
18312static tree
18313cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18314{
18315  if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18316    return cp_parser_omp_var_list_no_open (parser, kind, list);
18317  return list;
18318}
18319
18320/* OpenMP 2.5:
18321   default ( shared | none ) */
18322
18323static tree
18324cp_parser_omp_clause_default (cp_parser *parser, tree list)
18325{
18326  enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18327  tree c;
18328
18329  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18330    return list;
18331  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18332    {
18333      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18334      const char *p = IDENTIFIER_POINTER (id);
18335
18336      switch (p[0])
18337	{
18338	case 'n':
18339	  if (strcmp ("none", p) != 0)
18340	    goto invalid_kind;
18341	  kind = OMP_CLAUSE_DEFAULT_NONE;
18342	  break;
18343
18344	case 's':
18345	  if (strcmp ("shared", p) != 0)
18346	    goto invalid_kind;
18347	  kind = OMP_CLAUSE_DEFAULT_SHARED;
18348	  break;
18349
18350	default:
18351	  goto invalid_kind;
18352	}
18353
18354      cp_lexer_consume_token (parser->lexer);
18355    }
18356  else
18357    {
18358    invalid_kind:
18359      cp_parser_error (parser, "expected %<none%> or %<shared%>");
18360    }
18361
18362  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18363    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18364					   /*or_comma=*/false,
18365					   /*consume_paren=*/true);
18366
18367  if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18368    return list;
18369
18370  check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18371  c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18372  OMP_CLAUSE_CHAIN (c) = list;
18373  OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18374
18375  return c;
18376}
18377
18378/* OpenMP 2.5:
18379   if ( expression ) */
18380
18381static tree
18382cp_parser_omp_clause_if (cp_parser *parser, tree list)
18383{
18384  tree t, c;
18385
18386  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18387    return list;
18388
18389  t = cp_parser_condition (parser);
18390
18391  if (t == error_mark_node
18392      || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18393    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18394					   /*or_comma=*/false,
18395					   /*consume_paren=*/true);
18396
18397  check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18398
18399  c = build_omp_clause (OMP_CLAUSE_IF);
18400  OMP_CLAUSE_IF_EXPR (c) = t;
18401  OMP_CLAUSE_CHAIN (c) = list;
18402
18403  return c;
18404}
18405
18406/* OpenMP 2.5:
18407   nowait */
18408
18409static tree
18410cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18411{
18412  tree c;
18413
18414  check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18415
18416  c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18417  OMP_CLAUSE_CHAIN (c) = list;
18418  return c;
18419}
18420
18421/* OpenMP 2.5:
18422   num_threads ( expression ) */
18423
18424static tree
18425cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18426{
18427  tree t, c;
18428
18429  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18430    return list;
18431
18432  t = cp_parser_expression (parser, false);
18433
18434  if (t == error_mark_node
18435      || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18436    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18437					   /*or_comma=*/false,
18438					   /*consume_paren=*/true);
18439
18440  check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18441
18442  c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18443  OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18444  OMP_CLAUSE_CHAIN (c) = list;
18445
18446  return c;
18447}
18448
18449/* OpenMP 2.5:
18450   ordered */
18451
18452static tree
18453cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18454{
18455  tree c;
18456
18457  check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18458
18459  c = build_omp_clause (OMP_CLAUSE_ORDERED);
18460  OMP_CLAUSE_CHAIN (c) = list;
18461  return c;
18462}
18463
18464/* OpenMP 2.5:
18465   reduction ( reduction-operator : variable-list )
18466
18467   reduction-operator:
18468     One of: + * - & ^ | && || */
18469
18470static tree
18471cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18472{
18473  enum tree_code code;
18474  tree nlist, c;
18475
18476  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18477    return list;
18478
18479  switch (cp_lexer_peek_token (parser->lexer)->type)
18480    {
18481    case CPP_PLUS:
18482      code = PLUS_EXPR;
18483      break;
18484    case CPP_MULT:
18485      code = MULT_EXPR;
18486      break;
18487    case CPP_MINUS:
18488      code = MINUS_EXPR;
18489      break;
18490    case CPP_AND:
18491      code = BIT_AND_EXPR;
18492      break;
18493    case CPP_XOR:
18494      code = BIT_XOR_EXPR;
18495      break;
18496    case CPP_OR:
18497      code = BIT_IOR_EXPR;
18498      break;
18499    case CPP_AND_AND:
18500      code = TRUTH_ANDIF_EXPR;
18501      break;
18502    case CPP_OR_OR:
18503      code = TRUTH_ORIF_EXPR;
18504      break;
18505    default:
18506      cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18507    resync_fail:
18508      cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18509					     /*or_comma=*/false,
18510					     /*consume_paren=*/true);
18511      return list;
18512    }
18513  cp_lexer_consume_token (parser->lexer);
18514
18515  if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18516    goto resync_fail;
18517
18518  nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18519  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18520    OMP_CLAUSE_REDUCTION_CODE (c) = code;
18521
18522  return nlist;
18523}
18524
18525/* OpenMP 2.5:
18526   schedule ( schedule-kind )
18527   schedule ( schedule-kind , expression )
18528
18529   schedule-kind:
18530     static | dynamic | guided | runtime  */
18531
18532static tree
18533cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18534{
18535  tree c, t;
18536
18537  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18538    return list;
18539
18540  c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18541
18542  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18543    {
18544      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18545      const char *p = IDENTIFIER_POINTER (id);
18546
18547      switch (p[0])
18548	{
18549	case 'd':
18550	  if (strcmp ("dynamic", p) != 0)
18551	    goto invalid_kind;
18552	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18553	  break;
18554
18555	case 'g':
18556	  if (strcmp ("guided", p) != 0)
18557	    goto invalid_kind;
18558	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18559	  break;
18560
18561	case 'r':
18562	  if (strcmp ("runtime", p) != 0)
18563	    goto invalid_kind;
18564	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18565	  break;
18566
18567	default:
18568	  goto invalid_kind;
18569	}
18570    }
18571  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18572    OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18573  else
18574    goto invalid_kind;
18575  cp_lexer_consume_token (parser->lexer);
18576
18577  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18578    {
18579      cp_lexer_consume_token (parser->lexer);
18580
18581      t = cp_parser_assignment_expression (parser, false);
18582
18583      if (t == error_mark_node)
18584	goto resync_fail;
18585      else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18586	error ("schedule %<runtime%> does not take "
18587	       "a %<chunk_size%> parameter");
18588      else
18589	OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18590
18591      if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18592	goto resync_fail;
18593    }
18594  else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18595    goto resync_fail;
18596
18597  check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18598  OMP_CLAUSE_CHAIN (c) = list;
18599  return c;
18600
18601 invalid_kind:
18602  cp_parser_error (parser, "invalid schedule kind");
18603 resync_fail:
18604  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18605					 /*or_comma=*/false,
18606					 /*consume_paren=*/true);
18607  return list;
18608}
18609
18610/* Parse all OpenMP clauses.  The set clauses allowed by the directive
18611   is a bitmask in MASK.  Return the list of clauses found; the result
18612   of clause default goes in *pdefault.  */
18613
18614static tree
18615cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18616			   const char *where, cp_token *pragma_tok)
18617{
18618  tree clauses = NULL;
18619
18620  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18621    {
18622      pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18623      const char *c_name;
18624      tree prev = clauses;
18625
18626      switch (c_kind)
18627	{
18628	case PRAGMA_OMP_CLAUSE_COPYIN:
18629	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18630	  c_name = "copyin";
18631	  break;
18632	case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18633	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18634					    clauses);
18635	  c_name = "copyprivate";
18636	  break;
18637	case PRAGMA_OMP_CLAUSE_DEFAULT:
18638	  clauses = cp_parser_omp_clause_default (parser, clauses);
18639	  c_name = "default";
18640	  break;
18641	case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18642	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18643					    clauses);
18644	  c_name = "firstprivate";
18645	  break;
18646	case PRAGMA_OMP_CLAUSE_IF:
18647	  clauses = cp_parser_omp_clause_if (parser, clauses);
18648	  c_name = "if";
18649	  break;
18650	case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18651	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18652					    clauses);
18653	  c_name = "lastprivate";
18654	  break;
18655	case PRAGMA_OMP_CLAUSE_NOWAIT:
18656	  clauses = cp_parser_omp_clause_nowait (parser, clauses);
18657	  c_name = "nowait";
18658	  break;
18659	case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18660	  clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18661	  c_name = "num_threads";
18662	  break;
18663	case PRAGMA_OMP_CLAUSE_ORDERED:
18664	  clauses = cp_parser_omp_clause_ordered (parser, clauses);
18665	  c_name = "ordered";
18666	  break;
18667	case PRAGMA_OMP_CLAUSE_PRIVATE:
18668	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18669					    clauses);
18670	  c_name = "private";
18671	  break;
18672	case PRAGMA_OMP_CLAUSE_REDUCTION:
18673	  clauses = cp_parser_omp_clause_reduction (parser, clauses);
18674	  c_name = "reduction";
18675	  break;
18676	case PRAGMA_OMP_CLAUSE_SCHEDULE:
18677	  clauses = cp_parser_omp_clause_schedule (parser, clauses);
18678	  c_name = "schedule";
18679	  break;
18680	case PRAGMA_OMP_CLAUSE_SHARED:
18681	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18682					    clauses);
18683	  c_name = "shared";
18684	  break;
18685	default:
18686	  cp_parser_error (parser, "expected %<#pragma omp%> clause");
18687	  goto saw_error;
18688	}
18689
18690      if (((mask >> c_kind) & 1) == 0)
18691	{
18692	  /* Remove the invalid clause(s) from the list to avoid
18693	     confusing the rest of the compiler.  */
18694	  clauses = prev;
18695	  error ("%qs is not valid for %qs", c_name, where);
18696	}
18697    }
18698 saw_error:
18699  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18700  return finish_omp_clauses (clauses);
18701}
18702
18703/* OpenMP 2.5:
18704   structured-block:
18705     statement
18706
18707   In practice, we're also interested in adding the statement to an
18708   outer node.  So it is convenient if we work around the fact that
18709   cp_parser_statement calls add_stmt.  */
18710
18711static unsigned
18712cp_parser_begin_omp_structured_block (cp_parser *parser)
18713{
18714  unsigned save = parser->in_statement;
18715
18716  /* Only move the values to IN_OMP_BLOCK if they weren't false.
18717     This preserves the "not within loop or switch" style error messages
18718     for nonsense cases like
18719	void foo() {
18720	#pragma omp single
18721	  break;
18722	}
18723  */
18724  if (parser->in_statement)
18725    parser->in_statement = IN_OMP_BLOCK;
18726
18727  return save;
18728}
18729
18730static void
18731cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18732{
18733  parser->in_statement = save;
18734}
18735
18736static tree
18737cp_parser_omp_structured_block (cp_parser *parser)
18738{
18739  tree stmt = begin_omp_structured_block ();
18740  unsigned int save = cp_parser_begin_omp_structured_block (parser);
18741
18742  cp_parser_statement (parser, NULL_TREE, false, NULL);
18743
18744  cp_parser_end_omp_structured_block (parser, save);
18745  return finish_omp_structured_block (stmt);
18746}
18747
18748/* OpenMP 2.5:
18749   # pragma omp atomic new-line
18750     expression-stmt
18751
18752   expression-stmt:
18753     x binop= expr | x++ | ++x | x-- | --x
18754   binop:
18755     +, *, -, /, &, ^, |, <<, >>
18756
18757  where x is an lvalue expression with scalar type.  */
18758
18759static void
18760cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18761{
18762  tree lhs, rhs;
18763  enum tree_code code;
18764
18765  cp_parser_require_pragma_eol (parser, pragma_tok);
18766
18767  lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18768				    /*cast_p=*/false);
18769  switch (TREE_CODE (lhs))
18770    {
18771    case ERROR_MARK:
18772      goto saw_error;
18773
18774    case PREINCREMENT_EXPR:
18775    case POSTINCREMENT_EXPR:
18776      lhs = TREE_OPERAND (lhs, 0);
18777      code = PLUS_EXPR;
18778      rhs = integer_one_node;
18779      break;
18780
18781    case PREDECREMENT_EXPR:
18782    case POSTDECREMENT_EXPR:
18783      lhs = TREE_OPERAND (lhs, 0);
18784      code = MINUS_EXPR;
18785      rhs = integer_one_node;
18786      break;
18787
18788    default:
18789      switch (cp_lexer_peek_token (parser->lexer)->type)
18790	{
18791	case CPP_MULT_EQ:
18792	  code = MULT_EXPR;
18793	  break;
18794	case CPP_DIV_EQ:
18795	  code = TRUNC_DIV_EXPR;
18796	  break;
18797	case CPP_PLUS_EQ:
18798	  code = PLUS_EXPR;
18799	  break;
18800	case CPP_MINUS_EQ:
18801	  code = MINUS_EXPR;
18802	  break;
18803	case CPP_LSHIFT_EQ:
18804	  code = LSHIFT_EXPR;
18805	  break;
18806	case CPP_RSHIFT_EQ:
18807	  code = RSHIFT_EXPR;
18808	  break;
18809	case CPP_AND_EQ:
18810	  code = BIT_AND_EXPR;
18811	  break;
18812	case CPP_OR_EQ:
18813	  code = BIT_IOR_EXPR;
18814	  break;
18815	case CPP_XOR_EQ:
18816	  code = BIT_XOR_EXPR;
18817	  break;
18818	default:
18819	  cp_parser_error (parser,
18820			   "invalid operator for %<#pragma omp atomic%>");
18821	  goto saw_error;
18822	}
18823      cp_lexer_consume_token (parser->lexer);
18824
18825      rhs = cp_parser_expression (parser, false);
18826      if (rhs == error_mark_node)
18827	goto saw_error;
18828      break;
18829    }
18830  finish_omp_atomic (code, lhs, rhs);
18831  cp_parser_consume_semicolon_at_end_of_statement (parser);
18832  return;
18833
18834 saw_error:
18835  cp_parser_skip_to_end_of_block_or_statement (parser);
18836}
18837
18838
18839/* OpenMP 2.5:
18840   # pragma omp barrier new-line  */
18841
18842static void
18843cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18844{
18845  cp_parser_require_pragma_eol (parser, pragma_tok);
18846  finish_omp_barrier ();
18847}
18848
18849/* OpenMP 2.5:
18850   # pragma omp critical [(name)] new-line
18851     structured-block  */
18852
18853static tree
18854cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18855{
18856  tree stmt, name = NULL;
18857
18858  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18859    {
18860      cp_lexer_consume_token (parser->lexer);
18861
18862      name = cp_parser_identifier (parser);
18863
18864      if (name == error_mark_node
18865	  || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18866	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18867					       /*or_comma=*/false,
18868					       /*consume_paren=*/true);
18869      if (name == error_mark_node)
18870	name = NULL;
18871    }
18872  cp_parser_require_pragma_eol (parser, pragma_tok);
18873
18874  stmt = cp_parser_omp_structured_block (parser);
18875  return c_finish_omp_critical (stmt, name);
18876}
18877
18878/* OpenMP 2.5:
18879   # pragma omp flush flush-vars[opt] new-line
18880
18881   flush-vars:
18882     ( variable-list ) */
18883
18884static void
18885cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18886{
18887  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18888    (void) cp_parser_omp_var_list (parser, 0, NULL);
18889  cp_parser_require_pragma_eol (parser, pragma_tok);
18890
18891  finish_omp_flush ();
18892}
18893
18894/* Parse the restricted form of the for statment allowed by OpenMP.  */
18895
18896static tree
18897cp_parser_omp_for_loop (cp_parser *parser)
18898{
18899  tree init, cond, incr, body, decl, pre_body;
18900  location_t loc;
18901
18902  if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18903    {
18904      cp_parser_error (parser, "for statement expected");
18905      return NULL;
18906    }
18907  loc = cp_lexer_consume_token (parser->lexer)->location;
18908  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18909    return NULL;
18910
18911  init = decl = NULL;
18912  pre_body = push_stmt_list ();
18913  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18914    {
18915      cp_decl_specifier_seq type_specifiers;
18916
18917      /* First, try to parse as an initialized declaration.  See
18918	 cp_parser_condition, from whence the bulk of this is copied.  */
18919
18920      cp_parser_parse_tentatively (parser);
18921      cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18922				    &type_specifiers);
18923      if (!cp_parser_error_occurred (parser))
18924	{
18925	  tree asm_specification, attributes;
18926	  cp_declarator *declarator;
18927
18928	  declarator = cp_parser_declarator (parser,
18929					     CP_PARSER_DECLARATOR_NAMED,
18930					     /*ctor_dtor_or_conv_p=*/NULL,
18931					     /*parenthesized_p=*/NULL,
18932					     /*member_p=*/false);
18933	  attributes = cp_parser_attributes_opt (parser);
18934	  asm_specification = cp_parser_asm_specification_opt (parser);
18935
18936	  cp_parser_require (parser, CPP_EQ, "`='");
18937	  if (cp_parser_parse_definitely (parser))
18938	    {
18939	      tree pushed_scope;
18940
18941	      decl = start_decl (declarator, &type_specifiers,
18942				 /*initialized_p=*/false, attributes,
18943				 /*prefix_attributes=*/NULL_TREE,
18944				 &pushed_scope);
18945
18946	      init = cp_parser_assignment_expression (parser, false);
18947
18948	      cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18949			      asm_specification, LOOKUP_ONLYCONVERTING);
18950
18951	      if (pushed_scope)
18952		pop_scope (pushed_scope);
18953	    }
18954	}
18955      else
18956	cp_parser_abort_tentative_parse (parser);
18957
18958      /* If parsing as an initialized declaration failed, try again as
18959	 a simple expression.  */
18960      if (decl == NULL)
18961	init = cp_parser_expression (parser, false);
18962    }
18963  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18964  pre_body = pop_stmt_list (pre_body);
18965
18966  cond = NULL;
18967  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18968    cond = cp_parser_condition (parser);
18969  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18970
18971  incr = NULL;
18972  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18973    incr = cp_parser_expression (parser, false);
18974
18975  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18976    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18977					   /*or_comma=*/false,
18978					   /*consume_paren=*/true);
18979
18980  /* Note that we saved the original contents of this flag when we entered
18981     the structured block, and so we don't need to re-save it here.  */
18982  parser->in_statement = IN_OMP_FOR;
18983
18984  /* Note that the grammar doesn't call for a structured block here,
18985     though the loop as a whole is a structured block.  */
18986  body = push_stmt_list ();
18987  cp_parser_statement (parser, NULL_TREE, false, NULL);
18988  body = pop_stmt_list (body);
18989
18990  return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18991}
18992
18993/* OpenMP 2.5:
18994   #pragma omp for for-clause[optseq] new-line
18995     for-loop  */
18996
18997#define OMP_FOR_CLAUSE_MASK				\
18998	( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
18999	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
19000	| (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)		\
19001	| (1u << PRAGMA_OMP_CLAUSE_REDUCTION)		\
19002	| (1u << PRAGMA_OMP_CLAUSE_ORDERED)		\
19003	| (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)		\
19004	| (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19005
19006static tree
19007cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19008{
19009  tree clauses, sb, ret;
19010  unsigned int save;
19011
19012  clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19013				       "#pragma omp for", pragma_tok);
19014
19015  sb = begin_omp_structured_block ();
19016  save = cp_parser_begin_omp_structured_block (parser);
19017
19018  ret = cp_parser_omp_for_loop (parser);
19019  if (ret)
19020    OMP_FOR_CLAUSES (ret) = clauses;
19021
19022  cp_parser_end_omp_structured_block (parser, save);
19023  add_stmt (finish_omp_structured_block (sb));
19024
19025  return ret;
19026}
19027
19028/* OpenMP 2.5:
19029   # pragma omp master new-line
19030     structured-block  */
19031
19032static tree
19033cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19034{
19035  cp_parser_require_pragma_eol (parser, pragma_tok);
19036  return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19037}
19038
19039/* OpenMP 2.5:
19040   # pragma omp ordered new-line
19041     structured-block  */
19042
19043static tree
19044cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19045{
19046  cp_parser_require_pragma_eol (parser, pragma_tok);
19047  return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19048}
19049
19050/* OpenMP 2.5:
19051
19052   section-scope:
19053     { section-sequence }
19054
19055   section-sequence:
19056     section-directive[opt] structured-block
19057     section-sequence section-directive structured-block  */
19058
19059static tree
19060cp_parser_omp_sections_scope (cp_parser *parser)
19061{
19062  tree stmt, substmt;
19063  bool error_suppress = false;
19064  cp_token *tok;
19065
19066  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19067    return NULL_TREE;
19068
19069  stmt = push_stmt_list ();
19070
19071  if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19072    {
19073      unsigned save;
19074
19075      substmt = begin_omp_structured_block ();
19076      save = cp_parser_begin_omp_structured_block (parser);
19077
19078      while (1)
19079	{
19080	  cp_parser_statement (parser, NULL_TREE, false, NULL);
19081
19082	  tok = cp_lexer_peek_token (parser->lexer);
19083	  if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19084	    break;
19085	  if (tok->type == CPP_CLOSE_BRACE)
19086	    break;
19087	  if (tok->type == CPP_EOF)
19088	    break;
19089	}
19090
19091      cp_parser_end_omp_structured_block (parser, save);
19092      substmt = finish_omp_structured_block (substmt);
19093      substmt = build1 (OMP_SECTION, void_type_node, substmt);
19094      add_stmt (substmt);
19095    }
19096
19097  while (1)
19098    {
19099      tok = cp_lexer_peek_token (parser->lexer);
19100      if (tok->type == CPP_CLOSE_BRACE)
19101	break;
19102      if (tok->type == CPP_EOF)
19103	break;
19104
19105      if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19106	{
19107	  cp_lexer_consume_token (parser->lexer);
19108	  cp_parser_require_pragma_eol (parser, tok);
19109	  error_suppress = false;
19110	}
19111      else if (!error_suppress)
19112	{
19113	  cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19114	  error_suppress = true;
19115	}
19116
19117      substmt = cp_parser_omp_structured_block (parser);
19118      substmt = build1 (OMP_SECTION, void_type_node, substmt);
19119      add_stmt (substmt);
19120    }
19121  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19122
19123  substmt = pop_stmt_list (stmt);
19124
19125  stmt = make_node (OMP_SECTIONS);
19126  TREE_TYPE (stmt) = void_type_node;
19127  OMP_SECTIONS_BODY (stmt) = substmt;
19128
19129  add_stmt (stmt);
19130  return stmt;
19131}
19132
19133/* OpenMP 2.5:
19134   # pragma omp sections sections-clause[optseq] newline
19135     sections-scope  */
19136
19137#define OMP_SECTIONS_CLAUSE_MASK			\
19138	( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
19139	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
19140	| (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)		\
19141	| (1u << PRAGMA_OMP_CLAUSE_REDUCTION)		\
19142	| (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19143
19144static tree
19145cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19146{
19147  tree clauses, ret;
19148
19149  clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19150				       "#pragma omp sections", pragma_tok);
19151
19152  ret = cp_parser_omp_sections_scope (parser);
19153  if (ret)
19154    OMP_SECTIONS_CLAUSES (ret) = clauses;
19155
19156  return ret;
19157}
19158
19159/* OpenMP 2.5:
19160   # pragma parallel parallel-clause new-line
19161   # pragma parallel for parallel-for-clause new-line
19162   # pragma parallel sections parallel-sections-clause new-line  */
19163
19164#define OMP_PARALLEL_CLAUSE_MASK			\
19165	( (1u << PRAGMA_OMP_CLAUSE_IF)			\
19166	| (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
19167	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
19168	| (1u << PRAGMA_OMP_CLAUSE_DEFAULT)		\
19169	| (1u << PRAGMA_OMP_CLAUSE_SHARED)		\
19170	| (1u << PRAGMA_OMP_CLAUSE_COPYIN)		\
19171	| (1u << PRAGMA_OMP_CLAUSE_REDUCTION)		\
19172	| (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19173
19174static tree
19175cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19176{
19177  enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19178  const char *p_name = "#pragma omp parallel";
19179  tree stmt, clauses, par_clause, ws_clause, block;
19180  unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19181  unsigned int save;
19182
19183  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19184    {
19185      cp_lexer_consume_token (parser->lexer);
19186      p_kind = PRAGMA_OMP_PARALLEL_FOR;
19187      p_name = "#pragma omp parallel for";
19188      mask |= OMP_FOR_CLAUSE_MASK;
19189      mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19190    }
19191  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19192    {
19193      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19194      const char *p = IDENTIFIER_POINTER (id);
19195      if (strcmp (p, "sections") == 0)
19196	{
19197	  cp_lexer_consume_token (parser->lexer);
19198	  p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19199	  p_name = "#pragma omp parallel sections";
19200	  mask |= OMP_SECTIONS_CLAUSE_MASK;
19201	  mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19202	}
19203    }
19204
19205  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19206  block = begin_omp_parallel ();
19207  save = cp_parser_begin_omp_structured_block (parser);
19208
19209  switch (p_kind)
19210    {
19211    case PRAGMA_OMP_PARALLEL:
19212      cp_parser_already_scoped_statement (parser);
19213      par_clause = clauses;
19214      break;
19215
19216    case PRAGMA_OMP_PARALLEL_FOR:
19217      c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19218      stmt = cp_parser_omp_for_loop (parser);
19219      if (stmt)
19220	OMP_FOR_CLAUSES (stmt) = ws_clause;
19221      break;
19222
19223    case PRAGMA_OMP_PARALLEL_SECTIONS:
19224      c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19225      stmt = cp_parser_omp_sections_scope (parser);
19226      if (stmt)
19227	OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19228      break;
19229
19230    default:
19231      gcc_unreachable ();
19232    }
19233
19234  cp_parser_end_omp_structured_block (parser, save);
19235  stmt = finish_omp_parallel (par_clause, block);
19236  if (p_kind != PRAGMA_OMP_PARALLEL)
19237    OMP_PARALLEL_COMBINED (stmt) = 1;
19238  return stmt;
19239}
19240
19241/* OpenMP 2.5:
19242   # pragma omp single single-clause[optseq] new-line
19243     structured-block  */
19244
19245#define OMP_SINGLE_CLAUSE_MASK				\
19246	( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
19247	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
19248	| (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)		\
19249	| (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19250
19251static tree
19252cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19253{
19254  tree stmt = make_node (OMP_SINGLE);
19255  TREE_TYPE (stmt) = void_type_node;
19256
19257  OMP_SINGLE_CLAUSES (stmt)
19258    = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19259				 "#pragma omp single", pragma_tok);
19260  OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19261
19262  return add_stmt (stmt);
19263}
19264
19265/* OpenMP 2.5:
19266   # pragma omp threadprivate (variable-list) */
19267
19268static void
19269cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19270{
19271  tree vars;
19272
19273  vars = cp_parser_omp_var_list (parser, 0, NULL);
19274  cp_parser_require_pragma_eol (parser, pragma_tok);
19275
19276  if (!targetm.have_tls)
19277    sorry ("threadprivate variables not supported in this target");
19278
19279  finish_omp_threadprivate (vars);
19280}
19281
19282/* Main entry point to OpenMP statement pragmas.  */
19283
19284static void
19285cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19286{
19287  tree stmt;
19288
19289  switch (pragma_tok->pragma_kind)
19290    {
19291    case PRAGMA_OMP_ATOMIC:
19292      cp_parser_omp_atomic (parser, pragma_tok);
19293      return;
19294    case PRAGMA_OMP_CRITICAL:
19295      stmt = cp_parser_omp_critical (parser, pragma_tok);
19296      break;
19297    case PRAGMA_OMP_FOR:
19298      stmt = cp_parser_omp_for (parser, pragma_tok);
19299      break;
19300    case PRAGMA_OMP_MASTER:
19301      stmt = cp_parser_omp_master (parser, pragma_tok);
19302      break;
19303    case PRAGMA_OMP_ORDERED:
19304      stmt = cp_parser_omp_ordered (parser, pragma_tok);
19305      break;
19306    case PRAGMA_OMP_PARALLEL:
19307      stmt = cp_parser_omp_parallel (parser, pragma_tok);
19308      break;
19309    case PRAGMA_OMP_SECTIONS:
19310      stmt = cp_parser_omp_sections (parser, pragma_tok);
19311      break;
19312    case PRAGMA_OMP_SINGLE:
19313      stmt = cp_parser_omp_single (parser, pragma_tok);
19314      break;
19315    default:
19316      gcc_unreachable ();
19317    }
19318
19319  if (stmt)
19320    SET_EXPR_LOCATION (stmt, pragma_tok->location);
19321}
19322
19323/* The parser.  */
19324
19325static GTY (()) cp_parser *the_parser;
19326
19327
19328/* Special handling for the first token or line in the file.  The first
19329   thing in the file might be #pragma GCC pch_preprocess, which loads a
19330   PCH file, which is a GC collection point.  So we need to handle this
19331   first pragma without benefit of an existing lexer structure.
19332
19333   Always returns one token to the caller in *FIRST_TOKEN.  This is
19334   either the true first token of the file, or the first token after
19335   the initial pragma.  */
19336
19337static void
19338cp_parser_initial_pragma (cp_token *first_token)
19339{
19340  tree name = NULL;
19341
19342  cp_lexer_get_preprocessor_token (NULL, first_token);
19343  if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19344    return;
19345
19346  cp_lexer_get_preprocessor_token (NULL, first_token);
19347  if (first_token->type == CPP_STRING)
19348    {
19349      name = first_token->u.value;
19350
19351      cp_lexer_get_preprocessor_token (NULL, first_token);
19352      if (first_token->type != CPP_PRAGMA_EOL)
19353	error ("junk at end of %<#pragma GCC pch_preprocess%>");
19354    }
19355  else
19356    error ("expected string literal");
19357
19358  /* Skip to the end of the pragma.  */
19359  while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19360    cp_lexer_get_preprocessor_token (NULL, first_token);
19361
19362  /* Now actually load the PCH file.  */
19363  if (name)
19364    c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19365
19366  /* Read one more token to return to our caller.  We have to do this
19367     after reading the PCH file in, since its pointers have to be
19368     live.  */
19369  cp_lexer_get_preprocessor_token (NULL, first_token);
19370}
19371
19372/* Normal parsing of a pragma token.  Here we can (and must) use the
19373   regular lexer.  */
19374
19375static bool
19376cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19377{
19378  cp_token *pragma_tok;
19379  unsigned int id;
19380
19381  pragma_tok = cp_lexer_consume_token (parser->lexer);
19382  gcc_assert (pragma_tok->type == CPP_PRAGMA);
19383  parser->lexer->in_pragma = true;
19384
19385  id = pragma_tok->pragma_kind;
19386  switch (id)
19387    {
19388    case PRAGMA_GCC_PCH_PREPROCESS:
19389      error ("%<#pragma GCC pch_preprocess%> must be first");
19390      break;
19391
19392    case PRAGMA_OMP_BARRIER:
19393      switch (context)
19394	{
19395	case pragma_compound:
19396	  cp_parser_omp_barrier (parser, pragma_tok);
19397	  return false;
19398	case pragma_stmt:
19399	  error ("%<#pragma omp barrier%> may only be "
19400		 "used in compound statements");
19401	  break;
19402	default:
19403	  goto bad_stmt;
19404	}
19405      break;
19406
19407    case PRAGMA_OMP_FLUSH:
19408      switch (context)
19409	{
19410	case pragma_compound:
19411	  cp_parser_omp_flush (parser, pragma_tok);
19412	  return false;
19413	case pragma_stmt:
19414	  error ("%<#pragma omp flush%> may only be "
19415		 "used in compound statements");
19416	  break;
19417	default:
19418	  goto bad_stmt;
19419	}
19420      break;
19421
19422    case PRAGMA_OMP_THREADPRIVATE:
19423      cp_parser_omp_threadprivate (parser, pragma_tok);
19424      return false;
19425
19426    case PRAGMA_OMP_ATOMIC:
19427    case PRAGMA_OMP_CRITICAL:
19428    case PRAGMA_OMP_FOR:
19429    case PRAGMA_OMP_MASTER:
19430    case PRAGMA_OMP_ORDERED:
19431    case PRAGMA_OMP_PARALLEL:
19432    case PRAGMA_OMP_SECTIONS:
19433    case PRAGMA_OMP_SINGLE:
19434      if (context == pragma_external)
19435	goto bad_stmt;
19436      cp_parser_omp_construct (parser, pragma_tok);
19437      return true;
19438
19439    case PRAGMA_OMP_SECTION:
19440      error ("%<#pragma omp section%> may only be used in "
19441	     "%<#pragma omp sections%> construct");
19442      break;
19443
19444    default:
19445      gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19446      c_invoke_pragma_handler (id);
19447      break;
19448
19449    bad_stmt:
19450      cp_parser_error (parser, "expected declaration specifiers");
19451      break;
19452    }
19453
19454  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19455  return false;
19456}
19457
19458/* The interface the pragma parsers have to the lexer.  */
19459
19460enum cpp_ttype
19461pragma_lex (tree *value)
19462{
19463  cp_token *tok;
19464  enum cpp_ttype ret;
19465
19466  tok = cp_lexer_peek_token (the_parser->lexer);
19467
19468  ret = tok->type;
19469  *value = tok->u.value;
19470
19471  if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19472    ret = CPP_EOF;
19473  else if (ret == CPP_STRING)
19474    *value = cp_parser_string_literal (the_parser, false, false);
19475  else
19476    {
19477      cp_lexer_consume_token (the_parser->lexer);
19478      if (ret == CPP_KEYWORD)
19479	ret = CPP_NAME;
19480    }
19481
19482  return ret;
19483}
19484
19485
19486/* External interface.  */
19487
19488/* Parse one entire translation unit.  */
19489
19490void
19491c_parse_file (void)
19492{
19493  bool error_occurred;
19494  static bool already_called = false;
19495
19496  if (already_called)
19497    {
19498      sorry ("inter-module optimizations not implemented for C++");
19499      return;
19500    }
19501  already_called = true;
19502
19503  the_parser = cp_parser_new ();
19504  push_deferring_access_checks (flag_access_control
19505				? dk_no_deferred : dk_no_check);
19506  error_occurred = cp_parser_translation_unit (the_parser);
19507  the_parser = NULL;
19508}
19509
19510/* This variable must be provided by every front end.  */
19511
19512int yydebug;
19513
19514#include "gt-cp-parser.h"
19515